Polish the imageviewer example.
authorFriedemann Kleint <Friedemann.Kleint@theqtcompany.com>
Thu, 9 Jul 2015 11:25:16 +0000 (13:25 +0200)
committerFriedemann Kleint <Friedemann.Kleint@theqtcompany.com>
Fri, 21 Aug 2015 10:37:22 +0000 (10:37 +0000)
- Remove unneeded member variables.
- Use member initialization in the constructor.
- Use new connection syntax in createActions()
  to assemble the menu there, removing the createMenus()
  function.
- Fix coding style issue (braces)
- Avoid empty label showing up by setting the scroll
  area invisible until an image is loaded.
- Set a new image only if image read succeeds.
- Add status bar with information message.

Task-number: QTBUG-46848
Change-Id: I32d5af70d8eb71ec16dd58a0b98c32eb2bd988d7
Reviewed-by: Topi Reiniƶ <topi.reinio@digia.com>
examples/widgets/doc/src/imageviewer.qdoc
examples/widgets/widgets/imageviewer/imageviewer.cpp
examples/widgets/widgets/imageviewer/imageviewer.h

index 91ae56b5d781840809205ce502f41e4690cfadae..4457da9d6f6997a73a6222994e7307bb2a6c2496 100644 (file)
     \snippet widgets/imageviewer/imageviewer.cpp 18
 
     In the private \c createAction() function, we create the
-    actions providing the application features.
+    actions providing the application features and populate
+    a menu with them.
 
     We assign a short-cut key to each action and connect them to the
     appropriate slots. We only enable the \c openAct and \c exitAct at
     been loaded into the application. In addition we make the \c
     fitToWindowAct \l {QAction::checkable}{checkable}.
 
-    \snippet widgets/imageviewer/imageviewer.cpp 19
-    \snippet widgets/imageviewer/imageviewer.cpp 20
-
-    In the private \c createMenu() function, we add the previously
-    created actions to the \uicontrol File, \uicontrol View and \uicontrol Help menus.
-
     The QMenu class provides a menu widget for use in menu bars,
     context menus, and other popup menus. The QMenuBar class provides
     a horizontal menu bar that consists of a list of pull-down menu
-    items. So at the end we put the menus in the \c {ImageViewer}'s
+    items. So we put the menus in the \c {ImageViewer}'s
     menu bar which we retrieve with the QMainWindow::menuBar()
     function.
 
index 93b88e9c1855638e192554c5ef5ccc8fcd4ccf90..2c36491b5083acc3a3cb71b26b23df4ab9780adb 100644 (file)
 
 //! [0]
 ImageViewer::ImageViewer()
+   : imageLabel(new QLabel)
+   , scrollArea(new QScrollArea)
+   , scaleFactor(1)
 {
-    imageLabel = new QLabel;
     imageLabel->setBackgroundRole(QPalette::Base);
     imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
     imageLabel->setScaledContents(true);
 
-    scrollArea = new QScrollArea;
     scrollArea->setBackgroundRole(QPalette::Dark);
     scrollArea->setWidget(imageLabel);
+    scrollArea->setVisible(false);
     setCentralWidget(scrollArea);
 
     createActions();
-    createMenus();
 
     resize(QGuiApplication::primaryScreen()->availableSize() * 3 / 5);
 }
@@ -74,10 +75,8 @@ bool ImageViewer::loadFile(const QString &fileName)
     const QImage image = reader.read();
     if (image.isNull()) {
         QMessageBox::information(this, QGuiApplication::applicationDisplayName(),
-                                 tr("Cannot load %1.").arg(QDir::toNativeSeparators(fileName)));
-        setWindowFilePath(QString());
-        imageLabel->setPixmap(QPixmap());
-        imageLabel->adjustSize();
+                                 tr("Cannot load %1: %2")
+                                 .arg(QDir::toNativeSeparators(fileName)), reader.errorString());
         return false;
     }
 //! [2] //! [3]
@@ -85,6 +84,7 @@ bool ImageViewer::loadFile(const QString &fileName)
 //! [3] //! [4]
     scaleFactor = 1.0;
 
+    scrollArea->setVisible(true);
     printAct->setEnabled(true);
     fitToWindowAct->setEnabled(true);
     updateActions();
@@ -93,6 +93,10 @@ bool ImageViewer::loadFile(const QString &fileName)
         imageLabel->adjustSize();
 
     setWindowFilePath(fileName);
+
+    const QString message = tr("Opened \"%1\", %2x%3, Depth: %4")
+        .arg(QDir::toNativeSeparators(fileName)).arg(image.width()).arg(image.height()).arg(image.depth());
+    statusBar()->showMessage(message);
     return true;
 }
 
@@ -167,9 +171,8 @@ void ImageViewer::fitToWindow()
 {
     bool fitToWindow = fitToWindowAct->isChecked();
     scrollArea->setWidgetResizable(fitToWindow);
-    if (!fitToWindow) {
+    if (!fitToWindow)
         normalSize();
-    }
     updateActions();
 }
 //! [14]
