main.cpp 4.45 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/>.
***/

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

29
#ifdef Q_OS_ANDROID
30
#include <QGuiApplication>
31
#include <QIcon>
32 33
#else
#include <QApplication>
34
#endif
Camilo Higuita's avatar
Camilo Higuita committed
35

36 37 38 39
#ifdef STATIC_KIRIGAMI
#include "./3rdparty/kirigami/src/kirigamiplugin.h"
#endif

40 41 42
#ifdef STATIC_MAUIKIT
#include "./mauikit/src/mauikit.h"
#endif
43

44
#ifdef STATIC_MAUIKIT
45
#include "fmh.h"
46 47
#include "tagging.h"
#else
48
#include <MauiKit/fmh.h>
49 50 51
#include <MauiKit/tagging.h>
#endif

52 53 54
#include "./src/models/basemodel.h"
#include "./src/models/baselist.h"
#include "./src/models/gallery/gallery.h"
55
#include "./src/models/albums/albums.h"
Camilo higuita's avatar
Camilo higuita committed
56
//#include "./src/models/cloud/cloud.h"
57 58 59

#include "./src/models/folders/foldermodel.h"
#include "./src/models/folders/folders.h"
60

61 62 63 64 65 66
QStringList getFolderImages(const QString &path)
{
    QStringList urls;

    if (QFileInfo(path).isDir())
    {
67
        QDirIterator it(path, FMH::FILTER_LIST[FMH::FILTER_TYPE::IMAGE], QDir::Files, QDirIterator::Subdirectories);
Camilo higuita's avatar
Camilo higuita committed
68 69
        while (it.hasNext())
            urls << it.next();
70

Camilo higuita's avatar
Camilo higuita committed
71 72
    }else if (QFileInfo(path).isFile())
        urls << path;
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

    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
Camilo Higuita committed
94 95 96 97
int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

98 99 100
#ifdef Q_OS_ANDROID
    QGuiApplication app(argc, argv);
#else
101
    QApplication app(argc, argv);
102 103
#endif

104 105 106 107
    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
108

109 110 111 112 113 114 115 116 117 118 119 120 121
    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);

122
    Pix pix;
123

Camilo Higuita's avatar
Camilo Higuita committed
124
    QQmlApplicationEngine engine;
125 126 127
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, [&]()
    {
        pix.refreshCollection();
128 129
        if(!pics.isEmpty())
            pix.openPics(pics);
130
    });
131

132
    auto context = engine.rootContext();
Camilo Higuita's avatar
Camilo Higuita committed
133
    context->setContextProperty("pix", &pix);
134 135 136 137

    const auto dba = DBActions::getInstance();
    context->setContextProperty("tag", dba->tag);
    context->setContextProperty("dba", dba);
138

139
//    qmlRegisterUncreatableMetaObject(PIX::staticMetaObject, "PIX", 1, 0, "KEY", "Error");
140 141 142 143 144

    qmlRegisterUncreatableType<BaseList>("BaseList", 1, 0, "BaseList", QStringLiteral("BaseList should not be created in QML"));

    qmlRegisterType<BaseModel>("PixModel", 1, 0, "PixModel");
    qmlRegisterType<Gallery>("GalleryList", 1, 0, "GalleryList");
145
    qmlRegisterType<Albums>("AlbumsList", 1, 0, "AlbumsList");
146 147
    qmlRegisterType<FolderModel>("FolderModel", 1, 0, "FolderModel");
    qmlRegisterType<Folders>("FoldersList", 1, 0, "FoldersList");
Camilo higuita's avatar
Camilo higuita committed
148
//    qmlRegisterType<Cloud>("CloudList", 1, 0, "CloudList");
149

150
#ifdef STATIC_KIRIGAMI
151
    KirigamiPlugin::getInstance().registerTypes();
152 153
#endif

154 155
#ifdef STATIC_MAUIKIT
    MauiKit::getInstance().registerTypes();
156 157
#endif

Camilo Higuita's avatar
Camilo Higuita committed
158 159 160 161 162 163
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;

    return app.exec();
}