50e5b1ff99ea630c48a0c36ffe90149bffe01653
[quassel.git] / src / qtui / chatmonitorview.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include "chatmonitorview.h"
22
23 #include <QAction>
24 #include <QIcon>
25 #include <QMenu>
26 #include <QContextMenuEvent>
27
28 #include "chatmonitorfilter.h"
29 #include "chatlinemodel.h"
30 #include "chatitem.h"
31 #include "chatscene.h"
32 #include "client.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     // The normal message prefixes get replaced by the network and buffer name.  Re-add brackets for
47     // all message types.
48     scene()->setAlwaysBracketSender(true);
49     connect(Client::instance(), SIGNAL(coreConnectionStateChanged(bool)), this, SLOT(coreConnectionStateChanged(bool)));
50 }
51
52
53 void ChatMonitorView::addActionsToMenu(QMenu *menu, const QPointF &pos)
54 {
55     ChatView::addActionsToMenu(menu, pos);
56     menu->addSeparator();
57     QAction *showOwnNicksAction = menu->addAction(tr("Show Own Messages"), _filter, SLOT(setShowOwnMessages(bool)));
58     showOwnNicksAction->setCheckable(true);
59     showOwnNicksAction->setChecked(_filter->showOwnMessages());
60
61     if (scene()->columnByScenePos(pos) == ChatLineModel::SenderColumn) {
62         menu->addSeparator();
63
64         QAction *showNetworkAction = menu->addAction(tr("Show Network Name"), this, SLOT(showFieldsChanged(bool)));
65         showNetworkAction->setCheckable(true);
66         showNetworkAction->setChecked(_filter->showFields() & ChatMonitorFilter::NetworkField);
67         showNetworkAction->setData(ChatMonitorFilter::NetworkField);
68
69         QAction *showBufferAction = menu->addAction(tr("Show Buffer Name"), this, SLOT(showFieldsChanged(bool)));
70         showBufferAction->setCheckable(true);
71         showBufferAction->setChecked(_filter->showFields() & ChatMonitorFilter::BufferField);
72         showBufferAction->setData(ChatMonitorFilter::BufferField);
73     }
74
75     menu->addSeparator();
76     menu->addAction(QIcon::fromTheme("configure"), tr("Configure..."), this, SLOT(showSettingsPage()));
77 }
78
79
80 void ChatMonitorView::mouseDoubleClickEvent(QMouseEvent *event)
81 {
82     if (scene()->columnByScenePos(event->pos()) != ChatLineModel::SenderColumn) {
83         ChatView::mouseDoubleClickEvent(event);
84         return;
85     }
86
87     ChatItem *chatItem = scene()->chatItemAt(mapToScene(event->pos()));
88     if (!chatItem) {
89         event->ignore();
90         return;
91     }
92
93     event->accept();
94     BufferId bufferId = chatItem->data(MessageModel::BufferIdRole).value<BufferId>();
95     if (!bufferId.isValid())
96         return;
97
98     Client::bufferModel()->switchToBuffer(bufferId);
99 }
100
101
102 void ChatMonitorView::showFieldsChanged(bool checked)
103 {
104     QAction *showAction = qobject_cast<QAction *>(sender());
105     if (!showAction)
106         return;
107
108     if (checked)
109         _filter->addShowField(showAction->data().toInt());
110     else
111         _filter->removeShowField(showAction->data().toInt());
112 }
113
114
115 void ChatMonitorView::showSettingsPage()
116 {
117     SettingsPageDlg dlg(new ChatMonitorSettingsPage(), this);
118     dlg.exec();
119 }
120
121
122 // connect only after client is synced to core since ChatMonitorView is created before
123 // the ignoreListManager
124 void ChatMonitorView::coreConnectionStateChanged(bool connected)
125 {
126     if (connected)
127         connect(Client::ignoreListManager(), SIGNAL(ignoreListChanged()), _filter, SLOT(invalidateFilter()));
128 }