From 463628ef553ef0fe8ab582030c9643353a30e820 Mon Sep 17 00:00:00 2001 From: Piyush Raj Date: Tue, 13 Aug 2019 11:23:43 +0530 Subject: [PATCH] init --- Notepad.pro | 40 ++++++++ main.cpp | 11 ++ mainwindow.cpp | 138 +++++++++++++++++++++++++ mainwindow.h | 53 ++++++++++ mainwindow.ui | 265 +++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 507 insertions(+) create mode 100644 Notepad.pro create mode 100644 main.cpp create mode 100644 mainwindow.cpp create mode 100644 mainwindow.h create mode 100644 mainwindow.ui diff --git a/Notepad.pro b/Notepad.pro new file mode 100644 index 0000000..918009b --- /dev/null +++ b/Notepad.pro @@ -0,0 +1,40 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2019-08-10T22:10:36 +# +#------------------------------------------------- + +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets printsupport + +TARGET = Notepad +TEMPLATE = app + +# The following define makes your compiler emit warnings if you use +# any feature of Qt which has been marked as deprecated (the exact warnings +# depend on your compiler). Please consult the documentation of the +# deprecated API in order to know how to port your code away from it. +DEFINES += QT_DEPRECATED_WARNINGS + +# You can also make your code fail to compile if you use deprecated APIs. +# In order to do so, uncomment the following line. +# You can also select to disable deprecated APIs only up to a certain version of Qt. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +CONFIG += c++11 + +SOURCES += \ + main.cpp \ + mainwindow.cpp + +HEADERS += \ + mainwindow.h + +FORMS += \ + mainwindow.ui + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..b48f94e --- /dev/null +++ b/main.cpp @@ -0,0 +1,11 @@ +#include "mainwindow.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + + return a.exec(); +} diff --git a/mainwindow.cpp b/mainwindow.cpp new file mode 100644 index 0000000..01a9d44 --- /dev/null +++ b/mainwindow.cpp @@ -0,0 +1,138 @@ +#include "mainwindow.h" +#include "ui_mainwindow.h" + +MainWindow::MainWindow(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::MainWindow) +{ + ui->setupUi(this); + + #if !QT_CONFIG(printer) + ui->actionPrint->setEnabled(false); + #endif + + #if !QT_CONFIG(clipboard) + ui->actionCut->setEnabled(false); + ui->actionCopy->setEnabled(false); + ui->actionPaste->setEnabled(false); + #endif +} + +MainWindow::~MainWindow() +{ + delete ui; +} + +void MainWindow::on_actionNew_triggered() +{ + currentFile.clear(); + ui->textEdit->setText(QString()); +} + +void MainWindow::on_actionOpen_triggered() +{ + QString filename = QFileDialog::getOpenFileName(this, "Open File"); + if(filename.isEmpty()) return; + QFile file(filename); + currentFile = filename; + + if(!file.open(QIODevice::ReadOnly)){ + ui->statusBar->showMessage("Can't open file: "+file.errorString(), 3000); + } + + QTextStream in(&file); + QString text = in.readAll(); + ui->textEdit->setText(text); + file.close(); + this->setWindowTitle(QFileInfo(filename).fileName()+" - Notepad"); +} + +bool MainWindow::on_actionSave_triggered() +{ + QFile file(currentFile); + if(!file.exists()) + { + this->setWindowTitle("Notepad"); + QString filename = QFileDialog::getSaveFileName(this, "Save"); + if(filename.isEmpty()) return false; + file.setFileName(filename); + currentFile = filename; + this->setWindowTitle(QFileInfo(filename).fileName()+" - Notepad"); + } + + if(!file.open(QIODevice::WriteOnly)){ + ui->statusBar->showMessage("Can't save file: "+file.errorString(), 3000); + return false; + } + + QTextStream out(&file); + QString text = ui->textEdit->toPlainText(); + out << text; + + file.close(); + + return true; +} + +void MainWindow::on_actionSaveAs_triggered() +{ + QString tmp = currentFile; + currentFile.clear(); + + if(!on_actionSave_triggered()) + { + this->setWindowTitle(QFileInfo(tmp).fileName()+" - Notepad"); + currentFile = tmp; + } +} + + +void MainWindow::on_actionPrint_triggered() +{ + #if QT_CONFIG(printer) + QPrinter printDev; + #if QT_CONFIG(printdialog) + QPrintDialog dialog(&printDev, this); + if (dialog.exec() == QDialog::Rejected) + return; + #endif // QT_CONFIG(printdialog) + ui->textEdit->print(&printDev); + #endif // QT_CONFIG(printer) +} + +void MainWindow::on_actionExit_triggered() +{ + qApp->exit(); +} + +void MainWindow::on_actionCopy_triggered() +{ + ui->textEdit->copy(); +} + +void MainWindow::on_actionCut_triggered() +{ + ui->textEdit->cut(); +} + +void MainWindow::on_actionPaste_triggered() +{ + ui->textEdit->paste(); +} + +void MainWindow::on_actionUndo_triggered() +{ + ui->textEdit->undo(); +} + +void MainWindow::on_actionRedo_triggered() +{ + ui->textEdit->redo(); +} + +void MainWindow::on_actionAbout_triggered() +{ + QMessageBox msgBox; + msgBox.setText("Developed by PiyushXCoder"); + msgBox.exec(); +} diff --git a/mainwindow.h b/mainwindow.h new file mode 100644 index 0000000..45e5fc0 --- /dev/null +++ b/mainwindow.h @@ -0,0 +1,53 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include +#include +#include +#include +#include +#include + +namespace Ui { +class MainWindow; +} + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + explicit MainWindow(QWidget *parent = nullptr); + ~MainWindow(); + +private slots: + void on_actionNew_triggered(); + + void on_actionOpen_triggered(); + + bool on_actionSave_triggered(); + + void on_actionSaveAs_triggered(); + + void on_actionExit_triggered(); + + void on_actionCopy_triggered(); + + void on_actionCut_triggered(); + + void on_actionPaste_triggered(); + + void on_actionUndo_triggered(); + + void on_actionRedo_triggered(); + + void on_actionAbout_triggered(); + + void on_actionPrint_triggered(); + +private: + Ui::MainWindow *ui; + QString currentFile; +}; + +#endif // MAINWINDOW_H diff --git a/mainwindow.ui b/mainwindow.ui new file mode 100644 index 0000000..8542dce --- /dev/null +++ b/mainwindow.ui @@ -0,0 +1,265 @@ + + + MainWindow + + + + 0 + 0 + 600 + 500 + + + + Notepad + + + + .. + + + + + + + + + + + + 0 + 0 + 600 + 23 + + + + + File + + + + + + + + + + + + + Edit + + + + + + + + + + + Help + + + + + + + + + + TopToolBarArea + + + false + + + + + + + + + + + + + + + + + .. + + + New + + + New Document + + + Ctrl+N + + + + + + .. + + + Open + + + Open Document + + + Ctrl+O + + + + + + .. + + + Save + + + Save Document + + + Ctrl+S + + + + + + .. + + + SaveAs + + + Save Document As + + + Ctrl+Shift+S + + + + + + .. + + + Exit + + + Exit + + + Ctrl+Q + + + + + + .. + + + Copy + + + Copy Text + + + Ctrl+C + + + + + + .. + + + Cut + + + Cut Text + + + Ctrl+X + + + + + + .. + + + Paste + + + Paste Text + + + Ctrl+V + + + + + + .. + + + Undo + + + Undo Changes + + + Ctrl+Z + + + + + + .. + + + Redo + + + Redo Changes + + + Ctrl+Shift+Z + + + + + + + + About + + + + + + + + Print + + + Print Document + + + Ctrl+P + + + + + + +