diff options
| -rw-r--r-- | src/tagstrip.cpp | 54 | ||||
| -rw-r--r-- | src/tagstrip.h | 23 | ||||
| -rw-r--r-- | tests/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | tests/test_tagstrip.cpp | 108 |
4 files changed, 182 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. diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 5b0b85b..68148ee 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -55,3 +55,4 @@ add_qtmaildir_test(tagdialog) add_qtmaildir_test(tagrules) add_qtmaildir_test(rulequery) add_qtmaildir_test(searchterm) +add_qtmaildir_test(tagstrip) diff --git a/tests/test_tagstrip.cpp b/tests/test_tagstrip.cpp new file mode 100644 index 0000000..636b24b --- /dev/null +++ b/tests/test_tagstrip.cpp @@ -0,0 +1,108 @@ +/* + * qtmaildir - a Qt6 mail client for notmuch-indexed Maildirs + * Copyright (C) 2026 Danilo M. <danix@danix.xyz> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include <QSignalSpy> +#include <QtTest> + +#include "tagstrip.h" + +/// The chip hit test. +/// +/// Asserted on rects from chipRectAt(), which is the SAME function paintEvent +/// lays out from, so a drawn chip and a clickable chip cannot drift apart. Not +/// asserted by rendering: a pixel probe cannot tell a chip that is drawn from a +/// chip that is drawn and clickable, and both halves have been broken +/// independently in this project before. +class TestTagStrip : public QObject +{ + Q_OBJECT +private slots: + void chipAtFindsEachVisibleTag(); + void chipAtMissesTheGapAndTheEdges(); + void chipAtIgnoresTheOverflowChip(); +}; + +void TestTagStrip::chipAtFindsEachVisibleTag() +{ + TagStrip strip; + strip.resize(600, 30); + strip.setTags({ QStringLiteral("inbox"), QStringLiteral("unread") }); + + const QStringList visible = strip.visibleTags(); + QCOMPARE(visible.size(), 2); + + // The guard: the geometry this test depends on must exist before the test + // can mean anything. A zero-width chip would make every lookup below miss + // and the test would pass for the wrong reason. + for (int i = 0; i < visible.size(); ++i) { + const QRect rect = strip.chipRectAt(i); + QVERIFY2(rect.width() > 0 && rect.height() > 0, + qPrintable(QStringLiteral("chip %1 has an empty rect").arg(i))); + QCOMPARE(strip.chipAt(rect.center()), visible.at(i)); + } +} + +void TestTagStrip::chipAtMissesTheGapAndTheEdges() +{ + TagStrip strip; + strip.resize(600, 30); + strip.setTags({ QStringLiteral("inbox"), QStringLiteral("unread") }); + QCOMPARE(strip.visibleTags().size(), 2); + + const QRect first = strip.chipRectAt(0); + const QRect second = strip.chipRectAt(1); + QVERIFY2(second.left() > first.right() + 1, + "the two chips must not touch, or there is no gap to test"); + + // Between the chips: no tag, so no menu entry rather than the nearest one. + const QPoint gap((first.right() + second.left()) / 2, first.center().y()); + QVERIFY(strip.chipAt(gap).isEmpty()); + + // Past the last chip, where the strip is empty space. + QVERIFY(strip.chipAt(QPoint(strip.width() - 1, first.center().y())).isEmpty()); +} + +void TestTagStrip::chipAtIgnoresTheOverflowChip() +{ + // The +N chip stands for a LIST of tags, not for a tag, so there is no + // single value a search could be built from. + TagStrip strip; + strip.resize(90, 30); + strip.setTags({ QStringLiteral("inbox"), QStringLiteral("unread"), + QStringLiteral("flagged"), QStringLiteral("attachment"), + QStringLiteral("replied") }); + + QVERIFY2(!strip.hiddenTags().isEmpty(), + "the strip must actually overflow, or this asserts nothing"); + QVERIFY2(!strip.visibleTags().isEmpty(), + "the strip must show at least one chip to test against"); + + // Every point across the strip either finds a VISIBLE tag or nothing. The + // overflow chip sits after the visible ones and must yield nothing. + for (int x = 0; x < strip.width(); x += 3) { + const QString found = strip.chipAt(QPoint(x, strip.height() / 2)); + if (!found.isEmpty()) + QVERIFY(strip.visibleTags().contains(found)); + } + + const QRect last = strip.chipRectAt(strip.visibleTags().size() - 1); + QVERIFY(strip.chipAt(QPoint(last.right() + 5, strip.height() / 2)).isEmpty()); +} + +QTEST_MAIN(TestTagStrip) +#include "test_tagstrip.moc" |
