Use a single nick to color map

This commit is contained in:
Konstantinos Sideris 2017-04-11 22:48:02 +03:00
parent 5c964f3246
commit c431eee401
4 changed files with 49 additions and 47 deletions

View file

@ -44,12 +44,8 @@ public slots:
void sliderRangeChanged(int min, int max); void sliderRangeChanged(int min, int max);
private: private:
static const QList<QString> COLORS;
void init(); void init();
QString chooseRandomColor();
QVBoxLayout *top_layout_; QVBoxLayout *top_layout_;
QVBoxLayout *scroll_layout_; QVBoxLayout *scroll_layout_;
@ -57,7 +53,6 @@ private:
QWidget *scroll_widget_; QWidget *scroll_widget_;
QString last_sender_; QString last_sender_;
QMap<QString, QString> nick_colors_;
}; };
#endif // HISTORY_VIEW_H #endif // HISTORY_VIEW_H

View file

@ -38,6 +38,10 @@ public:
void sync(const Rooms &rooms); void sync(const Rooms &rooms);
void clearAll(); void clearAll();
static QString chooseRandomColor();
static QMap<QString, QString> NICK_COLORS;
static const QList<QString> COLORS;
public slots: public slots:
void setHistoryView(const RoomInfo &info); void setHistoryView(const RoomInfo &info);

View file

@ -15,8 +15,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <random>
#include <QDebug> #include <QDebug>
#include <QScrollBar> #include <QScrollBar>
#include <QtWidgets/QLabel> #include <QtWidgets/QLabel>
@ -24,31 +22,7 @@
#include "HistoryView.h" #include "HistoryView.h"
#include "HistoryViewItem.h" #include "HistoryViewItem.h"
#include "HistoryViewManager.h"
const QList<QString> 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"});
HistoryView::HistoryView(const QList<Event> &events, QWidget *parent) HistoryView::HistoryView(const QList<Event> &events, QWidget *parent)
: QWidget(parent) : QWidget(parent)
@ -65,8 +39,6 @@ HistoryView::HistoryView(QWidget *parent)
void HistoryView::clear() void HistoryView::clear()
{ {
nick_colors_.clear();
for (const auto msg : scroll_layout_->children()) for (const auto msg : scroll_layout_->children())
msg->deleteLater(); msg->deleteLater();
} }
@ -77,15 +49,6 @@ void HistoryView::sliderRangeChanged(int min, int max)
scroll_area_->verticalScrollBar()->setValue(max); scroll_area_->verticalScrollBar()->setValue(max);
} }
QString HistoryView::chooseRandomColor()
{
std::random_device random_device;
std::mt19937 engine{random_device()};
std::uniform_int_distribution<int> dist(0, HistoryView::COLORS.size() - 1);
return HistoryView::COLORS[dist(engine)];
}
void HistoryView::addEvents(const QList<Event> &events) void HistoryView::addEvents(const QList<Event> &events)
{ {
for (const auto &event : events) { for (const auto &event : events) {
@ -94,11 +57,11 @@ void HistoryView::addEvents(const QList<Event> &events)
if (msg_type == "m.text" || msg_type == "m.notice") { if (msg_type == "m.text" || msg_type == "m.notice") {
auto with_sender = last_sender_ != event.sender(); 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()) { if (color.isEmpty()) {
color = chooseRandomColor(); color = HistoryViewManager::chooseRandomColor();
nick_colors_.insert(event.sender(), color); HistoryViewManager::NICK_COLORS.insert(event.sender(), color);
} }
addHistoryItem(event, color, with_sender); addHistoryItem(event, color, with_sender);

View file

@ -15,6 +15,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <random>
#include <QDebug> #include <QDebug>
#include <QStackedWidget> #include <QStackedWidget>
#include <QWidget> #include <QWidget>
@ -39,6 +41,8 @@ HistoryViewManager::~HistoryViewManager()
void HistoryViewManager::clearAll() void HistoryViewManager::clearAll()
{ {
NICK_COLORS.clear();
for (const auto &view : views_) { for (const auto &view : views_) {
view->clear(); view->clear();
removeWidget(view); removeWidget(view);
@ -92,3 +96,39 @@ void HistoryViewManager::setHistoryView(const RoomInfo &info)
setCurrentWidget(widget); setCurrentWidget(widget);
} }
QMap<QString, QString> HistoryViewManager::NICK_COLORS;
const QList<QString> 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<int> dist(0, HistoryViewManager::COLORS.size() - 1);
return HistoryViewManager::COLORS[dist(engine)];
}