#include "graphics.h" #include #include #include #include #include #include #include #include /* shapes */ QGraphicsItem * ellipse () { QGraphicsEllipseItem * item = new QGraphicsEllipseItem; item->setRect (0, 0, 100, 50); item->setPen (QColor (Qt::blue)); item->setBrush (QBrush (Qt::yellow)); item->setFlag (QGraphicsItem::ItemIsMovable); item->setFlag (QGraphicsItem::ItemIsSelectable); item->setToolTip ("ellipse"); return item; }; QGraphicsItem * triangle () { QGraphicsPolygonItem * item = new QGraphicsPolygonItem; QPolygonF polygon; polygon << QPointF (25, 0); polygon << QPointF (0, 50); polygon << QPointF (50, 50); item->setPolygon (polygon); item->setPen (QColor (Qt::red)); item->setBrush (QBrush (Qt::green)); item->setFlag (QGraphicsItem::ItemIsMovable); item->setFlag (QGraphicsItem::ItemIsSelectable); item->setToolTip ("triangle"); return item; }; QGraphicsItem * wave () { QGraphicsPathItem * item = new QGraphicsPathItem; QPainterPath path; path.cubicTo (QPointF (50, 0), QPointF (0, 50), QPointF (50, 50)); item->setPath (path); item->setPen (QColor ("brown")); QLinearGradient brush (QPointF (0, 0), QPointF (50, 50)); brush.setColorAt (0, "yellow"); brush.setColorAt (1, "orange"); item->setBrush (brush); item->setFlag (QGraphicsItem::ItemIsMovable); item->setFlag (QGraphicsItem::ItemIsSelectable); item->setToolTip ("wave"); return item; }; /* MyArea */ Area::Area (QGraphicsItem * parent) : QGraphicsRectItem (parent) { } void Area::dragEnterEvent (QGraphicsSceneDragDropEvent * event) { const QMimeData * mime = event->mimeData (); if (mime->hasText() || mime->hasColor() || mime->hasFormat ("application/x-shape")) event->setAccepted (true); } void Area::dropEvent (QGraphicsSceneDragDropEvent * event) { const QMimeData * mime = event->mimeData (); if (mime->hasText()) { QPointF p = event->pos (); QGraphicsTextItem * item = new QGraphicsTextItem; item->setPlainText (mime->text()); item->setPos (p); item->setDefaultTextColor (QColor ("red")); item->setFlags (QGraphicsItem::ItemIsSelectable); item->setFlags (QGraphicsItem::ItemIsMovable); item->setToolTip ("text"); item->setParentItem (this); } else if (mime->hasColor()) { QColor color = mime->colorData().value (); if (event->dropAction() == Qt::CopyAction) this->setPen (color); else this->setBrush (color); } else if ( mime->hasFormat ("application/x-shape")) { QString name = mime->data ("application/x-shape"); if (name == "area") { Area * a = new Area (); QPointF p = event->pos (); a->setRect (p.x(), p.y(), 80, 40); a->setPen (QColor ("red")); a->setBrush (QColor ("cornflowerblue")); a->setFlags (QGraphicsItem::ItemIsSelectable); a->setFlags (QGraphicsItem::ItemIsMovable); a->setAcceptDrops (true); a->setParentItem (this); a->setToolTip ("area"); } else { QGraphicsItem * item = NULL; if (name == "ellipse") item = ellipse (); else if (name == "triangle") item = triangle (); else if (name == "wave") item = wave (); if (item != NULL) { QPointF p = event->pos (); item->setPos (p); item->setParentItem (this); } } } } /* ColorButton */ ColorButton::ColorButton (QWidget *parent) : QToolButton (parent) { } void ColorButton::setColor (QColor p_color) { color = p_color; /* QPalette p = palette (); p.setColor (QPalette::ButtonText, color); setPalette (p); */ QPixmap pixmap (24, 24); pixmap.fill (Qt::transparent); QPainter painter (&pixmap); painter.setPen (Qt::NoPen); painter.setBrush (QBrush (color)); painter.drawEllipse (0, 0, 24, 24); painter.end (); setIcon (pixmap); } void ColorButton::mousePressEvent (QMouseEvent * event) { QDrag * drag = new QDrag (this); QMimeData * mime = new QMimeData; mime->setColorData (color); drag->setMimeData (mime); drag->exec (Qt::CopyAction | Qt::MoveAction); } /* ShapeButton */ ShapeButton::ShapeButton (QWidget *parent) : QToolButton (parent) { } void ShapeButton::mousePressEvent (QMouseEvent * event) { QDrag * drag = new QDrag (this); QMimeData * mime = new QMimeData; QByteArray bytes = name.toLatin1 (); mime->setData ("application/x-shape", bytes); drag->setMimeData (mime); drag->exec (Qt::CopyAction | Qt::MoveAction); } /* MainWindow */ void MainWindow::addColor (QString name) { ColorButton * button = new ColorButton; button->setToolTip (name); button->setColor (QColor (name)); button->move (24*colorCount+4, 4); colorCount ++; button->setParent (colorTab); } void MainWindow::addShape (QString name) { ShapeButton * button = new ShapeButton; button->setText (name); button->name = name; button->move (80*shapeCount+4, 8); shapeCount ++; button->setParent (shapeTab); } void MainWindow::addColors () { addColor ("red"); addColor ("blue"); addColor ("green"); addColor ("yellow"); addColor ("orange"); addColor ("silver"); addColor ("gold"); addColor ("goldenrod"); addColor ("lime"); addColor ("lime green"); addColor ("yellow green"); addColor ("green yellow"); addColor ("forest green"); addColor ("coral"); addColor ("cornflower blue"); addColor ("dodger blue"); addColor ("royal blue"); addColor ("wheat"); addColor ("chocolate"); addColor ("peru"); addColor ("sienna"); addColor ("brown"); } QBrush squareBrush () { const int n = 16; QBitmap texture (n, n); texture.clear (); QPainter painter (& texture); painter.drawLine (0, 0, n-1, 0); painter.drawLine (0, 0, 0, n-1); QBrush brush (QColor ("cornflowerblue")); brush.setTexture (texture); return brush; } MainWindow::MainWindow (QWidget * parent) : QMainWindow(parent), scene (NULL), top (NULL), colorCount (0), shapeCount (1) { setupUi (this); scene = new QGraphicsScene (); graphicsView->setScene (scene); graphicsView->setAcceptDrops (true); addColors (); addShape ("area"); addShape ("ellipse"); addShape ("triangle"); addShape ("wave"); Area * a = new Area (); a->setRect (0, 0, 800, 640); a->setPen (QColor ("blue")); // a->setBrush (Qt::NoBrush); a->setBrush (squareBrush ()); a->setAcceptDrops (true); scene->addItem (a); top = a; } void MainWindow::on_toolButton_pressed () { QDrag * drag = new QDrag (this); QMimeData * mime = new QMimeData; mime->setText ("Hello from Item"); drag->setMimeData (mime); Qt::DropAction action = drag->exec (Qt::CopyAction | Qt::MoveAction | Qt::LinkAction, Qt::MoveAction); } void MainWindow::showItem (QTreeWidgetItem * target, QGraphicsItem * item) { QTreeWidgetItem * node = new QTreeWidgetItem; node->setText (0, "item"); if (item->toolTip() != "") node->setText (0, item->toolTip ()); QAbstractGraphicsShapeItem * shape = dynamic_cast (item); if (shape != NULL) { node->setForeground (0, shape->pen().color()); node->setBackground (0, shape->brush()); } if (target == NULL) treeWidget->addTopLevelItem (node); else target->addChild (node); foreach (QGraphicsItem * subitem, item->childItems()) showItem (node, subitem); } void MainWindow::on_actionRun_triggered() { treeWidget->clear (); if (top != NULL) showItem (NULL, top); } void MainWindow::on_actionQuit_triggered() { close (); } /* main */ int main(int argc, char *argv[]) { QApplication a (argc, argv); MainWindow w; w.show(); return a.exec(); }