-
Notifications
You must be signed in to change notification settings - Fork 413
/
RMainWindow.cpp
639 lines (563 loc) · 18.3 KB
/
RMainWindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
/**
* Copyright (c) 2011-2013 by Andrew Mustun. All rights reserved.
*
* This file is part of the QCAD project.
*
* QCAD is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QCAD is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QCAD.
*/
#include <QCoreApplication>
#include <QDir>
#include <QObject>
#include <QThread>
#include "RBlockListener.h"
#include "RCoordinateEvent.h"
#include "RCoordinateListener.h"
#include "RDebug.h"
#include "RDocumentInterface.h"
#include "RFocusListener.h"
#include "RFontList.h"
#include "RGraphicsView.h"
#include "RGuiAction.h"
#include "RLayerListener.h"
#include "RMainWindow.h"
#include "RNewDocumentListener.h"
#include "RPenListener.h"
#include "RExportListener.h"
#include "RImportListener.h"
#include "RPropertyListener.h"
#include "RSelectionListener.h"
#include "RSettings.h"
#include "RSnapListener.h"
#include "RUcsListener.h"
#include "RVector.h"
#include "RViewFocusListener.h"
#include "RViewListener.h"
#include "RViewListener.h"
RMainWindow* RMainWindow::mainWindow = NULL;
RMainWindow::RMainWindow() {
mainWindow = this;
}
RMainWindow::~RMainWindow() {
mainWindow = NULL;
}
RMainWindow* RMainWindow::getMainWindow() {
return mainWindow;
}
bool RMainWindow::hasMainWindow() {
return mainWindow != NULL;
}
void RMainWindow::installMessageHandler() {
#if QT_VERSION >= 0x050000
qInstallMessageHandler(RMainWindow::messageHandler);
#else
qInstallMsgHandler(RMainWindow::messageHandler);
#endif
}
#if QT_VERSION >= 0x050000
void RMainWindow::messageHandler(QtMsgType type, const QMessageLogContext& context, const QString& message) {
QByteArray localMsg = message.toLocal8Bit();
switch (type) {
case QtDebugMsg:
fprintf(stderr, "\033[36m%s:%u, %s:\033[0m\nDebug: %s\n", context.file, context.line, context.function, localMsg.constData());
fflush(stderr);
break;
case QtWarningMsg:
if (localMsg.contains("changing class of non-QScriptObject not supported")) {
break;
}
if (localMsg.startsWith("QPainter::")) {
break;
}
fprintf(stderr, "\033[31m%s:%u, %s:\033[0m\nWarning: %s\n", context.file, context.line, context.function, localMsg.constData());
fflush(stderr);
break;
case QtCriticalMsg:
fprintf(stderr, "\033[31m%s:%u, %s\033[0m\nCritical: %s\n", context.file, context.line, context.function, localMsg.constData());
fflush(stderr);
break;
case QtFatalMsg:
fprintf(stderr, "\033[31m%s:%u, %s\033[0m\nFatal: %s\n", context.file, context.line, context.function, localMsg.constData());
fflush(stderr);
abort();
}
}
#else
void RMainWindow::messageHandler(QtMsgType type, const char* msg) {
QByteArray localMsg = msg;
switch (type) {
case QtDebugMsg:
fprintf(stderr, "Debug: %s\n", localMsg.constData());
fflush(stderr);
break;
case QtWarningMsg:
if (localMsg.contains("changing class of non-QScriptObject not supported")) {
break;
}
if (localMsg.startsWith("QPainter::")) {
break;
}
fprintf(stderr, "Warning: %s\n", localMsg.constData());
fflush(stderr);
break;
case QtCriticalMsg:
fprintf(stderr, "Critical: %s\n", localMsg.constData());
fflush(stderr);
break;
case QtFatalMsg:
fprintf(stderr, "Fatal: %s\n", localMsg.constData());
fflush(stderr);
abort();
}
}
#endif
/**
* \return Pointer to the document interface of the current document
* (e.g. the active MDI window) or NULL if no document is open.
*/
RDocumentInterface* RMainWindow::getDocumentInterfaceStatic() {
if (mainWindow == NULL) {
return NULL;
}
return mainWindow->getDocumentInterface();
}
void RMainWindow::notifyListeners(bool withNull) {
RDocument* document = NULL;
RDocumentInterface* di = NULL;
RGraphicsView* view = NULL;
if (!withNull) {
document = getDocument();
di = getDocumentInterface();
if (di!=NULL) {
view = di->getLastKnownViewWithFocus();
}
}
notifyFocusListeners(di);
notifyViewFocusListeners(view);
notifyCoordinateListeners(di);
notifySnapListeners(di);
notifyTransactionListeners(document);
notifyPropertyListeners(document);
notifySelectionListeners(di);
notifyLayerListeners(di);
notifyPenListeners(di);
notifyBlockListeners(di);
notifyViewListeners(di);
}
/**
* Adds a listener for coordinate events. This can for example be a widget that
* displays the current coordinate.
*/
void RMainWindow::addCoordinateListener(RCoordinateListener* l) {
coordinateListeners.push_back(l);
}
void RMainWindow::removeCoordinateListener(RCoordinateListener* l) {
coordinateListeners.removeAll(l);
}
/**
* Notifies all coordinate listeners that the coordinate has changed to \c position.
*/
void RMainWindow::notifyCoordinateListeners(RDocumentInterface* documentInterface) {
QList<RCoordinateListener*>::iterator it;
for (it = coordinateListeners.begin(); it != coordinateListeners.end(); ++it) {
(*it)->updateCoordinate(documentInterface);
}
}
/**
* Adds a listener for selection events. This can for example be a widget that
* displays the number of selected entities.
*/
void RMainWindow::addSelectionListener(RSelectionListener* l) {
selectionListeners.push_back(l);
}
void RMainWindow::removeSelectionListener(RSelectionListener* l) {
selectionListeners.removeAll(l);
}
/**
* Notifies all selection listeners that the current selection has changed.
*/
void RMainWindow::notifySelectionListeners(RDocumentInterface* documentInterface) {
QList<RSelectionListener*>::iterator it;
for (it = selectionListeners.begin(); it != selectionListeners.end(); ++it) {
(*it)->updateSelectionListener(documentInterface);
}
}
/**
* Adds a listener for export events.
*/
void RMainWindow::addExportListener(RExportListener* l) {
if (l != NULL) {
exportListeners.push_back(l);
} else {
qWarning("RMainWindow::addExportListener(): Listener is NULL.");
}
}
void RMainWindow::removeExportListener(RExportListener* l) {
exportListeners.removeAll(l);
}
/**
* Notifies all Export listeners about pre export event.
*/
void RMainWindow::notifyExportListenersPre(RDocumentInterface* documentInterface) {
QList<RExportListener*>::iterator it;
for (it = exportListeners.begin(); it != exportListeners.end(); ++it) {
(*it)->preExportEvent(documentInterface);
}
}
/**
* Notifies all Export listeners about post export event.
*/
void RMainWindow::notifyExportListenersPost(RDocumentInterface* documentInterface) {
QList<RExportListener*>::iterator it;
for (it = exportListeners.begin(); it != exportListeners.end(); ++it) {
(*it)->postExportEvent(documentInterface);
}
}
/**
* Adds a listener for import events.
*/
void RMainWindow::addImportListener(RImportListener* l) {
if (l != NULL) {
importListeners.push_back(l);
} else {
qWarning("RMainWindow::addImportListener(): Listener is NULL.");
}
}
void RMainWindow::removeImportListener(RImportListener* l) {
importListeners.removeAll(l);
}
/**
* Notifies all import listeners about pre import event.
*/
void RMainWindow::notifyImportListenersPre(RDocumentInterface* documentInterface) {
QList<RImportListener*>::iterator it;
for (it = importListeners.begin(); it != importListeners.end(); ++it) {
(*it)->preImportEvent(documentInterface);
}
}
/**
* Notifies all import listeners about post import event.
*/
void RMainWindow::notifyImportListenersPost(RDocumentInterface* documentInterface) {
QList<RImportListener*>::iterator it;
for (it = importListeners.begin(); it != importListeners.end(); ++it) {
(*it)->postImportEvent(documentInterface);
}
}
/**
* Adds a listener for transaction events. This can for example be an undo or
* redo button.
*/
void RMainWindow::addTransactionListener(RTransactionListener* l) {
if (l != NULL) {
transactionListeners.push_back(l);
} else {
qWarning("RMainWindow::addTransactionListener(): Listener is NULL.");
}
}
void RMainWindow::removeTransactionListener(RTransactionListener* l) {
transactionListeners.removeAll(l);
}
/**
* Notifies all transaction listeners.
*/
void RMainWindow::notifyTransactionListeners(RDocument* document, RTransaction* transaction) {
QList<RTransactionListener*>::iterator it;
for (it = transactionListeners.begin(); it != transactionListeners.end(); ++it) {
(*it)->updateTransactionListener(document, transaction);
}
}
/**
* Adds a listener for transaction in progress events.
*/
void RMainWindow::addInterTransactionListener(RInterTransactionListener* l) {
if (l != NULL) {
interTransactionListeners.push_back(l);
} else {
qWarning("RMainWindow::addInterTransactionListener(): Listener is NULL.");
}
}
void RMainWindow::removeInterTransactionListener(RInterTransactionListener* l) {
interTransactionListeners.removeAll(l);
}
/**
* Notifies all transaction in progress listeners.
*/
void RMainWindow::notifyInterTransactionListeners(RDocument* document, RTransaction* transaction) {
QList<RInterTransactionListener*>::iterator it;
for (it = interTransactionListeners.begin(); it != interTransactionListeners.end(); ++it) {
(*it)->updateInterTransactionListener(document, transaction);
}
}
/**
* Adds a listener for transaction in progress events.
*/
void RMainWindow::addNewDocumentListener(RNewDocumentListener* l) {
if (l != NULL) {
newDocumentListeners.push_back(l);
} else {
qWarning("RMainWindow::addNewDocumentListener(): Listener is NULL.");
}
}
void RMainWindow::removeNewDocumentListener(RNewDocumentListener* l) {
newDocumentListeners.removeAll(l);
}
/**
* Notifies all transaction in progress listeners.
*/
void RMainWindow::notifyNewDocumentListeners(RDocument* document, RTransaction* transaction) {
QList<RNewDocumentListener*>::iterator it;
for (it = newDocumentListeners.begin(); it != newDocumentListeners.end(); ++it) {
(*it)->updateNewDocumentListener(document, transaction);
}
}
/**
* Adds a listener for snap mode changes. This can for example be
* a snap status widget.
*/
void RMainWindow::addSnapListener(RSnapListener* l) {
if (l != NULL) {
snapListeners.push_back(l);
} else {
qWarning("RMainWindow::addSnapListener(): Listener is NULL.");
}
}
void RMainWindow::removeSnapListener(RSnapListener* l) {
snapListeners.removeAll(l);
}
/**
* Notifies all snap mode listeners.
*/
void RMainWindow::notifySnapListeners(RDocumentInterface* documentInterface) {
QList<RSnapListener*>::iterator it;
for (it = snapListeners.begin(); it != snapListeners.end(); ++it) {
(*it)->updateSnap(documentInterface);
}
}
/**
* Adds a listener for focus changes.
*/
void RMainWindow::addFocusListener(RFocusListener* l) {
if (l != NULL) {
focusListeners.push_back(l);
} else {
qWarning("RMainWindow::addFocusListener(): Listener is NULL.");
}
}
void RMainWindow::removeFocusListener(RFocusListener* l) {
focusListeners.removeAll(l);
}
/**
* Notifies all focus listeners.
*/
void RMainWindow::notifyFocusListeners(RDocumentInterface* documentInterface) {
QList<RFocusListener*>::iterator it;
for (it = focusListeners.begin(); it != focusListeners.end(); ++it) {
(*it)->updateFocus(documentInterface);
}
}
/**
* Adds a listener for view focus changes.
*/
void RMainWindow::addViewFocusListener(RViewFocusListener* l) {
if (l != NULL) {
viewFocusListeners.push_back(l);
} else {
qWarning("RMainWindow::addViewFocusListener(): Listener is NULL.");
}
}
void RMainWindow::removeViewFocusListener(RViewFocusListener* l) {
viewFocusListeners.removeAll(l);
}
/**
* Notifies all view focus listeners.
*/
void RMainWindow::notifyViewFocusListeners(RGraphicsView* view) {
QList<RViewFocusListener*>::iterator it;
for (it = viewFocusListeners.begin(); it != viewFocusListeners.end(); ++it) {
(*it)->updateFocus(view);
}
}
/**
* Adds a listener for property events. This can for example be a widget that
* displays the properties of selected entities or the current action.
*/
void RMainWindow::addPropertyListener(RPropertyListener* l) {
propertyListeners.push_back(l);
}
void RMainWindow::removePropertyListener(RPropertyListener* l) {
propertyListeners.removeAll(l);
}
/**
* Notifies all property listeners that the current property has changed.
*/
void RMainWindow::notifyPropertyListeners(
RDocument* document,
bool onlyChanges,
RS::EntityType entityTypeFilter) {
QList<RPropertyListener*>::iterator it;
for (it = propertyListeners.begin(); it != propertyListeners.end(); ++it) {
(*it)->updateFromDocument(document, onlyChanges, entityTypeFilter);
}
}
/**
* Notifies all property listeners that the current property is the given entity.
*/
void RMainWindow::notifyPropertyListeners(RDocument& document, REntity& entity) {
QList<RPropertyListener*>::iterator it;
for (it = propertyListeners.begin(); it != propertyListeners.end(); ++it) {
(*it)->updateFromObject((RObject*) &entity, &document);
}
}
/**
* Notifies all property listeners that no properties are currently relevant.
*/
void RMainWindow::notifyPropertyListeners() {
QList<RPropertyListener*>::iterator it;
for (it = propertyListeners.begin(); it != propertyListeners.end(); ++it) {
(*it)->clearEditor();
}
}
void RMainWindow::propertyChangeEvent(RPropertyEvent& event) {
RDocumentInterface* documentInterface = getDocumentInterface();
if (documentInterface == NULL) {
return;
}
documentInterface->propertyChangeEvent(event);
}
/**
* Adds a listener for UCS change / add / remove events.
* This can for example be a widget that displays all UCS objects.
*/
void RMainWindow::addUcsListener(RUcsListener* l) {
ucsListeners.push_back(l);
}
void RMainWindow::removeUcsListener(RUcsListener* l) {
ucsListeners.removeAll(l);
}
/**
* Notifies all UCS listeners that at least one UCS object has changed.
*/
void RMainWindow::notifyUcsListeners(RDocumentInterface* documentInterface) {
QList<RUcsListener*>::iterator it;
for (it = ucsListeners.begin(); it != ucsListeners.end(); ++it) {
(*it)->update(documentInterface);
}
}
/**
* Adds a listener for layer change / add / remove events.
*/
void RMainWindow::addLayerListener(RLayerListener* l) {
layerListeners.push_back(l);
}
void RMainWindow::removeLayerListener(RLayerListener* l) {
layerListeners.removeAll(l);
}
/**
* Notifies all layer listeners that at least one layer object has changed.
*/
void RMainWindow::notifyLayerListeners(RDocumentInterface* documentInterface) {
QList<RLayerListener*>::iterator it;
for (it = layerListeners.begin(); it != layerListeners.end(); ++it) {
(*it)->updateLayers(documentInterface);
}
}
/**
* Adds a listener for block change / add / remove events.
*/
void RMainWindow::addBlockListener(RBlockListener* l) {
blockListeners.push_back(l);
}
void RMainWindow::removeBlockListener(RBlockListener* l) {
blockListeners.removeAll(l);
}
/**
* Notifies all block listeners that at least one block object has changed.
*/
void RMainWindow::notifyBlockListeners(RDocumentInterface* documentInterface) {
QList<RBlockListener*>::iterator it;
for (it = blockListeners.begin(); it != blockListeners.end(); ++it) {
(*it)->updateBlocks(documentInterface);
}
}
/**
* Adds a listener for view change / add / remove events.
*/
void RMainWindow::addViewListener(RViewListener* l) {
viewListeners.push_back(l);
}
void RMainWindow::removeViewListener(RViewListener* l) {
viewListeners.removeAll(l);
}
/**
* Notifies all view listeners that at least one view object has changed.
*/
void RMainWindow::notifyViewListeners(RDocumentInterface* documentInterface) {
QList<RViewListener*>::iterator it;
for (it = viewListeners.begin(); it != viewListeners.end(); ++it) {
(*it)->updateViews(documentInterface);
}
}
/**
* Adds a listener for pen change events.
*/
void RMainWindow::addPenListener(RPenListener* l) {
penListeners.push_back(l);
}
void RMainWindow::removePenListener(RPenListener* l) {
penListeners.removeAll(l);
}
/**
* Notifies all pen listeners that the current pen has changed.
*/
void RMainWindow::notifyPenListeners(RDocumentInterface* documentInterface) {
QList<RPenListener*>::iterator it;
for (it = penListeners.begin(); it != penListeners.end(); ++it) {
(*it)->updatePen(documentInterface);
}
}
/**
* Called immediately after the user has activated a new UCS to be used as current UCS.
*/
void RMainWindow::ucsSetEvent(const QString& ucsName) {
RDocumentInterface* documentInterface = getDocumentInterface();
if (documentInterface == NULL) {
return;
}
documentInterface->ucsSetEvent(ucsName);
}
/**
* Restores the application window settings (size, position, ...).
*/
bool RMainWindow::readSettings() {
bool ret = false;
int x = RSettings::getQSettings()->value("Appearance/Position.X", -1).toInt();
int y = RSettings::getQSettings()->value("Appearance/Position.Y", -1).toInt();
int width = RSettings::getQSettings()->value("Appearance/Width", 1024).toInt();
int height = RSettings::getQSettings()->value("Appearance/Height", 800).toInt();
if (x != -1 && y != -1) {
move(x, y);
ret = true;
}
resize(width, height);
return ret;
}
/**
* Stores the application window settings (size, position, ...).
*/
void RMainWindow::writeSettings() {
RSettings::getQSettings()->setValue("Appearance/Position.X", getPositionX());
RSettings::getQSettings()->setValue("Appearance/Position.Y", getPositionY());
RSettings::getQSettings()->setValue("Appearance/Width", getWidth());
RSettings::getQSettings()->setValue("Appearance/Height", getHeight());
}