1 /***************************************************************************
2 * Copyright (C) 2005-09 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
23 #include <QHeaderView>
28 #include "buffermodel.h"
30 #include "contextmenuactionprovider.h"
31 #include "graphicalui.h"
33 #include "nickviewfilter.h"
34 #include "networkmodel.h"
36 #include "uisettings.h"
38 class ExpandAllEvent : public QEvent {
40 ExpandAllEvent() : QEvent(QEvent::User) {}
43 NickView::NickView(QWidget *parent)
46 QAbstractItemDelegate *oldDelegate = itemDelegate();
47 NickViewDelegate *newDelegate = new NickViewDelegate(this);
48 setItemDelegate(newDelegate);
54 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
55 setSortingEnabled(true);
56 sortByColumn(0, Qt::AscendingOrder);
58 setContextMenuPolicy(Qt::CustomContextMenu);
59 setSelectionMode(QAbstractItemView::ExtendedSelection);
61 connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), SLOT(showContextMenu(const QPoint&)));
63 #if defined Q_WS_QWS || defined Q_WS_X11
64 connect(this, SIGNAL(doubleClicked(QModelIndex)), SLOT(startQuery(QModelIndex)));
66 // afaik this is better on Mac and Windows
67 connect(this, SIGNAL(activated(QModelIndex)), SLOT(startQuery(QModelIndex)));
71 void NickView::init() {
75 for(int i = 1; i < model()->columnCount(); i++)
76 setColumnHidden(i, true);
78 connect(selectionModel(), SIGNAL(currentChanged(QModelIndex, QModelIndex)), SIGNAL(selectionUpdated()));
79 connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)), SIGNAL(selectionUpdated()));
82 void NickView::setModel(QAbstractItemModel *model_) {
84 disconnect(model(), 0, this, 0);
86 QTreeView::setModel(model_);
90 void NickView::rowsInserted(const QModelIndex &parent, int start, int end) {
91 QTreeView::rowsInserted(parent, start, end);
92 if(model()->data(parent, NetworkModel::ItemTypeRole) == NetworkModel::UserCategoryItemType && !isExpanded(parent)) {
93 QCoreApplication::postEvent(this, new ExpandAllEvent);
97 void NickView::setRootIndex(const QModelIndex &index) {
98 QAbstractItemView::setRootIndex(index);
100 QCoreApplication::postEvent(this, new ExpandAllEvent);
103 QModelIndexList NickView::selectedIndexes() const {
104 QModelIndexList indexList = QTreeView::selectedIndexes();
106 // make sure the item we clicked on is first
107 if(indexList.contains(currentIndex())) {
108 indexList.removeAll(currentIndex());
109 indexList.prepend(currentIndex());
115 void NickView::showContextMenu(const QPoint &pos ) {
118 QMenu contextMenu(this);
119 GraphicalUi::contextMenuActionProvider()->addActions(&contextMenu, selectedIndexes());
120 contextMenu.exec(QCursor::pos());
123 void NickView::startQuery(const QModelIndex &index) {
124 if(index.data(NetworkModel::ItemTypeRole) != NetworkModel::IrcUserItemType)
127 IrcUser *ircUser = qobject_cast<IrcUser *>(index.data(NetworkModel::IrcUserRole).value<QObject *>());
128 NetworkId networkId = index.data(NetworkModel::NetworkIdRole).value<NetworkId>();
129 if(!ircUser || !networkId.isValid())
132 BufferId bufId = Client::networkModel()->bufferId(networkId, ircUser->nick());
134 Client::bufferModel()->switchToBuffer(bufId);
136 Client::userInput(index.data(NetworkModel::BufferInfoRole).value<BufferInfo>(), QString("/QUERY %1").arg(ircUser->nick()));
139 void NickView::customEvent(QEvent *event) {
140 // THIS IS A REPLACEMENT FOR expandAll()
141 /* WARNING: do not call expandAll()!
142 * it fucks up big time in combination with sorting and changing the rootIndex
143 * the following sequence of commands leads to unexpected behavior when inserting new items
144 * setSortingEnabled(true);
149 if(event->type() != QEvent::User)
152 QModelIndex topLevelIdx;
153 for(int i = 0; i < model()->rowCount(rootIndex()); i++) {
154 topLevelIdx = model()->index(i, 0, rootIndex());
155 if(isExpanded(topLevelIdx))
159 if(i < model()->rowCount(rootIndex()) - 1)
160 QCoreApplication::postEvent(this, new ExpandAllEvent);
168 // ****************************************
170 // ****************************************
171 NickViewDelegate::NickViewDelegate(QObject *parent)
172 : QStyledItemDelegate(parent)
174 UiSettings s("QtUiStyle/Colors");
175 _FgOnlineStatus = s.value("onlineStatusFG", QVariant(QColor(Qt::black))).value<QColor>();
176 _FgAwayStatus = s.value("awayStatusFG", QVariant(QColor(Qt::gray))).value<QColor>();
179 void NickViewDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const {
180 QStyledItemDelegate::initStyleOption(option, index);
185 QColor fgColor = _FgOnlineStatus;
186 if(!index.data(NetworkModel::ItemActiveRole).toBool())
187 fgColor = _FgAwayStatus;
189 option->palette.setColor(QPalette::Text, fgColor);