filtersettingspage.cpp 9.08 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
** Commercial Usage
10
**
11
12
13
14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
15
**
16
** GNU Lesser General Public License Usage
17
**
18
19
20
21
22
23
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
**
28
**************************************************************************/
con's avatar
con committed
29
30

#include "filtersettingspage.h"
31

con's avatar
con committed
32
#include "filternamedialog.h"
33
34
#include "helpconstants.h"

kh1's avatar
kh1 committed
35
36
#include <coreplugin/helpmanager.h>

37
#include <QtCore/QCoreApplication>
con's avatar
con committed
38
39
40

#include <QtGui/QFileDialog>
#include <QtGui/QMessageBox>
41

con's avatar
con committed
42
using namespace Help::Internal;
kh's avatar
kh committed
43

44
FilterSettingsPage::FilterSettingsPage()
con's avatar
con committed
45
46
47
{
}

48
QString FilterSettingsPage::id() const
con's avatar
con committed
49
{
50
    return QLatin1String("D.Filters");
51
52
}

53
QString FilterSettingsPage::displayName() const
54
55
{
    return tr("Filters");
con's avatar
con committed
56
57
58
59
}

QString FilterSettingsPage::category() const
{
60
    return QLatin1String(Help::Constants::HELP_CATEGORY);
con's avatar
con committed
61
62
}

63
QString FilterSettingsPage::displayCategory() const
con's avatar
con committed
64
{
65
    return QCoreApplication::translate("Help", Help::Constants::HELP_TR_CATEGORY);
con's avatar
con committed
66
67
}

68
69
70
71
72
QIcon FilterSettingsPage::categoryIcon() const
{
    return QIcon(QLatin1String(Help::Constants::HELP_CATEGORY_ICON));
}

con's avatar
con committed
73
74
QWidget *FilterSettingsPage::createPage(QWidget *parent)
{
75
76
77
    QWidget *widget = new QWidget(parent);
    m_ui.setupUi(widget);

78
    updateFilterPage();
79

con's avatar
con committed
80
81
82
    connect(m_ui.attributeWidget, SIGNAL(itemChanged(QTreeWidgetItem*, int)),
        this, SLOT(updateFilterMap()));
    connect(m_ui.filterWidget,
kh's avatar
kh committed
83
84
85
86
87
        SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)), this,
        SLOT(updateAttributes(QListWidgetItem*)));
    connect(m_ui.filterAddButton, SIGNAL(clicked()), this, SLOT(addFilter()));
    connect(m_ui.filterRemoveButton, SIGNAL(clicked()), this,
        SLOT(removeFilter()));
kh1's avatar
kh1 committed
88
89
    connect(Core::HelpManager::instance(), SIGNAL(documentationChanged()),
        this, SLOT(updateFilterPage()));
kh's avatar
kh committed
90

91
92
93
94
95
    if (m_searchKeywords.isEmpty()) {
        m_searchKeywords = m_ui.filterGroupBox->title() + QLatin1Char(' ')
            + m_ui.attributesGroupBox->title();
    }
    return widget;
con's avatar
con committed
96
97
98
99
100
101
102
103
}

void FilterSettingsPage::updateFilterPage()
{
    m_ui.filterWidget->clear();
    m_ui.attributeWidget->clear();

    m_filterMapBackup.clear();
104
105
106

    QString lastTrUnfiltered;
    const QString trUnfiltered = tr("Unfiltered");
107
108
    Core::HelpManager *manager = Core::HelpManager::instance();
    if (manager->customValue(Help::Constants::WeAddedFilterKey).toInt() == 1) {
109
        lastTrUnfiltered =
110
            manager->customValue(Help::Constants::PreviousFilterNameKey).toString();
111
112
    }

113
    Core::HelpManager::Filters filters = manager->userDefinedFilters();
114
115
116
    Core::HelpManager::Filters::const_iterator it;
    for (it = filters.constBegin(); it != filters.constEnd(); ++it) {
        const QString &filter = it.key();
117
118
119
        if (filter == trUnfiltered || filter == lastTrUnfiltered)
            continue;

120
        m_filterMapBackup.insert(filter, it.value());
con's avatar
con committed
121
        if (!m_filterMap.contains(filter))
122
            m_filterMap.insert(filter, it.value());
con's avatar
con committed
123
124
125
    }
    m_ui.filterWidget->addItems(m_filterMap.keys());

126
127
128
129
130
    QSet<QString> attributes;
    filters = manager->filters();
    for (it = filters.constBegin(); it != filters.constEnd(); ++it)
        attributes += it.value().toSet();

131
132
    foreach (const QString &attribute, attributes)
        new QTreeWidgetItem(m_ui.attributeWidget, QStringList(attribute));
kh's avatar
kh committed
133

kh1's avatar
kh1 committed
134
    if (!m_filterMap.keys().isEmpty()) {
con's avatar
con committed
135
        m_ui.filterWidget->setCurrentRow(0);
136
137
        updateAttributes(m_ui.filterWidget->currentItem());
    }
con's avatar
con committed
138
139
140
141
142
143
144
}

void FilterSettingsPage::updateAttributes(QListWidgetItem *item)
{
    QStringList checkedList;
    if (item)
        checkedList = m_filterMap.value(item->text());
145
146
147

    for (int i = 0; i < m_ui.attributeWidget->topLevelItemCount(); ++i) {
        QTreeWidgetItem *itm = m_ui.attributeWidget->topLevelItem(i);
con's avatar
con committed
148
149
150
151
152
        if (checkedList.contains(itm->text(0)))
            itm->setCheckState(0, Qt::Checked);
        else
            itm->setCheckState(0, Qt::Unchecked);
    }
153
154

    updateFilterDescription(item ? item->text() : QString());
con's avatar
con committed
155
156
157
158
159
160
}

