/
MainWindow.cpp
2099 lines (1886 loc) · 74.3 KB
/
MainWindow.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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
* Copyright (c) 2005 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library 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 Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
# include <QApplication>
# include <QByteArray>
# include <QCheckBox>
# include <QClipboard>
# include <QCloseEvent>
# include <QContextMenuEvent>
# include <QDesktopServices>
# include <QDockWidget>
# include <QFontMetrics>
# include <QKeySequence>
# include <QLabel>
# include <QMdiSubWindow>
# include <QMenu>
# include <QMessageBox>
# include <QMimeData>
# include <QPainter>
# include <QScreen>
# include <QSettings>
# include <QSignalMapper>
# include <QStatusBar>
# include <QThread>
# include <QTimer>
# include <QToolBar>
# include <QUrlQuery>
# include <QWhatsThis>
# include <QPushButton>
#endif
#include <App/Application.h>
#include <App/Document.h>
#include <App/DocumentObject.h>
#include <App/DocumentObjectGroup.h>
#include <Base/Parameter.h>
#include <Base/Exception.h>
#include <Base/FileInfo.h>
#include <Base/Interpreter.h>
#include <Base/Stream.h>
#include <DAGView/DAGView.h>
#include <TaskView/TaskView.h>
#include "MainWindow.h"
#include "Action.h"
#include "Assistant.h"
#include "BitmapFactory.h"
#include "ComboView.h"
#include "Command.h"
#include "DockWindowManager.h"
#include "DownloadManager.h"
#include "FileDialog.h"
#include "MenuManager.h"
#include "ProgressBar.h"
#include "PropertyView.h"
#include "PythonConsole.h"
#include "ReportView.h"
#include "SelectionView.h"
#include "Splashscreen.h"
#include "ToolBarManager.h"
#include "ToolBoxManager.h"
#include "Tree.h"
#include "WaitCursor.h"
#include "WorkbenchManager.h"
#include "Workbench.h"
#include "MergeDocuments.h"
#include "ViewProviderExtern.h"
#include "SpaceballEvent.h"
#include "View3DInventor.h"
#include "View3DInventorViewer.h"
#include "DlgObjectSelection.h"
#include "Tools.h"
FC_LOG_LEVEL_INIT("MainWindow",false,true,true)
#if defined(Q_OS_WIN32)
#define slots
//#include <private/qmainwindowlayout_p.h>
//#include <private/qwidgetresizehandler_p.h>
#endif
using namespace Gui;
using namespace Gui::DockWnd;
using namespace std;
MainWindow* MainWindow::instance = nullptr;
namespace Gui {
/**
* The CustomMessageEvent class is used to send messages as events in the methods
* Error(), Warning() and Message() of the StatusBarObserver class to the main window
* to display them on the status bar instead of printing them directly to the status bar.
*
* This makes the usage of StatusBarObserver thread-safe.
* @author Werner Mayer
*/
class CustomMessageEvent : public QEvent
{
public:
CustomMessageEvent(int t, const QString& s, int timeout=0)
: QEvent(QEvent::User), _type(t), msg(s), _timeout(timeout)
{ }
~CustomMessageEvent()
{ }
int type() const
{ return _type; }
const QString& message() const
{ return msg; }
int timeout() const
{ return _timeout; }
private:
int _type;
QString msg;
int _timeout;
};
// -------------------------------------
// Pimpl class
struct MainWindowP
{
QLabel* sizeLabel;
QLabel* actionLabel;
QTimer* actionTimer;
QTimer* statusTimer;
QTimer* activityTimer;
QMdiArea* mdiArea;
QPointer<MDIView> activeView;
QSignalMapper* windowMapper;
QSplashScreen* splashscreen;
StatusBarObserver* status;
bool whatsthis;
QString whatstext;
Assistant* assistant;
int currentStatusType = 100;
int actionUpdateDelay = 0;
QMap<QString, QPointer<UrlHandler> > urlHandler;
};
class MDITabbar : public QTabBar
{
public:
MDITabbar( QWidget * parent = nullptr ) : QTabBar(parent)
{
menu = new QMenu(this);
setDrawBase(false);
setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Fixed);
}
~MDITabbar()
{
delete menu;
}
protected:
void contextMenuEvent ( QContextMenuEvent * e )
{
menu->clear();
CommandManager& cMgr = Application::Instance->commandManager();
if (tabRect(currentIndex()).contains(e->pos()))
cMgr.getCommandByName("Std_CloseActiveWindow")->addTo(menu);
cMgr.getCommandByName("Std_CloseAllWindows")->addTo(menu);
menu->addSeparator();
cMgr.getCommandByName("Std_CascadeWindows")->addTo(menu);
cMgr.getCommandByName("Std_ArrangeIcons")->addTo(menu);
cMgr.getCommandByName("Std_TileWindows")->addTo(menu);
menu->addSeparator();
cMgr.getCommandByName("Std_Windows")->addTo(menu);
menu->popup(e->globalPos());
}
private:
QMenu* menu;
};
#if defined(Q_OS_WIN32)
class MainWindowTabBar : public QTabBar
{
public:
MainWindowTabBar(QWidget *parent) : QTabBar(parent)
{
setExpanding(false);
}
protected:
bool event(QEvent *e)
{
// show the tooltip if tab is too small to fit label
if (e->type() != QEvent::ToolTip)
return QTabBar::event(e);
QSize size = this->size();
QSize hint = sizeHint();
if (shape() == QTabBar::RoundedWest || shape() == QTabBar::RoundedEast) {
size.transpose();
hint.transpose();
}
if (size.width() < hint.width())
return QTabBar::event(e);
e->accept();
return true;
}
void tabInserted (int index)
{
// get all dock windows
QList<QDockWidget*> dw = getMainWindow()->findChildren<QDockWidget*>();
for (QList<QDockWidget*>::iterator it = dw.begin(); it != dw.end(); ++it) {
// compare tab text and window title to get the right dock window
if (this->tabText(index) == (*it)->windowTitle()) {
QWidget* dock = (*it)->widget();
if (dock) {
QIcon icon = dock->windowIcon();
if (!icon.isNull())
setTabIcon(index, icon);
}
break;
}
}
}
};
#endif
} // namespace Gui
/* TRANSLATOR Gui::MainWindow */
MainWindow::MainWindow(QWidget * parent, Qt::WindowFlags f)
: QMainWindow( parent, f/*WDestructiveClose*/ )
{
d = new MainWindowP;
d->splashscreen = nullptr;
d->activeView = nullptr;
d->whatsthis = false;
d->assistant = new Assistant();
// global access
instance = this;
// support for grouped dragging of dockwidgets
// https://woboq.com/blog/qdockwidget-changes-in-56.html
setDockOptions(dockOptions() | QMainWindow::GroupedDragging);
// Create the layout containing the workspace and a tab bar
d->mdiArea = new QMdiArea();
// Movable tabs
d->mdiArea->setTabsMovable(true);
d->mdiArea->setTabPosition(QTabWidget::South);
d->mdiArea->setViewMode(QMdiArea::TabbedView);
QTabBar* tab = d->mdiArea->findChild<QTabBar*>();
if (tab) {
tab->setTabsClosable(true);
// The tabs might be very wide
tab->setExpanding(false);
}
d->mdiArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
d->mdiArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
d->mdiArea->setOption(QMdiArea::DontMaximizeSubWindowOnActivation, false);
d->mdiArea->setActivationOrder(QMdiArea::ActivationHistoryOrder);
d->mdiArea->setBackground(QBrush(QColor(160,160,160)));
setCentralWidget(d->mdiArea);
statusBar()->setObjectName(QString::fromLatin1("statusBar"));
connect(statusBar(), SIGNAL(messageChanged(const QString &)), this, SLOT(statusMessageChanged()));
// labels and progressbar
d->status = new StatusBarObserver();
d->actionLabel = new QLabel(statusBar());
// d->actionLabel->setMinimumWidth(120);
d->sizeLabel = new QLabel(tr("Dimension"), statusBar());
d->sizeLabel->setMinimumWidth(120);
statusBar()->addWidget(d->actionLabel, 1);
QProgressBar* progressBar = Gui::SequencerBar::instance()->getProgressBar(statusBar());
statusBar()->addPermanentWidget(progressBar, 0);
statusBar()->addPermanentWidget(d->sizeLabel, 0);
// clears the action label
d->actionTimer = new QTimer( this );
d->actionTimer->setObjectName(QString::fromLatin1("actionTimer"));
connect(d->actionTimer, SIGNAL(timeout()), d->actionLabel, SLOT(clear()));
// clear status type
d->statusTimer = new QTimer( this );
d->statusTimer->setObjectName(QString::fromLatin1("statusTimer"));
connect(d->statusTimer, SIGNAL(timeout()), this, SLOT(clearStatus()));
// update gui timer
d->activityTimer = new QTimer(this);
d->activityTimer->setObjectName(QString::fromLatin1("activityTimer"));
connect(d->activityTimer, SIGNAL(timeout()),this, SLOT(_updateActions()));
d->activityTimer->setSingleShot(false);
d->activityTimer->start(150);
// update view-sensitive commands when clipboard has changed
QClipboard *clipbd = QApplication::clipboard();
connect(clipbd, SIGNAL(dataChanged()), this, SLOT(updateEditorActions()));
d->windowMapper = new QSignalMapper(this);
// connection between workspace, window menu and tab bar
connect(d->windowMapper, SIGNAL(mapped(QWidget *)),
this, SLOT(onSetActiveSubWindow(QWidget*)));
connect(d->mdiArea, SIGNAL(subWindowActivated(QMdiSubWindow*)),
this, SLOT(onWindowActivated(QMdiSubWindow* )));
DockWindowManager* pDockMgr = DockWindowManager::instance();
std::string hiddenDockWindows;;
const std::map<std::string,std::string>& config = App::Application::Config();
std::map<std::string, std::string>::const_iterator ht = config.find("HiddenDockWindow");
if (ht != config.end())
hiddenDockWindows = ht->second;
// Show all dockable windows over the workbench facility
//
#if 0
// Toolbox
if (hiddenDockWindows.find("Std_ToolBox") == std::string::npos) {
ToolBox* toolBox = new ToolBox(this);
toolBox->setObjectName(QT_TRANSLATE_NOOP("QDockWidget","Toolbox"));
pDockMgr->registerDockWindow("Std_ToolBox", toolBox);
ToolBoxManager::getInstance()->setToolBox( toolBox );
}
#endif
bool treeView = false, propertyView = false;
if (hiddenDockWindows.find("Std_TreeView") == std::string::npos) {
//work through parameter.
ParameterGrp::handle group = App::GetApplication().GetUserParameter().
GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("DockWindows")->GetGroup("TreeView");
bool enabled = group->GetBool("Enabled", true);
if (enabled != group->GetBool("Enabled", false)) {
enabled = App::GetApplication().GetUserParameter().GetGroup("BaseApp")
->GetGroup("MainWindow")->GetGroup("DockWindows")->GetBool("Std_TreeView",false);
}
group->SetBool("Enabled", enabled); //ensure entry exists.
if (enabled) {
treeView = true;
TreeDockWidget* tree = new TreeDockWidget(nullptr, this);
tree->setObjectName
(QString::fromLatin1(QT_TRANSLATE_NOOP("QDockWidget","Tree view")));
tree->setMinimumWidth(210);
pDockMgr->registerDockWindow("Std_TreeView", tree);
}
}
// Property view
if (hiddenDockWindows.find("Std_PropertyView") == std::string::npos) {
//work through parameter.
ParameterGrp::handle group = App::GetApplication().GetUserParameter().
GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("DockWindows")->GetGroup("PropertyView");
bool enabled = group->GetBool("Enabled", true);
if (enabled != group->GetBool("Enabled", false)) {
enabled = App::GetApplication().GetUserParameter().GetGroup("BaseApp")
->GetGroup("MainWindow")->GetGroup("DockWindows")->GetBool("Std_PropertyView",false);
}
group->SetBool("Enabled", enabled); //ensure entry exists.
if (enabled) {
propertyView = true;
PropertyDockView* pcPropView = new PropertyDockView(nullptr, this);
pcPropView->setObjectName
(QString::fromLatin1(QT_TRANSLATE_NOOP("QDockWidget","Property view")));
pcPropView->setMinimumWidth(210);
pDockMgr->registerDockWindow("Std_PropertyView", pcPropView);
}
}
// Selection view
if (hiddenDockWindows.find("Std_SelectionView") == std::string::npos) {
SelectionView* pcSelectionView = new SelectionView(nullptr, this);
pcSelectionView->setObjectName
(QString::fromLatin1(QT_TRANSLATE_NOOP("QDockWidget","Selection view")));
pcSelectionView->setMinimumWidth(210);
pDockMgr->registerDockWindow("Std_SelectionView", pcSelectionView);
}
// Combo view
if (hiddenDockWindows.find("Std_ComboView") == std::string::npos) {
bool enable = !treeView || !propertyView;
if (!enable) {
ParameterGrp::handle group = App::GetApplication().GetUserParameter().
GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("DockWindows")->GetGroup("ComboView");
enable = group->GetBool("Enabled", true);
}
ComboView* pcComboView = new ComboView(enable, nullptr, this);
pcComboView->setObjectName(QString::fromLatin1(QT_TRANSLATE_NOOP("QDockWidget","Combo View")));
pcComboView->setMinimumWidth(150);
pDockMgr->registerDockWindow("Std_ComboView", pcComboView);
}
// Report view (must be created before PythonConsole!)
if (hiddenDockWindows.find("Std_ReportView") == std::string::npos) {
ReportOutput* pcReport = new ReportOutput(this);
pcReport->setWindowIcon(BitmapFactory().pixmap("MacroEditor"));
pcReport->setObjectName
(QString::fromLatin1(QT_TRANSLATE_NOOP("QDockWidget","Report view")));
pDockMgr->registerDockWindow("Std_ReportView", pcReport);
ReportOutputObserver* rvObserver = new ReportOutputObserver(pcReport);
qApp->installEventFilter(rvObserver);
}
// Python console
if (hiddenDockWindows.find("Std_PythonView") == std::string::npos) {
PythonConsole* pcPython = new PythonConsole(this);
pcPython->setWindowIcon(Gui::BitmapFactory().iconFromTheme("applications-python"));
pcPython->setObjectName
(QString::fromLatin1(QT_TRANSLATE_NOOP("QDockWidget","Python console")));
pDockMgr->registerDockWindow("Std_PythonView", pcPython);
}
//TODO: Add external object support for DAGView
#if 1
//Dag View.
if (hiddenDockWindows.find("Std_DAGView") == std::string::npos) {
//work through parameter.
// old group name
ParameterGrp::handle deprecateGroup = App::GetApplication().GetUserParameter().
GetGroup("BaseApp")->GetGroup("Preferences");
bool enabled = false;
if (deprecateGroup->HasGroup("DAGView")) {
deprecateGroup = deprecateGroup->GetGroup("DAGView");
enabled = deprecateGroup->GetBool("Enabled", enabled);
}
// new group name
ParameterGrp::handle group = App::GetApplication().GetUserParameter().
GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("DockWindows")->GetGroup("DAGView");
enabled = group->GetBool("Enabled", enabled);
group->SetBool("Enabled", enabled); //ensure entry exists.
if (enabled) {
DAG::DockWindow *dagDockWindow = new DAG::DockWindow(nullptr, this);
dagDockWindow->setObjectName
(QString::fromLatin1(QT_TRANSLATE_NOOP("QDockWidget","DAG View")));
pDockMgr->registerDockWindow("Std_DAGView", dagDockWindow);
}
}
#endif
#if 0 //defined(Q_OS_WIN32) this portion of code is not able to run with a vanilla Qtlib build on Windows.
// The MainWindowTabBar is used to show tabbed dock windows with icons
//
// add our own QTabBar-derived class to the main window layout
// NOTE: This uses some private stuff from QMainWindow which doesn't
// seem to be accessible on all platforms.
QMainWindowLayout* l = static_cast<QMainWindowLayout*>(this->layout());
for (int i=0; i<5; i++) {
MainWindowTabBar* result = new MainWindowTabBar(this);
result->setDrawBase(true);
result->setElideMode(Qt::ElideRight);
result->hide(); // avoid to show horizontal bar in top left area
//result->setDocumentMode(_documentMode);
connect(result, SIGNAL(currentChanged(int)), l, SLOT(tabChanged()));
l->unusedTabBars << result;
}
#endif
// accept drops on the window, get handled in dropEvent, dragEnterEvent
setAcceptDrops(true);
statusBar()->showMessage(tr("Ready"), 2001);
}
MainWindow::~MainWindow()
{
delete d->status;
delete d;
instance = nullptr;
}
MainWindow* MainWindow::getInstance()
{
// MainWindow has a public constructor
return instance;
}
QMenu* MainWindow::createPopupMenu ()
{
QMenu* menu = QMainWindow::createPopupMenu();
Workbench* wb = WorkbenchManager::instance()->active();
if (wb) {
MenuItem item;
wb->createMainWindowPopupMenu(&item);
if (item.hasItems()) {
menu->addSeparator();
QList<MenuItem*> items = item.getItems();
for (QList<MenuItem*>::iterator it = items.begin(); it != items.end(); ++it) {
if ((*it)->command() == "Separator") {
menu->addSeparator();
}
else {
Command* cmd = Application::Instance->commandManager().getCommandByName((*it)->command().c_str());
if (cmd) cmd->addTo(menu);
}
}
}
}
return menu;
}
void MainWindow::arrangeIcons()
{
d->mdiArea->tileSubWindows();
}
void MainWindow::tile()
{
d->mdiArea->tileSubWindows();
}
void MainWindow::cascade()
{
d->mdiArea->cascadeSubWindows();
}
void MainWindow::closeActiveWindow ()
{
d->mdiArea->closeActiveSubWindow();
}
int MainWindow::confirmSave(const char *docName, QWidget *parent, bool addCheckbox) {
QMessageBox box(parent?parent:this);
box.setIcon(QMessageBox::Question);
box.setWindowTitle(QObject::tr("Unsaved document"));
if(docName)
box.setText(QObject::tr("Do you want to save your changes to document '%1' before closing?")
.arg(QString::fromUtf8(docName)));
else
box.setText(QObject::tr("Do you want to save your changes to document before closing?"));
box.setInformativeText(QObject::tr("If you don't save, your changes will be lost."));
box.setStandardButtons(QMessageBox::Discard | QMessageBox::Cancel | QMessageBox::Save);
box.setDefaultButton(QMessageBox::Save);
box.setEscapeButton(QMessageBox::Cancel);
QCheckBox checkBox(QObject::tr("Apply answer to all"));
ParameterGrp::handle hGrp;
if(addCheckbox) {
hGrp = App::GetApplication().GetUserParameter().
GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("General");
checkBox.setChecked(hGrp->GetBool("ConfirmAll",false));
checkBox.blockSignals(true);
box.addButton(&checkBox, QMessageBox::ResetRole);
}
// add shortcuts
QAbstractButton* saveBtn = box.button(QMessageBox::Save);
if (saveBtn->shortcut().isEmpty()) {
QString text = saveBtn->text();
text.prepend(QLatin1Char('&'));
saveBtn->setShortcut(QKeySequence::mnemonic(text));
}
QAbstractButton* discardBtn = box.button(QMessageBox::Discard);
if (discardBtn->shortcut().isEmpty()) {
QString text = discardBtn->text();
text.prepend(QLatin1Char('&'));
discardBtn->setShortcut(QKeySequence::mnemonic(text));
}
int res = ConfirmSaveResult::Cancel;
box.adjustSize(); // Silence warnings from Qt on Windows
switch (box.exec())
{
case QMessageBox::Save:
res = checkBox.isChecked()?ConfirmSaveResult::SaveAll:ConfirmSaveResult::Save;
break;
case QMessageBox::Discard:
res = checkBox.isChecked()?ConfirmSaveResult::DiscardAll:ConfirmSaveResult::Discard;
break;
}
if(addCheckbox && res)
hGrp->SetBool("ConfirmAll",checkBox.isChecked());
return res;
}
bool MainWindow::closeAllDocuments (bool close)
{
auto docs = App::GetApplication().getDocuments();
try {
docs = App::Document::getDependentDocuments(docs, true);
}
catch(Base::Exception &e) {
e.ReportException();
}
bool checkModify = true;
bool saveAll = false;
int failedSaves = 0;
for (auto doc : docs) {
auto gdoc = Application::Instance->getDocument(doc);
if (!gdoc)
continue;
if (!gdoc->canClose(false))
return false;
if (!gdoc->isModified()
|| doc->testStatus(App::Document::PartialDoc)
|| doc->testStatus(App::Document::TempDoc))
continue;
bool save = saveAll;
if (!save && checkModify) {
int res = confirmSave(doc->Label.getStrValue().c_str(), this, docs.size()>1);
switch (res)
{
case ConfirmSaveResult::Cancel:
return false;
case ConfirmSaveResult::SaveAll:
saveAll = true;
/* FALLTHRU */
case ConfirmSaveResult::Save:
save = true;
break;
case ConfirmSaveResult::DiscardAll:
checkModify = false;
}
}
if (save && !gdoc->save())
failedSaves++;
}
if (failedSaves > 0) {
int ret = QMessageBox::question(
getMainWindow(),
QObject::tr("%1 Document(s) not saved").arg(QString::number(failedSaves)),
QObject::tr("Some documents could not be saved. Do you want to cancel closing?"),
QMessageBox::Discard | QMessageBox::Cancel,
QMessageBox::Discard);
if (ret == QMessageBox::Cancel)
return false;
}
if (close)
App::GetApplication().closeAllDocuments();
return true;
}
void MainWindow::activateNextWindow ()
{
d->mdiArea->activateNextSubWindow();
}
void MainWindow::activatePreviousWindow ()
{
d->mdiArea->activatePreviousSubWindow();
}
void MainWindow::activateWorkbench(const QString& name)
{
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/View");
bool saveWB = hGrp->GetBool("SaveWBbyTab", false);
QMdiSubWindow* subWin = d->mdiArea->activeSubWindow();
if (subWin /*!= nullptr*/ && saveWB) {
QString currWb = subWin->property("ownWB").toString();
if (currWb.isEmpty() || currWb != name) {
subWin->setProperty("ownWB", name);
}
}
// emit this signal
workbenchActivated(name);
updateActions(true);
}
void MainWindow::whatsThis()
{
QWhatsThis::enterWhatsThisMode();
}
void MainWindow::showDocumentation(const QString& help)
{
Base::PyGILStateLocker lock;
PyObject* module = PyImport_ImportModule("Help");
if (module) {
Py_DECREF(module);
Gui::Command::addModule(Gui::Command::Gui,"Help");
Gui::Command::doCommand(Gui::Command::Gui,"Help.show(\"%s\")", help.toStdString().c_str());
}
else {
PyErr_Clear();
QUrl url(help);
if (url.scheme().isEmpty()) {
QMessageBox msgBox(getMainWindow());
msgBox.setWindowTitle(tr("Help addon needed!"));
msgBox.setText(tr("The Help system of %1 is now handled by the \"Help\" addon. "
"It can easily be installed via the Addons Manager").arg(QString(qApp->applicationName())));
QAbstractButton* pButtonAddonMgr = msgBox.addButton(tr("Open Addon Manager"), QMessageBox::YesRole);
msgBox.addButton(QMessageBox::Ok);
msgBox.exec();
if (msgBox.clickedButton() == pButtonAddonMgr) {
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Addons");
hGrp->SetASCII("SelectedAddon", "Help");
Gui::Command::doCommand(Gui::Command::Gui,"Gui.runCommand('Std_AddonMgr',0)");
}
}
else {
QDesktopServices::openUrl(url);
}
}
}
bool MainWindow::event(QEvent *e)
{
if (e->type() == QEvent::EnterWhatsThisMode) {
// Unfortunately, for top-level widgets such as menus or dialogs we
// won't be notified when the user clicks the link in the hypertext of
// the what's this text. Thus, we have to install the main window to
// the application to observe what happens in eventFilter().
d->whatstext.clear();
if (!d->whatsthis) {
d-> whatsthis = true;
qApp->installEventFilter(this);
}
}
else if (e->type() == QEvent::LeaveWhatsThisMode) {
// Here we can't do anything because this event is sent
// before the WhatThisClicked event is sent. Thus, we handle
// this in eventFilter().
}
else if (e->type() == QEvent::WhatsThisClicked) {
QWhatsThisClickedEvent* wt = static_cast<QWhatsThisClickedEvent*>(e);
showDocumentation(wt->href());
}
else if (e->type() == QEvent::ApplicationWindowIconChange) {
// if application icon changes apply it to the main window and the "About..." dialog
this->setWindowIcon(QApplication::windowIcon());
Command* about = Application::Instance->commandManager().getCommandByName("Std_About");
if (about) {
Action* action = about->getAction();
if (action) action->setIcon(QApplication::windowIcon());
}
}
else if (e->type() == Spaceball::ButtonEvent::ButtonEventType) {
Spaceball::ButtonEvent *buttonEvent = dynamic_cast<Spaceball::ButtonEvent *>(e);
if (!buttonEvent)
return true;
buttonEvent->setHandled(true);
//only going to respond to button press events.
if (buttonEvent->buttonStatus() != Spaceball::BUTTON_PRESSED)
return true;
ParameterGrp::handle group = App::GetApplication().GetUserParameter().GetGroup("BaseApp")->
GetGroup("Spaceball")->GetGroup("Buttons");
QByteArray groupName(QVariant(buttonEvent->buttonNumber()).toByteArray());
if (group->HasGroup(groupName.data())) {
ParameterGrp::handle commandGroup = group->GetGroup(groupName.data());
std::string commandName(commandGroup->GetASCII("Command"));
if (commandName.empty())
return true;
else
Application::Instance->commandManager().runCommandByName(commandName.c_str());
}
else
return true;
}
else if (e->type() == Spaceball::MotionEvent::MotionEventType) {
Spaceball::MotionEvent *motionEvent = dynamic_cast<Spaceball::MotionEvent *>(e);
if (!motionEvent)
return true;
motionEvent->setHandled(true);
Gui::Document *doc = Application::Instance->activeDocument();
if (!doc)
return true;
View3DInventor *temp = dynamic_cast<View3DInventor *>(doc->getActiveView());
if (!temp)
return true;
View3DInventorViewer *view = temp->getViewer();
if (view) {
Spaceball::MotionEvent anotherEvent(*motionEvent);
qApp->sendEvent(view, &anotherEvent);
}
return true;
}else if(e->type() == QEvent::StatusTip) {
// make sure warning and error message don't get blocked by tooltips
if(std::abs(d->currentStatusType) <= MainWindow::Wrn)
return true;
}
return QMainWindow::event(e);
}
bool MainWindow::eventFilter(QObject* o, QEvent* e)
{
if (o != this) {
if (e->type() == QEvent::WindowStateChange) {
// notify all mdi views when the active view receives a show normal, show minimized
// or show maximized event
MDIView * view = qobject_cast<MDIView*>(o);
if (view) { // emit this signal
Qt::WindowStates oldstate = static_cast<QWindowStateChangeEvent*>(e)->oldState();
Qt::WindowStates newstate = view->windowState();
if (oldstate != newstate)
windowStateChanged(view);
}
}
// We don't want to show the bubble help for the what's this text but want to
// start the help viewer with the according key word.
// Thus, we have to observe WhatThis events if called for a widget, use its text and
// must avoid to make the bubble widget visible.
if (e->type() == QEvent::WhatsThis) {
if (!o->isWidgetType())
return false;
// clicked on a widget in what's this mode
QWidget * w = static_cast<QWidget *>(o);
d->whatstext = w->whatsThis();
}
if (e->type() == QEvent::WhatsThisClicked) {
// if the widget is a top-level window
if (o->isWidgetType() && qobject_cast<QWidget*>(o)->isWindow()) {
// re-direct to the widget
QApplication::sendEvent(this, e);
}
}
// special treatment for menus because they directly call QWhatsThis::showText()
// whereby we must be informed for which action the help should be shown
if (o->inherits("QMenu") && QWhatsThis::inWhatsThisMode()) {
bool whatthis = false;
if (e->type() == QEvent::KeyPress) {
QKeyEvent* ke = static_cast<QKeyEvent*>(e);
if (ke->key() == Qt::Key_Return || ke->key() == Qt::Key_Enter || ke->key() == Qt::Key_F1)
whatthis = true;
}
else if (e->type() == QEvent::MouseButtonRelease)
whatthis = true;
else if (e->type() == QEvent::EnterWhatsThisMode)
whatthis = true;
if (whatthis) {
QAction* cur = static_cast<QMenu*>(o)->activeAction();
if (cur) {
// get the help text for later usage
QString s = cur->whatsThis();
if (s.isEmpty())
s = static_cast<QMenu*>(o)->whatsThis();
d->whatstext = s;
}
}
}
if (o->inherits("QWhatsThat") && e->type() == QEvent::Show) {
// the bubble help should become visible which we avoid by marking the widget
// that it is out of range. Instead of, we show the help viewer
if (!d->whatstext.isEmpty()) {
QWhatsThisClickedEvent e(d->whatstext);
QApplication::sendEvent(this, &e);
}
static_cast<QWidget *>(o)->setAttribute(Qt::WA_OutsideWSRange);
o->deleteLater();
return true;
}
if (o->inherits("QWhatsThat") && e->type() == QEvent::Hide) {
// leave what's this mode
if (d->whatsthis) {
d->whatsthis = false;
d->whatstext.clear();
qApp->removeEventFilter(this);
}
}
}
return QMainWindow::eventFilter(o, e);
}
void MainWindow::addWindow(MDIView* view)
{
// make workspace parent of view
bool isempty = d->mdiArea->subWindowList().isEmpty();
QMdiSubWindow* child = qobject_cast<QMdiSubWindow*>(view->parentWidget());
if(!child) {
child = new QMdiSubWindow(d->mdiArea->viewport());
child->setAttribute(Qt::WA_DeleteOnClose);
child->setWidget(view);
child->setWindowIcon(view->windowIcon());
QMenu* menu = child->systemMenu();
// See StdCmdCloseActiveWindow (#0002631)
QList<QAction*> acts = menu->actions();
for (QList<QAction*>::iterator it = acts.begin(); it != acts.end(); ++it) {
if ((*it)->shortcut() == QKeySequence(QKeySequence::Close)) {
(*it)->setShortcuts(QList<QKeySequence>());
break;
}
}
QAction* action = menu->addAction(tr("Close All"));
connect(action, SIGNAL(triggered()), d->mdiArea, SLOT(closeAllSubWindows()));
d->mdiArea->addSubWindow(child);
}
connect(view, SIGNAL(message(const QString&, int)),
this, SLOT(showMessage(const QString&, int)));
connect(this, SIGNAL(windowStateChanged(MDIView*)),
view, SLOT(windowStateChanged(MDIView*)));
// listen to the incoming events of the view
view->installEventFilter(this);
// show the very first window in maximized mode
if (isempty)
view->showMaximized();
else
view->show();
}
/**
* Removes the instance of Gui::MDiView from the main window and sends am event
* to the parent widget, a QMdiSubWindow to delete itself.
* If you want to avoid that the Gui::MDIView instance gets destructed too you
* must reparent it afterwards, e.g. set parent to NULL.
*/
void MainWindow::removeWindow(Gui::MDIView* view, bool close)
{
// free all connections
disconnect(view, SIGNAL(message(const QString&, int)),
this, SLOT(showMessage(const QString&, int )));
disconnect(this, SIGNAL(windowStateChanged(MDIView*)),
view, SLOT(windowStateChanged(MDIView*)));
view->removeEventFilter(this);
// check if the focus widget is a child of the view
QWidget* foc = this->focusWidget();
if (foc) {
QWidget* par = foc->parentWidget();
while (par) {
if (par == view) {
foc->clearFocus();
break;
}
par = par->parentWidget();
}
}
QWidget* parent = view->parentWidget();
// The call of 'd->mdiArea->removeSubWindow(parent)' causes the QMdiSubWindow
// to lose its parent and thus the notification in QMdiSubWindow::closeEvent
// of other mdi windows to get maximized if this window is maximized will fail.
// However, we must let it here otherwise deleting MDI child views directly can
// cause other problems.
//
// The above mentioned problem can be fixed by setParent(0) which triggers a
// ChildRemoved event being handled properly inside QMidArea::viewportEvent()
//
auto subwindow = qobject_cast<QMdiSubWindow*>(parent);
if(subwindow && d->mdiArea->subWindowList().contains(subwindow)) {
subwindow->setParent(nullptr);
assert(!d->mdiArea->subWindowList().contains(subwindow));
// d->mdiArea->removeSubWindow(parent);
}
if(close)
parent->deleteLater();
updateActions();
}
void MainWindow::tabChanged(MDIView* view)
{
Q_UNUSED(view);
updateActions();
}
void MainWindow::tabCloseRequested(int index)
{
QTabBar* tab = d->mdiArea->findChild<QTabBar*>();
if (index < 0 || index >= tab->count())
return;
const QList<QMdiSubWindow *> subWindows = d->mdiArea->subWindowList();
Q_ASSERT(index < subWindows.size());
QMdiSubWindow *subWindow = d->mdiArea->subWindowList().at(index);
Q_ASSERT(subWindow);
subWindow->close();
updateActions();
}
void MainWindow::onSetActiveSubWindow(QWidget *window)
{