summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorKonstantinos Sideris <sideris.konstantin@gmail.com>2017-12-30 17:29:57 +0200
committerKonstantinos Sideris <sideris.konstantin@gmail.com>2017-12-30 17:29:57 +0200
commit208f9579118307d7749de9bf9be537bf1a1d2b27 (patch)
tree2f27f4bcfab73a996a46964da9a256e4377af322 /src
parentUse qobject_cast on TimelineItem (diff)
downloadnheko-208f9579118307d7749de9bf9be537bf1a1d2b27.tar.xz
Re-order room list based on activity
fixes #2
Diffstat (limited to 'src')
-rw-r--r--src/ChatPage.cc8
-rw-r--r--src/MainWindow.cc2
-rw-r--r--src/RoomList.cc62
-rw-r--r--src/UserSettingsPage.cc32
4 files changed, 91 insertions, 13 deletions
diff --git a/src/ChatPage.cc b/src/ChatPage.cc

index 8abd651b..071fef71 100644 --- a/src/ChatPage.cc +++ b/src/ChatPage.cc
@@ -37,15 +37,19 @@ #include "TopRoomBar.h" #include "TypingDisplay.h" #include "UserInfoWidget.h" +#include "UserSettingsPage.h" #include "timeline/TimelineViewManager.h" constexpr int MAX_INITIAL_SYNC_FAILURES = 5; constexpr int SYNC_RETRY_TIMEOUT = 10000; -ChatPage::ChatPage(QSharedPointer<MatrixClient> client, QWidget *parent) +ChatPage::ChatPage(QSharedPointer<MatrixClient> client, + QSharedPointer<UserSettings> userSettings, + QWidget *parent) : QWidget(parent) , client_(client) + , userSettings_{userSettings} { setObjectName("chatPage"); @@ -74,7 +78,7 @@ ChatPage::ChatPage(QSharedPointer<MatrixClient> client, QWidget *parent) sidebarActions_, &SideBarActions::createRoom, client_.data(), &MatrixClient::createRoom); user_info_widget_ = new UserInfoWidget(sideBar_); - room_list_ = new RoomList(client, sideBar_); + room_list_ = new RoomList(client, userSettings_, sideBar_); sideBarLayout_->addWidget(user_info_widget_); sideBarLayout_->addWidget(room_list_); diff --git a/src/MainWindow.cc b/src/MainWindow.cc
index 04b0e8e3..39f58dac 100644 --- a/src/MainWindow.cc +++ b/src/MainWindow.cc
@@ -58,7 +58,7 @@ MainWindow::MainWindow(QWidget *parent) welcome_page_ = new WelcomePage(this); login_page_ = new LoginPage(client_, this); register_page_ = new RegisterPage(client_, this); - chat_page_ = new ChatPage(client_, this); + chat_page_ = new ChatPage(client_, userSettings_, this); userSettingsPage_ = new UserSettingsPage(userSettings_, this); // Initialize sliding widget manager. diff --git a/src/RoomList.cc b/src/RoomList.cc
index ea13d700..a47c73a6 100644 --- a/src/RoomList.cc +++ b/src/RoomList.cc
@@ -18,6 +18,7 @@ #include <QBuffer> #include <QDebug> #include <QObject> +#include <QTimer> #include "Cache.h" #include "MainWindow.h" @@ -27,10 +28,14 @@ #include "RoomList.h" #include "RoomSettings.h" #include "RoomState.h" +#include "UserSettingsPage.h" -RoomList::RoomList(QSharedPointer<MatrixClient> client, QWidget *parent) +RoomList::RoomList(QSharedPointer<MatrixClient> client, + QSharedPointer<UserSettings> userSettings, + QWidget *parent) : QWidget(parent) , client_(client) + , userSettings_{userSettings} { setStyleSheet("border: none;"); topLayout_ = new QVBoxLayout(this); @@ -291,6 +296,61 @@ RoomList::updateRoomDescription(const QString &roomid, const DescInfo &info) } rooms_.value(roomid)->setDescriptionMessage(info); + + if (underMouse()) { + // When the user hover out of the roomlist a sort will be triggered. + isSortPending_ = true; + return; + } + + isSortPending_ = false; + + emit sortRoomsByLastMessage(); +} + +void +RoomList::sortRoomsByLastMessage() +{ + if (!userSettings_->isOrderingEnabled()) + return; + + isSortPending_ = false; + + std::multimap<uint64_t, RoomInfoListItem *, std::greater<uint64_t>> times; + + for (int ii = 0; ii < contentsLayout_->count(); ++ii) { + auto room = qobject_cast<RoomInfoListItem *>(contentsLayout_->itemAt(ii)->widget()); + + if (!room) + continue; + + // Not a room message. + if (room->lastMessageInfo().userid.isEmpty()) + times.emplace(0, room); + else + times.emplace(room->lastMessageInfo().datetime.toSecsSinceEpoch(), room); + } + + for (auto it = times.cbegin(); it != times.cend(); ++it) { + const auto roomWidget = it->second; + const auto currentIndex = contentsLayout_->indexOf(roomWidget); + const auto newIndex = std::distance(times.cbegin(), it); + + if (currentIndex == newIndex) + continue; + + contentsLayout_->removeWidget(roomWidget); + contentsLayout_->insertWidget(newIndex, roomWidget); + } +} + +void +RoomList::leaveEvent(QEvent *event) +{ + if (isSortPending_) + QTimer::singleShot(700, this, &RoomList::sortRoomsByLastMessage); + + QWidget::leaveEvent(event); } void diff --git a/src/UserSettingsPage.cc b/src/UserSettingsPage.cc
index a5851c57..82cf23a7 100644 --- a/src/UserSettingsPage.cc +++ b/src/UserSettingsPage.cc
@@ -33,8 +33,9 @@ void UserSettings::load() { QSettings settings; - isTrayEnabled_ = settings.value("user/window/tray", true).toBool(); - theme_ = settings.value("user/theme", "light").toString(); + isTrayEnabled_ = settings.value("user/window/tray", true).toBool(); + isOrderingEnabled_ = settings.value("user/room_ordering", true).toBool(); + theme_ = settings.value("user/theme", "light").toString(); applyTheme(); } @@ -48,12 +49,6 @@ UserSettings::setTheme(QString theme) } void -UserSettings::setTray(bool state) -{ - isTrayEnabled_ = state; - save(); -} -void UserSettings::applyTheme() { QFile stylefile; @@ -86,6 +81,7 @@ UserSettings::save() settings.setValue("tray", isTrayEnabled_); settings.endGroup(); + settings.setValue("room_ordering", isOrderingEnabled_); settings.setValue("theme", theme()); settings.endGroup(); } @@ -132,6 +128,17 @@ UserSettingsPage::UserSettingsPage(QSharedPointer<UserSettings> settings, QWidge trayOptionLayout_->addWidget(trayLabel); trayOptionLayout_->addWidget(trayToggle_, 0, Qt::AlignBottom | Qt::AlignRight); + auto orderRoomLayout = new QHBoxLayout; + orderRoomLayout->setContentsMargins(0, OptionMargin, 0, OptionMargin); + auto orderLabel = new QLabel(tr("Re-order rooms based on activity"), this); + roomOrderToggle_ = new Toggle(this); + roomOrderToggle_->setActiveColor(QColor("#38A3D8")); + roomOrderToggle_->setInactiveColor(QColor("gray")); + orderLabel->setStyleSheet("font-size: 15px;"); + + orderRoomLayout->addWidget(orderLabel); + orderRoomLayout->addWidget(roomOrderToggle_, 0, Qt::AlignBottom | Qt::AlignRight); + auto themeOptionLayout_ = new QHBoxLayout; themeOptionLayout_->setContentsMargins(0, OptionMargin, 0, OptionMargin); auto themeLabel_ = new QLabel(tr("App theme"), this); @@ -155,6 +162,8 @@ UserSettingsPage::UserSettingsPage(QSharedPointer<UserSettings> settings, QWidge mainLayout_->addWidget(new HorizontalLine(this)); mainLayout_->addLayout(trayOptionLayout_); mainLayout_->addWidget(new HorizontalLine(this)); + mainLayout_->addLayout(orderRoomLayout); + mainLayout_->addWidget(new HorizontalLine(this)); mainLayout_->addLayout(themeOptionLayout_); mainLayout_->addWidget(new HorizontalLine(this)); @@ -171,6 +180,10 @@ UserSettingsPage::UserSettingsPage(QSharedPointer<UserSettings> settings, QWidge emit trayOptionChanged(!isDisabled); }); + connect(roomOrderToggle_, &Toggle::toggled, this, [=](bool isDisabled) { + settings_->setRoomOrdering(!isDisabled); + }); + connect(backBtn_, &QPushButton::clicked, this, [=]() { settings_->save(); emit moveBack(); @@ -181,7 +194,8 @@ void UserSettingsPage::showEvent(QShowEvent *) { restoreThemeCombo(); - trayToggle_->setState(!settings_->isTrayEnabled()); // Treats true as "off" + trayToggle_->setState(!settings_->isTrayEnabled()); // Treats true as "off" + roomOrderToggle_->setState(!settings_->isOrderingEnabled()); // Treats true as "off" } void