Changeset View
Changeset View
Standalone View
Standalone View
src/creator/main.cpp
- This file was copied to src/common/peruse_helpers.cpp.
Show All 11 Lines | |||||
12 | * Lesser General Public License for more details. | 12 | * Lesser General Public License for more details. | ||
13 | * | 13 | * | ||
14 | * You should have received a copy of the GNU Lesser General Public | 14 | * You should have received a copy of the GNU Lesser General Public | ||
15 | * License along with this library; if not, write to the Free Software | 15 | * License along with this library; if not, write to the Free Software | ||
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
17 | * | 17 | * | ||
18 | */ | 18 | */ | ||
19 | 19 | | |||
20 | #include <QQmlEngine> | | |||
21 | #include <QQmlContext> | | |||
22 | #include <QQmlComponent> | | |||
23 | | ||||
24 | #include <QStandardPaths> | 20 | #include <QStandardPaths> | ||
25 | #include <QDir> | | |||
26 | #include <QDebug> | | |||
27 | #include <QThread> | | |||
28 | 21 | | |||
29 | #include <KDeclarative/KDeclarative> | | |||
30 | #include <KLocalizedString> | 22 | #include <KLocalizedString> | ||
31 | 23 | | |||
32 | #include <QApplication> | 24 | #include <QApplication> | ||
33 | #include <QCommandLineParser> | 25 | #include <QCommandLineParser> | ||
34 | #include <QCommandLineOption> | 26 | #include <QCommandLineOption> | ||
35 | 27 | | |||
36 | #include <iostream> | 28 | #include <iostream> | ||
37 | 29 | #include "peruse_helpers.h" | |||
38 | #include <QOpenGLContext> | | |||
39 | #include <QOffscreenSurface> | | |||
40 | #include <QOpenGLFunctions> | | |||
41 | | ||||
42 | int getMaxTextureSize() | | |||
43 | { | | |||
44 | int maxSize = 0; | | |||
45 | | ||||
46 | // Create a temp context - required if this is called from another thread | | |||
47 | QOpenGLContext ctx; | | |||
48 | if ( !ctx.create() ) | | |||
49 | { | | |||
50 | // TODO handle the error | | |||
51 | qDebug() << "No OpenGL context could be created, this is clearly bad..."; | | |||
52 | exit(-1); | | |||
53 | } | | |||
54 | | ||||
55 | // rather than using a QWindow - which actually dosen't seem to work in this case either! | | |||
56 | QOffscreenSurface surface; | | |||
57 | surface.setFormat( ctx.format() ); | | |||
58 | surface.create(); | | |||
59 | | ||||
60 | ctx.makeCurrent(&surface); | | |||
61 | | ||||
62 | // Now the call works | | |||
63 | QOpenGLFunctions glFuncs(QOpenGLContext::currentContext()); | | |||
64 | glFuncs.glEnable(GL_TEXTURE_2D); | | |||
65 | glFuncs.glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxSize); | | |||
66 | | ||||
67 | return maxSize; | | |||
68 | } | | |||
69 | | ||||
70 | 30 | | |||
71 | int main(int argc, char** argv) | 31 | int main(int argc, char** argv) | ||
72 | { | 32 | { | ||
73 | QApplication app(argc, argv); | 33 | QApplication app(argc, argv); | ||
74 | app.setApplicationDisplayName("Peruse Creator"); | 34 | app.setApplicationDisplayName("Peruse Creator"); | ||
75 | 35 | | |||
76 | QCommandLineParser parser; | 36 | QCommandLineParser parser; | ||
77 | // TODO file option for opening comics by passing them through on the command line | 37 | // TODO file option for opening comics by passing them through on the command line | ||
78 | parser.addHelpOption(); | 38 | parser.addHelpOption(); | ||
79 | parser.process(app); | 39 | parser.process(app); | ||
80 | 40 | | |||
81 | if (parser.positionalArguments().size() > 1) { | 41 | if (parser.positionalArguments().size() > 1) { | ||
82 | parser.showHelp(1); | 42 | parser.showHelp(1); | ||
83 | } | 43 | } | ||
84 | 44 | | |||
85 | KDeclarative::KDeclarative kdeclarative; | | |||
86 | QQmlEngine engine; | | |||
87 | kdeclarative.setDeclarativeEngine(&engine); | | |||
88 | kdeclarative.setupBindings(); | | |||
89 | | ||||
90 | bool osIsWindows = false; | | |||
91 | #ifdef Q_OS_WIN | | |||
92 | // Because windows is a bit funny with paths and whatnot, just so the thing with the lib paths... | | |||
93 | QDir appdir(qApp->applicationDirPath()); | | |||
94 | appdir.cdUp(); | | |||
95 | engine.addImportPath(appdir.canonicalPath() + "/lib/qml"); | | |||
96 | engine.addImportPath(appdir.canonicalPath() + "/lib/qml"); | | |||
97 | engine.addImportPath(appdir.canonicalPath() + "/qml"); | | |||
98 | osIsWindows = true; | | |||
99 | // Hey, let's try and avoid all those extra stale processes, right? | | |||
100 | qputenv("KDE_FORK_SLAVES", "true"); | | |||
101 | #endif | | |||
102 | engine.rootContext()->setContextProperty("osIsWindows", osIsWindows); | | |||
103 | | ||||
104 | QQmlContext* objectContext = engine.rootContext(); | | |||
105 | QString platformEnv(qgetenv("PLASMA_PLATFORM")); | | |||
106 | engine.rootContext()->setContextProperty("PLASMA_PLATFORM", platformEnv); | | |||
107 | // Yes, i realise this is a touch on the ugly side. I have found no better way to allow for | | |||
108 | // things like the archive book model to create imageproviders for the archives | | |||
109 | engine.rootContext()->setContextProperty("globalQmlEngine", &engine); | | |||
110 | engine.rootContext()->setContextProperty("maxTextureSize", getMaxTextureSize()); | | |||
111 | | ||||
112 | QString path= QStandardPaths::locate(QStandardPaths::DataLocation, "qml/Main.qml"); | 45 | QString path= QStandardPaths::locate(QStandardPaths::DataLocation, "qml/Main.qml"); | ||
113 | if(path.isEmpty()) { | 46 | return PeruseHelpers::init(path, app); | ||
114 | qCritical() << "The file structure is not set up currectly, and our data is somewhere weird."; | | |||
115 | path = QString("%1/data/perusecreator/qml/Main.qml").arg(qApp->applicationDirPath()); | | |||
116 | } | | |||
117 | int rt = 0; | | |||
118 | QQmlComponent component(&engine, path); | | |||
119 | if (component.isError()) | | |||
120 | { | | |||
121 | qCritical() << "Failed to load the component from disk. Reported error was:" << component.errorString(); | | |||
122 | rt = -1; | | |||
123 | } | | |||
124 | else | | |||
125 | { | | |||
126 | if(component.status() == QQmlComponent::Ready) | | |||
127 | { | | |||
128 | QObject* obj = component.create(objectContext); | | |||
129 | if(obj) | | |||
130 | { | | |||
131 | rt = app.exec(); | | |||
132 | } | | |||
133 | else | | |||
134 | { | | |||
135 | qCritical() << "Failed to create an object from our component"; | | |||
136 | rt = -2; | | |||
137 | } | | |||
138 | } | | |||
139 | else | | |||
140 | { | | |||
141 | qCritical() << "Failed to make the Qt Quick component ready. Status is:" << component.status(); | | |||
142 | rt = -3; | | |||
143 | } | | |||
144 | } | | |||
145 | | ||||
146 | return rt; | | |||
147 | } | 47 | } |