1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "SHAPERGUI_ToolbarsMgr.h"
22 #include "SHAPERGUI.h"
24 #include <CAM_Application.h>
25 #include <SUIT_Desktop.h>
26 #include <QtxActionToolMgr.h>
29 #include <QDialogButtonBox>
31 #include <QListWidget>
32 #include <QPushButton>
33 #include <QInputDialog>
34 #include <QMessageBox>
35 #include <QMouseEvent>
38 #define SEPARATOR "------"
39 #define LIST_WIDTH 180
41 class SHAPERGUI_CommandIdItem : public QListWidgetItem
44 SHAPERGUI_CommandIdItem(QListWidget* theParent, int theId, SHAPERGUI* theModule)
45 : QListWidgetItem(theParent), myId(theId), myModule(theModule) {}
48 virtual QVariant data(int theRole) const {
51 aAction = myModule->action(myId);
56 return aAction->text();
59 case Qt::DecorationRole:
61 return aAction->icon();
65 return QListWidgetItem::data(theRole);
69 int id() const { return myId; }
77 //************************************************************************************
78 //************************************************************************************
79 //************************************************************************************
80 SHAPERGUI_ToolbarsDlg::SHAPERGUI_ToolbarsDlg(SHAPERGUI* theModule)
81 : QDialog(theModule->application()->desktop()),
83 myResult(theModule->shaperToolbars()),
86 myFreeCommands = theModule->getFreeCommands();
88 setWindowTitle(tr("Toolbars"));
89 QVBoxLayout* aMailLayout = new QVBoxLayout(this);
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);
98 QWidget* aListWgt = new QWidget(aControlsWgt);
99 QVBoxLayout* aListLayout = new QVBoxLayout(aListWgt);
100 aListLayout->setContentsMargins(0, 0, 0, 0);
101 aContolsLay->addWidget(aListWgt);
103 aListLayout->addWidget(new QLabel(tr("Toolbars:"), aListWgt));
105 myToolbarsList = new QListWidget(aListWgt);
106 connect(myToolbarsList, SIGNAL(doubleClicked(const QModelIndex&)),
107 SLOT(onDoubleClick(const QModelIndex&)));
108 aListLayout->addWidget(myToolbarsList);
110 QWidget* aFreeItemsWgt = new QWidget(aListWgt);
111 QHBoxLayout* aFreeLayout = new QHBoxLayout(aFreeItemsWgt);
112 aFreeLayout->setContentsMargins(0, 0, 0, 0);
113 aListLayout->addWidget(aFreeItemsWgt);
115 aFreeLayout->addWidget(new QLabel(tr("Number of commands out of toolbars:"), aFreeItemsWgt));
116 myFreeNbLbl = new QLabel(aFreeItemsWgt);
117 aFreeLayout->addWidget(myFreeNbLbl);
120 QWidget* aButtonsWgt = new QWidget(aControlsWgt);
121 QVBoxLayout* aBtnLayout = new QVBoxLayout(aButtonsWgt);
122 aBtnLayout->setContentsMargins(0, 0, 0, 0);
123 aContolsLay->addWidget(aButtonsWgt);
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);
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);
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);
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);
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()));
154 updateToolbarsList();
158 void SHAPERGUI_ToolbarsDlg::onAdd()
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();
169 QString aMsg = tr("A tool bar with name %1 already exists").arg(aNewToolbar);
170 QMessageBox::warning(this, tr("Create toolbar"), aMsg);
175 void SHAPERGUI_ToolbarsDlg::onEdit()
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);
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();
189 updateToolbarsList();
195 void SHAPERGUI_ToolbarsDlg::onDelete()
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);
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();
216 void SHAPERGUI_ToolbarsDlg::updateToolbarsList()
218 myToolbarsList->clear();
220 QMap<QString, QIntList>::const_iterator aIt;
221 for (aIt = myResult.cbegin(); aIt != myResult.cend(); aIt++) {
222 aItems.append(aIt.key() + tr(" (%1 commands)").arg(aIt.value().size() - aIt.value().count(-1)));
224 myToolbarsList->addItems(aItems);
228 void SHAPERGUI_ToolbarsDlg::onDoubleClick(const QModelIndex& theIdx)
230 if (theIdx.isValid())
234 void SHAPERGUI_ToolbarsDlg::updateNumber()
236 myFreeNbLbl->setText(QString::number(myFreeCommands.size()));
239 void SHAPERGUI_ToolbarsDlg::onReset()
241 myResult = myModule->defaultToolbars();
243 updateToolbarsList();
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),
259 setWindowTitle(tr("Edit toolbar"));
261 QVBoxLayout* aMailLayout = new QVBoxLayout(this);
264 QWidget* aNameWgt = new QWidget(this);
265 QHBoxLayout* aNameLay = new QHBoxLayout(aNameWgt);
266 aNameLay->setContentsMargins(0, 0, 0, 0);
267 aMailLayout->addWidget(aNameWgt);
269 aNameLay->addWidget(new QLabel(tr("Toolbar name:"), aNameWgt));
270 QLabel* aNameLbl = new QLabel(theToolbar, aNameWgt);
271 QFont aFont = aNameLbl->font();
273 aNameLbl->setFont(aFont);
274 aNameLay->addWidget(aNameLbl);
275 aNameLay->addStretch(1);
278 QWidget* aControlsWgt = new QWidget(this);
279 QHBoxLayout* aCtrlLayout = new QHBoxLayout(aControlsWgt);
280 aCtrlLayout->setContentsMargins(0, 0, 0, 0);
281 aMailLayout->addWidget(aControlsWgt);
284 QWidget* aCommandsWgt = new QWidget(aControlsWgt);
285 QVBoxLayout* aCommandsLay = new QVBoxLayout(aCommandsWgt);
286 aCommandsLay->setContentsMargins(0, 0, 0, 0);
287 aCtrlLayout->addWidget(aCommandsWgt);
289 aCommandsLay->addWidget(new QLabel(tr("Out of toolbars:"), aCommandsWgt));
290 myCommandsList = new QListWidget(aCommandsWgt);
291 myCommandsList->setSortingEnabled(false);
293 myCommandsList->addItem(new SHAPERGUI_CommandIdItem(myCommandsList, -1, myModule));
294 foreach(int aId, theFreeItems) {
295 myCommandsList->addItem(new SHAPERGUI_CommandIdItem(myCommandsList, aId, myModule));
297 myCommandsList->setMaximumWidth(LIST_WIDTH);
298 aCommandsLay->addWidget(myCommandsList);
301 QWidget* aButtonsWgt = new QWidget(aControlsWgt);
302 QVBoxLayout* aBtnLayout = new QVBoxLayout(aButtonsWgt);
303 aBtnLayout->setContentsMargins(0, 0, 0, 0);
304 aCtrlLayout->addWidget(aButtonsWgt);
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);
311 aBtnLayout->addSpacing(20);
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);
319 QWidget* aItemsWgt = new QWidget(aControlsWgt);
320 QVBoxLayout* aItemsLay = new QVBoxLayout(aItemsWgt);
321 aItemsLay->setContentsMargins(0, 0, 0, 0);
322 aCtrlLayout->addWidget(aItemsWgt);
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));
330 myItemsList->setMaximumWidth(LIST_WIDTH);
331 myItemsList->viewport()->installEventFilter(this);
332 aItemsLay->addWidget(myItemsList);
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);
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);
345 aBtnLay->addSpacing(20);
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);
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()));
360 void SHAPERGUI_ToolbarItemsDlg::onAddItem()
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();
367 myCommandsList->removeItemWidget(aItem);
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);
377 myItemsList->addItem(aItem);
382 void SHAPERGUI_ToolbarItemsDlg::onDelItem()
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);
391 aItem = new SHAPERGUI_CommandIdItem(myCommandsList, aId, myModule);
392 myCommandsList->addItem(aItem);
398 bool SHAPERGUI_ToolbarItemsDlg::eventFilter(QObject* theObj, QEvent* theEvent)
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());
407 return QDialog::eventFilter(theObj, theEvent);
410 void SHAPERGUI_ToolbarItemsDlg::onUp()
412 QModelIndex aCurrentIdx = myItemsList->currentIndex();
413 if (aCurrentIdx.isValid()) {
414 int aRow = aCurrentIdx.row();
416 QListWidgetItem* aItem = myItemsList->takeItem(aRow);
418 myItemsList->insertItem(aRow, aItem);
419 myItemsList->setCurrentRow(aRow);
424 void SHAPERGUI_ToolbarItemsDlg::onDown()
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);
433 myItemsList->insertItem(aRow, aItem);
434 myItemsList->setCurrentRow(aRow);
439 QIntList SHAPERGUI_ToolbarItemsDlg::freeItems() const
441 return getItems(myCommandsList, 1);
444 QIntList SHAPERGUI_ToolbarItemsDlg::toolbarItems() const
446 return getItems(myItemsList, 0);
449 QIntList SHAPERGUI_ToolbarItemsDlg::getItems(QListWidget* theWidget, int theStart) const
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());