File: main.cpp

package info (click to toggle)
bespin 0.r1552-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 9,848 kB
  • sloc: cpp: 24,647; sh: 523; xml: 459; makefile: 79
file content (330 lines) | stat: -rw-r--r-- 12,006 bytes parent folder | download | duplicates (2)
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
/*
 *   Bespin style config for Qt4 and swiss army cli tool
 *   Copyright 2007-2012 by Thomas Lübking <thomas.luebking@gmail.com>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License version 2
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details
 *
 *   You should have received a copy of the GNU General Public
 *   License along with this program; if not, write to the
 *   Free Software Foundation, Inc.,
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "config.h"
#include "ui_uiDemo.h"
#include "dialog.h"
#include <QDir>
#include <QFile>
#include <QProcess>
#include <QSettings>
#include <QStyleFactory>
#include <QTimer>

#define CHAR(_QSTRING_) _QSTRING_.toLocal8Bit().data()
#define RETURN_APP_ERROR int ret = app->exec(); delete window; delete app; return ret

class BStyle : public QStyle
{
public:
    BStyle() : QStyle (){}
    virtual void init(const QSettings *settings) = 0;
};

static int
error(const QString & string)
{
    qWarning("Error: %s", CHAR(string));
    return 1;
}

static int
usage(const char* appname)
{
   printf(
"Usage:\n"
"==========================\n"
"%s [config]\t\t\t\t\t\tConfigure the Bespin Style\n"
"%s presets\t\t\t\t\t\tList the available Presets\n"
"%s demo [style]\t\t\t\t\tLaunch a demo, you can pass other stylenames\n"
"%s show <some_preset>\t\t\t\tOpen demo dialog with a preset\n"
"%s try <some_config.bespin>\t\t\t\tTry out an exported setting\n"
"%s sshot <some_file.png> [preset] [width]\t\tSave a screenshot\n"
"%s load <some_preset>\t\t\t\tLoad a preset to current settings\n"
"%s import <some_config.bespin>\t\t\tImport an exported setting\n"
"%s update <some_config.bespin>\t\t\tLike import, but overrides existing\n"
"%s export <some_preset> <some_config.bespin>\tExport an imported setting\n"
"%s listStyles \t\t\t\t\tList all styles on this System\n"
"%s loadPaletteFrom <style>\t\t\t\tLoad and store the default palette of a style\n"
"%s read <style|deco> <key> [value]\t\t\tRead a config key (defaulting to optional value)\n"
"%s write <style|deco> <key> <value>\t\t\tWrite a config key to value\n"
"%s delete <style|deco> <key>\t\t\tRemove a config key\n"
"%s list <style|deco> [filter]\t\t\tList config keys matching the optional filter\n",
appname, appname, appname, appname, appname, appname, appname, appname,
appname, appname, appname, appname, appname, appname, appname, appname );
   return 1;
}

enum Mode
{
    Invalid = 0, Configure, Presets, Import, Update, Export, Load, Demo, Try, Screenshot, ListStyles, Show, LoadPalette, ReadSetting, WriteSetting, DeleteSetting, ListSettings
};

int
main(int argc, char *argv[])
{
    Mode mode = Configure;
    QApplication *app = 0;
    QString title;
    if (argc > 1)
    {
        mode = Invalid;
        if (!qstrcmp( argv[1], "config" )) mode = Configure;
        else if (!qstrcmp( argv[1], "presets" )) mode = Presets;
        else if (!qstrcmp( argv[1], "import" )) mode = Import;
        else if (!qstrcmp( argv[1], "update" )) mode = Update;
        else if (!qstrcmp( argv[1], "export" )) mode = Export;
        else if (!qstrcmp( argv[1], "demo" )) mode = Demo;
        else if (!qstrcmp( argv[1], "try" )) mode = Try;
        else if (!qstrcmp( argv[1], "show" )) mode = Show;
        else if (!qstrcmp( argv[1], "sshot" )) mode = Screenshot;
        else if (!qstrcmp( argv[1], "load" )) mode = Load;
        else if (!qstrcmp( argv[1], "listStyles" )) mode = ListStyles;
        else if (!qstrcmp( argv[1], "loadPaletteFrom" )) mode = LoadPalette;
        else if (!qstrcmp( argv[1], "read" )) mode = ReadSetting;
        else if (!qstrcmp( argv[1], "write" )) mode = WriteSetting;
        else if (!qstrcmp( argv[1], "delete" )) mode = DeleteSetting;
        else if (!qstrcmp( argv[1], "list" )) mode = ListSettings;
    }

    switch (mode)
    {
    case Configure:
    {
        app = new QApplication(argc, argv);
        Config *config = new Config;
        BConfigDialog *window = new BConfigDialog(config, BConfigDialog::All &
                                                          ~(BConfigDialog::Import | BConfigDialog::Export));
        window->show();
        RETURN_APP_ERROR;
    }
    case Presets:
    {
        QSettings store("Bespin", "Store");
        foreach (QString name, store.childGroups())
            printf("%s\n", CHAR(name));
        return 0;
    }
    case Load:
    {
        if (argc < 3)
            return error("you lack <some_preset>");
        return Config::load(argv[2]);
    }
    case Update:
    case Import:
    {
        if (argc < 3)
            return error("you lack <some_config.bespin>");
        bool errors = false;
        for (int i = 2; i < argc; ++i)
        {
            if (!QFile::exists(argv[i]))
            {
                return error(QString("The file %1 does not exist").arg(argv[i]));
            }
            else {
                if (Config::sImport(argv[i], mode == Update).isNull())
                {
                    error(QString("Importing file %1 failed").arg(argv[i]));
                    errors = true;
                }
            }
        }
        return errors;
    }
    case Export:
    {
        if (argc < 3)
            return error("you lack <some_preset> <some_config.bespin>");
        if (argc < 4)
            return error("you lack <some_config.bespin>");
        bool suc = Config::sExport(argv[2], argv[3]);
        if (!suc)
            return error("export failed (invalid preset name?)");
        return 0;
    }
    case LoadPalette:
    {
        if (argc < 3)
            return error(QString("you lack <some_style>. Try \"%1 listStyles\"").arg(argv[0]));
        app = new QApplication(argc, argv);
        QStyle *style = QStyleFactory::create( argv[2] );
        if (!style)
        {
            delete app;
            return error(QString("Style \"%1\" does not exist. Try \"%2 listStyles\"").arg(argv[2]).arg(argv[0]));
        }
        Config::savePalette(style->standardPalette());
        delete app; delete style;
        break;
    }
    case Try:
    {
        app = new QApplication(argc, argv);
        if (argc < 3)
            return error("you lack <some_config.bespin>");

        if (!QFile::exists(argv[2]))
            return error(QString("The file %1 does not exist").arg(argv[2]));

        QSettings file(argv[2], QSettings::IniFormat);
        if (!file.childGroups().contains("BespinStyle"))
            return error(QString("%1 is not a valid Bespin configuration").arg(argv[2]));

        if (!app->setStyle("Bespin"))
            return error("Fatal: Bespin Style not found or loadable!");

        file.beginGroup("BespinStyle");
        title = file.value ( "StoreName", "" ).toString();
        // palette update =============================
        QPalette pal = app->palette();
        file.beginGroup("QPalette");
        QStringList list = file.value ( "active", Config::colors(pal, QPalette::Active) ).toStringList();
        Config::updatePalette(pal, QPalette::Active, list);
        list = file.value ( "inactive", Config::colors(pal, QPalette::Inactive) ).toStringList();
        Config::updatePalette(pal, QPalette::Inactive, list);
        list = file.value ( "disabled", Config::colors(pal, QPalette::Disabled) ).toStringList();
        Config::updatePalette(pal, QPalette::Disabled, list);
        file.endGroup();
        app->setPalette(pal);
        // ================================================
        static_cast<BStyle*>(app->style())->init(&file);

        file.endGroup();
    } // FALL THROUGH
    case Show:
    case Demo:
    {
        if (title.isEmpty())
            title = "Bespin Demo";

        char *preset = 0;
        if (mode == Show)
        {
            if (argc < 3)
                return error("you lack <preset>");
#ifndef Q_WS_WIN
            setenv("BESPIN_PRESET", argv[2], 1);
            preset = argv[2];
#endif
        }
        else if (mode == Demo && argc > 2)
        {   // allow setting another style
            if (!app) app = new QApplication(argc, argv);
            app->setStyle(argv[2]);
            app->setPalette(app->style()->standardPalette());
            title = QString("Bespin Demo / %1 Style").arg(argv[2]);
        }
        else
            preset = getenv("BESPIN_PRESET");
        if (preset)
            title = QString("%1 @ Bespin Demo").arg(preset);
        if (!app)
            app = new QApplication(argc, argv);
        Dialog *window = new Dialog;
        Ui::Demo ui;
        ui.setupUi(window);
        ui.tabWidget->setCurrentIndex(0);
        QObject::connect (ui.rtl, SIGNAL(toggled(bool)), window, SLOT(setLayoutDirection(bool)));
        window->setWindowTitle ( title );
        window->show();
        RETURN_APP_ERROR;
    }
    case Screenshot:
    {
        if (argc < 3)
            return error("you lack <some_output.png>");
#ifndef Q_WS_WIN
        if (argc > 3)
            setenv("BESPIN_PRESET", argv[3], 1);
#endif
        app = new QApplication(argc, argv);
        Dialog *window = new Dialog;
        Ui::Demo ui;
        ui.setupUi(window);
        ui.tabWidget->setCurrentIndex(0);
        ui.buttonBox->button(QDialogButtonBox::Ok)->setAttribute( Qt::WA_UnderMouse );
        ui.demoBrowser->setAttribute( Qt::WA_UnderMouse );
//       ui.demoLineEdit->setFocus(Qt::MouseFocusReason);

        QImage image(window->size(), QImage::Format_RGB32);
        window->showMinimized();
        window->QDialog::render(&image);
        if (argc > 4)
            image = image.scaledToWidth(atoi(argv[4]), Qt::SmoothTransformation);
        image.save ( argv[2], "png" );
        delete window;
        delete app;
        return 0;
    }
    case ListStyles:
    {
        foreach (QString string, QStyleFactory::keys())
            printf("%s\n", CHAR(string));
        return 0;
    }
    case ReadSetting:
    case WriteSetting:
    case DeleteSetting:
    case ListSettings:
    {
        bool e = false;
        bool deco = false;
        if (argc < (mode == WriteSetting ? 5 : (mode == ListSettings ? 3 : 4)))
            e = true;
        else if (!qstrcmp( argv[2], "deco" ))
            deco = true;
        else if (qstrcmp( argv[2], "style" ))
            e = true;
        if (e)
            return error(QString("try %1 %2 <style|deco> <key> [value]").arg(argv[0]).arg(argv[1]));
        QSettings config("Bespin", "Style");
        config.beginGroup(deco ? "Deco" : "Style");
        const QString key = QString::fromLocal8Bit(argv[3]);
        if (mode == ReadSetting)
            printf("%s\n", CHAR(config.value(key, argc > 4 ? argv[4] : QVariant()).toString()));
        else if (mode == DeleteSetting) {
            if (config.contains(key))
                config.remove(key);
            else
                return error(QString("there is no key \"%1\" in \"%2\"").arg(key).arg(argv[2]));
        }
        else if (mode == ListSettings) {
            QStringList keys = config.childKeys();
            if (argc > 3) {
                keys = keys.filter(key, Qt::CaseInsensitive);
            }
            foreach (const QString &k, keys)
                printf("%s : %s\n", CHAR(k), CHAR(config.value(k).toString()));
        }
        else
        {
            const bool added = !config.contains(QString::fromLocal8Bit(argv[3]));
            config.setValue(QString::fromLocal8Bit(argv[3]), QString::fromLocal8Bit(argv[4]));
            if (added)
                printf("added new key \"%s\" set to \"%s\"\n", argv[3], argv[4]);
            if (deco) // update kwin
                QProcess::startDetached("qdbus", QStringList() << "org.kde.kwin" << "/KWin" << "reconfigure" );
        }
        return 0;
    }
    default:
        return usage(argv[0]);
    }
}