diff options
Diffstat (limited to 'Swift/QtUI/Whiteboard/GView.cpp')
-rw-r--r-- | Swift/QtUI/Whiteboard/GView.cpp | 142 |
1 files changed, 105 insertions, 37 deletions
diff --git a/Swift/QtUI/Whiteboard/GView.cpp b/Swift/QtUI/Whiteboard/GView.cpp index e82d193..8d5e2bc 100644 --- a/Swift/QtUI/Whiteboard/GView.cpp +++ b/Swift/QtUI/Whiteboard/GView.cpp @@ -8,41 +8,56 @@ #include <QtSwiftUtil.h> namespace Swift { - GView::GView(QGraphicsScene* scene, QWidget* parent) : QGraphicsView(scene, parent), brush(QColor(Qt::white)) - { + GView::GView(QGraphicsScene* scene, QWidget* parent) : QGraphicsView(scene, parent), brush(QColor(Qt::white)), defaultBrush(QColor(Qt::white)) { selectionRect = 0; lastItem = 0; zValue = 0; } - void GView::setLineWidth(int i) - { + void GView::setLineWidth(int i) { pen.setWidth(i); + if (selectionRect) { + QGraphicsItem* item = selectionRect->data(1).value<QGraphicsItem*>(); + changePenAndBrush(selectionRect->data(1).value<QGraphicsItem*>(), pen, brush); + lastItemChanged(item, items_.indexOf(item)+1, Update); + } else { + defaultPen.setWidth(i); + } } - void GView::setLineColor(QColor color) - { + void GView::setLineColor(QColor color) { pen.setColor(color); + if (selectionRect) { + QGraphicsItem* item = selectionRect->data(1).value<QGraphicsItem*>(); + changePenAndBrush(selectionRect->data(1).value<QGraphicsItem*>(), pen, brush); + lastItemChanged(item, items_.indexOf(item)+1, Update); + } else { + defaultPen.setColor(color); + } } - QColor GView::getLineColor() - { + QColor GView::getLineColor() { return pen.color(); } - void GView::setBrushColor(QColor color) - { + void GView::setBrushColor(QColor color) { brush.setColor(color); + if (selectionRect) { + QGraphicsItem* item = selectionRect->data(1).value<QGraphicsItem*>(); + changePenAndBrush(selectionRect->data(1).value<QGraphicsItem*>(), pen, brush); + lastItemChanged(item, items_.indexOf(item)+1, Update); + } else { + defaultBrush.setColor(color); + } + // brush.setStyle(Qt::SolidPattern); } - QColor GView::getBrushColor() - { + QColor GView::getBrushColor() { return brush.color(); } - void GView::setMode(Mode mode) - { + void GView::setMode(Mode mode) { this->mode = mode; lastItem = 0; } @@ -62,7 +77,7 @@ namespace Swift { } std::cout << "items in:" << std::endl; for (QList<QGraphicsItem*>::const_iterator it = items_.begin(); it != items_.end(); ++it) { - std::cout << "item: " << (*it)->data(100).toString().toStdString() << std::endl; + std::cout << (*it)->data(100).toString().toStdString() << std::endl; } std::cout << std::endl; } @@ -199,16 +214,17 @@ namespace Swift { { mousePressed = true; if (selectionRect != 0) { + pen = defaultPen; + brush = defaultBrush; scene()->removeItem(selectionRect); delete selectionRect; selectionRect = 0; + lineWidthChanged(pen.width()); } if (mode == Line) { QPointF point = this->mapToScene(event->pos()); QGraphicsItem* item = scene()->addLine(point.x(), point.y(), point.x(), point.y(), pen); QString id = getNewID(); -// items_.append(item); -// item->setZValue(zValue++); item->setZValue(10000000); item->setData(100, id); item->setData(101, items_.size()); @@ -218,8 +234,6 @@ namespace Swift { QPointF point = this->mapToScene(event->pos()); QGraphicsRectItem* item = scene()->addRect(point.x(), point.y(), 0, 0, pen, brush); QString id = getNewID(); -// items_.append(item); -// item->setZValue(zValue++); item->setZValue(10000000); item->setData(0, point); item->setData(100, id); @@ -236,14 +250,6 @@ namespace Swift { QGraphicsItem* item = scene()->items(rect).first(); QString id = item->data(100).toString(); deleteItem(id); -/* int i = 1; - QList<QGraphicsItem*>::const_iterator it; - for (it = items_.begin(); it != items_.end(); ++it) { - if (*it == item) { - break; - } - i++; - }*/ itemDeleted(id, items_.indexOf(item)+1); } } @@ -251,8 +257,6 @@ namespace Swift { QPointF point = this->mapToScene(event->pos()); QGraphicsEllipseItem* item = scene()->addEllipse(point.x(), point.y(), 0, 0, pen, brush); QString id = getNewID(); -// items_.append(item); -// item->setZValue(zValue++); item->setZValue(10000000); item->setData(0, point); item->setData(100, id); @@ -263,10 +267,8 @@ namespace Swift { QPointF point = this->mapToScene(event->pos()); FreehandLineItem* item = new FreehandLineItem; QString id = getNewID(); -// items_.append(item); item->setPen(pen); item->setStartPoint(point); -// item->setZValue(zValue++); item->setZValue(10000000); item->setData(100, id); item->setData(101, items_.size()); @@ -278,8 +280,6 @@ namespace Swift { QPainterPath path; QGraphicsPathItem* item = scene()->addPath(path, pen, brush); QString id = getNewID(); -// items_.append(item); -// item->setZValue(zValue++); item->setZValue(10000000); item->setData(0, point); item->setData(100, id); @@ -290,8 +290,6 @@ namespace Swift { QPointF point = this->mapToScene(event->pos()); QGraphicsTextItem* item = scene()->addText(""); QString id = getNewID(); -// items_.append(item); -// item->setZValue(zValue++); item->setData(100, id); item->setData(101, items_.size()); item->setDefaultTextColor(pen.color()); @@ -311,8 +309,6 @@ namespace Swift { polygon.append(point); item = scene()->addPolygon(polygon, pen, brush); QString id = getNewID(); -// items_.append(item); -// item->setZValue(zValue++); item->setZValue(10000000); item->setData(100, id); item->setData(101, items_.size()); @@ -335,6 +331,7 @@ namespace Swift { pen.setColor(QColor(Qt::gray)); pen.setStyle(Qt::DashLine); QGraphicsItem *item = scene()->items(rect).first(); + setActualPenAndBrushFromItem(item); selectionRect = scene()->addRect(item->boundingRect(), pen); selectionRect->setZValue(1000000); selectionRect->setData(0, item->pos()-point); @@ -386,4 +383,75 @@ namespace Swift { QGraphicsItem* item = selectionRect->data(1).value<QGraphicsItem*>(); item->setZValue(item->zValue()-1); } + + void GView::changePenAndBrush(QGraphicsItem* item, QPen pen, QBrush brush) { + QGraphicsLineItem* lineItem = qgraphicsitem_cast<QGraphicsLineItem*>(item); + if (lineItem) { + lineItem->setPen(pen); + } + + FreehandLineItem* handLineItem = qgraphicsitem_cast<FreehandLineItem*>(item); + if (handLineItem) { + handLineItem->setPen(pen); + } + + QGraphicsRectItem* rectItem = qgraphicsitem_cast<QGraphicsRectItem*>(item); + if (rectItem) { + rectItem->setPen(pen); + rectItem->setBrush(brush); + } + + QGraphicsTextItem* textItem = qgraphicsitem_cast<QGraphicsTextItem*>(item); + if (textItem) { + textItem->setDefaultTextColor(pen.color()); + } + + QGraphicsPolygonItem* polygonItem = qgraphicsitem_cast<QGraphicsPolygonItem*>(item); + if (polygonItem) { + polygonItem->setPen(pen); + polygonItem->setBrush(brush); + } + + QGraphicsEllipseItem* ellipseItem = qgraphicsitem_cast<QGraphicsEllipseItem*>(item); + if (ellipseItem) { + ellipseItem->setPen(pen); + ellipseItem->setBrush(brush); + } + } + + void GView::setActualPenAndBrushFromItem(QGraphicsItem* item) { + QGraphicsLineItem* lineItem = qgraphicsitem_cast<QGraphicsLineItem*>(item); + if (lineItem) { + pen = lineItem->pen(); + } + + FreehandLineItem* handLineItem = qgraphicsitem_cast<FreehandLineItem*>(item); + if (handLineItem) { + pen = handLineItem->pen(); + } + + QGraphicsRectItem* rectItem = qgraphicsitem_cast<QGraphicsRectItem*>(item); + if (rectItem) { + pen = rectItem->pen(); + brush = rectItem->brush(); + } + + QGraphicsTextItem* textItem = qgraphicsitem_cast<QGraphicsTextItem*>(item); + if (textItem) { + pen.setColor(textItem->defaultTextColor()); + } + + QGraphicsPolygonItem* polygonItem = qgraphicsitem_cast<QGraphicsPolygonItem*>(item); + if (polygonItem) { + pen = polygonItem->pen(); + brush = polygonItem->brush(); + } + + QGraphicsEllipseItem* ellipseItem = qgraphicsitem_cast<QGraphicsEllipseItem*>(item); + if (ellipseItem) { + pen = ellipseItem->pen(); + brush = ellipseItem->brush(); + } + lineWidthChanged(pen.width()); + } } |