1a1c4bbaf9b966ee6732fb8dd11f487b109478cd
[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   setIndentation(10);
52   setAnimated(true);
53   header()->hide();
54   setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
55   setSortingEnabled(true);
56   sortByColumn(0, Qt::AscendingOrder);
57
58   setContextMenuPolicy(Qt::CustomContextMenu);
59   setSelectionMode(QAbstractItemView::ExtendedSelection);
60
61   connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), SLOT(showContextMenu(const QPoint&)));
62
63 #if defined Q_WS_QWS || defined Q_WS_X11
64   connect(this, SIGNAL(doubleClicked(QModelIndex)), SLOT(startQuery(QModelIndex)));
65 #else
66   // afaik this is better on Mac and Windows
67   connect(this, SIGNAL(activated(QModelIndex)), SLOT(startQuery(QModelIndex)));
68 #endif
69 }
70
71 void NickView::init() {
72   if(!model())
73     return;
74
75   for(int i = 1; i < model()->columnCount(); i++)
76     setColumnHidden(i, true);
77
78   connect(selectionModel(), SIGNAL(currentChanged(QModelIndex, QModelIndex)), SIGNAL(selectionUpdated()));
79   connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)), SIGNAL(selectionUpdated()));
80 }
81
82 void NickView::setModel(QAbstractItemModel *model_) {
83   if(model())
84     disconnect(model(), 0, this, 0);
85
86   QTreeView::setModel(model_);
87   init();
88 }
89
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);
94   }
95 }
96
97 void NickView::setRootIndex(const QModelIndex &index) {
98   QAbstractItemView::setRootIndex(index);
99   if(index.isValid())
100     QCoreApplication::postEvent(this, new ExpandAllEvent);
101 }
102
103 QModelIndexList NickView::selectedIndexes() const {
104   QModelIndexList indexList = QTreeView::selectedIndexes();
105
106   // make sure the item we clicked on is first
107   Q_ASSERT(indexList.contains(currentIndex()));
108   indexList.removeAll(currentIndex());
109   indexList.prepend(currentIndex());
110
111   return indexList;
112 }
113
114 void NickView::showContextMenu(const QPoint &pos ) {
115   Q_UNUSED(pos);
116
117   QMenu contextMenu(this);
118   GraphicalUi::contextMenuActionProvider()->addActions(&contextMenu, selectedIndexes());
119   contextMenu.exec(QCursor::pos());
120 }
121
122 void NickView::startQuery(const QModelIndex &index) {
123   if(index.data(NetworkModel::ItemTypeRole) != NetworkModel::IrcUserItemType)
124     return;
125
126   IrcUser *ircUser = qobject_cast<IrcUser *>(index.data(NetworkModel::IrcUserRole).value<QObject *>());
127   NetworkId networkId = index.data(NetworkModel::NetworkIdRole).value<NetworkId>();
128   if(!ircUser || !networkId.isValid())
129     return;
130
131   BufferId bufId = Client::networkModel()->bufferId(networkId, ircUser->nick());
132   if(bufId.isValid())
133     Client::bufferModel()->switchToBuffer(bufId);
134   else
135     Client::userInput(index.data(NetworkModel::BufferInfoRole).value<BufferInfo>(), QString("/QUERY %1").arg(ircUser->nick()));
136 }
137
138 void NickView::customEvent(QEvent *event) {
139   // THIS IS A REPLACEMENT FOR expandAll()
140   /* WARNING: do not call expandAll()!
141    * it fucks up big time in combination with sorting and changing the rootIndex
142    * the following sequence of commands leads to unexpected behavior when inserting new items
143    * setSortingEnabled(true);
144    * setModel();
145    * expandAll();
146    * setRootIndex();
147    */
148   if(event->type() != QEvent::User)
149     return;
150
151   QModelIndex topLevelIdx;
152   for(int i = 0; i < model()->rowCount(rootIndex()); i++) {
153     topLevelIdx = model()->index(i, 0, rootIndex());
154     if(isExpanded(topLevelIdx))
155       continue;
156     else {
157       expand(topLevelIdx);
158       if(i < model()->rowCount(rootIndex()) - 1)
159         QCoreApplication::postEvent(this, new ExpandAllEvent);
160       break;
161     }
162   }
163   event->accept();
164 }
165
166
167 // ****************************************
168 //  NickViewDelgate
169 // ****************************************
170 NickViewDelegate::NickViewDelegate(QObject *parent)
171   : QStyledItemDelegate(parent)
172 {
173   UiSettings s("QtUiStyle/Colors");
174   _FgOnlineStatus = s.value("onlineStatusFG", QVariant(QColor(Qt::black))).value<QColor>();
175   _FgAwayStatus = s.value("awayStatusFG", QVariant(QColor(Qt::gray))).value<QColor>();
176 }
177
178 void NickViewDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const {
179   QStyledItemDelegate::initStyleOption(option, index);
180
181   if(!index.isValid())
182     return;
183
184   QColor fgColor = _FgOnlineStatus;
185   if(!index.data(NetworkModel::ItemActiveRole).toBool())
186     fgColor = _FgAwayStatus;
187
188   option->palette.setColor(QPalette::Text, fgColor);
189 }