vnotebookinfodialog.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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),
  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. QLabel *infoLabel = NULL;
  23. if (!p_info.isEmpty()) {
  24. infoLabel = new QLabel(p_info);
  25. }
  26. m_nameEdit = new QLineEdit(m_notebook->getName());
  27. m_nameEdit->selectAll();
  28. m_pathEdit = new QLineEdit(m_notebook->getPath());
  29. m_pathEdit->setReadOnly(true);
  30. m_imageFolderEdit = new QLineEdit(m_notebook->getImageFolderConfig());
  31. m_imageFolderEdit->setPlaceholderText(tr("Use global configuration (%1)")
  32. .arg(g_config->getImageFolder()));
  33. m_imageFolderEdit->setToolTip(tr("Set the name of the folder for all the notes of this notebook to store images "
  34. "(empty to use global configuration)"));
  35. QValidator *validator = new QRegExpValidator(QRegExp(VUtils::c_fileNameRegExp), m_imageFolderEdit);
  36. m_imageFolderEdit->setValidator(validator);
  37. QLabel *imageFolderLabel = new QLabel(tr("&Image folder:"));
  38. imageFolderLabel->setBuddy(m_imageFolderEdit);
  39. imageFolderLabel->setToolTip(m_imageFolderEdit->toolTip());
  40. // Created time.
  41. QString createdTimeStr = const_cast<VNotebook *>(m_notebook)->getCreatedTimeUtc().toLocalTime()
  42. .toString(Qt::DefaultLocaleLongDate);
  43. QLabel *createdTimeLabel = new QLabel(createdTimeStr);
  44. QFormLayout *topLayout = new QFormLayout();
  45. topLayout->addRow(tr("Notebook &name:"), m_nameEdit);
  46. topLayout->addRow(tr("Notebook &root folder:"), m_pathEdit);
  47. topLayout->addRow(imageFolderLabel, m_imageFolderEdit);
  48. topLayout->addRow(tr("Created time:"), createdTimeLabel);
  49. // Warning label.
  50. m_warnLabel = new QLabel();
  51. m_warnLabel->setWordWrap(true);
  52. m_warnLabel->hide();
  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 (infoLabel) {
  61. mainLayout->addWidget(infoLabel);
  62. }
  63. mainLayout->addLayout(topLayout);
  64. mainLayout->addWidget(m_warnLabel);
  65. mainLayout->addWidget(m_btnBox);
  66. setLayout(mainLayout);
  67. mainLayout->setSizeConstraint(QLayout::SetFixedSize);
  68. setWindowTitle(p_title);
  69. }
  70. void VNotebookInfoDialog::handleInputChanged()
  71. {
  72. QString name = m_nameEdit->text();
  73. bool nameOk = !name.isEmpty();
  74. bool showWarnLabel = false;
  75. if (nameOk && name != m_notebook->getName()) {
  76. // Check if the name conflicts with existing notebook name.
  77. // Case-insensitive.
  78. int idx = -1;
  79. for (idx = 0; idx < m_notebooks.size(); ++idx) {
  80. if (m_notebooks[idx]->getName().toLower() == name.toLower()) {
  81. break;
  82. }
  83. }
  84. if (idx < m_notebooks.size()) {
  85. nameOk = false;
  86. showWarnLabel = true;
  87. QString nameConflictText = tr("<span style=\"%1\">WARNING</span>: Name (case-insensitive) already exists. "
  88. "Please choose another name.")
  89. .arg(g_config->c_warningTextStyle);
  90. m_warnLabel->setText(nameConflictText);
  91. }
  92. }
  93. m_warnLabel->setVisible(showWarnLabel);
  94. QPushButton *okBtn = m_btnBox->button(QDialogButtonBox::Ok);
  95. okBtn->setEnabled(nameOk);
  96. }
  97. QString VNotebookInfoDialog::getName() const
  98. {
  99. return m_nameEdit->text();
  100. }
  101. QString VNotebookInfoDialog::getImageFolder() const
  102. {
  103. return m_imageFolderEdit->text();
  104. }
  105. void VNotebookInfoDialog::showEvent(QShowEvent *p_event)
  106. {
  107. m_nameEdit->setFocus();
  108. QDialog::showEvent(p_event);
  109. }