Salome HOME
Sources formated according to the codeing standards
[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),
20       myDesktop(parent)
21 {
22   myGeneralPage = new XGUI_MenuGroupPanel(this);
23   myGeneralPage->setObjectName("Default");
24   myGeneralPage->parentWidget()->setMaximumWidth(200);
25   myGeneralPage->installEventFilter(this);
26   myGeneralPage->setFrameStyle(QFrame::StyledPanel);
27
28   myMenuTabs = new QTabWidget(this);
29   myMenuTabs->setStyleSheet("QTabBar::tab {height: 24px;} QTabWidget:pane {border: 0px;}");
30   QHBoxLayout* aMainLayout = new QHBoxLayout(this);
31   aMainLayout->addWidget(myGeneralPage);
32   aMainLayout->addWidget(myMenuTabs);
33   aMainLayout->setContentsMargins(0, 2, 2, 0);
34   aMainLayout->setSpacing(2);
35   setLayout(aMainLayout);
36   updateFromResources();
37 }
38
39 XGUI_MainMenu::~XGUI_MainMenu(void)
40 {
41 }
42
43 XGUI_Workbench* XGUI_MainMenu::addWorkbench(const QString& theId, const QString& theTitle)
44 {
45   QString aTitle = theTitle;
46   if (aTitle.isEmpty()) {
47     aTitle = tr(theId.toLatin1().constData());
48   }
49   XGUI_Workbench* aPage = new XGUI_Workbench(myMenuTabs);
50   aPage->setObjectName(theId);
51   myMenuTabs->addTab(aPage, aTitle);
52   myWorkbenches.append(aPage);
53   return aPage;
54 }
55
56 /*
57  * Searches for already created workbench with given name.
58  */
59 XGUI_Workbench* XGUI_MainMenu::findWorkbench(const QString& theObjName) const
60 {
61   return myDesktop->findChild<XGUI_Workbench*>(theObjName);
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, QTabBar::RightSide, aCloseTabButton);
85
86   connect(aCloseTabButton, SIGNAL(clicked()), myDesktop, SLOT(dockPythonConsole()));
87 }
88
89 void XGUI_MainMenu::removeConsole()
90 {
91   const int kLastTab = myMenuTabs->count() - 1;
92   myMenuTabs->removeTab(kLastTab);
93 }
94
95 XGUI_Command* XGUI_MainMenu::feature(const QString& theId) const
96 {
97   XGUI_Command* result;
98   result = myGeneralPage->feature(theId);
99   if (!result) {
100     XGUI_Workbench* aWbn;
101     foreach (aWbn, myWorkbenches)
102     {
103       result = aWbn->feature(theId);
104       if (result)
105         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   XGUI_Workbench* aWbn;
115   foreach (aWbn, myWorkbenches)
116   {
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(XGUI_Preferences::MENU_SECTION,
140                                                                  "rows_number", kDefaultRowsCount);
141   return aRowsCount;
142 }
143
144 void XGUI_MainMenu::updateFromResources()
145 {
146   setFixedHeight(menuHeight());
147   repaint();
148 }