Added ModelPropertyMapper which allows to keep track of /current/ changes in the...
[quassel.git] / src / qtui / bufferview.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-07 by The Quassel Team                             *
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) any later version.                                   *
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 "client.h"
22 #include "bufferview.h"
23 #include "buffertreemodel.h"
24
25 /*****************************************
26 * The TreeView showing the Buffers
27 *****************************************/
28 // Please be carefull when reimplementing methods which are used to inform the view about changes to the data
29 // to be on the safe side: call QTreeView's method aswell
30 BufferView::BufferView(QWidget *parent) : QTreeView(parent) {
31 }
32
33 void BufferView::init() {
34   setIndentation(10);
35   header()->hide();
36   header()->hideSection(1);
37   expandAll();
38   
39   setAnimated(true);
40   
41   setDragEnabled(true);
42   setAcceptDrops(true);
43   setDropIndicatorShown(true);
44   
45   setSortingEnabled(true);
46   sortByColumn(0, Qt::AscendingOrder);
47   
48   connect(this, SIGNAL(activated(QModelIndex)), this, SLOT(joinChannel(QModelIndex)));
49 }
50
51 void BufferView::setFilteredModel(QAbstractItemModel *model, BufferViewFilter::Modes mode, QStringList nets) {
52   BufferViewFilter *filter = new BufferViewFilter(model, mode, nets);
53   setModel(filter);
54   connect(this, SIGNAL(eventDropped(QDropEvent *)), filter, SLOT(dropEvent(QDropEvent *)));
55   connect(this, SIGNAL(removeBuffer(const QModelIndex &)), filter, SLOT(removeBuffer(const QModelIndex &)));
56 }
57
58 void BufferView::setModel(QAbstractItemModel *model) {
59   delete selectionModel();
60   QTreeView::setModel(model);
61   init();
62   
63 }
64
65 void BufferView::dropEvent(QDropEvent *event) {
66   if(event->source() != this) {
67     // another view(?) or widget is the source. maybe it's a drag 'n drop 
68     // view customization -> we tell our friend the filter:
69     emit eventDropped(event);
70   }
71   // in the case that the filter did not accept the event or if it's a merge
72   QTreeView::dropEvent(event);    
73 }
74
75 void BufferView::joinChannel(const QModelIndex &index) {
76   Buffer::Type bufferType = (Buffer::Type)index.data(BufferTreeModel::BufferTypeRole).toInt();
77
78   if(bufferType != Buffer::ChannelType)
79     return;
80   
81   Client::fakeInput(index.data(BufferTreeModel::BufferUidRole).toUInt(), QString("/JOIN %1").arg(index.sibling(index.row(), 0).data().toString()));
82 }
83
84 void BufferView::keyPressEvent(QKeyEvent *event) {
85   if(event->key() == Qt::Key_Backspace || event->key() == Qt::Key_Delete) {
86     event->accept();
87     QModelIndex index = selectionModel()->selectedIndexes().first();
88     if(index.isValid()) {
89       emit removeBuffer(index);
90     }
91   }
92   QTreeView::keyPressEvent(event);
93 }
94
95 // ensure that newly inserted network nodes are expanded per default
96 void BufferView::rowsInserted(const QModelIndex & parent, int start, int end) {
97   QTreeView::rowsInserted(parent, start, end);
98   if(model()->rowCount(parent) == 1 && parent != QModelIndex()) {
99     // without updating the parent the expand will have no effect... Qt Bug?
100     update(parent); 
101     expand(parent);
102   }
103 }