Provide some new accessors for Chat{View|Scene}
[quassel.git] / src / qtui / chatmonitorview.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 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 "chatmonitorview.h"
22
23 #include <QAction>
24 #include <QMenu>
25 #include <QContextMenuEvent>
26
27 #include "chatmonitorfilter.h"
28 #include "chatlinemodel.h"
29 #include "chatitem.h"
30 #include "chatscene.h"
31 #include "client.h"
32 #include "iconloader.h"
33 #include "networkmodel.h"
34 #include "buffermodel.h"
35 #include "messagemodel.h"
36 #include "qtuisettings.h"
37 #include "settingspagedlg.h"
38 #include "settingspages/chatmonitorsettingspage.h"
39 #include "clientignorelistmanager.h"
40
41 ChatMonitorView::ChatMonitorView(ChatMonitorFilter *filter, QWidget *parent)
42   : ChatView(filter, parent),
43     _filter(filter)
44 {
45   scene()->setSenderCutoffMode(ChatScene::CutoffLeft);
46   connect(Client::instance(), SIGNAL(coreConnectionStateChanged(bool)), this, SLOT(coreConnectionStateChanged(bool)));
47 }
48
49 void ChatMonitorView::addActionsToMenu(QMenu *menu, const QPointF &pos) {
50   ChatView::addActionsToMenu(menu, pos);
51   menu->addSeparator();
52   QAction *showOwnNicksAction = menu->addAction(tr("Show Own Messages"), _filter, SLOT(setShowOwnMessages(bool)));
53   showOwnNicksAction->setCheckable(true);
54   showOwnNicksAction->setChecked(_filter->showOwnMessages());
55
56   if(scene()->columnByScenePos(pos) == ChatLineModel::SenderColumn) {
57     menu->addSeparator();
58
59     QAction *showNetworkAction = menu->addAction(tr("Show Network Name"), this, SLOT(showFieldsChanged(bool)));
60     showNetworkAction->setCheckable(true);
61     showNetworkAction->setChecked(_filter->showFields() & ChatMonitorFilter::NetworkField);
62     showNetworkAction->setData(ChatMonitorFilter::NetworkField);
63
64     QAction *showBufferAction = menu->addAction(tr("Show Buffer Name"), this, SLOT(showFieldsChanged(bool)));
65     showBufferAction->setCheckable(true);
66     showBufferAction->setChecked(_filter->showFields() & ChatMonitorFilter::BufferField);
67     showBufferAction->setData(ChatMonitorFilter::BufferField);
68   }
69
70   menu->addSeparator();
71   menu->addAction(SmallIcon("configure"), tr("Configure..."), this, SLOT(showSettingsPage()));
72 }
73
74 void ChatMonitorView::mouseDoubleClickEvent(QMouseEvent *event) {
75   if(scene()->columnByScenePos(event->pos()) != ChatLineModel::SenderColumn) {
76     ChatView::mouseDoubleClickEvent(event);
77     return;
78   }
79
80   ChatItem *chatItem = scene()->chatItemAt(mapToScene(event->pos()));
81   if(!chatItem) {
82     event->ignore();
83     return;
84   }
85
86   event->accept();
87   BufferId bufferId = chatItem->data(MessageModel::BufferIdRole).value<BufferId>();
88   if(!bufferId.isValid())
89     return;
90
91   Client::bufferModel()->switchToBuffer(bufferId);
92 }
93
94 void ChatMonitorView::showFieldsChanged(bool checked) {
95   QAction *showAction = qobject_cast<QAction *>(sender());
96   if(!showAction)
97     return;
98
99   if(checked)
100     _filter->addShowField(showAction->data().toInt());
101   else
102     _filter->removeShowField(showAction->data().toInt());
103 }
104
105 void ChatMonitorView::showSettingsPage() {
106   SettingsPageDlg dlg(new ChatMonitorSettingsPage(), this);
107   dlg.exec();
108 }
109
110 // connect only after client is synced to core since ChatMonitorView is created before
111 // the ignoreListManager
112 void ChatMonitorView::coreConnectionStateChanged(bool connected) {
113   if(connected)
114     connect(Client::ignoreListManager(), SIGNAL(ignoreListChanged()), _filter, SLOT(invalidateFilter()));
115 }