vnotebookinfodialog.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include <QtWidgets>
  2. #include "vnotebookinfodialog.h"
  3. #include "vnotebook.h"
  4. #include "utils/vutils.h"
  5. #include "vconfigmanager.h"
  6. extern VConfigManager *g_config;
  7. VNotebookInfoDialog::VNotebookInfoDialog(const QString &p_title,
  8. const QString &p_info,
  9. const VNotebook *p_notebook,
  10. const QVector<VNotebook *> &p_notebooks,
  11. QWidget *p_parent)
  12. : QDialog(p_parent), m_notebook(p_notebook), m_infoLabel(NULL),
  13. m_notebooks(p_notebooks)
  14. {
  15. setupUI(p_title, p_info);
  16. connect(m_nameEdit, &QLineEdit::textChanged,
  17. this, &VNotebookInfoDialog::handleInputChanged);
  18. handleInputChanged();
  19. }
  20. void VNotebookInfoDialog::setupUI(const QString &p_title, const QString &p_info)
  21. {
  22. if (!p_info.isEmpty()) {
  23. m_infoLabel = new QLabel(p_info);
  24. }
  25. QLabel *nameLabel = new QLabel(tr("Notebook &name:"));
  26. m_nameEdit = new QLineEdit(m_notebook->getName());
  27. m_nameEdit->selectAll();
  28. nameLabel->setBuddy(m_nameEdit);
  29. QLabel *pathLabel = new QLabel(tr("Notebook &root folder:"));
  30. m_pathEdit = new QLineEdit(m_notebook->getPath());
  31. pathLabel->setBuddy(m_pathEdit);
  32. m_pathEdit->setReadOnly(true);
  33. QLabel *imageFolderLabel = new QLabel(tr("&Image folder:"));
  34. m_imageFolderEdit = new QLineEdit(m_notebook->getImageFolderConfig());
  35. m_imageFolderEdit->setPlaceholderText(tr("Use global configuration (%1)")
  36. .arg(g_config->getImageFolder()));
  37. imageFolderLabel->setBuddy(m_imageFolderEdit);
  38. QString imageFolderTip = tr("Set the name of the folder for all the notes of this notebook to store images "
  39. "(empty to use global configuration)");
  40. m_imageFolderEdit->setToolTip(imageFolderTip);
  41. imageFolderLabel->setToolTip(imageFolderTip);
  42. QValidator *validator = new QRegExpValidator(QRegExp(VUtils::c_fileNameRegExp), m_imageFolderEdit);
  43. m_imageFolderEdit->setValidator(validator);
  44. // Warning label.
  45. m_warnLabel = new QLabel();
  46. m_warnLabel->setWordWrap(true);
  47. m_warnLabel->hide();
  48. QFormLayout *topLayout = new QFormLayout();
  49. topLayout->addRow(nameLabel, m_nameEdit);
  50. topLayout->addRow(pathLabel, m_pathEdit);
  51. topLayout->addRow(imageFolderLabel, m_imageFolderEdit);
  52. topLayout->addRow(m_warnLabel);
  53. // Ok is the default button.
  54. m_btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
  55. connect(m_btnBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
  56. connect(m_btnBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
  57. QPushButton *okBtn = m_btnBox->button(QDialogButtonBox::Ok);
  58. m_pathEdit->setMinimumWidth(okBtn->sizeHint().width() * 3);
  59. QVBoxLayout *mainLayout = new QVBoxLayout();
  60. if (m_infoLabel) {
  61. mainLayout->addWidget(m_infoLabel);
  62. }
  63. mainLayout->addLayout(topLayout);
  64. mainLayout->addWidget(m_btnBox);
  65. setLayout(mainLayout);
  66. mainLayout->setSizeConstraint(QLayout::SetFixedSize);
  67. setWindowTitle(p_title);
  68. }
  69. void VNotebookInfoDialog::handleInputChanged()
  70. {
  71. QString name = m_nameEdit->text();
  72. bool nameOk = !name.isEmpty();
  73. bool showWarnLabel = false;
  74. if (nameOk && name != m_notebook->getName()) {
  75. // Check if the name conflicts with existing notebook name.
  76. // Case-insensitive.
  77. int idx = -1;
  78. for (idx = 0; idx < m_notebooks.size(); ++idx) {
  79. if (m_notebooks[idx]->getName().toLower() == name.toLower()) {
  80. break;
  81. }
  82. }
  83. if (idx < m_notebooks.size()) {
  84. nameOk = false;
  85. showWarnLabel = true;
  86. QString nameConflictText = tr("<span style=\"%1\">WARNING</span>: Name (case-insensitive) already exists. "
  87. "Please choose another name.")
  88. .arg(g_config->c_warningTextStyle);
  89. m_warnLabel->setText(nameConflictText);
  90. }
  91. }
  92. m_warnLabel->setVisible(showWarnLabel);
  93. QPushButton *okBtn = m_btnBox->button(QDialogButtonBox::Ok);
  94. okBtn->setEnabled(nameOk);
  95. }
  96. QString VNotebookInfoDialog::getName() const
  97. {
  98. return m_nameEdit->text();
  99. }
  100. QString VNotebookInfoDialog::getImageFolder() const
  101. {
  102. return m_imageFolderEdit->text();
  103. }
  104. void VNotebookInfoDialog::showEvent(QShowEvent *p_event)
  105. {
  106. m_nameEdit->setFocus();
  107. QDialog::showEvent(p_event);
  108. }