Introduce GraphicalUi (between QtUi and AbstractUi), rename NetworkModelActionProvider
[quassel.git] / src / uisupport / nickview.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include "nickview.h"
22
23 #include <QHeaderView>
24 #include <QScrollBar>
25 #include <QDebug>
26 #include <QMenu>
27
28 #include "buffermodel.h"
29 #include "client.h"
30 #include "contextmenuactionprovider.h"
31 #include "graphicalui.h"
32 #include "nickview.h"
33 #include "nickviewfilter.h"
34 #include "networkmodel.h"
35 #include "types.h"
36 #include "uisettings.h"
37
38 class ExpandAllEvent : public QEvent {
39 public:
40   ExpandAllEvent() : QEvent(QEvent::User) {}
41 };
42
43 NickView::NickView(QWidget *parent)
44   : QTreeView(parent)
45 {
46   QAbstractItemDelegate *oldDelegate = itemDelegate();
47   NickViewDelegate *newDelegate = new NickViewDelegate(this);
48   setItemDelegate(newDelegate);
49   delete oldDelegate;
50
51   
52   setIndentation(10);
53   setAnimated(true);
54   header()->hide();
55   setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
56   setSortingEnabled(true);
57   sortByColumn(0, Qt::AscendingOrder);
58
59   setContextMenuPolicy(Qt::CustomContextMenu);
60   setSelectionMode(QAbstractItemView::ExtendedSelection);
61
62   connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), SLOT(showContextMenu(const QPoint&)));
63
64 #if defined Q_WS_QWS || defined Q_WS_X11
65   connect(this, SIGNAL(doubleClicked(QModelIndex)), SLOT(startQuery(QModelIndex)));
66 #else
67   // afaik this is better on Mac and Windows
68   connect(this, SIGNAL(activated(QModelIndex)), SLOT(startQuery(QModelIndex)));
69 #endif
70 }
71
72 void NickView::init() {
73   if(!model())
74     return;
75
76   for(int i = 1; i < model()->columnCount(); i++)
77     setColumnHidden(i, true);
78 }
79
80 void NickView::setModel(QAbstractItemModel *model) {
81   QTreeView::setModel(model);
82   init();
83 }
84
85 void NickView::rowsInserted(const QModelIndex &parent, int start, int end) {
86   QTreeView::rowsInserted(parent, start, end);
87   if(model()->data(parent, NetworkModel::ItemTypeRole) == NetworkModel::UserCategoryItemType && !isExpanded(parent)) {
88     QCoreApplication::postEvent(this, new ExpandAllEvent);
89   }
90 }
91
92 void NickView::setRootIndex(const QModelIndex &index) {
93   QAbstractItemView::setRootIndex(index);
94   if(index.isValid())
95     QCoreApplication::postEvent(this, new ExpandAllEvent);
96 }
97
98 void NickView::showContextMenu(const QPoint & pos ) {
99   QModelIndex index = indexAt(pos);
100   if(index.data(NetworkModel::ItemTypeRole) != NetworkModel::IrcUserItemType)
101     return;
102
103   QModelIndexList indexList = selectedIndexes();
104   // make sure the item we clicked on is first
105   indexList.removeAll(index);
106   indexList.prepend(index);
107
108   QMenu contextMenu(this);
109   GraphicalUi::contextMenuActionProvider()->addActions(&contextMenu, indexList);
110   contextMenu.exec(QCursor::pos());
111 }
112
113 void NickView::startQuery(const QModelIndex &index) {
114   if(index.data(NetworkModel::ItemTypeRole) != NetworkModel::IrcUserItemType)
115     return;
116
117   IrcUser *ircUser = qobject_cast<IrcUser *>(index.data(NetworkModel::IrcUserRole).value<QObject *>());
118   NetworkId networkId = index.data(NetworkModel::NetworkIdRole).value<NetworkId>();
119   if(!ircUser || !networkId.isValid())
120     return;
121
122   BufferId bufId = Client::networkModel()->bufferId(networkId, ircUser->nick());
123   if(bufId.isValid())
124     Client::bufferModel()->switchToBuffer(bufId);
125   else
126     Client::userInput(index.data(NetworkModel::BufferInfoRole).value<BufferInfo>(), QString("/QUERY %1").arg(ircUser->nick()));
127 }
128
129 void NickView::customEvent(QEvent *event) {
130   // THIS IS A REPLACEMENT FOR expandAll()
131   /* WARNING: do not call expandAll()!
132    * it fucks up big time in combination with sorting and changing the rootIndex
133    * the following sequence of commands leads to unexpected behavior when inserting new items
134    * setSortingEnabled(true);
135    * setModel();
136    * expandAll();
137    * setRootIndex();
138    */
139   if(event->type() != QEvent::User)
140     return;
141
142   QModelIndex topLevelIdx;
143   for(int i = 0; i < model()->rowCount(rootIndex()); i++) {
144     topLevelIdx = model()->index(i, 0, rootIndex());
145     if(isExpanded(topLevelIdx))
146       continue;
147     else {
148       expand(topLevelIdx);
149       if(i < model()->rowCount(rootIndex()) - 1)
150         QCoreApplication::postEvent(this, new ExpandAllEvent);
151       break;
152     }
153   }
154   event->accept();
155 }
156
157
158 // ****************************************
159 //  NickViewDelgate
160 // ****************************************
161 NickViewDelegate::NickViewDelegate(QObject *parent)
162   : QStyledItemDelegate(parent)
163 {
164   UiSettings s("QtUiStyle/Colors");
165   _FgOnlineStatus = s.value("onlineStatusFG", QVariant(QColor(Qt::black))).value<QColor>();
166   _FgAwayStatus = s.value("awayStatusFG", QVariant(QColor(Qt::gray))).value<QColor>();
167 }
168
169 void NickViewDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const {
170   QStyledItemDelegate::initStyleOption(option, index);
171
172   if(!index.isValid())
173     return;
174
175   QColor fgColor = _FgOnlineStatus;
176   if(!index.data(NetworkModel::ItemActiveRole).toBool())
177     fgColor = _FgAwayStatus;
178
179   option->palette.setColor(QPalette::Text, fgColor);
180 }