dd7d1af53ae3c8adebf530428916e91994f717e0
[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 "nickviewfilter.h"
31 #include "networkmodel.h"
32 #include "quasselui.h"
33 #include "types.h"
34
35 class ExpandAllEvent : public QEvent {
36 public:
37   ExpandAllEvent() : QEvent(QEvent::User) {}
38 };
39
40 NickView::NickView(QWidget *parent)
41   : QTreeView(parent)
42 {
43   QAbstractItemDelegate *oldDelegate = itemDelegate();
44   NickViewDelegate *newDelegate = new NickViewDelegate(this);
45   setItemDelegate(newDelegate);
46   delete oldDelegate;
47
48   
49   setIndentation(10);
50   setAnimated(true);
51   header()->hide();
52   setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
53   setSortingEnabled(true);
54   sortByColumn(0, Qt::AscendingOrder);
55
56   setContextMenuPolicy(Qt::CustomContextMenu);
57   setSelectionMode(QAbstractItemView::ExtendedSelection);
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
77 void NickView::setModel(QAbstractItemModel *model) {
78   QTreeView::setModel(model);
79   init();
80 }
81
82 void NickView::rowsInserted(const QModelIndex &parent, int start, int end) {
83   QTreeView::rowsInserted(parent, start, end);
84   if(model()->data(parent, NetworkModel::ItemTypeRole) == NetworkModel::UserCategoryItemType && !isExpanded(parent)) {
85     QCoreApplication::postEvent(this, new ExpandAllEvent);
86   }
87 }
88
89 void NickView::setRootIndex(const QModelIndex &index) {
90   QAbstractItemView::setRootIndex(index);
91   if(index.isValid())
92     QCoreApplication::postEvent(this, new ExpandAllEvent);
93 }
94
95 void NickView::showContextMenu(const QPoint & pos ) {
96   QModelIndex index = indexAt(pos);
97   if(index.data(NetworkModel::ItemTypeRole) != NetworkModel::IrcUserItemType)
98     return;
99
100   QModelIndexList indexList = selectedIndexes();
101   // make sure the item we clicked on is first
102   indexList.removeAll(index);
103   indexList.prepend(index);
104
105   QMenu contextMenu(this);
106   Client::mainUi()->actionProvider()->addActions(&contextMenu, indexList);
107   contextMenu.exec(QCursor::pos());
108 }
109
110 void NickView::startQuery(const QModelIndex &index) {
111   if(index.data(NetworkModel::ItemTypeRole) != NetworkModel::IrcUserItemType)
112     return;
113
114   IrcUser *ircUser = qobject_cast<IrcUser *>(index.data(NetworkModel::IrcUserRole).value<QObject *>());
115   NetworkId networkId = index.data(NetworkModel::NetworkIdRole).value<NetworkId>();
116   if(!ircUser || !networkId.isValid())
117     return;
118
119   BufferId bufId = Client::networkModel()->bufferId(networkId, ircUser->nick());
120   if(bufId.isValid())
121     Client::bufferModel()->switchToBuffer(bufId);
122   else
123     Client::userInput(index.data(NetworkModel::BufferInfoRole).value<BufferInfo>(), QString("/QUERY %1").arg(ircUser->nick()));
124 }
125
126 void NickView::customEvent(QEvent *event) {
127   // THIS IS A REPLACEMENT FOR expandAll()
128   /* WARNING: do not call expandAll()!
129    * it fucks up big time in combination with sorting and changing the rootIndex
130    * the following sequence of commands leads to unexpected behavior when inserting new items
131    * setSortingEnabled(true);
132    * setModel();
133    * expandAll();
134    * setRootIndex();
135    */
136   if(event->type() != QEvent::User)
137     return;
138
139   QModelIndex topLevelIdx;
140   for(int i = 0; i < model()->rowCount(rootIndex()); i++) {
141     topLevelIdx = model()->index(i, 0, rootIndex());
142     if(isExpanded(topLevelIdx))
143       continue;
144     else {
145       expand(topLevelIdx);
146       if(i < model()->rowCount(rootIndex()) - 1)
147         QCoreApplication::postEvent(this, new ExpandAllEvent);
148       break;
149     }
150   }
151   event->accept();
152 }
153
154
155 // ****************************************
156 //  NickViewDelgate
157 // ****************************************
158 NickViewDelegate::NickViewDelegate(QObject *parent)
159   : QStyledItemDelegate(parent)
160 {
161 }
162
163 void NickViewDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const {
164   QStyledItemDelegate::initStyleOption(option, index);
165
166   if(!index.data(NetworkModel::ItemActiveRole).toBool())
167     option->palette.setColor(QPalette::Text, option->palette.color(QPalette::Dark));
168 }