summaryrefslogtreecommitdiffstats
path: root/tools/qconfig/main.cpp
blob: dd53e09893c3ad7f21a074d649f1aaafeda6a6e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the tools applications of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the either Technology Preview License Agreement or the
** Beta Release License Agreement.
**
** GNU Lesser General Public License Usage
** 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.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/

#include "graphics.h"
#include "feature.h"
#include "featuretreemodel.h"

#include <QtGui>

QT_BEGIN_NAMESPACE

static QString defaultPath;

class FeatureTextBrowser : public QTextBrowser {
    Q_OBJECT
public:
    FeatureTextBrowser(QWidget *parent) : QTextBrowser(parent) {
        QString docRoot;
        docRoot = QLibraryInfo::location(QLibraryInfo::DocumentationPath)
                  + "/html";
        setSearchPaths(searchPaths() << docRoot);
    }

signals:
    void featureClicked(const QString &feature);

public slots:
    void setSource(const QUrl &url)
    {
        if (url.scheme() == "feature")
            emit featureClicked(url.authority());
        else
            QTextBrowser::setSource(url);
    }
};

class Main : public QMainWindow {
    Q_OBJECT
public:
    Main();
    ~Main();
    void loadFeatures(const QString& filename);
    void loadConfig(const QString& filename);

public slots:
    void modelChanged();
    void showInfo(const QModelIndex &index);
    void showInfo(const QString &feature);
    void openConfig();
    void saveConfig();
    void expandView();
    void collapseView();
    void about();
    void aboutQt();
    void quit();
    void clear();
    void enableAll();
    void disableAll();

private:
    QTextBrowser *textBrowser;
    QTreeView *featureTree;
    FeatureTreeModel *featureModel;

    void init();
    void updateStatus(int numFeatures = -1);
    void completelyExpandIndex(const QModelIndex &parent);
};

template<typename Func>
void foreachIndex_helper(const QModelIndex &parent, Func func)
{
    const QAbstractItemModel *model = parent.model();
    const int rows = model->rowCount(parent);
    for (int i = 0; i < rows; ++i) {
        const QModelIndex child = model->index(i, 0, parent);
        func(child);
        foreachIndex_helper(child, func);
    }
}

template<typename Func>
void foreachIndex(const QAbstractItemModel *model, Func func)
{
    const int rows = model->rowCount(QModelIndex());
    for (int i = 0; i < rows; ++i) {
        const QModelIndex child = model->index(i, 0, QModelIndex());
        func(child);
        foreachIndex_helper(child, func);
    }
}

struct CheckStateSetter {

    CheckStateSetter(Qt::CheckState state, QAbstractItemModel *m)
        : checkState(state), model(m) {}

    void operator()(const QModelIndex &index) {
        model->setData(index, checkState, Qt::CheckStateRole);
    }

    Qt::CheckState checkState;
    QAbstractItemModel *model;
};

void Main::disableAll()
{
    QAbstractItemModel *model = featureTree->model();
    foreachIndex(model, CheckStateSetter(Qt::Unchecked, model));
}

void Main::enableAll()
{
    QAbstractItemModel *model = featureTree->model();
    foreachIndex(model, CheckStateSetter(Qt::Checked, model));
}

Main::Main()
{
    setWindowIcon(QIcon(QPixmap(logo_xpm)));

    QSplitter *splitter = new QSplitter(this);

    featureModel = new FeatureTreeModel(this);
    featureTree = new QTreeView(splitter);
    splitter->addWidget(featureTree);
    featureTree->setRootIsDecorated(true);
    featureTree->setModel(featureModel);
    featureTree->show();

    textBrowser = new FeatureTextBrowser(splitter);
    textBrowser->setFrameStyle(QFrame::WinPanel|QFrame::Sunken);
    splitter->addWidget(textBrowser);
    textBrowser->show();

    connect(textBrowser, SIGNAL(featureClicked(const QString&)),
            this, SLOT(showInfo(const QString&)));
    connect(featureTree, SIGNAL(activated(QModelIndex)),
	    this, SLOT(showInfo(QModelIndex)));
    connect(featureModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)),
            this, SLOT(modelChanged()));
    connect(featureTree, SIGNAL(clicked(QModelIndex)),
            this, SLOT(showInfo(QModelIndex)));

    setCentralWidget(splitter);

    QMenu *file = menuBar()->addMenu("&File");
    file->addAction("&Open...", this, SLOT(openConfig()),
                    Qt::CTRL + Qt::Key_O);
    file->addAction("&Save As...", this, SLOT(saveConfig()),
                    Qt::CTRL + Qt::Key_S);
    file->addSeparator();
    file->addAction("&Reset",  this, SLOT(clear()));
    file->addSeparator();
    file->addAction("E&xit",  this, SLOT(quit()), Qt::CTRL + Qt::Key_Q);

    QMenu *edit = menuBar()->addMenu("&Tools");
    edit->addAction("&Enable all features", this, SLOT(enableAll()));
    edit->addAction("&Disable all features", this, SLOT(disableAll()));

    menuBar()->addSeparator();

    QMenu *help = menuBar()->addMenu("&Help");
    help->addAction("&About", this, SLOT(about()));
    help->addAction("About &Qt", this, SLOT(aboutQt()));

    QToolBar *tb = new QToolBar("Expand/Collapse features");
    QToolButton *button;

    button = new QToolButton(tb);
    button->setIcon(QIcon(QPixmap(collapsed_xpm)));
    button->setText("Collapse");
    button->setToolTip("Collapse");
    connect(button, SIGNAL(clicked()), this, SLOT(collapseView()));
    tb->addWidget(button);

    button = new QToolButton(tb);
    button->setIcon(QIcon(QPixmap(expanded_xpm)));
    button->setText("Expand");
    button->setToolTip("Expand");
    connect(button, SIGNAL(clicked()), this, SLOT(expandView()));
    tb->addWidget(button);
    addToolBar(tb);

    init();
}

