Browse Source

Code required to fix #80. Additional configuration modifications required to take advantage of new feature.

Neal Wilson 11 years ago
parent
commit
bbc506bf71
1 changed files with 49 additions and 3 deletions
  1. 49
    3
      src/typica.w

+ 49
- 3
src/typica.w View File

@@ -1144,28 +1144,37 @@ As of version 1.4 window geometry management is provided for all windows. The
1144 1144
 |restoreSizeAndPosition()| and |saveSizeAndPosition()| methods should be
1145 1145
 considered depreciated.
1146 1146
 
1147
+Version 1.6 adds a new property for handling the |windowModified| property
1148
+such that an appropriate prompt is provided to confirm or cancel close events.
1149
+
1147 1150
 @<Class declarations@>=
1148 1151
 class ScriptQMainWindow : public QMainWindow@/
1149 1152
 {@t\1@>@/
1150 1153
 	Q_OBJECT@;@/
1154
+	Q_PROPERTY(QString closePrompt READ closePrompt WRITE setClosePrompt)@;@/
1151 1155
 	public:@/
1152
-		ScriptQMainWindow();@/
1156
+		ScriptQMainWindow();
1157
+		QString closePrompt();@/
1153 1158
 	@t\4@>public slots@t\kern-3pt@>:@/
1154 1159
 		void show();
1155 1160
 		void saveSizeAndPosition(const QString &key);
1156 1161
 		void restoreSizeAndPosition(const QString &key);
1157
-		void displayStatus(const QString &message = QString());@/
1162
+		void displayStatus(const QString &message = QString());
1163
+		void setClosePrompt(QString prompt);@/
1158 1164
 	protected:@/
1159 1165
 		void closeEvent(QCloseEvent *event);
1160 1166
 		void showEvent(QShowEvent *event);@/
1161 1167
 	signals:@/
1162 1168
 		void aboutToClose(void);@t\2@>@/
1169
+	private:@/
1170
+		QString cprompt;
1163 1171
 }@t\kern-3pt@>;
1164 1172
 
1165 1173
 @ The implementation of these functions is simple.
1166 1174
 
1167 1175
 @<Functions for scripting@>=
1168
-ScriptQMainWindow::ScriptQMainWindow()@+: QMainWindow(NULL)@/
1176
+ScriptQMainWindow::ScriptQMainWindow()@+: QMainWindow(NULL),
1177
+	cprompt(tr("Closing this window may result in loss of data. Continue?"))@/
1169 1178
 {
1170 1179
 	/* Nothing needs to be done here. */
1171 1180
 }
@@ -1217,13 +1226,50 @@ void ScriptQMainWindow::show()
1217 1226
 	QMainWindow::show();
1218 1227
 }
1219 1228
 
1229
+@ When a close event occurs, we check the |windowModified| property to
1230
+determine if closing the window could result in loss of data. If this is
1231
+true, we allow the event to be cancelled. Otherwise, a signal is emitted which
1232
+allows scripts to perform any cleanup that may be required before closing the
1233
+window and the window geometry data is saved before allowing the window to
1234
+close.
1235
+
1236
+@<Functions for scripting@>=
1220 1237
 void ScriptQMainWindow::closeEvent(QCloseEvent *event)
1221 1238
 {
1239
+	if(isWindowModified()) {
1240
+		@<Allow close event to be cancelled@>@;
1241
+	}
1222 1242
 	emit aboutToClose();
1223 1243
 	@<Save window geometry@>@;
1224 1244
 	event->accept();
1225 1245
 }
1226 1246
 
1247
+@ The prompt text for our confirmation window is provided through the
1248
+|closePrompt| property.
1249
+
1250
+@<Allow close event to be cancelled@>=
1251
+QMessageBox::StandardButton result;
1252
+result = QMessageBox::warning(this, "Typica", closePrompt(),
1253
+                              QMessageBox::Ok | QMessageBox::Cancel);
1254
+if(result == QMessageBox::Cancel)
1255
+{
1256
+	event->ignore();
1257
+	return;
1258
+}
1259
+
1260
+@ Implementation of the |closePrompt| property is trivial.
1261
+
1262
+@<Functions for scripting@>=
1263
+QString ScriptQMainWindow::closePrompt()
1264
+{
1265
+	return cprompt;
1266
+}
1267
+
1268
+void ScriptQMainWindow::setClosePrompt(QString prompt)
1269
+{
1270
+	cprompt = prompt;
1271
+}
1272
+
1227 1273
 @ Window geometry management from version 1.4 on makes use of the window ID to
1228 1274
 produce an appropriate QSettings key. This decision relies on the ID being set
1229 1275
 before any show or close events are received and it relies on every distinct

Loading…
Cancel
Save