support not deleting files from disk when deleting notebook

This commit is contained in:
Le Tan 2017-03-19 11:33:21 +08:00
parent 29b27b1ecd
commit 1b52cd3362
9 changed files with 203 additions and 44 deletions

View File

@ -0,0 +1,106 @@
#include <QtWidgets>
#include "vdeletenotebookdialog.h"
VDeleteNotebookDialog::VDeleteNotebookDialog(const QString &p_title, const QString &p_name,
const QString &p_path, QWidget *p_parent)
: QDialog(p_parent), m_path(p_path)
{
setupUI(p_title, p_name);
}
void VDeleteNotebookDialog::setupUI(const QString &p_title, const QString &p_name)
{
QLabel *infoLabel = new QLabel(tr("Are you sure to delete notebook: %1 ?").arg(p_name));
m_warningLabel = new QLabel();
m_notDeleteCheck = new QCheckBox(tr("Do not delete files from disk."), this);
m_notDeleteCheck->setChecked(false);
m_notDeleteCheck->setToolTip(tr("When checked, VNote just removes the notebook instead of deleting files from disk"));
connect(m_notDeleteCheck, &QCheckBox::stateChanged, this, &VDeleteNotebookDialog::notDeleteCheckChanged);
notDeleteCheckChanged(false);
// Ok is the default button.
m_btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
connect(m_btnBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
connect(m_btnBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
// Standard Warning icon.
QLabel *iconLabel = new QLabel();
QPixmap pixmap = standardIcon(QMessageBox::Warning);
if (pixmap.isNull()) {
iconLabel->hide();
} else {
iconLabel->setPixmap(pixmap);
}
QVBoxLayout *iconLayout = new QVBoxLayout();
iconLayout->addStretch();
iconLayout->addWidget(iconLabel);
iconLayout->addStretch();
QVBoxLayout *infoLayout = new QVBoxLayout();
infoLayout->addWidget(infoLabel);
infoLayout->addWidget(m_warningLabel);
infoLayout->addWidget(m_notDeleteCheck);
QHBoxLayout *topLayout = new QHBoxLayout();
topLayout->addLayout(iconLayout);
topLayout->addLayout(infoLayout);
QVBoxLayout *mainLayout = new QVBoxLayout();
mainLayout->addLayout(topLayout);
mainLayout->addWidget(m_btnBox);
setLayout(mainLayout);
mainLayout->setSizeConstraint(QLayout::SetFixedSize);
setWindowTitle(p_title);
}
bool VDeleteNotebookDialog::getDeleteFiles() const
{
return !m_notDeleteCheck->isChecked();
}
QPixmap VDeleteNotebookDialog::standardIcon(QMessageBox::Icon p_icon)
{
QStyle *style = this->style();
int iconSize = style->pixelMetric(QStyle::PM_MessageBoxIconSize, 0, this);
QIcon tmpIcon;
switch (p_icon) {
case QMessageBox::Information:
tmpIcon = style->standardIcon(QStyle::SP_MessageBoxInformation, 0, this);
break;
case QMessageBox::Warning:
tmpIcon = style->standardIcon(QStyle::SP_MessageBoxWarning, 0, this);
break;
case QMessageBox::Critical:
tmpIcon = style->standardIcon(QStyle::SP_MessageBoxCritical, 0, this);
break;
case QMessageBox::Question:
tmpIcon = style->standardIcon(QStyle::SP_MessageBoxQuestion, 0, this);
break;
default:
break;
}
if (!tmpIcon.isNull()) {
QWindow *window = this->windowHandle();
if (!window) {
if (const QWidget *nativeParent = this->nativeParentWidget()) {
window = nativeParent->windowHandle();
}
}
return tmpIcon.pixmap(window, QSize(iconSize, iconSize));
}
return QPixmap();
}
void VDeleteNotebookDialog::notDeleteCheckChanged(int p_state)
{
if (p_state) {
m_warningLabel->setText(tr("VNote won't delete files under this directory: %1 .").arg(m_path));
} else {
m_warningLabel->setText(tr("This will delete any files under this directory: %1 !").arg(m_path));
}
}

View File

@ -0,0 +1,34 @@
#ifndef VDELETENOTEBOOKDIALOG_H
#define VDELETENOTEBOOKDIALOG_H
#include <QDialog>
#include <QMessageBox>
class QLabel;
class QLineEdit;
class QString;
class QCheckBox;
class QDialogButtonBox;
class VDeleteNotebookDialog : public QDialog
{
Q_OBJECT
public:
VDeleteNotebookDialog(const QString &p_title, const QString &p_name, const QString &p_path,
QWidget *p_parent = 0);
bool getDeleteFiles() const;
private slots:
void notDeleteCheckChanged(int p_state);
private:
void setupUI(const QString &p_title, const QString &p_name);
QPixmap standardIcon(QMessageBox::Icon p_icon);
QString m_path;
QLabel *m_warningLabel;
QCheckBox *m_notDeleteCheck;
QDialogButtonBox *m_btnBox;
};
#endif // VDELETENOTEBOOKDIALOG_H

View File

@ -53,7 +53,8 @@ SOURCES += main.cpp\
vavatar.cpp \ vavatar.cpp \
vmdedit.cpp \ vmdedit.cpp \
dialog/vfindreplacedialog.cpp \ dialog/vfindreplacedialog.cpp \
dialog/vsettingsdialog.cpp dialog/vsettingsdialog.cpp \
dialog/vdeletenotebookdialog.cpp
HEADERS += vmainwindow.h \ HEADERS += vmainwindow.h \
vdirectorytree.h \ vdirectorytree.h \
@ -92,7 +93,8 @@ HEADERS += vmainwindow.h \
vavatar.h \ vavatar.h \
vmdedit.h \ vmdedit.h \
dialog/vfindreplacedialog.h \ dialog/vfindreplacedialog.h \
dialog/vsettingsdialog.h dialog/vsettingsdialog.h \
dialog/vdeletenotebookdialog.h
RESOURCES += \ RESOURCES += \
vnote.qrc \ vnote.qrc \

Binary file not shown.

View File

@ -1,6 +1,34 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS> <!DOCTYPE TS>
<TS version="2.1" language="zh_CN"> <TS version="2.1" language="zh_CN">
<context>
<name>VDeleteNotebookDialog</name>
<message>
<location filename="../dialog/vdeletenotebookdialog.cpp" line="13"/>
<source>Are you sure to delete notebook: %1 ?</source>
<translation>: %1 ?</translation>
</message>
<message>
<location filename="../dialog/vdeletenotebookdialog.cpp" line="16"/>
<source>Do not delete files from disk.</source>
<translation></translation>
</message>
<message>
<location filename="../dialog/vdeletenotebookdialog.cpp" line="18"/>
<source>When checked, VNote just removes the notebook instead of deleting files from disk</source>
<translation>VNote只会移除该笔记本</translation>
</message>
<message>
<location filename="../dialog/vdeletenotebookdialog.cpp" line="102"/>
<source>VNote won&apos;t delete files under this directory: %1 .</source>
<translation>VNote不会删除该目录下的文件: %1 </translation>
</message>
<message>
<location filename="../dialog/vdeletenotebookdialog.cpp" line="104"/>
<source>This will delete any files under this directory: %1 !</source>
<translation>: %1 </translation>
</message>
</context>
<context> <context>
<name>VDirInfoDialog</name> <name>VDirInfoDialog</name>
<message> <message>
@ -1065,58 +1093,48 @@ Visit https://github.com/tamlok/vnote.git for more information.</source>
<context> <context>
<name>VNotebookSelector</name> <name>VNotebookSelector</name>
<message> <message>
<location filename="../vnotebookselector.cpp" line="48"/> <location filename="../vnotebookselector.cpp" line="49"/>
<source>&amp;Delete</source> <source>&amp;Delete</source>
<translation> (&amp;D)</translation> <translation> (&amp;D)</translation>
</message> </message>
<message> <message>
<location filename="../vnotebookselector.cpp" line="49"/> <location filename="../vnotebookselector.cpp" line="50"/>
<source>Delete current notebook</source> <source>Delete current notebook</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../vnotebookselector.cpp" line="54"/> <location filename="../vnotebookselector.cpp" line="55"/>
<source>&amp;Info</source> <source>&amp;Info</source>
<translation> (&amp;I)</translation> <translation> (&amp;I)</translation>
</message> </message>
<message> <message>
<location filename="../vnotebookselector.cpp" line="55"/> <location filename="../vnotebookselector.cpp" line="56"/>
<source>View and edit current notebook&apos;s information</source> <source>View and edit current notebook&apos;s information</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../vnotebookselector.cpp" line="103"/> <location filename="../vnotebookselector.cpp" line="104"/>
<source>Create or import a notebook</source> <source>Create or import a notebook</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../vnotebookselector.cpp" line="99"/> <location filename="../vnotebookselector.cpp" line="214"/>
<location filename="../vnotebookselector.cpp" line="161"/> <source>Delete Notebook</source>
<translation></translation>
</message>
<message>
<location filename="../vnotebookselector.cpp" line="100"/>
<location filename="../vnotebookselector.cpp" line="162"/>
<source>Add Notebook</source> <source>Add Notebook</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../vnotebookselector.cpp" line="167"/> <location filename="../vnotebookselector.cpp" line="168"/>
<source>Name already exists. Please choose another name.</source> <source>Name already exists. Please choose another name.</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../vnotebookselector.cpp" line="215"/> <location filename="../vnotebookselector.cpp" line="261"/>
<source>Warning</source>
<translation></translation>
</message>
<message>
<location filename="../vnotebookselector.cpp" line="216"/>
<source>Are you sure to delete notebook %1?</source>
<translation>: %1</translation>
</message>
<message>
<location filename="../vnotebookselector.cpp" line="217"/>
<source>This will delete any files in this notebook (%1).</source>
<translation> (%1)</translation>
</message>
<message>
<location filename="../vnotebookselector.cpp" line="264"/>
<source>Notebook Information</source> <source>Notebook Information</source>
<translation></translation> <translation></translation>
</message> </message>

View File

@ -60,7 +60,7 @@ VNotebook *VNotebook::createNotebook(const QString &p_name, const QString &p_pat
return nb; return nb;
} }
void VNotebook::deleteNotebook(VNotebook *p_notebook) void VNotebook::deleteNotebook(VNotebook *p_notebook, bool p_deleteFiles)
{ {
if (!p_notebook) { if (!p_notebook) {
return; return;
@ -70,10 +70,12 @@ void VNotebook::deleteNotebook(VNotebook *p_notebook)
p_notebook->close(); p_notebook->close();
delete p_notebook; delete p_notebook;
if (p_deleteFiles) {
QDir dir(path); QDir dir(path);
if (!dir.removeRecursively()) { if (!dir.removeRecursively()) {
qWarning() << "fail to delete" << path; qWarning() << "fail to delete" << path;
} }
}
} }
void VNotebook::rename(const QString &p_name) void VNotebook::rename(const QString &p_name)

View File

@ -28,7 +28,7 @@ public:
static VNotebook *createNotebook(const QString &p_name, const QString &p_path, bool p_import, static VNotebook *createNotebook(const QString &p_name, const QString &p_path, bool p_import,
QObject *p_parent = 0); QObject *p_parent = 0);
static void deleteNotebook(VNotebook *p_notebook); static void deleteNotebook(VNotebook *p_notebook, bool p_deleteFiles);
signals: signals:
void contentChanged(); void contentChanged();

View File

@ -8,6 +8,7 @@
#include "vconfigmanager.h" #include "vconfigmanager.h"
#include "dialog/vnewnotebookdialog.h" #include "dialog/vnewnotebookdialog.h"
#include "dialog/vnotebookinfodialog.h" #include "dialog/vnotebookinfodialog.h"
#include "dialog/vdeletenotebookdialog.h"
#include "vnotebook.h" #include "vnotebook.h"
#include "vdirectory.h" #include "vdirectory.h"
#include "utils/vutils.h" #include "utils/vutils.h"
@ -209,20 +210,16 @@ void VNotebookSelector::deleteNotebook()
VNotebook *notebook = getNotebookFromComboIndex(index); VNotebook *notebook = getNotebookFromComboIndex(index);
Q_ASSERT(notebook); Q_ASSERT(notebook);
QString curName = notebook->getName();
QString curPath = notebook->getPath();
int ret = VUtils::showMessage(QMessageBox::Warning, tr("Warning"), VDeleteNotebookDialog dialog(tr("Delete Notebook"), notebook->getName(), notebook->getPath(), this);
tr("Are you sure to delete notebook %1?").arg(curName), if (dialog.exec() == QDialog::Accepted) {
tr("This will delete any files in this notebook (%1).").arg(curPath), bool deleteFiles = dialog.getDeleteFiles();
QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Ok, this);
if (ret == QMessageBox::Ok) {
m_editArea->closeFile(notebook, true); m_editArea->closeFile(notebook, true);
deleteNotebook(notebook); deleteNotebook(notebook, deleteFiles);
} }
} }
void VNotebookSelector::deleteNotebook(VNotebook *p_notebook) void VNotebookSelector::deleteNotebook(VNotebook *p_notebook, bool p_deleteFiles)
{ {
int idx = indexOfNotebook(p_notebook); int idx = indexOfNotebook(p_notebook);
@ -231,7 +228,7 @@ void VNotebookSelector::deleteNotebook(VNotebook *p_notebook)
removeNotebookItem(idx); removeNotebookItem(idx);
VNotebook::deleteNotebook(p_notebook); VNotebook::deleteNotebook(p_notebook, p_deleteFiles);
} }
int VNotebookSelector::indexOfNotebook(const VNotebook *p_notebook) int VNotebookSelector::indexOfNotebook(const VNotebook *p_notebook)

View File

@ -30,8 +30,6 @@ signals:
public slots: public slots:
bool newNotebook(); bool newNotebook();
void deleteNotebook();
void editNotebookInfo();
protected: protected:
bool eventFilter(QObject *watched, QEvent *event) Q_DECL_OVERRIDE; bool eventFilter(QObject *watched, QEvent *event) Q_DECL_OVERRIDE;
@ -40,6 +38,8 @@ private slots:
void handleCurIndexChanged(int p_index); void handleCurIndexChanged(int p_index);
void handleItemActivated(int p_index); void handleItemActivated(int p_index);
void requestPopupListContextMenu(QPoint p_pos); void requestPopupListContextMenu(QPoint p_pos);
void deleteNotebook();
void editNotebookInfo();
private: private:
void initActions(); void initActions();
@ -49,7 +49,7 @@ private:
int indexOfNotebook(const VNotebook *p_notebook); int indexOfNotebook(const VNotebook *p_notebook);
// if @p_import is true, we will use the existing config file. // if @p_import is true, we will use the existing config file.
void createNotebook(const QString &p_name, const QString &p_path, bool p_import); void createNotebook(const QString &p_name, const QString &p_path, bool p_import);
void deleteNotebook(VNotebook *p_notebook); void deleteNotebook(VNotebook *p_notebook, bool p_deleteFiles);
void addNotebookItem(const QString &p_name); void addNotebookItem(const QString &p_name);
// @p_index is the index of m_notebooks, NOT of QComboBox. // @p_index is the index of m_notebooks, NOT of QComboBox.
void removeNotebookItem(int p_index); void removeNotebookItem(int p_index);