fixes #531 - changing buffer view colors needs client restart
[quassel.git] / src / uisupport / bufferview.cpp
index 0b33869..ccea4e8 100644 (file)
 #include "buffersettings.h"
 #include "buffersyncer.h"
 #include "client.h"
+#include "contextmenuactionprovider.h"
+#include "graphicalui.h"
 #include "iconloader.h"
 #include "network.h"
 #include "networkmodel.h"
-#include "networkmodelactionprovider.h"
-#include "quasselui.h"
+#include "contextmenuactionprovider.h"
 #include "uisettings.h"
 
 /*****************************************
@@ -56,8 +57,8 @@ BufferView::BufferView(QWidget *parent)
   setSelectionMode(QAbstractItemView::ExtendedSelection);
 
   QAbstractItemDelegate *oldDelegate = itemDelegate();
-  BufferViewDelegate *newDelegate = new BufferViewDelegate(this);
-  setItemDelegate(newDelegate);
+  BufferViewDelegate *tristateDelegate = new BufferViewDelegate(this);
+  setItemDelegate(tristateDelegate);
   delete oldDelegate;
 }
 
@@ -386,7 +387,7 @@ void BufferView::addActionsToMenu(QMenu *contextMenu, const QModelIndex &index)
   indexList.removeAll(index);
   indexList.prepend(index);
 
-  Client::mainUi()->actionProvider()->addActions(contextMenu, indexList, this, "menuActionTriggered", (bool)config());
+  GraphicalUi::contextMenuActionProvider()->addActions(contextMenu, indexList, this, "menuActionTriggered", (bool)config());
 }
 
 void BufferView::addFilterActions(QMenu *contextMenu, const QModelIndex &index) {
@@ -403,12 +404,12 @@ void BufferView::addFilterActions(QMenu *contextMenu, const QModelIndex &index)
 }
 
 void BufferView::menuActionTriggered(QAction *result) {
-  NetworkModelActionProvider::ActionType type = (NetworkModelActionProvider::ActionType)result->data().toInt();
+  ContextMenuActionProvider::ActionType type = (ContextMenuActionProvider::ActionType)result->data().toInt();
   switch(type) {
-    case NetworkModelActionProvider::HideBufferTemporarily:
+    case ContextMenuActionProvider::HideBufferTemporarily:
       removeSelectedBuffers();
       break;
-    case NetworkModelActionProvider::HideBufferPermanently:
+    case ContextMenuActionProvider::HideBufferPermanently:
       removeSelectedBuffers(true);
       break;
     default:
@@ -464,10 +465,47 @@ QSize BufferView::sizeHint() const {
 // ****************************************
 //  BufferViewDelgate
 // ****************************************
+class ColorsChangedEvent : public QEvent {
+public:
+  ColorsChangedEvent() : QEvent(QEvent::User) {};
+};
+
 BufferViewDelegate::BufferViewDelegate(QObject *parent)
-  : QStyledItemDelegate(parent)
+  : QStyledItemDelegate(parent),
+    _updateColors(false)
 {
+  loadColors();
+
+  UiSettings s("QtUiStyle/Colors");
+  s.notify("inactiveActivityFG", this, SLOT(colorsChanged()));
+  s.notify("noActivityFG", this, SLOT(colorsChanged()));
+  s.notify("highlightActivityFG", this, SLOT(colorsChanged()));
+  s.notify("newMessageActivityFG", this, SLOT(colorsChanged()));
+  s.notify("otherActivityFG", this, SLOT(colorsChanged()));
+}
+
+void BufferViewDelegate::colorsChanged() {
+  // avoid mutliple unneded reloads of all colors
+  if(_updateColors)
+    return;
+  _updateColors = true;
+  QCoreApplication::postEvent(this, new ColorsChangedEvent());
+}
+
+void BufferViewDelegate::customEvent(QEvent *event) {
+  if(event->type() != QEvent::User)
+    return;
+
+  loadColors();
+  _updateColors = false;
+
+  event->accept();
+}
+
+void BufferViewDelegate::loadColors() {
   UiSettings s("QtUiStyle/Colors");
+  _FgColorInactiveActivity = s.value("inactiveActivityFG", QVariant(QColor(Qt::gray))).value<QColor>();
+  _FgColorNoActivity = s.value("noActivityFG", QVariant(QColor(Qt::black))).value<QColor>();
   _FgColorHighlightActivity = s.value("highlightActivityFG", QVariant(QColor(Qt::magenta))).value<QColor>();
   _FgColorNewMessageActivity = s.value("newMessageActivityFG", QVariant(QColor(Qt::green))).value<QColor>();
   _FgColorOtherActivity = s.value("otherActivityFG", QVariant(QColor(Qt::darkGreen))).value<QColor>();
@@ -507,24 +545,23 @@ bool BufferViewDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, c
 void BufferViewDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const {
   QStyledItemDelegate::initStyleOption(option, index);
 
+  if(!index.isValid())
+    return;
+
   BufferInfo::ActivityLevel activity = (BufferInfo::ActivityLevel)index.data(NetworkModel::BufferActivityRole).toInt();
 
+  QColor fgColor = _FgColorNoActivity;
   if(activity & BufferInfo::Highlight) {
-    option->palette.setColor(QPalette::Text, _FgColorHighlightActivity);
-    return;
-  }
-  if(activity & BufferInfo::NewMessage) {
-    option->palette.setColor(QPalette::Text, _FgColorNewMessageActivity);
-    return;
-  }
-  if(activity & BufferInfo::OtherActivity) {
-    option->palette.setColor(QPalette::Text, _FgColorOtherActivity);
-    return;
+    fgColor = _FgColorHighlightActivity;
+  } else if(activity & BufferInfo::NewMessage) {
+    fgColor = _FgColorNewMessageActivity;
+  } else if(activity & BufferInfo::OtherActivity) {
+    fgColor = _FgColorOtherActivity;
+  } else if(!index.data(NetworkModel::ItemActiveRole).toBool() || index.data(NetworkModel::UserAwayRole).toBool()) {
+    fgColor = _FgColorInactiveActivity;
   }
 
-  if(!index.data(NetworkModel::ItemActiveRole).toBool() || index.data(NetworkModel::UserAwayRole).toBool()) {
-    option->palette.setColor(QPalette::Text, QPalette().color(QPalette::Dark));
-  }
+  option->palette.setColor(QPalette::Text, fgColor);
 }