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/>.
26 #include <QMouseEvent>
30 #include "UiUtils/Color.h"
32 #include "TagWidget.h"
37 static const QLatin1String
closeIndicator(" | x");
39 TagWidget::TagWidget(const Mode mode
, const QString
&tagName
, const QColor
&backgroundColor
):
43 m_tint(backgroundColor
),
46 m_tint
.setAlpha(m_tint
.alpha()/3);
47 setAlignment(Qt::AlignCenter
);
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!"));
62 TagWidget
*TagWidget::userKeyword(const QString
&tagName
)
64 auto res
= new TagWidget(Mode::UserKeyword
, tagName
, Qt::yellow
);
65 res
->setText(tagName
+ closeIndicator
);
66 res
->setMouseTracking(true);
70 TagWidget
*TagWidget::systemFlag(const QString
&flagName
)
72 auto res
= new TagWidget(Mode::SystemFlag
, flagName
, Qt::gray
);
73 res
->setText(flagName
);
77 bool TagWidget::event(QEvent
*e
)
79 if (e
->type() == QEvent::MouseMove
) {
80 if (m_mode
== Mode::UserKeyword
)
81 setCursor(static_cast<QMouseEvent
*>(e
)->pos().x() > m_splitPos
? Qt::PointingHandCursor
: Qt::ArrowCursor
);
82 } else if (e
->type() == QEvent::Resize
) {
83 if (m_mode
== Mode::UserKeyword
)
84 m_splitPos
= contentsRect().right() - fontMetrics().width(closeIndicator
);
85 } else if (e
->type() == QEvent::MouseButtonPress
) {
87 case Mode::AddingWidget
:
90 case Mode::UserKeyword
: {
91 Q_ASSERT(!m_tagName
.isEmpty());
92 if (static_cast<QMouseEvent
*>(e
)->pos().x() > m_splitPos
) {
95 setCursor(Qt::ArrowCursor
);
96 setMouseTracking(false);
97 emit
removeClicked(m_tagName
);
101 case Mode::SystemFlag
:
102 // do nothing -- this is just an indicator
105 } else if (e
->type() == QEvent::Paint
) {
108 p
.setRenderHint(QPainter::Antialiasing
);
109 p
.setBrush(UiUtils::tintColor(palette().color(QPalette::Active
, backgroundRole()), m_tint
));
111 const int rnd
= qMin(width()/2, height()/4);
112 p
.drawRoundedRect(rect(), rnd
, rnd
);
117 return QLabel::event(e
);
120 QString
TagWidget::tagName() const