simplenotepad.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include "simplenotepad.h"
  2. #include "ui_simplenotepad.h"
  3. #include <QMessageBox>
  4. simplenotepad::simplenotepad(QWidget *parent)
  5. : QMainWindow(parent)
  6. , ui(new Ui::simplenotepad)
  7. {
  8. ui->setupUi(this);
  9. }
  10. simplenotepad::~simplenotepad()
  11. {
  12. delete ui;
  13. }
  14. void simplenotepad::on_actionSave_as_triggered()
  15. {
  16. QString file_name = QFileDialog::getSaveFileName(this, "Save the file");
  17. QFile file(file_name);
  18. if (!file.open(QIODevice::ReadWrite))
  19. {
  20. QMessageBox::critical(this, "Error", "File not save");
  21. return;
  22. }
  23. QTextStream in(&file);
  24. in << ui->textEdit->toPlainText();
  25. file.close();
  26. ui->statusbar->showMessage(file_name + " is save");
  27. }
  28. void simplenotepad::on_actionOpen_triggered()
  29. {
  30. QString file_name = QFileDialog::getOpenFileName(this, "Open the file");
  31. QFile file(file_name);
  32. if (!file.open(QIODevice::ReadWrite))
  33. {
  34. QMessageBox::critical(this, "Error", "File not open");
  35. return;
  36. }
  37. QTextStream out(&file);
  38. ui->textEdit->setText(out.readAll());
  39. file.flush();
  40. file.close();
  41. ui->statusbar->showMessage(file_name + " is open");
  42. }
  43. void simplenotepad::on_actionCopy_triggered()
  44. {
  45. ui->textEdit->copy();
  46. }
  47. void simplenotepad::on_actionPaste_triggered()
  48. {
  49. ui->textEdit->paste();
  50. }
  51. void simplenotepad::on_actionGo_to_begin_triggered()
  52. {
  53. ui->textEdit->moveCursor(QTextCursor::Start);
  54. }
  55. void simplenotepad::on_actionGo_to_end_triggered()
  56. {
  57. ui->textEdit->moveCursor(QTextCursor::End);
  58. }
  59. void simplenotepad::on_actionNew_triggered()
  60. {
  61. if(ui->textEdit->toPlainText() == "")
  62. return;
  63. else
  64. {
  65. QMessageBox::StandardButton save = QMessageBox::question(this, "File not saved", "Do you want save file?");
  66. if(save == QMessageBox::Yes)
  67. on_actionSave_as_triggered();
  68. ui->textEdit->setText("");
  69. }
  70. }