Hardware Design: SIE
Sign in or create your account | Project List | Help
Hardware Design: SIE Git Source Tree
Root/
Source at commit 9d578912b727722b22319832985451a79ec35747 created 13 years 4 months ago. By Juan64Bits, Updating prototype of SIE code generator. | |
---|---|
1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
4 | ** All rights reserved. |
5 | ** Contact: Nokia Corporation (qt-info@nokia.com) |
6 | ** |
7 | ** This file is part of the examples of the Qt Toolkit. |
8 | ** |
9 | ** $QT_BEGIN_LICENSE:LGPL$ |
10 | ** Commercial Usage |
11 | ** Licensees holding valid Qt Commercial licenses may use this file in |
12 | ** accordance with the Qt Commercial License Agreement provided with the |
13 | ** Software or, alternatively, in accordance with the terms contained in |
14 | ** a written agreement between you and Nokia. |
15 | ** |
16 | ** GNU Lesser General Public License Usage |
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
18 | ** General Public License version 2.1 as published by the Free Software |
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the |
20 | ** packaging of this file. Please review the following information to |
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements |
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
23 | ** |
24 | ** In addition, as a special exception, Nokia gives you certain additional |
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception |
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
27 | ** |
28 | ** GNU General Public License Usage |
29 | ** Alternatively, this file may be used under the terms of the GNU |
30 | ** General Public License version 3.0 as published by the Free Software |
31 | ** Foundation and appearing in the file LICENSE.GPL included in the |
32 | ** packaging of this file. Please review the following information to |
33 | ** ensure the GNU General Public License version 3.0 requirements will be |
34 | ** met: http://www.gnu.org/copyleft/gpl.html. |
35 | ** |
36 | ** If you have questions regarding the use of this file, please contact |
37 | ** Nokia at qt-info@nokia.com. |
38 | ** $QT_END_LICENSE$ |
39 | ** |
40 | ****************************************************************************/ |
41 | |
42 | #include <QtGui> |
43 | #include <QLabel> |
44 | |
45 | #include "mainwindow.h" |
46 | |
47 | const int InsertTextButton = 100000; |
48 | |
49 | MainWindow::MainWindow() |
50 | { |
51 | buttonGroup=0; |
52 | toolBox=0; |
53 | |
54 | createActions(); |
55 | createMenus(); |
56 | scene = new DiagramScene(itemMenu,this); |
57 | scene->setSceneRect(QRectF(0, 0, 5000, 5000)); |
58 | connect(scene, SIGNAL(itemInserted(DiagramItem*)), |
59 | this, SLOT(itemInserted(DiagramItem*))); |
60 | connect(scene, SIGNAL(textInserted(QGraphicsTextItem*)), |
61 | this, SLOT(textInserted(QGraphicsTextItem*))); |
62 | connect(scene, SIGNAL(itemSelected(QGraphicsItem*)), |
63 | this, SLOT(itemSelected(QGraphicsItem*))); |
64 | createToolBox(); |
65 | createToolbars(); |
66 | |
67 | setWindowTitle(tr("SIE Code Generator (Diagram Editor)")); |
68 | setUnifiedTitleAndToolBarOnMac(true); |
69 | myFilePath = ""; |
70 | libDialog=0; |
71 | |
72 | if(QApplication::argc()>1) |
73 | {newDiagram(QString(QApplication::argv()[1]));} |
74 | } |
75 | |
76 | void MainWindow::backgroundButtonGroupClicked(QAbstractButton *button) |
77 | { |
78 | QList<QAbstractButton *> buttons = backgroundButtonGroup->buttons(); |
79 | foreach (QAbstractButton *myButton, buttons) { |
80 | if (myButton != button) |
81 | button->setChecked(false); |
82 | } |
83 | QString text = button->text(); |
84 | if (text == tr("Blue Grid")) |
85 | scene->setBackgroundBrush(QPixmap(":/images/background1.png")); |
86 | else if (text == tr("White Grid")) |
87 | scene->setBackgroundBrush(QPixmap(":/images/background2.png")); |
88 | else if (text == tr("Gray Grid")) |
89 | scene->setBackgroundBrush(QPixmap(":/images/background3.png")); |
90 | else |
91 | scene->setBackgroundBrush(QPixmap(":/images/background4.png")); |
92 | |
93 | scene->update(); |
94 | view->update(); |
95 | } |
96 | |
97 | void MainWindow::buttonGroupClicked(int id) |
98 | { |
99 | QList<QAbstractButton *> buttons = buttonGroup->buttons(); |
100 | foreach (QAbstractButton *button, buttons) { |
101 | if (buttonGroup->button(id) != button) |
102 | button->setChecked(false); |
103 | } |
104 | if (id == InsertTextButton) { |
105 | scene->setMode(DiagramScene::InsertText); |
106 | } else { |
107 | scene->setItemType(buttonGroup->button(id)->text()); //Block name |
108 | scene->setMode(DiagramScene::InsertItem); |
109 | } |
110 | } |
111 | |
112 | void MainWindow::deleteItem() |
113 | { |
114 | foreach (QGraphicsItem *item, scene->selectedItems()) { |
115 | if (item->type() == DiagramItem::Type) { |
116 | qgraphicsitem_cast<DiagramItem *>(item)->removeArrows(); |
117 | qgraphicsitem_cast<DiagramItem *>(item)->removeTextItems(); |
118 | scene->removeItem(item); |
119 | delete(item); |
120 | } |
121 | //If arrow is deleted then is romoved from diagram owner |
122 | else if (item->type() == Arrow::Type) { |
123 | qgraphicsitem_cast<Arrow *>(item)->removeLines(); |
124 | qgraphicsitem_cast<Arrow *>(item)->startItem()->ownerItem() |
125 | ->removeArrow(qgraphicsitem_cast<Arrow *>(item)); |
126 | qgraphicsitem_cast<Arrow *>(item)->endItem()->ownerItem() |
127 | ->removeArrow(qgraphicsitem_cast<Arrow *>(item)); |
128 | scene->removeItem(item); |
129 | delete(item); |
130 | } |
131 | //If line is deleted then is romoved from the arrow owner |
132 | else if (item->type() == lineItem::Type && |
133 | !qgraphicsitem_cast<lineItem *>(item)->itemIsMovable()) { |
134 | |
135 | qgraphicsitem_cast<lineItem *>(item)->myOwner()->removeLine( |
136 | qgraphicsitem_cast<lineItem *>(item)); |
137 | qgraphicsitem_cast<lineItem *>(item)->myOwner()->updatePosition(); |
138 | scene->removeItem(item); |
139 | delete(item); |
140 | } |
141 | |
142 | } |
143 | } |
144 | |
145 | void MainWindow::pointerGroupClicked(int) |
146 | { |
147 | scene->setMode(DiagramScene::Mode(pointerTypeGroup->checkedId())); |
148 | } |
149 | |
150 | void MainWindow::bringToFront() |
151 | { |
152 | sendTo(1); |
153 | } |
154 | |
155 | void MainWindow::sendToBack() |
156 | { |
157 | sendTo(0); |
158 | } |
159 | |
160 | void MainWindow::sendTo(bool value) |
161 | { |
162 | if (scene->selectedItems().isEmpty()) |
163 | return; |
164 | |
165 | QGraphicsItem *selectedItem = scene->selectedItems().first(); |
166 | QList<QGraphicsItem *> overlapItems = selectedItem->collidingItems(); |
167 | |
168 | qreal zValue = 0; |
169 | foreach (QGraphicsItem *item, overlapItems) { |
170 | if (item->zValue() <= zValue && |
171 | item->type() == DiagramItem::Type) |
172 | { |
173 | if(value) |
174 | zValue = item->zValue() + 0.1; |
175 | else |
176 | zValue = item->zValue() - 0.1; |
177 | } |
178 | } |
179 | selectedItem->setZValue(zValue); |
180 | if(selectedItem->type() == DiagramItem::Type) |
181 | foreach (DiagramTextItem *texts, |
182 | qgraphicsitem_cast<DiagramItem *>(selectedItem)->getTextItems()) |
183 | { |
184 | texts->updatePosition(); |
185 | } |
186 | |
187 | } |
188 | |
189 | void MainWindow::itemInserted(DiagramItem *item) |
190 | { |
191 | pointerTypeGroup->button(int(DiagramScene::MoveItem))->setChecked(true); |
192 | scene->setMode(DiagramScene::Mode(pointerTypeGroup->checkedId())); |
193 | buttonGroup->button(buttonIdByName.value(item->diagramType()))->setChecked(false); |
194 | } |
195 | |
196 | void MainWindow::textInserted(QGraphicsTextItem *) |
197 | { |
198 | buttonGroup->button(InsertTextButton)->setChecked(false); |
199 | scene->setMode(DiagramScene::Mode(pointerTypeGroup->checkedId())); |
200 | } |
201 | |
202 | void MainWindow::currentFontChanged(const QFont &) |
203 | { |
204 | handleFontChange(); |
205 | } |
206 | |
207 | void MainWindow::fontSizeChanged(const QString &) |
208 | { |
209 | handleFontChange(); |
210 | } |
211 | |
212 | void MainWindow::sceneScaleChanged(const QString &scale) |
213 | { |
214 | double newScale = scale.left(scale.indexOf(tr("%"))).toDouble() / 100.0; |
215 | QMatrix oldMatrix = view->matrix(); |
216 | view->resetMatrix(); |
217 | view->translate(oldMatrix.dx(), oldMatrix.dy()); |
218 | view->scale(newScale, newScale); |
219 | } |
220 | |
221 | void MainWindow::textColorChanged() |
222 | { |
223 | textAction = qobject_cast<QAction *>(sender()); |
224 | fontColorToolButton->setIcon(createColorToolButtonIcon( |
225 | ":/images/textpointer.png", |
226 | qVariantValue<QColor>(textAction->data()))); |
227 | textButtonTriggered(); |
228 | } |
229 | |
230 | void MainWindow::itemColorChanged() |
231 | { |
232 | fillAction = qobject_cast<QAction *>(sender()); |
233 | fillColorToolButton->setIcon(createColorToolButtonIcon( |
234 | ":/images/floodfill.png", |
235 | qVariantValue<QColor>(fillAction->data()))); |
236 | fillButtonTriggered(); |
237 | } |
238 | |
239 | void MainWindow::lineColorChanged() |
240 | { |
241 | lineAction = qobject_cast<QAction *>(sender()); |
242 | lineColorToolButton->setIcon(createColorToolButtonIcon( |
243 | ":/images/linecolor.png", |
244 | qVariantValue<QColor>(lineAction->data()))); |
245 | lineButtonTriggered(); |
246 | } |
247 | |
248 | void MainWindow::textButtonTriggered() |
249 | { |
250 | scene->setTextColor(qVariantValue<QColor>(textAction->data())); |
251 | } |
252 | |
253 | void MainWindow::fillButtonTriggered() |
254 | { |
255 | scene->setItemColor(qVariantValue<QColor>(fillAction->data())); |
256 | } |
257 | |
258 | void MainWindow::lineButtonTriggered() |
259 | { |
260 | scene->setLineColor(qVariantValue<QColor>(lineAction->data())); |
261 | } |
262 | |
263 | void MainWindow::handleFontChange() |
264 | { |
265 | QFont font = fontCombo->currentFont(); |
266 | font.setPointSize(fontSizeCombo->currentText().toInt()); |
267 | font.setWeight(boldAction->isChecked() ? QFont::Bold : QFont::Normal); |
268 | font.setItalic(italicAction->isChecked()); |
269 | font.setUnderline(underlineAction->isChecked()); |
270 | |
271 | scene->setFont(font); |
272 | } |
273 | |
274 | void MainWindow::itemSelected(QGraphicsItem *item) |
275 | { |
276 | DiagramTextItem *textItem = |
277 | qgraphicsitem_cast<DiagramTextItem *>(item); |
278 | |
279 | QFont font = textItem->font(); |
280 | QColor color = textItem->defaultTextColor(); |
281 | fontCombo->setCurrentFont(font); |
282 | fontSizeCombo->setEditText(QString().setNum(font.pointSize())); |
283 | boldAction->setChecked(font.weight() == QFont::Bold); |
284 | italicAction->setChecked(font.italic()); |
285 | underlineAction->setChecked(font.underline()); |
286 | } |
287 | |
288 | void MainWindow::about() |
289 | { |
290 | QMessageBox::question(this, tr("About SIE Code Generator"), |
291 | tr("TODO <b>:)</b>")); |
292 | } |
293 | |
294 | void MainWindow::addLibrariesButtons(QGridLayout *layout) |
295 | { |
296 | int i=1; |
297 | domElementsByName.clear(); buttonIdByName.clear(); |
298 | QDomDocument customItems; |
299 | foreach(QString filepath, libraryList) |
300 | { |
301 | customItems = parseDocument(filepath); |
302 | for (QDomNode node = customItems.firstChild() ; |
303 | !node.isNull() ; |
304 | node = node.nextSibling()) |
305 | { |
306 | QDomElement *customItem = new QDomElement(node.toElement()); |
307 | if(customItem->tagName()=="CustomItem") |
308 | { |
309 | if(domElementsByName.contains( |
310 | customItem->attribute("BlockName"))) |
311 | { |
312 | QMessageBox::warning(this,"Block name in use.", |
313 | "The library " + filepath + |
314 | " has one block called" + |
315 | customItem->attribute("BlockName") + |
316 | " that is already in use. Only the " |
317 | " first Block loaded can be used, this" |
318 | " library will be ignored."); |
319 | } |
320 | else |
321 | { |
322 | domElementsByName.insert(customItem->attribute("BlockName"), |
323 | customItem); |
324 | buttonIdByName.insert(customItem->attribute("BlockName"),i-1); |
325 | |
326 | scene->setDomElementsByName(domElementsByName); |
327 | scene->setButtonIdByName(buttonIdByName); |
328 | |
329 | layout->addWidget(createCellWidget( |
330 | customItem->attribute("BlockName"), |
331 | domElementsByName. |
332 | value(customItem->attribute("BlockName"))),i/2,i&1); |
333 | i++; |
334 | } |
335 | } |
336 | } |
337 | } |
338 | } |
339 | |
340 | |
341 | |
342 | void MainWindow::createToolBox() |
343 | { |
344 | if(buttonGroup!=0) delete(buttonGroup); |
345 | if(toolBox!=0) delete(toolBox); |
346 | buttonGroup = new QButtonGroup; |
347 | buttonGroup->setExclusive(false); |
348 | connect(buttonGroup, SIGNAL(buttonClicked(int)), |
349 | this, SLOT(buttonGroupClicked(int))); |
350 | |
351 | QGridLayout *layout = new QGridLayout; |
352 | |
353 | addLibrariesButtons(layout); |
354 | |
355 | QToolButton *textButton = new QToolButton; |
356 | textButton->setCheckable(true); |
357 | buttonGroup->addButton(textButton, InsertTextButton); |
358 | textButton->setIcon(QIcon(QPixmap(":/images/textpointer.png") |
359 | .scaled(30, 30))); |
360 | textButton->setIconSize(QSize(50, 50)); |
361 | QGridLayout *textLayout = new QGridLayout; |
362 | textLayout->addWidget(textButton, 0, 0, Qt::AlignHCenter); |
363 | textLayout->addWidget(new QLabel(tr("Text")), 1, 0, Qt::AlignCenter); |
364 | QWidget *textWidget = new QWidget; |
365 | textWidget->setLayout(textLayout); |
366 | layout->addWidget(textWidget, 0, 0); |
367 | |
368 | layout->setRowStretch(3, 10); |
369 | layout->setColumnStretch(2, 10); |
370 | |
371 | QWidget *itemWidget = new QWidget; |
372 | itemWidget->setLayout(layout); |
373 | |
374 | toolBox = new QToolBox; |
375 | toolBox->setSizePolicy(QSizePolicy(QSizePolicy::Maximum, QSizePolicy::Ignored)); |
376 | toolBox->setMinimumWidth(itemWidget->sizeHint().width()); |
377 | toolBox->addItem(itemWidget, tr("Basic blocks")); |
378 | //toolBox->addItem(backgroundWidget, tr("Backgrounds")); |
379 | |
380 | //Add tool box to window |
381 | QHBoxLayout *newLayout = new QHBoxLayout; |
382 | newLayout->addWidget(toolBox); |
383 | view = new QGraphicsView(scene); |
384 | newLayout->addWidget(view); |
385 | |
386 | QWidget *widget = new QWidget; |
387 | widget->setLayout(newLayout); |
388 | |
389 | setCentralWidget(widget); |
390 | } |
391 | |
392 | void MainWindow::createActions() |
393 | { |
394 | toFrontAction = new QAction(QIcon(":/images/bringtofront.png"), |
395 | tr("Bring to &Front"), this); |
396 | toFrontAction->setShortcut(tr("Ctrl+F")); |
397 | toFrontAction->setStatusTip(tr("Bring item to front")); |
398 | connect(toFrontAction, SIGNAL(triggered()), |
399 | this, SLOT(bringToFront())); |
400 | |
401 | sendBackAction = new QAction(QIcon(":/images/sendtoback.png"), |
402 | tr("Send to &Back"), this); |
403 | sendBackAction->setShortcut(tr("Ctrl+B")); |
404 | sendBackAction->setStatusTip(tr("Send item to back")); |
405 | connect(sendBackAction, SIGNAL(triggered()), |
406 | this, SLOT(sendToBack())); |
407 | |
408 | deleteAction = new QAction(QIcon(":/images/delete.png"), |
409 | tr("&Delete"), this); |
410 | deleteAction->setShortcut(tr("Ctrl+Delete")); |
411 | deleteAction->setStatusTip(tr("Delete item from diagram")); |
412 | connect(deleteAction, SIGNAL(triggered()), |
413 | this, SLOT(deleteItem())); |
414 | |
415 | |
416 | newAction = new QAction(QIcon(":/images/new.png"),tr("&New"),this); |
417 | newAction->setShortcuts(QKeySequence::New); |
418 | newAction->setStatusTip("New diagram"); |
419 | connect(newAction,SIGNAL(triggered()),this,SLOT(newDiagram())); |
420 | |
421 | saveAction = new QAction(QIcon(":/images/save.png"),tr("&Save"),this); |
422 | saveAction->setShortcuts(QKeySequence::Save); |
423 | saveAction->setStatusTip("Save current diagram"); |
424 | connect(saveAction,SIGNAL(triggered()),this,SLOT(saveDiagram())); |
425 | |
426 | saveAsAction = new QAction(QIcon(":/images/save_as.png"), |
427 | tr("Save &As..."),this); |
428 | saveAsAction->setShortcuts(QKeySequence::SaveAs); |
429 | saveAsAction->setStatusTip("Save current diagram with another name"); |
430 | connect(saveAsAction,SIGNAL(triggered()),this,SLOT(saveAsDiagram())); |
431 | |
432 | openAction = new QAction(QIcon(":/images/open.png"),tr("&Open"),this); |
433 | openAction->setShortcuts(QKeySequence::Open); |
434 | openAction->setStatusTip("Open diagram"); |
435 | connect(openAction,SIGNAL(triggered()),this,SLOT(openDiagram())); |
436 | |
437 | exportImgAction = new QAction(tr("&Export to ..."),this); |
438 | exportImgAction->setStatusTip("Export current diagram to picture"); |
439 | connect(exportImgAction,SIGNAL(triggered()),this,SLOT(exportDiagram())); |
440 | |
441 | exitAction = new QAction(tr("E&xit"), this); |
442 | exitAction->setShortcuts(QKeySequence::Quit); |
443 | exitAction->setStatusTip(tr("Quit diagram editor")); |
444 | connect(exitAction, SIGNAL(triggered()), this, SLOT(close())); |
445 | |
446 | boldAction = new QAction(QIcon(":/images/bold.png"),tr("Bold"), this); |
447 | boldAction->setCheckable(true); |
448 | boldAction->setShortcut(tr("Ctrl+B")); |
449 | connect(boldAction, SIGNAL(triggered()), |
450 | this, SLOT(handleFontChange())); |
451 | |
452 | italicAction = new QAction(QIcon(":/images/italic.png"), |
453 | tr("Italic"), this); |
454 | italicAction->setCheckable(true); |
455 | italicAction->setShortcut(tr("Ctrl+I")); |
456 | connect(italicAction, SIGNAL(triggered()), |
457 | this, SLOT(handleFontChange())); |
458 | |
459 | underlineAction = new QAction(QIcon(":/images/underline.png"), |
460 | tr("Underline"), this); |
461 | underlineAction->setCheckable(true); |
462 | underlineAction->setShortcut(tr("Ctrl+U")); |
463 | connect(underlineAction, SIGNAL(triggered()), |
464 | this, SLOT(handleFontChange())); |
465 | |
466 | |
467 | |
468 | libraryAction = new QAction(tr("&Library..."),this); |
469 | connect(libraryAction, SIGNAL(triggered()), |
470 | this, SLOT(libraryForm())); |
471 | |
472 | optionsAction = new QAction(tr("&Options"),this); |
473 | optionsAction->setShortcuts(QKeySequence::Preferences); |
474 | connect(optionsAction, SIGNAL(triggered()), |
475 | this, SLOT(optionsForm())); |
476 | |
477 | aboutAction = new QAction(tr("A&bout"), this); |
478 | aboutAction->setShortcut(tr("Ctrl+B")); |
479 | connect(aboutAction, SIGNAL(triggered()), |
480 | this, SLOT(about())); |
481 | } |
482 | |
483 | void MainWindow::createMenus() |
484 | { |
485 | fileMenu = menuBar()->addMenu(tr("&File")); |
486 | fileMenu->addAction(newAction); |
487 | fileMenu->addAction(openAction); |
488 | fileMenu->addSeparator(); |
489 | fileMenu->addAction(saveAction); |
490 | fileMenu->addAction(saveAsAction); |
491 | fileMenu->addAction(exportImgAction); |
492 | fileMenu->addSeparator(); |
493 | fileMenu->addAction(exitAction); |
494 | |
495 | itemMenu = menuBar()->addMenu(tr("&Item")); |
496 | itemMenu->addAction(deleteAction); |
497 | itemMenu->addSeparator(); |
498 | itemMenu->addAction(toFrontAction); |
499 | itemMenu->addAction(sendBackAction); |
500 | |
501 | preferencesMenu= menuBar()->addMenu(tr("&Preferences")); |
502 | preferencesMenu->addAction(libraryAction); |
503 | preferencesMenu->addAction(optionsAction); |
504 | |
505 | aboutMenu = menuBar()->addMenu(tr("&Help")); |
506 | aboutMenu->addAction(aboutAction); |
507 | } |
508 | |
509 | void MainWindow::createToolbars() |
510 | { |
511 | fileToolBar = addToolBar(tr("File")); |
512 | fileToolBar->addAction(newAction); |
513 | fileToolBar->addAction(openAction); |
514 | fileToolBar->addAction(saveAction); |
515 | |
516 | editToolBar = addToolBar(tr("Edit")); |
517 | editToolBar->addAction(deleteAction); |
518 | editToolBar->addAction(toFrontAction); |
519 | editToolBar->addAction(sendBackAction); |
520 | |
521 | fontCombo = new QFontComboBox(); |
522 | fontSizeCombo = new QComboBox(); |
523 | connect(fontCombo, SIGNAL(currentFontChanged(QFont)), |
524 | this, SLOT(currentFontChanged(QFont))); |
525 | |
526 | fontSizeCombo = new QComboBox; |
527 | fontSizeCombo->setEditable(true); |
528 | for (int i = 8; i < 30; i = i + 2) |
529 | fontSizeCombo->addItem(QString().setNum(i)); |
530 | QIntValidator *validator = new QIntValidator(2, 64, this); |
531 | fontSizeCombo->setValidator(validator); |
532 | connect(fontSizeCombo, SIGNAL(currentIndexChanged(QString)), |
533 | this, SLOT(fontSizeChanged(QString))); |
534 | |
535 | fontColorToolButton = new QToolButton; |
536 | fontColorToolButton->setPopupMode(QToolButton::MenuButtonPopup); |
537 | fontColorToolButton->setMenu(createColorMenu(SLOT(textColorChanged()), |
538 | Qt::black)); |
539 | textAction = fontColorToolButton->menu()->defaultAction(); |
540 | fontColorToolButton->setIcon(createColorToolButtonIcon( |
541 | ":/images/textpointer.png", Qt::black)); |
542 | fontColorToolButton->setAutoFillBackground(true); |
543 | connect(fontColorToolButton, SIGNAL(clicked()), |
544 | this, SLOT(textButtonTriggered())); |
545 | |
546 | fillColorToolButton = new QToolButton; |
547 | fillColorToolButton->setPopupMode(QToolButton::MenuButtonPopup); |
548 | fillColorToolButton->setMenu(createColorMenu(SLOT(itemColorChanged()), |
549 | Qt::white)); |
550 | fillAction = fillColorToolButton->menu()->defaultAction(); |
551 | fillColorToolButton->setIcon(createColorToolButtonIcon( |
552 | ":/images/floodfill.png", Qt::white)); |
553 | connect(fillColorToolButton, SIGNAL(clicked()), |
554 | this, SLOT(fillButtonTriggered())); |
555 | |
556 | lineColorToolButton = new QToolButton; |
557 | lineColorToolButton->setPopupMode(QToolButton::MenuButtonPopup); |
558 | lineColorToolButton->setMenu(createColorMenu(SLOT(lineColorChanged()), |
559 | Qt::black)); |
560 | lineAction = lineColorToolButton->menu()->defaultAction(); |
561 | lineColorToolButton->setIcon(createColorToolButtonIcon( |
562 | ":/images/linecolor.png", Qt::black)); |
563 | connect(lineColorToolButton, SIGNAL(clicked()), |
564 | this, SLOT(lineButtonTriggered())); |
565 | |
566 | textToolBar = addToolBar(tr("Font")); |
567 | textToolBar->addWidget(fontCombo); |
568 | textToolBar->addWidget(fontSizeCombo); |
569 | textToolBar->addAction(boldAction); |
570 | textToolBar->addAction(italicAction); |
571 | textToolBar->addAction(underlineAction); |
572 | |
573 | colorToolBar = addToolBar(tr("Color")); |
574 | colorToolBar->addWidget(fontColorToolButton); |
575 | colorToolBar->addWidget(fillColorToolButton); |
576 | colorToolBar->addWidget(lineColorToolButton); |
577 | |
578 | QToolButton *pointerButton = new QToolButton; |
579 | pointerButton->setCheckable(true); |
580 | pointerButton->setChecked(true); |
581 | pointerButton->setIcon(QIcon(":/images/pointer.png")); |
582 | QToolButton *linePointerButton = new QToolButton; |
583 | linePointerButton->setCheckable(true); |
584 | linePointerButton->setIcon(QIcon(":/images/linepointer.png")); |
585 | |
586 | pointerTypeGroup = new QButtonGroup; |
587 | pointerTypeGroup->addButton(pointerButton, int(DiagramScene::MoveItem)); |
588 | pointerTypeGroup->addButton(linePointerButton, |
589 | int(DiagramScene::InsertLine)); |
590 | connect(pointerTypeGroup, SIGNAL(buttonClicked(int)), |
591 | this, SLOT(pointerGroupClicked(int))); |
592 | |
593 | sceneScaleCombo = new QComboBox; |
594 | QStringList scales; |
595 | scales << tr("50%") << tr("75%") << tr("100%") << tr("125%") << tr("150%"); |
596 | sceneScaleCombo->addItems(scales); |
597 | sceneScaleCombo->setCurrentIndex(2); |
598 | connect(sceneScaleCombo, SIGNAL(currentIndexChanged(QString)), |
599 | this, SLOT(sceneScaleChanged(QString))); |
600 | |
601 | pointerToolbar = addToolBar(tr("Pointer type")); |
602 | pointerToolbar->addWidget(pointerButton); |
603 | pointerToolbar->addWidget(linePointerButton); |
604 | pointerToolbar->addWidget(sceneScaleCombo); |
605 | } |
606 | |
607 | QWidget *MainWindow::createCellWidget(QString text, QDomElement *customItem) |
608 | { |
609 | DiagramItem item(itemMenu,text,customItem); |
610 | QIcon icon(item.image()); |
611 | |
612 | QToolButton *button = new QToolButton; |
613 | button->setText(text); |
614 | button->setIcon(icon); |
615 | button->setIconSize(QSize(50, 50)); |
616 | button->setCheckable(true); |
617 | buttonGroup->addButton(button, buttonIdByName.value(text)); |
618 | |
619 | QGridLayout *layout = new QGridLayout; |
620 | layout->addWidget(button, 0, 0, Qt::AlignHCenter); |
621 | layout->addWidget(new QLabel(text), 1, 0, Qt::AlignCenter); |
622 | |
623 | QWidget *widget = new QWidget; |
624 | widget->setLayout(layout); |
625 | |
626 | return widget; |
627 | } |
628 | |
629 | QMenu *MainWindow::createColorMenu(const char *slot, QColor defaultColor) |
630 | { |
631 | QList<QColor> colors; |
632 | colors << Qt::black << Qt::white << Qt::magenta |
633 | << Qt::cyan << Qt::red << Qt::blue << Qt::yellow << Qt::green |
634 | << Qt::darkMagenta << Qt::darkCyan << Qt::darkRed << Qt::darkBlue |
635 | << Qt::darkYellow << Qt::darkGreen << Qt::darkGray; |
636 | |
637 | QStringList names; |
638 | names << tr("Black") << tr("White") << tr("Magenta") << tr("Cyan") |
639 | << tr("Red") << tr("Blue") << tr("Yellow") << tr("Green") |
640 | << tr("Dark Magenta") << tr("Dark Cyan") << tr("Dark Red") |
641 | << tr("Dark Blue") << tr("Dark Yellow") << tr("Dark Green") |
642 | << tr("Dark Gray") ; |
643 | |
644 | QMenu *colorMenu = new QMenu; |
645 | for (int i = 0; i < colors.count(); ++i) { |
646 | QAction *action = new QAction(names.at(i), this); |
647 | action->setData(colors.at(i)); |
648 | action->setIcon(createColorIcon(colors.at(i))); |
649 | connect(action, SIGNAL(triggered()), |
650 | this, slot); |
651 | colorMenu->addAction(action); |
652 | if (colors.at(i) == defaultColor) { |
653 | colorMenu->setDefaultAction(action); |
654 | } |
655 | } |
656 | return colorMenu; |
657 | } |
658 | |
659 | QIcon MainWindow::createColorToolButtonIcon(const QString &imageFile, |
660 | QColor color) |
661 | { |
662 | QPixmap pixmap(50, 80); |
663 | pixmap.fill(Qt::transparent); |
664 | QPainter painter(&pixmap); |
665 | QPixmap image(imageFile); |
666 | QRect target(0, 0, 50, 60); |
667 | QRect source(0, 0, 42, 42); |
668 | painter.fillRect(QRect(0, 60, 50, 80), color); |
669 | painter.drawPixmap(target, image, source); |
670 | |
671 | return QIcon(pixmap); |
672 | } |
673 | |
674 | QIcon MainWindow::createColorIcon(QColor color) |
675 | { |
676 | QPixmap pixmap(20, 20); |
677 | QPainter painter(&pixmap); |
678 | painter.setPen(Qt::NoPen); |
679 | painter.fillRect(QRect(0, 0, 20, 20), color); |
680 | |
681 | return QIcon(pixmap); |
682 | } |
683 | |
684 | int MainWindow::newDiagram(QString filePath) |
685 | { |
686 | saveIfNeeded(); |
687 | scene->cleanScene(); |
688 | libraryList.clear(); //or set defaults |
689 | domElementsByName.clear(); //or set defaults |
690 | buttonIdByName.clear();//or set defaults |
691 | scene->setLibList(libraryList); |
692 | updateLibraries(); |
693 | myFilePath=""; |
694 | |
695 | if(filePath=="") |
696 | return 0; |
697 | |
698 | myFilePath=filePath; |
699 | if(!scene->fromXmlFormat(parseDocument(myFilePath))) |
700 | newDiagram(); |
701 | |
702 | return 1; |
703 | } |
704 | |
705 | QDomDocument MainWindow::parseDocument(QString filePath) |
706 | { |
707 | QDomDocument document; |
708 | QFile file(filePath); |
709 | if(file.open(QIODevice::ReadOnly | QIODevice::Text)) |
710 | { |
711 | bool parsing=document.setContent(&file); |
712 | file.close(); |
713 | if(!parsing) |
714 | { |
715 | QMessageBox::warning(this,"Operation failed","Failed to parse file, " |
716 | "wrong format or encoding."); |
717 | } |
718 | } |
719 | else |
720 | QMessageBox::critical(this,"Error","Could not open file for read."); |
721 | |
722 | return document; |
723 | } |
724 | |
725 | |
726 | |
727 | int MainWindow::saveIfNeeded() |
728 | { |
729 | if(myFilePath!="" || scene->items().count()>0) |
730 | {}//TODO save opened or modified diagram |
731 | return 0; |
732 | } |
733 | |
734 | int MainWindow::openDiagram() |
735 | { |
736 | QString |
737 | filePath = QFileDialog::getOpenFileName(this,"Open", |
738 | currentDir(),"Diagram for SIE code generator (*.sie)"); |
739 | |
740 | if(filePath.isEmpty()) |
741 | return 0; |
742 | |
743 | if(!QFileInfo(filePath).isReadable()) |
744 | { |
745 | QMessageBox::critical(this,"Error","File is not readable " |
746 | " or not exists."); |
747 | return 0; |
748 | } |
749 | |
750 | newDiagram(filePath); |
751 | return 0; |
752 | } |
753 | |
754 | int MainWindow::saveDiagram() |
755 | { |
756 | if(myFilePath=="") |
757 | { |
758 | saveAsDiagram(); |
759 | return 0; |
760 | } |
761 | if(!QFileInfo(myFilePath).isWritable() && QFileInfo(myFilePath).exists()) |
762 | { |
763 | QMessageBox::critical(this,"Error","File is not writable."); |
764 | return 0; |
765 | } |
766 | QFile file(myFilePath); |
767 | if(file.open(QIODevice::WriteOnly | QIODevice::Text)) |
768 | { |
769 | scene->setLibList(libraryList); |
770 | QDomDocument document = scene->toXmlFormat(); |
771 | QTextStream out(&file); |
772 | out.setCodec("UTF-8"); |
773 | out << document.toString(4); |
774 | file.close(); |
775 | return 1; |
776 | } |
777 | else |
778 | QMessageBox::critical(this,"Error","Could not open file for write."); |
779 | return 0; |
780 | } |
781 | |
782 | int MainWindow::saveAsDiagram() |
783 | { |
784 | QString filePath = QFileDialog::getSaveFileName(this,"Save as...", |
785 | currentDir(),"Diagram for SIE code generator (*.sie)"); |
786 | |
787 | if(!filePath.isEmpty()) |
788 | { |
789 | myFilePath = filePath; |
790 | saveDiagram(); |
791 | return 1; |
792 | } |
793 | return 0; |
794 | } |
795 | |
796 | int MainWindow::exportDiagram() |
797 | { |
798 | QString picturePath = QFileDialog::getSaveFileName(this,"Export", |
799 | currentDir(),"Portable Network Graphics (*.png)"); |
800 | |
801 | if(picturePath.isEmpty()) return 0; |
802 | |
803 | if(!QFileInfo(picturePath).isWritable() && QFileInfo(picturePath).exists()) |
804 | { |
805 | QMessageBox::critical(this,"Error","File is not writable."); |
806 | return 0; |
807 | } |
808 | scene->setDawGrid(0); |
809 | QSize sizeScene= scene->sceneRect().size().toSize(); |
810 | QImage img(sizeScene,QImage::Format_ARGB32_Premultiplied); |
811 | QPainter p(&img); |
812 | scene->render(&p, |
813 | scene->sceneRect(), |
814 | scene->itemsBoundingRect(), |
815 | Qt::KeepAspectRatio); |
816 | p.end(); |
817 | img.save(picturePath, "png"); |
818 | scene->setDawGrid(1); //TODO : set correct value |
819 | return 1; |
820 | } |
821 | |
822 | int MainWindow::libraryForm() |
823 | { |
824 | if(!libDialog) |
825 | { |
826 | createLibraryDialog(); |
827 | } |
828 | libDialog->setModal(1); |
829 | libUi->listLib->clear(); |
830 | libUi->listLib->addItems(libraryList); |
831 | libDialog->setWindowTitle(tr("Library paths...")); |
832 | QStringList oldLibraryList=libraryList; |
833 | libDialog->exec(); |
834 | |
835 | if (libDialog->result() == QDialog::Rejected) |
836 | libraryList = oldLibraryList; |
837 | else |
838 | { |
839 | scene->setLibList(libraryList); |
840 | updateLibraries(); |
841 | } |
842 | |
843 | return 0; |
844 | } |
845 | |
846 | void MainWindow::createLibraryDialog() |
847 | { |
848 | libDialog = new QDialog(this); |
849 | libUi = new Ui_libraryDialog; |
850 | libUi->setupUi(libDialog); |
851 | |
852 | connect(libUi->addLib,SIGNAL(clicked()),this,SLOT(addLibPath())); |
853 | connect(libUi->delLib,SIGNAL(clicked()),this,SLOT(delLibPath())); |
854 | connect(libUi->editLib,SIGNAL(clicked()),this,SLOT(editLibPath())); |
855 | } |
856 | |
857 | void MainWindow::addLibPath() |
858 | { |
859 | QString |
860 | filePath = QFileDialog::getOpenFileName(this,"Open", |
861 | currentDir(),"Custom block for SIE code generator (*.die)"); |
862 | |
863 | if(filePath.isEmpty()) |
864 | return; |
865 | |
866 | if(!QFileInfo(filePath).isReadable()) |
867 | { |
868 | QMessageBox::critical(this,"Error","File is not readable " |
869 | " or not exists."); |
870 | return; |
871 | } |
872 | if(libUi->listLib->findItems(filePath,Qt::MatchExactly).count()==0) |
873 | { |
874 | QDir myCurrentDir = this->currentDir(); |
875 | QString relativePath=myCurrentDir.relativeFilePath(filePath); |
876 | libraryList.append(relativePath); |
877 | libUi->listLib->addItem(relativePath); |
878 | } |
879 | else |
880 | QMessageBox::information(this,"Information","Library already exist."); |
881 | } |
882 | |
883 | void MainWindow::delLibPath() |
884 | { |
885 | QList<QListWidgetItem *> selected = libUi->listLib->selectedItems(); |
886 | if(selected.count()>0) |
887 | { |
888 | libraryList.removeOne(selected.at(0)->text()); |
889 | libUi->listLib->clear(); |
890 | libUi->listLib->addItems(libraryList); |
891 | } |
892 | } |
893 | |
894 | void MainWindow::editLibPath() |
895 | { |
896 | QList<QListWidgetItem *> selected = libUi->listLib->selectedItems(); |
897 | if(selected.count()>0) |
898 | { |
899 | callexternapp("block_editor/blockeditor",selected.at(0)->text()); |
900 | } |
901 | } |
902 | |
903 | int MainWindow::updateLibraries() |
904 | { |
905 | libraryList=scene->getLibList(); |
906 | createToolBox(); |
907 | return 0; |
908 | } |
909 | |
910 | |
911 | int MainWindow::optionsForm() |
912 | { |
913 | |
914 | return 0; |
915 | } |
916 | |
917 | QString MainWindow::callexternapp(QString xexecFile, QString xexecParm) |
918 | { |
919 | QEventLoop cxaw; |
920 | callexternappT cxat; |
921 | connect(&cxat, SIGNAL(finished()), &cxaw, SLOT(quit())); |
922 | cxat.execFile = xexecFile; |
923 | cxat.execParm = xexecParm; |
924 | cxat.start(); |
925 | cxaw.exec(); |
926 | return cxat.retnValu; |
927 | } |
928 | |
929 | |
930 |
Branches:
master