modernize: Use auto where the type is clear from context
[quassel.git] / src / uisupport / nickview.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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
38 NickView::NickView(QWidget *parent)
39     : TreeViewTouch(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     setAnimated(true);
51
52     connect(this, SIGNAL(customContextMenuRequested(const QPoint &)), SLOT(showContextMenu(const QPoint &)));
53
54 #if defined Q_OS_MACOS || defined Q_OS_WIN
55     // afaik this is better on Mac and Windows
56     connect(this, SIGNAL(activated(QModelIndex)), SLOT(startQuery(QModelIndex)));
57 #else
58     connect(this, SIGNAL(doubleClicked(QModelIndex)), SLOT(startQuery(QModelIndex)));
59 #endif
60 }
61
62
63 void NickView::init()
64 {
65     if (!model())
66         return;
67
68     for (int i = 1; i < model()->columnCount(); i++)
69         setColumnHidden(i, true);
70
71     connect(selectionModel(), SIGNAL(currentChanged(QModelIndex, QModelIndex)), SIGNAL(selectionUpdated()));
72     connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)), SIGNAL(selectionUpdated()));
73 }
74
75
76 void NickView::setModel(QAbstractItemModel *model_)
77 {
78     if (model())
79         disconnect(model(), nullptr, this, nullptr);
80
81     TreeViewTouch::setModel(model_);
82     init();
83 }
84
85
86 void NickView::rowsInserted(const QModelIndex &parent, int start, int end)
87 {
88     TreeViewTouch::rowsInserted(parent, start, end);
89     if (model()->data(parent, NetworkModel::ItemTypeRole) == NetworkModel::UserCategoryItemType && !isExpanded(parent)) {
90         unanimatedExpandAll();
91     }
92 }
93
94
95 void NickView::setRootIndex(const QModelIndex &index)
96 {
97     QAbstractItemView::setRootIndex(index);
98     if (index.isValid())
99         unanimatedExpandAll();
100 }
101
102
103 QModelIndexList NickView::selectedIndexes() const
104 {
105     QModelIndexList indexList = TreeViewTouch::selectedIndexes();
106
107     // make sure the item we clicked on is first
108     if (indexList.contains(currentIndex())) {
109         indexList.removeAll(currentIndex());
110         indexList.prepend(currentIndex());
111     }
112
113     return indexList;
114 }
115
116
117 void NickView::unanimatedExpandAll()
118 {
119     // since of Qt Version 4.8.0 the default expandAll will not properly work if
120     // animations are enabled. Therefore we perform an unanimated expand when a
121     // model is set or a toplevel node is inserted.
122     bool wasAnimated = isAnimated();
123     setAnimated(false);
124     expandAll();
125     setAnimated(wasAnimated);
126 }
127
128
129 void NickView::showContextMenu(const QPoint &pos)
130 {
131     Q_UNUSED(pos);
132
133     QMenu contextMenu(this);
134     GraphicalUi::contextMenuActionProvider()->addActions(&contextMenu, selectedIndexes());
135     contextMenu.exec(QCursor::pos());
136 }
137
138
139 void NickView::startQuery(const QModelIndex &index)
140 {
141     if (index.data(NetworkModel::ItemTypeRole) != NetworkModel::IrcUserItemType)
142         return;
143
144     auto *ircUser = qobject_cast<IrcUser *>(index.data(NetworkModel::IrcUserRole).value<QObject *>());
145     NetworkId networkId = index.data(NetworkModel::NetworkIdRole).value<NetworkId>();
146     if (!ircUser || !networkId.isValid())
147         return;
148
149     Client::bufferModel()->switchToOrStartQuery(networkId, ircUser->nick());
150 }