From e498a1d09d79cbb26437a4823b67ae56c5542514 Mon Sep 17 00:00:00 2001 From: Shawn Rutledge Date: Thu, 14 Sep 2017 16:13:53 +0200 Subject: Tablet example: add a menu item to clear the canvas It's annoying to have to kill the app and start over when it gets too cluttered. Change-Id: Ic72e372e5742de4e7c6bb818a3150283498a517b Reviewed-by: Friedemann Kleint --- examples/widgets/widgets/tablet/mainwindow.cpp | 16 +++++++++++++--- examples/widgets/widgets/tablet/mainwindow.h | 3 ++- examples/widgets/widgets/tablet/tabletcanvas.cpp | 6 ++++++ examples/widgets/widgets/tablet/tabletcanvas.h | 1 + 4 files changed, 22 insertions(+), 4 deletions(-) (limited to 'examples/widgets') diff --git a/examples/widgets/widgets/tablet/mainwindow.cpp b/examples/widgets/widgets/tablet/mainwindow.cpp index 994666d4de..a048119533 100644 --- a/examples/widgets/widgets/tablet/mainwindow.cpp +++ b/examples/widgets/widgets/tablet/mainwindow.cpp @@ -104,15 +104,16 @@ void MainWindow::setEventCompression(bool compress) } //! [5] -void MainWindow::save() +bool MainWindow::save() { QString path = QDir::currentPath() + "/untitled.png"; QString fileName = QFileDialog::getSaveFileName(this, tr("Save Picture"), path); - - if (!m_canvas->saveImage(fileName)) + bool success = m_canvas->saveImage(fileName); + if (!success) QMessageBox::information(this, "Error Saving Picture", "Could not save the image"); + return success; } //! [5] @@ -128,6 +129,14 @@ void MainWindow::load() } //! [6] +void MainWindow::clear() +{ + if (QMessageBox::question(this, tr("Save changes"), tr("Do you want to save your changes?"), + QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, + QMessageBox::Save) != QMessageBox::Save || save()) + m_canvas->clear(); +} + //! [7] void MainWindow::about() { @@ -142,6 +151,7 @@ void MainWindow::createMenus() QMenu *fileMenu = menuBar()->addMenu(tr("&File")); fileMenu->addAction(tr("&Open..."), this, &MainWindow::load, QKeySequence::Open); fileMenu->addAction(tr("&Save As..."), this, &MainWindow::save, QKeySequence::SaveAs); + fileMenu->addAction(tr("&New"), this, &MainWindow::clear, QKeySequence::New); fileMenu->addAction(tr("E&xit"), this, &MainWindow::close, QKeySequence::Quit); QMenu *brushMenu = menuBar()->addMenu(tr("&Brush")); diff --git a/examples/widgets/widgets/tablet/mainwindow.h b/examples/widgets/widgets/tablet/mainwindow.h index 4b99324f04..4be28784b5 100644 --- a/examples/widgets/widgets/tablet/mainwindow.h +++ b/examples/widgets/widgets/tablet/mainwindow.h @@ -72,8 +72,9 @@ private slots: void setLineWidthValuator(QAction *action); void setSaturationValuator(QAction *action); void setEventCompression(bool compress); - void save(); + bool save(); void load(); + void clear(); void about(); private: diff --git a/examples/widgets/widgets/tablet/tabletcanvas.cpp b/examples/widgets/widgets/tablet/tabletcanvas.cpp index 73678ab754..3ff26d2ec8 100644 --- a/examples/widgets/widgets/tablet/tabletcanvas.cpp +++ b/examples/widgets/widgets/tablet/tabletcanvas.cpp @@ -90,6 +90,12 @@ bool TabletCanvas::loadImage(const QString &file) } //! [2] +void TabletCanvas::clear() +{ + m_pixmap.fill(Qt::white); + update(); +} + //! [3] void TabletCanvas::tabletEvent(QTabletEvent *event) { diff --git a/examples/widgets/widgets/tablet/tabletcanvas.h b/examples/widgets/widgets/tablet/tabletcanvas.h index 671d5376f8..c63ef76893 100644 --- a/examples/widgets/widgets/tablet/tabletcanvas.h +++ b/examples/widgets/widgets/tablet/tabletcanvas.h @@ -79,6 +79,7 @@ public: bool saveImage(const QString &file); bool loadImage(const QString &file); + void clear(); void setAlphaChannelValuator(Valuator type) { m_alphaChannelValuator = type; } void setColorSaturationValuator(Valuator type) -- cgit v1.2.3