Main::~Main()
{
    delete textBrowser;
    delete featureModel;
    delete featureTree;
}

void Main::clear()
{
    QSettings settings;
    settings.clear();
    featureModel->clear();
    featureTree->reset();
    init();
}

void Main::quit()
{
    if (isWindowModified()) {
        int button = QMessageBox::question(this, "Quit Program",
                                           "You have unsaved changes.\n"
                                           "Do you want to quit anyway?",
                                           QMessageBox::Yes,
                                           QMessageBox::No);
        if (static_cast<QMessageBox::Button>(button) != QMessageBox::Yes)
            return;
    }
    QApplication::instance()->quit();
}

/*
  Recursively expand expand \a parent and all of its children.
*/
void Main::completelyExpandIndex(const QModelIndex &parent)
{
    featureTree->setExpanded(parent, true);

    const QAbstractItemModel *model = featureTree->model();
    const int rows = model->rowCount(parent);
    for (int i = 0; i < rows; ++i)
        completelyExpandIndex(model->index(i, 0, parent));
}

void Main::expandView()
{
    completelyExpandIndex(QModelIndex());
}

void Main::collapseView()
{
    const QAbstractItemModel *model = featureTree->model();
    const int rows = model->rowCount(QModelIndex());
    for (int i = 0; i < rows; ++i) {
        QModelIndex index = model->index(i, 0, QModelIndex());
        featureTree->setExpanded(index, false);
    }
}

void Main::updateStatus(int numFeatures)
{
    QSettings settings;
    QString featureFile = settings.value("featureFile").toString();
    QString configFile = settings.value("lastConfig").toString();
    QString message("Using features from %1");

    if (numFeatures >= 0) {
        QString s("%1 features loaded from %2");
        statusBar()->showMessage(s.arg(numFeatures).arg(featureFile));
    }
    QString appName = QApplication::applicationName();
    if (configFile.isEmpty())
        configFile = "New File";
    setWindowTitle(appName + " - " + configFile + "[*]");
}

void Main::modelChanged()
{
    setWindowModified(true);
}

void Main::init()
{
    QSettings settings;
    QString features = settings.value("featureFile").toString();

    if (features.isEmpty() || !QFileInfo(features).isFile()) {
        features = QFileDialog::getOpenFileName(this,
                                                "Open a feature file",
                                                defaultPath,
                                                "Qt Features (qfeatures.txt)");
    }
    settings.setValue("featureFile", features);
    loadFeatures(features);

    expandView();
    collapseView();

    QString confFile = settings.value("lastConfig").toString();
    if (confFile.isEmpty())
        return;
    loadConfig(confFile);
}

void Main::openConfig()
{
    QSettings settings;
    QString configDir;

    QString prevFile = settings.value("lastConfig").toString();
    if (!prevFile.isEmpty())
        configDir = QFileInfo(prevFile).path();

    if (configDir.isEmpty())
        configDir = defaultPath;

    QString configFile;
    configFile = QFileDialog::getOpenFileName(this,
                                              "Open a configuration file",
                                              configDir,
                                              "Header files (*.h)");
    enableAll();
    if (!configFile.isEmpty())
        loadConfig(configFile);
    settings.setValue("lastConfig", QFileInfo(configFile).absoluteFilePath());
}

void Main::saveConfig()
{
    QSettings settings;
    QString configDir;

    QString prevFile = settings.value("lastConfig").toString();
    if (!prevFile.isEmpty())
        configDir = QFileInfo(prevFile).path();

    if (configDir.isEmpty())
        configDir = defaultPath;

    QString configFile;
    configFile = QFileDialog::getSaveFileName(this,
                                              "Save configuration file",
                                              configDir,
                                              "Header files (*.h)");
    if (configFile.isEmpty())
        return;

    QFile file(configFile);
    if (!file.open(QIODevice::WriteOnly)) {
	QMessageBox::warning(this,"Warning",
                             "Cannot write to file " + configFile);
	return;
    }

    QTextStream stream(&file);
    FeatureTreeModel *model;
    model = static_cast<FeatureTreeModel*>(featureTree->model());
    model->writeConfig(stream);

    settings.setValue("lastConfig", QFileInfo(configFile).absoluteFilePath());
    setWindowModified(false);
    updateStatus();
}

