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