diff options
Diffstat (limited to 'doc/src/snippets/widgets-tutorial/notepad/notepad.cpp')
-rw-r--r-- | doc/src/snippets/widgets-tutorial/notepad/notepad.cpp | 143 |
1 files changed, 108 insertions, 35 deletions
diff --git a/doc/src/snippets/widgets-tutorial/notepad/notepad.cpp b/doc/src/snippets/widgets-tutorial/notepad/notepad.cpp index c7ef71bcf..f519bb66a 100644 --- a/doc/src/snippets/widgets-tutorial/notepad/notepad.cpp +++ b/doc/src/snippets/widgets-tutorial/notepad/notepad.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2017 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the documentation of the Qt Toolkit. @@ -52,66 +52,139 @@ #include "ui_notepad.h" //! [0] -#include <QFileDialog> #include <QFile> -#include <QMessageBox> +#include <QFileDialog> #include <QTextStream> +#include <QMessageBox> +#include <QprintDev> +#include <QPrintDialog> +#include <QFont> +#include <QFontDialog> //! [0] +//! [1] Notepad::Notepad(QWidget *parent) : QMainWindow(parent), ui(new Ui::Notepad) { ui->setupUi(this); + this->setCentralWidget(ui->textEdit); } +//! [1] Notepad::~Notepad() { delete ui; } -//! [1] -void Notepad::on_quitButton_clicked() +//! [2] +void Notepad::on_actionNew_triggered() { - QCoreApplication::quit(); + currentFile = ""; + ui->textEdit->setText(""); } -//! [1] - //! [2] + +//! [3] void Notepad::on_actionOpen_triggered() { - QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QString(), - tr("Text Files (*.txt);;C++ Files (*.cpp *.h)")); - - if (!fileName.isEmpty()) { - QFile file(fileName); - if (!file.open(QIODevice::ReadOnly)) { - QMessageBox::critical(this, tr("Error"), tr("Could not open file")); - return; - } - QTextStream in(&file); - ui->textEdit->setText(in.readAll()); - file.close(); + QString fileName = QFileDialog::getOpenFileName(this, "Open the file"); + QFile file(fileName); + currentFile = fileName; + if (!file.open(QIODevice::ReadOnly | QFile::Text)) { + QMessageBox::warning(this,"..","File not opened."); + return; } + QTextStream in(&file); + QString text = in.readAll(); + ui->textEdit->setText(text); + file.close(); } -//! [2] - //! [3] + +//! [4] void Notepad::on_actionSave_triggered() { - QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), QString(), - tr("Text Files (*.txt);;C++ Files (*.cpp *.h)")); - - if (!fileName.isEmpty()) { - QFile file(fileName); - if (!file.open(QIODevice::WriteOnly)) { - // error message - } else { - QTextStream stream(&file); - stream << ui->textEdit->toPlainText(); - stream.flush(); - file.close(); + QFile file(currentFile); + if (!file.open(QIODevice::WriteOnly | QFile::Text)) { + QMessageBox::warning(this,"..","No file opened. Use Save As"); + return; + } + QTextStream out(&file); + QString text = ui->textEdit->toPlainText(); + out << text; + file.flush(); + file.close(); +} +//! [4] + +//! [5] +void Notepad::on_actionSave_as_triggered() +{ + QString fileName = QFileDialog::getSaveFileName(this, "Save as"); + QFile file(fileName); + currentFile = fileName; + if (!file.open(QFile::WriteOnly | QFile::Text)) { + QMessageBox::warning(this,"..","File not opened."); + return; + } + QTextStream out(&file); + QString text = ui->textEdit->toPlainText(); + out << text; + file.flush(); + file.close(); +} +//! [5] + +//! [6] +void Notepad::on_actionPrint_triggered() +{ + QprintDev printDev; + QPrintDialog dialog(&printDev, this); + if (dialog.exec() == QDialog::Rejected) { + return; } + ui->textEdit->print(&printDev); +} +//! [6] + +void Notepad::on_actionExit_triggered() +{ + QCoreApplication::quit(); +} + +void Notepad::on_actionCopy_triggered() +{ + ui->textEdit->copy(); +} + +void Notepad::on_actionCut_triggered() +{ + ui->textEdit->cut(); +} + +void Notepad::on_actionPaste_triggered() +{ + ui->textEdit->paste(); +} + +void Notepad::on_actionUndo_triggered() +{ + ui->textEdit->undo(); +} + +void Notepad::on_actionRedo_triggered() +{ + ui->textEdit->redo(); +} + +//! [7] +void Notepad::on_actionFont_triggered() +{ + bool fontSelected; + QFont font = QFontDialog::getFont(&fontSelected, this); + if (fontSelected) { + ui->textEdit->setFont(font); } } -//! [3] +//! [7] |