34fb554ec39bde4ac6915013e77af92acd4a77bc
[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 <QApplication>
24 #include <QHeaderView>
25 #include <QScrollBar>
26 #include <QDebug>
27 #include <QMenu>
28
29 #include "buffermodel.h"
30 #include "client.h"
31 #include "contextmenuactionprovider.h"
32 #include "graphicalui.h"
33 #include "nickview.h"
34 #include "nickviewfilter.h"
35 #include "networkmodel.h"
36 #include "types.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   setIndentation(10);
47   header()->hide();
48   setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
49   setSortingEnabled(true);
50   sortByColumn(0, Qt::AscendingOrder);
51
52   setContextMenuPolicy(Qt::CustomContextMenu);
53   setSelectionMode(QAbstractItemView::ExtendedSelection);
54
55   // breaks with Qt 4.8
56   if(QString("4.8.0") > qVersion()) // FIXME breaks with Qt versions >= 4.10!
57     setAnimated(true);
58
59   connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), SLOT(showContextMenu(const QPoint&)));
60
61 #if defined Q_WS_QWS || defined Q_WS_X11
62   connect(this, SIGNAL(doubleClicked(QModelIndex)), SLOT(startQuery(QModelIndex)));
63 #else
64   // afaik this is better on Mac and Windows
65   connect(this, SIGNAL(activated(QModelIndex)), SLOT(startQuery(QModelIndex)));
66 #endif
67 }
68
69 void NickView::init() {
70   if(!model())
71     return;
72
73   for(int i = 1; i < model()->columnCount(); i++)
74     setColumnHidden(i, true);
75
76   connect(selectionModel(), SIGNAL(currentChanged(QModelIndex, QModelIndex)), SIGNAL(selectionUpdated()));
77   connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)), SIGNAL(selectionUpdated()));
78 }
79
80 void NickView::setModel(QAbstractItemModel *model_) {
81   if(model())
82     disconnect(model(), 0, this, 0);
83
84   QTreeView::setModel(model_);
85   init();
86 }
87
88 void NickView::rowsInserted(const QModelIndex &parent, int start, int end) {
89   QTreeView::rowsInserted(parent, start, end);
90   if(model()->data(parent, NetworkModel::ItemTypeRole) == NetworkModel::UserCategoryItemType && !isExpanded(parent)) {
91     QCoreApplication::postEvent(this, new ExpandAllEvent);
92   }
93 }
94
95 void NickView::setRootIndex(const QModelIndex &index) {
96   QAbstractItemView::setRootIndex(index);
97   if(index.isValid())
98     QCoreApplication::postEvent(this, new ExpandAllEvent);
99 }
100
101 QModelIndexList NickView::selectedIndexes() const {
102   QModelIndexList indexList = QTreeView::selectedIndexes();
103
104   // make sure the item we clicked on is first
105   if(indexList.contains(currentIndex())) {
106     indexList.removeAll(currentIndex());
107     indexList.prepend(currentIndex());
108   }
109
110   return indexList;
111 }
112
113 void NickView::showContextMenu(const QPoint &pos ) {
114   Q_UNUSED(pos);
115
116   QMenu contextMenu(this);
117   GraphicalUi::contextMenuActionProvider()->addActions(&contextMenu, selectedIndexes());
118   contextMenu.exec(QCursor::pos());
119 }
120
121 void NickView::startQuery(const QModelIndex &index) {
122   if(index.data(NetworkModel::ItemTypeRole) != NetworkModel::IrcUserItemType)
123     return;
124
125   IrcUser *ircUser = qobject_cast<IrcUser *>(index.data(NetworkModel::IrcUserRole).value<QObject *>());
126   NetworkId networkId = index.data(NetworkModel::NetworkIdRole).value<NetworkId>();
127   if(!ircUser || !networkId.isValid())
128     return;
129
130   Client::bufferModel()->switchToOrStartQuery(networkId, ircUser->nick());
131 }
132
133 void NickView::customEvent(QEvent *event) {
134   // THIS IS A REPLACEMENT FOR expandAll()
135   /* WARNING: do not call expandAll()!
136    * it fucks up big time in combination with sorting and changing the rootIndex
137    * the following sequence of commands leads to unexpected behavior when inserting new items
138    * setSortingEnabled(true);
139    * setModel();
140    * expandAll();
141    * setRootIndex();
142    */
143   if(event->type() != QEvent::User)
144     return;
145
146   if(!model())
147     return;
148
149   QModelIndex topLevelIdx;
150   for(int i = 0; i < model()->rowCount(rootIndex()); i++) {
151     topLevelIdx = model()->index(i, 0, rootIndex());
152     if(isExpanded(topLevelIdx))
153       continue;
154     else {
155       expand(topLevelIdx);
156       if(i < model()->rowCount(rootIndex()) - 1)
157         QCoreApplication::postEvent(this, new ExpandAllEvent);
158       break;
159     }
160   }
161   event->accept();
162 }