filtersettingspage.cpp 7.16 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
35
36
#include "helpconstants.h"
#include "helpmanager.h"

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

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

41
#include <QtHelp/QHelpEngineCore>
con's avatar
con committed
42
43

using namespace Help::Internal;
kh's avatar
kh committed
44

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

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

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

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

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

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

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

    updateFilterPage(); // does call setupData on the engine

con's avatar
con committed
81
82
83
    connect(m_ui.attributeWidget, SIGNAL(itemChanged(QTreeWidgetItem*, int)),
        this, SLOT(updateFilterMap()));
    connect(m_ui.filterWidget,
kh's avatar
kh committed
84
85
86
87
88
89
        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()));

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

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

    m_filterMapBackup.clear();
103
104
    const QHelpEngineCore &engine = HelpManager::helpEngineCore();
    const QStringList &filters = engine.customFilters();
105
    foreach (const QString &filter, filters) {
106
107
        const QStringList &attributes = engine.filterAttributes(filter);
        m_filterMapBackup.insert(filter, attributes);
con's avatar
con committed
108
        if (!m_filterMap.contains(filter))
109
            m_filterMap.insert(filter, attributes);
con's avatar
con committed
110
111
112
    }
    m_ui.filterWidget->addItems(m_filterMap.keys());

113
114
115
    const QStringList &attributes = engine.filterAttributes();
    foreach (const QString &attribute, attributes)
        new QTreeWidgetItem(m_ui.attributeWidget, QStringList(attribute));
kh's avatar
kh committed
116

117
    if (m_filterMap.keys().isEmpty()) {
con's avatar
con committed
118
        m_ui.filterWidget->setCurrentRow(0);
119
120
        updateAttributes(m_ui.filterWidget->currentItem());
    }
con's avatar
con committed
121
122
123
124
125
126
127
}

void FilterSettingsPage::updateAttributes(QListWidgetItem *item)
{
    QStringList checkedList;
    if (item)
        checkedList = m_filterMap.value(item->text());
128
129
130

    for (int i = 0; i < m_ui.attributeWidget->topLevelItemCount(); ++i) {
        QTreeWidgetItem *itm = m_ui.attributeWidget->topLevelItem(i);
con's avatar
con committed
131
132
133
134
135
136
137
138
139
140
141
        if (checkedList.contains(itm->text(0)))
            itm->setCheckState(0, Qt::Checked);
        else
            itm->setCheckState(0, Qt::Unchecked);
    }
}

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

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

con's avatar
con committed
147
    QStringList newAtts;
148
149
    for (int i = 0; i < m_ui.attributeWidget->topLevelItemCount(); ++i) {
        QTreeWidgetItem *itm = m_ui.attributeWidget->topLevelItem(i);
con's avatar
con committed
150
151
152
        if (itm->checkState(0) == Qt::Checked)
            newAtts.append(itm->text(0));
    }
kh's avatar
kh committed
153
    m_filterMap[filter] = newAtts;
con's avatar
con committed
154
155
156
157
}

void FilterSettingsPage::addFilter()
{
158
    FilterNameDialog dia(m_ui.filterWidget);
con's avatar
con committed
159
160
161
    if (dia.exec() == QDialog::Rejected)
        return;

162
    const QString &filterName = dia.filterName();
con's avatar
con committed
163
164
165
166
167
    if (!m_filterMap.contains(filterName)) {
        m_filterMap.insert(filterName, QStringList());
        m_ui.filterWidget->addItem(filterName);
    }

168
169
    const QList<QListWidgetItem*> &lst = m_ui.filterWidget->findItems(filterName,
        Qt::MatchCaseSensitive);
kh's avatar
kh committed
170
    m_ui.filterWidget->setCurrentItem(lst.first());
con's avatar
con committed
171
172
173
174
}

void FilterSettingsPage::removeFilter()
{
175
176
    QListWidgetItem *item =
        m_ui.filterWidget->takeItem(m_ui.filterWidget->currentRow());
con's avatar
con committed
177
178
179
180
181
182
183
184
185
186
    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);
}

187
void FilterSettingsPage::apply()
con's avatar
con committed
188
{
189
190
191
192
193
194
195
196
197
198
199
200
201
    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
202
                    }
203
204
                } else {
                    changed = true;
con's avatar
con committed
205
                }
206
207
            } else {
                changed = true;
con's avatar
con committed
208
209
210
            }
        }
    }
211

con's avatar
con committed
212
    if (changed) {
213
        QHelpEngineCore *engine = &HelpManager::helpEngineCore();
214
        foreach (const QString &filter, m_removedFilters)
215
216
217
218
219
220
           engine->removeCustomFilter(filter);

        FilterMap::const_iterator it;
        for (it = m_filterMap.constBegin(); it != m_filterMap.constEnd(); ++it)
            engine->addCustomFilter(it.key(), it.value());

221
222
223
224
        // 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
225
}
226
227
228
229
230

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