main.cpp 3.14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/***
Pix  Copyright (C) 2018  Camilo Higuita
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.

 This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

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, see <http://www.gnu.org/licenses/>.
***/

21
#include <QApplication>
Camilo Higuita's avatar
src  
Camilo Higuita committed
22
#include <QQmlApplicationEngine>
23
#include <QQmlContext>
Camilo Higuita's avatar
Camilo Higuita committed
24
#include <QQuickStyle>
25
#include <QIcon>
26 27
#include <QCommandLineParser>
#include <QFileInfo>
28
#include "src/pix.h"
29

30 31 32
#ifdef Q_OS_ANDROID
#include "./3rdparty/kirigami/src/kirigamiplugin.h"
#endif
Camilo Higuita's avatar
src  
Camilo Higuita committed
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
QStringList getFolderImages(const QString &path)
{
    QStringList urls;

    if (QFileInfo(path).isDir())
    {
        QDirIterator it(path, PIX::formats, QDir::Files, QDirIterator::Subdirectories);
        while (it.hasNext()) urls<<it.next();

    } else if (QFileInfo(path).isFile()) urls<<path;

    return urls;
}

QStringList openFiles(const QStringList &files)
{
    QStringList urls;

    if(files.size()>1)
        urls = files;
    else
    {
        auto folder = QFileInfo(files.first()).dir().absolutePath();
        urls = getFolderImages(folder);
        urls.removeOne(QString(files.first()));
        urls.insert(0, QString(files.first()));
    }

    return urls;
}

Camilo Higuita's avatar
src  
Camilo Higuita committed
65 66 67 68
int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

69
    QApplication app(argc, argv);
70 71 72 73
    app.setApplicationName(PIX::App);
    app.setApplicationVersion(PIX::version);
    app.setApplicationDisplayName(PIX::App);
    app.setWindowIcon(QIcon(":/img/assets/pix.png"));
Camilo Higuita's avatar
Camilo Higuita committed
74

75 76 77 78 79 80 81 82 83 84 85 86 87 88
    QCommandLineParser parser;
    parser.setApplicationDescription("Pix Image gallery viewer");
    const QCommandLineOption versionOption = parser.addVersionOption();
    parser.addOption(versionOption);
    parser.process(app);

    const QStringList args = parser.positionalArguments();

    QStringList pics;

    if(!args.isEmpty())
        pics = openFiles(args);


89 90
#ifdef Q_OS_ANDROID
    KirigamiPlugin::getInstance().registerTypes();
91 92 93
    //#else
    //    if(QQuickStyle::availableStyles().contains("nomad"))
    //        QQuickStyle::setStyle("nomad");
94
#endif
Camilo Higuita's avatar
Camilo Higuita committed
95

96
    Pix pix;
97

Camilo Higuita's avatar
src  
Camilo Higuita committed
98
    QQmlApplicationEngine engine;
99 100 101 102
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, [&]()
    {
        qDebug()<<"FINISHED LOADING QML APP";
        pix.refreshCollection();
103 104
        if(!pics.isEmpty())
            pix.openPics(pics);
105
    });
106

107
    auto context = engine.rootContext();
Camilo Higuita's avatar
Camilo Higuita committed
108
    context->setContextProperty("pix", &pix);
109

Camilo Higuita's avatar
src  
Camilo Higuita committed
110 111 112 113 114 115
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;

    return app.exec();
}