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
 |restoreSizeAndPosition()| and |saveSizeAndPosition()| methods should be
1144
 |restoreSizeAndPosition()| and |saveSizeAndPosition()| methods should be
1145
 considered depreciated.
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
 @<Class declarations@>=
1150
 @<Class declarations@>=
1148
 class ScriptQMainWindow : public QMainWindow@/
1151
 class ScriptQMainWindow : public QMainWindow@/
1149
 {@t\1@>@/
1152
 {@t\1@>@/
1150
 	Q_OBJECT@;@/
1153
 	Q_OBJECT@;@/
1154
+	Q_PROPERTY(QString closePrompt READ closePrompt WRITE setClosePrompt)@;@/
1151
 	public:@/
1155
 	public:@/
1152
-		ScriptQMainWindow();@/
1156
+		ScriptQMainWindow();
1157
+		QString closePrompt();@/
1153
 	@t\4@>public slots@t\kern-3pt@>:@/
1158
 	@t\4@>public slots@t\kern-3pt@>:@/
1154
 		void show();
1159
 		void show();
1155
 		void saveSizeAndPosition(const QString &key);
1160
 		void saveSizeAndPosition(const QString &key);
1156
 		void restoreSizeAndPosition(const QString &key);
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
 	protected:@/
1164
 	protected:@/
1159
 		void closeEvent(QCloseEvent *event);
1165
 		void closeEvent(QCloseEvent *event);
1160
 		void showEvent(QShowEvent *event);@/
1166
 		void showEvent(QShowEvent *event);@/
1161
 	signals:@/
1167
 	signals:@/
1162
 		void aboutToClose(void);@t\2@>@/
1168
 		void aboutToClose(void);@t\2@>@/
1169
+	private:@/
1170
+		QString cprompt;
1163
 }@t\kern-3pt@>;
1171
 }@t\kern-3pt@>;
1164
 
1172
 
1165
 @ The implementation of these functions is simple.
1173
 @ The implementation of these functions is simple.
1166
 
1174
 
1167
 @<Functions for scripting@>=
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
 	/* Nothing needs to be done here. */
1179
 	/* Nothing needs to be done here. */
1171
 }
1180
 }
1217
 	QMainWindow::show();
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
 void ScriptQMainWindow::closeEvent(QCloseEvent *event)
1237
 void ScriptQMainWindow::closeEvent(QCloseEvent *event)
1221
 {
1238
 {
1239
+	if(isWindowModified()) {
1240
+		@<Allow close event to be cancelled@>@;
1241
+	}
1222
 	emit aboutToClose();
1242
 	emit aboutToClose();
1223
 	@<Save window geometry@>@;
1243
 	@<Save window geometry@>@;
1224
 	event->accept();
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
 @ Window geometry management from version 1.4 on makes use of the window ID to
1273
 @ Window geometry management from version 1.4 on makes use of the window ID to
1228
 produce an appropriate QSettings key. This decision relies on the ID being set
1274
 produce an appropriate QSettings key. This decision relies on the ID being set
1229
 before any show or close events are received and it relies on every distinct
1275
 before any show or close events are received and it relies on every distinct

Loading…
Cancel
Save