Some fixups for the per-chat history stuff
[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
38 class ExpandAllEvent : public QEvent {
39 public:
40   ExpandAllEvent() : QEvent(QEvent::User) {}
41 };
42
43 NickView::NickView(QWidget *parent)
44   : QTreeView(parent)
45 {
46   setIndentation(10);
47   setAnimated(true);
48   header()->hide();
49   setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
50   setSortingEnabled(true);
51   sortByColumn(0, Qt::AscendingOrder);
52
53   setContextMenuPolicy(Qt::CustomContextMenu);
54   setSelectionMode(QAbstractItemView::ExtendedSelection);
55
56   connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), SLOT(showContextMenu(const QPoint&)));
57
58 #if defined Q_WS_QWS || defined Q_WS_X11
59   connect(this, SIGNAL(doubleClicked(QModelIndex)), SLOT(startQuery(QModelIndex)));
60 #else
61   // afaik this is better on Mac and Windows
62   connect(this, SIGNAL(activated(QModelIndex)), SLOT(startQuery(QModelIndex)));
63 #endif
64 }
65
66 void NickView::init() {
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 void NickView::setModel(QAbstractItemModel *model_) {
78   if(model())
79     disconnect(model(), 0, this, 0);
80
81   QTreeView::setModel(model_);
82   init();
83 }
84
85 void NickView::rowsInserted(const QModelIndex &parent, int start, int end) {
86   QTreeView::rowsInserted(parent, start, end);
87   if(model()->data(parent, NetworkModel::ItemTypeRole) == NetworkModel::UserCategoryItemType && !isExpanded(parent)) {
88     QCoreApplication::postEvent(this, new ExpandAllEvent);
89   }
90 }
91
92 void NickView::setRootIndex(const QModelIndex &index) {
93   QAbstractItemView::setRootIndex(index);
94   if(index.isValid())
95     QCoreApplication::postEvent(this, new ExpandAllEvent);
96 }
97
98 QModelIndexList NickView::selectedIndexes() const {
99   QModelIndexList indexList = QTreeView::selectedIndexes();
100
101   // make sure the item we clicked on is first
102   if(indexList.contains(currentIndex())) {
103     indexList.removeAll(currentIndex());
104     indexList.prepend(currentIndex());
105   }
106
107   return indexList;
108 }
109
110 void NickView::showContextMenu(const QPoint &pos ) {
111   Q_UNUSED(pos);
112
113   QMenu contextMenu(this);
114   GraphicalUi::contextMenuActionProvider()->addActions(&contextMenu, selectedIndexes());
115   contextMenu.exec(QCursor::pos());
116 }
117
118 void NickView::startQuery(const QModelIndex &index) {
119   if(index.data(NetworkModel::ItemTypeRole) != NetworkModel::IrcUserItemType)
120     return;
121
122   IrcUser *ircUser = qobject_cast<IrcUser *>(index.data(NetworkModel::IrcUserRole).value<QObject *>());
123   NetworkId networkId = index.data(NetworkModel::NetworkIdRole).value<NetworkId>();
124   if(!ircUser || !networkId.isValid())
125     return;
126
127   Client::bufferModel()->switchToOrStartQuery(networkId, ircUser->nick());
128 }
129
130 void NickView::customEvent(QEvent *event) {
131   // THIS IS A REPLACEMENT FOR expandAll()
132   /* WARNING: do not call expandAll()!
133    * it fucks up big time in combination with sorting and changing the rootIndex
134    * the following sequence of commands leads to unexpected behavior when inserting new items
135    * setSortingEnabled(true);
136    * setModel();
137    * expandAll();
138    * setRootIndex();
139    */
140   if(event->type() != QEvent::User)
141     return;
142
143   if(!model())
144     return;
145
146   QModelIndex topLevelIdx;
147   for(int i = 0; i < model()->rowCount(rootIndex()); i++) {
148     topLevelIdx = model()->index(i, 0, rootIndex());
149     if(isExpanded(topLevelIdx))
150       continue;
151     else {
152       expand(topLevelIdx);
153       if(i < model()->rowCount(rootIndex()) - 1)
154         QCoreApplication::postEvent(this, new ExpandAllEvent);
155       break;
156     }
157   }
158   event->accept();
159 }