1/***************************************************************************
2 * Copyright (C) 2005-2014 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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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
38NickView::NickView(QWidget *parent)
39 : QTreeView(parent)
40{
41 setIndentation(10);
42 header()->hide();
43 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
44 setSortingEnabled(true);
45 sortByColumn(0, Qt::AscendingOrder);
46
47 setContextMenuPolicy(Qt::CustomContextMenu);
48 setSelectionMode(QAbstractItemView::ExtendedSelection);
49
50// // breaks with Qt 4.8
51// if(QString("4.8.0") > qVersion()) // FIXME breaks with Qt versions >= 4.10!
52 setAnimated(true);
53
54 connect(this, SIGNAL(customContextMenuRequested(const QPoint &)), SLOT(showContextMenu(const QPoint &)));
55
56#if defined Q_WS_QWS || defined Q_WS_X11
57 connect(this, SIGNAL(doubleClicked(QModelIndex)), SLOT(startQuery(QModelIndex)));
58#else
59 // afaik this is better on Mac and Windows
60 connect(this, SIGNAL(activated(QModelIndex)), SLOT(startQuery(QModelIndex)));
61#endif
62}
63
64
65void NickView::init()
66{
67 if (!model())
68 return;
69
70 for (int i = 1; i < model()->columnCount(); i++)
71 setColumnHidden(i, true);
72
73 connect(selectionModel(), SIGNAL(currentChanged(QModelIndex, QModelIndex)), SIGNAL(selectionUpdated()));
74 connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)), SIGNAL(selectionUpdated()));
75}
76
77
78void NickView::setModel(QAbstractItemModel *model_)
79{
80 if (model())
81 disconnect(model(), 0, this, 0);
82
83 QTreeView::setModel(model_);
84 init();
85}
86
87
88void NickView::rowsInserted(const QModelIndex &parent, int start, int end)
89{
90 QTreeView::rowsInserted(parent, start, end);
91 if (model()->data(parent, NetworkModel::ItemTypeRole) == NetworkModel::UserCategoryItemType && !isExpanded(parent)) {
92 unanimatedExpandAll();
93 }
94}
95
96
97void NickView::setRootIndex(const QModelIndex &index)
98{
99 QAbstractItemView::setRootIndex(index);
100 if (index.isValid())
101 unanimatedExpandAll();
102}
103
104
105QModelIndexList NickView::selectedIndexes() const
106{
107 QModelIndexList indexList = QTreeView::selectedIndexes();
108
109 // make sure the item we clicked on is first
110 if (indexList.contains(currentIndex())) {
111 indexList.removeAll(currentIndex());
112 indexList.prepend(currentIndex());
113 }
114
115 return indexList;
116}
117
118
119void NickView::unanimatedExpandAll()
120{
121 // since of Qt Version 4.8.0 the default expandAll will not properly work if
122 // animations are enabled. Therefore we perform an unanimated expand when a
123 // model is set or a toplevel node is inserted.
124 bool wasAnimated = isAnimated();
125 setAnimated(false);
126 expandAll();
127 setAnimated(wasAnimated);
128}
129
130
131void NickView::showContextMenu(const QPoint &pos)
132{
133 Q_UNUSED(pos);
134
135 QMenu contextMenu(this);
136 GraphicalUi::contextMenuActionProvider()->addActions(&contextMenu, selectedIndexes());
137 contextMenu.exec(QCursor::pos());
138}
139
140
141void NickView::startQuery(const QModelIndex &index)
142{
143 if (index.data(NetworkModel::ItemTypeRole) != NetworkModel::IrcUserItemType)
144 return;
145
146 IrcUser *ircUser = qobject_cast<IrcUser *>(index.data(NetworkModel::IrcUserRole).value<QObject *>());
147 NetworkId networkId = index.data(NetworkModel::NetworkIdRole).value<NetworkId>();
148 if (!ircUser || !networkId.isValid())
149 return;
150
151 Client::bufferModel()->switchToOrStartQuery(networkId, ircUser->nick());
152}
153