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