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>
22 * @attention Currently not in use
24 const char *UnitTypes[TYPENUM] = {
33 * Editor constructor. Initializes and sets variables of Loglan Editor.
35 * @param argc command line argc parameter
36 * @param argv command line argv parameter
38 Editor::Editor(int argc, char **argv)
43 strcpy(HomeDir, argv[1]);
49 compiler_path.sprintf("%s/%s", HomeDir, "compile/logcomp");
50 gen_path.sprintf("%s/%s", HomeDir, "compile/gen");
51 file_path.sprintf("%s", HomeDir);
54 connect(editor, SIGNAL(cursorPositionChanged()), this, SLOT(on_editor_cursorPositionChanged()));
65 * Displays line:column position of cursor
67 void Editor::on_editor_cursorPositionChanged()
73 cx = editor->textCursor().blockNumber();
74 cy = editor->textCursor().columnNumber();
75 position.sprintf("%d:%d", cx, cy);
77 statusBar()->showMessage(position);
81 * Loads given file content to the editor.
83 * @param fileName Filename of file which will be read.
85 void Editor::load(const char *fileName)
87 fname.sprintf("%s", fileName);
90 if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
95 QTextStream textStream(&file);
96 while (!file.atEnd()) {
97 editor->append(textStream.readLine());
103 setWindowTitle(fileName);
107 * Saves editor content to the given filename.
108 * @param fileName File name of the file where content should be saved.
110 void Editor::save(const char *fileName)
112 QFile file(fileName);
113 if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
117 file.write(editor->toPlainText().toAscii().data(), editor->toPlainText().length());
122 * Invokes compiler in the giving mode.
123 * @param mode Mode of compilation. Possible values:
124 * - COMP_MODE - compilation mode
125 * - GEN_MODE - program generation mode
126 * - ALL_MODE - compilation & generation mode.
128 void Editor::compile(int mode)
131 messages->setReadOnly(FALSE);
135 /*i = fname.find('.');*/
141 sprintf(cmd, "%s %s > comp_data!", compiler_path.toAscii().data(),
142 fname.toAscii().data());
145 sprintf(cmd, "%s %s > comp_data!", gen_path.toAscii().data(),
146 fname.toAscii().data());
149 sprintf(cmd, "%s %s > comp_data!", compiler_path.toAscii().data(),
150 fname.toAscii().data());
152 sprintf(cmd, "%s %s >> comp_data!", gen_path.toAscii().data(),
153 fname.toAscii().data());
158 QFile f("comp_data!");
159 if (!f.open(QIODevice::ReadOnly | QIODevice::Text))
164 QString s = t.readLine();
169 messages->setReadOnly(TRUE);
172 unlink("comp_data!");
177 * Empties editor content.
179 void Editor::on_actionNew_triggered()
181 on_actionClear_all_triggered();
182 fname.sprintf("%s", "");}
185 * Displays additional window
187 void Editor::on_actionOpen_triggered()
189 QString fn = QFileDialog::getOpenFileName(this, "Load file", file_path, "*.log");
191 load(fn.toAscii().data());
195 * Saves editor content to the file.
196 * If content has been read from file, it is written to this file. Otherwise
197 * dialog is shown to save content to the specified by user, file.
199 void Editor::on_actionSave_triggered()
201 if (fname.isEmpty()) {
202 QString fn = QFileDialog::getSaveFileName(this, "Save file",
205 fname.sprintf("%s", fn.toAscii().data());
206 save(fn.toAscii().data());
209 save(fname.toAscii().data());
211 setWindowTitle(fname);
215 * Saves editor content to the file.
216 * Forces saving editor content to the new file. Special dialog is shown for
219 void Editor::on_actionSave_as_triggered()
221 QString fn = QFileDialog::getSaveFileName(this, "Save file as",
224 fname.sprintf("%s", fn.toAscii().data());
225 save(fn.toAscii().data());
227 setWindowTitle(fname);
229 void Editor::on_actionQuit_triggered()
231 QApplication::instance()->quit();
233 void Editor::on_actionCopy_triggered()
237 void Editor::on_actionPaste_triggered()
241 void Editor::on_actionCut_triggered()
245 void Editor::on_actionClear_all_triggered()
251 * Searches for given text in editor content.
252 * Displays window to set search parameters. If text is found sets cursor
255 void Editor::on_actionFind_triggered()
257 QDialog dlg(this, Qt::Dialog);
259 QLineEdit *tmpQLineEdit;
260 tmpQLineEdit = new QLineEdit("", &dlg);
261 tmpQLineEdit->setGeometry(60, 10, 180, 30);
264 tmpQLabel = new QLabel(&dlg);
265 tmpQLabel->setGeometry(10, 10, 50, 30);
267 tmpQLabel->setText("Text:");
269 QCheckBox *tmpQRadioButton;
270 tmpQRadioButton = new QCheckBox("Case sensitive", &dlg);
271 tmpQRadioButton->setGeometry(70, 50, 150, 30);
272 tmpQRadioButton->setAutoRepeat(FALSE);
274 QPushButton *okbtn, *cbtn;
275 okbtn = new QPushButton("Find", &dlg);
276 okbtn->setGeometry(260, 10, 100, 30);
277 okbtn->setDefault(TRUE);
278 connect(okbtn,SIGNAL(clicked()), &dlg, SLOT(accept()));
280 cbtn = new QPushButton("Close", &dlg);
281 cbtn->setGeometry(260, 50, 100, 30);
282 connect(cbtn, SIGNAL(clicked()), &dlg, SLOT(reject()));
287 sensitive = tmpQRadioButton->isChecked();
288 find_text = tmpQLineEdit->text();
290 QTextDocument::FindFlags flags = 0;
293 flags |= QTextDocument::FindCaseSensitively;
295 editor->find(find_text, flags);
300 * Searches for next occurence of given text in editor content.
301 * Displays window to set search parameters. If text is found sets cursor
304 void Editor::on_actionFind_Next_triggered()
306 if (!find_text.isEmpty()) {
307 QTextDocument::FindFlags flags = 0;
310 flags |= QTextDocument::FindCaseSensitively;
312 editor->find(find_text, flags);
317 * Displays window with editor properties
319 void Editor::on_actionPreferences_triggered()
321 QDialog dlg(this, Qt::Dialog);
324 files = new QLineEdit(&dlg);
325 files->setGeometry(130, 20, 250, 30);
326 files->setText(file_path);
327 files->setMaxLength(32767);
328 files->setEchoMode(QLineEdit::Normal);
329 files->setFrame(TRUE);
332 tmpQLabel = new QLabel("Path to files:", &dlg);
333 tmpQLabel->setGeometry(10, 20, 100, 30);
334 tmpQLabel->setAlignment(Qt::AlignLeft);
335 tmpQLabel->setMargin(-1);
337 tmpQLabel = new QLabel("Path to compiler:", &dlg);
338 tmpQLabel->setGeometry(10, 60, 100, 30);
339 tmpQLabel->setAlignment(Qt::AlignLeft);
340 tmpQLabel->setMargin(-1);
343 tmpQLabel = new QLabel(&dlg, "Label_3");
344 tmpQLabel->setGeometry(10, 100, 100, 30);
345 tmpQLabel->setText("Path to gen:");
346 tmpQLabel->setAlignment(289);
347 tmpQLabel->setMargin(-1);
351 compp = new QLineEdit(compiler_path, &dlg);
352 compp->setGeometry(130, 60, 250, 30);
353 compp->setMaxLength(32767);
354 compp->setEchoMode(QLineEdit::Normal);
355 compp->setFrame(TRUE);
359 genp = new QLineEdit(&dlg, "g_path");
360 genp->setGeometry(130, 100, 250, 30);
361 genp->setText(gen_path.data());
362 genp->setMaxLength(32767);
363 genp->setEchoMode(QLineEdit::Normal);
364 genp->setFrame(TRUE);
367 QPushButton* tmpQPushButton;
368 tmpQPushButton = new QPushButton("Ok", &dlg);
369 tmpQPushButton->setGeometry(90, 100, 70, 30);
370 tmpQPushButton->setAutoRepeat(FALSE);
371 connect(tmpQPushButton,SIGNAL(clicked()), &dlg, SLOT(accept()));
373 tmpQPushButton = new QPushButton("Cancel", &dlg);
374 tmpQPushButton->setGeometry(180, 100, 70, 30);
375 tmpQPushButton->setAutoRepeat(FALSE);
376 connect(tmpQPushButton,SIGNAL(clicked()), &dlg, SLOT(reject()));
378 dlg.resize(400, 140);
381 compiler_path.sprintf("%s", compp->text().toAscii().data());
382 /* gen_path.sprintf("%s",genp->text()); */
383 file_path.sprintf("%s", files->text().toAscii().data());
388 * Saves and compiles code.
390 void Editor::on_actionCompile_triggered()
392 on_actionSave_triggered();
399 void Editor::on_actionGen_triggered()
405 * Saves, compiles and generates code.
407 void Editor::on_actionCompile_Gen_triggered()
409 on_actionSave_triggered();
413 void Editor::on_actionProgram_structure_triggered()
415 QDialog dlg(this, "unit", TRUE);
419 files = new QLineEdit(&dlg, "f_path");
420 files->setGeometry(130, 20, 250, 30);
422 files->setMaxLength(32767);
423 files->setEchoMode(QLineEdit::Normal);
424 files->setFrame(TRUE);
427 tmpQLabel = new QLabel(&dlg);
428 tmpQLabel->setGeometry(10, 20, 100, 30);
429 tmpQLabel->setText("Program name:");
431 QPushButton* tmpQPushButton;
432 tmpQPushButton = new QPushButton(&dlg);
433 tmpQPushButton->setGeometry(40, 70, 70, 30);
434 tmpQPushButton->setText("Ok");
435 tmpQPushButton->setAutoRepeat(FALSE);
436 connect(tmpQPushButton,SIGNAL(clicked()), &dlg, SLOT(accept()));
438 tmpQPushButton = new QPushButton(&dlg);
439 tmpQPushButton->setGeometry(130, 70, 100, 30);
440 tmpQPushButton->setText("Cancel");
441 tmpQPushButton->setAutoRepeat(FALSE);
442 connect(tmpQPushButton,SIGNAL(clicked()), &dlg, SLOT(reject()));
445 strcpy(uname, files->text());
448 txt.sprintf("PROGRAM %s\n\nBEGIN\n\nEND", uname);
450 editor->textCursor().insertText(txt);
454 void Editor::on_actionUnit_structure_triggered()
456 QDialog dlg(this, Qt::Dialog);
461 files = new QLineEdit(&dlg, "f_path");
462 files->setGeometry(130, 20, 250, 30);
464 files->setMaxLength(32767);
465 files->setEchoMode(QLineEdit::Normal);
466 files->setFrame(TRUE);
469 tmpQLabel = new QLabel(&dlg);
470 tmpQLabel->setGeometry(10, 20, 100, 30);
471 tmpQLabel->setText("Unit name:");
472 tmpQLabel->setAlignment(289);
473 tmpQLabel->setMargin(-1);
475 QPushButton* tmpQPushButton;
476 tmpQPushButton = new QPushButton(&dlg);
477 tmpQPushButton->setGeometry(40, 170, 70, 30);
478 tmpQPushButton->setText("Ok");
479 tmpQPushButton->setAutoRepeat(FALSE);
480 connect(tmpQPushButton,SIGNAL(clicked()), &dlg, SLOT(accept()));
482 tmpQPushButton = new QPushButton(&dlg);
483 tmpQPushButton->setGeometry(130, 170, 100, 30);
484 tmpQPushButton->setText("Cancel");
485 tmpQPushButton->setAutoRepeat(FALSE);
486 connect(tmpQPushButton,SIGNAL(clicked()), &dlg, SLOT(reject()));
488 tmpQLabel = new QLabel(&dlg);
489 tmpQLabel->setGeometry(10, 50, 100, 60);
490 tmpQLabel->setText("Unit type:");
492 QListWidget *listWidget = new QListWidget(&dlg);
493 for(i = 0; i < TYPENUM; i++) {
494 QListWidgetItem *newItem = new QListWidgetItem;
495 newItem->setText(UnitTypes[i]);
496 listWidget->insertItem(i, newItem);
498 listWidget->setCurrentItem(0);
501 strcpy(uname, files->text());
504 txt.sprintf("UNIT %s : %s( <params> );\nBEGIN\n\nEND %s;",
506 listWidget->currentItem()->text().toStdString().c_str(),
510 editor->textCursor().insertText(txt);
515 * Program main function.
516 * argv[1] is mandatory and should be a path to the home directory of
517 * application (same as in configuration file).
519 * @param argc Number of program arguments
520 * @param argv Program arguments
522 int main(int argc, char **argv)
524 QApplication app(argc, argv);
525 Editor * editor = new Editor(argc, argv);