[[qshape]]
 
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
 
#include <QMainWindow>
#include <QGraphicsScene>
#include <QAbstractGraphicsShapeItem>
 
namespace Ui {
class MainWindow;
}
 
class ColorButton : public QToolButton
{
private :
    QString name;
    QColor color;
public :
    ColorButton (QString s);
};
 
 
 
typedef QAbstractGraphicsShapeItem ShapeItem;
 
class ParamList : public QMap <QString, QString>
{
   public:
      int number (QString key, int default_value = 0);
};
 
class MainWindow : public QMainWindow
{
    Q_OBJECT
 
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
 
    void readFile(QString fileName);
 
    ShapeItem * createRectangle (QGraphicsItem * target,
                               ParamList param);
 
    ShapeItem * createEllipse (QGraphicsItem * target,
                               ParamList param);
 
    void modifyShape (QGraphicsItem * target,
                      ShapeItem * shape,
                      ParamList param);
 
private:
    Ui::MainWindow * ui;
    QGraphicsScene * scene;
};
 
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
 
#include <QFile>
#include <QXmlStreamReader>
#include <QXmlStreamAttribute>
 
#include <QGraphicsRectItem>
#include <QGraphicsEllipseItem>
 
int ParamList::number(QString key, int default_value)
{
    QString s = this->value (key);
    bool ok;
    int n = s.toInt (&ok);
    if (!ok)
        n = default_value;
    return n;
}
 
ColorButton::ColorButton(QString s)
  : name (s), color (QColor (s))
{
    setText (s);
}
 
 
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
 
    QList <QString> names;
    names << "red" << "blue" << "green" << "yellow";
 
    for (auto name : names)
    {
       ColorButton * b = new ColorButton (name);
       ui->mainToolBar->addWidget (b);
    }
 
 
    scene = new QGraphicsScene;
    ui->graphicsView->setScene (scene);
 
    scene->setSceneRect (0, 0, 1024, 800);
 
    readFile ("../xml/data.xml");
}
 
MainWindow::~MainWindow()
{
    delete ui;
}
 
void MainWindow::readFile (QString fileName)
{
    QFile file (fileName);
    if (file.open (QFile::ReadOnly | QFile::Text ))
    {
        QTreeWidgetItem * branch =
                ui->treeWidget->invisibleRootItem();
        QGraphicsItem * target = NULL;
 
        QXmlStreamReader reader (&file);
        while (! reader.atEnd())
        {
            if (reader.isStartElement())
            {
                QString s = reader.name().toString();
                QTreeWidgetItem * item = new QTreeWidgetItem;
                item->setText (0, s);
                item->setForeground (0, QColor ("blue"));
                branch->addChild (item);
                branch = item;
 
                ParamList list;
                // for (QXmlStreamAttribute a : reader.attributes())
                for (auto a : reader.attributes())
                {
                   auto node = new QTreeWidgetItem;
                   QString t = a.name().toString();
                   QString v = a.value().toString();
                   node->setText (0, t + " = " + v);
                   node->setForeground (0, QColor ("green"));
                   item->addChild (node);
 
                   // list.insert(t, v);
                   list [t] = v;
                }
 
                if (s == "rectangle")
                   target = createRectangle (target, list);
                else if (s == "ellipse")
                   target = createEllipse (target, list);
            }
            else if (reader.isCharacters())
            {
                QString s = reader.text().toString().simplified();
                if (s != "")
                {
                   QTreeWidgetItem * item = new QTreeWidgetItem;
                   item->setText (0, s);
                   item->setForeground (0, QColor ("orange"));
                   branch->addChild (item);
                }
            }
            else if (reader.isEndElement())
            {
                branch = branch->parent();
 
                QString s = reader.name().toString();
                if (s == "rectangle" || s == "ellipse")
                   target = target->parentItem ();
            }
            reader.readNext();
        }
        ui->treeWidget->expandAll();
    }
}
 
ShapeItem * MainWindow::createRectangle
                        (QGraphicsItem * target,
                         ParamList list)
{
    QGraphicsRectItem * shape = new QGraphicsRectItem;
 
    int w = list.number ("w", 100);
    int h = list.number ("h", 40);
 
    shape->setRect (0, 0, w, h);
 
    modifyShape (target, shape, list);
 
    return shape;
}
 
ShapeItem * MainWindow::createEllipse
                        (QGraphicsItem * target,
                         ParamList list)
{
    QGraphicsEllipseItem * shape = new QGraphicsEllipseItem;
 
    int w = list.number ("w", 10);
    int h = list.number ("h", 10);
 
    shape->setRect (0, 0, w, h);
 
    modifyShape (target, shape, list);
 
    return shape;
}
 
void MainWindow::modifyShape (QGraphicsItem * target,
                              ShapeItem * shape,
                              ParamList list)
{
    int x = list.number ("x", 20);
    int y = list.number ("y", 20);
    shape->setPos (x, y);
 
    QString pen = list ["pen"];
    if (pen != "")
       shape->setPen (QColor (pen));
 
    QString brush = list ["brush"];
    if (brush != "")
       shape->setBrush (QColor (brush));
 
    shape->setFlag (QGraphicsItem::ItemIsMovable);
 
    if (target == NULL)
       scene->addItem (shape);
    else
       shape->setParentItem (target);
}
<data>
   <rectangle x="10" y="10" w="80" h="40" pen="red" brush="yellow">
 
      <ellipse x="10" y="10" w="10" h="10" pen="red" brush="blue" />
      <ellipse x="70" y="10" w="10" h="10" pen="red" brush="orange" />
 
   </rectangle>
 
</data>
 
qshape.txt · Last modified: 2017/04/03 16:45 by 147.32.8.115
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki