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 ***************************************************************************/
22 #include "buffersyncer.h"
23 #include "bufferview.h"
24 #include "networkmodel.h"
27 #include "uisettings.h"
29 /*****************************************
30 * The TreeView showing the Buffers
31 *****************************************/
32 // Please be carefull when reimplementing methods which are used to inform the view about changes to the data
33 // to be on the safe side: call QTreeView's method aswell
34 BufferView::BufferView(QWidget *parent) : QTreeView(parent) {
35 setContextMenuPolicy(Qt::CustomContextMenu);
37 connect(this, SIGNAL(customContextMenuRequested(const QPoint &)),
38 this, SLOT(showContextMenu(const QPoint &)));
41 void BufferView::init() {
43 header()->setContextMenuPolicy(Qt::ActionsContextMenu);
50 #ifndef QT_NO_DRAGANDDROP
53 setDropIndicatorShown(true);
56 setSortingEnabled(true);
57 sortByColumn(0, Qt::AscendingOrder);
58 connect(this, SIGNAL(activated(QModelIndex)), this, SLOT(joinChannel(QModelIndex)));
61 void BufferView::setFilteredModel(QAbstractItemModel *model, BufferViewFilter::Modes mode, QList<NetworkId> nets) {
62 BufferViewFilter *filter = new BufferViewFilter(model, mode, nets);
64 connect(this, SIGNAL(removeBuffer(const QModelIndex &)), filter, SLOT(removeBuffer(const QModelIndex &)));
67 void BufferView::setModel(QAbstractItemModel *model) {
68 delete selectionModel();
69 QTreeView::setModel(model);
73 QList<QAction *> oldactions = header()->actions();
74 foreach(QAction *action, oldactions) {
75 header()->removeAction(action);
76 action->deleteLater();
81 for(int i = 1; i < model->columnCount(); i++) {
82 sectionName = (model->headerData(i, Qt::Horizontal, Qt::DisplayRole)).toString();
83 showSection = new QAction(sectionName, header());
84 showSection->setCheckable(true);
85 showSection->setChecked(!isColumnHidden(i));
86 showSection->setProperty("column", i);
87 connect(showSection, SIGNAL(toggled(bool)), this, SLOT(toggleHeader(bool)));
88 header()->addAction(showSection);
93 void BufferView::joinChannel(const QModelIndex &index) {
94 BufferInfo::Type bufferType = (BufferInfo::Type)index.data(NetworkModel::BufferTypeRole).value<int>();
96 if(bufferType != BufferInfo::ChannelBuffer)
99 BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
101 Client::userInput(bufferInfo, QString("/JOIN %1").arg(bufferInfo.bufferName()));
104 void BufferView::keyPressEvent(QKeyEvent *event) {
105 if(event->key() == Qt::Key_Backspace || event->key() == Qt::Key_Delete) {
107 QModelIndex index = selectionModel()->selectedIndexes().first();
108 if(index.isValid()) {
109 emit removeBuffer(index);
112 QTreeView::keyPressEvent(event);
115 // ensure that newly inserted network nodes are expanded per default
116 void BufferView::rowsInserted(const QModelIndex & parent, int start, int end) {
117 QTreeView::rowsInserted(parent, start, end);
118 if(model()->rowCount(parent) == 1 && parent != QModelIndex()) {
119 // without updating the parent the expand will have no effect... Qt Bug?
125 void BufferView::toggleHeader(bool checked) {
126 QAction *action = qobject_cast<QAction *>(sender());
127 header()->setSectionHidden((action->property("column")).toInt(), !checked);
130 void BufferView::showContextMenu(const QPoint &pos) {
131 QModelIndex index = indexAt(pos);
132 if(!index.isValid()) return;
133 QMenu contextMenu(this);
134 QAction *connectNetAction = new QAction(tr("Connect"), this);
135 QAction *disconnectNetAction = new QAction(tr("Disconnect"), this);
137 QAction *joinBufferAction = new QAction(tr("Join"), this);
138 QAction *partBufferAction = new QAction(tr("Part"), this);
139 QAction *removeBufferAction = new QAction(tr("Delete buffer"), this);
141 QMenu *hideEventsMenu = new QMenu(tr("Hide Events"), this);
142 QAction *hideJoinAction = hideEventsMenu->addAction(tr("Join Events"));
143 QAction *hidePartAction = hideEventsMenu->addAction(tr("Part Events"));
144 QAction *hideKillAction = hideEventsMenu->addAction(tr("Kill Events"));
145 QAction *hideQuitAction = hideEventsMenu->addAction(tr("Quit Events"));
146 QAction *hideModeAction = hideEventsMenu->addAction(tr("Mode Events"));
147 hideJoinAction->setCheckable(true);
148 hidePartAction->setCheckable(true);
149 hideKillAction->setCheckable(true);
150 hideQuitAction->setCheckable(true);
151 hideModeAction->setCheckable(true);
152 hideJoinAction->setEnabled(false);
153 hidePartAction->setEnabled(false);
154 hideKillAction->setEnabled(false);
155 hideQuitAction->setEnabled(false);
156 hideModeAction->setEnabled(false);
158 QAction *ignoreListAction = new QAction(tr("Ignore list"), this);
159 ignoreListAction->setEnabled(false);
160 QAction *whoBufferAction = new QAction(tr("WHO"), this);
162 if(index.data(NetworkModel::ItemTypeRole) == NetworkModel::NetworkItemType) {
163 if(index.data(NetworkModel::ItemActiveRole).toBool()) {
164 contextMenu.addAction(disconnectNetAction);
166 contextMenu.addAction(connectNetAction);
170 BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
171 QString channelname = index.sibling(index.row(), 0).data().toString();
173 if(index.data(NetworkModel::ItemTypeRole) == NetworkModel::BufferItemType) {
174 if(bufferInfo.type() != BufferInfo::ChannelBuffer && bufferInfo.type() != BufferInfo::QueryBuffer) return;
175 contextMenu.addAction(joinBufferAction);
176 contextMenu.addAction(partBufferAction);
177 contextMenu.addAction(removeBufferAction);
178 contextMenu.addMenu(hideEventsMenu);
179 contextMenu.addAction(ignoreListAction);
180 contextMenu.addAction(whoBufferAction);
182 if(bufferInfo.type() == BufferInfo::ChannelBuffer) {
183 if(index.data(NetworkModel::ItemActiveRole).toBool()) {
184 removeBufferAction->setEnabled(false);
185 removeBufferAction->setToolTip("To delete the buffer, part the channel first.");
186 joinBufferAction->setVisible(false);
188 partBufferAction->setVisible(false);
191 joinBufferAction->setVisible(false);
192 partBufferAction->setVisible(false);
196 QAction *result = contextMenu.exec(QCursor::pos());
197 if(result == connectNetAction || result == disconnectNetAction) {
198 const Network *network = Client::network(index.data(NetworkModel::NetworkIdRole).value<NetworkId>());
200 if(network->connectionState() == Network::Disconnected)
201 network->requestConnect();
203 network->requestDisconnect();
205 if(result == joinBufferAction) {
206 Client::instance()->userInput(bufferInfo, QString("/JOIN %1").arg(channelname));
208 if(result == partBufferAction) {
209 Client::instance()->userInput(bufferInfo, QString("/PART %1").arg(channelname));
211 if(result == removeBufferAction) {
212 int res = QMessageBox::question(this, tr("Remove buffer permanently?"),
213 tr("Do you want to delete the buffer \"%1\" permanently? This will delete all related data, including all backlog "
214 "data, from the core's database!").arg(bufferInfo.bufferName()),
215 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
216 if(res == QMessageBox::Yes) {
217 Client::removeBuffer(bufferInfo.bufferId());
220 if(result == whoBufferAction) {
221 Client::instance()->userInput(bufferInfo, QString("/WHO %1").arg(channelname));
225 void BufferView::wheelEvent(QWheelEvent* event)
228 if(s.value("MouseWheelChangesBuffers",QVariant(true)).toBool()) {
229 int rowDelta = ( event->delta() > 0 ) ? -1 : 1;
230 QModelIndex currentIndex = selectionModel()->currentIndex();
231 QModelIndex resultingIndex;
232 if( model()->hasIndex( currentIndex.row() + rowDelta, currentIndex.column(), currentIndex.parent() ) )
234 resultingIndex = currentIndex.sibling( currentIndex.row() + rowDelta, currentIndex.column() );
236 else //if we scroll into a the parent node...
238 QModelIndex parent = currentIndex.parent();
239 QModelIndex aunt = parent.sibling( parent.row() + rowDelta, parent.column() );
241 resultingIndex = aunt.child( model()->rowCount( aunt ) - 1, 0 );
243 resultingIndex = aunt.child( 0, 0 );
244 if( !resultingIndex.isValid() )
247 selectionModel()->setCurrentIndex( resultingIndex, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows );
248 selectionModel()->select( resultingIndex, QItemSelectionModel::ClearAndSelect );
250 QAbstractScrollArea::wheelEvent(event);