support deleting notebook

Signed-off-by: Le Tan <tamlokveer@gmail.com>
This commit is contained in:
Le Tan 2016-10-17 21:17:52 +08:00
parent 5506cec07b
commit f31272966e
4 changed files with 49 additions and 0 deletions

View File

@ -86,6 +86,8 @@ void VMainWindow::setupUI()
tabs, &VTabWidget::openFile);
connect(newNotebookBtn, &QPushButton::clicked,
this, &VMainWindow::onNewNotebookBtnClicked);
connect(deleteNotebookBtn, &QPushButton::clicked,
this, &VMainWindow::onDeleteNotebookBtnClicked);
connect(vnote, &VNote::notebooksChanged,
this, &VMainWindow::updateNotebookComboBox);
@ -194,3 +196,19 @@ bool VMainWindow::isConflictWithExistingNotebooks(const QString &name, const QSt
}
return false;
}
void VMainWindow::onDeleteNotebookBtnClicked()
{
int curIndex = notebookComboBox->currentIndex();
QString curName = vnote->getNotebooks()[curIndex].getName();
QString curPath = vnote->getNotebooks()[curIndex].getPath();
QMessageBox msgBox(QMessageBox::Warning, tr("Warning"), QString("Are you sure you want to delete notebook \"%1\"?")
.arg(curName));
msgBox.setInformativeText(QString("This will delete any files in this notebook (\"%1\").").arg(curPath));
msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Ok);
if (msgBox.exec() == QMessageBox::Ok) {
vnote->removeNotebook(curName);
}
}

View File

@ -30,6 +30,7 @@ private slots:
void setCurNotebookIndex(int index);
// Create a notebook
void onNewNotebookBtnClicked();
void onDeleteNotebookBtnClicked();
void updateNotebookComboBox(const QVector<VNotebook> &notebooks);
signals:

View File

@ -2,6 +2,7 @@
#include <QDebug>
#include <QJsonObject>
#include <QJsonArray>
#include <QDir>
#include "vnote.h"
#include "utils/vutils.h"
#include "vconfigmanager.h"
@ -50,3 +51,31 @@ void VNote::createNotebook(const QString &name, const QString &path)
emit notebooksChanged(notebooks);
}
void VNote::removeNotebook(const QString &name)
{
// Update notebooks settings
QString path;
int index;
for (index = 0; index < notebooks.size(); ++index) {
if (notebooks[index].getName() == name) {
path = notebooks[index].getPath();
break;
}
}
if (index == notebooks.size()) {
return;
}
notebooks.remove(index);
vconfig.setNotebooks(notebooks);
vconfig.setCurNotebookIndex(notebooks.isEmpty() ? -1 : 0);
// Delete the directory
QDir dir(path);
if (!dir.removeRecursively()) {
qWarning() << "error: fail to delete" << path << "recursively";
} else {
qDebug() << "delete" << path << "recursively";
}
emit notebooksChanged(notebooks);
}

View File

@ -21,6 +21,7 @@ public:
static QString templateHtml;
void createNotebook(const QString &name, const QString &path);
void removeNotebook(const QString &name);
signals:
void notebooksChanged(const QVector<VNotebook> &notebooks);