diff --git a/WorkPad.pro b/WorkPad.pro index 49b04c7..5412722 100755 --- a/WorkPad.pro +++ b/WorkPad.pro @@ -4,8 +4,8 @@ greaterThan(QT_MAJOR_VERSION, 5): QT += widgets CONFIG += c++17 -win32:VERSION = 2.0.0.0 # major.minor.patch.build -else:VERSION = 2.0.0 # major.minor.patch +win32:VERSION = 2.1.0.0 # major.minor.patch.build +else:VERSION = 2.1.0 # major.minor.patch DEFINES += APP_VERSION=\"\\\"$${VERSION}\\\"\" DEFINES += APP_NAME=\"\\\"WorkPad\\\"\" @@ -69,6 +69,7 @@ linux-* { #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ + src/frames/exportdialog.cpp \ src/frames/renamedialog.cpp \ src/frames/aboutdialog.cpp \ src/frames/createdialog.cpp \ @@ -80,6 +81,7 @@ SOURCES += \ src/services/savemanager.cpp HEADERS += \ + src/frames/exportdialog.h \ src/frames/renamedialog.h \ src/frames/aboutdialog.h \ src/frames/createdialog.h \ @@ -90,6 +92,7 @@ HEADERS += \ src/services/savemanager.h FORMS += \ + src/frames/exportdialog.ui \ src/frames/renamedialog.ui \ src/frames/aboutdialog.ui \ src/frames/createdialog.ui \ diff --git a/src/frames/exportdialog.cpp b/src/frames/exportdialog.cpp new file mode 100644 index 0000000..f2ba33b --- /dev/null +++ b/src/frames/exportdialog.cpp @@ -0,0 +1,23 @@ +#include "exportdialog.h" +#include "ui_exportdialog.h" + +ExportDialog::ExportDialog(QWidget *parent) : + QDialog(parent), + ui(new Ui::ExportDialog) +{ + ui->setupUi(this); +} + +ExportDialog::~ExportDialog() +{ + delete ui; +} + +int ExportDialog::getResult() +{ + if (ui->markdownButton->isChecked()) + { + return MARKDOWN; + } + return PLAIN; +} diff --git a/src/frames/exportdialog.h b/src/frames/exportdialog.h new file mode 100644 index 0000000..2fb2536 --- /dev/null +++ b/src/frames/exportdialog.h @@ -0,0 +1,26 @@ +#ifndef EXPORTDIALOG_H +#define EXPORTDIALOG_H + +#include + +#define MARKDOWN 1 +#define PLAIN 2 + +namespace Ui { +class ExportDialog; +} + +class ExportDialog : public QDialog +{ + Q_OBJECT + +public: + explicit ExportDialog(QWidget *parent = nullptr); + ~ExportDialog(); + int getResult(); + +private: + Ui::ExportDialog *ui; +}; + +#endif // EXPORTDIALOG_H diff --git a/src/frames/exportdialog.ui b/src/frames/exportdialog.ui new file mode 100644 index 0000000..a6e9516 --- /dev/null +++ b/src/frames/exportdialog.ui @@ -0,0 +1,109 @@ + + + ExportDialog + + + + 0 + 0 + 223 + 138 + + + + + 223 + 138 + + + + + 223 + 138 + + + + Export + + + + + 0 + 100 + 211 + 32 + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + 30 + 20 + 171 + 23 + + + + Markdown file + + + true + + + + + + 30 + 60 + 92 + 23 + + + + Text file + + + + + + + buttonBox + accepted() + ExportDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + ExportDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + + diff --git a/src/frames/mainwindow.cpp b/src/frames/mainwindow.cpp index 3929951..8c12d03 100755 --- a/src/frames/mainwindow.cpp +++ b/src/frames/mainwindow.cpp @@ -5,6 +5,7 @@ #include "createdialog.h" #include "movedialog.h" #include "renamedialog.h" +#include "exportdialog.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) @@ -124,7 +125,7 @@ void MainWindow::save() void MainWindow::showAboutBox() { - AboutDialog dialog; + AboutDialog dialog(this); dialog.setModal(true); dialog.exec(); } @@ -146,6 +147,9 @@ void MainWindow::prepareMenu(const QPoint &pos) QAction *moveAction = new QAction(tr("Move to..."), this); connect(moveAction, &QAction::triggered, this, &MainWindow::moveNote); menu.addAction(moveAction); + QAction *exportAction = new QAction(tr("Export"), this); + connect(exportAction, &QAction::triggered, this, &MainWindow::exportNote); + menu.addAction(exportAction); } menu.exec(ui->treeWidget->mapToGlobal(pos)); } @@ -156,37 +160,41 @@ void MainWindow::deleteItem() if (ui->treeWidget->selectedItems().length() == 1) { QTreeWidgetItem *item = ui->treeWidget->selectedItems()[0]; QString uuid = item->text(COLUMN_UUID); - if (item->text(COLUMN_TYPE) == TYPE_NOTE) + QMessageBox::StandardButton res = QMessageBox::question(this, tr("Remove"), QString("Do you want to remove '%1'?").arg(item->text(COLUMN_NAME))); + if (res == QMessageBox::Yes) { - QString uuidFolder = item->parent()->text(COLUMN_UUID); - Note *n = savemng->getNoteByUUID(uuid); - if (n == nullptr) + if (item->text(COLUMN_TYPE) == TYPE_NOTE) { - return; + QString uuidFolder = item->parent()->text(COLUMN_UUID); + Note *n = savemng->getNoteByUUID(uuid); + if (n == nullptr) + { + return; + } + if (!savemng->removeNote(uuidFolder, uuid)) + { + QMessageBox::critical(this, tr("WorkPad"), "Failed to remove this note", QMessageBox::Ok, QMessageBox::Ok); + return; + } + delete n; } - if (!savemng->removeNote(uuidFolder, uuid)) + else { - QMessageBox::critical(this, tr("WorkPad"), "Failed to remove this note", QMessageBox::Ok, QMessageBox::Ok); - return; + Folder *f = savemng->getFolderByUUID(uuid); + if (f == nullptr) + { + return; + } + if (!savemng->removeFolder(uuid)) + { + QMessageBox::critical(this, tr("WorkPad"), "Failed to remove this folder", QMessageBox::Ok, QMessageBox::Ok); + return; + } + delete f; } - delete n; + savemng->flushSave(); + updateListView(); } - else - { - Folder *f = savemng->getFolderByUUID(uuid); - if (f == nullptr) - { - return; - } - if (!savemng->removeFolder(uuid)) - { - QMessageBox::critical(this, tr("WorkPad"), "Failed to remove this folder", QMessageBox::Ok, QMessageBox::Ok); - return; - } - delete f; - } - savemng->flushSave(); - updateListView(); } } @@ -276,6 +284,51 @@ void MainWindow::editName() } } +void MainWindow::exportNote() +{ + if (ui->treeWidget->selectedItems().length() == 1) { + QTreeWidgetItem *item = ui->treeWidget->selectedItems()[0]; + if (item->text(COLUMN_TYPE) == TYPE_NOTE) + { + Note *n = savemng->getNoteByUUID(item->text(COLUMN_UUID)); + if (n == nullptr) + { + QMessageBox::critical(this, tr("WorkPad"), "The note is not found", QMessageBox::Ok, QMessageBox::Ok); + return; + } + ExportDialog d(this); + if (d.exec() == QDialog::Accepted) + { + int fileType = d.getResult(); + QString filter = "Plain text file (*.txt)"; + if (fileType == MARKDOWN) + { + filter = "Markdown file (*.md)"; + } + QString fileName = QFileDialog::getSaveFileName(this, tr("Export note"), "", filter); + if (!fileName.isEmpty()) + { + if (fileType == MARKDOWN && !fileName.endsWith(".md", Qt::CaseInsensitive)) + { + fileName += ".md"; + } + else if (fileType == PLAIN && !fileName.endsWith(".txt", Qt::CaseInsensitive)) + { + fileName += ".txt"; + } + QFile *f = new QFile(fileName); + if (f->open(QIODevice::WriteOnly)) + { + f->write(n->getContent().toUtf8()); + f->close(); + } + delete f; + } + } + } + } +} + void MainWindow::markdownContentChanged() { timer->stop(); @@ -286,8 +339,14 @@ void MainWindow::markdownContentChanged() QString uuid = ui->treeWidget->selectedItems()[0]->text(COLUMN_UUID); Note *n = savemng->getNoteByUUID(uuid); if (n != nullptr) { + QScrollBar *scrollbar = ui->markdownViewer->verticalScrollBar(); + QString content = ui->contentEdit->toPlainText(); + + int pos = scrollbar->sliderPosition(); ui->markdownViewer->setMarkdown(content); + scrollbar->setSliderPosition(pos); + ui->markdownViewer2->setMarkdown(content); ui->plainTextEdit->setPlainText(content); n->setContent(content); diff --git a/src/frames/mainwindow.h b/src/frames/mainwindow.h index 4dd3452..d07b463 100755 --- a/src/frames/mainwindow.h +++ b/src/frames/mainwindow.h @@ -8,6 +8,8 @@ #include #include #include +#include +#include #include "../services/savemanager.h" @@ -42,6 +44,7 @@ private slots: void deleteItem(); void moveNote(); void editName(); + void exportNote(); private: Ui::MainWindow *ui; diff --git a/src/frames/mainwindow.ui b/src/frames/mainwindow.ui index 610eed9..97e908b 100755 --- a/src/frames/mainwindow.ui +++ b/src/frames/mainwindow.ui @@ -56,6 +56,18 @@ + + + + + 14 + + + + + + + @@ -72,30 +84,18 @@ Plain text - + false - - - - - 14 - - - - - - - - Mardown editor + Markdown editor diff --git a/src/frames/movedialog.ui b/src/frames/movedialog.ui index 68e1037..996db41 100644 --- a/src/frames/movedialog.ui +++ b/src/frames/movedialog.ui @@ -23,7 +23,7 @@ - Dialog + Move note diff --git a/src/frames/renamedialog.ui b/src/frames/renamedialog.ui index 1f3c2ab..dbe1600 100644 --- a/src/frames/renamedialog.ui +++ b/src/frames/renamedialog.ui @@ -11,7 +11,7 @@ - Dialog + Rename