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