2 #include <QtCore/QTextStream>
3 #include <QtGui/QApplication>
4 #include <QtGui/QMenuBar>
5 #include <QtGui/QFileDialog>
6 #include <QtGui/QColor>
7 #include <QtGui/QPalette>
8 #include <QtGui/QCloseEvent>
9 #include <QtGui/QTextDocument>
10 #include <QtGui/QMainWindow>
11 #include <QtGui/QStatusBar>
12 #include <QtGui/QCheckBox>
13 #include <QtGui/QVBoxLayout>
14 #include <QtGui/QListWidget>
15 #include <QtGui/QListWidgetItem>
17 #include "ProgramStructureDialog.h"
18 #include "UnitStructureDialog.h"
19 #include "PreferencesDialog.h"
24 * Editor constructor. Initializes and sets variables of Loglan Editor.
26 * @param argc command line argc parameter
27 * @param argv command line argv parameter
29 Editor::Editor(int argc, char **argv)
34 strcpy(HomeDir, argv[1]);
40 compiler_path.sprintf("%s/%s", HomeDir, "compile/logcomp");
41 gen_path.sprintf("%s/%s", HomeDir, "compile/gen");
42 file_path.sprintf("%s", HomeDir);
45 connect(editor, SIGNAL(cursorPositionChanged()), this, SLOT(on_editor_cursorPositionChanged()));
56 * Displays line:column position of cursor
58 void Editor::on_editor_cursorPositionChanged()
64 cx = editor->textCursor().blockNumber();
65 cy = editor->textCursor().columnNumber();
66 position.sprintf("%d:%d", cx, cy);
68 statusBar()->showMessage(position);
72 * Loads given file content to the editor.
74 * @param fileName Filename of file which will be read.
76 void Editor::load(const char *fileName)
78 fname.sprintf("%s", fileName);
81 if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
86 QTextStream textStream(&file);
87 while (!file.atEnd()) {
88 editor->append(textStream.readLine());
94 setWindowTitle(fileName);
98 * Saves editor content to the given filename.
99 * @param fileName File name of the file where content should be saved.
101 void Editor::save(const char *fileName)
103 QFile file(fileName);
104 if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
108 file.write(editor->toPlainText().toAscii().data(), editor->toPlainText().length());
113 * Invokes compiler in the giving mode.
114 * @param mode Mode of compilation. Possible values:
115 * - COMP_MODE - compilation mode
116 * - GEN_MODE - program generation mode
117 * - ALL_MODE - compilation & generation mode.
119 void Editor::compile(int mode)
122 messages->setReadOnly(FALSE);
126 /*i = fname.find('.');*/
132 sprintf(cmd, "%s %s > comp_data!", compiler_path.toStdString().c_str(),
133 fname.toStdString().c_str());
136 sprintf(cmd, "%s %s > comp_data!", gen_path.toStdString().c_str(),
137 fname.toStdString().c_str());
140 sprintf(cmd, "%s %s > comp_data!", compiler_path.toStdString().c_str(),
141 fname.toStdString().c_str());
143 sprintf(cmd, "%s %s >> comp_data!", gen_path.toStdString().c_str(),
144 fname.toStdString().c_str());
149 QFile f("comp_data!");
150 if (!f.open(QIODevice::ReadOnly | QIODevice::Text))
155 QString s = t.readLine();
160 messages->setReadOnly(TRUE);
163 unlink("comp_data!");
168 * Empties editor content.
170 void Editor::on_actionNew_triggered()
172 on_actionClear_all_triggered();
173 fname.sprintf("%s", "");}
176 * Displays additional window
178 void Editor::on_actionOpen_triggered()
180 QString fn = QFileDialog::getOpenFileName(this, "Load file", file_path, "*.log");
182 load(fn.toAscii().data());
186 * Saves editor content to the file.
187 * If content has been read from file, it is written to this file. Otherwise
188 * dialog is shown to save content to the specified by user, file.
190 void Editor::on_actionSave_triggered()
192 if (fname.isEmpty()) {
193 QString fn = QFileDialog::getSaveFileName(this, "Save file",
196 fname.sprintf("%s", fn.toAscii().data());
197 save(fn.toAscii().data());
200 save(fname.toAscii().data());
202 setWindowTitle(fname);
206 * Saves editor content to the file.
207 * Forces saving editor content to the new file. Special dialog is shown for
210 void Editor::on_actionSave_as_triggered()
212 QString fn = QFileDialog::getSaveFileName(this, "Save file as",
215 fname.sprintf("%s", fn.toAscii().data());
216 save(fn.toAscii().data());
218 setWindowTitle(fname);
220 void Editor::on_actionQuit_triggered()
222 QApplication::instance()->quit();
224 void Editor::on_actionCopy_triggered()
228 void Editor::on_actionPaste_triggered()
232 void Editor::on_actionCut_triggered()
236 void Editor::on_actionClear_all_triggered()
242 * Searches for given text in editor content.
243 * Displays window to set search parameters. If text is found sets cursor
246 void Editor::on_actionFind_triggered()
248 QDialog dlg(this, Qt::Dialog);
250 QLineEdit *tmpQLineEdit;
251 tmpQLineEdit = new QLineEdit("", &dlg);
252 tmpQLineEdit->setGeometry(60, 10, 180, 30);
255 tmpQLabel = new QLabel(&dlg);
256 tmpQLabel->setGeometry(10, 10, 50, 30);
258 tmpQLabel->setText("Text:");
260 QCheckBox *tmpQRadioButton;
261 tmpQRadioButton = new QCheckBox("Case sensitive", &dlg);
262 tmpQRadioButton->setGeometry(70, 50, 150, 30);
263 tmpQRadioButton->setAutoRepeat(FALSE);
265 QPushButton *okbtn, *cbtn;
266 okbtn = new QPushButton("Find", &dlg);
267 okbtn->setGeometry(260, 10, 100, 30);
268 okbtn->setDefault(TRUE);
269 connect(okbtn,SIGNAL(clicked()), &dlg, SLOT(accept()));
271 cbtn = new QPushButton("Close", &dlg);
272 cbtn->setGeometry(260, 50, 100, 30);
273 connect(cbtn, SIGNAL(clicked()), &dlg, SLOT(reject()));
278 sensitive = tmpQRadioButton->isChecked();
279 find_text = tmpQLineEdit->text();
281 QTextDocument::FindFlags flags = 0;
284 flags |= QTextDocument::FindCaseSensitively;
286 editor->find(find_text, flags);
291 * Searches for next occurence of given text in editor content.
292 * Displays window to set search parameters. If text is found sets cursor
295 void Editor::on_actionFind_Next_triggered()
297 if (!find_text.isEmpty()) {
298 QTextDocument::FindFlags flags = 0;
301 flags |= QTextDocument::FindCaseSensitively;
303 editor->find(find_text, flags);
308 * Displays window with editor properties
310 void Editor::on_actionPreferences_triggered()
312 PreferencesDialog dialog(this);
314 dialog.setCompilerPath(compiler_path);
315 dialog.setFilesPath(file_path);
316 dialog.setGenPath(gen_path);
319 compiler_path.sprintf("%s", dialog.getCompilerPath().toStdString().c_str());
320 gen_path.sprintf("%s",dialog.getGenPath().toStdString().c_str());
321 file_path.sprintf("%s", dialog.getFilesPath().toStdString().c_str());
326 * Saves and compiles code.
328 void Editor::on_actionCompile_triggered()
330 on_actionSave_triggered();
337 void Editor::on_actionGen_triggered()
343 * Saves, compiles and generates code.
345 void Editor::on_actionCompile_Gen_triggered()
347 on_actionSave_triggered();
351 void Editor::on_actionProgram_structure_triggered()
353 ProgramStructureDialog dialog(this);
356 editor->textCursor().insertText(dialog.getCode());
360 void Editor::on_actionUnit_structure_triggered()
362 UnitStructureDialog dialog(this);
365 editor->textCursor().insertText(dialog.getCode());
370 * Program main function.
371 * argv[1] is mandatory and should be a path to the home directory of
372 * application (same as in configuration file).
374 * @param argc Number of program arguments
375 * @param argv Program arguments
377 int main(int argc, char **argv)
379 QApplication app(argc, argv);
380 Editor * editor = new Editor(argc, argv);