Make Message a proper class rather than a struct (i.e. use setters/getters and
[quassel.git] / src / qtgui / 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 "bufferview.h"
22
23 /*****************************************
24 * The TreeView showing the Buffers
25 *****************************************/
26 // Please be carefull when reimplementing methods which are used to inform the view about changes to the data
27 // to be on the safe side: call QTreeView's method aswell
28 BufferView::BufferView(QWidget *parent) : QTreeView(parent) {
29 }
30
31 void BufferView::init() {
32   setIndentation(10);
33   header()->hide();
34   header()->hideSection(1);
35   expandAll();
36   
37   setAnimated(true);
38   
39   setDragEnabled(true);
40   setAcceptDrops(true);
41   setDropIndicatorShown(true);
42   
43   setSortingEnabled(true);
44   sortByColumn(0, Qt::AscendingOrder);
45   
46   connect(selectionModel(), SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)),
47           model(), SLOT(changeCurrent(const QModelIndex &, const QModelIndex &)));
48   
49   connect(this, SIGNAL(doubleClicked(const QModelIndex &)),
50           model(), SLOT(doubleClickReceived(const QModelIndex &)));
51   
52   connect(model(), SIGNAL(selectionChanged(const QModelIndex &)),
53           this, SLOT(select(const QModelIndex &)));
54   
55   connect(this, SIGNAL(selectionChanged(const QModelIndex &, QItemSelectionModel::SelectionFlags)),
56           selectionModel(), SLOT(select(const QModelIndex &, QItemSelectionModel::SelectionFlags)));
57
58 }
59
60 void BufferView::setFilteredModel(QAbstractItemModel *model, BufferViewFilter::Modes mode, QStringList nets) {
61   BufferViewFilter *filter = new BufferViewFilter(model, mode, nets);
62   setModel(filter);
63   connect(this, SIGNAL(eventDropped(QDropEvent *)), filter, SLOT(dropEvent(QDropEvent *)));
64   connect(this, SIGNAL(removeBuffer(const QModelIndex &)), filter, SLOT(removeBuffer(const QModelIndex &)));
65 }
66
67 void BufferView::setModel(QAbstractItemModel *model) {
68   QTreeView::setModel(model);
69   init();
70 }
71
72 void BufferView::select(const QModelIndex &current) {
73   emit selectionChanged(current, QItemSelectionModel::ClearAndSelect);
74 }
75
76 void BufferView::dropEvent(QDropEvent *event) {
77   if(event->source() != this) {
78     // another view(?) or widget is the source. maybe it's a drag 'n drop 
79     // view customization -> we tell our friend the filter:
80     emit eventDropped(event);
81   }
82   // in the case that the filter did not accept the event or if it's a merge
83   QTreeView::dropEvent(event);    
84 }
85
86 void BufferView::keyPressEvent(QKeyEvent *event) {
87   if(event->key() == Qt::Key_Backspace || event->key() == Qt::Key_Delete) {
88     event->accept();
89     QModelIndex index = selectionModel()->selectedIndexes().first();
90     if(index.isValid()) {
91       emit removeBuffer(index);
92     }
93   }
94   QTreeView::keyPressEvent(event);
95 }
96
97 // ensure that newly inserted network nodes are expanded per default
98 void BufferView::rowsInserted(const QModelIndex & parent, int start, int end) {
99   if(parent == QModelIndex())
100     setExpanded(model()->index(start, 0, parent), true);
101   QTreeView::rowsInserted(parent, start, end);
102 }