X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fuisupport%2Fbufferview.cpp;h=ccea4e8cb8f12d1228ad44f91fa4a1493e3ab36d;hp=df6bd04fc844547f830afb266d6f77cf1755c404;hb=cdc6b6dd319564e94f4fdba47ec4c8feaa2cf8a2;hpb=199c1b1ed23c855e8c85809228145ca64c46b051 diff --git a/src/uisupport/bufferview.cpp b/src/uisupport/bufferview.cpp index df6bd04f..ccea4e8c 100644 --- a/src/uisupport/bufferview.cpp +++ b/src/uisupport/bufferview.cpp @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2005-08 by the Quassel Project * + * Copyright (C) 2005-09 by the Quassel Project * * devel@quassel-irc.org * * * * This program is free software; you can redistribute it and/or modify * @@ -35,48 +35,14 @@ #include "buffersettings.h" #include "buffersyncer.h" #include "client.h" +#include "contextmenuactionprovider.h" +#include "graphicalui.h" #include "iconloader.h" -#include "mappedselectionmodel.h" #include "network.h" #include "networkmodel.h" -#include "networkmodelactionprovider.h" -#include "quasselui.h" +#include "contextmenuactionprovider.h" #include "uisettings.h" -bool TristateDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) { - if(event->type() != QEvent::MouseButtonRelease) - return QStyledItemDelegate::editorEvent(event, model, option, index); - - if(!(model->flags(index) & Qt::ItemIsUserCheckable)) - return QStyledItemDelegate::editorEvent(event, model, option, index); - - QVariant value = index.data(Qt::CheckStateRole); - if(!value.isValid()) - return QStyledItemDelegate::editorEvent(event, model, option, index); - - QStyleOptionViewItemV4 viewOpt(option); - initStyleOption(&viewOpt, index); - - QRect checkRect = viewOpt.widget->style()->subElementRect(QStyle::SE_ItemViewItemCheckIndicator, &viewOpt, viewOpt.widget); - QMouseEvent *me = static_cast(event); - - if(me->button() != Qt::LeftButton || !checkRect.contains(me->pos())) - return QStyledItemDelegate::editorEvent(event, model, option, index); - - Qt::CheckState state = static_cast(value.toInt()); - if(state == Qt::Unchecked) - state = Qt::PartiallyChecked; - else if(state == Qt::PartiallyChecked) - state = Qt::Checked; - else - state = Qt::Unchecked; - model->setData(index, state, Qt::CheckStateRole); - return true; -} - - - - /***************************************** * The TreeView showing the Buffers *****************************************/ @@ -85,13 +51,13 @@ bool TristateDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, con BufferView::BufferView(QWidget *parent) : QTreeView(parent) { - connect(this, SIGNAL(collapsed(const QModelIndex &)), SLOT(on_collapse(const QModelIndex &))); - connect(this, SIGNAL(expanded(const QModelIndex &)), SLOT(on_expand(const QModelIndex &))); + connect(this, SIGNAL(collapsed(const QModelIndex &)), SLOT(storeExpandedState(const QModelIndex &))); + connect(this, SIGNAL(expanded(const QModelIndex &)), SLOT(storeExpandedState(const QModelIndex &))); setSelectionMode(QAbstractItemView::ExtendedSelection); QAbstractItemDelegate *oldDelegate = itemDelegate(); - TristateDelegate *tristateDelegate = new TristateDelegate(this); + BufferViewDelegate *tristateDelegate = new BufferViewDelegate(this); setItemDelegate(tristateDelegate); delete oldDelegate; } @@ -100,6 +66,7 @@ void BufferView::init() { header()->setContextMenuPolicy(Qt::ActionsContextMenu); hideColumn(1); hideColumn(2); + setIndentation(10); expandAll(); setAnimated(true); @@ -333,38 +300,49 @@ void BufferView::on_configChanged() { continue; update(networkIdx); - - bool expandNetwork = false; - if(_expandedState.contains(networkId)) - expandNetwork = _expandedState[networkId]; - else - expandNetwork = model()->data(networkIdx, NetworkModel::ItemActiveRole).toBool(); - - if(expandNetwork) - expand(networkIdx); - else - collapse(networkIdx); + setExpandedState(networkIdx); } - // update selection to current one - MappedSelectionModel *mappedSelectionModel = qobject_cast(selectionModel()); - if(!config() || !mappedSelectionModel) - return; + if(config()) { + // update selection to current one + Client::bufferModel()->synchronizeView(this); + } - mappedSelectionModel->mappedSetCurrentIndex(Client::bufferModel()->standardSelectionModel()->currentIndex(), QItemSelectionModel::Current); - mappedSelectionModel->mappedSelect(Client::bufferModel()->standardSelectionModel()->selection(), QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows); + return; } -void BufferView::on_collapse(const QModelIndex &index) { - storeExpandedState(index.data(NetworkModel::NetworkIdRole).value(), false); -} +void BufferView::storeExpandedState(const QModelIndex &networkIdx) { + NetworkId networkId = model()->data(networkIdx, NetworkModel::NetworkIdRole).value(); + + int oldState = 0; + if(isExpanded(networkIdx)) + oldState |= WasExpanded; + if(model()->data(networkIdx, NetworkModel::ItemActiveRole).toBool()) + oldState |= WasActive; -void BufferView::on_expand(const QModelIndex &index) { - storeExpandedState(index.data(NetworkModel::NetworkIdRole).value(), true); + _expandedState[networkId] = oldState; } -void BufferView::storeExpandedState(NetworkId networkId, bool expanded) { - _expandedState[networkId] = expanded; +void BufferView::setExpandedState(const QModelIndex &networkIdx) { + if(model()->data(networkIdx, NetworkModel::ItemTypeRole) != NetworkModel::NetworkItemType) + return; + + if(model()->rowCount(networkIdx) == 0) + return; + + NetworkId networkId = model()->data(networkIdx, NetworkModel::NetworkIdRole).value(); + + bool networkActive = model()->data(networkIdx, NetworkModel::ItemActiveRole).toBool(); + bool expandNetwork = networkActive; + if(_expandedState.contains(networkId)) { + int oldState = _expandedState[networkId]; + if((bool)(oldState & WasActive) == networkActive) + expandNetwork = (bool)(oldState & WasExpanded); + } + + storeExpandedState(networkIdx); // this call is needed to keep track of the isActive state + if(expandNetwork != isExpanded(networkIdx)) + setExpanded(networkIdx, expandNetwork); } void BufferView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) { @@ -377,15 +355,7 @@ void BufferView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bott for(int i = topLeft.row(); i <= bottomRight.row(); i++) { QModelIndex networkIdx = topLeft.sibling(i, 0); - if(model()->rowCount(networkIdx) == 0) - continue; - - bool isActive = networkIdx.data(NetworkModel::ItemActiveRole).toBool(); -#ifdef SPUTDEV - if(isExpanded(networkIdx) != isActive) setExpanded(networkIdx, true); -#else - if(isExpanded(networkIdx) != isActive) setExpanded(networkIdx, isActive); -#endif + setExpandedState(networkIdx); } } @@ -412,7 +382,12 @@ void BufferView::contextMenuEvent(QContextMenuEvent *event) { } void BufferView::addActionsToMenu(QMenu *contextMenu, const QModelIndex &index) { - Client::mainUi()->actionProvider()->addActions(contextMenu, index, this, "menuActionTriggered", (bool)config()); + QModelIndexList indexList = selectedIndexes(); + // make sure the item we clicked on is first + indexList.removeAll(index); + indexList.prepend(index); + + GraphicalUi::contextMenuActionProvider()->addActions(contextMenu, indexList, this, "menuActionTriggered", (bool)config()); } void BufferView::addFilterActions(QMenu *contextMenu, const QModelIndex &index) { @@ -429,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: @@ -486,6 +461,110 @@ QSize BufferView::sizeHint() const { return QSize(columnSize, 50); } + +// **************************************** +// BufferViewDelgate +// **************************************** +class ColorsChangedEvent : public QEvent { +public: + ColorsChangedEvent() : QEvent(QEvent::User) {}; +}; + +BufferViewDelegate::BufferViewDelegate(QObject *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(); + _FgColorNoActivity = s.value("noActivityFG", QVariant(QColor(Qt::black))).value(); + _FgColorHighlightActivity = s.value("highlightActivityFG", QVariant(QColor(Qt::magenta))).value(); + _FgColorNewMessageActivity = s.value("newMessageActivityFG", QVariant(QColor(Qt::green))).value(); + _FgColorOtherActivity = s.value("otherActivityFG", QVariant(QColor(Qt::darkGreen))).value(); +} + +bool BufferViewDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) { + if(event->type() != QEvent::MouseButtonRelease) + return QStyledItemDelegate::editorEvent(event, model, option, index); + + if(!(model->flags(index) & Qt::ItemIsUserCheckable)) + return QStyledItemDelegate::editorEvent(event, model, option, index); + + QVariant value = index.data(Qt::CheckStateRole); + if(!value.isValid()) + return QStyledItemDelegate::editorEvent(event, model, option, index); + + QStyleOptionViewItemV4 viewOpt(option); + initStyleOption(&viewOpt, index); + + QRect checkRect = viewOpt.widget->style()->subElementRect(QStyle::SE_ItemViewItemCheckIndicator, &viewOpt, viewOpt.widget); + QMouseEvent *me = static_cast(event); + + if(me->button() != Qt::LeftButton || !checkRect.contains(me->pos())) + return QStyledItemDelegate::editorEvent(event, model, option, index); + + Qt::CheckState state = static_cast(value.toInt()); + if(state == Qt::Unchecked) + state = Qt::PartiallyChecked; + else if(state == Qt::PartiallyChecked) + state = Qt::Checked; + else + state = Qt::Unchecked; + model->setData(index, state, Qt::CheckStateRole); + return true; +} + +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) { + 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; + } + + option->palette.setColor(QPalette::Text, fgColor); +} + + // ============================== // BufferView Dock // ============================== @@ -498,14 +577,6 @@ BufferViewDock::BufferViewDock(BufferViewConfig *config, QWidget *parent) connect(config, SIGNAL(bufferViewNameSet(const QString &)), this, SLOT(bufferViewRenamed(const QString &))); } -BufferViewDock::BufferViewDock(QWidget *parent) - : QDockWidget(tr("All Buffers"), parent) -{ - setObjectName("BufferViewDock--1"); - toggleViewAction()->setData((int)-1); - setAllowedAreas(Qt::RightDockWidgetArea|Qt::LeftDockWidgetArea); -} - void BufferViewDock::bufferViewRenamed(const QString &newName) { setWindowTitle(newName); toggleViewAction()->setText(newName);