Merge branch 'master' of scm.dev.nokia.troll.no:qt/oslo-staging-1 into master-integration
[qt-netbsd.git] / tools / qconfig / feature.cpp
blob98bc97c5b122dd95166012c342be476d14787aeb
1 /****************************************************************************
2 **
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the tools applications of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
38 ** $QT_END_LICENSE$
40 ****************************************************************************/
42 #include "feature.h"
43 #include <QTextStream>
44 #include <QRegExp>
45 #include <QLibraryInfo>
46 #include <QFileInfo>
47 #include <QApplication>
48 #include <QPalette>
50 QT_BEGIN_NAMESPACE
52 QMap<QString, Feature*> Feature::instances;
54 Feature* Feature::getInstance(const QString &key)
56 QString ukey = key.toUpper();
57 if (!instances.contains(ukey))
58 instances[ukey] = new Feature(ukey);
59 return instances[ukey];
62 Feature::~Feature()
64 delete d;
67 void Feature::clear()
69 foreach (Feature *f, instances.values())
70 delete f;
71 instances.clear();
74 static QString listToHtml(const QString &title, const QStringList &list)
76 if (list.isEmpty())
77 return QString();
79 QString str;
80 QTextStream stream(&str);
82 stream << "<h3>" << title << ":</h3>";
83 stream << "<ul>";
84 foreach (QString l, list)
85 stream << "<li>" << l << "</li>";
86 stream << "</ul>";
88 return str;
91 static QString listToHtml(const QString &title, const QList<Feature*> &list)
93 QStringList stringlist;
94 foreach (Feature *f, list) {
95 QString s("[%3] <a href=\"feature://%1\">%2</a>");
96 s = s.arg(f->key()).arg(f->key());
97 s = s.arg(f->selectable() && f->enabled() ? "On" : "Off");
98 stringlist << s;
100 return listToHtml(title, stringlist);
103 static QString linkify(const QString &src)
105 static QRegExp classRegexp("\\b(Q[\\w]+)");
106 QString docRoot = QLibraryInfo::location(QLibraryInfo::DocumentationPath);
107 QString result = src;
108 int pos = 0;
109 while ((pos = classRegexp.indexIn(result, pos)) != -1) {
110 QString className = classRegexp.cap(1);
111 QString file = docRoot + "/html/" + className.toLower() + ".html";
112 QFileInfo info(file);
113 if (info.isFile()) {
114 QString link = QString("<a href=\"file://%1\">%2</a>")
115 .arg(file).arg(className);
116 result.replace(pos, className.length(), link);
117 pos += link.length();
118 } else {
119 pos += className.length();
123 return result;
126 QString Feature::toHtml() const
128 QString str;
129 QTextStream stream(&str);
131 const QString linkColor = QApplication::palette().color(QPalette::Link).name();
132 stream << "<h2><font size=\"+2\" color=\"" << linkColor << "\">"
133 << key() << "</font></h2>"
134 << "<h2><font size=\"+2\">" << title() << "</font></h2>";
135 if (!description().isEmpty())
136 stream << "<p>" << description() << "</p>";
137 stream << listToHtml("Section", QStringList(section()))
138 << listToHtml("Requires", dependencies())
139 << listToHtml("Required for", supports())
140 << listToHtml("See also", relations());
142 return linkify(str);
145 Feature::Feature(const QString &key) : d(new FeaturePrivate(key)) {}
147 void Feature::setTitle(const QString &title)
149 d->title = title;
152 void Feature::setSection(const QString &section)
154 d->section = section;
157 void Feature::setDescription(const QString &description)
159 d->description = description;
162 void Feature::addRelation(const QString &key)
164 d->relations.insert(getInstance(key));
167 void Feature::setRelations(const QStringList &keys)
169 foreach(QString key, keys)
170 if (key != "???")
171 addRelation(key);
174 QList<Feature*> Feature::relations() const
176 return d->relations.toList();
179 void Feature::addDependency(const QString &key)
181 Feature *f = getInstance(key);
182 d->dependencies.insert(f);
183 f->d->supports.insert(this);
186 void Feature::setDependencies(const QStringList &keys)
188 foreach(QString key, keys)
189 addDependency(key);
192 QList<Feature*> Feature::dependencies() const
194 return d->dependencies.toList();
197 QList<Feature*> Feature::supports() const
199 return d->supports.toList();
203 Returns a html formatted detailed description of this Feature.
205 QString Feature::getDocumentation() const
207 return QString() + "<h2>" + d->title + "</h2>";
211 void Feature::setEnabled(bool on)
213 if (on == d->enabled)
214 return;
216 d->enabled = on;
217 foreach (Feature *f, supports())
218 f->updateSelectable();
219 emit changed();
223 Update whether this feature should be selectable.
224 A feature is selectable if all its dependencies are enabled.
226 void Feature::updateSelectable()
228 bool selectable = true;
229 foreach (Feature *f, dependencies())
230 if (!f->selectable() || !f->enabled())
231 selectable = false;
232 if (selectable != d->selectable) {
233 d->selectable = selectable;
234 foreach (Feature *f, supports())
235 f->updateSelectable();
236 emit changed();
240 QT_END_NAMESPACE