]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_MainMenu.cpp
Salome HOME
Merge branch 'master' of newgeom:newgeom.git
[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   setFixedHeight(menuHeight());
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   return aPage;
52 }
53
54 /*
55  * Searches for already created workbench with given name.
56  */
57 XGUI_Workbench* XGUI_MainMenu::findWorkbench(const QString& theObjName) const
58 {
59   return myDesktop->findChild<XGUI_Workbench*>(theObjName);
60 }
61
62
63 bool XGUI_MainMenu::eventFilter(QObject *theWatched, QEvent *theEvent)
64 {
65   if (theWatched == myGeneralPage) {
66     if (theEvent->type() == QEvent::Show) {
67       myGeneralPage->parentWidget()->setMaximumWidth(16777215);
68       myGeneralPage->removeEventFilter(this);
69     }
70   }
71   return QObject::eventFilter(theWatched, theEvent);
72 }
73
74 void XGUI_MainMenu::insertConsole(QWidget* theConsole)
75 {
76   int aConsoleTabId = myMenuTabs->addTab(theConsole, "Console");
77
78   QTabBar* aTabBar = myMenuTabs->findChild<QTabBar*>();
79   QPushButton* aCloseTabButton = new QPushButton();
80   aCloseTabButton->setFixedSize(16, 16);
81   aCloseTabButton->setIcon(QIcon(":pictures/wnd_close.png"));
82   aCloseTabButton->setFlat(true);
83   aTabBar->setTabButton(aConsoleTabId,
84                         QTabBar::RightSide,
85                         aCloseTabButton);
86
87   connect(aCloseTabButton, SIGNAL(clicked()),
88           myDesktop, SLOT(dockPythonConsole()));
89 }
90
91 void XGUI_MainMenu::removeConsole()
92 {
93   const int kLastTab = myMenuTabs->count() - 1;
94   myMenuTabs->removeTab(kLastTab);
95 }
96
97 XGUI_Command* XGUI_MainMenu::feature(const QString& theId) const
98 {
99   XGUI_Command* result;
100   result = myGeneralPage->feature(theId);
101   if (!result) {
102     for (int aTabIdx = 0; aTabIdx < myMenuTabs->count(); ++aTabIdx) {
103       XGUI_Workbench* aWbn = static_cast<XGUI_Workbench*>(myMenuTabs->widget(aTabIdx));
104       result = aWbn->feature(theId);
105       if (result) break;
106     }
107   }
108   return result;
109 }
110
111 QList<XGUI_Command*> XGUI_MainMenu::features() const
112 {
113   QList<XGUI_Command*> aList = myGeneralPage->features();
114   for (int aTabIdx = 0; aTabIdx < myMenuTabs->count(); ++aTabIdx) {
115     XGUI_Workbench* aWbn = static_cast<XGUI_Workbench*>(myMenuTabs->widget(aTabIdx));
116     aList.append(aWbn->features());
117   }
118   return aList;
119 }
120
121 int XGUI_MainMenu::menuItemSize() const
122 {
123   int DEFAULT_ITEM_SIZE = XGUI_Preferences::resourceMgr()->integerValue(
124     XGUI_Preferences::MENU_SECTION, "item_size");
125   return DEFAULT_ITEM_SIZE;
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 = 4;
133   return rows * menuItemSize() + kMarginsSpacings;
134 }
135
136 int XGUI_MainMenu::menuItemRowsCount() const
137 {
138   int DEFAULT_ITEM_ROWS_COUNT = XGUI_Preferences::resourceMgr()->integerValue(
139     XGUI_Preferences::MENU_SECTION, "rows_number");
140   return DEFAULT_ITEM_ROWS_COUNT;
141 }
142
143 void XGUI_MainMenu::updateFromResources()
144 {
145 }