File indexing completed on 2024-05-12 17:08:24

0001 /*
0002     SPDX-FileCopyrightText: 2014 Sebastian Kügler <sebas@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 // Qt
0008 #include <QApplication>
0009 #include <QCommandLineOption>
0010 #include <QCommandLineParser>
0011 #include <QDebug>
0012 #include <QQmlApplicationEngine>
0013 #include <QQmlContext>
0014 #include <QQmlEngine>
0015 
0016 // Frameworks
0017 #include <KConfigGroup>
0018 #include <KLocalizedString>
0019 #include <Plasma/Theme>
0020 
0021 // Own
0022 #include "colorschemes.h"
0023 #include "iconmodel.h"
0024 #include "sortfiltermodel.h"
0025 
0026 void messageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
0027 {
0028     if ((msg.contains("qt.svg") && msg.contains("Could not resolve property: #linearGradient")) || msg.contains("Could not resolve property: #pattern")) {
0029         return;
0030     }
0031 
0032     QByteArray localMsg = msg.toLocal8Bit();
0033     const char *file = context.file ? context.file : "";
0034     const char *function = context.function ? context.function : "";
0035     switch (type) {
0036     case QtDebugMsg:
0037         fprintf(stderr, "Debug: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function);
0038         break;
0039     case QtInfoMsg:
0040         fprintf(stderr, "Info: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function);
0041         break;
0042     case QtWarningMsg:
0043         fprintf(stderr, "\u001b[33mWarning\u001b[0m: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function);
0044         break;
0045     case QtCriticalMsg:
0046         fprintf(stderr, "\u001b[33;1mCritical\u001b[0m: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function);
0047         break;
0048     case QtFatalMsg:
0049         fprintf(stderr, "\u001b[31;1mFatal\u001b[0m: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function);
0050         break;
0051     }
0052 }
0053 
0054 int main(int argc, char **argv)
0055 {
0056     qInstallMessageHandler(messageOutput);
0057 
0058     QApplication app(argc, argv);
0059     KLocalizedString::setApplicationDomain("cuttlefish");
0060 
0061     app.setApplicationVersion(PROJECT_VERSION);
0062     app.setDesktopFileName(QStringLiteral("org.kde.plasma.cuttlefish"));
0063     app.setOrganizationName("KDE");
0064     app.setOrganizationDomain("org.kde");
0065     app.setApplicationName("Cuttlefish");
0066 
0067     const static auto _category = QStringLiteral("category");
0068     QCommandLineOption category = QCommandLineOption(QStringList() << QStringLiteral("c") << _category, i18n("Start with category"), i18n("category"));
0069 
0070     const static auto _f = QStringLiteral("fullscreen");
0071     QCommandLineOption fullscreen = QCommandLineOption(QStringList() << QStringLiteral("f") << _f, i18n("Start full-screen"));
0072 
0073     const static auto _p = QStringLiteral("picker");
0074     QCommandLineOption picker = QCommandLineOption(QStringList() << QStringLiteral("p") << _p, i18n("Run in icon-picker mode"));
0075 
0076     QCommandLineParser parser;
0077     parser.addVersionOption();
0078     parser.setApplicationDescription("Cuttlefish Icon Browser");
0079     parser.addHelpOption();
0080     parser.addOption(category);
0081     parser.addOption(fullscreen);
0082     parser.addOption(picker);
0083 
0084     parser.process(app);
0085 
0086     QString _cc = parser.value(category);
0087 
0088     QQmlApplicationEngine engine;
0089 
0090     auto l10nContext = new KLocalizedContext(&engine);
0091     l10nContext->setTranslationDomain(QStringLiteral("cuttlefish"));
0092     engine.rootContext()->setContextObject(l10nContext);
0093 
0094     auto iconModel = new CuttleFish::IconModel(engine.rootContext());
0095     auto proxyModel = new CuttleFish::SortFilterModel(engine.rootContext());
0096     proxyModel->setSourceModel(iconModel);
0097     proxyModel->sort(0);
0098     auto colorSchemes = new CuttleFish::ColorSchemes(engine.rootContext());
0099 
0100     engine.rootContext()->setContextProperty("iconModel", iconModel);
0101     engine.rootContext()->setContextProperty("proxyModel", proxyModel);
0102     engine.rootContext()->setContextProperty("pickerMode", parser.isSet("picker"));
0103     engine.rootContext()->setContextProperty("colorSchemes", colorSchemes);
0104 
0105     engine.load(QUrl("qrc:/qml/cuttlefish.qml"));
0106     if (engine.rootObjects().isEmpty()) {
0107         return -1;
0108     }
0109 
0110     qmlRegisterAnonymousType<CuttleFish::IconModel>("org.kde.plasma.sdk", 1);
0111     qmlRegisterAnonymousType<CuttleFish::ColorSchemes>("org.kde.plasma.sdk", 1);
0112 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0113     app.setAttribute(Qt::AA_UseHighDpiPixmaps);
0114 #endif
0115     return app.exec();
0116 }