Always put own nick last in nick completion
[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   QAbstractItemDelegate *oldDelegate = itemDelegate();
48   NickViewDelegate *newDelegate = new NickViewDelegate(this);
49   setItemDelegate(newDelegate);
50   delete oldDelegate;
51
52   setIndentation(10);
53   setAnimated(true);
54   header()->hide();
55   setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
56   setSortingEnabled(true);
57   sortByColumn(0, Qt::AscendingOrder);
58
59   setContextMenuPolicy(Qt::CustomContextMenu);
60   setSelectionMode(QAbstractItemView::ExtendedSelection);
61
62   connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), SLOT(showContextMenu(const QPoint&)));
63
64 #if defined Q_WS_QWS || defined Q_WS_X11
65   connect(this, SIGNAL(doubleClicked(QModelIndex)), SLOT(startQuery(QModelIndex)));
66 #else
67   // afaik this is better on Mac and Windows
68   connect(this, SIGNAL(activated(QModelIndex)), SLOT(startQuery(QModelIndex)));
69 #endif
70
71   UiStyleSettings s("QtUiStyle/Fonts"); // li'l dirty here, but fonts are stored in QtUiStyle :/
72   s.notify("BufferView", this, SLOT(setCustomFont(QVariant))); // yes, we share the BufferView settings
73   setCustomFont(s.value("BufferView", QFont()));
74 }
75
76 void NickView::init() {
77   if(!model())
78     return;
79
80   for(int i = 1; i < model()->columnCount(); i++)
81     setColumnHidden(i, true);
82
83   connect(selectionModel(), SIGNAL(currentChanged(QModelIndex, QModelIndex)), SIGNAL(selectionUpdated()));
84   connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)), SIGNAL(selectionUpdated()));
85 }
86
87 void NickView::setModel(QAbstractItemModel *model_) {
88   if(model())
89     disconnect(model(), 0, this, 0);
90
91   QTreeView::setModel(model_);
92   init();
93 }
94
95 void NickView::setCustomFont(const QVariant &v) {
96   QFont font = v.value<QFont>();
97   if(font.family().isEmpty())
98     font = QApplication::font();
99   setFont(font);
100 }
101
102 void NickView::rowsInserted(const QModelIndex &parent, int start, int end) {
103   QTreeView::rowsInserted(parent, start, end);
104   if(model()->data(parent, NetworkModel::ItemTypeRole) == NetworkModel::UserCategoryItemType && !isExpanded(parent)) {
105     QCoreApplication::postEvent(this, new ExpandAllEvent);
106   }
107 }
108
109 void NickView::setRootIndex(const QModelIndex &index) {
110   QAbstractItemView::setRootIndex(index);
111   if(index.isValid())
112     QCoreApplication::postEvent(this, new ExpandAllEvent);
113 }
114
115 QModelIndexList NickView::selectedIndexes() const {
116   QModelIndexList indexList = QTreeView::selectedIndexes();
117
118   // make sure the item we clicked on is first
119   if(indexList.contains(currentIndex())) {
120     indexList.removeAll(currentIndex());
121     indexList.prepend(currentIndex());
122   }
123
124   return indexList;
125 }
126
127 void NickView::showContextMenu(const QPoint &pos ) {
128   Q_UNUSED(pos);
129
130   QMenu contextMenu(this);
131   GraphicalUi::contextMenuActionProvider()->addActions(&contextMenu, selectedIndexes());
132   contextMenu.exec(QCursor::pos());
133 }
134
135 void NickView::startQuery(const QModelIndex &index) {
136   if(index.data(NetworkModel::ItemTypeRole) != NetworkModel::IrcUserItemType)
137     return;
138
139   IrcUser *ircUser = qobject_cast<IrcUser *>(index.data(NetworkModel::IrcUserRole).value<QObject *>());
140   NetworkId networkId = index.data(NetworkModel::NetworkIdRole).value<NetworkId>();
141   if(!ircUser || !networkId.isValid())
142     return;
143
144   BufferId bufId = Client::networkModel()->bufferId(networkId, ircUser->nick());
145   if(bufId.isValid())
146     Client::bufferModel()->switchToBuffer(bufId);
147   else
148     Client::userInput(index.data(NetworkModel::BufferInfoRole).value<BufferInfo>(), QString("/QUERY %1").arg(ircUser->nick()));
149 }
150
151 void NickView::customEvent(QEvent *event) {
152   // THIS IS A REPLACEMENT FOR expandAll()
153   /* WARNING: do not call expandAll()!
154    * it fucks up big time in combination with sorting and changing the rootIndex
155    * the following sequence of commands leads to unexpected behavior when inserting new items
156    * setSortingEnabled(true);
157    * setModel();
158    * expandAll();
159    * setRootIndex();
160    */
161   if(event->type() != QEvent::User)
162     return;
163
164   QModelIndex topLevelIdx;
165   for(int i = 0; i < model()->rowCount(rootIndex()); i++) {
166     topLevelIdx = model()->index(i, 0, rootIndex());
167     if(isExpanded(topLevelIdx))
168       continue;
169     else {
170       expand(topLevelIdx);
171       if(i < model()->rowCount(rootIndex()) - 1)
172         QCoreApplication::postEvent(this, new ExpandAllEvent);
173       break;
174     }
175   }
176   event->accept();
177 }
178
179
180 // ****************************************
181 //  NickViewDelgate
182 // ****************************************
183 NickViewDelegate::NickViewDelegate(QObject *parent)
184   : QStyledItemDelegate(parent)
185 {
186   UiSettings s("QtUiStyle/Colors");
187   _FgOnlineStatus = s.value("onlineStatusFG", QVariant(QColor(Qt::black))).value<QColor>();
188   _FgAwayStatus = s.value("awayStatusFG", QVariant(QColor(Qt::gray))).value<QColor>();
189 }
190
191 void NickViewDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const {
192   QStyledItemDelegate::initStyleOption(option, index);
193
194   if(!index.isValid())
195     return;
196
197   QColor fgColor = _FgOnlineStatus;
198   if(!index.data(NetworkModel::ItemActiveRole).toBool())
199     fgColor = _FgAwayStatus;
200
201   option->palette.setColor(QPalette::Text, fgColor);
202 }