Salome HOME
Refresh menu size after chnges in preferences
[modules/shaper.git] / src / XGUI / XGUI_MainMenu.cpp
1 #include <XGUI_MainMenu.h>
2 #include <XGUI_Workbench.h>
3 #include <XGUI_MenuGroupPanel.h>
4 #include <XGUI_MainWindow.h>
5 #include <XGUI_Command.h>
6 #include <XGUI_Preferences.h>
7
8 #include <SUIT_ResourceMgr.h>
9
10 #include <QLayout>
11 #include <QTabWidget>
12 #include <QLabel>
13 #include <QDockWidget>
14 #include <QEvent>
15 #include <QPushButton>
16 #include <QTabBar>
17
18 XGUI_MainMenu::XGUI_MainMenu(XGUI_MainWindow *parent)
19     : QWidget(parent), myDesktop(parent)
20 {
21   myGeneralPage = new XGUI_MenuGroupPanel(this);
22   myGeneralPage->setObjectName("Default");
23   myGeneralPage->parentWidget()->setMaximumWidth(200);
24   myGeneralPage->installEventFilter(this);
25   myGeneralPage->setFrameStyle(QFrame::StyledPanel);
26
27   myMenuTabs = new QTabWidget(this);
28   myMenuTabs->setStyleSheet("QTabBar::tab {height: 24px;} QTabWidget:pane {border: 0px;}");
29   QHBoxLayout* aMainLayout = new QHBoxLayout(this);
30   aMainLayout->addWidget(myGeneralPage);
31   aMainLayout->addWidget(myMenuTabs);
32   aMainLayout->setContentsMargins(0, 2, 2, 0);
33   aMainLayout->setSpacing(2);
34   setLayout(aMainLayout);
35   updateFromResources();
36 }
37
38 XGUI_MainMenu::~XGUI_MainMenu(void)
39 {
40 }
41
42 XGUI_Workbench* XGUI_MainMenu::addWorkbench(const QString& theId, const QString& theTitle)
43 {
44   QString aTitle = theTitle;
45   if (aTitle.isEmpty()) {
46     aTitle = tr(theId.toLatin1().constData());
47   }
48   XGUI_Workbench* aPage = new XGUI_Workbench(myMenuTabs);
49   aPage->setObjectName(theId);
50   myMenuTabs->addTab(aPage, aTitle);
51   myWorkbenches.append(aPage);
52   return aPage;
53 }
54
55 /*
56  * Searches for already created workbench with given name.
57  */
58 XGUI_Workbench* XGUI_MainMenu::findWorkbench(const QString& theObjName) const
59 {
60   return myDesktop->findChild<XGUI_Workbench*>(theObjName);
61 }
62
63
64 bool XGUI_MainMenu::eventFilter(QObject *theWatched, QEvent *theEvent)
65 {
66   if (theWatched == myGeneralPage) {
67     if (theEvent->type() == QEvent::Show) {
68       myGeneralPage->parentWidget()->setMaximumWidth(16777215);
69       myGeneralPage->removeEventFilter(this);
70     }
71   }
72   return QObject::eventFilter(theWatched, theEvent);
73 }
74
75 void XGUI_MainMenu::insertConsole(QWidget* theConsole)
76 {
77   int aConsoleTabId = myMenuTabs->addTab(theConsole, "Console");
78
79   QTabBar* aTabBar = myMenuTabs->findChild<QTabBar*>();
80   QPushButton* aCloseTabButton = new QPushButton();
81   aCloseTabButton->setFixedSize(16, 16);
82   aCloseTabButton->setIcon(QIcon(":pictures/wnd_close.png"));
83   aCloseTabButton->setFlat(true);
84   aTabBar->setTabButton(aConsoleTabId,
85                         QTabBar::RightSide,
86                         aCloseTabButton);
87
88   connect(aCloseTabButton, SIGNAL(clicked()),
89           myDesktop, SLOT(dockPythonConsole()));
90 }
91
92 void XGUI_MainMenu::removeConsole()
93 {
94   const int kLastTab = myMenuTabs->count() - 1;
95   myMenuTabs->removeTab(kLastTab);
96 }
97
98 XGUI_Command* XGUI_MainMenu::feature(const QString& theId) const
99 {
100   XGUI_Command* result;
101   result = myGeneralPage->feature(theId);
102   if (!result) {
103     XGUI_Workbench* aWbn;
104     foreach (aWbn, myWorkbenches) {
105       result = aWbn->feature(theId);
106       if (result) break;
107     }
108   }
109   return result;
110 }
111
112 QList<XGUI_Command*> XGUI_MainMenu::features() const
113 {
114   QList<XGUI_Command*> aList = myGeneralPage->features();
115   XGUI_Workbench* aWbn;
116   foreach (aWbn, myWorkbenches) {
117     aList.append(aWbn->features());
118   }
119   return aList;
120 }
121
122 int XGUI_MainMenu::menuItemSize() const
123 {
124   const int kDefaultItemSize = 25;
125   return kDefaultItemSize;
126 }
127
128 int XGUI_MainMenu::menuHeight() const
129 {
130   // Default group has no tabs above --> one extra row
131   int rows = menuItemRowsCount() + 1;
132   const int kMarginsSpacings = 5;
133   return rows * menuItemSize() + kMarginsSpacings;
134 }
135
136 int XGUI_MainMenu::menuItemRowsCount() const
137 {
138   const int kDefaultRowsCount = 3;
139   int aRowsCount = XGUI_Preferences::resourceMgr()->integerValue(
140     XGUI_Preferences::MENU_SECTION, "rows_number", kDefaultRowsCount);
141   return aRowsCount;
142 }
143
144 void XGUI_MainMenu::updateFromResources()
145 {
146   setFixedHeight(menuHeight());
147   repaint();
148 }