Salome HOME
96106a9111357da303ce4d14993cd204893aa057
[modules/shaper.git] / src / XGUI / XGUI_MainMenu.cpp
1 #include <XGUI_MainMenu.h>
2 #include <XGUI_Workbench.h>
3 #include <XGUI_MainWindow.h>
4 #include <XGUI_Command.h>
5
6 #include <QLayout>
7 #include <QTabWidget>
8 #include <QLabel>
9 #include <QDockWidget>
10 #include <QEvent>
11
12 XGUI_MainMenu::XGUI_MainMenu(XGUI_MainWindow *parent)
13     : QObject(parent), myDesktop(parent)
14 {
15   parent->setTabPosition(Qt::TopDockWidgetArea, QTabWidget::North);
16   myGeneralPage = addWorkbench(tr("General"));
17   myGeneralPage->parentWidget()->setMaximumWidth(200);
18   myGeneralPage->installEventFilter(this);
19 }
20
21 XGUI_MainMenu::~XGUI_MainMenu(void)
22 {
23 }
24
25 XGUI_Workbench* XGUI_MainMenu::addWorkbench(const QString& theId, const QString& theTitle)
26 {
27   QDockWidget* aDock = new QDockWidget(myDesktop);
28   aDock->setFeatures(QDockWidget::DockWidgetVerticalTitleBar);
29   aDock->setAllowedAreas(Qt::TopDockWidgetArea);
30   QString aTitle = theTitle;
31   if (aTitle.isEmpty()) {
32     aTitle = tr(theId.toLatin1().constData());
33   }
34   aDock->setWindowTitle(aTitle);
35   aDock->setMinimumHeight(30);
36   aDock->setContentsMargins(0, 0, 0, 0);
37
38   XGUI_Workbench* aPage = new XGUI_Workbench(aDock);
39   aPage->setObjectName(theId);
40   aDock->setWidget(aPage);
41
42   myDesktop->addDockWidget(Qt::TopDockWidgetArea, aDock);
43   if (myMenuTabs.length() > 1) {
44     myDesktop->tabifyDockWidget(myMenuTabs.last(), aDock);
45   }
46
47   myMenuTabs.append(aDock);
48   return aPage;
49 }
50
51 /*
52  * Searches for already created workbench with given name.
53  */
54 XGUI_Workbench* XGUI_MainMenu::findWorkbench(const QString& theObjName)
55 {
56   return myDesktop->findChild<XGUI_Workbench*>(theObjName);
57 }
58
59
60 bool XGUI_MainMenu::eventFilter(QObject *theWatched, QEvent *theEvent)
61 {
62   if (theWatched == myGeneralPage) {
63     if (theEvent->type() == QEvent::Show) {
64       myGeneralPage->parentWidget()->setMaximumWidth(16777215);
65       myGeneralPage->removeEventFilter(this);
66     }
67   }
68   return QObject::eventFilter(theWatched, theEvent);
69 }
70
71 XGUI_Command* XGUI_MainMenu::feature(const QString& theId) const
72 {
73   QList<QDockWidget*>::const_iterator aIt;
74   for (aIt = myMenuTabs.constBegin(); aIt != myMenuTabs.constEnd(); ++aIt) {
75     XGUI_Workbench* aWbn = static_cast<XGUI_Workbench*>((*aIt)->widget());
76     XGUI_Command* aCmd = aWbn->feature(theId);
77     if (aCmd)
78       return aCmd;
79   }
80   return 0;
81 }
82
83 QList<XGUI_Command*> XGUI_MainMenu::features() const
84 {
85   QList<XGUI_Command*> aList;
86   QList<QDockWidget*>::const_iterator aIt;
87   for (aIt = myMenuTabs.constBegin(); aIt != myMenuTabs.constEnd(); ++aIt) {
88     XGUI_Workbench* aWbn = static_cast<XGUI_Workbench*>((*aIt)->widget());
89     aList.append(aWbn->features());
90   }
91   return aList;
92 }
93
94 void XGUI_MainMenu::onFeatureChecked(bool isChecked)
95 {
96   if (!isChecked) {
97     restoreCommandState();
98     return;
99   }
100
101   saveCommandsState();
102   QStringList aSkippedIds;
103   XGUI_Command* aToggledFeature = dynamic_cast<XGUI_Command*>(sender());
104   aSkippedIds.append(aToggledFeature->unblockableCommands());
105 //  aSkippedIds.append(aToggledFeature->id());
106   XGUI_Workbench* aGeneralWB = findWorkbench(tr("General"));
107   foreach(XGUI_Command* eachFeature, aGeneralWB->features()) {
108     aSkippedIds.append(eachFeature->id());
109   }
110   QList<XGUI_Command*> allFeatures = features();
111   foreach(XGUI_Command* eachFeature, allFeatures) {
112     QString aFeatureId = eachFeature->id();
113     if (aSkippedIds.removeAll(aFeatureId) > 0) {
114       continue;
115     }
116     eachFeature->setEnabled(false);
117   }
118 }
119
120 void XGUI_MainMenu::saveCommandsState()
121 {
122   myCommandState.clear();
123   QList<XGUI_Command*> allFeatures = features();
124   XGUI_Command* eachFeature = NULL;
125   foreach(eachFeature, allFeatures) {
126     myCommandState.insert(eachFeature, eachFeature->enabled());
127   }
128 }
129
130 void XGUI_MainMenu::restoreCommandState()
131 {
132   QList<XGUI_Command*> allFeatures = features();
133   XGUI_Command* eachFeature = NULL;
134   foreach(eachFeature, allFeatures) {
135     eachFeature->setChecked(false);
136     eachFeature->setEnabled(myCommandState[eachFeature]);
137   }
138 }