380828d3d43308945612dd88f9f0d6453f662f84
[quassel.git] / src / uisupport / bufferview.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 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 "client.h"
22 #include "buffersyncer.h"
23 #include "bufferview.h"
24 #include "networkmodel.h"
25 #include "network.h"
26
27 /*****************************************
28 * The TreeView showing the Buffers
29 *****************************************/
30 // Please be carefull when reimplementing methods which are used to inform the view about changes to the data
31 // to be on the safe side: call QTreeView's method aswell
32 BufferView::BufferView(QWidget *parent) : QTreeView(parent) {
33   setContextMenuPolicy(Qt::CustomContextMenu);
34
35   connect(this, SIGNAL(customContextMenuRequested(const QPoint &)),
36           this, SLOT(showContextMenu(const QPoint &)));
37 }
38
39 void BufferView::init() {
40   setIndentation(10);
41   header()->setContextMenuPolicy(Qt::ActionsContextMenu);
42   hideColumn(1);
43   hideColumn(2);
44   expandAll();
45
46   setAnimated(true);
47
48 #ifndef QT_NO_DRAGANDDROP
49   setDragEnabled(true);
50   setAcceptDrops(true);
51   setDropIndicatorShown(true);
52 #endif
53
54   setSortingEnabled(true);
55   sortByColumn(0, Qt::AscendingOrder);
56   connect(this, SIGNAL(activated(QModelIndex)), this, SLOT(joinChannel(QModelIndex)));
57 }
58
59 void BufferView::setFilteredModel(QAbstractItemModel *model, BufferViewFilter::Modes mode, QList<NetworkId> nets) {
60   BufferViewFilter *filter = new BufferViewFilter(model, mode, nets);
61   setModel(filter);
62   connect(this, SIGNAL(removeBuffer(const QModelIndex &)), filter, SLOT(removeBuffer(const QModelIndex &)));
63 }
64
65 void BufferView::setModel(QAbstractItemModel *model) {
66   delete selectionModel();
67   QTreeView::setModel(model);
68   init();
69
70   // remove old Actions
71   QList<QAction *> oldactions = header()->actions();
72   foreach(QAction *action, oldactions) {
73     header()->removeAction(action);
74     action->deleteLater();
75   }
76
77   QString sectionName;
78   QAction *showSection;
79   for(int i = 1; i < model->columnCount(); i++) {
80     sectionName = (model->headerData(i, Qt::Horizontal, Qt::DisplayRole)).toString();
81     showSection = new QAction(sectionName, header());
82     showSection->setCheckable(true);
83     showSection->setChecked(!isColumnHidden(i));
84     showSection->setProperty("column", i);
85     connect(showSection, SIGNAL(toggled(bool)), this, SLOT(toggleHeader(bool)));
86     header()->addAction(showSection);
87   }
88   
89 }
90
91 void BufferView::joinChannel(const QModelIndex &index) {
92   BufferInfo::Type bufferType = (BufferInfo::Type)index.data(NetworkModel::BufferTypeRole).value<int>();
93
94   if(bufferType != BufferInfo::ChannelBuffer)
95     return;
96
97   BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
98   
99   Client::userInput(bufferInfo, QString("/JOIN %1").arg(bufferInfo.bufferName()));
100 }
101
102 void BufferView::keyPressEvent(QKeyEvent *event) {
103   if(event->key() == Qt::Key_Backspace || event->key() == Qt::Key_Delete) {
104     event->accept();
105     QModelIndex index = selectionModel()->selectedIndexes().first();
106     if(index.isValid()) {
107       emit removeBuffer(index);
108     }
109   }
110   QTreeView::keyPressEvent(event);
111 }
112
113 // ensure that newly inserted network nodes are expanded per default
114 void BufferView::rowsInserted(const QModelIndex & parent, int start, int end) {
115   QTreeView::rowsInserted(parent, start, end);
116   if(model()->rowCount(parent) == 1 && parent != QModelIndex()) {
117     // without updating the parent the expand will have no effect... Qt Bug?
118     update(parent); 
119     expand(parent);
120   }
121 }
122
123 void BufferView::toggleHeader(bool checked) {
124   QAction *action = qobject_cast<QAction *>(sender());
125   header()->setSectionHidden((action->property("column")).toInt(), !checked);
126 }
127
128 void BufferView::showContextMenu(const QPoint &pos) {
129   QModelIndex index = indexAt(pos);
130   if(!index.isValid()) return;
131   QMenu contextMenu(this);
132   QAction *connectNetAction = new QAction(tr("Connect"), this);
133   QAction *disconnectNetAction = new QAction(tr("Disconnect"), this);
134
135   QAction *joinBufferAction = new QAction(tr("Join"), this);
136   QAction *partBufferAction = new QAction(tr("Part"), this);
137   QAction *removeBufferAction = new QAction(tr("Delete buffer"), this);
138
139   QMenu *hideEventsMenu = new QMenu(tr("Hide Events"), this);
140   QAction *hideJoinAction = hideEventsMenu->addAction(tr("Join Events"));
141   QAction *hidePartAction = hideEventsMenu->addAction(tr("Part Events"));
142   QAction *hideKillAction = hideEventsMenu->addAction(tr("Kill Events"));
143   QAction *hideQuitAction = hideEventsMenu->addAction(tr("Quit Events"));
144   QAction *hideModeAction = hideEventsMenu->addAction(tr("Mode Events"));
145   hideJoinAction->setCheckable(true);
146   hidePartAction->setCheckable(true);
147   hideKillAction->setCheckable(true);
148   hideQuitAction->setCheckable(true);
149   hideModeAction->setCheckable(true);
150   hideJoinAction->setEnabled(false);
151   hidePartAction->setEnabled(false);
152   hideKillAction->setEnabled(false);
153   hideQuitAction->setEnabled(false);
154   hideModeAction->setEnabled(false);
155
156   QAction *ignoreListAction = new QAction(tr("Ignore list"), this);
157   ignoreListAction->setEnabled(false);
158   QAction *whoBufferAction = new QAction(tr("WHO"), this);
159
160   if(index.data(NetworkModel::ItemTypeRole) == NetworkModel::NetworkItemType) {
161     if(index.data(NetworkModel::ItemActiveRole).toBool()) {
162       contextMenu.addAction(disconnectNetAction);
163     } else {
164       contextMenu.addAction(connectNetAction);
165     }
166   }
167
168   BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
169   QString channelname = index.sibling(index.row(), 0).data().toString();
170
171   if(index.data(NetworkModel::ItemTypeRole) == NetworkModel::BufferItemType) {
172     if(bufferInfo.type() != BufferInfo::ChannelBuffer && bufferInfo.type() != BufferInfo::QueryBuffer) return;
173     contextMenu.addAction(joinBufferAction);
174     contextMenu.addAction(partBufferAction);
175     contextMenu.addAction(removeBufferAction);
176     contextMenu.addMenu(hideEventsMenu);
177     contextMenu.addAction(ignoreListAction);
178     contextMenu.addAction(whoBufferAction);
179
180     if(bufferInfo.type() == BufferInfo::ChannelBuffer) {
181       if(index.data(NetworkModel::ItemActiveRole).toBool()) {
182         removeBufferAction->setEnabled(false);
183         removeBufferAction->setToolTip("To delete the buffer, part the channel first.");
184         joinBufferAction->setVisible(false);
185       } else {
186         partBufferAction->setVisible(false);
187       }
188     } else {
189       joinBufferAction->setVisible(false);
190       partBufferAction->setVisible(false);
191     }
192   }
193
194   QAction *result = contextMenu.exec(QCursor::pos());
195   if(result == connectNetAction || result == disconnectNetAction) {
196     const Network *network = Client::network(index.data(NetworkModel::NetworkIdRole).value<NetworkId>());
197     if(!network) return;
198     if(network->connectionState() == Network::Disconnected) 
199       network->requestConnect();
200     else 
201       network->requestDisconnect();
202   } else
203   if(result == joinBufferAction) {
204     Client::instance()->userInput(bufferInfo, QString("/JOIN %1").arg(channelname));
205   } else
206   if(result == partBufferAction) {
207     Client::instance()->userInput(bufferInfo, QString("/PART %1").arg(channelname));
208   } else
209   if(result == removeBufferAction) {
210     int res = QMessageBox::question(this, tr("Remove buffer permanently?"),
211                                     tr("Do you want to delete the buffer \"%1\" permanently? This will delete all related data, including all backlog "
212                                        "data, from the core's database!").arg(bufferInfo.bufferName()),
213                                         QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
214     if(res == QMessageBox::Yes) {
215       Client::removeBuffer(bufferInfo.bufferId());
216     } 
217   } else 
218   if(result == whoBufferAction) {
219     Client::instance()->userInput(bufferInfo, QString("/WHO %1").arg(channelname));
220   }
221 }
222
223 void BufferView::wheelEvent(QWheelEvent* event)
224 {
225     int rowDelta = ( event->delta() > 0 ) ? -1 : 1;
226     QModelIndex currentIndex = selectionModel()->currentIndex();
227     QModelIndex resultingIndex;
228     if( model()->hasIndex(  currentIndex.row() + rowDelta, currentIndex.column(), currentIndex.parent() ) )
229     {
230         resultingIndex = currentIndex.sibling( currentIndex.row() + rowDelta, currentIndex.column() );
231     }
232     else //if we scroll into a the parent node...
233     {
234         QModelIndex parent = currentIndex.parent();
235         QModelIndex aunt = parent.sibling( parent.row() + rowDelta, parent.column() );
236         if( rowDelta == -1 )
237             resultingIndex = aunt.child( model()->rowCount( aunt ) - 1, 0 );
238         else
239             resultingIndex = aunt.child( 0, 0 );
240         if( !resultingIndex.isValid() )
241             return;
242     }
243     selectionModel()->setCurrentIndex( resultingIndex, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows );
244     selectionModel()->select( resultingIndex, QItemSelectionModel::ClearAndSelect );
245 }
246