diff options
author | Friedemann Kleint <Friedemann.Kleint@digia.com> | 2014-03-27 08:57:59 (GMT) |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2014-05-06 01:05:29 (GMT) |
commit | 248fb5b7f084c4ae81ea6b5a366a93e882930641 (patch) | |
tree | 04de9e7b3efea3ff546ea42860ebeb17377b9c71 /examples | |
parent | 2913f89cea4f9dd76988b854390526dd1fc35a99 (diff) | |
download | Qt-248fb5b7f084c4ae81ea6b5a366a93e882930641.zip Qt-248fb5b7f084c4ae81ea6b5a366a93e882930641.tar.gz Qt-248fb5b7f084c4ae81ea6b5a366a93e882930641.tar.bz2 |
Polish the image gestures example.
- Add command line parsing to properly evaluate help option and
directory argument, add options to disable gestures.
- Add verbose option so that the user can actually see what is
happening.
- Scale large images.
- Use qreal instead of float.
- Minor polishing: set window title, position file dialog at
pictures location.
Task-number: QTBUG-37759
Task-number: QTBUG-37203
Change-Id: Ibaf54a13034b150386a8aee476f83a9eba298298
Reviewed-by: Indrajit Tapadar <indrajit.tapadar@digia.com>
Reviewed-by: Shawn Rutledge <shawn.rutledge@digia.com>
(cherry picked from qtbase/c0701f3789acf3234e9f6431b4e8c7dc3ccefabb)
Reviewed-by: Marko Kangas <marko.kangas@digia.com>
Diffstat (limited to 'examples')
-rw-r--r-- | examples/gestures/imagegestures/imagewidget.cpp | 62 | ||||
-rw-r--r-- | examples/gestures/imagegestures/imagewidget.h | 13 | ||||
-rw-r--r-- | examples/gestures/imagegestures/main.cpp | 72 | ||||
-rw-r--r-- | examples/gestures/imagegestures/mainwidget.cpp | 18 | ||||
-rw-r--r-- | examples/gestures/imagegestures/mainwidget.h | 1 |
5 files changed, 137 insertions, 29 deletions
diff --git a/examples/gestures/imagegestures/imagewidget.cpp b/examples/gestures/imagegestures/imagewidget.cpp index eb7a512..7e884bd 100644 --- a/examples/gestures/imagegestures/imagewidget.cpp +++ b/examples/gestures/imagegestures/imagewidget.cpp @@ -43,6 +43,8 @@ #include <QtGui> +bool ImageWidget::verbose = false; + //! [constructor] ImageWidget::ImageWidget(QWidget *parent) : QWidget(parent), @@ -55,15 +57,17 @@ ImageWidget::ImageWidget(QWidget *parent) { setMinimumSize(QSize(100,100)); - -//! [enable gestures] - grabGesture(Qt::PanGesture); - grabGesture(Qt::PinchGesture); - grabGesture(Qt::SwipeGesture); -//! [enable gestures] } //! [constructor] +void ImageWidget::grabGestures(const QList<Qt::GestureType> &gestures) +{ + //! [enable gestures] + foreach (Qt::GestureType gesture, gestures) + grabGesture(gesture); + //! [enable gestures] +} + //! [event handler] bool ImageWidget::event(QEvent *event) { @@ -77,10 +81,10 @@ void ImageWidget::paintEvent(QPaintEvent*) { QPainter p(this); - float iw = currentImage.width(); - float ih = currentImage.height(); - float wh = height(); - float ww = width(); + const qreal iw = currentImage.width(); + const qreal ih = currentImage.height(); + const qreal wh = height(); + const qreal ww = width(); p.translate(ww/2, wh/2); p.translate(horizontalOffset, verticalOffset); @@ -98,11 +102,15 @@ void ImageWidget::mouseDoubleClickEvent(QMouseEvent *) verticalOffset = 0; horizontalOffset = 0; update(); + if (ImageWidget::verbose) + qDebug() << "reset on mouse double click"; } //! [gesture event handler] bool ImageWidget::gestureEvent(QGestureEvent *event) { + if (ImageWidget::verbose) + qDebug() << "gestureEvent():" << event->gestures().size(); if (QGesture *swipe = event->gesture(Qt::SwipeGesture)) swipeTriggered(static_cast<QSwipeGesture *>(swipe)); else if (QGesture *pan = event->gesture(Qt::PanGesture)) @@ -126,6 +134,8 @@ void ImageWidget::panTriggered(QPanGesture *gesture) } #endif QPointF delta = gesture->delta(); + if (ImageWidget::verbose) + qDebug() << "panTriggered():" << delta; horizontalOffset += delta.x(); verticalOffset += delta.y(); update(); @@ -135,13 +145,18 @@ void ImageWidget::pinchTriggered(QPinchGesture *gesture) { QPinchGesture::ChangeFlags changeFlags = gesture->changeFlags(); if (changeFlags & QPinchGesture::RotationAngleChanged) { - qreal value = gesture->property("rotationAngle").toReal(); - qreal lastValue = gesture->property("lastRotationAngle").toReal(); - rotationAngle += value - lastValue; + const qreal value = gesture->property("rotationAngle").toReal(); + const qreal lastValue = gesture->property("lastRotationAngle").toReal(); + const qreal rotationAngleDelta = value - lastValue; + rotationAngle += rotationAngleDelta; + if (ImageWidget::verbose) + qDebug() << "pinchTriggered(): rotation by" << rotationAngleDelta << rotationAngle; } if (changeFlags & QPinchGesture::ScaleFactorChanged) { qreal value = gesture->property("scaleFactor").toReal(); currentStepScaleFactor = value; + if (ImageWidget::verbose) + qDebug() << "pinchTriggered(): " << currentStepScaleFactor; } if (gesture->state() == Qt::GestureFinished) { scaleFactor *= currentStepScaleFactor; @@ -155,10 +170,15 @@ void ImageWidget::swipeTriggered(QSwipeGesture *gesture) { if (gesture->state() == Qt::GestureFinished) { if (gesture->horizontalDirection() == QSwipeGesture::Left - || gesture->verticalDirection() == QSwipeGesture::Up) + || gesture->verticalDirection() == QSwipeGesture::Up) { + if (ImageWidget::verbose) + qDebug() << "swipeTriggered(): swipe to previous"; goPrevImage(); - else + } else { + if (ImageWidget::verbose) + qDebug() << "swipeTriggered(): swipe to next"; goNextImage(); + } update(); } } @@ -184,17 +204,23 @@ void ImageWidget::openDirectory(const QString &path) QImage ImageWidget::loadImage(const QString &fileName) { + qDebug() << position << files << fileName; QImageReader reader(fileName); + if (ImageWidget::verbose) + qDebug() << "loading" << QDir::toNativeSeparators(fileName) << position << '/' << files.size(); if (!reader.canRead()) { - qDebug() << fileName << ": can't load image"; + qWarning() << QDir::toNativeSeparators(fileName) << ": can't load image"; return QImage(); } QImage image; if (!reader.read(&image)) { - qDebug() << fileName << ": corrupted image"; + qWarning() << QDir::toNativeSeparators(fileName) << ": corrupted image: " << reader.errorString(); return QImage(); } + const QSize maximumSize(2000, 2000); // Reduce in case someone has large photo images. + if (image.size().width() > maximumSize.width() || image.height() > maximumSize.height()) + image = image.scaled(maximumSize, Qt::KeepAspectRatio, Qt::SmoothTransformation); return image; } @@ -238,7 +264,7 @@ void ImageWidget::goToImage(int index) return; if (index < 0 || index >= files.size()) { - qDebug() << "goToImage: invalid index: " << index; + qWarning() << "goToImage: invalid index: " << index; return; } diff --git a/examples/gestures/imagegestures/imagewidget.h b/examples/gestures/imagegestures/imagewidget.h index 2d5d901..4c2ec00 100644 --- a/examples/gestures/imagegestures/imagewidget.h +++ b/examples/gestures/imagegestures/imagewidget.h @@ -60,6 +60,8 @@ class ImageWidget : public QWidget public: ImageWidget(QWidget *parent = 0); void openDirectory(const QString &path); + void grabGestures(const QList<Qt::GestureType> &gestures); + static void setVerbose(bool v) { ImageWidget::verbose = v; } protected: bool event(QEvent *event); @@ -88,11 +90,12 @@ private: QImage prevImage, nextImage; QImage currentImage; - float horizontalOffset; - float verticalOffset; - float rotationAngle; - float scaleFactor; - float currentStepScaleFactor; + qreal horizontalOffset; + qreal verticalOffset; + qreal rotationAngle; + qreal scaleFactor; + qreal currentStepScaleFactor; + static bool verbose; //! [class definition end] }; //! [class definition end] diff --git a/examples/gestures/imagegestures/main.cpp b/examples/gestures/imagegestures/main.cpp index 6f5a1c6..918d743 100644 --- a/examples/gestures/imagegestures/main.cpp +++ b/examples/gestures/imagegestures/main.cpp @@ -41,18 +41,82 @@ #include <QtGui> #include "mainwidget.h" +#include "imagewidget.h" + +static const char helpText[] = +"Usage: ./imagegestures [options] Directory\n\ +Image Gestures Example\n\ +\n\ +Options:\n\ + --no-pan Disable pan gesture\n\ + --no-pinch Disable pinch gesture\n\ + --no-swipe Disable swipe gesture\n\ + -h, --help Displays this help.\n\ +\n\ +Arguments:\n\ + Directory Directory to display\n"; + +static void showHelp(const QString errorMessage = QString()) +{ + QString text; + QTextStream str(&text); + str << "<html><head/><body>"; + if (!errorMessage.isEmpty()) + str << "<p>" << errorMessage << "</p>"; + str << "<pre>" << helpText << "</pre></body></html>"; + QMessageBox box(errorMessage.isEmpty() ? QMessageBox::Information : QMessageBox::Warning, + QLatin1String("Image Gestures Example"), text, + QMessageBox::Ok); + box.exec(); +} int main(int argc, char *argv[]) { QApplication app(argc, argv); + QCoreApplication::setApplicationVersion(QT_VERSION_STR); + QCoreApplication::setApplicationName(QLatin1String("imagegestures")); + + QStringList arguments = QApplication::arguments(); + + QString directory; + QList<Qt::GestureType> gestures; + gestures << Qt::PanGesture << Qt::PinchGesture << Qt::SwipeGesture; + for (int a = 1; a < arguments.size(); ++a) { + const QString &argument = arguments.at(a); + if (argument.startsWith(QLatin1Char('-'))) { + if (argument == QLatin1String("--no-pan")) + gestures.removeAll(Qt::PanGesture); + else if (argument == QLatin1String("--no-pinch")) + gestures.removeAll(Qt::PinchGesture); + else if (argument == QLatin1String("--no-swipe")) + gestures.removeAll(Qt::SwipeGesture); + else if (argument == QLatin1String("--verbose")) + ImageWidget::setVerbose(true); + else if (argument == QLatin1String("-h") || argument == QLatin1String("--help")) { + showHelp(); + return 0; + } else { + showHelp(QLatin1String("Invalid option: ") + argument); + return 1; + } + } else + directory = argument; + } MainWidget w; + w.grabGestures(gestures); w.show(); - if (QApplication::arguments().size() > 1) - w.openDirectory(QApplication::arguments().at(1)); - else - w.openDirectory(QFileDialog::getExistingDirectory(0, "Select image folder")); + if (directory.isEmpty()) { + const QString picturesLocation = QDesktopServices::storageLocation(QDesktopServices::PicturesLocation); + directory = + QFileDialog::getExistingDirectory(&w, "Select image folder", + picturesLocation.isEmpty() ? QString() : picturesLocation); + if (directory.isEmpty()) + return 0; + } + + w.openDirectory(directory); return app.exec(); } diff --git a/examples/gestures/imagegestures/mainwidget.cpp b/examples/gestures/imagegestures/mainwidget.cpp index 9737c5c..5b16cc2 100644 --- a/examples/gestures/imagegestures/mainwidget.cpp +++ b/examples/gestures/imagegestures/mainwidget.cpp @@ -41,15 +41,29 @@ #include "imagewidget.h" #include "mainwidget.h" +#include <QScreen> +#include <QApplication> +#include <QDesktopWidget> +#include <QDir> + MainWidget::MainWidget(QWidget *parent) : QMainWindow(parent) + , imageWidget(new ImageWidget(this)) { - resize(400, 300); - imageWidget = new ImageWidget(this); setCentralWidget(imageWidget); + const QRect screenGeometry = QApplication::desktop()->availableGeometry(); + QRect geometry(QPoint(0, 0), QSize(screenGeometry.width() * 3 / 4, screenGeometry.height() * 3 / 4)); + geometry.moveCenter(screenGeometry.center()); + setGeometry(geometry); } void MainWidget::openDirectory(const QString &path) { + setWindowTitle(QDir::toNativeSeparators(path)); imageWidget->openDirectory(path); } + +void MainWidget::grabGestures(const QList<Qt::GestureType> &gestures) +{ + imageWidget->grabGestures(gestures); +} diff --git a/examples/gestures/imagegestures/mainwidget.h b/examples/gestures/imagegestures/mainwidget.h index 1dc87ee..79ed3a0 100644 --- a/examples/gestures/imagegestures/mainwidget.h +++ b/examples/gestures/imagegestures/mainwidget.h @@ -51,6 +51,7 @@ class MainWidget : public QMainWindow public: MainWidget(QWidget *parent = 0); + void grabGestures(const QList<Qt::GestureType> &gestures); public slots: void openDirectory(const QString &path); |