From c431eee40156534e3fbf2c1f14336f3b13fcab6b Mon Sep 17 00:00:00 2001 From: Konstantinos Sideris Date: Tue, 11 Apr 2017 22:48:02 +0300 Subject: [PATCH] Use a single nick to color map --- include/HistoryView.h | 5 ---- include/HistoryViewManager.h | 4 ++++ src/HistoryView.cc | 45 ++++-------------------------------- src/HistoryViewManager.cc | 42 ++++++++++++++++++++++++++++++++- 4 files changed, 49 insertions(+), 47 deletions(-) diff --git a/include/HistoryView.h b/include/HistoryView.h index f5fabbb6..9eb0783f 100644 --- a/include/HistoryView.h +++ b/include/HistoryView.h @@ -44,12 +44,8 @@ public slots: void sliderRangeChanged(int min, int max); private: - static const QList COLORS; - void init(); - QString chooseRandomColor(); - QVBoxLayout *top_layout_; QVBoxLayout *scroll_layout_; @@ -57,7 +53,6 @@ private: QWidget *scroll_widget_; QString last_sender_; - QMap nick_colors_; }; #endif // HISTORY_VIEW_H diff --git a/include/HistoryViewManager.h b/include/HistoryViewManager.h index dcca609b..cdb20e98 100644 --- a/include/HistoryViewManager.h +++ b/include/HistoryViewManager.h @@ -38,6 +38,10 @@ public: void sync(const Rooms &rooms); void clearAll(); + static QString chooseRandomColor(); + static QMap NICK_COLORS; + static const QList COLORS; + public slots: void setHistoryView(const RoomInfo &info); diff --git a/src/HistoryView.cc b/src/HistoryView.cc index 3692c493..ce2e2f6c 100644 --- a/src/HistoryView.cc +++ b/src/HistoryView.cc @@ -15,8 +15,6 @@ * along with this program. If not, see . */ -#include - #include #include #include @@ -24,31 +22,7 @@ #include "HistoryView.h" #include "HistoryViewItem.h" - -const QList HistoryView::COLORS({"#FFF46E", - "#A58BFF", - "#50C9BA", - "#9EE6CF", - "#FFDD67", - "#2980B9", - "#FC993C", - "#2772DB", - "#CB8589", - "#DDE8B9", - "#55A44E", - "#A9EEE6", - "#53B759", - "#9E3997", - "#5D89D5", - "#BB86B7", - "#50a0cf", - "#3C989F", - "#5A4592", - "#235e5b", - "#d58247", - "#e0a729", - "#a2b636", - "#4BBE2E"}); +#include "HistoryViewManager.h" HistoryView::HistoryView(const QList &events, QWidget *parent) : QWidget(parent) @@ -65,8 +39,6 @@ HistoryView::HistoryView(QWidget *parent) void HistoryView::clear() { - nick_colors_.clear(); - for (const auto msg : scroll_layout_->children()) msg->deleteLater(); } @@ -77,15 +49,6 @@ void HistoryView::sliderRangeChanged(int min, int max) scroll_area_->verticalScrollBar()->setValue(max); } -QString HistoryView::chooseRandomColor() -{ - std::random_device random_device; - std::mt19937 engine{random_device()}; - std::uniform_int_distribution dist(0, HistoryView::COLORS.size() - 1); - - return HistoryView::COLORS[dist(engine)]; -} - void HistoryView::addEvents(const QList &events) { for (const auto &event : events) { @@ -94,11 +57,11 @@ void HistoryView::addEvents(const QList &events) if (msg_type == "m.text" || msg_type == "m.notice") { auto with_sender = last_sender_ != event.sender(); - auto color = nick_colors_.value(event.sender()); + auto color = HistoryViewManager::NICK_COLORS.value(event.sender()); if (color.isEmpty()) { - color = chooseRandomColor(); - nick_colors_.insert(event.sender(), color); + color = HistoryViewManager::chooseRandomColor(); + HistoryViewManager::NICK_COLORS.insert(event.sender(), color); } addHistoryItem(event, color, with_sender); diff --git a/src/HistoryViewManager.cc b/src/HistoryViewManager.cc index 2091f98b..a91dcc4f 100644 --- a/src/HistoryViewManager.cc +++ b/src/HistoryViewManager.cc @@ -15,6 +15,8 @@ * along with this program. If not, see . */ +#include + #include #include #include @@ -39,7 +41,9 @@ HistoryViewManager::~HistoryViewManager() void HistoryViewManager::clearAll() { - for (const auto &view: views_) { + NICK_COLORS.clear(); + + for (const auto &view : views_) { view->clear(); removeWidget(view); view->deleteLater(); @@ -92,3 +96,39 @@ void HistoryViewManager::setHistoryView(const RoomInfo &info) setCurrentWidget(widget); } + +QMap HistoryViewManager::NICK_COLORS; + +const QList HistoryViewManager::COLORS({"#FFF46E", + "#A58BFF", + "#50C9BA", + "#9EE6CF", + "#FFDD67", + "#2980B9", + "#FC993C", + "#2772DB", + "#CB8589", + "#DDE8B9", + "#55A44E", + "#A9EEE6", + "#53B759", + "#9E3997", + "#5D89D5", + "#BB86B7", + "#50a0cf", + "#3C989F", + "#5A4592", + "#235e5b", + "#d58247", + "#e0a729", + "#a2b636", + "#4BBE2E"}); + +QString HistoryViewManager::chooseRandomColor() +{ + std::random_device random_device; + std::mt19937 engine{random_device()}; + std::uniform_int_distribution dist(0, HistoryViewManager::COLORS.size() - 1); + + return HistoryViewManager::COLORS[dist(engine)]; +}