comparison gui/src/FileEditorMdiSubWindow.cpp @ 13524:8c143d6d0330

gui-editor: improved behaviour when closing a modified file
author ttl (Torsten) <ttl@justmail.de>
date Wed, 20 Jul 2011 21:23:07 +0200
parents c70511cf64ee
children 8e87f3ba3beb
comparison
equal deleted inserted replaced
13523:103b7bebb38f 13524:8c143d6d0330
32 construct (); 32 construct ();
33 } 33 }
34 34
35 FileEditorMdiSubWindow::~FileEditorMdiSubWindow () 35 FileEditorMdiSubWindow::~FileEditorMdiSubWindow ()
36 { 36 {
37 while (checkFileModified ("Close File")) 37 }
38 { 38
39 } // don't close if something went wrong while saving the file 39 void
40 FileEditorMdiSubWindow::closeEvent(QCloseEvent *event)
41 {
42 // ignore close event if file is not saved and user cancels closing this window
43 // TODO: This does not work if the main window is closed!
44 if (checkFileModified ("Close File")==QMessageBox::Cancel)
45 {
46 event->ignore();
47 }
48 else
49 {
50 event->accept();
51 }
40 } 52 }
41 53
42 void 54 void
43 FileEditorMdiSubWindow::loadFile (QString fileName) 55 FileEditorMdiSubWindow::loadFile (QString fileName)
44 { 56 {
57 QApplication::restoreOverrideCursor (); 69 QApplication::restoreOverrideCursor ();
58 70
59 m_fileName = fileName; 71 m_fileName = fileName;
60 setWindowTitle (fileName); 72 setWindowTitle (fileName);
61 m_statusBar->showMessage (tr ("File loaded."), 2000); 73 m_statusBar->showMessage (tr ("File loaded."), 2000);
62 m_editor->setModified (false); 74 m_editor->setModified (false); // loaded file is not modified yet
63 } 75 }
64 76
65 void 77 void
66 FileEditorMdiSubWindow::newFile () 78 FileEditorMdiSubWindow::newFile ()
67 { 79 {
68 if (checkFileModified ("Open New File")) 80 if (checkFileModified ("Open New File")==QMessageBox::Cancel)
69 { 81 {
70 return; // something went wrong while saving the old file 82 return; // existing file not saved and creating new file canceled by user
71 } 83 }
72 m_fileName = "<unnamed>"; 84 m_fileName = "<unnamed>";
73 setWindowTitle (m_fileName); 85 setWindowTitle (m_fileName);
74 m_editor->setText (""); 86 m_editor->setText ("");
75 m_editor->setModified (false); 87 m_editor->setModified (false); // new file is not modified yet
76 } 88 }
77 89
78 int 90 int
79 FileEditorMdiSubWindow::checkFileModified (QString msg) 91 FileEditorMdiSubWindow::checkFileModified (QString msg)
80 { 92 {
93 int decision = QMessageBox::Yes;
81 if (m_editor->isModified ()) 94 if (m_editor->isModified ())
82 { 95 {
83 int decision = QMessageBox::question (this, 96 // file is modified but not saved, aks user what to do
84 msg, 97 decision = QMessageBox::question (this,
85 "Do you want to save the current file?", 98 msg,
86 QMessageBox::Yes, 99 tr ("Do you want to save the current file\n%1 ?").
87 QMessageBox::No); 100 arg (m_fileName),
101 QMessageBox::Cancel,
102 QMessageBox::No,
103 QMessageBox::Yes);
88 104
89 if (decision == QMessageBox::Yes) 105 if (decision == QMessageBox::Yes)
90 { 106 {
91 saveFile (); 107 saveFile ();
92 if (m_editor->isModified ()) 108 if (m_editor->isModified ())
93 { 109 {
94 // If the user attempted to save the file, but it's still 110 // If the user attempted to save the file, but it's still
95 // modified, then probably something went wrong, so return error 111 // modified, then probably something went wrong, so return cancel
96 return (1); 112 // for cancel this operation
97 } 113 return (QMessageBox::Cancel);
98 } 114 }
99 } 115 }
100 return (0); 116 }
117 return (decision);
101 } 118 }
102 119
103 void 120 void
104 FileEditorMdiSubWindow::saveFile () 121 FileEditorMdiSubWindow::saveFile ()
105 { 122 {
123 QTextStream out (&file); 140 QTextStream out (&file);
124 QApplication::setOverrideCursor (Qt::WaitCursor); 141 QApplication::setOverrideCursor (Qt::WaitCursor);
125 out << m_editor->text (); 142 out << m_editor->text ();
126 QApplication::restoreOverrideCursor (); 143 QApplication::restoreOverrideCursor ();
127 m_statusBar->showMessage (tr ("File saved"), 2000); 144 m_statusBar->showMessage (tr ("File saved"), 2000);
145 m_editor->setModified (false); // files is save -> not modified
128 } 146 }
129 147
130 void 148 void
131 FileEditorMdiSubWindow::saveFileAs () 149 FileEditorMdiSubWindow::saveFileAs ()
132 { /* 150 { /*