Meh, apparently MSVC doesn't accept 'or' in preprocessor defines either :P
[quassel.git] / src / uisupport / nickview.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 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
51   connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), SLOT(showContextMenu(const QPoint&)));
52
53 #if defined Q_WS_QWS || defined Q_WS_X11
54   connect(this, SIGNAL(doubleClicked(QModelIndex)), SLOT(startQuery(QModelIndex)));
55 #else
56   // afaik this is better on Mac and Windows
57   connect(this, SIGNAL(activated(QModelIndex)), SLOT(startQuery(QModelIndex)));
58 #endif
59 }
60
61 void NickView::init() {
62   if(!model())
63     return;
64
65   for(int i = 1; i < model()->columnCount(); i++)
66     setColumnHidden(i, true);
67 }
68
69 void NickView::setModel(QAbstractItemModel *model) {
70   QTreeView::setModel(model);
71   init();
72 }
73
74 void NickView::rowsInserted(const QModelIndex &parent, int start, int end) {
75   QTreeView::rowsInserted(parent, start, end);
76   if(model()->data(parent, NetworkModel::ItemTypeRole) == NetworkModel::UserCategoryItemType && !isExpanded(parent)) {
77     QCoreApplication::postEvent(this, new ExpandAllEvent);
78   }
79 }
80
81 void NickView::setRootIndex(const QModelIndex &index) {
82   QAbstractItemView::setRootIndex(index);
83   if(index.isValid())
84     QCoreApplication::postEvent(this, new ExpandAllEvent);
85 }
86
87 void NickView::showContextMenu(const QPoint & pos ) {
88   QModelIndex index = indexAt(pos);
89   if(index.data(NetworkModel::ItemTypeRole) != NetworkModel::IrcUserItemType)
90     return;
91
92   QMenu contextMenu(this);
93   Client::mainUi()->actionProvider()->addActions(&contextMenu, index);
94   contextMenu.exec(QCursor::pos());
95 }
96
97 void NickView::startQuery(const QModelIndex &index) {
98   if(index.data(NetworkModel::ItemTypeRole) != NetworkModel::IrcUserItemType)
99     return;
100
101   IrcUser *ircUser = qobject_cast<IrcUser *>(index.data(NetworkModel::IrcUserRole).value<QObject *>());
102   NetworkId networkId = index.data(NetworkModel::NetworkIdRole).value<NetworkId>();
103   if(!ircUser || !networkId.isValid())
104     return;
105
106   BufferId bufId = Client::networkModel()->bufferId(networkId, ircUser->nick());
107   if(bufId.isValid())
108     Client::bufferModel()->switchToBuffer(bufId);
109   else
110     Client::userInput(index.data(NetworkModel::BufferInfoRole).value<BufferInfo>(), QString("/QUERY %1").arg(ircUser->nick()));
111 }
112
113 void NickView::customEvent(QEvent *event) {
114   // THIS IS A REPLACEMENT FOR expandAll()
115   /* WARNING: do not call expandAll()!
116    * it fucks up big time in combination with sorting and changing the rootIndex
117    * the following sequence of commands leads to unexpected behavior when inserting new items
118    * setSortingEnabled(true);
119    * setModel();
120    * expandAll();
121    * setRootIndex();
122    */
123   if(event->type() != QEvent::User)
124     return;
125
126   QModelIndex topLevelIdx;
127   for(int i = 0; i < model()->rowCount(rootIndex()); i++) {
128     topLevelIdx = model()->index(i, 0, rootIndex());
129     if(isExpanded(topLevelIdx))
130       continue;
131     else {
132       expand(topLevelIdx);
133       if(i < model()->rowCount(rootIndex()) - 1)
134         QCoreApplication::postEvent(this, new ExpandAllEvent);
135       break;
136     }
137   }
138   event->accept();
139 }