| 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 | ** Commercial Usage | 
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 14 | ** a written agreement between you and Nokia. | 
|---|
| 15 | ** | 
|---|
| 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. | 
|---|
| 23 | ** | 
|---|
| 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. | 
|---|
| 27 | ** | 
|---|
| 28 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you have questions regarding the use of this file, please contact | 
|---|
| 37 | ** Nokia at qt-info@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #include "feature.h" | 
|---|
| 43 | #include <QTextStream> | 
|---|
| 44 | #include <QRegExp> | 
|---|
| 45 | #include <QLibraryInfo> | 
|---|
| 46 | #include <QFileInfo> | 
|---|
| 47 | #include <QApplication> | 
|---|
| 48 | #include <QPalette> | 
|---|
| 49 |  | 
|---|
| 50 | QT_BEGIN_NAMESPACE | 
|---|
| 51 |  | 
|---|
| 52 | QMap<QString, Feature*> Feature::instances; | 
|---|
| 53 |  | 
|---|
| 54 | Feature* Feature::getInstance(const QString &key) | 
|---|
| 55 | { | 
|---|
| 56 | QString ukey = key.toUpper(); | 
|---|
| 57 | if (!instances.contains(ukey)) | 
|---|
| 58 | instances[ukey] = new Feature(ukey); | 
|---|
| 59 | return instances[ukey]; | 
|---|
| 60 | } | 
|---|
| 61 |  | 
|---|
| 62 | Feature::~Feature() | 
|---|
| 63 | { | 
|---|
| 64 | delete d; | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | void Feature::clear() | 
|---|
| 68 | { | 
|---|
| 69 | foreach (Feature *f, instances.values()) | 
|---|
| 70 | delete f; | 
|---|
| 71 | instances.clear(); | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 74 | static QString listToHtml(const QString &title, const QStringList &list) | 
|---|
| 75 | { | 
|---|
| 76 | if (list.isEmpty()) | 
|---|
| 77 | return QString(); | 
|---|
| 78 |  | 
|---|
| 79 | QString str; | 
|---|
| 80 | QTextStream stream(&str); | 
|---|
| 81 |  | 
|---|
| 82 | stream << "<h3>" << title << ":</h3>"; | 
|---|
| 83 | stream << "<ul>"; | 
|---|
| 84 | foreach (QString l, list) | 
|---|
| 85 | stream << "<li>" << l << "</li>"; | 
|---|
| 86 | stream << "</ul>"; | 
|---|
| 87 |  | 
|---|
| 88 | return str; | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | static QString listToHtml(const QString &title, const QList<Feature*> &list) | 
|---|
| 92 | { | 
|---|
| 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; | 
|---|
| 99 | } | 
|---|
| 100 | return listToHtml(title, stringlist); | 
|---|
| 101 | } | 
|---|
| 102 |  | 
|---|
| 103 | static QString linkify(const QString &src) | 
|---|
| 104 | { | 
|---|
| 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(); | 
|---|
| 120 | } | 
|---|
| 121 | } | 
|---|
| 122 |  | 
|---|
| 123 | return result; | 
|---|
| 124 | } | 
|---|
| 125 |  | 
|---|
| 126 | QString Feature::toHtml() const | 
|---|
| 127 | { | 
|---|
| 128 | QString str; | 
|---|
| 129 | QTextStream stream(&str); | 
|---|
| 130 |  | 
|---|
| 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()); | 
|---|
| 141 |  | 
|---|
| 142 | return linkify(str); | 
|---|
| 143 | } | 
|---|
| 144 |  | 
|---|
| 145 | Feature::Feature(const QString &key) : d(new FeaturePrivate(key)) {} | 
|---|
| 146 |  | 
|---|
| 147 | void Feature::setTitle(const QString &title) | 
|---|
| 148 | { | 
|---|
| 149 | d->title = title; | 
|---|
| 150 | } | 
|---|
| 151 |  | 
|---|
| 152 | void Feature::setSection(const QString §ion) | 
|---|
| 153 | { | 
|---|
| 154 | d->section = section; | 
|---|
| 155 | } | 
|---|
| 156 |  | 
|---|
| 157 | void Feature::setDescription(const QString &description) | 
|---|
| 158 | { | 
|---|
| 159 | d->description = description; | 
|---|
| 160 | } | 
|---|
| 161 |  | 
|---|
| 162 | void Feature::addRelation(const QString &key) | 
|---|
| 163 | { | 
|---|
| 164 | d->relations.insert(getInstance(key)); | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|
| 167 | void Feature::setRelations(const QStringList &keys) | 
|---|
| 168 | { | 
|---|
| 169 | foreach(QString key, keys) | 
|---|
| 170 | if (key != "???") | 
|---|
| 171 | addRelation(key); | 
|---|
| 172 | } | 
|---|
| 173 |  | 
|---|
| 174 | QList<Feature*> Feature::relations() const | 
|---|
| 175 | { | 
|---|
| 176 | return d->relations.toList(); | 
|---|
| 177 | } | 
|---|
| 178 |  | 
|---|
| 179 | void Feature::addDependency(const QString &key) | 
|---|
| 180 | { | 
|---|
| 181 | Feature *f = getInstance(key); | 
|---|
| 182 | d->dependencies.insert(f); | 
|---|
| 183 | f->d->supports.insert(this); | 
|---|
| 184 | } | 
|---|
| 185 |  | 
|---|
| 186 | void Feature::setDependencies(const QStringList &keys) | 
|---|
| 187 | { | 
|---|
| 188 | foreach(QString key, keys) | 
|---|
| 189 | addDependency(key); | 
|---|
| 190 | } | 
|---|
| 191 |  | 
|---|
| 192 | QList<Feature*> Feature::dependencies() const | 
|---|
| 193 | { | 
|---|
| 194 | return d->dependencies.toList(); | 
|---|
| 195 | } | 
|---|
| 196 |  | 
|---|
| 197 | QList<Feature*> Feature::supports() const | 
|---|
| 198 | { | 
|---|
| 199 | return d->supports.toList(); | 
|---|
| 200 | } | 
|---|
| 201 |  | 
|---|
| 202 | /* | 
|---|
| 203 | Returns a html formatted detailed description of this Feature. | 
|---|
| 204 | */ | 
|---|
| 205 | QString Feature::getDocumentation() const | 
|---|
| 206 | { | 
|---|
| 207 | return QString() + "<h2>" + d->title + "</h2>"; | 
|---|
| 208 |  | 
|---|
| 209 | } | 
|---|
| 210 |  | 
|---|
| 211 | void Feature::setEnabled(bool on) | 
|---|
| 212 | { | 
|---|
| 213 | if (on == d->enabled) | 
|---|
| 214 | return; | 
|---|
| 215 |  | 
|---|
| 216 | d->enabled = on; | 
|---|
| 217 | foreach (Feature *f, supports()) | 
|---|
| 218 | f->updateSelectable(); | 
|---|
| 219 | emit changed(); | 
|---|
| 220 | } | 
|---|
| 221 |  | 
|---|
| 222 | /* | 
|---|
| 223 | Update whether this feature should be selectable. | 
|---|
| 224 | A feature is selectable if all its dependencies are enabled. | 
|---|
| 225 | */ | 
|---|
| 226 | void Feature::updateSelectable() | 
|---|
| 227 | { | 
|---|
| 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(); | 
|---|
| 237 | } | 
|---|
| 238 | } | 
|---|
| 239 |  | 
|---|
| 240 | QT_END_NAMESPACE | 
|---|