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 8bd95fc1..e57fae39 100644 Binary files a/src/translations/vnote_zh_CN.qm and b/src/translations/vnote_zh_CN.qm differ 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);