Merge "persistent color scheme selection"
[trojita.git] / src / Gui / TagWidget.cpp
blob0b2fd8b66b476dbf4ee475476b7fd4057d201d6a
1 /* Copyright (C) 2012 Mildred <mildred-pub@mildred.fr>
2 Copyright (C) 2015 Erik Quaeghebeur <trojita@equaeghe.nospammail.net>
3 Copyright (C) 2006 - 2015 Jan Kundrát <jkt@kde.org>
5 This file is part of the Trojita Qt IMAP e-mail client,
6 http://trojita.flaska.net/
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of
11 the License or (at your option) version 3 or any later version
12 accepted by the membership of KDE e.V. (or its successor approved
13 by the membership of KDE e.V.), which shall act as a proxy
14 defined in Section 14 of version 3 of the license.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include <QEvent>
26 #include <QMouseEvent>
27 #include <QPainter>
28 #include <QPair>
30 #include "UiUtils/Color.h"
32 #include "TagWidget.h"
34 namespace Gui
37 static const QLatin1String closeIndicator(" | x");
39 TagWidget::TagWidget(const Mode mode, const QString &tagName, const QColor &backgroundColor):
40 QLabel(),
41 m_tagName(tagName),
42 m_mode(mode),
43 m_tint(backgroundColor),
44 m_splitPos(0)
46 m_tint.setAlpha(m_tint.alpha()/3);
47 setAlignment(Qt::AlignCenter);
48 int l,t,r,b;
49 getContentsMargins(&l, &t, &r, &b);
50 setContentsMargins(l + 4, t, r + 4, b);
51 if (m_mode == Mode::AddingWidget)
52 setCursor(Qt::PointingHandCursor);
55 TagWidget *TagWidget::addingWidget()
57 auto res = new TagWidget(Mode::AddingWidget, QString(), Qt::green);
58 res->setText(tr("Tag", "This is a verb!"));
59 return res;
62 TagWidget *TagWidget::userKeyword(const QString &tagName, Imap::Mailbox::FavoriteTagsModel *m_favoriteTags)
64 QColor color = m_favoriteTags->findBestColorForTags({tagName});
65 if (!color.isValid())
66 color = Qt::yellow;
67 auto res = new TagWidget(Mode::UserKeyword, tagName, color);
68 res->setText(tagName + closeIndicator);
69 res->setMouseTracking(true);
70 return res;
73 TagWidget *TagWidget::systemFlag(const QString &flagName)
75 auto res = new TagWidget(Mode::SystemFlag, flagName, Qt::gray);
76 res->setText(flagName);
77 return res;
80 bool TagWidget::event(QEvent *e)
82 if (e->type() == QEvent::MouseMove) {
83 if (m_mode == Mode::UserKeyword)
84 setCursor(static_cast<QMouseEvent*>(e)->pos().x() > m_splitPos ? Qt::PointingHandCursor : Qt::ArrowCursor);
85 } else if (e->type() == QEvent::Resize) {
86 if (m_mode == Mode::UserKeyword)
87 m_splitPos = contentsRect().right() - fontMetrics().width(closeIndicator);
88 } else if (e->type() == QEvent::MouseButtonPress) {
89 switch (m_mode) {
90 case Mode::AddingWidget:
91 emit addingClicked();
92 return true;
93 case Mode::UserKeyword: {
94 Q_ASSERT(!m_tagName.isEmpty());
95 if (static_cast<QMouseEvent*>(e)->pos().x() > m_splitPos) {
96 setDisabled(true);
97 setText(m_tagName);
98 setCursor(Qt::ArrowCursor);
99 setMouseTracking(false);
100 emit removeClicked(m_tagName);
102 return true;
104 case Mode::SystemFlag:
105 // do nothing -- this is just an indicator
106 break;
108 } else if (e->type() == QEvent::Paint) {
109 if (isEnabled()) {
110 QPainter p(this);
111 p.setRenderHint(QPainter::Antialiasing);
112 p.setBrush(UiUtils::tintColor(palette().color(QPalette::Active, backgroundRole()), m_tint));
113 p.setPen(Qt::NoPen);
114 const int rnd = qMin(width()/2, height()/4);
115 p.drawRoundedRect(rect(), rnd, rnd);
116 p.end();
120 return QLabel::event(e);
123 QString TagWidget::tagName() const
125 return m_tagName;
128 } // namespace Gui