[kde-doc-english] [trojita] src/Gui: GUI: TagWidget improvements

Jan Kundrát jkt at kde.org
Fri Nov 27 13:09:43 UTC 2015


Git commit 372485c1009d9b8471a4b3f852dc6c54e9d110bd by Jan Kundrát, on behalf of Erik Quaeghebeur.
Committed on 25/11/2015 at 20:49.
Pushed by gerrit into branch 'master'.

GUI: TagWidget improvements

Some special keywords ($MDNSent, $Submitted, $SubmitPending) do not have
a tailor-made presentation in the GUI. A TagWidget variant is added so
that these keywords can still be visualized (as gray, unremovable tags).

The function for adding new tags allowed (unintentionally?) for setting
multiple tags by space-separating them. With multiple tags, the check
for reserved flags failed. We now still allow the useful multiple tag
addition feature, but do a tag-per-tag reserved keyword check.

The TagWidget got changed to better reflect the three separate use cases
("user keyword", "system flag", "adding button"). We're now also using a
hard-coded black border because it makes more sense, IMHO. Users with
dark themes are probably screwed, but I believe that the level of
screwedness is a at most the same as previously.

Now that the code was rewritten to a large extent, let's change the
license to match the rest of Trojita. If this is a derived work, the
change is still OK because the original license explicitly allows such a
change.

Change-Id: Ia8f374bd45201b1d65b5a03300794550d3f90a43

M  +59   -19   src/Gui/TagListWidget.cpp
M  +20   -4    src/Gui/TagListWidget.h
M  +52   -16   src/Gui/TagWidget.cpp
M  +30   -6    src/Gui/TagWidget.h

http://commits.kde.org/trojita/372485c1009d9b8471a4b3f852dc6c54e9d110bd

diff --git a/src/Gui/TagListWidget.cpp b/src/Gui/TagListWidget.cpp
index cf31d6f..611a1db 100644
--- a/src/Gui/TagListWidget.cpp
+++ b/src/Gui/TagListWidget.cpp
@@ -1,10 +1,25 @@
 /* Copyright (C) 2012 Mildred <mildred-pub at mildred.fr>
+   Copyright (C) 2015 Erik Quaeghebeur <trojita at equaeghe.nospammail.net>
+   Copyright (C) 2006 - 2015 Jan Kundrát <jkt at kde.org>
 
    This file is part of the Trojita Qt IMAP e-mail client,
    http://trojita.flaska.net/
 
-   This program is free software, you can do what you want with it, including
-   changing its license (which is this text right here).
+   This program is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License as
+   published by the Free Software Foundation; either version 2 of
+   the License or (at your option) version 3 or any later version
+   accepted by the membership of KDE e.V. (or its successor approved
+   by the membership of KDE e.V.), which shall act as a proxy
+   defined in Section 14 of version 3 of the license.
+
+   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, see <http://www.gnu.org/licenses/>.
 */
 
 #include <QHBoxLayout>
@@ -12,6 +27,7 @@
 #include <QLabel>
 #include <QMessageBox>
 #include <QPushButton>
+#include <QStringList>
 
 #include "TagListWidget.h"
 #include "FlowLayout.h"
@@ -28,11 +44,15 @@ TagListWidget::TagListWidget(QWidget *parent) :
     parentLayout = new FlowLayout(this, 0);
     setLayout(parentLayout);
 
-    addButton = new TagWidget(QLatin1String("+"));
-    connect(addButton, SIGNAL(clicked()), this, SLOT(newTagRequested()));
+    addButton = TagWidget::addingWidget();
+    connect(addButton, SIGNAL(addingClicked()), this, SLOT(newTagsRequested()));
 
     parentLayout->addWidget(new QLabel(tr("<b>Tags:</b>")));
     parentLayout->addWidget(addButton);
+
+    unsupportedReservedKeywords.insert(Imap::Mailbox::FlagNames::mdnsent.toLower());
+    unsupportedReservedKeywords.insert(Imap::Mailbox::FlagNames::submitted.toLower());
+    unsupportedReservedKeywords.insert(Imap::Mailbox::FlagNames::submitpending.toLower());
 }
 
 void TagListWidget::setTagList(QStringList list)
@@ -40,14 +60,22 @@ void TagListWidget::setTagList(QStringList list)
     empty();
     parentLayout->removeWidget(addButton);
 
