1 /***************************************************************************
2 * Copyright (C) 2005-08 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "bufferview.h"
23 #include "buffermodel.h"
24 #include "bufferviewfilter.h"
25 #include "buffersyncer.h"
27 #include "mappedselectionmodel.h"
29 #include "networkmodel.h"
31 #include "uisettings.h"
37 #include <QHeaderView>
38 #include <QInputDialog>
41 #include <QMessageBox>
43 /*****************************************
44 * The TreeView showing the Buffers
45 *****************************************/
46 // Please be carefull when reimplementing methods which are used to inform the view about changes to the data
47 // to be on the safe side: call QTreeView's method aswell
48 BufferView::BufferView(QWidget *parent) : QTreeView(parent) {
49 setContextMenuPolicy(Qt::CustomContextMenu);
51 connect(this, SIGNAL(customContextMenuRequested(const QPoint &)),
52 this, SLOT(showContextMenu(const QPoint &)));
55 void BufferView::init() {
57 header()->setContextMenuPolicy(Qt::ActionsContextMenu);
64 #ifndef QT_NO_DRAGANDDROP
67 setDropIndicatorShown(true);
70 setSortingEnabled(true);
71 sortByColumn(0, Qt::AscendingOrder);
73 // this is a workaround to not join channels automatically... we need a saner way to navigate for qtopia anyway though,
74 // such as mark first, activate at second click...
75 connect(this, SIGNAL(activated(QModelIndex)), this, SLOT(joinChannel(QModelIndex)));
77 connect(this, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(joinChannel(QModelIndex))); // Qtopia uses single click for activation
81 void BufferView::setModel(QAbstractItemModel *model) {
82 delete selectionModel();
83 if(QTreeView::model()) {
84 disconnect(QTreeView::model(), SIGNAL(layoutChanged()), this, SLOT(updateSelection()));
87 QTreeView::setModel(model);
90 QList<QAction *> oldactions = header()->actions();
91 foreach(QAction *action, oldactions) {
92 header()->removeAction(action);
93 action->deleteLater();
99 connect(model, SIGNAL(layoutChanged()), this, SLOT(updateSelection()));
102 QAction *showSection;
103 for(int i = 1; i < model->columnCount(); i++) {
104 sectionName = (model->headerData(i, Qt::Horizontal, Qt::DisplayRole)).toString();
105 showSection = new QAction(sectionName, header());
106 showSection->setCheckable(true);
107 showSection->setChecked(!isColumnHidden(i));
108 showSection->setProperty("column", i);
109 connect(showSection, SIGNAL(toggled(bool)), this, SLOT(toggleHeader(bool)));
110 header()->addAction(showSection);
115 void BufferView::setFilteredModel(QAbstractItemModel *model_, BufferViewConfig *config) {
116 BufferViewFilter *filter = qobject_cast<BufferViewFilter *>(model());
118 filter->setConfig(config);
124 disconnect(this, 0, model(), 0);
130 BufferViewFilter *filter = new BufferViewFilter(model_, config);
132 connect(this, SIGNAL(removeBuffer(const QModelIndex &)), filter, SLOT(removeBuffer(const QModelIndex &)));
137 void BufferView::setConfig(BufferViewConfig *config) {
138 if(_config == config)
142 disconnect(_config, 0, this, 0);
147 connect(config, SIGNAL(networkIdSet(const NetworkId &)), this, SLOT(setRootIndexForNetworkId(const NetworkId &)));
148 setRootIndexForNetworkId(config->networkId());
150 setRootIndex(QModelIndex());
154 void BufferView::setRootIndexForNetworkId(const NetworkId &networkId) {
155 if(!networkId.isValid() || !model()) {
156 setRootIndex(QModelIndex());
158 int networkCount = model()->rowCount();
160 for(int i = 0; i < networkCount; i++) {
161 child = model()->index(i, 0);
162 if(networkId == model()->data(child, NetworkModel::NetworkIdRole).value<NetworkId>())
168 void BufferView::joinChannel(const QModelIndex &index) {
169 BufferInfo::Type bufferType = (BufferInfo::Type)index.data(NetworkModel::BufferTypeRole).value<int>();
171 if(bufferType != BufferInfo::ChannelBuffer)
174 BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
176 Client::userInput(bufferInfo, QString("/JOIN %1").arg(bufferInfo.bufferName()));
179 void BufferView::keyPressEvent(QKeyEvent *event) {
180 if(event->key() == Qt::Key_Backspace || event->key() == Qt::Key_Delete) {
182 QModelIndex index = selectionModel()->selectedIndexes().first();
183 if(index.isValid()) {
184 emit removeBuffer(index);
187 QTreeView::keyPressEvent(event);
190 // ensure that newly inserted network nodes are expanded per default
191 void BufferView::rowsInserted(const QModelIndex & parent, int start, int end) {
192 QTreeView::rowsInserted(parent, start, end);
193 if(parent.data(NetworkModel::ItemTypeRole) != NetworkModel::NetworkItemType)
196 if(model()->rowCount(parent) == 1 && parent.data(NetworkModel::ItemActiveRole) == true) {
197 // without updating the parent the expand will have no effect... Qt Bug?
203 void BufferView::updateSelection() {
204 MappedSelectionModel *mappedSelectionModel = qobject_cast<MappedSelectionModel *>(selectionModel());
208 mappedSelectionModel->mappedSetCurrentIndex(Client::bufferModel()->standardSelectionModel()->currentIndex(), QItemSelectionModel::Current);
209 mappedSelectionModel->mappedSelect(Client::bufferModel()->standardSelectionModel()->selection(), QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
212 void BufferView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
213 QTreeView::dataChanged(topLeft, bottomRight);
215 // determine how many items have been changed and if any of them is a networkitem
216 // which just swichted from active to inactive or vice versa
217 if(topLeft.data(NetworkModel::ItemTypeRole) != NetworkModel::NetworkItemType)
220 for(int i = topLeft.row(); i <= bottomRight.row(); i++) {
221 QModelIndex networkIdx = topLeft.sibling(topLeft.row(), 0);
222 if(model()->rowCount(networkIdx) == 0)
225 bool isActive = networkIdx.data(NetworkModel::ItemActiveRole).toBool();
227 if(isExpanded(networkIdx) != isActive) setExpanded(networkIdx, true);
229 if(isExpanded(networkIdx) != isActive) setExpanded(networkIdx, isActive);
235 void BufferView::toggleHeader(bool checked) {
236 QAction *action = qobject_cast<QAction *>(sender());
237 header()->setSectionHidden((action->property("column")).toInt(), !checked);
240 void BufferView::showContextMenu(const QPoint &pos) {
241 QModelIndex index = indexAt(pos);
242 if(!index.isValid()) return;
243 QMenu contextMenu(this);
244 QAction *connectNetAction = new QAction(tr("Connect"), this);
245 QAction *disconnectNetAction = new QAction(tr("Disconnect"), this);
246 QAction *joinChannelAction = new QAction(tr("Join Channel"), this);
248 QAction *joinBufferAction = new QAction(tr("Join"), this);
249 QAction *partBufferAction = new QAction(tr("Part"), this);
250 QAction *removeBufferAction = new QAction(tr("Delete buffer"), this);
252 QMenu *hideEventsMenu = new QMenu(tr("Hide Events"), this);
253 QAction *hideJoinAction = hideEventsMenu->addAction(tr("Join Events"));
254 QAction *hidePartAction = hideEventsMenu->addAction(tr("Part Events"));
255 QAction *hideKillAction = hideEventsMenu->addAction(tr("Kill Events"));
256 QAction *hideQuitAction = hideEventsMenu->addAction(tr("Quit Events"));
257 QAction *hideModeAction = hideEventsMenu->addAction(tr("Mode Events"));
258 hideJoinAction->setCheckable(true);
259 hidePartAction->setCheckable(true);
260 hideKillAction->setCheckable(true);
261 hideQuitAction->setCheckable(true);
262 hideModeAction->setCheckable(true);
263 hideJoinAction->setEnabled(false);
264 hidePartAction->setEnabled(false);
265 hideKillAction->setEnabled(false);
266 hideQuitAction->setEnabled(false);
267 hideModeAction->setEnabled(false);
269 QAction *ignoreListAction = new QAction(tr("Ignore list"), this);
270 ignoreListAction->setEnabled(false);
271 QAction *whoBufferAction = new QAction(tr("WHO"), this);
273 if(index.data(NetworkModel::ItemTypeRole) == NetworkModel::NetworkItemType) {
274 if(index.data(NetworkModel::ItemActiveRole).toBool()) {
275 contextMenu.addAction(disconnectNetAction);
276 contextMenu.addSeparator();
277 contextMenu.addAction(joinChannelAction);
279 contextMenu.addAction(connectNetAction);
283 BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
284 QString channelname = index.sibling(index.row(), 0).data().toString();
286 if(index.data(NetworkModel::ItemTypeRole) == NetworkModel::BufferItemType) {
287 if(bufferInfo.type() != BufferInfo::ChannelBuffer && bufferInfo.type() != BufferInfo::QueryBuffer) return;
288 contextMenu.addAction(joinBufferAction);
289 contextMenu.addAction(partBufferAction);
290 contextMenu.addAction(removeBufferAction);
291 contextMenu.addMenu(hideEventsMenu);
292 contextMenu.addAction(ignoreListAction);
293 contextMenu.addAction(whoBufferAction);
295 if(bufferInfo.type() == BufferInfo::ChannelBuffer) {
296 if(index.data(NetworkModel::ItemActiveRole).toBool()) {
297 removeBufferAction->setEnabled(false);
298 removeBufferAction->setToolTip("To delete the buffer, part the channel first.");
299 joinBufferAction->setVisible(false);
300 whoBufferAction->setVisible(false);
302 partBufferAction->setVisible(false);
305 joinBufferAction->setVisible(false);
306 partBufferAction->setVisible(false);
310 QAction *result = contextMenu.exec(QCursor::pos());
311 if(result == connectNetAction || result == disconnectNetAction) {
312 const Network *network = Client::network(index.data(NetworkModel::NetworkIdRole).value<NetworkId>());
314 if(network->connectionState() == Network::Disconnected)
315 network->requestConnect();
317 network->requestDisconnect();
319 if(result == joinChannelAction) {
320 // FIXME no QInputDialog in Qtopia
323 QString channelName = QInputDialog::getText(this, tr("Join Channel"),
324 tr("Input channel name:"),QLineEdit::Normal,
325 QDir::home().dirName(), &ok);
327 if (ok && !channelName.isEmpty()) {
328 BufferInfo bufferInfo = index.child(0,0).data(NetworkModel::BufferInfoRole).value<BufferInfo>();
329 if(bufferInfo.isValid()) {
330 Client::instance()->userInput(bufferInfo, QString("/J %1").arg(channelName));
335 if(result == joinBufferAction) {
336 Client::instance()->userInput(bufferInfo, QString("/JOIN %1").arg(channelname));
338 if(result == partBufferAction) {
339 Client::instance()->userInput(bufferInfo, QString("/PART %1").arg(channelname));
341 if(result == removeBufferAction) {
342 int res = QMessageBox::question(this, tr("Remove buffer permanently?"),
343 tr("Do you want to delete the buffer \"%1\" permanently? This will delete all related data, including all backlog "
344 "data, from the core's database!").arg(bufferInfo.bufferName()),
345 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
346 if(res == QMessageBox::Yes) {
347 Client::removeBuffer(bufferInfo.bufferId());
350 if(result == whoBufferAction) {
351 Client::instance()->userInput(bufferInfo, QString("/WHO %1").arg(channelname));
355 void BufferView::wheelEvent(QWheelEvent* event) {
356 if(UiSettings().value("MouseWheelChangesBuffers", QVariant(true)).toBool() == (bool)(event->modifiers() & Qt::AltModifier))
357 return QTreeView::wheelEvent(event);
359 int rowDelta = ( event->delta() > 0 ) ? -1 : 1;
360 QModelIndex currentIndex = selectionModel()->currentIndex();
361 QModelIndex resultingIndex;
362 if( model()->hasIndex( currentIndex.row() + rowDelta, currentIndex.column(), currentIndex.parent() ) )
364 resultingIndex = currentIndex.sibling( currentIndex.row() + rowDelta, currentIndex.column() );
366 else //if we scroll into a the parent node...
368 QModelIndex parent = currentIndex.parent();
369 QModelIndex aunt = parent.sibling( parent.row() + rowDelta, parent.column() );
371 resultingIndex = aunt.child( model()->rowCount( aunt ) - 1, 0 );
373 resultingIndex = aunt.child( 0, 0 );
374 if( !resultingIndex.isValid() )
377 selectionModel()->setCurrentIndex( resultingIndex, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows );
378 selectionModel()->select( resultingIndex, QItemSelectionModel::ClearAndSelect );
383 QSize BufferView::sizeHint() const {
384 return QTreeView::sizeHint();
387 return QTreeView::sizeHint();
389 if(model()->rowCount() == 0)
390 return QSize(120, 50);
393 for(int i = 0; i < model()->columnCount(); i++) {
394 if(!isColumnHidden(i))
395 columnSize += sizeHintForColumn(i);
397 return QSize(columnSize, 50);
400 // ==============================
402 // ==============================
403 BufferViewDock::BufferViewDock(BufferViewConfig *config, QWidget *parent)
404 : QDockWidget(config->bufferViewName(), parent)
406 setObjectName("BufferViewDock-" + QString::number(config->bufferViewId()));
407 toggleViewAction()->setData(config->bufferViewId());
408 setAllowedAreas(Qt::RightDockWidgetArea|Qt::LeftDockWidgetArea);
409 connect(config, SIGNAL(bufferViewNameSet(const QString &)), this, SLOT(bufferViewRenamed(const QString &)));
412 BufferViewDock::BufferViewDock(QWidget *parent)
413 : QDockWidget(tr("All Buffers"), parent)
415 setObjectName("BufferViewDock--1");
416 toggleViewAction()->setData((int)-1);
417 setAllowedAreas(Qt::RightDockWidgetArea|Qt::LeftDockWidgetArea);
420 void BufferViewDock::bufferViewRenamed(const QString &newName) {
421 setWindowTitle(newName);
422 toggleViewAction()->setText(newName);