nn-usb-fpga/Software/sie_cg/mainwindow.cpp

930 lines
30 KiB
C++

/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial Usage
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtGui>
#include <QLabel>
#include "mainwindow.h"
const int InsertTextButton = 100000;
MainWindow::MainWindow()
{
buttonGroup=0;
toolBox=0;
createActions();
createMenus();
scene = new DiagramScene(itemMenu,this);
scene->setSceneRect(QRectF(0, 0, 5000, 5000));
connect(scene, SIGNAL(itemInserted(DiagramItem*)),
this, SLOT(itemInserted(DiagramItem*)));
connect(scene, SIGNAL(textInserted(QGraphicsTextItem*)),
this, SLOT(textInserted(QGraphicsTextItem*)));
connect(scene, SIGNAL(itemSelected(QGraphicsItem*)),
this, SLOT(itemSelected(QGraphicsItem*)));
createToolBox();
createToolbars();
setWindowTitle(tr("SIE Code Generator (Diagram Editor)"));
setUnifiedTitleAndToolBarOnMac(true);
myFilePath = "";
libDialog=0;
if(QApplication::argc()>1)
{newDiagram(QString(QApplication::argv()[1]));}
}
void MainWindow::backgroundButtonGroupClicked(QAbstractButton *button)
{
QList<QAbstractButton *> buttons = backgroundButtonGroup->buttons();
foreach (QAbstractButton *myButton, buttons) {
if (myButton != button)
button->setChecked(false);
}
QString text = button->text();
if (text == tr("Blue Grid"))
scene->setBackgroundBrush(QPixmap(":/images/background1.png"));
else if (text == tr("White Grid"))
scene->setBackgroundBrush(QPixmap(":/images/background2.png"));
else if (text == tr("Gray Grid"))
scene->setBackgroundBrush(QPixmap(":/images/background3.png"));
else
scene->setBackgroundBrush(QPixmap(":/images/background4.png"));
scene->update();
view->update();
}
void MainWindow::buttonGroupClicked(int id)
{
QList<QAbstractButton *> buttons = buttonGroup->buttons();
foreach (QAbstractButton *button, buttons) {
if (buttonGroup->button(id) != button)
button->setChecked(false);
}
if (id == InsertTextButton) {
scene->setMode(DiagramScene::InsertText);
} else {
scene->setItemType(buttonGroup->button(id)->text()); //Block name
scene->setMode(DiagramScene::InsertItem);
}
}
void MainWindow::deleteItem()
{
foreach (QGraphicsItem *item, scene->selectedItems()) {
if (item->type() == DiagramItem::Type) {
qgraphicsitem_cast<DiagramItem *>(item)->removeArrows();
qgraphicsitem_cast<DiagramItem *>(item)->removeTextItems();
scene->removeItem(item);
delete(item);
}
//If arrow is deleted then is romoved from diagram owner
else if (item->type() == Arrow::Type) {
qgraphicsitem_cast<Arrow *>(item)->removeLines();
qgraphicsitem_cast<Arrow *>(item)->startItem()->ownerItem()
->removeArrow(qgraphicsitem_cast<Arrow *>(item));
qgraphicsitem_cast<Arrow *>(item)->endItem()->ownerItem()
->removeArrow(qgraphicsitem_cast<Arrow *>(item));
scene->removeItem(item);
delete(item);
}
//If line is deleted then is romoved from the arrow owner
else if (item->type() == lineItem::Type &&
!qgraphicsitem_cast<lineItem *>(item)->itemIsMovable()) {
qgraphicsitem_cast<lineItem *>(item)->myOwner()->removeLine(
qgraphicsitem_cast<lineItem *>(item));
qgraphicsitem_cast<lineItem *>(item)->myOwner()->updatePosition();
scene->removeItem(item);
delete(item);
}
}
}
void MainWindow::pointerGroupClicked(int)
{
scene->setMode(DiagramScene::Mode(pointerTypeGroup->checkedId()));
}
void MainWindow::bringToFront()
{
sendTo(1);
}
void MainWindow::sendToBack()
{
sendTo(0);
}
void MainWindow::sendTo(bool value)
{
if (scene->selectedItems().isEmpty())
return;
QGraphicsItem *selectedItem = scene->selectedItems().first();
QList<QGraphicsItem *> overlapItems = selectedItem->collidingItems();
qreal zValue = 0;
foreach (QGraphicsItem *item, overlapItems) {
if (item->zValue() <= zValue &&
item->type() == DiagramItem::Type)
{
if(value)
zValue = item->zValue() + 0.1;
else
zValue = item->zValue() - 0.1;
}
}
selectedItem->setZValue(zValue);
if(selectedItem->type() == DiagramItem::Type)
foreach (DiagramTextItem *texts,
qgraphicsitem_cast<DiagramItem *>(selectedItem)->getTextItems())
{
texts->updatePosition();
}
}
void MainWindow::itemInserted(DiagramItem *item)
{
pointerTypeGroup->button(int(DiagramScene::MoveItem))->setChecked(true);
scene->setMode(DiagramScene::Mode(pointerTypeGroup->checkedId()));
buttonGroup->button(buttonIdByName.value(item->diagramType()))->setChecked(false);
}
void MainWindow::textInserted(QGraphicsTextItem *)
{
buttonGroup->button(InsertTextButton)->setChecked(false);
scene->setMode(DiagramScene::Mode(pointerTypeGroup->checkedId()));
}
void MainWindow::currentFontChanged(const QFont &)
{
handleFontChange();
}
void MainWindow::fontSizeChanged(const QString &)
{
handleFontChange();
}
void MainWindow::sceneScaleChanged(const QString &scale)
{
double newScale = scale.left(scale.indexOf(tr("%"))).toDouble() / 100.0;
QMatrix oldMatrix = view->matrix();
view->resetMatrix();
view->translate(oldMatrix.dx(), oldMatrix.dy());
view->scale(newScale, newScale);
}
void MainWindow::textColorChanged()
{
textAction = qobject_cast<QAction *>(sender());
fontColorToolButton->setIcon(createColorToolButtonIcon(
":/images/textpointer.png",
qVariantValue<QColor>(textAction->data())));
textButtonTriggered();
}
void MainWindow::itemColorChanged()
{
fillAction = qobject_cast<QAction *>(sender());
fillColorToolButton->setIcon(createColorToolButtonIcon(
":/images/floodfill.png",
qVariantValue<QColor>(fillAction->data())));
fillButtonTriggered();
}
void MainWindow::lineColorChanged()
{
lineAction = qobject_cast<QAction *>(sender());
lineColorToolButton->setIcon(createColorToolButtonIcon(
":/images/linecolor.png",
qVariantValue<QColor>(lineAction->data())));
lineButtonTriggered();
}
void MainWindow::textButtonTriggered()
{
scene->setTextColor(qVariantValue<QColor>(textAction->data()));
}
void MainWindow::fillButtonTriggered()
{
scene->setItemColor(qVariantValue<QColor>(fillAction->data()));
}
void MainWindow::lineButtonTriggered()
{
scene->setLineColor(qVariantValue<QColor>(lineAction->data()));
}
void MainWindow::handleFontChange()
{
QFont font = fontCombo->currentFont();
font.setPointSize(fontSizeCombo->currentText().toInt());
font.setWeight(boldAction->isChecked() ? QFont::Bold : QFont::Normal);
font.setItalic(italicAction->isChecked());
font.setUnderline(underlineAction->isChecked());
scene->setFont(font);
}
void MainWindow::itemSelected(QGraphicsItem *item)
{
DiagramTextItem *textItem =
qgraphicsitem_cast<DiagramTextItem *>(item);
QFont font = textItem->font();
QColor color = textItem->defaultTextColor();
fontCombo->setCurrentFont(font);
fontSizeCombo->setEditText(QString().setNum(font.pointSize()));
boldAction->setChecked(font.weight() == QFont::Bold);
italicAction->setChecked(font.italic());
underlineAction->setChecked(font.underline());
}
void MainWindow::about()
{
QMessageBox::question(this, tr("About SIE Code Generator"),
tr("TODO <b>:)</b>"));
}
void MainWindow::addLibrariesButtons(QGridLayout *layout)
{
int i=1;
domElementsByName.clear(); buttonIdByName.clear();
QDomDocument customItems;
foreach(QString filepath, libraryList)
{
customItems = parseDocument(filepath);
for (QDomNode node = customItems.firstChild() ;
!node.isNull() ;
node = node.nextSibling())
{
QDomElement *customItem = new QDomElement(node.toElement());
if(customItem->tagName()=="CustomItem")
{
if(domElementsByName.contains(
customItem->attribute("BlockName")))
{
QMessageBox::warning(this,"Block name in use.",
"The library " + filepath +
" has one block called" +
customItem->attribute("BlockName") +
" that is already in use. Only the "
" first Block loaded can be used, this"
" library will be ignored.");
}
else
{
domElementsByName.insert(customItem->attribute("BlockName"),
customItem);
buttonIdByName.insert(customItem->attribute("BlockName"),i-1);
scene->setDomElementsByName(domElementsByName);
scene->setButtonIdByName(buttonIdByName);
layout->addWidget(createCellWidget(
customItem->attribute("BlockName"),
domElementsByName.
value(customItem->attribute("BlockName"))),i/2,i&1);
i++;
}
}
}
}
}
void MainWindow::createToolBox()
{
if(buttonGroup!=0) delete(buttonGroup);
if(toolBox!=0) delete(toolBox);
buttonGroup = new QButtonGroup;
buttonGroup->setExclusive(false);
connect(buttonGroup, SIGNAL(buttonClicked(int)),
this, SLOT(buttonGroupClicked(int)));
QGridLayout *layout = new QGridLayout;
addLibrariesButtons(layout);
QToolButton *textButton = new QToolButton;
textButton->setCheckable(true);
buttonGroup->addButton(textButton, InsertTextButton);
textButton->setIcon(QIcon(QPixmap(":/images/textpointer.png")
.scaled(30, 30)));
textButton->setIconSize(QSize(50, 50));
QGridLayout *textLayout = new QGridLayout;
textLayout->addWidget(textButton, 0, 0, Qt::AlignHCenter);
textLayout->addWidget(new QLabel(tr("Text")), 1, 0, Qt::AlignCenter);
QWidget *textWidget = new QWidget;
textWidget->setLayout(textLayout);
layout->addWidget(textWidget, 0, 0);
layout->setRowStretch(3, 10);
layout->setColumnStretch(2, 10);
QWidget *itemWidget = new QWidget;
itemWidget->setLayout(layout);
toolBox = new QToolBox;
toolBox->setSizePolicy(QSizePolicy(QSizePolicy::Maximum, QSizePolicy::Ignored));
toolBox->setMinimumWidth(itemWidget->sizeHint().width());
toolBox->addItem(itemWidget, tr("Basic blocks"));
//toolBox->addItem(backgroundWidget, tr("Backgrounds"));
//Add tool box to window
QHBoxLayout *newLayout = new QHBoxLayout;
newLayout->addWidget(toolBox);
view = new QGraphicsView(scene);
newLayout->addWidget(view);
QWidget *widget = new QWidget;
widget->setLayout(newLayout);
setCentralWidget(widget);
}
void MainWindow::createActions()
{
toFrontAction = new QAction(QIcon(":/images/bringtofront.png"),
tr("Bring to &Front"), this);
toFrontAction->setShortcut(tr("Ctrl+F"));
toFrontAction->setStatusTip(tr("Bring item to front"));
connect(toFrontAction, SIGNAL(triggered()),
this, SLOT(bringToFront()));
sendBackAction = new QAction(QIcon(":/images/sendtoback.png"),
tr("Send to &Back"), this);
sendBackAction->setShortcut(tr("Ctrl+B"));
sendBackAction->setStatusTip(tr("Send item to back"));
connect(sendBackAction, SIGNAL(triggered()),
this, SLOT(sendToBack()));
deleteAction = new QAction(QIcon(":/images/delete.png"),
tr("&Delete"), this);
deleteAction->setShortcut(tr("Ctrl+Delete"));
deleteAction->setStatusTip(tr("Delete item from diagram"));
connect(deleteAction, SIGNAL(triggered()),
this, SLOT(deleteItem()));
newAction = new QAction(QIcon(":/images/new.png"),tr("&New"),this);
newAction->setShortcuts(QKeySequence::New);
newAction->setStatusTip("New diagram");
connect(newAction,SIGNAL(triggered()),this,SLOT(newDiagram()));
saveAction = new QAction(QIcon(":/images/save.png"),tr("&Save"),this);
saveAction->setShortcuts(QKeySequence::Save);
saveAction->setStatusTip("Save current diagram");
connect(saveAction,SIGNAL(triggered()),this,SLOT(saveDiagram()));
saveAsAction = new QAction(QIcon(":/images/save_as.png"),
tr("Save &As..."),this);
saveAsAction->setShortcuts(QKeySequence::SaveAs);
saveAsAction->setStatusTip("Save current diagram with another name");
connect(saveAsAction,SIGNAL(triggered()),this,SLOT(saveAsDiagram()));
openAction = new QAction(QIcon(":/images/open.png"),tr("&Open"),this);
openAction->setShortcuts(QKeySequence::Open);
openAction->setStatusTip("Open diagram");
connect(openAction,SIGNAL(triggered()),this,SLOT(openDiagram()));
exportImgAction = new QAction(tr("&Export to ..."),this);
exportImgAction->setStatusTip("Export current diagram to picture");
connect(exportImgAction,SIGNAL(triggered()),this,SLOT(exportDiagram()));
exitAction = new QAction(tr("E&xit"), this);
exitAction->setShortcuts(QKeySequence::Quit);
exitAction->setStatusTip(tr("Quit diagram editor"));
connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
boldAction = new QAction(QIcon(":/images/bold.png"),tr("Bold"), this);
boldAction->setCheckable(true);
boldAction->setShortcut(tr("Ctrl+B"));
connect(boldAction, SIGNAL(triggered()),
this, SLOT(handleFontChange()));
italicAction = new QAction(QIcon(":/images/italic.png"),
tr("Italic"), this);
italicAction->setCheckable(true);
italicAction->setShortcut(tr("Ctrl+I"));
connect(italicAction, SIGNAL(triggered()),
this, SLOT(handleFontChange()));
underlineAction = new QAction(QIcon(":/images/underline.png"),
tr("Underline"), this);
underlineAction->setCheckable(true);
underlineAction->setShortcut(tr("Ctrl+U"));
connect(underlineAction, SIGNAL(triggered()),
this, SLOT(handleFontChange()));
libraryAction = new QAction(tr("&Library..."),this);
connect(libraryAction, SIGNAL(triggered()),
this, SLOT(libraryForm()));
optionsAction = new QAction(tr("&Options"),this);
optionsAction->setShortcuts(QKeySequence::Preferences);
connect(optionsAction, SIGNAL(triggered()),
this, SLOT(optionsForm()));
aboutAction = new QAction(tr("A&bout"), this);
aboutAction->setShortcut(tr("Ctrl+B"));
connect(aboutAction, SIGNAL(triggered()),
this, SLOT(about()));
}
void MainWindow::createMenus()
{
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(newAction);
fileMenu->addAction(openAction);
fileMenu->addSeparator();
fileMenu->addAction(saveAction);
fileMenu->addAction(saveAsAction);
fileMenu->addAction(exportImgAction);
fileMenu->addSeparator();
fileMenu->addAction(exitAction);
itemMenu = menuBar()->addMenu(tr("&Item"));
itemMenu->addAction(deleteAction);
itemMenu->addSeparator();
itemMenu->addAction(toFrontAction);
itemMenu->addAction(sendBackAction);
preferencesMenu= menuBar()->addMenu(tr("&Preferences"));
preferencesMenu->addAction(libraryAction);
preferencesMenu->addAction(optionsAction);
aboutMenu = menuBar()->addMenu(tr("&Help"));
aboutMenu->addAction(aboutAction);
}
void MainWindow::createToolbars()
{
fileToolBar = addToolBar(tr("File"));
fileToolBar->addAction(newAction);
fileToolBar->addAction(openAction);
fileToolBar->addAction(saveAction);
editToolBar = addToolBar(tr("Edit"));
editToolBar->addAction(deleteAction);
editToolBar->addAction(toFrontAction);
editToolBar->addAction(sendBackAction);
fontCombo = new QFontComboBox();
fontSizeCombo = new QComboBox();
connect(fontCombo, SIGNAL(currentFontChanged(QFont)),
this, SLOT(currentFontChanged(QFont)));
fontSizeCombo = new QComboBox;
fontSizeCombo->setEditable(true);
for (int i = 8; i < 30; i = i + 2)
fontSizeCombo->addItem(QString().setNum(i));
QIntValidator *validator = new QIntValidator(2, 64, this);
fontSizeCombo->setValidator(validator);
connect(fontSizeCombo, SIGNAL(currentIndexChanged(QString)),
this, SLOT(fontSizeChanged(QString)));
fontColorToolButton = new QToolButton;
fontColorToolButton->setPopupMode(QToolButton::MenuButtonPopup);
fontColorToolButton->setMenu(createColorMenu(SLOT(textColorChanged()),
Qt::black));
textAction = fontColorToolButton->menu()->defaultAction();
fontColorToolButton->setIcon(createColorToolButtonIcon(
":/images/textpointer.png", Qt::black));
fontColorToolButton->setAutoFillBackground(true);
connect(fontColorToolButton, SIGNAL(clicked()),
this, SLOT(textButtonTriggered()));
fillColorToolButton = new QToolButton;
fillColorToolButton->setPopupMode(QToolButton::MenuButtonPopup);
fillColorToolButton->setMenu(createColorMenu(SLOT(itemColorChanged()),
Qt::white));
fillAction = fillColorToolButton->menu()->defaultAction();
fillColorToolButton->setIcon(createColorToolButtonIcon(
":/images/floodfill.png", Qt::white));
connect(fillColorToolButton, SIGNAL(clicked()),
this, SLOT(fillButtonTriggered()));
lineColorToolButton = new QToolButton;
lineColorToolButton->setPopupMode(QToolButton::MenuButtonPopup);
lineColorToolButton->setMenu(createColorMenu(SLOT(lineColorChanged()),
Qt::black));
lineAction = lineColorToolButton->menu()->defaultAction();
lineColorToolButton->setIcon(createColorToolButtonIcon(
":/images/linecolor.png", Qt::black));
connect(lineColorToolButton, SIGNAL(clicked()),
this, SLOT(lineButtonTriggered()));
textToolBar = addToolBar(tr("Font"));
textToolBar->addWidget(fontCombo);
textToolBar->addWidget(fontSizeCombo);
textToolBar->addAction(boldAction);
textToolBar->addAction(italicAction);
textToolBar->addAction(underlineAction);
colorToolBar = addToolBar(tr("Color"));
colorToolBar->addWidget(fontColorToolButton);
colorToolBar->addWidget(fillColorToolButton);
colorToolBar->addWidget(lineColorToolButton);
QToolButton *pointerButton = new QToolButton;
pointerButton->setCheckable(true);
pointerButton->setChecked(true);
pointerButton->setIcon(QIcon(":/images/pointer.png"));
QToolButton *linePointerButton = new QToolButton;
linePointerButton->setCheckable(true);
linePointerButton->setIcon(QIcon(":/images/linepointer.png"));
pointerTypeGroup = new QButtonGroup;
pointerTypeGroup->addButton(pointerButton, int(DiagramScene::MoveItem));
pointerTypeGroup->addButton(linePointerButton,
int(DiagramScene::InsertLine));
connect(pointerTypeGroup, SIGNAL(buttonClicked(int)),
this, SLOT(pointerGroupClicked(int)));
sceneScaleCombo = new QComboBox;
QStringList scales;
scales << tr("50%") << tr("75%") << tr("100%") << tr("125%") << tr("150%");
sceneScaleCombo->addItems(scales);
sceneScaleCombo->setCurrentIndex(2);
connect(sceneScaleCombo, SIGNAL(currentIndexChanged(QString)),
this, SLOT(sceneScaleChanged(QString)));
pointerToolbar = addToolBar(tr("Pointer type"));
pointerToolbar->addWidget(pointerButton);
pointerToolbar->addWidget(linePointerButton);
pointerToolbar->addWidget(sceneScaleCombo);
}
QWidget *MainWindow::createCellWidget(QString text, QDomElement *customItem)
{
DiagramItem item(itemMenu,text,customItem);
QIcon icon(item.image());
QToolButton *button = new QToolButton;
button->setText(text);
button->setIcon(icon);
button->setIconSize(QSize(50, 50));
button->setCheckable(true);
buttonGroup->addButton(button, buttonIdByName.value(text));
QGridLayout *layout = new QGridLayout;
layout->addWidget(button, 0, 0, Qt::AlignHCenter);
layout->addWidget(new QLabel(text), 1, 0, Qt::AlignCenter);
QWidget *widget = new QWidget;
widget->setLayout(layout);
return widget;
}
QMenu *MainWindow::createColorMenu(const char *slot, QColor defaultColor)
{
QList<QColor> colors;
colors << Qt::black << Qt::white << Qt::magenta
<< Qt::cyan << Qt::red << Qt::blue << Qt::yellow << Qt::green
<< Qt::darkMagenta << Qt::darkCyan << Qt::darkRed << Qt::darkBlue
<< Qt::darkYellow << Qt::darkGreen << Qt::darkGray;
QStringList names;
names << tr("Black") << tr("White") << tr("Magenta") << tr("Cyan")
<< tr("Red") << tr("Blue") << tr("Yellow") << tr("Green")
<< tr("Dark Magenta") << tr("Dark Cyan") << tr("Dark Red")
<< tr("Dark Blue") << tr("Dark Yellow") << tr("Dark Green")
<< tr("Dark Gray") ;
QMenu *colorMenu = new QMenu;
for (int i = 0; i < colors.count(); ++i) {
QAction *action = new QAction(names.at(i), this);
action->setData(colors.at(i));
action->setIcon(createColorIcon(colors.at(i)));
connect(action, SIGNAL(triggered()),
this, slot);
colorMenu->addAction(action);
if (colors.at(i) == defaultColor) {
colorMenu->setDefaultAction(action);
}
}
return colorMenu;
}
QIcon MainWindow::createColorToolButtonIcon(const QString &imageFile,
QColor color)
{
QPixmap pixmap(50, 80);
pixmap.fill(Qt::transparent);
QPainter painter(&pixmap);
QPixmap image(imageFile);
QRect target(0, 0, 50, 60);
QRect source(0, 0, 42, 42);
painter.fillRect(QRect(0, 60, 50, 80), color);
painter.drawPixmap(target, image, source);
return QIcon(pixmap);
}
QIcon MainWindow::createColorIcon(QColor color)
{
QPixmap pixmap(20, 20);
QPainter painter(&pixmap);
painter.setPen(Qt::NoPen);
painter.fillRect(QRect(0, 0, 20, 20), color);
return QIcon(pixmap);
}
int MainWindow::newDiagram(QString filePath)
{
saveIfNeeded();
scene->cleanScene();
libraryList.clear(); //or set defaults
domElementsByName.clear(); //or set defaults
buttonIdByName.clear();//or set defaults
scene->setLibList(libraryList);
updateLibraries();
myFilePath="";
if(filePath=="")
return 0;
myFilePath=filePath;
if(!scene->fromXmlFormat(parseDocument(myFilePath)))
newDiagram();
return 1;
}
QDomDocument MainWindow::parseDocument(QString filePath)
{
QDomDocument document;
QFile file(filePath);
if(file.open(QIODevice::ReadOnly | QIODevice::Text))
{
bool parsing=document.setContent(&file);
file.close();
if(!parsing)
{
QMessageBox::warning(this,"Operation failed","Failed to parse file, "
"wrong format or encoding.");
}
}
else
QMessageBox::critical(this,"Error","Could not open file for read.");
return document;
}
int MainWindow::saveIfNeeded()
{
if(myFilePath!="" || scene->items().count()>0)
{}//TODO save opened or modified diagram
return 0;
}
int MainWindow::openDiagram()
{
QString
filePath = QFileDialog::getOpenFileName(this,"Open",
currentDir(),"Diagram for SIE code generator (*.sie)");
if(filePath.isEmpty())
return 0;
if(!QFileInfo(filePath).isReadable())
{
QMessageBox::critical(this,"Error","File is not readable "
" or not exists.");
return 0;
}
newDiagram(filePath);
return 0;
}
int MainWindow::saveDiagram()
{
if(myFilePath=="")
{
saveAsDiagram();
return 0;
}
if(!QFileInfo(myFilePath).isWritable() && QFileInfo(myFilePath).exists())
{
QMessageBox::critical(this,"Error","File is not writable.");
return 0;
}
QFile file(myFilePath);
if(file.open(QIODevice::WriteOnly | QIODevice::Text))
{
scene->setLibList(libraryList);
QDomDocument document = scene->toXmlFormat();
QTextStream out(&file);
out.setCodec("UTF-8");
out << document.toString(4);
file.close();
return 1;
}
else
QMessageBox::critical(this,"Error","Could not open file for write.");
return 0;
}
int MainWindow::saveAsDiagram()
{
QString filePath = QFileDialog::getSaveFileName(this,"Save as...",
currentDir(),"Diagram for SIE code generator (*.sie)");
if(!filePath.isEmpty())
{
myFilePath = filePath;
saveDiagram();
return 1;
}
return 0;
}
int MainWindow::exportDiagram()
{
QString picturePath = QFileDialog::getSaveFileName(this,"Export",
currentDir(),"Portable Network Graphics (*.png)");
if(picturePath.isEmpty()) return 0;
if(!QFileInfo(picturePath).isWritable() && QFileInfo(picturePath).exists())
{
QMessageBox::critical(this,"Error","File is not writable.");
return 0;
}
scene->setDawGrid(0);
QSize sizeScene= scene->sceneRect().size().toSize();
QImage img(sizeScene,QImage::Format_ARGB32_Premultiplied);
QPainter p(&img);
scene->render(&p,
scene->sceneRect(),
scene->itemsBoundingRect(),
Qt::KeepAspectRatio);
p.end();
img.save(picturePath, "png");
scene->setDawGrid(1); //TODO : set correct value
return 1;
}
int MainWindow::libraryForm()
{
if(!libDialog)
{
createLibraryDialog();
}
libDialog->setModal(1);
libUi->listLib->clear();
libUi->listLib->addItems(libraryList);
libDialog->setWindowTitle(tr("Library paths..."));
QStringList oldLibraryList=libraryList;
libDialog->exec();
if (libDialog->result() == QDialog::Rejected)
libraryList = oldLibraryList;
else
{
scene->setLibList(libraryList);
updateLibraries();
}
return 0;
}
void MainWindow::createLibraryDialog()
{
libDialog = new QDialog(this);
libUi = new Ui_libraryDialog;
libUi->setupUi(libDialog);
connect(libUi->addLib,SIGNAL(clicked()),this,SLOT(addLibPath()));
connect(libUi->delLib,SIGNAL(clicked()),this,SLOT(delLibPath()));
connect(libUi->editLib,SIGNAL(clicked()),this,SLOT(editLibPath()));
}
void MainWindow::addLibPath()
{
QString
filePath = QFileDialog::getOpenFileName(this,"Open",
currentDir(),"Custom block for SIE code generator (*.die)");
if(filePath.isEmpty())
return;
if(!QFileInfo(filePath).isReadable())
{
QMessageBox::critical(this,"Error","File is not readable "
" or not exists.");
return;
}
if(libUi->listLib->findItems(filePath,Qt::MatchExactly).count()==0)
{
QDir myCurrentDir = this->currentDir();
QString relativePath=myCurrentDir.relativeFilePath(filePath);
libraryList.append(relativePath);
libUi->listLib->addItem(relativePath);
}
else
QMessageBox::information(this,"Information","Library already exist.");
}
void MainWindow::delLibPath()
{
QList<QListWidgetItem *> selected = libUi->listLib->selectedItems();
if(selected.count()>0)
{
libraryList.removeOne(selected.at(0)->text());
libUi->listLib->clear();
libUi->listLib->addItems(libraryList);
}
}
void MainWindow::editLibPath()
{
QList<QListWidgetItem *> selected = libUi->listLib->selectedItems();
if(selected.count()>0)
{
callexternapp("block_editor/blockeditor",selected.at(0)->text());
}
}
int MainWindow::updateLibraries()
{
libraryList=scene->getLibList();
createToolBox();
return 0;
}
int MainWindow::optionsForm()
{
return 0;
}
QString MainWindow::callexternapp(QString xexecFile, QString xexecParm)
{
QEventLoop cxaw;
callexternappT cxat;
connect(&cxat, SIGNAL(finished()), &cxaw, SLOT(quit()));
cxat.execFile = xexecFile;
cxat.execParm = xexecParm;
cxat.start();
cxaw.exec();
return cxat.retnValu;
}