-    foreach(const QString &tagName, list) {
-        if (Imap::Mailbox::FlagNames::toCanonical.contains(tagName.toLower()))
-            continue;
-        TagWidget *lbl = new TagWidget(tagName, QLatin1String("x"));
-        parentLayout->addWidget(lbl);
-        connect(lbl, SIGNAL(removeClicked(QString)), this, SIGNAL(tagRemoved(QString)));
-
-        children << lbl;
+    Q_FOREACH(const QString &tagName, list) {
+        QString tagNameLowerCase = tagName.toLower();
+        if (Imap::Mailbox::FlagNames::toCanonical.contains(tagNameLowerCase)) {
+            if (unsupportedReservedKeywords.contains(tagNameLowerCase)) {
+                TagWidget *lbl = TagWidget::systemFlag(tagName);
+                parentLayout->addWidget(lbl);
+                children << lbl;
+            } else {
+                continue;
+            }
+        } else {
+            TagWidget *lbl = TagWidget::userKeyword(tagName);
+            parentLayout->addWidget(lbl);
+            connect(lbl, SIGNAL(removeClicked(QString)), this, SIGNAL(tagRemoved(QString)));
+            children << lbl;
+        }
     }
 
     parentLayout->addWidget(addButton);
@@ -59,19 +87,31 @@ void TagListWidget::empty()
     children.clear();
 }
 
-void TagListWidget::newTagRequested()
+void TagListWidget::newTagsRequested()
 {
-    QString tag = QInputDialog::getText(this, tr("New Tag"), tr("Tag name:"));
-    if (tag.isEmpty()) {
+    QString tags = QInputDialog::getText(this, tr("New Tags"), tr("Tag names (space-separated):"));
+
+    // Check whether any text has been entered
+    if (tags.isEmpty()) {
         return;
     }
-    if (Imap::Mailbox::FlagNames::toCanonical.contains(tag.toLower())) {
-        QMessageBox::warning(this, tr("Invalid tag value"),
-                             tr("Tag name %1 is a reserved name which cannot be manipulated this way.").arg(tag));
+
+    // Check whether reserved keywords have been entered
+    QStringList tagList = tags.split(QLatin1String(" "), QString::SkipEmptyParts);
+    tagList.removeDuplicates();
+    QStringList reservedTagsList = QStringList();
+    for (QStringList::const_iterator it = tagList.constBegin(); it != tagList.constEnd(); ++it) {
+        if (Imap::Mailbox::FlagNames::toCanonical.contains(it->toLower())) {
+            reservedTagsList << *it;
+        }
+    }
+    if (!reservedTagsList.isEmpty()) {
+        QMessageBox::warning(this, tr("Disallowed tag value"),
+                             tr("No tags were set because the following given tag(s) are reserved and have been disallowed from being set in this way: %1.").arg(reservedTagsList.join(QLatin1String(", "))));
         return;
     }
 
-    emit tagAdded(tag);
+    emit tagAdded(tags);
 }
 
 }
diff --git a/src/Gui/TagListWidget.h b/src/Gui/TagListWidget.h
index ba85c9d..4bf43e3 100644
--- a/src/Gui/TagListWidget.h
+++ b/src/Gui/TagListWidget.h
@@ -1,16 +1,32 @@
 /* Copyright (C) 2012 Mildred <mildred-pub at mildred.fr>
+   Copyright (C) 2015 Erik Quaeghebeur <trojita at equaeghe.nospammail.net>
+   Copyright (C) 2006 - 2015 Jan Kundrát <jkt at kde.org>
 
    This file is part of the Trojita Qt IMAP e-mail client,
    http://trojita.flaska.net/
 
-   This program is free software, you can do what you want with it, including
-   changing its license (which is this text right here).
+   This program is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License as
+   published by the Free Software Foundation; either version 2 of
+   the License or (at your option) version 3 or any later version
+   accepted by the membership of KDE e.V. (or its successor approved
+   by the membership of KDE e.V.), which shall act as a proxy
+   defined in Section 14 of version 3 of the license.
+
+   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, see <http://www.gnu.org/licenses/>.
 */
 
 #ifndef TAGLISTWIDGET_H
 #define TAGLISTWIDGET_H
 
 #include <QList>
+#include <QSet>
 #include <QWidget>
 
 class QPushButton;
@@ -33,13 +49,13 @@ signals:
     void tagRemoved(QString tag);
 
 private slots:
-    void newTagRequested();
+    void newTagsRequested();
 
 private:
     FlowLayout *parentLayout;
     TagWidget *addButton;
     QList<QObject *> children;
-
+    QSet<QString> unsupportedReservedKeywords;
     void empty();
 };
 
diff --git a/src/Gui/TagWidget.cpp b/src/Gui/TagWidget.cpp
index c2590a0..521ecf9 100644
--- a/src/Gui/TagWidget.cpp
+++ b/src/Gui/TagWidget.cpp
@@ -1,38 +1,79 @@
 /* Copyright (C) 2012 Mildred <mildred-pub at mildred.fr>
+   Copyright (C) 2015 Erik Quaeghebeur <trojita at equaeghe.nospammail.net>
+   Copyright (C) 2006 - 2015 Jan Kundrát <jkt at kde.org>
 
    This file is part of the Trojita Qt IMAP e-mail client,
    http://trojita.flaska.net/
 
-   This program is free software, you can do what you want with it, including
-   changing its license (which is this text right here).
+   This program is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License as
+   published by the Free Software Foundation; either version 2 of
+   the License or (at your option) version 3 or any later version
+   accepted by the membership of KDE e.V. (or its successor approved
+   by the membership of KDE e.V.), which shall act as a proxy
+   defined in Section 14 of version 3 of the license.
+
+   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, see <http://www.gnu.org/licenses/>.
 */
 
 #include <QEvent>
