~zeal-developers/zeal/master

« back to all changes in this revision

Viewing changes to src/main.cpp

  • Committer: Oleg Shparber
  • Date: 2015-01-12 03:51:13 UTC
  • Revision ID: git-v1:792a35241c995b7f5913ee426f4bf5aa706b43d9
Move .pro file to the top, rename zeal dir into src

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "ui/mainwindow.h"
 
2
 
 
3
#include <QApplication>
 
4
#include <QCommandLineParser>
 
5
#include <QDir>
 
6
#include <QLocalSocket>
 
7
#include <QStandardPaths>
 
8
#include <QTextStream>
 
9
 
 
10
#ifdef Q_OS_WIN32
 
11
#include <QProxyStyle>
 
12
#include <QStyleOption>
 
13
#endif
 
14
 
 
15
struct CommandLineParameters
 
16
{
 
17
    bool force;
 
18
    QString query;
 
19
};
 
20
 
 
21
CommandLineParameters parseCommandLine(const QCoreApplication &app)
 
22
{
 
23
    QCommandLineParser parser;
 
24
    parser.setApplicationDescription(QObject::tr("Zeal - Offline documentation browser."));
 
25
    parser.addHelpOption();
 
26
    parser.addVersionOption();
 
27
 
 
28
    /// TODO: [Qt 5.4] parser.addOption({{"f", "force"}, "Force the application run."});
 
29
    parser.addOption(QCommandLineOption({QStringLiteral("f"), QStringLiteral("force")},
 
30
                                        QObject::tr("Force the application run.")));
 
31
    parser.addOption(QCommandLineOption({QStringLiteral("q"), QStringLiteral("query")},
 
32
                                        QObject::tr("Query <search term>."),
 
33
                                        QStringLiteral("term")));
 
34
    parser.process(app);
 
35
 
 
36
    return {
 
37
        parser.isSet(QStringLiteral("force")),
 
38
        parser.value(QStringLiteral("query"))
 
39
    };
 
40
}
 
41
 
 
42
/// TODO: Verify if this bug still exists in Qt 5.2+
 
43
#ifdef Q_OS_WIN32
 
44
class ZealProxyStyle : public QProxyStyle
 
45
{
 
46
public:
 
47
    void drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter,
 
48
                       const QWidget *widget = 0) const
 
49
    {
 
50
        if (element == PE_FrameLineEdit && option->styleObject) {
 
51
            option->styleObject->setProperty("_q_no_animation", true);
 
52
            // Workaround for a probable bug in QWindowsVistaStyle - for example opening the 'String (CommandEvent)'
 
53
            // item from wxPython docset (available at http://wxpython.org/Phoenix/docsets) causes very high CPU usage.
 
54
            // Some rough debugging shows that the 'd->startAnimation(t);' call in QWindowsVistaStyle::drawPrimitive
 
55
            // is the cuplrit and setting _q_no_animation to true here fixes the issue.
 
56
        }
 
57
        return QProxyStyle::drawPrimitive(element, option, painter, widget);
 
58
    }
 
59
};
 
60
#endif
 
61
 
 
62
int main(int argc, char *argv[])
 
63
{
 
64
    QCoreApplication::setApplicationName(QStringLiteral("Zeal"));
 
65
    QCoreApplication::setApplicationVersion(ZEAL_VERSION);
 
66
    QCoreApplication::setOrganizationDomain(QStringLiteral("zealdocs.org"));
 
67
    QCoreApplication::setOrganizationName(QStringLiteral("Zeal"));
 
68
 
 
69
    QApplication qapp(argc, argv);
 
70
 
 
71
#ifdef Q_OS_WIN32
 
72
    qapp.setStyle(new ZealProxyStyle());
 
73
#endif
 
74
 
 
75
    const CommandLineParameters clParams = parseCommandLine(qapp);
 
76
 
 
77
    // Detect already running instance and optionally pass a search query to it.
 
78
    if (!clParams.force) {
 
79
        QScopedPointer<QLocalSocket> socket(new QLocalSocket());
 
80
        socket->connectToServer(serverName);
 
81
 
 
82
        if (socket->waitForConnected(500)) {
 
83
            if (!clParams.query.isEmpty()) {
 
84
                socket->write(clParams.query.toLocal8Bit());
 
85
                socket->flush();
 
86
            } else {
 
87
                QTextStream(stdout) << QObject::tr("Already running. Terminating.") << endl;
 
88
            }
 
89
 
 
90
            return 0;
 
91
        }
 
92
    }
 
93
 
 
94
    // look for icons in:
 
95
    // 1. user's data directory (same as docsets dir, but in icons/)
 
96
    // 2. executable directory/icons
 
97
    // 3. on unix, standard/legacy install location
 
98
    // 4. current directory/icons
 
99
    QStringList searchPaths;
 
100
    searchPaths << QStandardPaths::locateAll(QStandardPaths::DataLocation, QStringLiteral("icons"),
 
101
                                             QStandardPaths::LocateDirectory);
 
102
    searchPaths << QDir(QCoreApplication::applicationDirPath())
 
103
                   .absoluteFilePath(QStringLiteral("icons"));
 
104
#ifndef Q_OS_WIN32
 
105
    searchPaths << QStringLiteral("/usr/share/pixmaps/zeal");
 
106
#endif
 
107
    searchPaths << QStringLiteral("./icons");
 
108
    QDir::setSearchPaths(QStringLiteral("icons"), searchPaths);
 
109
 
 
110
    QScopedPointer<MainWindow> mainWindow(new MainWindow());
 
111
 
 
112
    if (!mainWindow->startHidden())
 
113
        mainWindow->show();
 
114
 
 
115
    if (!clParams.query.isEmpty())
 
116
        mainWindow->bringToFrontAndSearch(clParams.query);
 
117
 
 
118
    return qapp.exec();
 
119
}