Root/Software/sie_cg/block_editor/mainwindow.cpp

Source at commit 9d578912b727722b22319832985451a79ec35747 created 13 years 27 days 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
47const int InsertTextButton = 10;
48
49MainWindow::MainWindow()
50{
51    createActions();
52    createToolBox();
53    createMenus();
54
55    scene = new DiagramScene(itemMenu);
56    scene->setSceneRect(QRectF(0, 0, 1000, 1000));
57    connect(scene, SIGNAL(textInserted(QGraphicsTextItem*)),
58        this, SLOT(textInserted(QGraphicsTextItem*)));
59
60    createToolbars();
61
62    QHBoxLayout *layout = new QHBoxLayout;
63    layout->addWidget(toolBox);
64    view = new QGraphicsView(scene);
65    layout->addWidget(view);
66
67    QWidget *widget = new QWidget;
68    widget->setLayout(layout);
69
70    setCentralWidget(widget);
71    setWindowTitle(tr("SIE Code Generator (Block Editor)"));
72    setUnifiedTitleAndToolBarOnMac(true);
73    myFilePath = "";
74
75    if(QApplication::argc()>1)
76        {newDiagram(QString(QApplication::argv()[1]));}
77}
78
79void MainWindow::deleteItem()
80{
81    foreach (QGraphicsItem *item, scene->selectedItems())
82    {
83        if (item->type() == Arrow::Type) {
84           qgraphicsitem_cast<Arrow *>(item)->removeLines();
85           scene->removeItem(item);
86           delete(item);
87        }
88        //If line is deleted then is romoved from the arrow owner
89        else if (item->type() == lineItem::Type &&
90                 !qgraphicsitem_cast<lineItem *>(item)->itemIsMovable()) {
91
92           qgraphicsitem_cast<lineItem *>(item)->myOwner()->removeLine(
93                                    qgraphicsitem_cast<lineItem *>(item));
94           qgraphicsitem_cast<lineItem *>(item)->myOwner()->updatePosition();
95           scene->removeItem(item);
96           delete(item);
97       }
98       else if (item->type() == DiagramTextItem::Type) {
99           if(qgraphicsitem_cast<DiagramTextItem *>(item)->styleIO()!=0xFFF)
100           {
101               scene->removeTextItem(qgraphicsitem_cast
102                                     <DiagramTextItem *>(item));
103               scene->removeItem(item);
104               delete(item);
105           }
106       }
107    }
108}
109
110void MainWindow::textInserted(QGraphicsTextItem*)
111{
112    buttonGroup->button(selectedButton)->setChecked(false);
113    scene->setMode(DiagramScene::MoveItem);
114}
115
116
117void MainWindow::sceneScaleChanged(const QString &scale)
118{
119    double newScale = scale.left(scale.indexOf(tr("%"))).toDouble() / 100.0;
120    QMatrix oldMatrix = view->matrix();
121    view->resetMatrix();
122    view->translate(oldMatrix.dx(), oldMatrix.dy());
123    view->scale(newScale, newScale);
124}
125
126void MainWindow::about()
127{
128    QMessageBox::question(this, tr("About SIE Code Generator"),
129                       tr("TODO <b>:)</b>"));
130}
131
132QWidget *MainWindow::createToolButton(int ID, QString type, QIcon icon)
133{
134    QToolButton *button = new QToolButton;
135    button->setIcon(icon);
136    button->setIconSize(QSize(50, 50));
137    button->setCheckable(true);
138    button->setText(type);
139    buttonGroup->addButton(button,ID);
140
141    QGridLayout *layout = new QGridLayout;
142    layout->addWidget(button, 0, 0, Qt::AlignHCenter);
143    layout->addWidget(new QLabel(type), 1, 0, Qt::AlignCenter);
144
145    QWidget *widget = new QWidget;
146    widget->setLayout(layout);
147
148    return widget;
149}
150
151void MainWindow::createToolBox()
152{
153    buttonGroup = new QButtonGroup;
154    buttonGroup->setExclusive(false);
155    connect(buttonGroup, SIGNAL(buttonClicked(int)),
156            this, SLOT(buttonGroupClicked(int)));
157
158    QGridLayout *layout = new QGridLayout;
159    //INPUTS
160    int i=0;
161    layout->addWidget(createToolButton(129+i,tr("Bool"),
162                      QIcon(":/images/background1.png")),++i,0);
163    layout->addWidget(createToolButton(129+i,tr("Char"),
164                      QIcon(":/images/background1.png")),++i,0);
165    layout->addWidget(createToolButton(129+i,tr("Integer"),
166                      QIcon(":/images/background1.png")),++i,0);
167    layout->addWidget(createToolButton(129+i,tr("Double"),
168                      QIcon(":/images/background1.png")),++i,0);
169    layout->addWidget(createToolButton(129+i,tr("Float"),
170                      QIcon(":/images/background1.png")),++i,0);
171    layout->addWidget(createToolButton(129+i,tr("Short"),
172                      QIcon(":/images/background1.png")),++i,0);
173    layout->addWidget(createToolButton(129+i,tr("Long"),
174                      QIcon(":/images/background1.png")),++i,0);
175    layout->addWidget(createToolButton(129+i,tr("UChar"),
176                      QIcon(":/images/background1.png")),++i,0);
177    layout->addWidget(createToolButton(129+i,tr("UInt"),
178                      QIcon(":/images/background1.png")),++i,0);
179    layout->addWidget(createToolButton(129+i,tr("UShort"),
180                      QIcon(":/images/background1.png")),++i,0);
181    layout->addWidget(createToolButton(129+i,tr("ULong"),
182                      QIcon(":/images/background1.png")),++i,0);
183
184
185
186    //OUTPUTS
187    i=0;
188    layout->addWidget(createToolButton(i,tr("Bool"),
189                      QIcon(":/images/background3.png")),++i,1);
190    layout->addWidget(createToolButton(i,tr("Char"),
191                      QIcon(":/images/background3.png")),++i,1);
192    layout->addWidget(createToolButton(i,tr("Integer"),
193                      QIcon(":/images/background3.png")),++i,1);
194    layout->addWidget(createToolButton(i,tr("Double"),
195                      QIcon(":/images/background3.png")),++i,1);
196    layout->addWidget(createToolButton(i,tr("Float"),
197                      QIcon(":/images/background3.png")),++i,1);
198    layout->addWidget(createToolButton(i,tr("Short"),
199                      QIcon(":/images/background3.png")),++i,1);
200    layout->addWidget(createToolButton(i,tr("Long"),
201                      QIcon(":/images/background3.png")),++i,1);
202    layout->addWidget(createToolButton(i,tr("UChar"),
203                      QIcon(":/images/background3.png")),++i,1);
204    layout->addWidget(createToolButton(i,tr("UInt"),
205                      QIcon(":/images/background3.png")),++i,1);
206    layout->addWidget(createToolButton(i,tr("UShort"),
207                      QIcon(":/images/background3.png")),++i,1);
208    layout->addWidget(createToolButton(i,tr("ULong"),
209                      QIcon(":/images/background3.png")),++i,1);
210
211    layout->setRowStretch(3, 10);
212    layout->setColumnStretch(2, 10);
213    QWidget *ioWidget = new QWidget;
214    ioWidget->setLayout(layout);
215
216    layout = new QGridLayout;
217    //Labels
218    layout->addWidget(createToolButton(256,tr("Label"),
219                      QIcon(":/images/background4.png")),0,0);
220    //Values
221    layout->addWidget(createToolButton(257,tr("Value"),
222                      QIcon(":/images/background4.png")),0,1);
223
224    //Polygon
225    layout->addWidget(createToolButton(258,tr("Polygon"),
226                      QIcon(":/images/background2.png")),1,0);
227
228    layout->setRowStretch(3, 10);
229    layout->setColumnStretch(2, 10);
230    QWidget *labelWidget = new QWidget;
231    labelWidget->setLayout(layout);
232
233
234    toolBox = new QToolBox;
235    toolBox->setSizePolicy(QSizePolicy(QSizePolicy::Maximum, QSizePolicy::Ignored));
236    toolBox->setMinimumWidth(labelWidget->sizeHint().width());
237    toolBox->addItem(labelWidget, tr("Basic draw"));
238    toolBox->addItem(ioWidget, tr("Inputs/Outputs"));
239
240}
241
242void MainWindow::buttonGroupClicked(int id)
243{
244    QList<QAbstractButton *> buttons = buttonGroup->buttons();
245    foreach (QAbstractButton *button, buttons)
246        {if (buttonGroup->button(id) != button) button->setChecked(false);}
247    selectedButton=id;
248
249    if(id==258)
250    { //Polygon edition
251        scene->setMode(DiagramScene::EditPolygon);
252    } else {
253        scene->setMode(DiagramScene::InsertText);
254    }
255
256    scene->setTextType(id,buttonGroup->button(id)->text());
257
258}
259
260void MainWindow::createActions()
261{
262
263    deleteAction = new QAction(QIcon(":/images/delete.png"),
264                               tr("&Delete"), this);
265    deleteAction->setShortcut(tr("Ctrl+Delete"));
266    deleteAction->setStatusTip(tr("Delete item from diagram"));
267    connect(deleteAction, SIGNAL(triggered()),
268        this, SLOT(deleteItem()));
269
270
271    newAction = new QAction(QIcon(":/images/new.png"),tr("&New"),this);
272    newAction->setShortcuts(QKeySequence::New);
273    newAction->setStatusTip("New diagram");
274    connect(newAction,SIGNAL(triggered()),this,SLOT(newDiagram()));
275
276    saveAction = new QAction(QIcon(":/images/save.png"),tr("&Save"),this);
277    saveAction->setShortcuts(QKeySequence::Save);
278    saveAction->setStatusTip("Save current diagram");
279    connect(saveAction,SIGNAL(triggered()),this,SLOT(saveDiagram()));
280
281    saveAsAction = new QAction(QIcon(":/images/save_as.png"),
282                               tr("Save &As..."),this);
283    saveAsAction->setShortcuts(QKeySequence::SaveAs);
284    saveAsAction->setStatusTip("Save current diagram with another name");
285    connect(saveAsAction,SIGNAL(triggered()),this,SLOT(saveAsDiagram()));
286
287    openAction = new QAction(QIcon(":/images/open.png"),tr("&Open"),this);
288    openAction->setShortcuts(QKeySequence::Open);
289    openAction->setStatusTip("Open diagram");
290    connect(openAction,SIGNAL(triggered()),this,SLOT(openDiagram()));
291
292    exitAction = new QAction(tr("E&xit"), this);
293    exitAction->setShortcuts(QKeySequence::Quit);
294    exitAction->setStatusTip(tr("Quit diagram editor"));
295    connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
296
297    aboutAction = new QAction(tr("A&bout"), this);
298    aboutAction->setShortcut(tr("Ctrl+B"));
299    connect(aboutAction, SIGNAL(triggered()),
300            this, SLOT(about()));
301}
302
303void MainWindow::createMenus()
304{
305    fileMenu = menuBar()->addMenu(tr("&File"));
306    fileMenu->addAction(newAction);
307    fileMenu->addAction(openAction);
308    fileMenu->addSeparator();
309    fileMenu->addAction(saveAction);
310    fileMenu->addAction(saveAsAction);
311    fileMenu->addSeparator();
312    fileMenu->addAction(exitAction);
313
314    itemMenu = menuBar()->addMenu(tr("&Item"));
315    itemMenu->addAction(deleteAction);
316
317    aboutMenu = menuBar()->addMenu(tr("&Help"));
318    aboutMenu->addAction(aboutAction);
319}
320
321void MainWindow::createToolbars()
322{
323    fileToolBar = addToolBar(tr("File"));
324    fileToolBar->addAction(newAction);
325    fileToolBar->addAction(openAction);
326    fileToolBar->addAction(saveAction);
327
328    editToolBar = addToolBar(tr("Edit"));
329    editToolBar->addAction(deleteAction);
330
331    sceneScaleCombo = new QComboBox;
332    QStringList scales;
333    scales << tr("75%") << tr("100%") << tr("125%") << tr("150%") << tr("175%");
334    sceneScaleCombo->addItems(scales);
335    sceneScaleCombo->setCurrentIndex(1);
336    connect(sceneScaleCombo, SIGNAL(currentIndexChanged(QString)),
337            this, SLOT(sceneScaleChanged(QString)));
338
339    editToolBar->addWidget(sceneScaleCombo);
340}
341
342bool MainWindow::newDiagram(QString filePath)
343{
344    saveIfNeeded();
345    scene->cleanScene();
346    myFilePath="";
347
348    if(filePath=="")
349        return 0;
350
351    myFilePath=filePath;
352    QFile file(myFilePath);
353    if(file.open(QIODevice::ReadOnly | QIODevice::Text))
354    {
355        QDomDocument document;
356        bool parsing=document.setContent(&file);
357        file.close();
358        if(!parsing)
359        {
360            QMessageBox::warning(this,"Aborting","Failed to parse file, "
361                                 "wrong format or encoding.");
362            return 0;
363        }
364        scene->fromXmlFormat(document);
365        return 1;
366    }
367    else
368        QMessageBox::critical(this,"Error","Could not open file for read.");
369
370    return 0;
371
372}
373
374void MainWindow::saveIfNeeded()
375{
376    if(myFilePath!="" || scene->items().count()>0)
377    {}//TODO save opened or modified diagram
378}
379
380bool MainWindow::openDiagram()
381{
382    QString
383    filePath = QFileDialog::getOpenFileName(this,"Open",
384               currentDir(),"Custom item for SIE code generator (*.die)");
385
386    if(filePath.isEmpty())
387        return 0;
388
389    if(!QFileInfo(filePath).isReadable())
390    {
391        QMessageBox::critical(this,"Error","File is not readable "
392                                           " or not exists.");
393        return 0;
394    }
395
396    newDiagram(filePath);
397    return 0;
398}
399
400bool MainWindow::saveDiagram()
401{
402    if(myFilePath=="")
403    {
404        saveAsDiagram();
405        return 0;
406    }
407    if(!QFileInfo(myFilePath).isWritable() && QFileInfo(myFilePath).exists())
408    {
409        QMessageBox::critical(this,"Error","File is not writable.");
410        return 0;
411    }
412    QFile file(myFilePath);
413    if(file.open(QIODevice::WriteOnly | QIODevice::Text))
414    {
415        QDomDocument document = scene->toXmlFormat();
416        QTextStream out(&file);
417        out.setCodec("UTF-8");
418        out << document.toString(4);
419        file.close();
420        return 1;
421    }
422    else
423        QMessageBox::critical(this,"Error","Could not open file for write.");
424    return 0;
425}
426
427bool MainWindow::saveAsDiagram()
428{
429    QString filePath = QFileDialog::getSaveFileName(this,"Save as...",
430                 currentDir(),"Custom item for SIE code generator (*.die)");
431
432    if(!filePath.isEmpty())
433    {
434        myFilePath = filePath;
435        saveDiagram();
436        return 1;
437    }
438    return 0;
439}
440

Archive Download this file

Branches:
master



interactive