modernize: Prefer default member init over ctor init
[quassel.git] / src / qtui / channellistdlg.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 "channellistdlg.h"
22
23 #include <QHeaderView>
24 #include <QHBoxLayout>
25 #include <QSpacerItem>
26
27 #include "client.h"
28 #include "clientirclisthelper.h"
29 #include "icon.h"
30
31 ChannelListDlg::ChannelListDlg(QWidget *parent)
32     : QDialog(parent),
33     _ircListModel(this),
34     _sortFilter(this)
35 {
36     _sortFilter.setSourceModel(&_ircListModel);
37     _sortFilter.setFilterCaseSensitivity(Qt::CaseInsensitive);
38     _sortFilter.setFilterKeyColumn(-1);
39
40     ui.setupUi(this);
41     ui.advancedModeLabel->setPixmap(icon::get("edit-rename").pixmap(22));
42
43     ui.channelListView->setSelectionBehavior(QAbstractItemView::SelectRows);
44     ui.channelListView->setSelectionMode(QAbstractItemView::SingleSelection);
45     ui.channelListView->setAlternatingRowColors(true);
46     ui.channelListView->setTabKeyNavigation(false);
47     ui.channelListView->setModel(&_sortFilter);
48     ui.channelListView->setSortingEnabled(true);
49     // Sort A-Z by default
50     ui.channelListView->sortByColumn(0, Qt::AscendingOrder);
51     ui.channelListView->verticalHeader()->hide();
52     ui.channelListView->horizontalHeader()->setStretchLastSection(true);
53
54     ui.searchChannelsButton->setAutoDefault(false);
55
56     setWindowIcon(icon::get("format-list-unordered"));
57
58     connect(ui.advancedModeLabel, SIGNAL(clicked()), this, SLOT(toggleMode()));
59     connect(ui.searchChannelsButton, SIGNAL(clicked()), this, SLOT(requestSearch()));
60     connect(ui.channelNameLineEdit, SIGNAL(returnPressed()), this, SLOT(requestSearch()));
61     connect(ui.filterLineEdit, SIGNAL(textChanged(QString)), &_sortFilter, SLOT(setFilterFixedString(QString)));
62     connect(Client::ircListHelper(), SIGNAL(channelListReceived(const NetworkId &, const QStringList &, QList<IrcListHelper::ChannelDescription> )),
63         this, SLOT(receiveChannelList(NetworkId, QStringList, QList<IrcListHelper::ChannelDescription> )));
64     connect(Client::ircListHelper(), SIGNAL(finishedListReported(const NetworkId &)), this, SLOT(reportFinishedList()));
65     connect(Client::ircListHelper(), SIGNAL(errorReported(const QString &)), this, SLOT(showError(const QString &)));
66     connect(ui.channelListView, SIGNAL(activated(QModelIndex)), this, SLOT(joinChannel(QModelIndex)));
67
68     setAdvancedMode(false);
69     enableQuery(true);
70     showFilterLine(false);
71     showErrors(false);
72
73     // Set initial input focus
74     updateInputFocus();
75 }
76
77
78 void ChannelListDlg::setNetwork(NetworkId netId)
79 {
80     if (_netId == netId)
81         return;
82
83     _netId = netId;
84     _ircListModel.setChannelList();
85     showFilterLine(false);
86 }
87
88
89 void ChannelListDlg::setChannelFilters(const QString &channelFilters)
90 {
91     // Enable advanced mode if searching
92     setAdvancedMode(!channelFilters.isEmpty());
93     // Set channel search text after setting advanced mode so it's not cleared
94     ui.channelNameLineEdit->setText(channelFilters.trimmed());
95 }
96
97
98 void ChannelListDlg::requestSearch()
99 {
100     if (!_netId.isValid()) {
101         // No valid network set yet
102         return;
103     }
104
105     _listFinished = false;
106     enableQuery(false);
107     showErrors(false);
108     QStringList channelFilters;
109     channelFilters << ui.channelNameLineEdit->text().trimmed();
110     Client::ircListHelper()->requestChannelList(_netId, channelFilters);
111 }
112
113
114 void ChannelListDlg::receiveChannelList(const NetworkId &netId, const QStringList &channelFilters, const QList<IrcListHelper::ChannelDescription> &channelList)
115 {
116     Q_UNUSED(channelFilters)
117     if (netId != _netId)
118         return;
119
120     showFilterLine(!channelList.isEmpty());
121     _ircListModel.setChannelList(channelList);
122     enableQuery(_listFinished);
123     // Reset input focus since UI changed
124     updateInputFocus();
125 }
126
127
128 void ChannelListDlg::showFilterLine(bool show)
129 {
130     ui.line->setVisible(show);
131     ui.filterLabel->setVisible(show);
132     ui.filterLineEdit->setVisible(show);
133 }
134
135
136 void ChannelListDlg::enableQuery(bool enable)
137 {
138     ui.channelNameLineEdit->setEnabled(enable);
139     ui.searchChannelsButton->setEnabled(enable);
140 }
141
142
143 void ChannelListDlg::setAdvancedMode(bool advanced)
144 {
145     _advancedMode = advanced;
146
147     if (advanced) {
148         if (_simpleModeSpacer) {
149             ui.searchLayout->removeItem(_simpleModeSpacer);
150             delete _simpleModeSpacer;
151             _simpleModeSpacer = nullptr;
152         }
153         ui.advancedModeLabel->setPixmap(icon::get("edit-clear-locationbar-rtl").pixmap(16));
154     }
155     else {
156         if (!_simpleModeSpacer) {
157             _simpleModeSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
158             ui.searchLayout->insertSpacerItem(0, _simpleModeSpacer);
159         }
160         ui.advancedModeLabel->setPixmap(icon::get("edit-rename").pixmap(16));
161     }
162
163     ui.channelNameLineEdit->clear();
164     ui.channelNameLineEdit->setVisible(advanced);
165     ui.searchPatternLabel->setVisible(advanced);
166 }
167
168
169 void ChannelListDlg::updateInputFocus()
170 {
171     // Update keyboard focus to match what options are available.  Prioritize the channel name
172     // editor as one likely won't need to filter when already limiting the list.
173     if (ui.channelNameLineEdit->isVisible()) {
174         ui.channelNameLineEdit->setFocus();
175     } else if (ui.filterLineEdit->isVisible()) {
176         ui.filterLineEdit->setFocus();
177     }
178 }
179
180
181 void ChannelListDlg::showErrors(bool show)
182 {
183     if (!show) {
184         ui.errorTextEdit->clear();
185     }
186     ui.errorLabel->setVisible(show);
187     ui.errorTextEdit->setVisible(show);
188 }
189
190
191 void ChannelListDlg::reportFinishedList()
192 {
193     _listFinished = true;
194 }
195
196
197 void ChannelListDlg::showError(const QString &error)
198 {
199     showErrors(true);
200     ui.errorTextEdit->moveCursor(QTextCursor::End);
201     ui.errorTextEdit->insertPlainText(error + "\n");
202 }
203
204
205 void ChannelListDlg::joinChannel(const QModelIndex &index)
206 {
207     Client::instance()->userInput(BufferInfo::fakeStatusBuffer(_netId), QString("/JOIN %1").arg(index.sibling(index.row(), 0).data().toString()));
208 }