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>
28 #include <QDialogButtonBox>
30 #include <QListWidget>
31 #include <QPushButton>
32 #include <QInputDialog>
33 #include <QMessageBox>
36 class SHAPERGUI_CommandIdItem : public QListWidgetItem
39 SHAPERGUI_CommandIdItem(QListWidget* theParent, int theId, SHAPERGUI* theModule)
40 : QListWidgetItem(theParent), myId(theId), myModule(theModule) {}
43 virtual QVariant data(int theRole) const {
44 if (theRole == Qt::DisplayRole) {
47 QAction* aAction = myModule->action(myId);
49 return aAction->text();
51 return QListWidgetItem::data(theRole);
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()),
69 myActionsList(theActionsList),
70 myToolbars(theToolbars)
72 setWindowTitle(tr("Toolbars"));
73 QVBoxLayout* aMailLayout = new QVBoxLayout(this);
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);
82 QWidget* aListWgt = new QWidget(aControlsWgt);
83 QVBoxLayout* aListLayout = new QVBoxLayout(aListWgt);
84 aListLayout->setContentsMargins(0, 0, 0, 0);
85 aContolsLay->addWidget(aListWgt);
87 aListLayout->addWidget(new QLabel(tr("Toolbars:"), aListWgt));
89 myToolbarsList = new QListWidget(aListWgt);
90 myToolbarsList->addItems(theToolbars.keys());
91 aListLayout->addWidget(myToolbarsList);
94 QWidget* aButtonsWgt = new QWidget(aControlsWgt);
95 QVBoxLayout* aBtnLayout = new QVBoxLayout(aButtonsWgt);
96 aBtnLayout->setContentsMargins(0, 0, 0, 0);
97 aContolsLay->addWidget(aButtonsWgt);
99 QPushButton* aAddBtn = new QPushButton(tr("Add..."), aButtonsWgt);
100 connect(aAddBtn, SIGNAL(clicked(bool)), SLOT(onAdd()));
101 aBtnLayout->addWidget(aAddBtn);
103 QPushButton* aEditBtn = new QPushButton(tr("Edit..."), aButtonsWgt);
104 connect(aEditBtn, SIGNAL(clicked(bool)), SLOT(onEdit()));
105 aBtnLayout->addWidget(aEditBtn);
107 QPushButton* aDeleteBtn = new QPushButton(tr("Delete"), aButtonsWgt);
108 connect(aDeleteBtn, SIGNAL(clicked(bool)), SLOT(onDelete()));
109 aBtnLayout->addWidget(aDeleteBtn);
110 aBtnLayout->addStretch(1);
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()));
120 void SHAPERGUI_ToolbarsDlg::onAdd()
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();
130 QString aMsg = tr("A tool bar with name %1 already exists").arg(aNewToolbar);
131 QMessageBox::warning(this, tr("Create toolbar"), aMsg);
136 void SHAPERGUI_ToolbarsDlg::onEdit()
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]);
148 void SHAPERGUI_ToolbarsDlg::onDelete()
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();
161 void SHAPERGUI_ToolbarsDlg::updateToolbarsList()
163 myToolbarsList->clear();
164 myToolbarsList->addItems(myToolbars.keys());
167 QIntList SHAPERGUI_ToolbarsDlg::getFreeCommands() const
169 QIntList aFreeCommands;
170 QMap<QString, QIntList>::const_iterator aIt;
171 foreach(int aCmd, myActionsList) {
173 for (aIt = myToolbars.cbegin(); aIt != myToolbars.cend(); aIt++) {
174 if (aIt.value().contains(aCmd)) {
180 aFreeCommands.append(aCmd);
182 return aFreeCommands;
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),
196 myFreeItems(theFreeItems),
197 myToolItems(theItemsList)
199 setWindowTitle(tr("Edit toolbar items"));
201 QVBoxLayout* aMailLayout = new QVBoxLayout(this);
204 QWidget* aNameWgt = new QWidget(this);
205 QHBoxLayout* aNameLay = new QHBoxLayout(aNameWgt);
206 aNameLay->setContentsMargins(0, 0, 0, 0);
207 aMailLayout->addWidget(aNameWgt);
209 aNameLay->addWidget(new QLabel(tr("Toolbar name:"), aNameWgt));
210 QLabel* aNameLbl = new QLabel(theToolbar, aNameWgt);
211 QFont aFont = aNameLbl->font();
213 aNameLbl->setFont(aFont);
214 aNameLay->addWidget(aNameLbl);
215 aNameLay->addStretch(1);
218 QWidget* aControlsWgt = new QWidget(this);
219 QHBoxLayout* aCtrlLayout = new QHBoxLayout(aControlsWgt);
220 aCtrlLayout->setContentsMargins(0, 0, 0, 0);
221 aMailLayout->addWidget(aControlsWgt);
224 QWidget* aCommandsWgt = new QWidget(aControlsWgt);
225 QVBoxLayout* aCommandsLay = new QVBoxLayout(aCommandsWgt);
226 aCommandsLay->setContentsMargins(0, 0, 0, 0);
227 aCtrlLayout->addWidget(aCommandsWgt);
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));
234 myCommandsList->setMaximumWidth(150);
235 aCommandsLay->addWidget(myCommandsList);
238 QWidget* aButtonsWgt = new QWidget(aControlsWgt);
239 QVBoxLayout* aBtnLayout = new QVBoxLayout(aButtonsWgt);
240 aBtnLayout->setContentsMargins(0, 0, 0, 0);
241 aCtrlLayout->addWidget(aButtonsWgt);
243 aBtnLayout->addStretch(1);
244 QPushButton* aAddButton = new QPushButton("--->", aButtonsWgt);
245 connect(aAddButton, SIGNAL(clicked(bool)), SLOT(onAddItem()));
246 aBtnLayout->addWidget(aAddButton);
248 aBtnLayout->addSpacing(20);
250 QPushButton* aDelButton = new QPushButton("<---", aButtonsWgt);
251 connect(aDelButton, SIGNAL(clicked(bool)), SLOT(onDelItem()));
252 aBtnLayout->addWidget(aDelButton);
253 aBtnLayout->addStretch(1);
256 QWidget* aItemsWgt = new QWidget(aControlsWgt);
257 QVBoxLayout* aItemsLay = new QVBoxLayout(aItemsWgt);
258 aItemsLay->setContentsMargins(0, 0, 0, 0);
259 aCtrlLayout->addWidget(aItemsWgt);
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));
266 myItemsList->setMaximumWidth(150);
267 aItemsLay->addWidget(myItemsList);
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()));
277 void SHAPERGUI_ToolbarItemsDlg::onAddItem()
282 void SHAPERGUI_ToolbarItemsDlg::onDelItem()