void Main::loadConfig(const QString &filename)
{
    if (!QFileInfo(filename).isFile())
	return;

    QFile file(filename);
    if (!file.open(QIODevice::ReadOnly)) {
	QMessageBox::warning(this,"Warning", "Cannot open file " + filename);
	return;
    }

    QTextStream stream(&file);
    FeatureTreeModel *model;
    model = static_cast<FeatureTreeModel*>(featureTree->model());
    model->readConfig(stream);

    QSettings settings;
    settings.setValue("lastConfig", QFileInfo(filename).absoluteFilePath());
    setWindowModified(false);
    updateStatus();
}

void Main::loadFeatures(const QString &filename)
{
    Feature::clear();

    QFile file(filename);
    if (!file.open(QIODevice::ReadOnly)) {
	QMessageBox::warning(this,"Warning", "Cannot open file " + filename);
	return;
    }

    Feature *feature = 0;
    int numFeatures = 0;
    updateStatus(numFeatures);
    QTextStream s(&file);
    for (QString line = s.readLine(); !s.atEnd(); line = s.readLine()) {
	line = line.simplified();
	if (line.isEmpty())
	    continue;
	if (line.startsWith('#'))
	    continue;

	int colon = line.indexOf(':');
	if (colon < 0) { // assume description
            QString description = feature->description().simplified();
            description += " " + line;
            feature->setDescription(description);
	    continue;
	}

	QString tag = line.left(colon);
	QString value = line.mid(colon+1).simplified();
	if (tag == "Feature") {
            if (feature)
                featureModel->addFeature(feature);
            feature = Feature::getInstance(value);
            updateStatus(++numFeatures);
	} else if (tag == "Requires") {
            Q_ASSERT(feature);
            feature->setDependencies(value.split(' ', QString::SkipEmptyParts));
	} else if (tag == "Name") {
	    Q_ASSERT(feature);
            feature->setTitle(value);
	} else if (tag == "Section") {
	    Q_ASSERT(feature);
            feature->setSection(value);
	} else if (tag == "SeeAlso") {
	    Q_ASSERT(feature);
            feature->setRelations(value.split(' ', QString::SkipEmptyParts));
        } else if (tag == "Description") {
            Q_ASSERT(feature);
            feature->setDescription(value);
	}
    }
    if (feature)
        featureModel->addFeature(feature);

    featureTree->resizeColumnToContents(0);

    QSettings settings;
    settings.setValue("featureFile", QFileInfo(filename).absoluteFilePath());

    updateStatus();
}

void Main::showInfo(const QModelIndex &index)
{
    FeatureTreeModel *model;
    model = static_cast<FeatureTreeModel*>(featureTree->model());

    if (const Feature *feature = model->getFeature(index))
	textBrowser->setHtml(feature->toHtml());

    // Ensure index is visible
    QModelIndex parent = model->parent(index);
    while (parent.isValid()) {
        featureTree->setExpanded(parent, true);
        parent = model->parent(parent);
    }

    featureTree->scrollTo(index);
    featureTree->setCurrentIndex(index);
}

void Main::showInfo(const QString &feature)
{
    const Feature *f = Feature::getInstance(feature);
    FeatureTreeModel *model;
    model = static_cast<FeatureTreeModel*>(featureTree->model());
    showInfo(model->index(f));
}

void Main::about()
{
    QMessageBox::about(this, "About qconfig",
	"<p><b><font size=\"+2\">Qtopia Core build configuration</font></b></p>"
	"<p></p>"
	"<p>Version 2.0</p>"
	"<p>Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).</p>"
	"<p></p>"
	"<p>This program is licensed to you under the terms of the GNU General "
	"Public License Version 2 as published by the Free Software Foundation. This "
	"gives you legal permission to copy, distribute and/or modify this software "
	"under certain conditions. For details, see the file 'LICENSE.GPL' that came with "
	"this software distribution. If you did not get the file, send email to "
	"qt-info@nokia.com.</p>\n\n<p>The program is provided AS IS with NO WARRANTY "
	"OF ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS "
	"FOR A PARTICULAR PURPOSE.</p>"
    );
}

void Main::aboutQt()
{
    QMessageBox::aboutQt( this, tr("qconfig") );
}

QT_END_NAMESPACE

int main(int argc, char** argv)
{
    QT_USE_NAMESPACE
    QApplication app(argc,argv);
    app.setOrganizationDomain("trolltech.com");
    app.setOrganizationName("Trolltech");
    app.setApplicationName("QConfig");
    Main m;

    defaultPath = QLibraryInfo::location(QLibraryInfo::PrefixPath)
                  + "/src/corelib/global";

    for (int i = 1; i < argc; ++i) {
	QString arg = argv[i];
	if (arg == "-f" && i+1 < argc)
            m.loadFeatures(argv[++i]);
	else if (arg == "-c" && i+1 < argc)
            m.loadConfig(argv[++i]);
    }
    m.resize(m.sizeHint() + QSize(500,300));
    m.show();
    return app.exec();
}

#include "main.moc"