1 /***************************************************************************
2 * Copyright (C) 2005-2018 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
23 #include <QApplication>
25 #include <QHeaderView>
29 #include "buffermodel.h"
31 #include "contextmenuactionprovider.h"
32 #include "graphicalui.h"
33 #include "networkmodel.h"
35 #include "nickviewfilter.h"
38 NickView::NickView(QWidget* parent)
39 : TreeViewTouch(parent)
43 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
44 setSortingEnabled(true);
45 sortByColumn(0, Qt::AscendingOrder);
47 setContextMenuPolicy(Qt::CustomContextMenu);
48 setSelectionMode(QAbstractItemView::ExtendedSelection);
52 connect(this, &QWidget::customContextMenuRequested, this, &NickView::showContextMenu);
54 #if defined Q_OS_MACOS || defined Q_OS_WIN
55 // afaik this is better on Mac and Windows
56 connect(this, &QAbstractItemView::activated, this, &NickView::startQuery);
58 connect(this, &QAbstractItemView::doubleClicked, this, &NickView::startQuery);
67 for (int i = 1; i < model()->columnCount(); i++)
68 setColumnHidden(i, true);
70 connect(selectionModel(), &QItemSelectionModel::currentChanged, this, &NickView::selectionUpdated);
71 connect(selectionModel(), &QItemSelectionModel::selectionChanged, this, &NickView::selectionUpdated);
74 void NickView::setModel(QAbstractItemModel* model_)
77 disconnect(model(), nullptr, this, nullptr);
79 TreeViewTouch::setModel(model_);
83 void NickView::rowsInserted(const QModelIndex& parent, int start, int end)
85 TreeViewTouch::rowsInserted(parent, start, end);
86 if (model()->data(parent, NetworkModel::ItemTypeRole) == NetworkModel::UserCategoryItemType && !isExpanded(parent)) {
87 unanimatedExpandAll();
91 void NickView::setRootIndex(const QModelIndex& index)
93 QAbstractItemView::setRootIndex(index);
95 unanimatedExpandAll();
98 QModelIndexList NickView::selectedIndexes() const
100 QModelIndexList indexList = TreeViewTouch::selectedIndexes();
102 // make sure the item we clicked on is first
103 if (indexList.contains(currentIndex())) {
104 indexList.removeAll(currentIndex());
105 indexList.prepend(currentIndex());
111 void NickView::unanimatedExpandAll()
113 // since of Qt Version 4.8.0 the default expandAll will not properly work if
114 // animations are enabled. Therefore we perform an unanimated expand when a
115 // model is set or a toplevel node is inserted.
116 bool wasAnimated = isAnimated();
119 setAnimated(wasAnimated);
122 void NickView::showContextMenu(const QPoint& pos)
126 QMenu contextMenu(this);
127 GraphicalUi::contextMenuActionProvider()->addActions(&contextMenu, selectedIndexes());
128 contextMenu.exec(QCursor::pos());
131 void NickView::startQuery(const QModelIndex& index)
133 if (index.data(NetworkModel::ItemTypeRole) != NetworkModel::IrcUserItemType)
136 auto* ircUser = qobject_cast<IrcUser*>(index.data(NetworkModel::IrcUserRole).value<QObject*>());
137 NetworkId networkId = index.data(NetworkModel::NetworkIdRole).value<NetworkId>();
138 if (!ircUser || !networkId.isValid())
141 Client::bufferModel()->switchToOrStartQuery(networkId, ircUser->nick());