diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/tagstrip.cpp | 54 | ||||
| -rw-r--r-- | src/tagstrip.h | 23 |
2 files changed, 73 insertions, 4 deletions
diff --git a/src/tagstrip.cpp b/src/tagstrip.cpp index bad116a..7e671ce 100644 --- a/src/tagstrip.cpp +++ b/src/tagstrip.cpp @@ -18,6 +18,7 @@ #include "tagstrip.h" +#include <QContextMenuEvent> #include <QFontMetrics> #include <QPainter> @@ -109,6 +110,50 @@ void TagStrip::resizeEvent(QResizeEvent *event) relayout(); } +QRect TagStrip::chipRectAt(int index) const +{ + if (index < 0 || index >= m_visible.size()) + return {}; + + const QFontMetrics metrics(font()); + + // Reproduces paintEvent's own vertical placement exactly, which is derived + // from the font's height rather than from the chip's, so a chip whose text + // is shorter than the line still lands on the same baseline. + const int top = (height() - (metrics.height() + TagChip::kPaddingY * 2)) / 2; + + int x = 0; + for (int i = 0; i < index; ++i) + x += TagChip::sizeFor(metrics, m_visible.at(i)).width() + TagChip::kSpacing; + + return QRect(QPoint(x, top), TagChip::sizeFor(metrics, m_visible.at(index))); +} + +QString TagStrip::chipAt(const QPoint &point) const +{ + for (int i = 0; i < m_visible.size(); ++i) { + if (chipRectAt(i).contains(point)) + return m_visible.at(i); + } + // Deliberately nothing for the overflow chip and for empty space: the +N + // chip names a list, not a tag. + return {}; +} + +void TagStrip::contextMenuEvent(QContextMenuEvent *event) +{ + const QString tag = chipAt(event->pos()); + if (tag.isEmpty()) { + // Ignored rather than accepted, so a parent that offers its own menu + // still gets the chance to show it. + event->ignore(); + return; + } + + event->accept(); + emit tagContextMenuRequested(tag, event->globalPos()); +} + void TagStrip::paintEvent(QPaintEvent *) { if (m_visible.isEmpty()) @@ -119,12 +164,13 @@ void TagStrip::paintEvent(QPaintEvent *) const int top = (height() - (metrics.height() + TagChip::kPaddingY * 2)) / 2; int x = 0; - for (const QString &tag : m_visible) { - const QSize size = TagChip::sizeFor(metrics, tag); + for (int i = 0; i < m_visible.size(); ++i) { + const QString &tag = m_visible.at(i); + const QRect rect = chipRectAt(i); const QColor colour = m_tagColors ? m_tagColors->colourFor(tag) : TagColors().colourFor(tag); - TagChip::paint(&painter, QRect(QPoint(x, top), size), tag, colour); - x += size.width() + TagChip::kSpacing; + TagChip::paint(&painter, rect, tag, colour); + x = rect.right() + 1 + TagChip::kSpacing; } if (!m_hidden.isEmpty()) { diff --git a/src/tagstrip.h b/src/tagstrip.h index 4102bed..f59233b 100644 --- a/src/tagstrip.h +++ b/src/tagstrip.h @@ -18,10 +18,12 @@ #pragma once +#include <QRect> #include <QStringList> #include <QWidget> class TagColors; +class QContextMenuEvent; /// One row of tag chips under the message pane. /// @@ -48,9 +50,30 @@ public: QStringList visibleTags() const { return m_visible; } QStringList hiddenTags() const { return m_hidden; } + /// The rect of the visible chip at `index`, empty when out of range. + /// + /// The SAME function paintEvent lays out from, so what is drawn and what + /// is clickable cannot drift. `CardDelegate::expanderRectFor` exists for + /// this reason and this follows it. + QRect chipRectAt(int index) const; + + /// The tag under `point`, empty when the point is on no chip. + /// + /// The trailing "+N" chip yields an empty string: it stands for a list of + /// tags rather than for one, so there is nothing a search could name. + QString chipAt(const QPoint &point) const; + +signals: + /// A visible chip was right-clicked. `globalPos` is where to pop a menu. + /// + /// The strip does not build the menu itself: what a tag can do belongs to + /// the window, which owns the query bar and the actions. + void tagContextMenuRequested(const QString &tag, const QPoint &globalPos); + protected: void paintEvent(QPaintEvent *event) override; void resizeEvent(QResizeEvent *event) override; + void contextMenuEvent(QContextMenuEvent *event) override; private: /// Recomputes the visible/hidden split for the current width. |
