File indexing completed on 2024-06-16 05:01:31
0001 /* Copyright (C) 2012 Mildred <mildred-pub@mildred.fr> 0002 Copyright (C) 2015 Erik Quaeghebeur <trojita@equaeghe.nospammail.net> 0003 Copyright (C) 2006 - 2015 Jan Kundrát <jkt@kde.org> 0004 0005 This file is part of the Trojita Qt IMAP e-mail client, 0006 http://trojita.flaska.net/ 0007 0008 This program is free software; you can redistribute it and/or 0009 modify it under the terms of the GNU General Public License as 0010 published by the Free Software Foundation; either version 2 of 0011 the License or (at your option) version 3 or any later version 0012 accepted by the membership of KDE e.V. (or its successor approved 0013 by the membership of KDE e.V.), which shall act as a proxy 0014 defined in Section 14 of version 3 of the license. 0015 0016 This program is distributed in the hope that it will be useful, 0017 but WITHOUT ANY WARRANTY; without even the implied warranty of 0018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0019 GNU General Public License for more details. 0020 0021 You should have received a copy of the GNU General Public License 0022 along with this program. If not, see <http://www.gnu.org/licenses/>. 0023 */ 0024 0025 #include <QEvent> 0026 #include <QMouseEvent> 0027 #include <QPainter> 0028 #include <QPair> 0029 0030 #include "UiUtils/Color.h" 0031 0032 #include "TagWidget.h" 0033 0034 namespace Gui 0035 { 0036 0037 static const QLatin1String closeIndicator(" | x"); 0038 0039 TagWidget::TagWidget(const Mode mode, const QString &tagName, const QColor &backgroundColor): 0040 QLabel(), 0041 m_tagName(tagName), 0042 m_mode(mode), 0043 m_tint(backgroundColor), 0044 m_splitPos(0) 0045 { 0046 m_tint.setAlpha(m_tint.alpha()/3); 0047 setAlignment(Qt::AlignCenter); 0048 auto margins = contentsMargins(); 0049 margins.setLeft(margins.left() + 4); 0050 margins.setRight(margins.right() + 4); 0051 setContentsMargins(margins); 0052 if (m_mode == Mode::AddingWidget) 0053 setCursor(Qt::PointingHandCursor); 0054 } 0055 0056 TagWidget *TagWidget::addingWidget() 0057 { 0058 auto res = new TagWidget(Mode::AddingWidget, QString(), Qt::green); 0059 res->setText(tr("Tag", "This is a verb!")); 0060 return res; 0061 } 0062 0063 TagWidget *TagWidget::userKeyword(const QString &tagName, Imap::Mailbox::FavoriteTagsModel *m_favoriteTags) 0064 { 0065 QColor color = m_favoriteTags->findBestColorForTags({tagName}); 0066 if (!color.isValid()) 0067 color = Qt::yellow; 0068 auto res = new TagWidget(Mode::UserKeyword, tagName, color); 0069 res->setText(tagName + closeIndicator); 0070 res->setMouseTracking(true); 0071 return res; 0072 } 0073 0074 TagWidget *TagWidget::systemFlag(const QString &flagName) 0075 { 0076 auto res = new TagWidget(Mode::SystemFlag, flagName, Qt::gray); 0077 res->setText(flagName); 0078 return res; 0079 } 0080 0081 bool TagWidget::event(QEvent *e) 0082 { 0083 if (e->type() == QEvent::MouseMove) { 0084 if (m_mode == Mode::UserKeyword) 0085 setCursor(static_cast<QMouseEvent*>(e)->pos().x() > m_splitPos ? Qt::PointingHandCursor : Qt::ArrowCursor); 0086 } else if (e->type() == QEvent::Resize) { 0087 if (m_mode == Mode::UserKeyword) 0088 m_splitPos = contentsRect().right() - fontMetrics().horizontalAdvance(closeIndicator); 0089 } else if (e->type() == QEvent::MouseButtonPress) { 0090 switch (m_mode) { 0091 case Mode::AddingWidget: 0092 emit addingClicked(); 0093 return true; 0094 case Mode::UserKeyword: { 0095 Q_ASSERT(!m_tagName.isEmpty()); 0096 if (static_cast<QMouseEvent*>(e)->pos().x() > m_splitPos) { 0097 setDisabled(true); 0098 setText(m_tagName); 0099 setCursor(Qt::ArrowCursor); 0100 setMouseTracking(false); 0101 emit removeClicked(m_tagName); 0102 } 0103 return true; 0104 } 0105 case Mode::SystemFlag: 0106 // do nothing -- this is just an indicator 0107 break; 0108 } 0109 } else if (e->type() == QEvent::Paint) { 0110 if (isEnabled()) { 0111 QPainter p(this); 0112 p.setRenderHint(QPainter::Antialiasing); 0113 p.setBrush(UiUtils::tintColor(palette().color(QPalette::Active, backgroundRole()), m_tint)); 0114 p.setPen(Qt::NoPen); 0115 const int rnd = qMin(width()/2, height()/4); 0116 p.drawRoundedRect(rect(), rnd, rnd); 0117 p.end(); 0118 } 0119 } 0120 0121 return QLabel::event(e); 0122 } 0123 0124 QString TagWidget::tagName() const 0125 { 0126 return m_tagName; 0127 } 0128 0129 } // namespace Gui