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) {
101 void GroupWidget::createActions() {
103 butAddConnection = new QToolButton(this);
104 butAddConnection->setIcon(QPixmap::fromImage(QImage("icons/add_connection.png")));
105 butAddConnection->setToolTip("Adding connections");
106 QPalette pal = butAddConnection->palette();
107 butBaseColor = pal.color(QPalette::Button);
108 connect(butAddConnection, SIGNAL(clicked()), this, SLOT(slotAddConnection()));
110 butEdit = new QToolButton(this);
111 butEdit->setIcon(QPixmap::fromImage(QImage("icons/edit_block.png")));
112 butEdit->setToolTip("Edit items");
113 connect(butEdit, SIGNAL(clicked()), this, SLOT(slotEdit()));
115 copyBlockAct = new QAction(tr("&Copy block"), this);
116 copyBlockAct->setIcon(QPixmap::fromImage(QImage("icons/copy.png")));
117 copyBlockAct->setStatusTip(tr("Copy the selected block"));
118 connect(copyBlockAct, SIGNAL(triggered()), this, SLOT(slotCopyBlock()));
120 newEmptyGroupAct = new QAction(tr("&Create a new empty group"), this);
121 newEmptyGroupAct->setIcon(QPixmap::fromImage(QImage("icons/add_group_void.png")));
122 newEmptyGroupAct->setStatusTip(tr("Create a new empty group"));
123 connect(newEmptyGroupAct, SIGNAL(triggered()), this, SLOT(slotNewEmptyGroup()));
125 newGroupAct = new QAction(tr("&Create a new group"), this);
126 newGroupAct->setIcon(QPixmap::fromImage(QImage("icons/add_group_select.png")));
127 newGroupAct->setStatusTip(tr("Create a new group"));
128 newGroupAct->setDisabled(true);
129 connect(newGroupAct, SIGNAL(triggered()), this, SLOT(slotNewGroup()));
131 deleteAct = new QAction(tr("&Delete selected elements"), this);
132 deleteAct->setIcon(QPixmap::fromImage(QImage("icons/delete.png")));
133 deleteAct->setStatusTip(tr("Delete selected elements"));
134 connect(deleteAct, SIGNAL(triggered()), this, SLOT(slotDeleteItems()));
136 selectAllAct = new QAction(tr("&Select all elements"), this);
137 selectAllAct->setIcon(QPixmap::fromImage(QImage("icons/delete.png")));
138 selectAllAct->setStatusTip(tr("Select all elements"));
139 connect(selectAllAct, SIGNAL(triggered()), this, SLOT(slotSelectAll()));
141 unselectAllAct = new QAction(tr("&unselect all elements"), this);
142 unselectAllAct->setIcon(QPixmap::fromImage(QImage("icons/delete.png")));
143 unselectAllAct->setStatusTip(tr("Unselect all elements"));
144 connect(unselectAllAct, SIGNAL(triggered()), this, SLOT(slotUnselectAll()));
147 void GroupWidget::createToolbar() {
148 toolbarEditMode = new QToolBar(tr("Mode"));
149 toolbarAdd = new QToolBar(tr("Group"));
150 toolbarTools = new QToolBar(tr("Tools"));
152 toolbarEditMode->addWidget(new QLabel("Mode"));
153 toolbarTools->addWidget(new QLabel("Tools"));
155 toolbarEditMode->addSeparator();
156 toolbarTools->addSeparator();
158 toolbarEditMode->addWidget(butAddConnection);
159 toolbarEditMode->addWidget(butEdit);
161 toolbarAdd->addAction(copyBlockAct);
162 toolbarAdd->addAction(newEmptyGroupAct);
163 toolbarAdd->addAction(newGroupAct);
165 toolbarTools->addAction(deleteAct);
166 toolbarTools->addAction(selectAllAct);
167 toolbarTools->addAction(unselectAllAct);
169 layout->addWidget(toolbarEditMode,0,0);
170 layout->addWidget(toolbarAdd,0,1);
171 layout->addWidget(toolbarTools,0,2);
174 void GroupWidget::slotEdit() {
175 dispatcher->unselectAllItems();
176 getScene()->setEditionMode(GroupScene::ItemEdition);
180 void GroupWidget::slotCopyBlock() {
181 foreach (BoxItem *item, params->getCurrentScene()->getBlockItems()) {
182 if(item->isSelected()){
183 dispatcher->duplicateBlock(item);
188 void GroupWidget::slotAddConnection() {
189 dispatcher->unselectAllItems();
190 getScene()->setEditionMode(GroupScene::AddConnection);
194 void GroupWidget::updateBlockButton() {
196 // reset all buttons to light gray
197 QPalette pal = butAddConnection->palette();
198 pal.setColor(QPalette::Button, butBaseColor);
200 butAddConnection->setAutoFillBackground(true);
201 butAddConnection->setPalette(pal);
202 butAddConnection->update();
203 butEdit->setAutoFillBackground(true);
204 butEdit->setPalette(pal);
207 // set the good one to dark gray
208 pal.setColor(QPalette::Button, QColor(Qt::darkGray));
210 if(getScene()->getEditionMode() == GroupScene::AddConnection) {
211 butAddConnection->setAutoFillBackground(true);
212 butAddConnection->setPalette(pal);
213 butAddConnection->update();
215 else if(getScene()->getEditionMode() == GroupScene::ItemEdition) {
216 butEdit->setAutoFillBackground(true);
217 butEdit->setPalette(pal);
223 void GroupWidget::slotNewEmptyGroup() {
225 // creating the GroupBlock in graph model
226 GroupBlock* parent = AB_TO_GRP(scene->getGroupItem()->getRefBlock());
227 cout << "new group : parent = "<< qPrintable(parent->getName()) << endl;
228 GroupBlock* groupBlock = params->getGraph()->createChildBlock(parent);
229 cout << "new group : child = "<< qPrintable(groupBlock->getName()) << ", child of " << qPrintable(groupBlock->getParent()->getName()) << endl;
230 // creating the BlockItem in the scene
231 BoxItem* newItem = scene->createBlockItem(groupBlock);
233 GroupWidget* child = dispatcher->createChildScene(this,newItem);
237 void GroupWidget::slotNewGroup()
239 dispatcher->addNewFullGroup();
242 void GroupWidget::slotDeleteItems() {
243 foreach (BoxItem *item, scene->getBlockItems()) {
244 if(item->isSelected()){
245 dispatcher->removeBlock(item);
248 foreach (ConnectionItem *item, scene->getConnectionItems()) {
249 if(item->isSelected()){
250 dispatcher->removeConnection(item);
255 void GroupWidget::slotSelectAll()
257 foreach(QGraphicsItem *item, params->getCurrentScene()->items()){
258 if(item->data(0) == "block"){
259 BoxItem *b = dynamic_cast<BoxItem*>(item);
260 b->setSelected(true);
261 } else if(item->data(0) == "connection"){
262 ConnectionItem *c = dynamic_cast<ConnectionItem*>(item);
263 c->setSelected(true);
265 item->update(item->boundingRect());
269 void GroupWidget::slotUnselectAll()
271 foreach(QGraphicsItem *item, params->getCurrentScene()->items()){
272 if(item->data(0) == "block"){
273 BoxItem *b = dynamic_cast<BoxItem*>(item);
274 b->setSelected(false);
275 } else if(item->data(0) == "connection"){
276 ConnectionItem *c = dynamic_cast<ConnectionItem*>(item);
277 c->setSelected(false);
280 item->update(item->boundingRect());