+#include <QPair>
 
 #include "TagWidget.h"
 
 namespace Gui
 {
 
-TagWidget::TagWidget(const QString &buttonText, QWidget *parent, Qt::WindowFlags f) :
-    QLabel(buttonText, parent, f)
+TagWidget::TagWidget(const Mode mode, const QString &tagName, const QString &backgroundColor):
+    QLabel(),
+    m_tagName(tagName),
+    m_mode(mode)
+{
+    setStyleSheet(QString::fromLatin1("border: 1px solid black; border-radius: 4px; background-color: %1;").arg(backgroundColor));
+}
+
+TagWidget *TagWidget::addingWidget()
 {
-    commonInit();
+    auto res = new TagWidget(Mode::AddingWidget, QString(), QLatin1String("lightgreen"));
+    res->setText(QLatin1String("+"));
+    return res;
 }
 
-TagWidget::TagWidget(const QString &tagName, const QString &buttonText, QWidget *parent, Qt::WindowFlags f) :
-    QLabel(tagName + QLatin1String(" | ") + buttonText, parent, f), m_tagName(tagName)
+TagWidget *TagWidget::userKeyword(const QString &tagName)
 {
-    commonInit();
+    auto res = new TagWidget(Mode::UserKeyword, tagName, QLatin1String("lightyellow"));
+    res->setText(tagName + QLatin1String(" | x"));
+    return res;
+}
+
+TagWidget *TagWidget::systemFlag(const QString &flagName)
+{
+    auto res = new TagWidget(Mode::SystemFlag, flagName, QLatin1String("lightgrey"));
+    res->setText(flagName);
+    return res;
 }
 
 bool TagWidget::event(QEvent *e)
 {
     if (e->type() == QEvent::MouseButtonPress) {
-        if (!m_tagName.isEmpty())
+        switch (m_mode) {
+        case Mode::AddingWidget:
+            emit addingClicked();
+            return true;
+        case Mode::UserKeyword:
+            Q_ASSERT(!m_tagName.isEmpty());
             emit removeClicked(m_tagName);
-        emit clicked();
-        return true;
+            return true;
+        case Mode::SystemFlag:
+            // do nothing -- this is just an indicator
+            break;
+        }
     }
 
     return QLabel::event(e);
@@ -43,9 +84,4 @@ QString TagWidget::tagName() const
     return m_tagName;
 }
 
-void TagWidget::commonInit()
-{
-    setStyleSheet(QLatin1String("border: 1px solid green; border-radius: 4px; background-color: lightgreen;"));
-}
-
 } // namespace Gui
diff --git a/src/Gui/TagWidget.h b/src/Gui/TagWidget.h
index 05016c0..1faba34 100644
--- a/src/Gui/TagWidget.h
+++ b/src/Gui/TagWidget.h
@@ -1,10 +1,25 @@
 /* Copyright (C) 2012 Mildred <mildred-pub at mildred.fr>
+   Copyright (C) 2015 Erik Quaeghebeur <trojita at equaeghe.nospammail.net>
+   Copyright (C) 2006 - 2015 Jan Kundrát <jkt at kde.org>
 
    This file is part of the Trojita Qt IMAP e-mail client,
    http://trojita.flaska.net/
 
-   This program is free software, you can do what you want with it, including
-   changing its license (which is this text right here).
+   This program is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License as
+   published by the Free Software Foundation; either version 2 of
+   the License or (at your option) version 3 or any later version
+   accepted by the membership of KDE e.V. (or its successor approved
+   by the membership of KDE e.V.), which shall act as a proxy
+   defined in Section 14 of version 3 of the license.
+
+   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, see <http://www.gnu.org/licenses/>.
 */
 
 #ifndef GUI_TAGWIDGET_H
@@ -20,8 +35,9 @@ class TagWidget : public QLabel
     Q_OBJECT
     Q_PROPERTY(QString tagName READ tagName USER true)
 public:
-    explicit TagWidget(const QString &buttonText, QWidget *parent=0, Qt::WindowFlags f=0);
-    explicit TagWidget(const QString &tagName, const QString &buttonText, QWidget *parent=0, Qt::WindowFlags f=0);
+    static TagWidget *addingWidget();
+    static TagWidget *userKeyword(const QString &tagName);
+    static TagWidget *systemFlag(const QString &flagName);
 
     QString tagName() const;
 
@@ -29,11 +45,19 @@ public:
 
 signals:
     void removeClicked(QString);
-    void clicked();
+    void addingClicked();
 
 private:
+    enum class Mode {
+        SystemFlag,
+        UserKeyword,
+        AddingWidget,
+    };
+
     QString m_tagName;
-    void commonInit();
+    const Mode m_mode;
+
+    TagWidget(const Mode mode, const QString &tagName, const QString &backgroundColor);
 };
 
 } // namespace Gui


More information about the kde-doc-english mailing list