Skip to content
Snippets Groups Projects
Select Git revision
  • 397dcf87e6d54dd065032925895ef1469fa4b62f
  • master default protected
  • development
  • cpp-experiment
  • v0.3.0
5 results

main.cpp

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    main.cpp 2.01 KiB
    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlContext>
    #include <QVariantList>
    #include <QImageReader>
    #include <QColor>
    #include <QDir>
    #include <QUrl>
    
    QString getFileUrlRelativeToExecutable(const QString &relativeFilePath) {
        // Get the directory path of the executable
        QString basePath = QCoreApplication::applicationDirPath();
        // Combine the base path with the relative file path
        QDir dir(basePath);
        QString fullPath = dir.filePath(relativeFilePath);  // Ensures the path is correctly formed
        // Convert the file path to a URL
        QUrl fileUrl = QUrl::fromLocalFile(fullPath);
        return fileUrl.toString();
    }
    
    
    int main(int argc, char *argv[])
    {
        QGuiApplication gui_app(argc, argv);
    
        // Check if TIFF format is supported
        QStringList supportedFormats;
        for (const QByteArray &format : QImageReader::supportedImageFormats()) {
            supportedFormats.append(QString::fromLatin1(format));
        }
        if (!supportedFormats.contains("tiff", Qt::CaseInsensitive)) {
            qWarning() << "TIFF format is not supported!";
            return -1;
        }
    
        QQmlApplicationEngine engine;
    
        QString mapImagePath = getFileUrlRelativeToExecutable("../../data/regions/jena-small/landcover.tif");
        engine.rootContext()->setContextProperty("mapImagePath", mapImagePath);
    
        QVariantList colors;
        colors.append(QVariant::fromValue(QColor(255, 0, 0)));   // Red
        colors.append(QVariant::fromValue(QColor(0, 255, 0)));   // Green
        colors.append(QVariant::fromValue(QColor(0, 0, 255)));   // Blue
        engine.rootContext()->setContextProperty("colorList", colors);
    
        const QUrl qml_url(u"qrc:/main.qml"_qs);
        QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &gui_app,
                         [qml_url](QObject *obj, const QUrl &obj_url) {
                             if (!obj && qml_url == obj_url)
                                 QCoreApplication::exit(-1);
                         },
                         Qt::QueuedConnection);
        engine.load(qml_url);
        return gui_app.exec();
    }