1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the examples of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:BSD$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** BSD License Usage |
18 | ** Alternatively, you may use this file under the terms of the BSD license |
19 | ** as follows: |
20 | ** |
21 | ** "Redistribution and use in source and binary forms, with or without |
22 | ** modification, are permitted provided that the following conditions are |
23 | ** met: |
24 | ** * Redistributions of source code must retain the above copyright |
25 | ** notice, this list of conditions and the following disclaimer. |
26 | ** * Redistributions in binary form must reproduce the above copyright |
27 | ** notice, this list of conditions and the following disclaimer in |
28 | ** the documentation and/or other materials provided with the |
29 | ** distribution. |
30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
31 | ** contributors may be used to endorse or promote products derived |
32 | ** from this software without specific prior written permission. |
33 | ** |
34 | ** |
35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
46 | ** |
47 | ** $QT_END_LICENSE$ |
48 | ** |
49 | ****************************************************************************/ |
50 | |
51 | #include <QtWidgets> |
52 | |
53 | #include "mainwidget.h" |
54 | |
55 | static void showHelp(QCommandLineParser &parser, const QString errorMessage = QString()) |
56 | { |
57 | QString text; |
58 | QTextStream str(&text); |
59 | str << "<html><head/><body>" ; |
60 | if (!errorMessage.isEmpty()) |
61 | str << "<p>" << errorMessage << "</p>" ; |
62 | str << "<pre>" << parser.helpText() << "</pre></body></html>" ; |
63 | QMessageBox box(errorMessage.isEmpty() ? QMessageBox::Information : QMessageBox::Warning, |
64 | QGuiApplication::applicationDisplayName(), text, |
65 | QMessageBox::Ok); |
66 | box.setTextInteractionFlags(Qt::TextBrowserInteraction); |
67 | box.exec(); |
68 | } |
69 | |
70 | int main(int argc, char *argv[]) |
71 | { |
72 | QApplication app(argc, argv); |
73 | QCoreApplication::setApplicationVersion(QT_VERSION_STR); |
74 | QCoreApplication::setApplicationName(QStringLiteral("imagegestures" )); |
75 | QGuiApplication::setApplicationDisplayName(QStringLiteral("Image Gestures Example" )); |
76 | |
77 | QCommandLineParser commandLineParser; |
78 | const QCommandLineOption disablePanOption("no-pan" , "Disable pan gesture" ); |
79 | commandLineParser.addOption(commandLineOption: disablePanOption); |
80 | const QCommandLineOption disablePinchOption("no-pinch" , "Disable pinch gesture" ); |
81 | commandLineParser.addOption(commandLineOption: disablePinchOption); |
82 | const QCommandLineOption disableSwipeOption("no-swipe" , "Disable swipe gesture" ); |
83 | commandLineParser.addOption(commandLineOption: disableSwipeOption); |
84 | const QCommandLineOption helpOption = commandLineParser.addHelpOption(); |
85 | commandLineParser.addPositionalArgument(QStringLiteral("Directory" ), |
86 | QStringLiteral("Directory to display" )); |
87 | |
88 | const QString description = QGuiApplication::applicationDisplayName() |
89 | + QLatin1String("\n\nEnable \"debug\" on the logging category \"qt.examples.imagegestures\" in order to\n" |
90 | "in order to obtain verbose information about Qt's gesture event processing,\n" |
91 | "for example by setting the environment variables QT_LOGGING_RULES to\n" |
92 | "qt.examples.imagegestures.debug=true\n" ); |
93 | commandLineParser.setApplicationDescription(description); |
94 | |
95 | commandLineParser.process(arguments: QCoreApplication::arguments()); |
96 | |
97 | QStringList arguments = commandLineParser.positionalArguments(); |
98 | if (!arguments.isEmpty() && !QFileInfo(arguments.front()).isDir()) { |
99 | showHelp(parser&: commandLineParser, |
100 | errorMessage: QLatin1Char('"') + QDir::toNativeSeparators(pathName: arguments.front()) |
101 | + QStringLiteral("\" is not a directory." )); |
102 | return -1; |
103 | } |
104 | |
105 | QVector<Qt::GestureType> gestures; |
106 | if (!commandLineParser.isSet(option: disablePanOption)) |
107 | gestures << Qt::PanGesture; |
108 | if (!commandLineParser.isSet(option: disablePinchOption)) |
109 | gestures << Qt::PinchGesture; |
110 | if (!commandLineParser.isSet(option: disableSwipeOption)) |
111 | gestures << Qt::SwipeGesture; |
112 | |
113 | MainWidget w; |
114 | w.grabGestures(gestures); |
115 | w.show(); |
116 | |
117 | if (arguments.isEmpty()) { |
118 | const QStringList picturesLocations = QStandardPaths::standardLocations(type: QStandardPaths::PicturesLocation); |
119 | const QString directory = |
120 | QFileDialog::getExistingDirectory(parent: &w, caption: "Select image folder" , |
121 | dir: picturesLocations.isEmpty() ? QString() : picturesLocations.front()); |
122 | if (directory.isEmpty()) |
123 | return 0; |
124 | arguments.append(t: directory); |
125 | } |
126 | |
127 | w.openDirectory(path: arguments.front()); |
128 | |
129 | return app.exec(); |
130 | } |
131 | |