The WebPreviews are now controlled via a neat state machine
[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 #include "uisettings.h"
35
36 class ExpandAllEvent : public QEvent {
37 public:
38   ExpandAllEvent() : QEvent(QEvent::User) {}
39 };
40
41 NickView::NickView(QWidget *parent)
42   : QTreeView(parent)
43 {
44   QAbstractItemDelegate *oldDelegate = itemDelegate();
45   NickViewDelegate *newDelegate = new NickViewDelegate(this);
46   setItemDelegate(newDelegate);
47   delete oldDelegate;
48
49   
50   setIndentation(10);
51   setAnimated(true);
52   header()->hide();
53   setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
54   setSortingEnabled(true);
55   sortByColumn(0, Qt::AscendingOrder);
56
57   setContextMenuPolicy(Qt::CustomContextMenu);
58   setSelectionMode(QAbstractItemView::ExtendedSelection);
59
60   connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), SLOT(showContextMenu(const QPoint&)));
61
62 #if defined Q_WS_QWS || defined Q_WS_X11
63   connect(this, SIGNAL(doubleClicked(QModelIndex)), SLOT(startQuery(QModelIndex)));
64 #else
65   // afaik this is better on Mac and Windows
66   connect(this, SIGNAL(activated(QModelIndex)), SLOT(startQuery(QModelIndex)));
67 #endif
68 }
69
70 void NickView::init() {
71   if(!model())
72     return;
73
74   for(int i = 1; i < model()->columnCount(); i++)
75     setColumnHidden(i, true);
76 }
77
78 void NickView::setModel(QAbstractItemModel *model) {
79   QTreeView::setModel(model);
80   init();
81 }
82
83 void NickView::rowsInserted(const QModelIndex &parent, int start, int end) {
84   QTreeView::rowsInserted(parent, start, end);
85   if(model()->data(parent, NetworkModel::ItemTypeRole) == NetworkModel::UserCategoryItemType && !isExpanded(parent)) {
86     QCoreApplication::postEvent(this, new ExpandAllEvent);
87   }
88 }
89
90 void NickView::setRootIndex(const QModelIndex &index) {
91   QAbstractItemView::setRootIndex(index);
92   if(index.isValid())
93     QCoreApplication::postEvent(this, new ExpandAllEvent);
94 }
95
96 void NickView::showContextMenu(const QPoint & pos ) {
97   QModelIndex index = indexAt(pos);
98   if(index.data(NetworkModel::ItemTypeRole) != NetworkModel::IrcUserItemType)
99     return;
100
101   QModelIndexList indexList = selectedIndexes();
102   // make sure the item we clicked on is first
103   indexList.removeAll(index);
104   indexList.prepend(index);
105
106   QMenu contextMenu(this);
107   Client::mainUi()->actionProvider()->addActions(&contextMenu, indexList);
108   contextMenu.exec(QCursor::pos());
109 }
110
111 void NickView::startQuery(const QModelIndex &index) {
112   if(index.data(NetworkModel::ItemTypeRole) != NetworkModel::IrcUserItemType)
113     return;
114
115   IrcUser *ircUser = qobject_cast<IrcUser *>(index.data(NetworkModel::IrcUserRole).value<QObject *>());
116   NetworkId networkId = index.data(NetworkModel::NetworkIdRole).value<NetworkId>();
117   if(!ircUser || !networkId.isValid())
118     return;
119
120   BufferId bufId = Client::networkModel()->bufferId(networkId, ircUser->nick());
121   if(bufId.isValid())
122     Client::bufferModel()->switchToBuffer(bufId);
123   else
124     Client::userInput(index.data(NetworkModel::BufferInfoRole).value<BufferInfo>(), QString("/QUERY %1").arg(ircUser->nick()));
125 }
126
127 void NickView::customEvent(QEvent *event) {
128   // THIS IS A REPLACEMENT FOR expandAll()
129   /* WARNING: do not call expandAll()!
130    * it fucks up big time in combination with sorting and changing the rootIndex
131    * the following sequence of commands leads to unexpected behavior when inserting new items
132    * setSortingEnabled(true);
133    * setModel();
134    * expandAll();
135    * setRootIndex();
136    */
137   if(event->type() != QEvent::User)
138     return;
139
140   QModelIndex topLevelIdx;
141   for(int i = 0; i < model()->rowCount(rootIndex()); i++) {
142     topLevelIdx = model()->index(i, 0, rootIndex());
143     if(isExpanded(topLevelIdx))
144       continue;
145     else {
146       expand(topLevelIdx);
147       if(i < model()->rowCount(rootIndex()) - 1)
148         QCoreApplication::postEvent(this, new ExpandAllEvent);
149       break;
150     }
151   }
152   event->accept();
153 }
154
155
156 // ****************************************
157 //  NickViewDelgate
158 // ****************************************
159 NickViewDelegate::NickViewDelegate(QObject *parent)
160   : QStyledItemDelegate(parent)
161 {
162   UiSettings s("QtUiStyle/Colors");
163   _FgOnlineStatus = s.value("onlineStatusFG", QVariant(QColor(Qt::black))).value<QColor>();
164   _FgAwayStatus = s.value("awayStatusFG", QVariant(QColor(Qt::gray))).value<QColor>();
165 }
166
167 void NickViewDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const {
168   QStyledItemDelegate::initStyleOption(option, index);
169
170   if(!index.isValid())
171     return;
172
173   QColor fgColor = _FgOnlineStatus;
174   if(!index.data(NetworkModel::ItemActiveRole).toBool())
175     fgColor = _FgAwayStatus;
176
177   option->palette.setColor(QPalette::Text, fgColor);
178 }