1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the Qt Assistant of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include <QtGui/QMessageBox>
43 #include "filterpage.h"
44 
45 QT_BEGIN_NAMESPACE
46 
FilterPage(QWidget * parent)47 FilterPage::FilterPage(QWidget *parent)
48     : QWizardPage(parent)
49 {
50     setTitle(tr("Filter Settings"));
51     setSubTitle(tr("Specify the filter attributes for the "
52         "documentation. If filter attributes are used, "
53         "also define a custom filter for it. Both the "
54         "filter attributes and the custom filters are "
55         "optional."));
56 
57     m_ui.setupUi(this);
58     m_ui.customFilterWidget->headerItem()->setText(0, tr("Filter Name"));
59     m_ui.customFilterWidget->headerItem()->setText(1, tr("Filter Attributes"));
60     m_ui.customFilterWidget->setRootIsDecorated(false);
61     m_ui.removeButton->setDisabled(true);
62     connect(m_ui.addButton, SIGNAL(clicked()),
63         this, SLOT(addFilter()));
64     connect(m_ui.removeButton, SIGNAL(clicked()),
65         this, SLOT(removeFilter()));
66 }
67 
validatePage()68 bool FilterPage::validatePage()
69 {
70     m_filterAttributes.clear();
71     foreach (const QString &f, m_ui.filterLineEdit->text().split(QLatin1Char(','))) {
72         if (!f.trimmed().isEmpty())
73             m_filterAttributes.append(f.trimmed());
74     }
75 
76     m_customFilters.clear();
77     QSet<QString> names;
78     QSet<QString> atts;
79     QString str;
80     CustomFilter customFilter;
81     QTreeWidgetItem *item = 0;
82     for (int i=0; i<m_ui.customFilterWidget->topLevelItemCount(); ++i) {
83         item = m_ui.customFilterWidget->topLevelItem(i);
84         str = item->text(0);
85         if (str.isEmpty() || names.contains(str)) {
86             QMessageBox::critical(this, tr("Custom Filters"),
87                 tr("The custom filter \'%1\' is defined multiple times.")
88                 .arg(str));
89             return false;
90         }
91         names.insert(str);
92         customFilter.name = str;
93 
94         str.clear();
95         QStringList lst;
96         foreach (const QString &s, item->text(1).split(QLatin1Char(','))) {
97             const QString st = s.trimmed();
98             if (!st.isEmpty()) {
99                 str += QLatin1Char(',') + st;
100                 lst.append(st);
101             }
102         }
103         if (atts.contains(str)) {
104             QMessageBox::critical(this, tr("Custom Filters"),
105                 tr("The attributes for custom filter \'%1\' are defined multiple times.")
106                 .arg(customFilter.name));
107             return false;
108         }
109         atts.insert(str);
110         customFilter.filterAttributes = lst;
111         m_customFilters.append(customFilter);
112     }
113     return true;
114 }
115 
filterAttributes() const116 QStringList FilterPage::filterAttributes() const
117 {
118     return m_filterAttributes;
119 }
120 
customFilters() const121 QList<CustomFilter> FilterPage::customFilters() const
122 {
123     return m_customFilters;
124 }
125 
addFilter()126 void FilterPage::addFilter()
127 {
128     QTreeWidgetItem *item = new QTreeWidgetItem(m_ui.customFilterWidget);
129     item->setFlags(Qt::ItemIsEnabled|Qt::ItemIsEditable|Qt::ItemIsSelectable);
130     item->setText(0, tr("unfiltered", "list of available documentation"));
131     item->setText(1, QLatin1String(""));
132     m_ui.customFilterWidget->editItem(item, 0);
133     m_ui.removeButton->setDisabled(false);
134 }
135 
removeFilter()136 void FilterPage::removeFilter()
137 {
138     QModelIndex idx = m_ui.customFilterWidget->currentIndex();
139     if (!idx.isValid())
140         return;
141     QTreeWidgetItem *item = m_ui.customFilterWidget->takeTopLevelItem(idx.row());
142     delete item;
143     if (!m_ui.customFilterWidget->topLevelItemCount())
144         m_ui.removeButton->setDisabled(true);
145 }
146 
147 QT_END_NAMESPACE
148