Newly created buffers are now selected automatically.
[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 = 0; 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   // select newly inserted buffers
124   if(parent.data(NetworkModel::ItemTypeRole) != NetworkModel::NetworkItemType)
125     return;
126
127   QModelIndex newCurrent = parent.child(end, 0);
128   selectionModel()->setCurrentIndex(newCurrent, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
129   selectionModel()->select(newCurrent, QItemSelectionModel::ClearAndSelect);
130 }
131
132 void BufferView::toggleHeader(bool checked) {
133   QAction *action = qobject_cast<QAction *>(sender());
134   header()->setSectionHidden((action->property("column")).toInt(), !checked);
135 }
136
137 void BufferView::showContextMenu(const QPoint &pos) {
138   QModelIndex index = indexAt(pos);
139   if(!index.isValid()) return;
140   QMenu contextMenu(this);
141   QAction *connectNetAction = new QAction(tr("Connect"), this);
142   QAction *disconnectNetAction = new QAction(tr("Disconnect"), this);
143
144   QAction *joinBufferAction = new QAction(tr("Join"), this);
145   QAction *partBufferAction = new QAction(tr("Part"), this);
146   QAction *removeBufferAction = new QAction(tr("Delete buffer"), this);
147
148   QMenu *hideEventsMenu = new QMenu(tr("Hide Events"), this);
149   QAction *hideJoinAction = hideEventsMenu->addAction(tr("Join Events"));
150   QAction *hidePartAction = hideEventsMenu->addAction(tr("Part Events"));
151   QAction *hideKillAction = hideEventsMenu->addAction(tr("Kill Events"));
152   QAction *hideQuitAction = hideEventsMenu->addAction(tr("Quit Events"));
153   QAction *hideModeAction = hideEventsMenu->addAction(tr("Mode Events"));
154   hideJoinAction->setCheckable(true);
155   hidePartAction->setCheckable(true);
156   hideKillAction->setCheckable(true);
157   hideQuitAction->setCheckable(true);
158   hideModeAction->setCheckable(true);
159   hideJoinAction->setEnabled(false);
160   hidePartAction->setEnabled(false);
161   hideKillAction->setEnabled(false);
162   hideQuitAction->setEnabled(false);
163   hideModeAction->setEnabled(false);
164
165   QAction *ignoreListAction = new QAction(tr("Ignore list"), this);
166   ignoreListAction->setEnabled(false);
167   QAction *whoBufferAction = new QAction(tr("WHO"), this);
168
169   if(index.data(NetworkModel::ItemTypeRole) == NetworkModel::NetworkItemType) {
170     if(index.data(NetworkModel::ItemActiveRole).toBool()) {
171       contextMenu.addAction(disconnectNetAction);
172     } else {
173       contextMenu.addAction(connectNetAction);
174     }
175   }
176
177   BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
178   QString channelname = index.sibling(index.row(), 0).data().toString();
179
180   if(index.data(NetworkModel::ItemTypeRole) == NetworkModel::BufferItemType) {
181     if(bufferInfo.type() != BufferInfo::ChannelBuffer && bufferInfo.type() != BufferInfo::QueryBuffer) return;
182     contextMenu.addAction(joinBufferAction);
183     contextMenu.addAction(partBufferAction);
184     contextMenu.addAction(removeBufferAction);
185     contextMenu.addMenu(hideEventsMenu);
186     contextMenu.addAction(ignoreListAction);
187     contextMenu.addAction(whoBufferAction);
188
189     if(bufferInfo.type() == BufferInfo::ChannelBuffer) {
190       if(index.data(NetworkModel::ItemActiveRole).toBool()) {
191         removeBufferAction->setEnabled(false);
192         removeBufferAction->setToolTip("To delete the buffer, part the channel first.");
193         joinBufferAction->setVisible(false);
194       } else {
195         partBufferAction->setVisible(false);
196       }
197     } else {
198       joinBufferAction->setVisible(false);
199       partBufferAction->setVisible(false);
200     }
201   }
202
203   QAction *result = contextMenu.exec(QCursor::pos());
204   if(result == connectNetAction || result == disconnectNetAction) {
205     const Network *network = Client::network(index.data(NetworkModel::NetworkIdRole).value<NetworkId>());
206     if(!network) return;
207     if(network->connectionState() == Network::Disconnected) 
208       network->requestConnect();
209     else 
210       network->requestDisconnect();
211   } else
212   if(result == joinBufferAction) {
213     Client::instance()->userInput(bufferInfo, QString("/JOIN %1").arg(channelname));
214   } else
215   if(result == partBufferAction) {
216     Client::instance()->userInput(bufferInfo, QString("/PART %1").arg(channelname));
217   } else
218   if(result == removeBufferAction) {
219     int res = QMessageBox::question(this, tr("Remove buffer permanently?"),
220                                     tr("Do you want to delete the buffer \"%1\" permanently? This will delete all related data, including all backlog "
221                                        "data, from the core's database!").arg(bufferInfo.bufferName()),
222                                         QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
223     if(res == QMessageBox::Yes) {
224       Client::removeBuffer(bufferInfo.bufferId());
225     } 
226   } else 
227   if(result == whoBufferAction) {
228     Client::instance()->userInput(bufferInfo, QString("/WHO %1").arg(channelname));
229   }
230 }
231