From 1b52cd3362ed189d2b9601ee031d6e4e4583c2e8 Mon Sep 17 00:00:00 2001 From: Le Tan Date: Sun, 19 Mar 2017 11:33:21 +0800 Subject: [PATCH] support not deleting files from disk when deleting notebook --- src/dialog/vdeletenotebookdialog.cpp | 106 +++++++++++++++++++++++++++ src/dialog/vdeletenotebookdialog.h | 34 +++++++++ src/src.pro | 6 +- src/translations/vnote_zh_CN.qm | Bin 18457 -> 18919 bytes src/translations/vnote_zh_CN.ts | 66 +++++++++++------ src/vnotebook.cpp | 10 ++- src/vnotebook.h | 2 +- src/vnotebookselector.cpp | 17 ++--- src/vnotebookselector.h | 6 +- 9 files changed, 203 insertions(+), 44 deletions(-) create mode 100644 src/dialog/vdeletenotebookdialog.cpp create mode 100644 src/dialog/vdeletenotebookdialog.h diff --git a/src/dialog/vdeletenotebookdialog.cpp b/src/dialog/vdeletenotebookdialog.cpp new file mode 100644 index 00000000..483d0100 --- /dev/null +++ b/src/dialog/vdeletenotebookdialog.cpp @@ -0,0 +1,106 @@ +#include +#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)); + } +} diff --git a/src/dialog/vdeletenotebookdialog.h b/src/dialog/vdeletenotebookdialog.h new file mode 100644 index 00000000..6c1706d1 --- /dev/null +++ b/src/dialog/vdeletenotebookdialog.h @@ -0,0 +1,34 @@ +#ifndef VDELETENOTEBOOKDIALOG_H +#define VDELETENOTEBOOKDIALOG_H + +#include +#include + +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 diff --git a/src/src.pro b/src/src.pro index d57d8840..3d870461 100644 --- a/src/src.pro +++ b/src/src.pro @@ -53,7 +53,8 @@ SOURCES += main.cpp\ vavatar.cpp \ vmdedit.cpp \ dialog/vfindreplacedialog.cpp \ - dialog/vsettingsdialog.cpp + dialog/vsettingsdialog.cpp \ + dialog/vdeletenotebookdialog.cpp HEADERS += vmainwindow.h \ vdirectorytree.h \ @@ -92,7 +93,8 @@ HEADERS += vmainwindow.h \ vavatar.h \ vmdedit.h \ dialog/vfindreplacedialog.h \ - dialog/vsettingsdialog.h + dialog/vsettingsdialog.h \ + dialog/vdeletenotebookdialog.h RESOURCES += \ vnote.qrc \ diff --git a/src/translations/vnote_zh_CN.qm b/src/translations/vnote_zh_CN.qm index 8bd95fc11f7f9cca54326ca91b2d34d535d704d3..e57fae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1)|n?SZ)Jmp=9zu#xZT^#wLNP{ z>^<|2&NqOtPV=6UTXaFTx&1Juwa+zouU1*`wz)67kc~bwUm0LS%Mv*@m?KInk{>Q} z0dY&@xrex0qdoGXO+j?Q9@*PKrL&*cF(=$7FZRshOw#04P2aO|ul(XwN*TUaUSFBb zz#JXNb<69|nYlETa@{s66j?97Rl}JMcgtLq1m zTO0V%QTj<~ys(xxyQRGIHX9|mmCI({Se$0Do)@h+F-6CJZ!EPFjH5D;P6dyovrC0pK1U_+rn$AGVN z%qg|>Or-+BM$7m0PQX-Y4OuL5G_PAT!*&5NrPl025^T5dY^{M}w zQBD6R&r`dcZ;@e|I&l9{+9p!{U*TLTl3)upvQg;kHuq4Wpslv~>4CK4NLx)C*3ofF$E+$HbK7;C7+~A{F$ubF+M0LzC|&L>+sV!x+URZDZ!>ui z^R4~9-Fvz9EA7vYIn6iYS$kc>FfM1fy=7koUE;DI_$UO(O0}Ojc!kP@*-v|^(6CkZ zz8<>5Ww&1opbewlnz{WV(%PVS|rbpHnuMDm|P?~^jM8buXJ0s+wZ8q^6?JtOkkQaEp3nN`A zx&Eq2i(B236(J*`w!ipn;oQRTq3qgzJ-@)cta?FSsZvr?Fu+PNL$wf_%)gmGtEKpz u!4bpDDxm%n;aNNeL1kMKL>Gy|Xj diff --git a/src/translations/vnote_zh_CN.ts b/src/translations/vnote_zh_CN.ts index 3e10878b..9c0b34af 100644 --- a/src/translations/vnote_zh_CN.ts +++ b/src/translations/vnote_zh_CN.ts @@ -1,6 +1,34 @@ + + VDeleteNotebookDialog + + + Are you sure to delete notebook: %1 ? + 确认删除笔记本: %1 ? + + + + Do not delete files from disk. + 不要从磁盘中删除文件。 + + + + When checked, VNote just removes the notebook instead of deleting files from disk + 启用时,VNote只会移除该笔记本,不会从磁盘中删除文件 + + + + VNote won't delete files under this directory: %1 . + VNote不会删除该目录下的文件: %1 。 + + + + This will delete any files under this directory: %1 ! + 该操作会删除该目录下的所有文件: %1 ! + + VDirInfoDialog @@ -1065,58 +1093,48 @@ Visit https://github.com/tamlok/vnote.git for more information. VNotebookSelector - + &Delete 删除 (&D) - + Delete current notebook 删除当前笔记本 - + &Info 信息 (&I) - + View and edit current notebook's information 查看并编辑当前笔记本的信息 - + Create or import a notebook 新建或导入一个笔记本 - - + + Delete Notebook + 删除笔记本 + + + + Add Notebook 添加笔记本 - + Name already exists. Please choose another name. 该笔记本名已存在。请选择另一个名字。 - - Warning - 警告 - - - - Are you sure to delete notebook %1? - 确认删除笔记本: %1? - - - - This will delete any files in this notebook (%1). - 该操作会删除该笔记本中的所有文件 (%1)。 - - - + Notebook Information 笔记本信息 diff --git a/src/vnotebook.cpp b/src/vnotebook.cpp index 623ffd5b..5aa1a53e 100644 --- a/src/vnotebook.cpp +++ b/src/vnotebook.cpp @@ -60,7 +60,7 @@ VNotebook *VNotebook::createNotebook(const QString &p_name, const QString &p_pat return nb; } -void VNotebook::deleteNotebook(VNotebook *p_notebook) +void VNotebook::deleteNotebook(VNotebook *p_notebook, bool p_deleteFiles) { if (!p_notebook) { return; @@ -70,9 +70,11 @@ void VNotebook::deleteNotebook(VNotebook *p_notebook) p_notebook->close(); delete p_notebook; - QDir dir(path); - if (!dir.removeRecursively()) { - qWarning() << "fail to delete" << path; + if (p_deleteFiles) { + QDir dir(path); + if (!dir.removeRecursively()) { + qWarning() << "fail to delete" << path; + } } } diff --git a/src/vnotebook.h b/src/vnotebook.h index a85b6de7..96216f6a 100644 --- a/src/vnotebook.h +++ b/src/vnotebook.h @@ -28,7 +28,7 @@ public: static VNotebook *createNotebook(const QString &p_name, const QString &p_path, bool p_import, QObject *p_parent = 0); - static void deleteNotebook(VNotebook *p_notebook); + static void deleteNotebook(VNotebook *p_notebook, bool p_deleteFiles); signals: void contentChanged(); diff --git a/src/vnotebookselector.cpp b/src/vnotebookselector.cpp index a70f2b8b..50dcb93b 100644 --- a/src/vnotebookselector.cpp +++ b/src/vnotebookselector.cpp @@ -8,6 +8,7 @@ #include "vconfigmanager.h" #include "dialog/vnewnotebookdialog.h" #include "dialog/vnotebookinfodialog.h" +#include "dialog/vdeletenotebookdialog.h" #include "vnotebook.h" #include "vdirectory.h" #include "utils/vutils.h" @@ -209,20 +210,16 @@ void VNotebookSelector::deleteNotebook() VNotebook *notebook = getNotebookFromComboIndex(index); Q_ASSERT(notebook); - QString curName = notebook->getName(); - QString curPath = notebook->getPath(); - int ret = VUtils::showMessage(QMessageBox::Warning, tr("Warning"), - tr("Are you sure to delete notebook %1?").arg(curName), - tr("This will delete any files in this notebook (%1).").arg(curPath), - QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Ok, this); - if (ret == QMessageBox::Ok) { + VDeleteNotebookDialog dialog(tr("Delete Notebook"), notebook->getName(), notebook->getPath(), this); + if (dialog.exec() == QDialog::Accepted) { + bool deleteFiles = dialog.getDeleteFiles(); 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); @@ -231,7 +228,7 @@ void VNotebookSelector::deleteNotebook(VNotebook *p_notebook) removeNotebookItem(idx); - VNotebook::deleteNotebook(p_notebook); + VNotebook::deleteNotebook(p_notebook, p_deleteFiles); } int VNotebookSelector::indexOfNotebook(const VNotebook *p_notebook) diff --git a/src/vnotebookselector.h b/src/vnotebookselector.h index 0d06c7af..5d70ea68 100644 --- a/src/vnotebookselector.h +++ b/src/vnotebookselector.h @@ -30,8 +30,6 @@ signals: public slots: bool newNotebook(); - void deleteNotebook(); - void editNotebookInfo(); protected: bool eventFilter(QObject *watched, QEvent *event) Q_DECL_OVERRIDE; @@ -40,6 +38,8 @@ private slots: void handleCurIndexChanged(int p_index); void handleItemActivated(int p_index); void requestPopupListContextMenu(QPoint p_pos); + void deleteNotebook(); + void editNotebookInfo(); private: void initActions(); @@ -49,7 +49,7 @@ private: int indexOfNotebook(const VNotebook *p_notebook); // 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 deleteNotebook(VNotebook *p_notebook); + void deleteNotebook(VNotebook *p_notebook, bool p_deleteFiles); void addNotebookItem(const QString &p_name); // @p_index is the index of m_notebooks, NOT of QComboBox. void removeNotebookItem(int p_index);