vdeletenotebookdialog.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include <QtWidgets>
  2. #include "vdeletenotebookdialog.h"
  3. #include "vconfigmanager.h"
  4. extern VConfigManager *g_config;
  5. VDeleteNotebookDialog::VDeleteNotebookDialog(const QString &p_title,
  6. const VNotebook *p_notebook,
  7. QWidget *p_parent)
  8. : QDialog(p_parent), m_notebook(p_notebook)
  9. {
  10. setupUI(p_title, m_notebook->getName());
  11. }
  12. void VDeleteNotebookDialog::setupUI(const QString &p_title, const QString &p_name)
  13. {
  14. QLabel *infoLabel = new QLabel(tr("Are you sure to delete notebook <span style=\"%1\">%2</span>?")
  15. .arg(g_config->c_dataTextStyle).arg(p_name));
  16. m_warningLabel = new QLabel();
  17. m_warningLabel->setWordWrap(true);
  18. m_deleteCheck = new QCheckBox(tr("Delete files from disk"), this);
  19. m_deleteCheck->setChecked(false);
  20. m_deleteCheck->setToolTip(tr("When checked, VNote will delete all files (including Recycle Bin) of this notebook from disk"));
  21. connect(m_deleteCheck, &QCheckBox::stateChanged,
  22. this, &VDeleteNotebookDialog::deleteCheckChanged);
  23. deleteCheckChanged(false);
  24. // Ok is the default button.
  25. m_btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
  26. connect(m_btnBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
  27. connect(m_btnBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
  28. QPushButton *okBtn = m_btnBox->button(QDialogButtonBox::Ok);
  29. okBtn->setProperty("DangerBtn", true);
  30. // Standard Warning icon.
  31. QLabel *iconLabel = new QLabel();
  32. QPixmap pixmap = standardIcon(QMessageBox::Warning);
  33. if (pixmap.isNull()) {
  34. iconLabel->hide();
  35. } else {
  36. iconLabel->setPixmap(pixmap);
  37. }
  38. QVBoxLayout *iconLayout = new QVBoxLayout();
  39. iconLayout->addStretch();
  40. iconLayout->addWidget(iconLabel);
  41. iconLayout->addStretch();
  42. QVBoxLayout *infoLayout = new QVBoxLayout();
  43. infoLayout->addWidget(infoLabel);
  44. infoLayout->addWidget(m_deleteCheck);
  45. infoLayout->addWidget(m_warningLabel);
  46. QHBoxLayout *topLayout = new QHBoxLayout();
  47. topLayout->addLayout(iconLayout);
  48. topLayout->addLayout(infoLayout);
  49. QVBoxLayout *mainLayout = new QVBoxLayout();
  50. mainLayout->addLayout(topLayout);
  51. mainLayout->addWidget(m_btnBox);
  52. setLayout(mainLayout);
  53. mainLayout->setSizeConstraint(QLayout::SetFixedSize);
  54. setWindowTitle(p_title);
  55. }
  56. bool VDeleteNotebookDialog::getDeleteFiles() const
  57. {
  58. return m_deleteCheck->isChecked();
  59. }
  60. QPixmap VDeleteNotebookDialog::standardIcon(QMessageBox::Icon p_icon)
  61. {
  62. QStyle *style = this->style();
  63. int iconSize = style->pixelMetric(QStyle::PM_MessageBoxIconSize, 0, this);
  64. QIcon tmpIcon;
  65. switch (p_icon) {
  66. case QMessageBox::Information:
  67. tmpIcon = style->standardIcon(QStyle::SP_MessageBoxInformation, 0, this);
  68. break;
  69. case QMessageBox::Warning:
  70. tmpIcon = style->standardIcon(QStyle::SP_MessageBoxWarning, 0, this);
  71. break;
  72. case QMessageBox::Critical:
  73. tmpIcon = style->standardIcon(QStyle::SP_MessageBoxCritical, 0, this);
  74. break;
  75. case QMessageBox::Question:
  76. tmpIcon = style->standardIcon(QStyle::SP_MessageBoxQuestion, 0, this);
  77. break;
  78. default:
  79. break;
  80. }
  81. if (!tmpIcon.isNull()) {
  82. QWindow *window = this->windowHandle();
  83. if (!window) {
  84. if (const QWidget *nativeParent = this->nativeParentWidget()) {
  85. window = nativeParent->windowHandle();
  86. }
  87. }
  88. return tmpIcon.pixmap(window, QSize(iconSize, iconSize));
  89. }
  90. return QPixmap();
  91. }
  92. void VDeleteNotebookDialog::deleteCheckChanged(int p_state)
  93. {
  94. if (!p_state) {
  95. m_warningLabel->setText(tr("VNote won't delete files in directory <span style=\"%1\">%2</span>.")
  96. .arg(g_config->c_dataTextStyle).arg(m_notebook->getPath()));
  97. } else {
  98. m_warningLabel->setText(tr("<span style=\"%1\">WARNING</span>: "
  99. "VNote may delete <b>ANY</b> files in directory <span style=\"%2\">%3</span> "
  100. "and directory <span style=\"%2\">%4</span>!<br>"
  101. "VNote will try to delete all the root folders within this notebook one by one.<br>"
  102. "It may be UNRECOVERABLE!")
  103. .arg(g_config->c_warningTextStyle)
  104. .arg(g_config->c_dataTextStyle)
  105. .arg(m_notebook->getPath())
  106. .arg(m_notebook->getRecycleBinFolderPath()));
  107. }
  108. }