Root/Software/sie_cg/diagramscene.cpp

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 <QHash>
44#include "diagramscene.h"
45#include "arrow.h"
46
47DiagramScene::DiagramScene(QMenu *itemMenu, MainWindow *ownerWindow,
48                           QObject *parent)
49    : QGraphicsScene(parent)
50{
51    myItemMenu = itemMenu;
52    myMode = MoveItem;
53    myItemType = "";
54    line = 0;
55    textItem = 0;
56    myItemColor = Qt::white;
57    myTextColor = Qt::black;
58    myLineColor = Qt::black;
59    snapToGrid=1;
60    myGrid=10;
61    drawGrid=0;
62    myOwnerWindow=ownerWindow;
63}
64
65void DiagramScene::setLineColor(const QColor &color)
66{
67    myLineColor = color;
68
69    //Lists of items
70    QList<Arrow *> Arrows;
71    QList<lineItem *> lineItems;
72    foreach(QGraphicsItem *item, selectedItems()){
73        if(item->type() == lineItem::Type)
74            lineItems.append(qgraphicsitem_cast<lineItem *>(item));
75        else if(item->type() == Arrow::Type)
76            Arrows.append(qgraphicsitem_cast<Arrow *>(item));
77    }
78
79    foreach(Arrow *arrow, Arrows){
80        saveUndoState();
81        arrow->setColor(myLineColor);
82    }
83
84    foreach(lineItem *line, lineItems){
85        saveUndoState();
86        line->myOwner()->setColor(myLineColor);
87    }
88
89    update();
90}
91
92void DiagramScene::setTextColor(const QColor &color)
93{
94    myTextColor = color;
95    //Lists of items
96    QList<DiagramTextItem *> textItems;
97    foreach(QGraphicsItem *item, selectedItems()){
98        if(item->type() == DiagramTextItem::Type)
99            textItems.append(qgraphicsitem_cast<DiagramTextItem *>(item));
100    }
101
102    foreach(DiagramTextItem *textItem, textItems){
103        if(textItem->ownerItem()==0)
104        {
105            saveUndoState();
106            textItem->setDefaultTextColor(myTextColor);
107        }
108    }
109
110    update();
111}
112
113void DiagramScene::setItemColor(const QColor &color)
114{
115    myItemColor = color;
116    //Lists of items
117    QList<DiagramItem *> items;
118    foreach(QGraphicsItem *item, selectedItems()){
119        if(item->type() == DiagramItem::Type)
120            items.append(qgraphicsitem_cast<DiagramItem *>(item));
121    }
122
123    foreach(DiagramItem *item, items){
124        saveUndoState();
125        item->setColor(myItemColor);
126    }
127
128}
129
130void DiagramScene::setFont(QFont font)
131{
132    myFont = font;
133
134    if (isItemChange(DiagramTextItem::Type)) {
135        saveUndoState();
136        DiagramTextItem *item =
137            qgraphicsitem_cast<DiagramTextItem *>(selectedItems().first());
138            item->setFontt(myFont);
139    }
140}
141
142void DiagramScene::setMode(Mode mode)
143{
144    myMode = mode;
145}
146
147void DiagramScene::setItemType(QString type)
148{
149    myItemType = type;
150}
151
152void DiagramScene::saveUndoState()
153{
154    saveUndo();
155    clearRedo();
156    fflush(stdout);
157}
158void DiagramScene::saveUndo()
159{
160    myOwnerWindow->undoList.append(toXmlFormat());
161    fflush(stdout);
162}
163
164void DiagramScene::removeLastUndo()
165{
166    if(!myOwnerWindow->undoList.isEmpty())
167        myOwnerWindow->undoList.removeLast();
168    fflush(stdout);
169}
170
171void DiagramScene::clearRedo()
172{
173    myOwnerWindow->redoList.clear();
174    fflush(stdout);
175}
176
177void DiagramScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
178{
179    doSnapToGrid(mouseEvent);
180
181    if (mouseEvent->button() != Qt::LeftButton)
182        return;
183
184    foreach (QGraphicsItem *item, this->items(mouseEvent->scenePos())) {
185        if (item->type() == DiagramTextItem::Type)
186        {
187            myOwnerWindow->setFontSettings(qgraphicsitem_cast<DiagramTextItem *>
188                                           (item)->font());
189        }
190    }
191
192    DiagramItem *item;
193    int addResult=0;
194
195    switch (myMode) {
196        case InsertItem:
197            saveUndoState();
198            item = new DiagramItem(myItemMenu,myItemType,
199                                   domElementsByName->value(myItemType),0,this);
200            addResult=addDiagramItem(item);
201            if(addResult!=-1)
202            {
203                item->setColor(myItemColor);
204                item->setPos(mouseEvent->scenePos());
205                item->setID(addResult);
206                item->updateTexts();
207                addItem(item);
208            }
209            else
210            {
211                delete(item);
212                QMessageBox::warning(0,"Full","The diagram can only have "
213                                     "5000 blocks. <b>You are crasy?</b>");
214            }
215            emit itemInserted(item);
216            break;
217        case InsertLine:
218            line = new QGraphicsLineItem(QLineF(mouseEvent->scenePos(),
219                                        mouseEvent->scenePos()));
220            line->setPen(QPen(myLineColor, 2));
221            line->setZValue(1000.0);
222            snapToGrid=0;
223            addItem(line);
224            break;
225        case InsertText:
226            saveUndoState();
227            textItem = new DiagramTextItem(0,this);
228            textItem->setFontt(myFont);
229            textItem->setZValue(1000.0);
230            textItem->setDefaultTextColor(myTextColor);
231            textItem->setPos(mouseEvent->scenePos());
232            addItem(textItem);
233            emit textInserted(textItem);
234    default:
235        ;
236    }
237    QGraphicsScene::mousePressEvent(mouseEvent);
238}
239
240void DiagramScene::doSnapToGrid(QGraphicsSceneMouseEvent *mouseEvent)
241{
242    if(snapToGrid){
243        mouseEvent->setScenePos(QPointF(
244                                round(mouseEvent->scenePos().x()/myGrid)*myGrid,
245                                round(mouseEvent->scenePos().y()/myGrid)*myGrid
246                                ));
247    }
248}
249
250void DiagramScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
251{
252    doSnapToGrid(mouseEvent);
253    if (myMode == InsertLine && line != 0) {
254        QLineF newLine(line->line().p1(), mouseEvent->scenePos());
255        line->setLine(newLine);
256    } else if (myMode == MoveItem) {
257        QGraphicsScene::mouseMoveEvent(mouseEvent);
258    }
259    QPointF mousePos = mouseEvent->scenePos();
260    QString barMesg= QString("X[%1] Y[%2]").arg(QString::number(mousePos.x()))
261                                           .arg(QString::number(mousePos.y()));
262
263    foreach (QGraphicsItem *item, items(mousePos)) {
264        if (item->type() == DiagramItem::Type) {
265             barMesg+= tr(" ** Over diagram block with ID[") +
266                       QString::number(qgraphicsitem_cast<DiagramItem *>
267                                  (item)->getID()) + tr("]");
268        }
269    }
270    myOwnerWindow->statusBar->showMessage(barMesg);
271}
272
273void DiagramScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *mouseEvent)
274{
275    doSnapToGrid(mouseEvent);
276    QGraphicsScene::mouseDoubleClickEvent(mouseEvent);
277}
278
279void DiagramScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
280{
281    doSnapToGrid(mouseEvent);
282    if (line != 0 && myMode == InsertLine) {
283        QList<QGraphicsItem *> startItems = items(line->line().p1());
284        if (startItems.count() && startItems.first() == line)
285            startItems.removeFirst();
286        QList<QGraphicsItem *> endItems = items(line->line().p2());
287        if (endItems.count() && endItems.first() == line)
288            endItems.removeFirst();
289
290        removeItem(line);
291        delete line;
292        //Diferents items and valid type
293        if (startItems.count() > 0 && endItems.count() > 0 &&
294            startItems.first()->type() == DiagramTextItem::Type &&
295            endItems.first()->type() == DiagramTextItem::Type &&
296            startItems.first() != endItems.first())
297        {
298            DiagramTextItem *startItem_ =
299                qgraphicsitem_cast<DiagramTextItem *>(startItems.first());
300            DiagramTextItem *endItem_ =
301                qgraphicsitem_cast<DiagramTextItem *>(endItems.first());
302
303            //Real first and real end item
304            DiagramTextItem *startItem = startItem_;
305            DiagramTextItem *endItem = endItem_;
306            if(startItem_->styleIO()>>7)
307            {
308                startItem = endItem_;
309                endItem = startItem_;
310            }
311
312            //Inputs to outputs and diferent owner
313            if(startItem->styleIO()>0 &&
314               endItem->styleIO()>0 &&
315              (startItem->styleIO()>>7 != endItem->styleIO()>>7) &&
316              (startItem->ownerItem() != endItem->ownerItem()) &&
317               !endItem->ownerItem()->existArrow(startItem,endItem)
318              )
319            {
320                saveUndoState();
321                Arrow *arrow = new Arrow(startItem, endItem,0,this);
322                arrow->setColor(myLineColor);
323                startItem->ownerItem()->addArrow(arrow);
324                endItem->ownerItem()->addArrow(arrow);
325                arrow->setZValue(0.0);
326                addItem(arrow);
327                arrow->updatePosition();
328            }
329        }
330    }
331    line = 0;
332    snapToGrid=1;
333    QGraphicsScene::mouseReleaseEvent(mouseEvent);
334}
335
336bool DiagramScene::isItemChange(int type)
337{
338    foreach (QGraphicsItem *item, selectedItems()) {
339        if (item->type() == type)
340            return true;
341    }
342    return false;
343}
344
345int DiagramScene::addDiagramItem(DiagramItem * item)
346{
347    for(int i=0; i<5000; i++)
348    {
349        QHash<int,DiagramItem *>::iterator iter= DiagramsID.find(i);
350        if(iter==DiagramsID.end())
351        {
352            DiagramsID.insert(i,item);
353            return i;
354        }
355    }
356    return -1;
357}
358
359void DiagramScene::removeDiagramItem(DiagramItem * item)
360{
361    DiagramsID.remove(DiagramsID.key(item));
362}
363
364QDomDocument DiagramScene::toXmlFormat()
365{
366    QDomDocument document;
367    QDomComment initialComments=document.createComment(
368        "\nFile for SIE Code Generator.\n"
369        "**WARNING**If you are going to edit this file note that:\n"
370        "In order to segmentation faults prevention the load process \n"
371        "is started loading the libraries, then items and finally arrows.\n"
372        "Arrows depend of items, and items depend of libraries!!!!\n");
373    document.appendChild(initialComments);
374    QDomElement diagram = document.createElement("Diagram");
375    document.appendChild(diagram);
376
377    //Lists of items
378    QList<DiagramItem *> Items;
379    QList<Arrow *> Arrows;
380    QList<DiagramTextItem *> TextItems;
381    foreach(QGraphicsItem *item, items())
382    {
383        if(item->type() == DiagramItem::Type)
384            Items.append(qgraphicsitem_cast<DiagramItem *>(item));
385        else if(item->type() == Arrow::Type)
386            Arrows.append(qgraphicsitem_cast<Arrow *>(item));
387        else if(item->type() == DiagramTextItem::Type)
388            if(qgraphicsitem_cast<DiagramTextItem *>(item)->ownerItem() == 0)
389                TextItems.append(qgraphicsitem_cast<DiagramTextItem *>(item));
390    }
391    //Create the XML structure
392    QDomElement element;
393
394    //Save libraries
395    if(!libraryList.isEmpty())
396    {
397        QDomElement libraries = document.createElement("Libraries");
398        foreach(QString lib, libraryList)
399        {
400            element = document.createElement("Library");
401            element.setAttribute("Dir",lib);
402            libraries.appendChild(element);
403        }
404        diagram.appendChild(libraries);
405
406    }
407
408    //Save options
409        QDomElement options = document.createElement("Options");
410        options.setAttribute("templateDir", myOwnerWindow->templateDir);
411        options.setAttribute("templateFile", myOwnerWindow->templateFile);
412        options.setAttribute("makeFile", myOwnerWindow->makeFile);
413        options.setAttribute("workDir", myOwnerWindow->workDir);
414        options.setAttribute("executableFile", myOwnerWindow->executableFile);
415        options.setAttribute("mipsToolChainDir", myOwnerWindow->mipsToolChainDir);
416        options.setAttribute("mipsToolChain", myOwnerWindow->mipsToolChain);
417        options.setAttribute("sieWorkDir", myOwnerWindow->sieWorkDir);
418        options.setAttribute("fpgaFile", myOwnerWindow->fpgaFile);
419        options.setAttribute("sieIP", myOwnerWindow->sieIP);
420        options.setAttribute("configApp", myOwnerWindow->configApp);
421        diagram.appendChild(options);
422
423    //Save items
424    if(!Items.isEmpty())
425    {
426        QDomElement diagramItems = document.createElement("DiagramItems");
427        foreach(DiagramItem *item, Items)
428        {
429            element = item->toXml(document);
430            element.setAttribute("ID",item->getID()); //save index
431            diagramItems.appendChild(element);
432        }
433        diagram.appendChild(diagramItems);
434    }
435
436    //Save arrows
437    if(!Arrows.isEmpty())
438    {
439        QDomElement arrowItems = document.createElement("Arrows");
440        foreach(Arrow *item, Arrows)
441        {
442            element = item->toXml(document,DiagramsID);
443            element.setAttribute("ID",Arrows.indexOf(item)); //save index
444            arrowItems.appendChild(element);
445        }
446        diagram.appendChild(arrowItems);
447    }
448
449    //Save text items
450    if(!TextItems.isEmpty())
451    {
452        QDomElement textItems = document.createElement("TextItems");
453        foreach(DiagramTextItem *item, TextItems)
454        {
455            element = item->toXml(document);
456            textItems.appendChild(element);
457        }
458        diagram.appendChild(textItems);
459    }
460
461    return(document);
462}
463
464int DiagramScene::fromXmlFormat(QDomDocument document)
465{
466    DiagramsID.clear();
467    //Read diagrams TODO: in future... add multi projects functionality
468    QDomNodeList diagrams = document.elementsByTagName("Diagram");
469    if(!diagrams.at(0).isElement())
470        return 0;
471    //Load the first diagram in the document
472    QDomElement diagram = diagrams.at(0).toElement();
473    //In order to segmentation faults prevention the load process
474    //is started loading the libraries, then items and finally arrows.
475    //Arrows depend of items, and items depend of libraries!!!
476    //TODO: rewrite this process for reading in the order specified
477    for (QDomNode node = diagram.firstChild() ;
478         !node.isNull() ;
479         node = node.nextSibling())
480    {
481        QDomElement element = node.toElement();
482        if(element.tagName()=="Options")
483        {
484            myOwnerWindow->templateDir= element.attribute("templateDir");
485            myOwnerWindow->templateFile= element.attribute("templateFile");
486            myOwnerWindow->makeFile= element.attribute("makeFile");
487            myOwnerWindow->workDir= element.attribute("workDir");
488            myOwnerWindow->executableFile= element.attribute("executableFile");
489            myOwnerWindow->mipsToolChainDir= element.attribute("mipsToolChainDir");
490            myOwnerWindow->mipsToolChain= element.attribute("mipsToolChain");
491            myOwnerWindow->sieWorkDir= element.attribute("sieWorkDir");
492            myOwnerWindow->fpgaFile= element.attribute("fpgaFile");
493            myOwnerWindow->sieIP= element.attribute("sieIP");
494            myOwnerWindow->configApp= element.attribute("configApp");
495        }
496        else if(element.tagName()=="Libraries")
497        {
498            libraryList.clear();
499            for (QDomNode node = element.firstChild() ;
500                !node.isNull() ;
501                node = node.nextSibling())
502            {
503                //Load library directory
504                QDomElement Library = node.toElement();
505                if(Library.tagName()!="Library")
506                    return 0;
507                libraryList.append(Library.attribute("Dir"));
508            }
509            myOwnerWindow->updateLibraries();
510        }
511        else if(element.tagName()=="DiagramItems") //Load diagram items
512        {
513            for (QDomNode node = element.firstChild() ;
514                 !node.isNull() ;
515                 node = node.nextSibling())
516            {
517                //Load diagram item and add to scene
518                QDomElement diagramItem = node.toElement();
519                if(diagramItem.tagName()!="DiagramItem")
520                    return 0;
521
522                QPointF position = QPointF(diagramItem.attribute("x").toFloat(),
523                                          diagramItem.attribute("y").toFloat());
524                //PREVENT Segmentation faults:
525                if(!domElementsByName->contains(
526                   diagramItem.attribute("type")))
527                {
528                    QMessageBox::critical(0,"Error",QObject::tr(
529                                          "Diagram can't be loaded, because the"
530                                          " library for block [") +
531                                          diagramItem.attribute("type") +tr(
532                                          "] can't be found."));
533                    return 0;
534                }
535
536                DiagramItem *newItem=new DiagramItem(
537                        myItemMenu,
538                        diagramItem.attribute("type"),
539                        domElementsByName->value(diagramItem.attribute("type")),
540                        0,this, position,
541                        diagramItem.attribute("z").toDouble());
542
543                newItem->setColor(QColor(diagramItem.attribute("color")));
544                int itemID=diagramItem.attribute("ID").toInt();
545                newItem->setID(itemID);
546
547                addItem(newItem);
548                for (QDomNode node = diagramItem.firstChild() ;
549                     !node.isNull() ;
550                     node = node.nextSibling())
551                {
552                    //Load diagram text values and set on diagram item
553                    QDomElement diagramValues = node.toElement();
554                    if(diagramValues.tagName()!="diagramValues")
555                        return 0;
556
557                    for (QDomNode node = diagramValues.firstChild() ;
558                         !node.isNull() ;
559                         node = node.nextSibling())
560                    {
561                        QDomElement diagramValue = node.toElement();
562                        if(diagramValue.tagName()!="diagramValue")
563                            return 0;
564                        newItem->setValue(diagramValue.attribute("ID").toInt(),
565                                          diagramValue.attribute("value"));
566                    }
567                }
568                newItem->updateTexts();
569                if(DiagramsID.find(itemID)!=DiagramsID.end())
570                {
571                    int result=addDiagramItem(newItem);
572                    QMessageBox::warning(0,"ID Problems",
573                        tr("Block with ID[")+QString::number(itemID)+
574                        tr("] already exists, this will be reasigned to [")+
575                        QString::number(result)+
576                        tr("] for prevent problems."));
577                    newItem->setID(result);
578                }
579                else
580                    DiagramsID.insert(itemID,newItem);
581            }
582        }
583        else if(element.tagName()=="Arrows")
584        {
585
586            for (QDomNode node = element.firstChild() ;
587                 !node.isNull() ;
588                 node = node.nextSibling())
589            {
590                //Load arrow item and add to scene
591                QDomElement arrow = node.toElement();
592                if(arrow.tagName()!="Arrow")
593                    return 0;
594
595                DiagramTextItem *startItem=
596                        DiagramsID.value(arrow.attribute("start-Owner").toInt())
597                        ->pointerText(arrow.attribute("start-ID").toInt());
598                bool startIsIO =
599                        DiagramsID.value(arrow.attribute("start-Owner").toInt())
600                        ->textIsIO(arrow.attribute("start-ID").toInt());
601                DiagramTextItem *endItem=
602                        DiagramsID.value(arrow.attribute("end-Owner").toInt())
603                        ->pointerText(arrow.attribute("end-ID").toInt());
604                bool endIsIO =
605                        DiagramsID.value(arrow.attribute("end-Owner").toInt())
606                        ->textIsIO(arrow.attribute("end-ID").toInt());
607
608                if(!startIsIO||!endIsIO)
609                {
610                    QMessageBox::warning(0,"Arrow can't be loaded.",
611                                     tr("An arrow can not be loaded because ")+
612                                     tr("will be connected to a label")+
613                                     tr(" that isn't an Input/Output type. ")+
614                                     tr("Probable cause may be that the start")+
615                                     tr(" or end block has been modified."));
616                }
617                else if(startItem==0 || endItem==0)
618                {
619                    QMessageBox::warning(0,"Arrow can't be loaded",
620                                     tr("An arrow can not be loaded because ")+
621                                     tr("her IO label ID could not be found. ")+
622                                     tr("Probable cause may be that the start")+
623                                     tr(" or end block has been modified."));
624                }
625                else
626                {
627                    Arrow *newArrow = new Arrow(startItem, endItem,0,this);
628                    newArrow->setColor(QColor(arrow.attribute("color")));
629                    startItem->ownerItem()->addArrow(newArrow);
630                    endItem->ownerItem()->addArrow(newArrow);
631                    newArrow->setZValue(0.0);
632                    addItem(newArrow);
633                    newArrow->updatePosition();
634
635                    for (QDomNode node = arrow.firstChild() ;
636                         !node.isNull() ;
637                         node = node.nextSibling())
638                    {
639                        //Load diagram text values and set on diagram item
640                        QDomElement arrowCorners = node.toElement();
641                        if(arrowCorners.tagName()!="arrowCorners")
642                            return 0;
643                        int i=0;
644                        for (QDomNode node = arrowCorners.firstChild() ;
645                             !node.isNull() ;
646                             node = node.nextSibling())
647                        {
648                            QDomElement arrowCorner = node.toElement();
649                            if(arrowCorner.tagName()!="arrowCorner")
650                                return 0;
651
652                            QPointF cornerPos =
653                                    QPointF(arrowCorner.attribute("x").toFloat(),
654                                            arrowCorner.attribute("y").toFloat());
655
656                            newArrow->createCorner(cornerPos,++i);
657                        }
658                    }
659                }
660            }
661        }
662        else if(element.tagName()=="TextItems")
663        {
664            for (QDomNode node = element.firstChild() ;
665                !node.isNull() ;
666                node = node.nextSibling())
667            {
668                //Load library directory
669                QDomElement textItem = node.toElement();
670                if(textItem.tagName()!="TextItem")
671                    return 0;
672
673                QFont textFont;
674                textFont.setFamily(textItem.attribute("Family"));
675                textFont.setPointSize(textItem.attribute("PointSize").toInt());
676                textFont.setBold(textItem.attribute("Bold").toInt());
677                textFont.setItalic(textItem.attribute("Italic").toInt());
678                textFont.setUnderline(textItem.attribute("Underline").toInt());
679
680                DiagramTextItem *newText = new DiagramTextItem(0,this);
681                newText->setFontt(textFont);
682                newText->setDefaultTextColor(
683                        QColor(textItem.attribute("Color")));
684                newText->setPos(QPointF(
685                        textItem.attribute("x").toFloat(),
686                        textItem.attribute("y").toFloat()));
687                newText->setPlainText(textItem.attribute("Text"));
688                addItem(newText);
689            }
690        }
691    }
692
693    return 1;
694}
695
696void DiagramScene::cleanScene()
697{
698    //Lists of items
699    QList<DiagramItem *> Items;
700    QList<Arrow *> Arrows;
701    foreach(QGraphicsItem *item, items())
702    {
703        if(item->type() == DiagramItem::Type)
704            Items.append(qgraphicsitem_cast<DiagramItem *>(item));
705        else if(item->type() == Arrow::Type)
706            Arrows.append(qgraphicsitem_cast<Arrow *>(item));
707    }
708    //Delete only DiagramItems: When a diagramitem is deleted his arrows and
709    //textitems have to be deleted too, so if we delete only diagramitems then
710    //we are deleting all items in the scene. This is in order to prevent
711    //segmentation faults.
712    foreach(DiagramItem *item, Items)
713    {
714        item->removeArrows();
715        item->removeTextItems();
716        removeItem(item);
717        delete(item);
718    }
719    //Delete the text items without parents
720    foreach(QGraphicsItem *item, items())
721    {
722        removeItem(item);
723        delete(item);
724    }
725    this->clear();
726    DiagramsID.clear();
727}
728
729void DiagramScene::drawBackground(QPainter *p, const QRectF &r)
730{
731    //TODO: add a button for activate/deactivate the grid.
732
733    QGraphicsScene::drawBackground(p,r);
734    if(drawGrid)
735    {
736        p -> save();
737
738        p -> setRenderHint(QPainter::Antialiasing, false);
739        p -> setRenderHint(QPainter::TextAntialiasing, true);
740        p -> setRenderHint(QPainter::SmoothPixmapTransform, false);
741
742        p -> setPen(Qt::NoPen);
743        p -> setBrush(Qt::white);
744        p -> drawRect(r);
745
746
747        p -> setPen(Qt::black);
748        p -> setBrush(Qt::NoBrush);
749        qreal limite_x = r.x() + r.width();
750        qreal limite_y = r.y() + r.height();
751
752        int g_x = (int)ceil(r.x());
753        while (g_x % myGrid) ++ g_x;
754        int g_y = (int)ceil(r.y());
755        while (g_y % myGrid) ++ g_y;
756
757        QPolygon points;
758        for (int gx = g_x ; gx < limite_x ; gx += myGrid) {
759            for (int gy = g_y ; gy < limite_y ; gy += myGrid) {
760                points << QPoint(gx, gy);
761            }
762        }
763        p -> drawPoints(points);
764        p -> restore();
765    }
766}
767

Archive Download this file

Branches:
master



interactive