From f439cb01145bf3f17216242fe842af684aac6a06 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Mon, 5 Mar 2018 18:45:01 +0900 Subject: [PATCH] Interim support of room tags Closes #322. Full support includes room ordering within a tag, which implies organising rooms under tags in the UI (the scope of #323). --- client/roomdialogs.cpp | 35 ++++++++++++++++++++++++++++++++++- client/roomdialogs.h | 1 + client/roomlistdock.cpp | 33 +++++++++++++++++++++++++++++++-- client/roomlistdock.h | 2 ++ lib | 2 +- 5 files changed, 69 insertions(+), 4 deletions(-) diff --git a/client/roomdialogs.cpp b/client/roomdialogs.cpp index dd2ee48f4..cdbd00b68 100644 --- a/client/roomdialogs.cpp +++ b/client/roomdialogs.cpp @@ -108,6 +108,7 @@ RoomDialogBase::RoomDialogBase(const QString& title, RoomSettingsDialog::RoomSettingsDialog(QuaternionRoom* room, QWidget* parent) : RoomDialogBase(tr("Room settings: %1").arg(room->displayName()), tr("Update room"), room, parent) + , tagsList(new QListWidget) { Q_ASSERT(room != nullptr); connect(room, &QuaternionRoom::avatarChanged, this, [this, room] { @@ -115,7 +116,12 @@ RoomSettingsDialog::RoomSettingsDialog(QuaternionRoom* room, QWidget* parent) avatar->setPixmap(QPixmap::fromImage(room->avatar(64))); }); avatar->setPixmap(QPixmap::fromImage(room->avatar(64))); - // TODO: Make same for other fields + tagsList->setSizeAdjustPolicy( + QAbstractScrollArea::AdjustToContentsOnFirstShow); + tagsList->setUniformItemSizes(true); + tagsList->setSelectionMode(QAbstractItemView::ExtendedSelection); + + formLayout->addRow(tr("Tags"), tagsList); } void RoomSettingsDialog::load() @@ -123,6 +129,22 @@ void RoomSettingsDialog::load() roomName->setText(room->name()); alias->setText(room->canonicalAlias()); topic->setPlainText(room->topic()); + + tagsList->clear(); + auto roomTags = room->tagNames(); + for (const auto& tag: room->connection()->tagNames()) + { + auto tagDisplayName = + tag == QMatrixClient::FavouriteTag ? tr("Favourite") : + tag == QMatrixClient::LowPriorityTag ? tr("Low priority") : + tag; + auto* item = new QListWidgetItem(tagDisplayName, tagsList); + item->setData(Qt::UserRole, tag); + item->setFlags(Qt::ItemIsEnabled|Qt::ItemIsUserCheckable); + item->setCheckState( + roomTags.contains(tag) ? Qt::Checked : Qt::Unchecked); + tagsList->addItem(item); + } } void RoomSettingsDialog::apply() @@ -133,6 +155,17 @@ void RoomSettingsDialog::apply() room->setCanonicalAlias(alias->text()); if (topic->toPlainText() != room->topic()) room->setTopic(topic->toPlainText()); + auto tags = room->tags(); + for (int i = 0; i < tagsList->count(); ++i) + { + const auto* item = tagsList->item(i); + const auto tagName = item->data(Qt::UserRole).toString(); + if (item->checkState() == Qt::Checked) + tags[tagName]; // Just ensure the tag is there, no overwriting + else + tags.remove(tagName); + } + room->setTags(tags); accept(); } diff --git a/client/roomdialogs.h b/client/roomdialogs.h index 50580fe23..82f863854 100644 --- a/client/roomdialogs.h +++ b/client/roomdialogs.h @@ -70,6 +70,7 @@ class RoomSettingsDialog : public RoomDialogBase void apply() override; private: + QListWidget* tagsList; bool userChangedAvatar = false; }; diff --git a/client/roomlistdock.cpp b/client/roomlistdock.cpp index fa4d99e39..8f9a3baf3 100644 --- a/client/roomlistdock.cpp +++ b/client/roomlistdock.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include "models/roomlistmodel.h" #include "quaternionroom.h" @@ -104,6 +105,10 @@ RoomListDock::RoomListDock(QWidget* parent) connect(markAsReadAction, &QAction::triggered, this, &RoomListDock::menuMarkReadSelected); contextMenu->addAction(markAsReadAction); contextMenu->addSeparator(); + addTagsAction = new QAction(tr("Add tags..."), this); + connect(addTagsAction, &QAction::triggered, this, &RoomListDock::addTagsSelected); + contextMenu->addAction(addTagsAction); + contextMenu->addSeparator(); joinAction = new QAction(tr("Join room"), this); connect(joinAction, &QAction::triggered, this, &RoomListDock::menuJoinSelected); contextMenu->addAction(joinAction); @@ -138,13 +143,15 @@ void RoomListDock::showContextMenu(const QPoint& pos) auto room = model->roomAt(proxyModel->mapToSource(index)); using QMatrixClient::JoinState; - joinAction->setEnabled(room->joinState() != JoinState::Join); + bool joined = room->joinState() == JoinState::Join; + markAsReadAction->setEnabled(joined); + addTagsAction->setEnabled(joined); + joinAction->setEnabled(!joined); leaveAction->setText(room->joinState() == JoinState::Invite ? tr("Reject invitation") : tr("Leave room")); leaveAction->setEnabled(room->joinState() != JoinState::Leave); forgetAction->setText(room->joinState() == JoinState::Invite ? tr("Forget invitation") : tr("Forget room")); - markAsReadAction->setEnabled(room->joinState() == JoinState::Join); contextMenu->popup(mapToGlobal(pos)); } @@ -183,6 +190,28 @@ void RoomListDock::menuMarkReadSelected() room->markAllMessagesAsRead(); } +void RoomListDock::addTagsSelected() +{ + if (auto room = getSelectedRoom()) + { + auto tagsInput = QInputDialog::getMultiLineText(this, + tr("Enter new tags for the room"), + tr("Enter tags to add to this room, one tag per line")); + if (tagsInput.isEmpty()) + return; + + auto tags = room->tags(); + for (const auto& tag: tagsInput.split('\n')) + { + // No overwriting, just ensure the tag exists + tags[tag == tr("Favourite") ? QMatrixClient::FavouriteTag : + tag == tr("Low priority") ? QMatrixClient::LowPriorityTag : + tag]; + } + room->setTags(tags); + } +} + void RoomListDock::refreshTitle() { setWindowTitle(tr("Rooms (%1)").arg(model->rowCount(QModelIndex()))); diff --git a/client/roomlistdock.h b/client/roomlistdock.h index 211c8dc19..dba8d41fd 100644 --- a/client/roomlistdock.h +++ b/client/roomlistdock.h @@ -46,6 +46,7 @@ class RoomListDock : public QDockWidget void rowSelected(const QModelIndex& index); void showContextMenu(const QPoint& pos); void menuMarkReadSelected(); + void addTagsSelected(); void menuJoinSelected(); void menuLeaveSelected(); void menuForgetSelected(); @@ -57,6 +58,7 @@ class RoomListDock : public QDockWidget QSortFilterProxyModel* proxyModel; QMenu* contextMenu; QAction* markAsReadAction; + QAction* addTagsAction; QAction* joinAction; QAction* leaveAction; QAction* forgetAction; diff --git a/lib b/lib index 164938cc1..1c5b7f8a1 160000 --- a/lib +++ b/lib @@ -1 +1 @@ -Subproject commit 164938cc1900afe94128e83c8c52bf04a8981ded +Subproject commit 1c5b7f8a1c6d82a612f4a2a74eeb644a40370ca5