]> SALOME platform Git repositories - modules/shaper.git/blob - src/SHAPERGUI/SHAPERGUI_ToolbarsMgr.cpp
Salome HOME
Issue #2734: Add "Reset" button to the dialog box
[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 #include <QtxActionToolMgr.h>
27
28 #include <QLayout>
29 #include <QDialogButtonBox>
30 #include <QLabel>
31 #include <QListWidget>
32 #include <QPushButton>
33 #include <QInputDialog>
34 #include <QMessageBox>
35 #include <QMouseEvent>
36
37
38 #define SEPARATOR "------"
39 #define LIST_WIDTH 180
40
41 class SHAPERGUI_CommandIdItem : public QListWidgetItem
42 {
43 public:
44   SHAPERGUI_CommandIdItem(QListWidget* theParent, int theId, SHAPERGUI* theModule)
45     : QListWidgetItem(theParent), myId(theId), myModule(theModule) {}
46
47
48   virtual QVariant data(int theRole) const {
49     QAction* aAction = 0;
50     if (myId != -1)
51       aAction = myModule->action(myId);
52
53     switch (theRole) {
54     case Qt::DisplayRole:
55       if (aAction)
56         return aAction->text();
57       else
58         return SEPARATOR;
59     case Qt::DecorationRole:
60       if (aAction)
61         return aAction->icon();
62       else
63         return QIcon();
64     }
65     return QListWidgetItem::data(theRole);
66   }
67
68
69   int id() const { return myId; }
70
71 private:
72   SHAPERGUI* myModule;
73   int myId;
74 };
75
76
77 //************************************************************************************
78 //************************************************************************************
79 //************************************************************************************
80 SHAPERGUI_ToolbarsDlg::SHAPERGUI_ToolbarsDlg(SHAPERGUI* theModule)
81   : QDialog(theModule->application()->desktop()),
82   myModule(theModule),
83   myResult(theModule->shaperToolbars()),
84   myIsReset(false)
85 {
86   myFreeCommands = theModule->getFreeCommands();
87
88   setWindowTitle(tr("Toolbars"));
89   QVBoxLayout* aMailLayout = new QVBoxLayout(this);
90
91   // Controls part of the dialog
92   QWidget* aControlsWgt = new QWidget(this);
93   QHBoxLayout* aContolsLay = new QHBoxLayout(aControlsWgt);
94   aContolsLay->setContentsMargins(0, 0, 0, 0);
95   aMailLayout->addWidget(aControlsWgt);
96
97   // Right controls
98   QWidget* aListWgt = new QWidget(aControlsWgt);
99   QVBoxLayout* aListLayout = new QVBoxLayout(aListWgt);
100   aListLayout->setContentsMargins(0, 0, 0, 0);
101   aContolsLay->addWidget(aListWgt);
102
103   aListLayout->addWidget(new QLabel(tr("Toolbars:"), aListWgt));
104
105   myToolbarsList = new QListWidget(aListWgt);
106   connect(myToolbarsList, SIGNAL(doubleClicked(const QModelIndex&)),
107     SLOT(onDoubleClick(const QModelIndex&)));
108   aListLayout->addWidget(myToolbarsList);
109
110   QWidget* aFreeItemsWgt = new QWidget(aListWgt);
111   QHBoxLayout* aFreeLayout = new QHBoxLayout(aFreeItemsWgt);
112   aFreeLayout->setContentsMargins(0, 0, 0, 0);
113   aListLayout->addWidget(aFreeItemsWgt);
114
115   aFreeLayout->addWidget(new QLabel(tr("Number of commands out of toolbars:"), aFreeItemsWgt));
116   myFreeNbLbl = new QLabel(aFreeItemsWgt);
117   aFreeLayout->addWidget(myFreeNbLbl);
118
119   // Left controls
120   QWidget* aButtonsWgt = new QWidget(aControlsWgt);
121   QVBoxLayout* aBtnLayout = new QVBoxLayout(aButtonsWgt);
122   aBtnLayout->setContentsMargins(0, 0, 0, 0);
123   aContolsLay->addWidget(aButtonsWgt);
124
125   QPushButton* aAddBtn = new QPushButton(tr("Add..."), aButtonsWgt);
126   aAddBtn->setToolTip(tr("Add a new empty toolbar to the toolbars list"));
127   connect(aAddBtn, SIGNAL(clicked(bool)), SLOT(onAdd()));
128   aBtnLayout->addWidget(aAddBtn);
129
130   QPushButton* aEditBtn = new QPushButton(tr("Edit..."), aButtonsWgt);
131   aEditBtn->setToolTip(tr("Edit currently selected toolbar"));
132   connect(aEditBtn, SIGNAL(clicked(bool)), SLOT(onEdit()));
133   aBtnLayout->addWidget(aEditBtn);
134
135   QPushButton* aDeleteBtn = new QPushButton(tr("Delete"), aButtonsWgt);
136   aDeleteBtn->setToolTip(tr("Delete currently selected toolbar"));
137   connect(aDeleteBtn, SIGNAL(clicked(bool)), SLOT(onDelete()));
138   aBtnLayout->addWidget(aDeleteBtn);
139   aBtnLayout->addStretch(1);
140
141   QPushButton* aResetBtn = new QPushButton(tr("Reset"), aButtonsWgt);
142   aResetBtn->setToolTip(tr("Restore default toolbars structure"));
143   connect(aResetBtn, SIGNAL(clicked(bool)), SLOT(onReset()));
144   aBtnLayout->addWidget(aResetBtn);
145   aBtnLayout->addSpacing(19);
146
147   // Buttons part of the dialog
148   QDialogButtonBox* aButtons =
149     new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this);
150   aMailLayout->addWidget(aButtons);
151   connect(aButtons, SIGNAL(accepted()), SLOT(accept()));
152   connect(aButtons, SIGNAL(rejected()), SLOT(reject()));
153
154   updateToolbarsList();
155   updateNumber();
156 }
157
158 void SHAPERGUI_ToolbarsDlg::onAdd()
159 {
160   QString aNewToolbar =
161     QInputDialog::getText(this, tr("Create toolbar"), tr("Name of a new toolbar"));
162   if (!(aNewToolbar.isNull() || aNewToolbar.isEmpty())) {
163     if (!myResult.contains(aNewToolbar)) {
164       myResult[aNewToolbar] = QIntList();
165       updateToolbarsList();
166       myIsReset = false;
167     }
168     else {
169       QString aMsg = tr("A tool bar with name %1 already exists").arg(aNewToolbar);
170       QMessageBox::warning(this, tr("Create toolbar"), aMsg);
171     }
172   }
173 }
174
175 void SHAPERGUI_ToolbarsDlg::onEdit()
176 {
177   QList<QListWidgetItem*> aSelected = myToolbarsList->selectedItems();
178   if (aSelected.size() == 1) {
179     QString aToolbarName = aSelected.first()->text();
180     int aPos = aToolbarName.lastIndexOf(" (");
181     aToolbarName = aToolbarName.left(aPos);
182
183     SHAPERGUI_ToolbarItemsDlg aDlg(this, myModule,
184       aToolbarName, myFreeCommands, myResult[aToolbarName]);
185     if (aDlg.exec() == QDialog::Accepted) {
186       myFreeCommands = aDlg.freeItems();
187       myResult[aToolbarName] = aDlg.toolbarItems();
188       updateNumber();
189       updateToolbarsList();
190       myIsReset = false;
191     }
192   }
193 }
194
195 void SHAPERGUI_ToolbarsDlg::onDelete()
196 {
197   QList<QListWidgetItem*> aSelected = myToolbarsList->selectedItems();
198   if (aSelected.size() == 1) {
199     QString aToolbarName = aSelected.first()->text();
200     int aPos = aToolbarName.lastIndexOf(" (");
201     aToolbarName = aToolbarName.left(aPos);
202
203     QString aMsg = tr("Toolbar %1 will be deleted. Continue?").arg(aToolbarName);
204     if (QMessageBox::question(this, tr("Delete toolbar"), aMsg) == QMessageBox::Yes) {
205       myFreeCommands.append(myResult[aToolbarName]);
206       // remove separators from free items
207       myFreeCommands.removeAll(-1);
208       myResult.remove(aToolbarName);
209       updateToolbarsList();
210       updateNumber();
211       myIsReset = false;
212     }
213   }
214 }
215
216 void SHAPERGUI_ToolbarsDlg::updateToolbarsList()
217 {
218   myToolbarsList->clear();
219   QStringList aItems;
220   QMap<QString, QIntList>::const_iterator aIt;
221   for (aIt = myResult.cbegin(); aIt != myResult.cend(); aIt++) {
222     aItems.append(aIt.key() + tr(" (%1 items)").arg(aIt.value().size() - aIt.value().count(-1)));
223   }
224   myToolbarsList->addItems(aItems);
225 }
226
227
228 void SHAPERGUI_ToolbarsDlg::onDoubleClick(const QModelIndex& theIdx)
229 {
230   if (theIdx.isValid())
231     onEdit();
232 }
233
234 void SHAPERGUI_ToolbarsDlg::updateNumber()
235 {
236   myFreeNbLbl->setText(QString::number(myFreeCommands.size()));
237 }
238
239 void SHAPERGUI_ToolbarsDlg::onReset()
240 {
241   myResult = myModule->defaultToolbars();
242   updateNumber();
243   updateToolbarsList();
244   myIsReset = true;
245 }
246
247
248 //************************************************************************************
249 //************************************************************************************
250 //************************************************************************************
251 SHAPERGUI_ToolbarItemsDlg::SHAPERGUI_ToolbarItemsDlg(QWidget* theParent,
252   SHAPERGUI* theModule,
253   const QString& theToolbar,
254   const QIntList& theFreeItems,
255   const QIntList& theItemsList)
256   : QDialog(theParent),
257   myModule(theModule)
258 {
259   setWindowTitle(tr("Edit toolbar"));
260
261   QVBoxLayout* aMailLayout = new QVBoxLayout(this);
262
263   // Name of toolbar
264   QWidget* aNameWgt = new QWidget(this);
265   QHBoxLayout* aNameLay = new QHBoxLayout(aNameWgt);
266   aNameLay->setContentsMargins(0, 0, 0, 0);
267   aMailLayout->addWidget(aNameWgt);
268
269   aNameLay->addWidget(new QLabel(tr("Toolbar name:"), aNameWgt));
270   QLabel* aNameLbl = new QLabel(theToolbar, aNameWgt);
271   QFont aFont = aNameLbl->font();
272   aFont.setBold(true);
273   aNameLbl->setFont(aFont);
274   aNameLay->addWidget(aNameLbl);
275   aNameLay->addStretch(1);
276
277   // Lists widget
278   QWidget* aControlsWgt = new QWidget(this);
279   QHBoxLayout* aCtrlLayout = new QHBoxLayout(aControlsWgt);
280   aCtrlLayout->setContentsMargins(0, 0, 0, 0);
281   aMailLayout->addWidget(aControlsWgt);
282
283   // Left list
284   QWidget* aCommandsWgt = new QWidget(aControlsWgt);
285   QVBoxLayout* aCommandsLay = new QVBoxLayout(aCommandsWgt);
286   aCommandsLay->setContentsMargins(0, 0, 0, 0);
287   aCtrlLayout->addWidget(aCommandsWgt);
288
289   aCommandsLay->addWidget(new QLabel(tr("Out of toolbars:"), aCommandsWgt));
290   myCommandsList = new QListWidget(aCommandsWgt);
291   myCommandsList->setSortingEnabled(false);
292
293   myCommandsList->addItem(new SHAPERGUI_CommandIdItem(myCommandsList, -1, myModule));
294   foreach(int aId, theFreeItems) {
295     myCommandsList->addItem(new SHAPERGUI_CommandIdItem(myCommandsList, aId, myModule));
296   }
297   myCommandsList->setMaximumWidth(LIST_WIDTH);
298   aCommandsLay->addWidget(myCommandsList);
299
300   // Middle buttons
301   QWidget* aButtonsWgt = new QWidget(aControlsWgt);
302   QVBoxLayout* aBtnLayout = new QVBoxLayout(aButtonsWgt);
303   aBtnLayout->setContentsMargins(0, 0, 0, 0);
304   aCtrlLayout->addWidget(aButtonsWgt);
305
306   aBtnLayout->addStretch(1);
307   QPushButton* aAddButton = new QPushButton(QIcon(":pictures/arrow-right.png"), "", aButtonsWgt);
308   connect(aAddButton, SIGNAL(clicked(bool)), SLOT(onAddItem()));
309   aBtnLayout->addWidget(aAddButton);
310
311   aBtnLayout->addSpacing(20);
312
313   QPushButton* aDelButton = new QPushButton(QIcon(":pictures/arrow-left.png"), "", aButtonsWgt);
314   connect(aDelButton, SIGNAL(clicked(bool)), SLOT(onDelItem()));
315   aBtnLayout->addWidget(aDelButton);
316   aBtnLayout->addStretch(1);
317
318   // Right list
319   QWidget* aItemsWgt = new QWidget(aControlsWgt);
320   QVBoxLayout* aItemsLay = new QVBoxLayout(aItemsWgt);
321   aItemsLay->setContentsMargins(0, 0, 0, 0);
322   aCtrlLayout->addWidget(aItemsWgt);
323
324   aItemsLay->addWidget(new QLabel(tr("In the toolbar:"), aItemsWgt));
325   myItemsList = new QListWidget(aItemsWgt);
326   myItemsList->setSortingEnabled(false);
327   foreach(int aId, theItemsList) {
328     myItemsList->addItem(new SHAPERGUI_CommandIdItem(myItemsList, aId, myModule));
329   }
330   myItemsList->setMaximumWidth(LIST_WIDTH);
331   myItemsList->viewport()->installEventFilter(this);
332   aItemsLay->addWidget(myItemsList);
333
334   // Buttons of right list
335   QWidget* aBtnWgt = new QWidget(aControlsWgt);
336   QVBoxLayout* aBtnLay = new QVBoxLayout(aBtnWgt);
337   aBtnLay->setContentsMargins(0, 0, 0, 0);
338   aCtrlLayout->addWidget(aBtnWgt);
339
340   aBtnLay->addStretch(1);
341   QPushButton* aUpButton = new QPushButton(QIcon(":pictures/arrow-up.png"), "", aBtnWgt);
342   connect(aUpButton, SIGNAL(clicked(bool)), SLOT(onUp()));
343   aBtnLay->addWidget(aUpButton);
344
345   aBtnLay->addSpacing(20);
346
347   QPushButton* aDownButton = new QPushButton(QIcon(":pictures/arrow-down.png"), "", aBtnWgt);
348   connect(aDownButton, SIGNAL(clicked(bool)), SLOT(onDown()));
349   aBtnLay->addWidget(aDownButton);
350   aBtnLay->addStretch(1);
351
352   // Buttons part of the dialog
353   QDialogButtonBox* aButtons =
354     new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this);
355   aMailLayout->addWidget(aButtons);
356   connect(aButtons, SIGNAL(accepted()), SLOT(accept()));
357   connect(aButtons, SIGNAL(rejected()), SLOT(reject()));
358 }
359
360 void SHAPERGUI_ToolbarItemsDlg::onAddItem()
361 {
362   QList<QListWidgetItem*> aCurrentList = myCommandsList->selectedItems();
363   if (aCurrentList.size() > 0) {
364     SHAPERGUI_CommandIdItem* aItem = dynamic_cast<SHAPERGUI_CommandIdItem*>(aCurrentList.first());
365     int aId = aItem->id();
366     if (aId != -1) {
367       myCommandsList->removeItemWidget(aItem);
368       delete aItem;
369     }
370     QModelIndex aIdx = myItemsList->currentIndex();
371     aItem = new SHAPERGUI_CommandIdItem(0, aId, myModule);
372     if (aIdx.isValid()) {
373       int aRow = aIdx.row();
374       myItemsList->insertItem(aRow, aItem);
375     }
376     else {
377       myItemsList->addItem(aItem);
378     }
379   }
380 }
381
382 void SHAPERGUI_ToolbarItemsDlg::onDelItem()
383 {
384   QList<QListWidgetItem*> aCurrentList = myItemsList->selectedItems();
385   if (aCurrentList.size() > 0) {
386     SHAPERGUI_CommandIdItem* aItem = dynamic_cast<SHAPERGUI_CommandIdItem*>(aCurrentList.first());
387     int aId = aItem->id();
388     myItemsList->removeItemWidget(aItem);
389     delete aItem;
390     if (aId != -1) {
391       aItem = new SHAPERGUI_CommandIdItem(myCommandsList, aId, myModule);
392       myCommandsList->addItem(aItem);
393     }
394   }
395 }
396
397
398 bool SHAPERGUI_ToolbarItemsDlg::eventFilter(QObject* theObj, QEvent* theEvent)
399 {
400   if (theEvent->type() == QEvent::MouseButtonRelease) {
401     QMouseEvent* aMouseEvent = (QMouseEvent*)theEvent;
402     QModelIndex aIdx = myItemsList->indexAt(aMouseEvent->pos());
403     if (!aIdx.isValid() || aMouseEvent->button() == Qt::RightButton) {
404       myItemsList->setCurrentIndex(QModelIndex());
405     }
406   }
407   return QDialog::eventFilter(theObj, theEvent);
408 }
409
410 void SHAPERGUI_ToolbarItemsDlg::onUp()
411 {
412   QModelIndex aCurrentIdx = myItemsList->currentIndex();
413   if (aCurrentIdx.isValid()) {
414     int aRow = aCurrentIdx.row();
415     if (aRow > 0) {
416       QListWidgetItem* aItem = myItemsList->takeItem(aRow);
417       aRow--;
418       myItemsList->insertItem(aRow, aItem);
419       myItemsList->setCurrentRow(aRow);
420     }
421   }
422 }
423
424 void SHAPERGUI_ToolbarItemsDlg::onDown()
425 {
426   QModelIndex aCurrentIdx = myItemsList->currentIndex();
427   if (aCurrentIdx.isValid()) {
428     int aNb = myItemsList->count();
429     int aRow = aCurrentIdx.row();
430     if (aRow < (aNb - 1)) {
431       QListWidgetItem* aItem = myItemsList->takeItem(aRow);
432       aRow++;
433       myItemsList->insertItem(aRow, aItem);
434       myItemsList->setCurrentRow(aRow);
435     }
436   }
437 }
438
439 QIntList SHAPERGUI_ToolbarItemsDlg::freeItems() const
440 {
441   return getItems(myCommandsList, 1);
442 }
443
444 QIntList SHAPERGUI_ToolbarItemsDlg::toolbarItems() const
445 {
446   return getItems(myItemsList, 0);
447 }
448
449 QIntList SHAPERGUI_ToolbarItemsDlg::getItems(QListWidget* theWidget, int theStart) const
450 {
451   QIntList aList;
452   SHAPERGUI_CommandIdItem* aItem = 0;
453   int aNb = theWidget->count();
454   for (int i = theStart; i < aNb; i++) {
455     aItem = (SHAPERGUI_CommandIdItem*)theWidget->item(i);
456     aList.append(aItem->id());
457   }
458   return aList;
459 }