Salome HOME
Create dialog boxes
[modules/shaper.git] / src / SHAPERGUI / SHAPERGUI_ToolbarsMgr.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "SHAPERGUI_ToolbarsMgr.h"
22 #include "SHAPERGUI.h"
23
24 #include <CAM_Application.h>
25 #include <SUIT_Desktop.h>
26
27 #include <QLayout>
28 #include <QDialogButtonBox>
29 #include <QLabel>
30 #include <QListWidget>
31 #include <QPushButton>
32 #include <QInputDialog>
33 #include <QMessageBox>
34
35
36 class SHAPERGUI_CommandIdItem : public QListWidgetItem
37 {
38 public:
39   SHAPERGUI_CommandIdItem(QListWidget* theParent, int theId, SHAPERGUI* theModule)
40     : QListWidgetItem(theParent), myId(theId), myModule(theModule) {}
41
42
43   virtual QVariant data(int theRole) const {
44     if (theRole == Qt::DisplayRole) {
45       if (myId == -1)
46         return "------";
47       QAction* aAction = myModule->action(myId);
48       if (aAction)
49         return aAction->text();
50     }
51     return QListWidgetItem::data(theRole);
52   }
53
54
55 private:
56   SHAPERGUI* myModule;
57   int myId;
58 };
59
60
61 //************************************************************************************
62 //************************************************************************************
63 //************************************************************************************
64 SHAPERGUI_ToolbarsDlg::SHAPERGUI_ToolbarsDlg(SHAPERGUI* theModule,
65   const QIntList& theActionsList,
66   const QMap<QString, QIntList>& theToolbars)
67   : QDialog(theModule->application()->desktop()),
68   myModule(theModule),
69   myActionsList(theActionsList),
70   myToolbars(theToolbars)
71 {
72   setWindowTitle(tr("Toolbars"));
73   QVBoxLayout* aMailLayout = new QVBoxLayout(this);
74
75   // Controls part of the dialog
76   QWidget* aControlsWgt = new QWidget(this);
77   QHBoxLayout* aContolsLay = new QHBoxLayout(aControlsWgt);
78   aContolsLay->setContentsMargins(0, 0, 0, 0);
79   aMailLayout->addWidget(aControlsWgt);
80
81   // Right controls
82   QWidget* aListWgt = new QWidget(aControlsWgt);
83   QVBoxLayout* aListLayout = new QVBoxLayout(aListWgt);
84   aListLayout->setContentsMargins(0, 0, 0, 0);
85   aContolsLay->addWidget(aListWgt);
86
87   aListLayout->addWidget(new QLabel(tr("Toolbars:"), aListWgt));
88
89   myToolbarsList = new QListWidget(aListWgt);
90   myToolbarsList->addItems(theToolbars.keys());
91   aListLayout->addWidget(myToolbarsList);
92
93   // Left controls
94   QWidget* aButtonsWgt = new QWidget(aControlsWgt);
95   QVBoxLayout* aBtnLayout = new QVBoxLayout(aButtonsWgt);
96   aBtnLayout->setContentsMargins(0, 0, 0, 0);
97   aContolsLay->addWidget(aButtonsWgt);
98
99   QPushButton* aAddBtn = new QPushButton(tr("Add..."), aButtonsWgt);
100   connect(aAddBtn, SIGNAL(clicked(bool)), SLOT(onAdd()));
101   aBtnLayout->addWidget(aAddBtn);
102
103   QPushButton* aEditBtn = new QPushButton(tr("Edit..."), aButtonsWgt);
104   connect(aEditBtn, SIGNAL(clicked(bool)), SLOT(onEdit()));
105   aBtnLayout->addWidget(aEditBtn);
106
107   QPushButton* aDeleteBtn = new QPushButton(tr("Delete"), aButtonsWgt);
108   connect(aDeleteBtn, SIGNAL(clicked(bool)), SLOT(onDelete()));
109   aBtnLayout->addWidget(aDeleteBtn);
110   aBtnLayout->addStretch(1);
111
112   // Buttons part of the dialog
113   QDialogButtonBox* aButtons =
114     new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this);
115   aMailLayout->addWidget(aButtons);
116   connect(aButtons, SIGNAL(accepted()), SLOT(accept()));
117   connect(aButtons, SIGNAL(rejected()), SLOT(reject()));
118 }
119
120 void SHAPERGUI_ToolbarsDlg::onAdd()
121 {
122   QString aNewToolbar =
123     QInputDialog::getText(this, tr("Create toolbar"), tr("Name of a new toolbar"));
124   if (!(aNewToolbar.isNull() || aNewToolbar.isEmpty())) {
125     if (!myToolbars.contains(aNewToolbar)) {
126       myToolbars[aNewToolbar] = QIntList();
127       updateToolbarsList();
128     }
129     else {
130       QString aMsg = tr("A tool bar with name %1 already exists").arg(aNewToolbar);
131       QMessageBox::warning(this, tr("Create toolbar"), aMsg);
132     }
133   }
134 }
135
136 void SHAPERGUI_ToolbarsDlg::onEdit()
137 {
138   QList<QListWidgetItem*> aSelected = myToolbarsList->selectedItems();
139   if (aSelected.size() == 1) {
140     QString aToolbarName = aSelected.first()->text();
141     QIntList aFreeItems = getFreeCommands();
142     SHAPERGUI_ToolbarItemsDlg aDlg(this, myModule,
143       aToolbarName, aFreeItems, myToolbars[aToolbarName]);
144     aDlg.exec();
145   }
146 }
147
148 void SHAPERGUI_ToolbarsDlg::onDelete()
149 {
150   QList<QListWidgetItem*> aSelected = myToolbarsList->selectedItems();
151   if (aSelected.size() == 1) {
152     QString aToolbarName = aSelected.first()->text();
153     QString aMsg = tr("Toolbar %1 will be deleted. Continue?").arg(aToolbarName);
154     if (QMessageBox::question(this, tr("Delete toolbar"), aMsg) == QMessageBox::Yes) {
155       myToolbars.remove(aToolbarName);
156       updateToolbarsList();
157     }
158   }
159 }
160
161 void SHAPERGUI_ToolbarsDlg::updateToolbarsList()
162 {
163   myToolbarsList->clear();
164   myToolbarsList->addItems(myToolbars.keys());
165 }
166
167 QIntList SHAPERGUI_ToolbarsDlg::getFreeCommands() const
168 {
169   QIntList aFreeCommands;
170   QMap<QString, QIntList>::const_iterator aIt;
171   foreach(int aCmd, myActionsList) {
172     bool aIsFree = true;
173     for (aIt = myToolbars.cbegin(); aIt != myToolbars.cend(); aIt++) {
174       if (aIt.value().contains(aCmd)) {
175         aIsFree = false;
176         break;
177       }
178     }
179     if (aIsFree)
180       aFreeCommands.append(aCmd);
181   }
182   return aFreeCommands;
183 }
184
185
186 //************************************************************************************
187 //************************************************************************************
188 //************************************************************************************
189 SHAPERGUI_ToolbarItemsDlg::SHAPERGUI_ToolbarItemsDlg(QWidget* theParent,
190   SHAPERGUI* theModule,
191   const QString& theToolbar,
192   const QIntList& theFreeItems,
193   const QIntList& theItemsList)
194   : QDialog(theParent),
195   myModule(theModule),
196   myFreeItems(theFreeItems),
197   myToolItems(theItemsList)
198 {
199   setWindowTitle(tr("Edit toolbar items"));
200
201   QVBoxLayout* aMailLayout = new QVBoxLayout(this);
202
203   // Name of toolbar
204   QWidget* aNameWgt = new QWidget(this);
205   QHBoxLayout* aNameLay = new QHBoxLayout(aNameWgt);
206   aNameLay->setContentsMargins(0, 0, 0, 0);
207   aMailLayout->addWidget(aNameWgt);
208
209   aNameLay->addWidget(new QLabel(tr("Toolbar name:"), aNameWgt));
210   QLabel* aNameLbl = new QLabel(theToolbar, aNameWgt);
211   QFont aFont = aNameLbl->font();
212   aFont.setBold(true);
213   aNameLbl->setFont(aFont);
214   aNameLay->addWidget(aNameLbl);
215   aNameLay->addStretch(1);
216
217   // Lists widget
218   QWidget* aControlsWgt = new QWidget(this);
219   QHBoxLayout* aCtrlLayout = new QHBoxLayout(aControlsWgt);
220   aCtrlLayout->setContentsMargins(0, 0, 0, 0);
221   aMailLayout->addWidget(aControlsWgt);
222
223   // Left list
224   QWidget* aCommandsWgt = new QWidget(aControlsWgt);
225   QVBoxLayout* aCommandsLay = new QVBoxLayout(aCommandsWgt);
226   aCommandsLay->setContentsMargins(0, 0, 0, 0);
227   aCtrlLayout->addWidget(aCommandsWgt);
228
229   aCommandsLay->addWidget(new QLabel(tr("Out of toolbars:"), aCommandsWgt));
230   myCommandsList = new QListWidget(aCommandsWgt);
231   foreach(int aId, myFreeItems) {
232     myCommandsList->addItem(new SHAPERGUI_CommandIdItem(myCommandsList, aId, myModule));
233   }
234   myCommandsList->setMaximumWidth(150);
235   aCommandsLay->addWidget(myCommandsList);
236
237   // Middle buttons
238   QWidget* aButtonsWgt = new QWidget(aControlsWgt);
239   QVBoxLayout* aBtnLayout = new QVBoxLayout(aButtonsWgt);
240   aBtnLayout->setContentsMargins(0, 0, 0, 0);
241   aCtrlLayout->addWidget(aButtonsWgt);
242
243   aBtnLayout->addStretch(1);
244   QPushButton* aAddButton = new QPushButton("--->", aButtonsWgt);
245   connect(aAddButton, SIGNAL(clicked(bool)), SLOT(onAddItem()));
246   aBtnLayout->addWidget(aAddButton);
247
248   aBtnLayout->addSpacing(20);
249
250   QPushButton* aDelButton = new QPushButton("<---", aButtonsWgt);
251   connect(aDelButton, SIGNAL(clicked(bool)), SLOT(onDelItem()));
252   aBtnLayout->addWidget(aDelButton);
253   aBtnLayout->addStretch(1);
254
255   // Right list
256   QWidget* aItemsWgt = new QWidget(aControlsWgt);
257   QVBoxLayout* aItemsLay = new QVBoxLayout(aItemsWgt);
258   aItemsLay->setContentsMargins(0, 0, 0, 0);
259   aCtrlLayout->addWidget(aItemsWgt);
260
261   aItemsLay->addWidget(new QLabel(tr("In the toolbar:"), aItemsWgt));
262   myItemsList = new QListWidget(aItemsWgt);
263   foreach(int aId, myToolItems) {
264     myItemsList->addItem(new SHAPERGUI_CommandIdItem(myItemsList, aId, myModule));
265   }
266   myItemsList->setMaximumWidth(150);
267   aItemsLay->addWidget(myItemsList);
268
269   // Buttons part of the dialog
270   QDialogButtonBox* aButtons =
271     new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this);
272   aMailLayout->addWidget(aButtons);
273   connect(aButtons, SIGNAL(accepted()), SLOT(accept()));
274   connect(aButtons, SIGNAL(rejected()), SLOT(reject()));
275 }
276
277 void SHAPERGUI_ToolbarItemsDlg::onAddItem()
278 {
279
280 }
281
282 void SHAPERGUI_ToolbarItemsDlg::onDelItem()
283 {
284
285 }