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

Archive Download this file

Branches:
master



interactive