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())
85 myFreeCommands = theModule->getFreeCommands();
87 setWindowTitle(tr("Toolbars"));
88 QVBoxLayout* aMailLayout = new QVBoxLayout(this);
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);
97 QWidget* aListWgt = new QWidget(aControlsWgt);
98 QVBoxLayout* aListLayout = new QVBoxLayout(aListWgt);
99 aListLayout->setContentsMargins(0, 0, 0, 0);
100 aContolsLay->addWidget(aListWgt);
102 aListLayout->addWidget(new QLabel(tr("Toolbars:"), aListWgt));
104 myToolbarsList = new QListWidget(aListWgt);
105 connect(myToolbarsList, SIGNAL(doubleClicked(const QModelIndex&)),
106 SLOT(onDoubleClick(const QModelIndex&)));
107 aListLayout->addWidget(myToolbarsList);
109 QWidget* aFreeItemsWgt = new QWidget(aListWgt);
110 QHBoxLayout* aFreeLayout = new QHBoxLayout(aFreeItemsWgt);
111 aFreeLayout->setContentsMargins(0, 0, 0, 0);
112 aListLayout->addWidget(aFreeItemsWgt);
114 aFreeLayout->addWidget(new QLabel(tr("Number of commands out of toolbars:"), aFreeItemsWgt));
115 myFreeNbLbl = new QLabel(aFreeItemsWgt);
116 aFreeLayout->addWidget(myFreeNbLbl);
119 QWidget* aButtonsWgt = new QWidget(aControlsWgt);
120 QVBoxLayout* aBtnLayout = new QVBoxLayout(aButtonsWgt);
121 aBtnLayout->setContentsMargins(0, 0, 0, 0);
122 aContolsLay->addWidget(aButtonsWgt);
124 QPushButton* aAddBtn = new QPushButton(tr("Add..."), aButtonsWgt);
125 connect(aAddBtn, SIGNAL(clicked(bool)), SLOT(onAdd()));
126 aBtnLayout->addWidget(aAddBtn);
128 QPushButton* aEditBtn = new QPushButton(tr("Edit..."), aButtonsWgt);
129 connect(aEditBtn, SIGNAL(clicked(bool)), SLOT(onEdit()));
130 aBtnLayout->addWidget(aEditBtn);
132 QPushButton* aDeleteBtn = new QPushButton(tr("Delete"), aButtonsWgt);
133 connect(aDeleteBtn, SIGNAL(clicked(bool)), SLOT(onDelete()));
134 aBtnLayout->addWidget(aDeleteBtn);
135 aBtnLayout->addStretch(1);
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()));
144 updateToolbarsList();
148 void SHAPERGUI_ToolbarsDlg::onAdd()
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();
158 QString aMsg = tr("A tool bar with name %1 already exists").arg(aNewToolbar);
159 QMessageBox::warning(this, tr("Create toolbar"), aMsg);
164 void SHAPERGUI_ToolbarsDlg::onEdit()
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);
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();
178 updateToolbarsList();
183 void SHAPERGUI_ToolbarsDlg::onDelete()
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);
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();
203 void SHAPERGUI_ToolbarsDlg::updateToolbarsList()
205 myToolbarsList->clear();
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)));
211 myToolbarsList->addItems(aItems);
215 void SHAPERGUI_ToolbarsDlg::onDoubleClick(const QModelIndex& theIdx)
217 if (theIdx.isValid())
221 void SHAPERGUI_ToolbarsDlg::updateNumber()
223 myFreeNbLbl->setText(QString::number(myFreeCommands.size()));
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),
237 setWindowTitle(tr("Edit toolbar items"));
239 QVBoxLayout* aMailLayout = new QVBoxLayout(this);
242 QWidget* aNameWgt = new QWidget(this);
243 QHBoxLayout* aNameLay = new QHBoxLayout(aNameWgt);
244 aNameLay->setContentsMargins(0, 0, 0, 0);
245 aMailLayout->addWidget(aNameWgt);
247 aNameLay->addWidget(new QLabel(tr("Toolbar name:"), aNameWgt));
248 QLabel* aNameLbl = new QLabel(theToolbar, aNameWgt);
249 QFont aFont = aNameLbl->font();
251 aNameLbl->setFont(aFont);
252 aNameLay->addWidget(aNameLbl);
253 aNameLay->addStretch(1);
256 QWidget* aControlsWgt = new QWidget(this);
257 QHBoxLayout* aCtrlLayout = new QHBoxLayout(aControlsWgt);
258 aCtrlLayout->setContentsMargins(0, 0, 0, 0);
259 aMailLayout->addWidget(aControlsWgt);
262 QWidget* aCommandsWgt = new QWidget(aControlsWgt);
263 QVBoxLayout* aCommandsLay = new QVBoxLayout(aCommandsWgt);
264 aCommandsLay->setContentsMargins(0, 0, 0, 0);
265 aCtrlLayout->addWidget(aCommandsWgt);
267 aCommandsLay->addWidget(new QLabel(tr("Out of toolbars:"), aCommandsWgt));
268 myCommandsList = new QListWidget(aCommandsWgt);
269 myCommandsList->setSortingEnabled(false);
271 myCommandsList->addItem(new SHAPERGUI_CommandIdItem(myCommandsList, -1, myModule));
272 foreach(int aId, theFreeItems) {
273 myCommandsList->addItem(new SHAPERGUI_CommandIdItem(myCommandsList, aId, myModule));
275 myCommandsList->setMaximumWidth(LIST_WIDTH);
276 aCommandsLay->addWidget(myCommandsList);
279 QWidget* aButtonsWgt = new QWidget(aControlsWgt);
280 QVBoxLayout* aBtnLayout = new QVBoxLayout(aButtonsWgt);
281 aBtnLayout->setContentsMargins(0, 0, 0, 0);
282 aCtrlLayout->addWidget(aButtonsWgt);
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);
289 aBtnLayout->addSpacing(20);
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);
297 QWidget* aItemsWgt = new QWidget(aControlsWgt);
298 QVBoxLayout* aItemsLay = new QVBoxLayout(aItemsWgt);
299 aItemsLay->setContentsMargins(0, 0, 0, 0);
300 aCtrlLayout->addWidget(aItemsWgt);
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));
308 myItemsList->setMaximumWidth(LIST_WIDTH);
309 myItemsList->viewport()->installEventFilter(this);
310 aItemsLay->addWidget(myItemsList);
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);
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);
323 aBtnLay->addSpacing(20);
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);
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()));
338 void SHAPERGUI_ToolbarItemsDlg::onAddItem()
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();
345 myCommandsList->removeItemWidget(aItem);
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);
355 myItemsList->addItem(aItem);
360 void SHAPERGUI_ToolbarItemsDlg::onDelItem()
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);
369 aItem = new SHAPERGUI_CommandIdItem(myCommandsList, aId, myModule);
370 myCommandsList->addItem(aItem);
376 bool SHAPERGUI_ToolbarItemsDlg::eventFilter(QObject* theObj, QEvent* theEvent)
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());
385 return QDialog::eventFilter(theObj, theEvent);
388 void SHAPERGUI_ToolbarItemsDlg::onUp()
390 QModelIndex aCurrentIdx = myItemsList->currentIndex();
391 if (aCurrentIdx.isValid()) {
392 int aRow = aCurrentIdx.row();
394 QListWidgetItem* aItem = myItemsList->takeItem(aRow);
396 myItemsList->insertItem(aRow, aItem);
397 myItemsList->setCurrentRow(aRow);
402 void SHAPERGUI_ToolbarItemsDlg::onDown()
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);
411 myItemsList->insertItem(aRow, aItem);
412 myItemsList->setCurrentRow(aRow);
417 QIntList SHAPERGUI_ToolbarItemsDlg::freeItems() const
419 return getItems(myCommandsList, 1);
422 QIntList SHAPERGUI_ToolbarItemsDlg::toolbarItems() const
424 return getItems(myItemsList, 0);
427 QIntList SHAPERGUI_ToolbarItemsDlg::getItems(QListWidget* theWidget, int theStart) const
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());