@@ -199,75 +202,48 @@ void ImageViewer::about()
 void ImageViewer::createActions()
 //! [17] //! [18]
 {
-    openAct = new QAction(tr("&Open..."), this);
-    openAct->setShortcut(tr("Ctrl+O"));
-    connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
+    QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
 
-    printAct = new QAction(tr("&Print..."), this);
-    printAct->setShortcut(tr("Ctrl+P"));
+    QAction *openAct = fileMenu->addAction(tr("&Open..."), this, &ImageViewer::open);
+    openAct->setShortcut(QKeySequence::Open);
+
+    printAct = fileMenu->addAction(tr("&Print..."), this, &ImageViewer::print);
+    printAct->setShortcut(QKeySequence::Print);
     printAct->setEnabled(false);
-    connect(printAct, SIGNAL(triggered()), this, SLOT(print()));
 
-    exitAct = new QAction(tr("E&xit"), this);
+    fileMenu->addSeparator();
+
+    QAction *exitAct = fileMenu->addAction(tr("E&xit"), this, &QWidget::close);
     exitAct->setShortcut(tr("Ctrl+Q"));
-    connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
 
-    zoomInAct = new QAction(tr("Zoom &In (25%)"), this);
-    zoomInAct->setShortcut(tr("Ctrl++"));
+    QMenu *viewMenu = menuBar()->addMenu(tr("&View"));
+
+    zoomInAct = viewMenu->addAction(tr("Zoom &In (25%)"), this, &ImageViewer::zoomIn);
+    zoomInAct->setShortcut(QKeySequence::ZoomIn);
     zoomInAct->setEnabled(false);
-    connect(zoomInAct, SIGNAL(triggered()), this, SLOT(zoomIn()));
 
-    zoomOutAct = new QAction(tr("Zoom &Out (25%)"), this);
-    zoomOutAct->setShortcut(tr("Ctrl+-"));
+    zoomOutAct = viewMenu->addAction(tr("Zoom &Out (25%)"), this, &ImageViewer::zoomOut);
+    zoomOutAct->setShortcut(QKeySequence::ZoomOut);
     zoomOutAct->setEnabled(false);
-    connect(zoomOutAct, SIGNAL(triggered()), this, SLOT(zoomOut()));
 
-    normalSizeAct = new QAction(tr("&Normal Size"), this);
+    normalSizeAct = viewMenu->addAction(tr("&Normal Size"), this, &ImageViewer::normalSize);
     normalSizeAct->setShortcut(tr("Ctrl+S"));
     normalSizeAct->setEnabled(false);
-    connect(normalSizeAct, SIGNAL(triggered()), this, SLOT(normalSize()));
 
-    fitToWindowAct = new QAction(tr("&Fit to Window"), this);
+    viewMenu->addSeparator();
+
+    fitToWindowAct = viewMenu->addAction(tr("&Fit to Window"), this, &ImageViewer::fitToWindow);
     fitToWindowAct->setEnabled(false);
     fitToWindowAct->setCheckable(true);
     fitToWindowAct->setShortcut(tr("Ctrl+F"));
-    connect(fitToWindowAct, SIGNAL(triggered()), this, SLOT(fitToWindow()));
 
-    aboutAct = new QAction(tr("&About"), this);
-    connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
+    QMenu *helpMenu = menuBar()->addMenu(tr("&Help"));
 
-    aboutQtAct = new QAction(tr("About &Qt"), this);
-    connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
+    helpMenu->addAction(tr("&About"), this, &ImageViewer::about);
+    helpMenu->addAction(tr("About &Qt"), &QApplication::aboutQt);
 }
 //! [18]
 
-//! [19]
-void ImageViewer::createMenus()
-//! [19] //! [20]
-{
-    fileMenu = new QMenu(tr("&File"), this);
-    fileMenu->addAction(openAct);
-    fileMenu->addAction(printAct);
-    fileMenu->addSeparator();
-    fileMenu->addAction(exitAct);
-
-    viewMenu = new QMenu(tr("&View"), this);
-    viewMenu->addAction(zoomInAct);
-    viewMenu->addAction(zoomOutAct);
-    viewMenu->addAction(normalSizeAct);
-    viewMenu->addSeparator();
-    viewMenu->addAction(fitToWindowAct);
-
-    helpMenu = new QMenu(tr("&Help"), this);
-    helpMenu->addAction(aboutAct);
-    helpMenu->addAction(aboutQtAct);
-
-    menuBar()->addMenu(fileMenu);
-    menuBar()->addMenu(viewMenu);
-    menuBar()->addMenu(helpMenu);
-}
-//! [20]
-
 //! [21]
 void ImageViewer::updateActions()
 //! [21] //! [22]
index 7b35e3ee9e38145adec4bcd33c274da1c19b6648..6443428baf6612565c779557d3d0f54b94f667aa 100644 (file)
@@ -87,19 +87,11 @@ private:
     QPrinter printer;
 #endif
 
-    QAction *openAct;
     QAction *printAct;
-    QAction *exitAct;
     QAction *zoomInAct;
     QAction *zoomOutAct;
     QAction *normalSizeAct;
     QAction *fitToWindowAct;
-    QAction *aboutAct;
-    QAction *aboutQtAct;
-
-    QMenu *fileMenu;
-    QMenu *viewMenu;
-    QMenu *helpMenu;
 };
 //! [0]