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 <Friedemann.Kleint@qt.io>
This commit is contained in:
Shawn Rutledge 2017-09-14 16:13:53 +02:00
parent 5ded4e35db
commit e498a1d09d
4 changed files with 22 additions and 4 deletions

View File

@ -104,15 +104,16 @@ void MainWindow::setEventCompression(bool compress)
} }
//! [5] //! [5]
void MainWindow::save() bool MainWindow::save()
{ {
QString path = QDir::currentPath() + "/untitled.png"; QString path = QDir::currentPath() + "/untitled.png";
QString fileName = QFileDialog::getSaveFileName(this, tr("Save Picture"), QString fileName = QFileDialog::getSaveFileName(this, tr("Save Picture"),
path); path);
bool success = m_canvas->saveImage(fileName);
if (!m_canvas->saveImage(fileName)) if (!success)
QMessageBox::information(this, "Error Saving Picture", QMessageBox::information(this, "Error Saving Picture",
"Could not save the image"); "Could not save the image");
return success;
} }
//! [5] //! [5]
@ -128,6 +129,14 @@ void MainWindow::load()
} }
//! [6] //! [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] //! [7]
void MainWindow::about() void MainWindow::about()
{ {
@ -142,6 +151,7 @@ void MainWindow::createMenus()
QMenu *fileMenu = menuBar()->addMenu(tr("&File")); QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(tr("&Open..."), this, &MainWindow::load, QKeySequence::Open); fileMenu->addAction(tr("&Open..."), this, &MainWindow::load, QKeySequence::Open);
fileMenu->addAction(tr("&Save As..."), this, &MainWindow::save, QKeySequence::SaveAs); 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); fileMenu->addAction(tr("E&xit"), this, &MainWindow::close, QKeySequence::Quit);
QMenu *brushMenu = menuBar()->addMenu(tr("&Brush")); QMenu *brushMenu = menuBar()->addMenu(tr("&Brush"));

View File

@ -72,8 +72,9 @@ private slots:
void setLineWidthValuator(QAction *action); void setLineWidthValuator(QAction *action);
void setSaturationValuator(QAction *action); void setSaturationValuator(QAction *action);
void setEventCompression(bool compress); void setEventCompression(bool compress);
void save(); bool save();
void load(); void load();
void clear();
void about(); void about();
private: private:

View File

@ -90,6 +90,12 @@ bool TabletCanvas::loadImage(const QString &file)
} }
//! [2] //! [2]
void TabletCanvas::clear()
{
m_pixmap.fill(Qt::white);
update();
}
//! [3] //! [3]
void TabletCanvas::tabletEvent(QTabletEvent *event) void TabletCanvas::tabletEvent(QTabletEvent *event)
{ {

View File

@ -79,6 +79,7 @@ public:
bool saveImage(const QString &file); bool saveImage(const QString &file);
bool loadImage(const QString &file); bool loadImage(const QString &file);
void clear();
void setAlphaChannelValuator(Valuator type) void setAlphaChannelValuator(Valuator type)
{ m_alphaChannelValuator = type; } { m_alphaChannelValuator = type; }
void setColorSaturationValuator(Valuator type) void setColorSaturationValuator(Valuator type)