common: Make SyncableObject non-copyable
[quassel.git] / src / qtui / chatmonitorview.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2020 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 <QContextMenuEvent>
25 #include <QMenu>
26
27 #include "action.h"
28 #include "buffermodel.h"
29 #include "chatitem.h"
30 #include "chatlinemodel.h"
31 #include "chatmonitorfilter.h"
32 #include "chatscene.h"
33 #include "client.h"
34 #include "clientignorelistmanager.h"
35 #include "icon.h"
36 #include "messagemodel.h"
37 #include "networkmodel.h"
38 #include "qtuisettings.h"
39 #include "settingspagedlg.h"
40
41 #include "settingspages/chatmonitorsettingspage.h"
42
43 ChatMonitorView::ChatMonitorView(ChatMonitorFilter* filter, QWidget* parent)
44     : ChatView(filter, parent)
45     , _filter(filter)
46 {
47     scene()->setSenderCutoffMode(ChatScene::CutoffLeft);
48     // The normal message prefixes get replaced by the network and buffer name.  Re-add brackets for
49     // all message types.
50     scene()->setAlwaysBracketSender(true);
51     connect(Client::instance(), &Client::coreConnectionStateChanged, this, &ChatMonitorView::coreConnectionStateChanged);
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, &ChatMonitorFilter::setShowOwnMessages);
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, &ChatMonitorView::showFieldsChanged);
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, &ChatMonitorView::showFieldsChanged);
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, &ChatMonitorView::showSettingsPage));
81 }
82
83 void ChatMonitorView::mouseDoubleClickEvent(QMouseEvent* event)
84 {
85     if (scene()->columnByScenePos(event->pos()) != ChatLineModel::SenderColumn) {
86         ChatView::mouseDoubleClickEvent(event);
87         return;
88     }
89
90     ChatItem* chatItem = scene()->chatItemAt(mapToScene(event->pos()));
91     if (!chatItem) {
92         event->ignore();
93         return;
94     }
95
96     event->accept();
97     BufferId bufferId = chatItem->data(MessageModel::BufferIdRole).value<BufferId>();
98     if (!bufferId.isValid())
99         return;
100
101     Client::bufferModel()->switchToBuffer(bufferId);
102 }
103
104 void ChatMonitorView::showFieldsChanged(bool checked)
105 {
106     auto* showAction = qobject_cast<QAction*>(sender());
107     if (!showAction)
108         return;
109
110     if (checked)
111         _filter->addShowField(showAction->data().toInt());
112     else
113         _filter->removeShowField(showAction->data().toInt());
114 }
115
116 void ChatMonitorView::showSettingsPage()
117 {
118     SettingsPageDlg dlg(new ChatMonitorSettingsPage(), this);
119     dlg.exec();
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(), &ClientIgnoreListManager::ignoreListChanged, _filter, &ChatMonitorFilter::invalidateFilter);
128 }