comparison gui/src/FileEditorMdiSubWindow.cpp @ 13657:347dfbea2c8a

modified editor files can be saved when main window is closed
author ttl <ttl@justmail.de>
date Sun, 28 Aug 2011 00:03:28 +0200
parents 1347d498959c
children d98c6ef06dff
comparison
equal deleted inserted replaced
13656:1347d498959c 13657:347dfbea2c8a
15 * You should have received a copy of the GNU General Public License 15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */ 17 */
18 18
19 #include "FileEditorMdiSubWindow.h" 19 #include "FileEditorMdiSubWindow.h"
20 #include "MainWindow.h"
21 #include <QVBoxLayout> 20 #include <QVBoxLayout>
22 #include <QApplication> 21 #include <QApplication>
23 #include <QFile> 22 #include <QFile>
24 #include <QFont> 23 #include <QFont>
25 #include <QFileDialog> 24 #include <QFileDialog>
38 } 37 }
39 38
40 void 39 void
41 FileEditorMdiSubWindow::closeEvent(QCloseEvent *event) 40 FileEditorMdiSubWindow::closeEvent(QCloseEvent *event)
42 { 41 {
43 // ignore close event if file is not saved and user cancels closing this window 42 if ( m_mainWindow->isCloseApplication() )
44 // TODO: This does not work if the main window is closed! 43 {
45 if (checkFileModified ("Close File")==QMessageBox::Cancel) 44 // close wohle application: save file or not if modified
46 { 45 checkFileModified ("Close Octave GUI",0); // no cancel possible
47 event->ignore();
48 } 46 }
49 else 47 else
50 { 48 {
51 event->accept(); 49 // ignore close event if file is not saved and user cancels closing this window
50 if (checkFileModified ("Close File",QMessageBox::Cancel)==QMessageBox::Cancel)
51 event->ignore();
52 else
53 event->accept();
52 } 54 }
53 } 55 }
54 56
55 void 57 void
56 FileEditorMdiSubWindow::handleMarginClicked(int margin, int line, Qt::KeyboardModifiers state) 58 FileEditorMdiSubWindow::handleMarginClicked(int margin, int line, Qt::KeyboardModifiers state)
87 89
88 90
89 void 91 void
90 FileEditorMdiSubWindow::openFile () 92 FileEditorMdiSubWindow::openFile ()
91 { 93 {
92 if (checkFileModified ("Open File")==QMessageBox::Cancel) 94 if (checkFileModified ("Open File",QMessageBox::Cancel)==QMessageBox::Cancel)
93 { 95 {
94 return; // existing file not saved and opening another file canceled by user 96 return; // existing file not saved and opening another file canceled by user
95 } 97 }
96 QString openFileName; 98 QString openFileName;
97 QFileDialog dlg(this); 99 QFileDialog dlg(this);
131 } 133 }
132 134
133 void 135 void
134 FileEditorMdiSubWindow::newFile () 136 FileEditorMdiSubWindow::newFile ()
135 { 137 {
136 if (checkFileModified ("Open New File")==QMessageBox::Cancel) 138 if (checkFileModified ("Create New File",QMessageBox::Cancel)==QMessageBox::Cancel)
137 { 139 {
138 return; // existing file not saved and creating new file canceled by user 140 return; // existing file not saved and creating new file canceled by user
139 } 141 }
140 m_fileName = UNNAMED_FILE; 142 m_fileName = UNNAMED_FILE;
141 setWindowTitle (m_fileName); 143 setWindowTitle (m_fileName);
142 m_editor->setText (""); 144 m_editor->setText ("");
143 m_editor->setModified (false); // new file is not modified yet 145 m_editor->setModified (false); // new file is not modified yet
144 } 146 }
145 147
146 int 148 int
147 FileEditorMdiSubWindow::checkFileModified (QString msg) 149 FileEditorMdiSubWindow::checkFileModified (QString msg, int cancelButton)
148 { 150 {
149 int decision = QMessageBox::Yes; 151 int decision = QMessageBox::Yes;
150 if (m_editor->isModified ()) 152 if (m_editor->isModified ())
151 { 153 {
152 // file is modified but not saved, aks user what to do 154 // file is modified but not saved, aks user what to do
153 decision = QMessageBox::question (this, 155 decision = QMessageBox::warning (this,
154 msg, 156 msg,
155 tr ("Do you want to save the current file\n%1 ?"). 157 tr ("The file %1\n"
158 "has been modified. Do you want to save the changes?").
156 arg (m_fileName), 159 arg (m_fileName),
157 QMessageBox::Cancel, 160 QMessageBox::Save, QMessageBox::Discard, cancelButton );
158 QMessageBox::No, 161 if (decision == QMessageBox::Save)
159 QMessageBox::Yes);
160
161 if (decision == QMessageBox::Yes)
162 { 162 {
163 saveFile (); 163 saveFile ();
164 if (m_editor->isModified ()) 164 if (m_editor->isModified ())
165 { 165 {
166 // If the user attempted to save the file, but it's still 166 // If the user attempted to save the file, but it's still
167 // modified, then probably something went wrong, so return cancel 167 // modified, then probably something went wrong, so return cancel
168 // for cancel this operation 168 // for cancel this operation or try to save files as if cancel not
169 return (QMessageBox::Cancel); 169 // possible
170 if ( cancelButton )
171 return (QMessageBox::Cancel);
172 else
173 saveFileAs ();
170 } 174 }
171 } 175 }
172 } 176 }
173 return (decision); 177 return (decision);
174 } 178 }
329 } 333 }
330 334
331 // function for setting the already existing lexer from MainWindow 335 // function for setting the already existing lexer from MainWindow
332 void 336 void
333 FileEditorMdiSubWindow::initEditor (TerminalEmulation* terminalEmulation, 337 FileEditorMdiSubWindow::initEditor (TerminalEmulation* terminalEmulation,
334 LexerOctaveGui* lexer) 338 LexerOctaveGui* lexer,
339 MainWindow* mainWindow)
335 { 340 {
336 m_editor->setLexer(lexer); 341 m_editor->setLexer(lexer);
337 m_terminalEmulation = terminalEmulation; // for sending commands to octave 342 m_terminalEmulation = terminalEmulation; // for sending commands to octave
338 // TODO: make a global commandOctave function? 343 // TODO: make a global commandOctave function?
344 m_mainWindow = mainWindow; // get the MainWindow for chekcing state at subwindow close
339 } 345 }
340 346
341 // TODO: Do we still need tool tips in the status bar? Tool tips are now 347 // TODO: Do we still need tool tips in the status bar? Tool tips are now
342 // shown directly at the theme icons 348 // shown directly at the theme icons
343 void 349 void