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(){}
46 void GroupWidget::changeConnectionMode(int mode) {
48 QPalette pal = buttonNewConnection->palette();
52 if(params->sceneMode != Parameters::EditOnConnection){
53 params->sceneMode = Parameters::EditOnConnection;
54 pal.setColor(QPalette::Button, QColor(Qt::lightGray));
57 params->sceneMode = Parameters::EditNoOperation;
58 pal.setColor(QPalette::Button, QColor("#edeceb"));
59 dispatcher->unselectAllInterfaces();
61 } else if(mode == Parameters::EditOnConnection){
62 params->sceneMode = Parameters::EditOnConnection;
63 pal.setColor(QPalette::Button, QColor(Qt::lightGray));
65 params->sceneMode = Parameters::EditNoOperation;
66 pal.setColor(QPalette::Button, QColor("#edeceb"));
68 dispatcher->unselectAllInterfaces();
69 buttonNewConnection->setAutoFillBackground(true);
70 buttonNewConnection->setPalette(pal);
71 buttonNewConnection->update();
76 void GroupWidget::enableGroupButton(bool b) {
77 newGroupAct->setEnabled(b);
81 void GroupWidget::mousePressEvent(QMouseEvent *e) {
82 dispatcher->setCurrentGroupWidget(this);
83 QWidget::mousePressEvent(e);
86 void GroupWidget::focusInEvent(QFocusEvent *e) {
88 if(params->currentWindow != this){
89 params->setCurrentWindow(this);
90 changeConnectionMode(false);
95 void GroupWidget::closeEvent(QCloseEvent *e) {
102 void GroupWidget::createActions() {
104 butAddConnection = new QToolButton(this);
105 butAddConnection->setIcon(QPixmap::fromImage(QImage("icons/add_connection.png")));
106 butAddConnection->setToolTip("Adding connections");
107 QPalette pal = butAddConnection->palette();
108 butBaseColor = pal.color(QPalette::Button);
109 connect(butAddConnection, SIGNAL(clicked()), this, SLOT(slotAddConnection()));
111 butEdit = new QToolButton(this);
112 butEdit->setIcon(QPixmap::fromImage(QImage("icons/edit_block.png")));
113 butEdit->setToolTip("Edit items");
114 connect(butEdit, SIGNAL(clicked()), this, SLOT(slotEdit()));
116 copyBlockAct = new QAction(tr("&Copy block"), this);
117 copyBlockAct->setIcon(QPixmap::fromImage(QImage("icons/copy.png")));
118 copyBlockAct->setStatusTip(tr("Copy the selected block"));
119 connect(copyBlockAct, SIGNAL(triggered()), this, SLOT(slotCopyBlock()));
121 newEmptyGroupAct = new QAction(tr("&Create a new empty group"), this);
122 newEmptyGroupAct->setIcon(QPixmap::fromImage(QImage("icons/add_group_void.png")));
123 newEmptyGroupAct->setStatusTip(tr("Create a new empty group"));
124 connect(newEmptyGroupAct, SIGNAL(triggered()), this, SLOT(slotNewEmptyGroup()));
126 newGroupAct = new QAction(tr("&Create a new group"), this);
127 newGroupAct->setIcon(QPixmap::fromImage(QImage("icons/add_group_select.png")));
128 newGroupAct->setStatusTip(tr("Create a new group"));
129 newGroupAct->setDisabled(true);
130 connect(newGroupAct, SIGNAL(triggered()), this, SLOT(slotNewGroup()));
132 deleteAct = new QAction(tr("&Delete selected elements"), this);
133 deleteAct->setIcon(QPixmap::fromImage(QImage("icons/delete.png")));
134 deleteAct->setStatusTip(tr("Delete selected elements"));
135 connect(deleteAct, SIGNAL(triggered()), this, SLOT(slotDeleteItems()));
137 selectAllAct = new QAction(tr("&Select all elements"), this);
138 selectAllAct->setIcon(QPixmap::fromImage(QImage("icons/delete.png")));
139 selectAllAct->setStatusTip(tr("Select all elements"));
140 connect(selectAllAct, SIGNAL(triggered()), this, SLOT(slotSelectAll()));
142 unselectAllAct = new QAction(tr("&unselect all elements"), this);
143 unselectAllAct->setIcon(QPixmap::fromImage(QImage("icons/delete.png")));
144 unselectAllAct->setStatusTip(tr("Unselect all elements"));
145 connect(unselectAllAct, SIGNAL(triggered()), this, SLOT(slotUnselectAll()));
148 void GroupWidget::createToolbar() {
149 toolbarEditMode = new QToolBar(tr("Mode"));
150 toolbarAdd = new QToolBar(tr("Group"));
151 toolbarTools = new QToolBar(tr("Tools"));
153 toolbarEditMode->addWidget(new QLabel("Mode"));
154 toolbarTools->addWidget(new QLabel("Tools"));
156 toolbarEditMode->addSeparator();
157 toolbarTools->addSeparator();
159 toolbarEditMode->addWidget(butAddConnection);
160 toolbarEditMode->addWidget(butEdit);
162 toolbarAdd->addAction(copyBlockAct);
163 toolbarAdd->addAction(newEmptyGroupAct);
164 toolbarAdd->addAction(newGroupAct);
166 toolbarTools->addAction(deleteAct);
167 toolbarTools->addAction(selectAllAct);
168 toolbarTools->addAction(unselectAllAct);
170 layout->addWidget(toolbarEditMode,0,0);
171 layout->addWidget(toolbarAdd,0,1);
172 layout->addWidget(toolbarTools,0,2);
175 void GroupWidget::slotEdit() {
176 dispatcher->unselectAllItems();
177 getScene()->setEditionMode(GroupScene::ItemEdition);
181 void GroupWidget::slotCopyBlock() {
182 foreach (BoxItem *item, params->getCurrentScene()->getBlockItems()) {
183 if(item->isSelected()){
184 dispatcher->duplicateBlock(item);
189 void GroupWidget::slotAddConnection() {
190 dispatcher->unselectAllItems();
191 getScene()->setEditionMode(GroupScene::AddConnection);
195 void GroupWidget::updateBlockButton() {
197 // reset all buttons to light gray
198 QPalette pal = butAddConnection->palette();
199 pal.setColor(QPalette::Button, butBaseColor);
201 butAddConnection->setAutoFillBackground(true);
202 butAddConnection->setPalette(pal);
203 butAddConnection->update();
204 butEdit->setAutoFillBackground(true);
205 butEdit->setPalette(pal);
208 // set the good one to dark gray
209 pal.setColor(QPalette::Button, QColor(Qt::darkGray));
211 if(getScene()->getEditionMode() == GroupScene::AddConnection) {
212 butAddConnection->setAutoFillBackground(true);
213 butAddConnection->setPalette(pal);
214 butAddConnection->update();
216 else if(getScene()->getEditionMode() == GroupScene::ItemEdition) {
217 butEdit->setAutoFillBackground(true);
218 butEdit->setPalette(pal);
224 void GroupWidget::slotNewEmptyGroup() {
226 dispatcher->addNewEmptyGroup(scene);
230 void GroupWidget::slotNewGroup()
232 dispatcher->addNewFullGroup();
235 void GroupWidget::slotDeleteItems() {
236 foreach (BoxItem *item, scene->getBlockItems()) {
237 if(item->isSelected()){
238 dispatcher->removeBlock(item);
241 foreach (ConnectionItem *item, scene->getConnectionItems()) {
242 if(item->isSelected()){
243 dispatcher->removeConnection(item);
248 void GroupWidget::slotSelectAll()
250 foreach(QGraphicsItem *item, params->getCurrentScene()->items()){
251 if(item->data(0) == "block"){
252 BoxItem *b = dynamic_cast<BoxItem*>(item);
253 b->setSelected(true);
254 } else if(item->data(0) == "connection"){
255 ConnectionItem *c = dynamic_cast<ConnectionItem*>(item);
256 c->setSelected(true);
258 item->update(item->boundingRect());
262 void GroupWidget::slotUnselectAll()
264 foreach(QGraphicsItem *item, params->getCurrentScene()->items()){
265 if(item->data(0) == "block"){
266 BoxItem *b = dynamic_cast<BoxItem*>(item);
267 b->setSelected(false);
268 } else if(item->data(0) == "connection"){
269 ConnectionItem *c = dynamic_cast<ConnectionItem*>(item);
270 c->setSelected(false);
273 item->update(item->boundingRect());