ae59c62df4eb97ecf29f885e07809c785830361b
[vlp.git] / src / edit / editor.cpp
1 #include <unistd.h>
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/QTextDocument>
9 #include <QtGui/QMainWindow>
10 #include <QtGui/QStatusBar>
11 #include <QtGui/QCheckBox>
12 #include <QtGui/QVBoxLayout>
13 #include <QtGui/QListWidget>
14 #include <QtGui/QListWidgetItem>
15
16 #include "ProgramStructureDialog.h"
17 #include "UnitStructureDialog.h"
18 #include "PreferencesDialog.h"
19 #include "FindDialog.h"
20
21 #include "editor.h"
22
23 namespace loglan {
24 namespace vlp {
25
26 /**
27  * Editor constructor. Initializes and sets variables of Loglan Editor.
28  *
29  * @param argc command line argc parameter
30  * @param argv command line argv parameter
31  */
32 Editor::Editor(int argc, char **argv)
33 {
34         setupUi(this);
35
36         if (argc > 1) {
37                 strcpy(HomeDir, argv[1]);
38                 fprintf(stderr, "EDIT: HomeDir inited with: %s (%s)\n", HomeDir, argv[1]);
39         }
40
41         find_text = "";
42         sensitive = FALSE;
43
44         compiler_path.sprintf("%s/%s", HomeDir, "compile/logcomp");
45         gen_path.sprintf("%s/%s", HomeDir, "compile/gen");
46         file_path.sprintf("%s", HomeDir);
47         
48         
49         connect(editor, SIGNAL(cursorPositionChanged()), this, SLOT(on_editor_cursorPositionChanged()));
50 }
51
52 /**
53  * Editor destructor
54  */
55 Editor::~Editor()
56 {
57 }
58
59 /**
60  * Displays line:column position of cursor
61  */
62 void Editor::on_editor_cursorPositionChanged()
63 {
64         QString position;
65         int cx;
66         int cy;
67
68         cx = editor->textCursor().blockNumber();
69         cy = editor->textCursor().columnNumber();
70         position.sprintf("%d:%d", cx, cy);
71
72         statusBar()->showMessage(position);
73 }
74
75 /**
76  * Loads given file content to the editor.
77  *
78  * @param fileName Filename of file which will be read.
79  */
80 void Editor::load(const char *fileName)
81 {
82         fname.sprintf("%s", fileName);
83
84         QFile file(fileName);
85         if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
86                 return;
87
88         editor->clear();
89
90         QTextStream textStream(&file);
91         while (!file.atEnd()) {
92                 editor->append(textStream.readLine());
93         }
94
95         file.close();
96
97         editor->repaint();
98         setWindowTitle(fileName);
99 }
100
101 /**
102  * Saves editor content to the given filename.
103  * @param fileName File name of the file where content should be saved.
104  */
105 void Editor::save(const char *fileName)
106 {
107         QFile file(fileName);
108         if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
109                 return;
110
111         file.reset();
112         file.write(editor->toPlainText().toAscii().data(), editor->toPlainText().length());
113         file.close();
114 }
115
116 /**
117  * Invokes compiler in the giving mode.
118  * @param mode Mode of compilation. Possible values:
119  *             - COMP_MODE - compilation mode
120  *             - GEN_MODE - program generation mode
121  *             - ALL_MODE - compilation & generation mode.
122  */
123 void Editor::compile(int mode)
124 {
125         char cmd[255];
126         messages->setReadOnly(FALSE);
127         messages->clear();
128         messages->repaint();
129
130         /*i = fname.find('.');*/
131         /* if (i>=0) {*/
132
133         /*  fn.truncate(i);*/
134         switch(mode) {
135         case COMP_MODE:
136                 sprintf(cmd, "%s %s > comp_data!", compiler_path.toStdString().c_str(),
137                                                         fname.toStdString().c_str());
138                 break;
139         case GEN_MODE:
140                 sprintf(cmd, "%s %s > comp_data!", gen_path.toStdString().c_str(),
141                                                         fname.toStdString().c_str());
142                 break;
143         case ALL_MODE:
144                 sprintf(cmd, "%s %s > comp_data!", compiler_path.toStdString().c_str(),
145                                                         fname.toStdString().c_str());
146                 system(cmd);
147                 sprintf(cmd, "%s %s >> comp_data!", gen_path.toStdString().c_str(),
148                                                         fname.toStdString().c_str());
149         break;
150         }
151
152         system(cmd);
153         QFile f("comp_data!");
154         if (!f.open(QIODevice::ReadOnly | QIODevice::Text))
155                 return;
156
157         QTextStream t(&f);
158         while (!t.atEnd()) {
159                 QString s = t.readLine();
160                 messages->append(s);
161         }
162         f.close();
163
164         messages->setReadOnly(TRUE);
165
166         messages->repaint();
167         unlink("comp_data!");
168         /*}*/
169 }
170
171 /**
172  * Empties editor content.
173  */
174 void Editor::on_actionNew_triggered()
175 {
176         on_actionClear_all_triggered();
177         fname.sprintf("%s", "");}
178
179 /**
180  * Displays additional window 
181  */
182 void Editor::on_actionOpen_triggered()
183 {
184         QString fn = QFileDialog::getOpenFileName(this, "Load file", file_path, "*.log");
185         if (!fn.isEmpty())
186                 load(fn.toAscii().data());
187 }
188
189 /**
190  * Saves editor content to the file.
191  * If content has been read from file, it is written to this file. Otherwise
192  * dialog is shown to save content to the specified by user, file.
193  */
194 void Editor::on_actionSave_triggered()
195 {
196         if (fname.isEmpty()) {
197                 QString fn = QFileDialog::getSaveFileName(this, "Save file", 
198                                                         file_path, "*.log");
199                 if (!fn.isEmpty()) {
200                         fname.sprintf("%s", fn.toAscii().data());
201                         save(fn.toAscii().data());
202                 }
203         } else {
204                 save(fname.toAscii().data());
205         }
206         setWindowTitle(fname);
207 }
208
209 /**
210  * Saves editor content to the file.
211  * Forces saving editor content to the new file. Special dialog is shown for
212  * that purpose.
213  */
214 void Editor::on_actionSave_as_triggered()
215 {
216         QString fn = QFileDialog::getSaveFileName(this, "Save file as",
217                                                         file_path, "*.log");
218         if (!fn.isEmpty()) {
219                 fname.sprintf("%s", fn.toAscii().data());
220                 save(fn.toAscii().data());
221         }
222         setWindowTitle(fname);
223 }
224 void Editor::on_actionQuit_triggered()
225 {
226         QApplication::instance()->quit();
227 }
228 void Editor::on_actionCopy_triggered()
229 {
230         editor->copy();
231 }
232 void Editor::on_actionPaste_triggered()
233 {
234         editor->paste();
235 }
236 void Editor::on_actionCut_triggered()
237 {
238         editor->cut();
239 }
240 void Editor::on_actionClear_all_triggered()
241 {
242         editor->clear();
243 }
244
245 /**
246  * Searches for given text in editor content.
247  * Displays window to set search parameters. If text is found sets cursor
248  * position on it.
249  */
250 void Editor::on_actionFind_triggered()
251 {
252         dialog::FindDialog dialog(this);
253
254         if (dialog.exec()) {
255                 sensitive = dialog.isCaseSensitive();
256                 find_text = dialog.getSearchText();
257                 
258                 QTextDocument::FindFlags flags = 0;
259                 
260                 if (sensitive) {
261                         flags |= QTextDocument::FindCaseSensitively;
262                 }
263
264                 editor->find(find_text, flags);
265         }
266 }
267
268 /**
269  * Searches for next occurence of given text in editor content.
270  * Displays window to set search parameters. If text is found sets cursor
271  * position on it.
272  */
273 void Editor::on_actionFind_Next_triggered()
274 {
275         if (!find_text.isEmpty()) {
276                 QTextDocument::FindFlags flags = 0;
277                 
278                 if (sensitive) {
279                         flags |= QTextDocument::FindCaseSensitively;
280                 }
281                 editor->find(find_text, flags);
282         }
283 }
284
285 /**
286  * Displays window with editor properties
287  */
288 void Editor::on_actionPreferences_triggered()
289 {
290         dialog::PreferencesDialog dialog(this);
291
292         dialog.setCompilerPath(compiler_path);
293         dialog.setFilesPath(file_path);
294         dialog.setGenPath(gen_path);
295
296         if (dialog.exec()) {
297                 compiler_path.sprintf("%s", dialog.getCompilerPath().toStdString().c_str());
298                 gen_path.sprintf("%s",dialog.getGenPath().toStdString().c_str());
299                 file_path.sprintf("%s", dialog.getFilesPath().toStdString().c_str());
300         };
301 }
302
303 /**
304  * Saves and compiles code.
305  */
306 void Editor::on_actionCompile_triggered()
307 {
308         on_actionSave_triggered();
309         compile(COMP_MODE);
310 }
311
312 /**
313  * Generates program.
314  */
315 void Editor::on_actionGen_triggered()
316 {
317         compile(GEN_MODE);
318 }
319
320 /**
321  * Saves, compiles and generates code.
322  */
323 void Editor::on_actionCompile_Gen_triggered()
324 {
325         on_actionSave_triggered();
326         compile(ALL_MODE);
327 }
328
329 void Editor::on_actionProgram_structure_triggered()
330 {
331         dialog::ProgramStructureDialog dialog(this);
332
333         if (dialog.exec()) {
334                 editor->textCursor().insertText(dialog.getCode());
335         }
336 }
337
338 void Editor::on_actionUnit_structure_triggered()
339 {
340         dialog::UnitStructureDialog dialog(this);
341
342         if (dialog.exec()) {
343                 editor->textCursor().insertText(dialog.getCode());
344         }
345 }
346
347 }
348 }
349
350 /**
351  * Program main function.
352  * argv[1] is mandatory and should be a path to the home directory of
353  * application (same as in configuration file).
354  *
355  * @param argc Number of program arguments
356  * @param argv Program arguments
357  */
358 int main(int argc, char **argv)
359 {
360         QApplication app(argc, argv);
361         loglan::vlp::Editor * editor = new loglan::vlp::Editor(argc, argv);
362         editor->show();
363         return app.exec();
364 }
365