1 #include "GroupWidget.h"
3 #include "Dispatcher.h"
4 #include "Parameters.h"
6 #include "GroupScene.h"
7 #include "GroupBlock.h"
10 #include "ConnectionItem.h"
13 GroupWidget::GroupWidget(GroupWidget *_upperGroup, Dispatcher *_dispatcher,
15 QWidget *parent) : QWidget(parent) {
16 upperGroup = _upperGroup;
17 dispatcher = _dispatcher;
19 if (upperGroup == NULL) {
21 scene = new GroupScene(NULL, this, dispatcher, params, true);
25 scene = new GroupScene(upperGroup->getScene(), this, dispatcher, params, false);
28 layout = new QGridLayout;
30 QGraphicsView *view = new QGraphicsView(scene);
31 layout->addWidget(view,1,0,1,3);
36 setFocusPolicy(Qt::StrongFocus);
39 scene->setEditionMode(GroupScene::ItemEdition);
44 GroupWidget::~GroupWidget(){
45 static QString fctName = "GroupWidget::~GroupWidget()";
47 cout << "call to " << qPrintable(fctName) << endl;
49 // delete scene; // TO DO: must check if it is needed
52 void GroupWidget::changeConnectionMode(int mode) {
54 QPalette pal = buttonNewConnection->palette();
58 if(params->sceneMode != Parameters::EditOnConnection){
59 params->sceneMode = Parameters::EditOnConnection;
60 pal.setColor(QPalette::Button, QColor(Qt::lightGray));
63 params->sceneMode = Parameters::EditNoOperation;
64 pal.setColor(QPalette::Button, QColor("#edeceb"));
65 dispatcher->unselectAllInterfaces();
67 } else if(mode == Parameters::EditOnConnection){
68 params->sceneMode = Parameters::EditOnConnection;
69 pal.setColor(QPalette::Button, QColor(Qt::lightGray));
71 params->sceneMode = Parameters::EditNoOperation;
72 pal.setColor(QPalette::Button, QColor("#edeceb"));
74 dispatcher->unselectAllInterfaces();
75 buttonNewConnection->setAutoFillBackground(true);
76 buttonNewConnection->setPalette(pal);
77 buttonNewConnection->update();
82 void GroupWidget::enableGroupButton(bool b) {
83 newGroupAct->setEnabled(b);
87 void GroupWidget::mousePressEvent(QMouseEvent *e) {
88 dispatcher->setCurrentGroupWidget(Dispatcher::Design, this);
89 QWidget::mousePressEvent(e);
92 void GroupWidget::focusInEvent(QFocusEvent *e) {
94 if(params->currentWindow != this){
95 params->setCurrentWindow(this);
96 changeConnectionMode(false);
101 void GroupWidget::closeEvent(QCloseEvent *e) {
108 void GroupWidget::createActions() {
110 butAddConnection = new QToolButton(this);
111 butAddConnection->setIcon(QPixmap::fromImage(QImage("icons/add_connection.png")));
112 butAddConnection->setToolTip("Adding connections");
113 QPalette pal = butAddConnection->palette();
114 butBaseColor = pal.color(QPalette::Button);
115 connect(butAddConnection, SIGNAL(clicked()), this, SLOT(slotAddConnection()));
117 butEdit = new QToolButton(this);
118 butEdit->setIcon(QPixmap::fromImage(QImage("icons/edit_block.png")));
119 butEdit->setToolTip("Edit items");
120 connect(butEdit, SIGNAL(clicked()), this, SLOT(slotEdit()));
122 copyBlockAct = new QAction(tr("&Copy block"), this);
123 copyBlockAct->setIcon(QPixmap::fromImage(QImage("icons/copy.png")));
124 copyBlockAct->setStatusTip(tr("Copy the selected block"));
125 connect(copyBlockAct, SIGNAL(triggered()), this, SLOT(slotCopyBlock()));
127 newEmptyGroupAct = new QAction(tr("&Create a new empty group"), this);
128 newEmptyGroupAct->setIcon(QPixmap::fromImage(QImage("icons/add_group_void.png")));
129 newEmptyGroupAct->setStatusTip(tr("Create a new empty group"));
130 connect(newEmptyGroupAct, SIGNAL(triggered()), this, SLOT(slotNewEmptyGroup()));
132 newGroupAct = new QAction(tr("&Create a new group"), this);
133 newGroupAct->setIcon(QPixmap::fromImage(QImage("icons/add_group_select.png")));
134 newGroupAct->setStatusTip(tr("Create a new group"));
135 newGroupAct->setDisabled(true);
136 connect(newGroupAct, SIGNAL(triggered()), this, SLOT(slotNewGroup()));
138 deleteAct = new QAction(tr("&Delete selected elements"), this);
139 deleteAct->setIcon(QPixmap::fromImage(QImage("icons/delete.png")));
140 deleteAct->setStatusTip(tr("Delete selected elements"));
141 connect(deleteAct, SIGNAL(triggered()), this, SLOT(slotDeleteItems()));
143 selectAllAct = new QAction(tr("&Select all elements"), this);
144 selectAllAct->setIcon(QPixmap::fromImage(QImage("icons/delete.png")));
145 selectAllAct->setStatusTip(tr("Select all elements"));
146 connect(selectAllAct, SIGNAL(triggered()), this, SLOT(slotSelectAll()));
148 unselectAllAct = new QAction(tr("&unselect all elements"), this);
149 unselectAllAct->setIcon(QPixmap::fromImage(QImage("icons/delete.png")));
150 unselectAllAct->setStatusTip(tr("Unselect all elements"));
151 connect(unselectAllAct, SIGNAL(triggered()), this, SLOT(slotUnselectAll()));
154 void GroupWidget::createToolbar() {
155 toolbarEditMode = new QToolBar(tr("Mode"));
156 toolbarAdd = new QToolBar(tr("Group"));
157 //toolbarTools = new QToolBar(tr("Tools"));
159 toolbarEditMode->addWidget(new QLabel("Mode"));
160 //toolbarTools->addWidget(new QLabel("Tools"));
162 toolbarEditMode->addSeparator();
163 //toolbarTools->addSeparator();
165 toolbarEditMode->addWidget(butAddConnection);
166 toolbarEditMode->addWidget(butEdit);
168 //toolbarAdd->addAction(copyBlockAct);
169 toolbarAdd->addAction(newEmptyGroupAct);
170 toolbarAdd->addAction(newGroupAct);
172 //toolbarTools->addAction(deleteAct);
173 //toolbarTools->addAction(selectAllAct);
174 //toolbarTools->addAction(unselectAllAct);
176 layout->addWidget(toolbarEditMode,0,0);
177 layout->addWidget(toolbarAdd,0,1);
178 //layout->addWidget(toolbarTools,0,2);
181 void GroupWidget::slotEdit() {
182 dispatcher->unselectAllItems(Dispatcher::Design);
183 getScene()->setEditionMode(GroupScene::ItemEdition);
187 void GroupWidget::slotCopyBlock() {
188 foreach (BoxItem *item, params->getCurrentScene()->getBoxItems()) {
189 if(item->isSelected()){
190 dispatcher->duplicateBoxItem(Dispatcher::Design, item);
195 void GroupWidget::slotAddConnection() {
196 dispatcher->unselectAllItems(Dispatcher::Design);
197 getScene()->setEditionMode(GroupScene::AddConnection);
201 void GroupWidget::updateBlockButton() {
203 // reset all buttons to light gray
204 QPalette pal = butAddConnection->palette();
205 pal.setColor(QPalette::Button, butBaseColor);
207 butAddConnection->setAutoFillBackground(true);
208 butAddConnection->setPalette(pal);
209 butAddConnection->update();
210 butEdit->setAutoFillBackground(true);
211 butEdit->setPalette(pal);
214 // set the good one to dark gray
215 pal.setColor(QPalette::Button, QColor(Qt::darkGray));
217 if(getScene()->getEditionMode() == GroupScene::AddConnection) {
218 butAddConnection->setAutoFillBackground(true);
219 butAddConnection->setPalette(pal);
220 butAddConnection->update();
222 else if(getScene()->getEditionMode() == GroupScene::ItemEdition) {
223 butEdit->setAutoFillBackground(true);
224 butEdit->setPalette(pal);
230 void GroupWidget::slotNewEmptyGroup() {
232 dispatcher->addNewEmptyGroup(Dispatcher::Design, scene);
236 void GroupWidget::slotNewGroup()
238 dispatcher->addNewFullGroup(Dispatcher::Design );
241 void GroupWidget::slotDeleteItems() {
242 foreach (BoxItem *item, scene->getBoxItems()) {
243 if(item->isSelected()){
244 dispatcher->removeBoxItem(Dispatcher::Design, item);
247 foreach (ConnectionItem *item, scene->getConnectionItems()) {
248 if(item->isSelected()){
249 dispatcher->removeConnection(Dispatcher::Design, item);
254 void GroupWidget::slotSelectAll()
256 foreach(QGraphicsItem *item, params->getCurrentScene()->items()){
257 if(item->data(0) == "block"){
258 BoxItem *b = dynamic_cast<BoxItem*>(item);
259 b->setSelected(true);
260 } else if(item->data(0) == "connection"){
261 ConnectionItem *c = dynamic_cast<ConnectionItem*>(item);
262 c->setSelected(true);
264 item->update(item->boundingRect());
268 void GroupWidget::slotUnselectAll()
270 foreach(QGraphicsItem *item, params->getCurrentScene()->items()){
271 if(item->data(0) == "block"){
272 BoxItem *b = dynamic_cast<BoxItem*>(item);
273 b->setSelected(false);
274 } else if(item->data(0) == "connection"){
275 ConnectionItem *c = dynamic_cast<ConnectionItem*>(item);
276 c->setSelected(false);
279 item->update(item->boundingRect());