void FilterSettingsPage::updateFilterMap()
{
    if (!m_ui.filterWidget->currentItem())
        return;
161
162

    const QString &filter = m_ui.filterWidget->currentItem()->text();
con's avatar
con committed
163
164
    if (!m_filterMap.contains(filter))
        return;
kh's avatar
kh committed
165

con's avatar
con committed
166
    QStringList newAtts;
167
168
    for (int i = 0; i < m_ui.attributeWidget->topLevelItemCount(); ++i) {
        QTreeWidgetItem *itm = m_ui.attributeWidget->topLevelItem(i);
con's avatar
con committed
169
170
171
        if (itm->checkState(0) == Qt::Checked)
            newAtts.append(itm->text(0));
    }
kh's avatar
kh committed
172
    m_filterMap[filter] = newAtts;
173
    updateFilterDescription(filter);
con's avatar
con committed
174
175
176
177
}

void FilterSettingsPage::addFilter()
{
178
    FilterNameDialog dia(m_ui.filterWidget);
con's avatar
con committed
179
180
181
    if (dia.exec() == QDialog::Rejected)
        return;

182
    const QString &filterName = dia.filterName();
con's avatar
con committed
183
184
185
186
187
    if (!m_filterMap.contains(filterName)) {
        m_filterMap.insert(filterName, QStringList());
        m_ui.filterWidget->addItem(filterName);
    }

188
189
    const QList<QListWidgetItem*> &lst = m_ui.filterWidget->findItems(filterName,
        Qt::MatchCaseSensitive);
kh's avatar
kh committed
190
    m_ui.filterWidget->setCurrentItem(lst.first());
con's avatar
con committed
191
192
193
194
}

void FilterSettingsPage::removeFilter()
{
195
196
    QListWidgetItem *item =
        m_ui.filterWidget->takeItem(m_ui.filterWidget->currentRow());
con's avatar
con committed
197
198
199
200
201
202
203
204
    if (!item)
        return;

    m_filterMap.remove(item->text());
    m_removedFilters.append(item->text());
    delete item;
    if (m_ui.filterWidget->count())
        m_ui.filterWidget->setCurrentRow(0);
205
206
207

    item = m_ui.filterWidget->item(m_ui.filterWidget->currentRow());
    updateFilterDescription(item ? item->text() : QString());
con's avatar
con committed
208
209
}

210
void FilterSettingsPage::apply()
con's avatar
con committed
211
{
212
213
214
215
216
217
218
219
220
221
222
223
224
    bool changed = m_filterMap.count() != m_filterMapBackup.count();
    if (!changed) {
        FilterMap::const_iterator it = m_filterMapBackup.constBegin();
        for (; it != m_filterMapBackup.constEnd() && !changed; ++it) {
            if (m_filterMap.contains(it.key())) {
                const QStringList &a = it.value();
                const QStringList &b = m_filterMap.value(it.key());
                if (a.count() == b.count()) {
                    QStringList::const_iterator i = a.constBegin();
                    for (; i != a.constEnd() && !changed; ++i) {
                        if (b.contains(*i))
                            continue;
                        changed = true;
con's avatar
con committed
225
                    }
226
227
                } else {
                    changed = true;
con's avatar
con committed
228
                }
229
230
            } else {
                changed = true;
con's avatar
con committed
231
232
233
            }
        }
    }
234

con's avatar
con committed
235
    if (changed) {
236
        Core::HelpManager *manager = Core::HelpManager::instance();
237
        foreach (const QString &filter, m_removedFilters)
238
           manager->removeUserDefinedFilter(filter);
239
240
241

        FilterMap::const_iterator it;
        for (it = m_filterMap.constBegin(); it != m_filterMap.constEnd(); ++it)
242
            manager->addUserDefinedFilter(it.key(), it.value());
243

244
245
246
247
        // emit this signal to the help plugin, since we don't want
        // to force gui help engine setup if we are not in help mode
        emit filtersChanged();
    }
con's avatar
con committed
248
}
249

250
251
252
253
254
255
void FilterSettingsPage::finish()
{
    disconnect(Core::HelpManager::instance(), SIGNAL(documentationChanged()),
        this, SLOT(updateFilterPage()));
}

256
257
258
259
bool FilterSettingsPage::matches(const QString &s) const
{
    return m_searchKeywords.contains(s, Qt::CaseInsensitive);
}
260

261
QString FilterSettingsPage::msgFilterLabel(const QString &filter) const
262
{
263
264
265
    if (m_filterMap.keys().isEmpty())
        return tr("No user defined filters available or no filter selected.");

266
    const QStringList &checkedList = m_filterMap.value(filter);
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
    if (checkedList.isEmpty())
        return tr("The filter \"%1\" will show every documentation file"
                  " available, as no attributes are specified.").arg(filter);

    if (checkedList.size() == 1)
        return tr("The filter \"%1\" will only show documentation files that"
                  " have the attribute %2 specified.").
                arg(filter, checkedList.first());

    return tr("The filter \"%1\" will only show documentation files that"
              " have the attributes %2 specified.").
            arg(filter, checkedList.join(QLatin1String(", ")));
}

void FilterSettingsPage::updateFilterDescription(const QString &filter)
{
    m_ui.label->setText(msgFilterLabel(filter));
284
}