Mercurial > hg > octave-nkf
comparison gui/src/m-editor/file-editor.cc @ 14707:674740c44c09 gui
Changed various files to matche file naming conventions.
author | Jacob Dawid <jacob.dawid@googlemail.com> |
---|---|
date | Thu, 31 May 2012 22:19:53 +0200 |
parents | gui/src/editor/fileeditor.cc@f86884be20fc |
children | f50591409306 |
comparison
equal
deleted
inserted
replaced
14703:f86884be20fc | 14707:674740c44c09 |
---|---|
1 /* OctaveGUI - A graphical user interface for Octave | |
2 * Copyright (C) 2011 Jacob Dawid (jacob.dawid@googlemail.com) | |
3 * | |
4 * This program is free software: you can redistribute it and/or modify | |
5 * it under the terms of the GNU General Public License as | |
6 * published by the Free Software Foundation, either version 3 of the | |
7 * License, or (at your option) any later version. | |
8 * | |
9 * This program is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 * GNU General Public License for more details. | |
13 * | |
14 * You should have received a copy of the GNU General Public License | |
15 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 */ | |
17 | |
18 #include "file-editor.h" | |
19 #include <QVBoxLayout> | |
20 #include <QApplication> | |
21 #include <QFile> | |
22 #include <QFont> | |
23 #include <QFileDialog> | |
24 #include <QMessageBox> | |
25 #include <QStyle> | |
26 #include <QTextStream> | |
27 | |
28 FileEditor::FileEditor (QTerminal *terminal, MainWindow *mainWindow) | |
29 : FileEditorInterface(terminal, mainWindow) | |
30 { | |
31 construct (); | |
32 | |
33 m_terminal = terminal; | |
34 m_mainWindow = mainWindow; | |
35 setVisible (false); | |
36 } | |
37 | |
38 FileEditor::~FileEditor () | |
39 { | |
40 } | |
41 | |
42 LexerOctaveGui * | |
43 FileEditor::lexer () | |
44 { | |
45 return m_lexer; | |
46 } | |
47 | |
48 QTerminal * | |
49 FileEditor::terminal () | |
50 { | |
51 return m_terminal; | |
52 } | |
53 | |
54 MainWindow * | |
55 FileEditor::mainWindow () | |
56 { | |
57 return m_mainWindow; | |
58 } | |
59 | |
60 void | |
61 FileEditor::requestNewFile () | |
62 { | |
63 FileEditorTab *fileEditorTab = new FileEditorTab (this); | |
64 if (fileEditorTab) | |
65 { | |
66 addFileEditorTab (fileEditorTab); | |
67 fileEditorTab->newFile (); | |
68 } | |
69 } | |
70 | |
71 void | |
72 FileEditor::requestOpenFile () | |
73 { | |
74 FileEditorTab *fileEditorTab = new FileEditorTab (this); | |
75 if (fileEditorTab) | |
76 { | |
77 addFileEditorTab (fileEditorTab); | |
78 if (!fileEditorTab->openFile ()) | |
79 { | |
80 // If no file was loaded, remove the tab again. | |
81 m_tabWidget->removeTab (m_tabWidget->indexOf (fileEditorTab)); | |
82 } | |
83 } | |
84 } | |
85 | |
86 void | |
87 FileEditor::requestOpenFile (QString fileName) | |
88 { | |
89 if (!isVisible ()) | |
90 { | |
91 show (); | |
92 } | |
93 | |
94 FileEditorTab *fileEditorTab = new FileEditorTab (this); | |
95 if (fileEditorTab) | |
96 { | |
97 addFileEditorTab (fileEditorTab); | |
98 fileEditorTab->loadFile (fileName); | |
99 } | |
100 } | |
101 | |
102 void | |
103 FileEditor::requestUndo () | |
104 { | |
105 FileEditorTab *activeFileEditorTab = activeEditorTab (); | |
106 if (activeFileEditorTab) | |
107 activeFileEditorTab->undo (); | |
108 } | |
109 | |
110 void | |
111 FileEditor::requestRedo () | |
112 { | |
113 FileEditorTab *activeFileEditorTab = activeEditorTab (); | |
114 if (activeFileEditorTab) | |
115 activeFileEditorTab->redo (); | |
116 } | |
117 | |
118 void | |
119 FileEditor::requestCopy () | |
120 { | |
121 FileEditorTab *activeFileEditorTab = activeEditorTab (); | |
122 if (activeFileEditorTab) | |
123 activeFileEditorTab->copy (); | |
124 } | |
125 | |
126 void | |
127 FileEditor::requestCut () | |
128 { | |
129 FileEditorTab *activeFileEditorTab = activeEditorTab (); | |
130 if (activeFileEditorTab) | |
131 activeFileEditorTab->cut (); | |
132 } | |
133 | |
134 void | |
135 FileEditor::requestPaste () | |
136 { | |
137 FileEditorTab *activeFileEditorTab = activeEditorTab (); | |
138 if (activeFileEditorTab) | |
139 activeFileEditorTab->paste (); | |
140 } | |
141 | |
142 void | |
143 FileEditor::requestSaveFile () | |
144 { | |
145 FileEditorTab *activeFileEditorTab = activeEditorTab (); | |
146 if (activeFileEditorTab) | |
147 activeFileEditorTab->saveFile (); | |
148 } | |
149 | |
150 void | |
151 FileEditor::requestSaveFileAs () | |
152 { | |
153 FileEditorTab *activeFileEditorTab = activeEditorTab (); | |
154 if (activeFileEditorTab) | |
155 activeFileEditorTab->saveFileAs (); | |
156 } | |
157 | |
158 void | |
159 FileEditor::requestRunFile () | |
160 { | |
161 FileEditorTab *activeFileEditorTab = activeEditorTab (); | |
162 if (activeFileEditorTab) | |
163 activeFileEditorTab->runFile (); | |
164 } | |
165 | |
166 void | |
167 FileEditor::requestToggleBookmark () | |
168 { | |
169 FileEditorTab *activeFileEditorTab = activeEditorTab (); | |
170 if (activeFileEditorTab) | |
171 activeFileEditorTab->toggleBookmark (); | |
172 } | |
173 | |
174 void | |
175 FileEditor::requestNextBookmark () | |
176 { | |
177 FileEditorTab *activeFileEditorTab = activeEditorTab (); | |
178 if (activeFileEditorTab) | |
179 activeFileEditorTab->nextBookmark (); | |
180 } | |
181 | |
182 void | |
183 FileEditor::requestPreviousBookmark () | |
184 { | |
185 FileEditorTab *activeFileEditorTab = activeEditorTab (); | |
186 if (activeFileEditorTab) | |
187 activeFileEditorTab->previousBookmark (); | |
188 } | |
189 | |
190 void | |
191 FileEditor::requestRemoveBookmark () | |
192 { | |
193 FileEditorTab *activeFileEditorTab = activeEditorTab (); | |
194 if (activeFileEditorTab) | |
195 activeFileEditorTab->removeBookmark (); | |
196 } | |
197 | |
198 void | |
199 FileEditor::requestCommentSelectedText () | |
200 { | |
201 FileEditorTab *activeFileEditorTab = activeEditorTab (); | |
202 if (activeFileEditorTab) | |
203 activeFileEditorTab->commentSelectedText (); | |
204 } | |
205 | |
206 void | |
207 FileEditor::requestUncommentSelectedText () | |
208 { | |
209 FileEditorTab *activeFileEditorTab = activeEditorTab (); | |
210 if (activeFileEditorTab) | |
211 activeFileEditorTab->uncommentSelectedText (); | |
212 } | |
213 | |
214 void | |
215 FileEditor::handleFileNameChanged (QString fileName) | |
216 { | |
217 QObject *senderObject = sender (); | |
218 FileEditorTab *fileEditorTab = dynamic_cast<FileEditorTab*> (senderObject); | |
219 if (fileEditorTab) | |
220 { | |
221 for(int i = 0; i < m_tabWidget->count (); i++) | |
222 { | |
223 if (m_tabWidget->widget (i) == fileEditorTab) | |
224 { | |
225 m_tabWidget->setTabText (i, fileName); | |
226 } | |
227 } | |
228 } | |
229 } | |
230 | |
231 void | |
232 FileEditor::handleTabCloseRequest (int index) | |
233 { | |
234 FileEditorTab *fileEditorTab = dynamic_cast <FileEditorTab*> (m_tabWidget->widget (index)); | |
235 if (fileEditorTab) | |
236 if (fileEditorTab->close ()) | |
237 { | |
238 m_tabWidget->removeTab (index); | |
239 delete fileEditorTab; | |
240 } | |
241 } | |
242 | |
243 void | |
244 FileEditor::handleTabCloseRequest () | |
245 { | |
246 FileEditorTab *fileEditorTab = dynamic_cast <FileEditorTab*> (sender ()); | |
247 if (fileEditorTab) | |
248 if (fileEditorTab->close ()) | |
249 { | |
250 m_tabWidget->removeTab (m_tabWidget->indexOf (fileEditorTab)); | |
251 delete fileEditorTab; | |
252 } | |
253 } | |
254 | |
255 void | |
256 FileEditor::activeTabChanged (int index) | |
257 { | |
258 Q_UNUSED (index); | |
259 handleEditorStateChanged (); | |
260 } | |
261 | |
262 void | |
263 FileEditor::handleEditorStateChanged () | |
264 { | |
265 FileEditorTab *fileEditorTab = activeEditorTab (); | |
266 if (fileEditorTab) | |
267 { | |
268 bool copyAvailable = fileEditorTab->copyAvailable (); | |
269 m_copyAction->setEnabled (copyAvailable); | |
270 m_cutAction->setEnabled (copyAvailable); | |
271 } | |
272 } | |
273 | |
274 void | |
275 FileEditor::construct () | |
276 { | |
277 QWidget *widget = new QWidget (this); | |
278 QSettings *settings = ResourceManager::instance ()->settings (); | |
279 QStyle *style = QApplication::style (); | |
280 | |
281 m_menuBar = new QMenuBar (widget); | |
282 m_toolBar = new QToolBar (widget); | |
283 m_tabWidget = new QTabWidget (widget); | |
284 m_tabWidget->setTabsClosable (true); | |
285 | |
286 // Theme icons with QStyle icons as fallback | |
287 QAction *newAction = new QAction ( | |
288 QIcon::fromTheme("document-new",style->standardIcon (QStyle::SP_FileIcon)), | |
289 tr("&New File"), m_toolBar); | |
290 | |
291 QAction *openAction = new QAction ( | |
292 QIcon::fromTheme("document-open",style->standardIcon (QStyle::SP_DirOpenIcon)), | |
293 tr("&Open File"), m_toolBar); | |
294 | |
295 QAction *saveAction = new QAction ( | |
296 QIcon::fromTheme("document-save",style->standardIcon (QStyle::SP_DriveHDIcon)), | |
297 tr("&Save File"), m_toolBar); | |
298 | |
299 QAction *saveAsAction = new QAction ( | |
300 QIcon::fromTheme("document-save-as",style->standardIcon (QStyle::SP_DriveFDIcon)), | |
301 tr("Save File &As"), m_toolBar); | |
302 | |
303 QAction *undoAction = new QAction ( | |
304 QIcon::fromTheme("edit-undo",style->standardIcon (QStyle::SP_ArrowLeft)), | |
305 tr("&Undo"), m_toolBar); | |
306 | |
307 QAction *redoAction = new QAction ( | |
308 QIcon::fromTheme("edit-redo",style->standardIcon (QStyle::SP_ArrowRight)), | |
309 tr("&Redo"), m_toolBar); | |
310 | |
311 m_copyAction = new QAction (QIcon::fromTheme ("edit-copy"), tr ("&Copy"), m_toolBar); | |
312 m_cutAction = new QAction (QIcon::fromTheme ("edit-cut"), tr ("Cu&t"), m_toolBar); | |
313 | |
314 QAction *pasteAction = new QAction (QIcon::fromTheme ("edit-paste"), tr ("&Paste"),m_toolBar); | |
315 QAction *nextBookmarkAction = new QAction (tr ("&Next Bookmark"),m_toolBar); | |
316 QAction *prevBookmarkAction = new QAction (tr ("Pre&vious Bookmark"),m_toolBar); | |
317 QAction *toggleBookmarkAction = new QAction (tr ("Toggle &Bookmark"),m_toolBar); | |
318 QAction *removeBookmarkAction = new QAction (tr ("&Remove All Bookmarks"),m_toolBar); | |
319 QAction *commentSelectedAction = new QAction (tr ("&Comment Selected Text"),m_toolBar); | |
320 QAction *uncommentSelectedAction = new QAction (tr ("&Uncomment Selected Text"),m_toolBar); | |
321 | |
322 QAction *runAction = new QAction ( | |
323 QIcon::fromTheme ("media-play", style->standardIcon (QStyle::SP_MediaPlay)), | |
324 tr("&Run File"), m_toolBar); | |
325 | |
326 // some actions are disabled from the beginning | |
327 m_copyAction->setEnabled(false); | |
328 m_cutAction->setEnabled(false); | |
329 | |
330 // short cuts | |
331 newAction->setShortcut (QKeySequence::New); | |
332 openAction->setShortcut (QKeySequence::Open); | |
333 saveAction->setShortcut (QKeySequence::Save); | |
334 saveAsAction->setShortcut (QKeySequence::SaveAs); | |
335 undoAction->setShortcut (QKeySequence::Undo); | |
336 redoAction->setShortcut (QKeySequence::Redo); | |
337 m_copyAction->setShortcut (QKeySequence::Copy); | |
338 m_cutAction->setShortcut (QKeySequence::Cut); | |
339 pasteAction->setShortcut (QKeySequence::Paste); | |
340 runAction->setShortcut (Qt::Key_F5); | |
341 nextBookmarkAction->setShortcut (Qt::Key_F2); | |
342 prevBookmarkAction->setShortcut (Qt::SHIFT + Qt::Key_F2); | |
343 toggleBookmarkAction->setShortcut (Qt::Key_F7); | |
344 commentSelectedAction->setShortcut (Qt::CTRL + Qt::Key_R); | |
345 uncommentSelectedAction->setShortcut(Qt::CTRL + Qt::Key_T); | |
346 | |
347 // toolbar | |
348 m_toolBar->addAction (newAction); | |
349 m_toolBar->addAction (openAction); | |
350 m_toolBar->addAction (saveAction); | |
351 m_toolBar->addAction (saveAsAction); | |
352 m_toolBar->addSeparator (); | |
353 m_toolBar->addAction (undoAction); | |
354 m_toolBar->addAction (redoAction); | |
355 m_toolBar->addAction (m_copyAction); | |
356 m_toolBar->addAction (m_cutAction); | |
357 m_toolBar->addAction (pasteAction); | |
358 m_toolBar->addSeparator (); | |
359 m_toolBar->addAction (runAction); | |
360 | |
361 // menu bar | |
362 QMenu *fileMenu = new QMenu (tr ("&File"), m_menuBar); | |
363 fileMenu->addAction (newAction); | |
364 fileMenu->addAction (openAction); | |
365 fileMenu->addAction (saveAction); | |
366 fileMenu->addAction (saveAsAction); | |
367 fileMenu->addSeparator (); | |
368 m_menuBar->addMenu (fileMenu); | |
369 | |
370 QMenu *editMenu = new QMenu (tr ("&Edit"), m_menuBar); | |
371 editMenu->addAction (undoAction); | |
372 editMenu->addAction (redoAction); | |
373 editMenu->addSeparator (); | |
374 editMenu->addAction (m_copyAction); | |
375 editMenu->addAction (m_cutAction); | |
376 editMenu->addAction (pasteAction); | |
377 editMenu->addSeparator (); | |
378 editMenu->addAction (commentSelectedAction); | |
379 editMenu->addAction (uncommentSelectedAction); | |
380 editMenu->addSeparator (); | |
381 editMenu->addAction (toggleBookmarkAction); | |
382 editMenu->addAction (nextBookmarkAction); | |
383 editMenu->addAction (prevBookmarkAction); | |
384 editMenu->addAction (removeBookmarkAction); | |
385 m_menuBar->addMenu (editMenu); | |
386 | |
387 QMenu *runMenu = new QMenu (tr ("&Run"), m_menuBar); | |
388 runMenu->addAction (runAction); | |
389 m_menuBar->addMenu (runMenu); | |
390 | |
391 QVBoxLayout *layout = new QVBoxLayout (); | |
392 layout->addWidget (m_menuBar); | |
393 layout->addWidget (m_toolBar); | |
394 layout->addWidget (m_tabWidget); | |
395 layout->setMargin (0); | |
396 widget->setLayout (layout); | |
397 setWidget (widget); | |
398 | |
399 connect (newAction, SIGNAL (triggered ()), this, SLOT (requestNewFile ())); | |
400 connect (openAction, SIGNAL (triggered ()), this, SLOT (requestOpenFile ())); | |
401 connect (undoAction, SIGNAL (triggered ()), this, SLOT (requestUndo ())); | |
402 connect (redoAction, SIGNAL (triggered ()), this, SLOT (requestRedo ())); | |
403 connect (m_copyAction, SIGNAL (triggered ()), this, SLOT (requestCopy ())); | |
404 connect (m_cutAction, SIGNAL (triggered ()), this, SLOT (requestCut ())); | |
405 connect (pasteAction, SIGNAL (triggered ()), this, SLOT (requestPaste ())); | |
406 connect (saveAction, SIGNAL (triggered ()), this, SLOT (requestSaveFile ())); | |
407 connect (saveAsAction, SIGNAL (triggered ()), this, SLOT (requestSaveFileAs ())); | |
408 connect (runAction, SIGNAL (triggered ()), this, SLOT (requestRunFile ())); | |
409 connect (toggleBookmarkAction, SIGNAL (triggered ()), this, SLOT (requestToggleBookmark ())); | |
410 connect (nextBookmarkAction, SIGNAL (triggered ()), this, SLOT (requestNextBookmark ())); | |
411 connect (prevBookmarkAction, SIGNAL (triggered ()), this, SLOT (requestPreviousBookmark ())); | |
412 connect (removeBookmarkAction, SIGNAL (triggered ()), this, SLOT (requestRemoveBookmark ())); | |
413 connect (commentSelectedAction, SIGNAL (triggered ()), this, SLOT (requestCommentSelectedText ())); | |
414 connect (uncommentSelectedAction, SIGNAL (triggered ()), this, SLOT (requestUncommentSelectedText ())); | |
415 connect (m_tabWidget, SIGNAL (tabCloseRequested (int)), this, SLOT (handleTabCloseRequest (int))); | |
416 connect (m_tabWidget, SIGNAL (currentChanged(int)), this, SLOT (activeTabChanged (int))); | |
417 | |
418 // this has to be done only once, not for each editor | |
419 m_lexer = new LexerOctaveGui (); | |
420 | |
421 // Editor font (default or from settings) | |
422 m_lexer->setDefaultFont (QFont ( | |
423 settings->value ("editor/fontName","Courier").toString (), | |
424 settings->value ("editor/fontSize",10).toInt ())); | |
425 | |
426 // TODO: Autoindent not working as it should | |
427 m_lexer->setAutoIndentStyle (QsciScintilla::AiMaintain || | |
428 QsciScintilla::AiOpening || | |
429 QsciScintilla::AiClosing); | |
430 | |
431 // The API info that is used for auto completion | |
432 // TODO: Where to store a file with API info (raw or prepared?)? | |
433 // TODO: Also provide infos on octave-forge functions? | |
434 // TODO: Also provide infos on function parameters? | |
435 // By now, use the keywords-list from syntax highlighting | |
436 m_lexerAPI = new QsciAPIs (m_lexer); | |
437 | |
438 QString keyword; | |
439 QStringList keywordList; | |
440 keyword = m_lexer->keywords (1); // get whole string with all keywords | |
441 keywordList = keyword.split (QRegExp ("\\s+")); // split into single strings | |
442 int i; | |
443 for (i = 0; i < keywordList.size (); i++) | |
444 { | |
445 m_lexerAPI->add (keywordList.at (i)); // add single strings to the API | |
446 } | |
447 m_lexerAPI->prepare (); // prepare API info ... this make take some time | |
448 resize (500, 400); | |
449 setWindowIcon (QIcon::fromTheme ("accessories-text-editor", style->standardIcon (QStyle::SP_FileIcon))); | |
450 setWindowTitle ("Octave Editor"); | |
451 } | |
452 | |
453 void | |
454 FileEditor::addFileEditorTab (FileEditorTab *fileEditorTab) | |
455 { | |
456 m_tabWidget->addTab (fileEditorTab, ""); | |
457 connect (fileEditorTab, SIGNAL (fileNameChanged(QString)), | |
458 this, SLOT(handleFileNameChanged(QString))); | |
459 connect (fileEditorTab, SIGNAL (editorStateChanged ()), | |
460 this, SLOT (handleEditorStateChanged ())); | |
461 connect (fileEditorTab, SIGNAL (closeRequest ()), | |
462 this, SLOT (handleTabCloseRequest ())); | |
463 m_tabWidget->setCurrentWidget (fileEditorTab); | |
464 } | |
465 | |
466 FileEditorTab * | |
467 FileEditor::activeEditorTab () | |
468 { | |
469 return dynamic_cast<FileEditorTab*> (m_tabWidget->currentWidget ()); | |
470 } |