File indexing completed on 2024-12-15 04:14:22
0001 #include "peruse_helpers.h" 0002 #include <QQmlEngine> 0003 #include <QString> 0004 #include <QQmlComponent> 0005 #include <QQmlContext> 0006 #include <QStandardPaths> 0007 #include <QApplication> 0008 #include <QOpenGLContext> 0009 #include <QOffscreenSurface> 0010 #include <QOpenGLFunctions> 0011 #include <QDebug> 0012 #include <QDir> 0013 #include <QtQuick/QQuickView> 0014 0015 #include <KAboutData> 0016 #include <KCrash> 0017 #include <KLocalizedContext> 0018 #include <KQuickIconProvider> 0019 0020 #include "peruse_helpers.h" 0021 0022 namespace PeruseHelpers { 0023 int getMaxTextureSize() 0024 { 0025 int maxSize = 0; 0026 0027 // Create a temp context - required if this is called from another thread 0028 QOpenGLContext ctx; 0029 if ( !ctx.create() ) 0030 { 0031 // TODO handle the error 0032 qDebug() << "No OpenGL context could be created, this is clearly bad..."; 0033 exit(-1); 0034 } 0035 0036 // rather than using a QWindow - which actually doesn't seem to work in this case either! 0037 QOffscreenSurface surface; 0038 surface.setFormat( ctx.format() ); 0039 surface.create(); 0040 0041 ctx.makeCurrent(&surface); 0042 0043 // Now the call works 0044 QOpenGLFunctions glFuncs(QOpenGLContext::currentContext()); 0045 glFuncs.glEnable(GL_TEXTURE_2D); 0046 glFuncs.glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxSize); 0047 0048 return maxSize; 0049 } 0050 0051 int init(QString &path, QApplication& app, const QString &filename) { 0052 QQmlEngine engine; 0053 engine.addImageProvider(QStringLiteral("icon"), new KQuickIconProvider); 0054 engine.rootContext()->setContextObject(new KLocalizedContext(&app)); 0055 0056 bool osIsWindows = false; 0057 #ifdef Q_OS_WIN 0058 // Because windows is a bit funny with paths and whatnot, just so the thing with the lib paths... 0059 QDir appdir(qApp->applicationDirPath()); 0060 appdir.cdUp(); 0061 qApp->addLibraryPath(appdir.canonicalPath() + "/lib"); 0062 engine.addImportPath(appdir.canonicalPath() + "/lib/qml"); 0063 engine.addImportPath(appdir.canonicalPath() + "/qml"); 0064 osIsWindows = true; 0065 #endif 0066 0067 engine.rootContext()->setContextProperty("osIsWindows", osIsWindows); 0068 0069 engine.rootContext()->setContextProperty("fileToOpen", filename); 0070 0071 QQmlContext* objectContext = engine.rootContext(); 0072 QString platformEnv(qgetenv("PLASMA_PLATFORM")); 0073 engine.rootContext()->setContextProperty("PLASMA_PLATFORM", platformEnv); 0074 0075 // Yes, i realise this is a touch on the ugly side. I have found no better way to allow for 0076 // things like the archive book model to create imageproviders for the archives 0077 engine.rootContext()->setContextProperty("globalQmlEngine", &engine); 0078 engine.rootContext()->setContextProperty("maxTextureSize", getMaxTextureSize()); 0079 engine.rootContext()->setContextProperty(QStringLiteral("peruseAboutData"), QVariant::fromValue(KAboutData::applicationData())); 0080 0081 QQmlComponent component(&engine, path); 0082 if (component.isError()) { 0083 qCritical() << "Failed to load the component from disk. Reported error was:" << component.errorString(); 0084 return -1; 0085 } 0086 if (component.status() != QQmlComponent::Ready) { 0087 qCritical() << "Failed to make the Qt Quick component ready. Status is:" << component.status(); 0088 return -3; 0089 } 0090 0091 QObject* obj = component.create(objectContext); 0092 if (!obj) { 0093 qCritical() << "Failed to create an object from our component"; 0094 return -2; 0095 } 0096 QQuickWindow* window = qobject_cast<QQuickWindow*>(obj); 0097 QObject::connect(window, &QQuickView::sceneGraphError, &app, [] (QQuickWindow::SceneGraphError /*error*/, const QString &message) { 0098 KCrash::setErrorMessage(message); 0099 qFatal("%s", qPrintable(message)); 0100 }); 0101 0102 return app.exec(); 0103 } 0104 }