modernize: Replace most remaining old-style connects by PMF ones
[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 <QMenu>
25 #include <QContextMenuEvent>
26
27 #include "action.h"
28 #include "buffermodel.h"
29 #include "chatmonitorfilter.h"
30 #include "chatlinemodel.h"
31 #include "chatitem.h"
32 #include "chatscene.h"
33 #include "client.h"
34 #include "clientignorelistmanager.h"
35 #include "icon.h"
36 #include "networkmodel.h"
37 #include "messagemodel.h"
38 #include "qtuisettings.h"
39 #include "settingspagedlg.h"
40 #include "settingspages/chatmonitorsettingspage.h"
41
42 ChatMonitorView::ChatMonitorView(ChatMonitorFilter *filter, QWidget *parent)
43     : ChatView(filter, parent),
44     _filter(filter)
45 {
46     scene()->setSenderCutoffMode(ChatScene::CutoffLeft);
47     // The normal message prefixes get replaced by the network and buffer name.  Re-add brackets for
48     // all message types.
49     scene()->setAlwaysBracketSender(true);
50     connect(Client::instance(), &Client::coreConnectionStateChanged, this, &ChatMonitorView::coreConnectionStateChanged);
51 }
52
53
54 void ChatMonitorView::addActionsToMenu(QMenu *menu, const QPointF &pos)
55 {
56     ChatView::addActionsToMenu(menu, pos);
57     menu->addSeparator();
58     auto showOwnNicksAction = new Action(tr("Show Own Messages"), menu, _filter, SLOT(setShowOwnMessages(bool)));
59     showOwnNicksAction->setCheckable(true);
60     showOwnNicksAction->setChecked(_filter->showOwnMessages());
61     menu->addAction(showOwnNicksAction);
62
63     if (scene()->columnByScenePos(pos) == ChatLineModel::SenderColumn) {
64         menu->addSeparator();
65
66         auto showNetworkAction = new Action(tr("Show Network Name"), menu, this, SLOT(showFieldsChanged(bool)));
67         showNetworkAction->setCheckable(true);
68         showNetworkAction->setChecked(_filter->showFields() & ChatMonitorFilter::NetworkField);
69         showNetworkAction->setData(ChatMonitorFilter::NetworkField);
70         menu->addAction(showNetworkAction);
71
72         auto showBufferAction = new Action(tr("Show Buffer Name"), menu, this, SLOT(showFieldsChanged(bool)));
73         showBufferAction->setCheckable(true);
74         showBufferAction->setChecked(_filter->showFields() & ChatMonitorFilter::BufferField);
75         showBufferAction->setData(ChatMonitorFilter::BufferField);
76         menu->addAction(showBufferAction);
77     }
78
79     menu->addSeparator();
80     menu->addAction(new Action(icon::get("configure"), tr("Configure..."), menu, this, SLOT(showSettingsPage())));
81 }
82
83
84 void ChatMonitorView::mouseDoubleClickEvent(QMouseEvent *event)
85 {
86     if (scene()->columnByScenePos(event->pos()) != ChatLineModel::SenderColumn) {
87         ChatView::mouseDoubleClickEvent(event);
88         return;
89     }
90
91     ChatItem *chatItem = scene()->chatItemAt(mapToScene(event->pos()));
92     if (!chatItem) {
93         event->ignore();
94         return;
95     }
96
97     event->accept();
98     BufferId bufferId = chatItem->data(MessageModel::BufferIdRole).value<BufferId>();
99     if (!bufferId.isValid())
100         return;
101
102     Client::bufferModel()->switchToBuffer(bufferId);
103 }
104
105
106 void ChatMonitorView::showFieldsChanged(bool checked)
107 {
108     auto *showAction = qobject_cast<QAction *>(sender());
109     if (!showAction)
110         return;
111
112     if (checked)
113         _filter->addShowField(showAction->data().toInt());
114     else
115         _filter->removeShowField(showAction->data().toInt());
116 }
117
118
119 void ChatMonitorView::showSettingsPage()
120 {
121     SettingsPageDlg dlg(new ChatMonitorSettingsPage(), this);
122     dlg.exec();
123 }
124
125
126 // connect only after client is synced to core since ChatMonitorView is created before
127 // the ignoreListManager
128 void ChatMonitorView::coreConnectionStateChanged(bool connected)
129 {
130     if (connected)
131         connect(Client::ignoreListManager(), &ClientIgnoreListManager::ignoreListChanged, _filter, &ChatMonitorFilter::invalidateFilter);
132 }