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"
23 * Editor constructor. Initializes and sets variables of Loglan Editor.
25 * @param argc command line argc parameter
26 * @param argv command line argv parameter
28 Editor::Editor(int argc, char **argv)
33 strcpy(HomeDir, argv[1]);
39 compiler_path.sprintf("%s/%s", HomeDir, "compile/logcomp");
40 gen_path.sprintf("%s/%s", HomeDir, "compile/gen");
41 file_path.sprintf("%s", HomeDir);
44 connect(editor, SIGNAL(cursorPositionChanged()), this, SLOT(on_editor_cursorPositionChanged()));
55 * Displays line:column position of cursor
57 void Editor::on_editor_cursorPositionChanged()
63 cx = editor->textCursor().blockNumber();
64 cy = editor->textCursor().columnNumber();
65 position.sprintf("%d:%d", cx, cy);
67 statusBar()->showMessage(position);
71 * Loads given file content to the editor.
73 * @param fileName Filename of file which will be read.
75 void Editor::load(const char *fileName)
77 fname.sprintf("%s", fileName);
80 if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
85 QTextStream textStream(&file);
86 while (!file.atEnd()) {
87 editor->append(textStream.readLine());
93 setWindowTitle(fileName);
97 * Saves editor content to the given filename.
98 * @param fileName File name of the file where content should be saved.
100 void Editor::save(const char *fileName)
102 QFile file(fileName);
103 if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
107 file.write(editor->toPlainText().toAscii().data(), editor->toPlainText().length());
112 * Invokes compiler in the giving mode.
113 * @param mode Mode of compilation. Possible values:
114 * - COMP_MODE - compilation mode
115 * - GEN_MODE - program generation mode
116 * - ALL_MODE - compilation & generation mode.
118 void Editor::compile(int mode)
121 messages->setReadOnly(FALSE);
125 /*i = fname.find('.');*/
131 sprintf(cmd, "%s %s > comp_data!", compiler_path.toAscii().data(),
132 fname.toAscii().data());
135 sprintf(cmd, "%s %s > comp_data!", gen_path.toAscii().data(),
136 fname.toAscii().data());
139 sprintf(cmd, "%s %s > comp_data!", compiler_path.toAscii().data(),
140 fname.toAscii().data());
142 sprintf(cmd, "%s %s >> comp_data!", gen_path.toAscii().data(),
143 fname.toAscii().data());
148 QFile f("comp_data!");
149 if (!f.open(QIODevice::ReadOnly | QIODevice::Text))
154 QString s = t.readLine();
159 messages->setReadOnly(TRUE);
162 unlink("comp_data!");
167 * Empties editor content.
169 void Editor::on_actionNew_triggered()
171 on_actionClear_all_triggered();
172 fname.sprintf("%s", "");}
175 * Displays additional window
177 void Editor::on_actionOpen_triggered()
179 QString fn = QFileDialog::getOpenFileName(this, "Load file", file_path, "*.log");
181 load(fn.toAscii().data());
185 * Saves editor content to the file.
186 * If content has been read from file, it is written to this file. Otherwise
187 * dialog is shown to save content to the specified by user, file.
189 void Editor::on_actionSave_triggered()
191 if (fname.isEmpty()) {
192 QString fn = QFileDialog::getSaveFileName(this, "Save file",
195 fname.sprintf("%s", fn.toAscii().data());
196 save(fn.toAscii().data());
199 save(fname.toAscii().data());
201 setWindowTitle(fname);
205 * Saves editor content to the file.
206 * Forces saving editor content to the new file. Special dialog is shown for
209 void Editor::on_actionSave_as_triggered()
211 QString fn = QFileDialog::getSaveFileName(this, "Save file as",
214 fname.sprintf("%s", fn.toAscii().data());
215 save(fn.toAscii().data());
217 setWindowTitle(fname);
219 void Editor::on_actionQuit_triggered()
221 QApplication::instance()->quit();
223 void Editor::on_actionCopy_triggered()
227 void Editor::on_actionPaste_triggered()
231 void Editor::on_actionCut_triggered()
235 void Editor::on_actionClear_all_triggered()
241 * Searches for given text in editor content.
242 * Displays window to set search parameters. If text is found sets cursor
245 void Editor::on_actionFind_triggered()
247 QDialog dlg(this, Qt::Dialog);
249 QLineEdit *tmpQLineEdit;
250 tmpQLineEdit = new QLineEdit("", &dlg);
251 tmpQLineEdit->setGeometry(60, 10, 180, 30);
254 tmpQLabel = new QLabel(&dlg);
255 tmpQLabel->setGeometry(10, 10, 50, 30);
257 tmpQLabel->setText("Text:");
259 QCheckBox *tmpQRadioButton;
260 tmpQRadioButton = new QCheckBox("Case sensitive", &dlg);
261 tmpQRadioButton->setGeometry(70, 50, 150, 30);
262 tmpQRadioButton->setAutoRepeat(FALSE);
264 QPushButton *okbtn, *cbtn;
265 okbtn = new QPushButton("Find", &dlg);
266 okbtn->setGeometry(260, 10, 100, 30);
267 okbtn->setDefault(TRUE);
268 connect(okbtn,SIGNAL(clicked()), &dlg, SLOT(accept()));
270 cbtn = new QPushButton("Close", &dlg);
271 cbtn->setGeometry(260, 50, 100, 30);
272 connect(cbtn, SIGNAL(clicked()), &dlg, SLOT(reject()));
277 sensitive = tmpQRadioButton->isChecked();
278 find_text = tmpQLineEdit->text();
280 QTextDocument::FindFlags flags = 0;
283 flags |= QTextDocument::FindCaseSensitively;
285 editor->find(find_text, flags);
290 * Searches for next occurence of given text in editor content.
291 * Displays window to set search parameters. If text is found sets cursor
294 void Editor::on_actionFind_Next_triggered()
296 if (!find_text.isEmpty()) {
297 QTextDocument::FindFlags flags = 0;
300 flags |= QTextDocument::FindCaseSensitively;
302 editor->find(find_text, flags);
307 * Displays window with editor properties
309 void Editor::on_actionPreferences_triggered()
311 QDialog dlg(this, Qt::Dialog);
314 files = new QLineEdit(&dlg);
315 files->setGeometry(130, 20, 250, 30);
316 files->setText(file_path);
317 files->setMaxLength(32767);
318 files->setEchoMode(QLineEdit::Normal);
319 files->setFrame(TRUE);
322 tmpQLabel = new QLabel("Path to files:", &dlg);
323 tmpQLabel->setGeometry(10, 20, 100, 30);
324 tmpQLabel->setAlignment(Qt::AlignLeft);
325 tmpQLabel->setMargin(-1);
327 tmpQLabel = new QLabel("Path to compiler:", &dlg);
328 tmpQLabel->setGeometry(10, 60, 100, 30);
329 tmpQLabel->setAlignment(Qt::AlignLeft);
330 tmpQLabel->setMargin(-1);
333 tmpQLabel = new QLabel(&dlg, "Label_3");
334 tmpQLabel->setGeometry(10, 100, 100, 30);
335 tmpQLabel->setText("Path to gen:");
336 tmpQLabel->setAlignment(289);
337 tmpQLabel->setMargin(-1);
341 compp = new QLineEdit(compiler_path, &dlg);
342 compp->setGeometry(130, 60, 250, 30);
343 compp->setMaxLength(32767);
344 compp->setEchoMode(QLineEdit::Normal);
345 compp->setFrame(TRUE);
349 genp = new QLineEdit(&dlg, "g_path");
350 genp->setGeometry(130, 100, 250, 30);
351 genp->setText(gen_path.data());
352 genp->setMaxLength(32767);
353 genp->setEchoMode(QLineEdit::Normal);
354 genp->setFrame(TRUE);
357 QPushButton* tmpQPushButton;
358 tmpQPushButton = new QPushButton("Ok", &dlg);
359 tmpQPushButton->setGeometry(90, 100, 70, 30);
360 tmpQPushButton->setAutoRepeat(FALSE);
361 connect(tmpQPushButton,SIGNAL(clicked()), &dlg, SLOT(accept()));
363 tmpQPushButton = new QPushButton("Cancel", &dlg);
364 tmpQPushButton->setGeometry(180, 100, 70, 30);
365 tmpQPushButton->setAutoRepeat(FALSE);
366 connect(tmpQPushButton,SIGNAL(clicked()), &dlg, SLOT(reject()));
368 dlg.resize(400, 140);
371 compiler_path.sprintf("%s", compp->text().toAscii().data());
372 /* gen_path.sprintf("%s",genp->text()); */
373 file_path.sprintf("%s", files->text().toAscii().data());
378 * Saves and compiles code.
380 void Editor::on_actionCompile_triggered()
382 on_actionSave_triggered();
389 void Editor::on_actionGen_triggered()
395 * Saves, compiles and generates code.
397 void Editor::on_actionCompile_Gen_triggered()
399 on_actionSave_triggered();
403 void Editor::on_actionProgram_structure_triggered()
405 ProgramStructureDialog dialog(this);
408 editor->textCursor().insertText(dialog.getCode());
412 void Editor::on_actionUnit_structure_triggered()
414 UnitStructureDialog dialog(this);
417 editor->textCursor().insertText(dialog.getCode());
422 * Program main function.
423 * argv[1] is mandatory and should be a path to the home directory of
424 * application (same as in configuration file).
426 * @param argc Number of program arguments
427 * @param argv Program arguments
429 int main(int argc, char **argv)
431 QApplication app(argc, argv);
432 Editor * editor = new Editor(argc, argv);