Style NickView via UiStyle
[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 #include "uisettings.h"
38
39 class ExpandAllEvent : public QEvent {
40 public:
41   ExpandAllEvent() : QEvent(QEvent::User) {}
42 };
43
44 NickView::NickView(QWidget *parent)
45   : QTreeView(parent)
46 {
47   setIndentation(10);
48   setAnimated(true);
49   header()->hide();
50   setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
51   setSortingEnabled(true);
52   sortByColumn(0, Qt::AscendingOrder);
53
54   setContextMenuPolicy(Qt::CustomContextMenu);
55   setSelectionMode(QAbstractItemView::ExtendedSelection);
56
57   connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), SLOT(showContextMenu(const QPoint&)));
58
59 #if defined Q_WS_QWS || defined Q_WS_X11
60   connect(this, SIGNAL(doubleClicked(QModelIndex)), SLOT(startQuery(QModelIndex)));
61 #else
62   // afaik this is better on Mac and Windows
63   connect(this, SIGNAL(activated(QModelIndex)), SLOT(startQuery(QModelIndex)));
64 #endif
65
66   UiStyleSettings s("QtUiStyle/Fonts"); // li'l dirty here, but fonts are stored in QtUiStyle :/
67   s.notify("BufferView", this, SLOT(setCustomFont(QVariant))); // yes, we share the BufferView settings
68   setCustomFont(s.value("BufferView", QFont()));
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::setCustomFont(const QVariant &v) {
91   QFont font = v.value<QFont>();
92   if(font.family().isEmpty())
93     font = QApplication::font();
94   setFont(font);
95 }
96
97 void NickView::rowsInserted(const QModelIndex &parent, int start, int end) {
98   QTreeView::rowsInserted(parent, start, end);
99   if(model()->data(parent, NetworkModel::ItemTypeRole) == NetworkModel::UserCategoryItemType && !isExpanded(parent)) {
100     QCoreApplication::postEvent(this, new ExpandAllEvent);
101   }
102 }
103
104 void NickView::setRootIndex(const QModelIndex &index) {
105   QAbstractItemView::setRootIndex(index);
106   if(index.isValid())
107     QCoreApplication::postEvent(this, new ExpandAllEvent);
108 }
109
110 QModelIndexList NickView::selectedIndexes() const {
111   QModelIndexList indexList = QTreeView::selectedIndexes();
112
113   // make sure the item we clicked on is first
114   if(indexList.contains(currentIndex())) {
115     indexList.removeAll(currentIndex());
116     indexList.prepend(currentIndex());
117   }
118
119   return indexList;
120 }
121
122 void NickView::showContextMenu(const QPoint &pos ) {
123   Q_UNUSED(pos);
124
125   QMenu contextMenu(this);
126   GraphicalUi::contextMenuActionProvider()->addActions(&contextMenu, selectedIndexes());
127   contextMenu.exec(QCursor::pos());
128 }
129
130 void NickView::startQuery(const QModelIndex &index) {
131   if(index.data(NetworkModel::ItemTypeRole) != NetworkModel::IrcUserItemType)
132     return;
133
134   IrcUser *ircUser = qobject_cast<IrcUser *>(index.data(NetworkModel::IrcUserRole).value<QObject *>());
135   NetworkId networkId = index.data(NetworkModel::NetworkIdRole).value<NetworkId>();
136   if(!ircUser || !networkId.isValid())
137     return;
138
139   BufferId bufId = Client::networkModel()->bufferId(networkId, ircUser->nick());
140   if(bufId.isValid())
141     Client::bufferModel()->switchToBuffer(bufId);
142   else
143     Client::userInput(index.data(NetworkModel::BufferInfoRole).value<BufferInfo>(), QString("/QUERY %1").arg(ircUser->nick()));
144 }
145
146 void NickView::customEvent(QEvent *event) {
147   // THIS IS A REPLACEMENT FOR expandAll()
148   /* WARNING: do not call expandAll()!
149    * it fucks up big time in combination with sorting and changing the rootIndex
150    * the following sequence of commands leads to unexpected behavior when inserting new items
151    * setSortingEnabled(true);
152    * setModel();
153    * expandAll();
154    * setRootIndex();
155    */
156   if(event->type() != QEvent::User)
157     return;
158
159   QModelIndex topLevelIdx;
160   for(int i = 0; i < model()->rowCount(rootIndex()); i++) {
161     topLevelIdx = model()->index(i, 0, rootIndex());
162     if(isExpanded(topLevelIdx))
163       continue;
164     else {
165       expand(topLevelIdx);
166       if(i < model()->rowCount(rootIndex()) - 1)
167         QCoreApplication::postEvent(this, new ExpandAllEvent);
168       break;
169     }
170   }
171   event->accept();
172 }