From 2deadc6cfbf71e06908963fb1bae628cfe370f9d Mon Sep 17 00:00:00 2001 From: jaseg Date: Sun, 16 Aug 2020 17:04:32 +0200 Subject: Basic model/view action works --- tagitem.cpp | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 64 insertions(+), 3 deletions(-) (limited to 'tagitem.cpp') diff --git a/tagitem.cpp b/tagitem.cpp index d251428..e493b81 100644 --- a/tagitem.cpp +++ b/tagitem.cpp @@ -1,25 +1,86 @@ #include "tagitem.h" +#include +#include + TagItem::TagItem(const Tag &tag) : valid(true) { setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable - | QGraphicsItem::ItemIsFocusable); - /* TODO text_it.setFlags(QGraphicsItem::ItemIgnoresTransformations); - */ + | QGraphicsItem::ItemIsFocusable + | QGraphicsItem::ItemIgnoresTransformations + | QGraphicsItem::ItemSendsGeometryChanges); + QFont font(QGuiApplication::font()); + font.setPointSize(18); + setFont(font); tagUpdated(tag); } void TagItem::tagUpdated(const Tag &tag) { m_tag = tag; + qDebug() << "TagItem updated" << tag.name << tag.anchor; setText(tag.name); setPos(tag.anchor); } +QRectF TagItem::boundingRect() const +{ + QRectF parentRect(QGraphicsSimpleTextItem::boundingRect()); + parentRect.translate(-(parentRect.bottomRight() - parentRect.topLeft()) * 0.5); + return parentRect.marginsAdded(QMargins(5, 5, 5, 5)); +} + +/* For some reason this is not exposed through the public API so we have to copy-paste it here m( */ +static void paintSelectionHighlightBorder(QPainter *painter, const QStyleOptionGraphicsItem *option, TagItem *item) +{ + const QRectF mbrect = painter->transform().mapRect(item->boundingRect()); + if (qMin(mbrect.width(), mbrect.height()) < qreal(1.0)) + return; + + const qreal pad = item->pen().widthF() / 2; + const qreal penWidth = 0; // cosmetic pen + const QColor fgcolor = option->palette.windowText().color(); + const QColor bgcolor( // ensure good contrast against fgcolor + fgcolor.red() > 127 ? 0 : 255, + fgcolor.green() > 127 ? 0 : 255, + fgcolor.blue() > 127 ? 0 : 255); + painter->setPen(QPen(bgcolor, penWidth, Qt::SolidLine)); + painter->setBrush(Qt::NoBrush); + painter->drawRect(item->boundingRect().adjusted(pad, pad, -pad, -pad)); + painter->setPen(QPen(option->palette.windowText(), 0, Qt::DashLine)); + painter->setBrush(Qt::NoBrush); + painter->drawRect(item->boundingRect().adjusted(pad, pad, -pad, -pad)); +} + +void TagItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) +{ + painter->setBrush(Qt::white); + painter->drawRect(boundingRect()); + + QRectF parentRect(QGraphicsSimpleTextItem::boundingRect()); + QPointF pos = (parentRect.bottomRight() - parentRect.topLeft()) * 0.5; + painter->translate(-pos); + + QStyleOptionGraphicsItem newOption(*option); + newOption.state = ~(QStyle::State_Selected |QStyle::State_HasFocus); + QGraphicsSimpleTextItem::paint(painter, &newOption, widget); + + painter->translate(pos); + if (option->state & (QStyle::State_Selected | QStyle::State_HasFocus)) + paintSelectionHighlightBorder(painter, option, this); +} + +QPainterPath TagItem::shape() const +{ + QPainterPath path; + return QGraphicsItem::shape(); +} + QVariant TagItem::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value) { + qDebug() << "itemChange" << m_tag.name << this->boundingRect() << change; if (change == ItemPositionChange) { /* https://gist.github.com/csukuangfj/c2a06416062bec9ed99eddd705c21275#file-qgraphicsscenetest-cpp-L90 * -- cgit