More UI work

This commit is contained in:
jaseg 2020-08-17 00:56:17 +02:00
parent 2deadc6cfb
commit c6713d0876
15 changed files with 352 additions and 75 deletions

View file

@ -1,18 +1,22 @@
#include "tagitem.h"
#include "tagscene.h"
#include <QPainter>
#include <QGuiApplication>
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
TagItem::TagItem(const Tag &tag)
: valid(true)
, m_margins(2, 2, 2, 2)
{
setFlags(QGraphicsItem::ItemIsMovable
| QGraphicsItem::ItemIsSelectable
setFlags(QGraphicsItem::ItemIsSelectable
| QGraphicsItem::ItemIsMovable
| QGraphicsItem::ItemIsFocusable
| QGraphicsItem::ItemIgnoresTransformations
| QGraphicsItem::ItemSendsGeometryChanges);
QFont font(QGuiApplication::font());
font.setPointSize(18);
font.setPointSize(12);
setFont(font);
tagUpdated(tag);
}
@ -29,7 +33,7 @@ QRectF TagItem::boundingRect() const
{
QRectF parentRect(QGraphicsSimpleTextItem::boundingRect());
parentRect.translate(-(parentRect.bottomRight() - parentRect.topLeft()) * 0.5);
return parentRect.marginsAdded(QMargins(5, 5, 5, 5));
return parentRect.marginsAdded(m_margins);
}
/* For some reason this is not exposed through the public API so we have to copy-paste it here m( */
@ -56,7 +60,11 @@ static void paintSelectionHighlightBorder(QPainter *painter, const QStyleOptionG
void TagItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->setBrush(Qt::white);
bool selectionHighlight = option->state & (QStyle::State_Selected | QStyle::State_HasFocus);
if (selectionHighlight)
painter->setBrush(QColor("#80aaaaff"));
else
painter->setBrush(QColor("#80ffffff"));
painter->drawRect(boundingRect());
QRectF parentRect(QGraphicsSimpleTextItem::boundingRect());
@ -68,7 +76,7 @@ void TagItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, Q
QGraphicsSimpleTextItem::paint(painter, &newOption, widget);
painter->translate(pos);
if (option->state & (QStyle::State_Selected | QStyle::State_HasFocus))
if (selectionHighlight)
paintSelectionHighlightBorder(painter, option, this);
}
@ -80,13 +88,40 @@ QPainterPath TagItem::shape() const
QVariant TagItem::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
{
qDebug() << "itemChange" << m_tag.name << this->boundingRect() << change;
if (change == ItemPositionChange) {
//qDebug() << "itemChange" << m_tag.name << this->boundingRect() << change;
if (change == ItemSceneChange) {
assert(qvariant_cast<TagScene *>(value)); /* TagItems must only be used in TagScene. */
} else if (change == ItemPositionChange) {
/* https://gist.github.com/csukuangfj/c2a06416062bec9ed99eddd705c21275#file-qgraphicsscenetest-cpp-L90
*
*/
/* FIXME */
}
return QGraphicsItem::itemChange(change, value);
}
void TagItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
if (event->buttons() & Qt::LeftButton) {
QPoint delta = event->screenPos() - event->buttonDownScreenPos(Qt::LeftButton);
int pixelThreshold = 10;
if (QPoint::dotProduct(delta, delta) > pixelThreshold*pixelThreshold)
dragAboveThreshold = true;
if (!dragAboveThreshold) {
// patch up event to prevent small movements
event->setPos(event->buttonDownPos(Qt::LeftButton));
event->setScenePos(event->buttonDownScenePos(Qt::LeftButton));
event->setScreenPos(event->buttonDownScreenPos(Qt::LeftButton));
}
}
QGraphicsItem::mouseMoveEvent(event);
}
void TagItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
Q_UNUSED(event);
dragAboveThreshold = false;
QGraphicsItem::mouseReleaseEvent(event);
}