vdirinfodialog.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include <QtWidgets>
  2. #include "vdirinfodialog.h"
  3. #include "vdirectory.h"
  4. #include "vconfigmanager.h"
  5. #include "vmetawordlineedit.h"
  6. #include "utils/vutils.h"
  7. extern VConfigManager *g_config;
  8. VDirInfoDialog::VDirInfoDialog(const QString &title,
  9. const QString &info,
  10. const VDirectory *directory,
  11. VDirectory *parentDirectory,
  12. QWidget *parent)
  13. : QDialog(parent), title(title), info(info),
  14. m_directory(directory), m_parentDirectory(parentDirectory)
  15. {
  16. setupUI();
  17. connect(m_nameEdit, &VMetaWordLineEdit::textChanged, this, &VDirInfoDialog::handleInputChanged);
  18. handleInputChanged();
  19. }
  20. void VDirInfoDialog::setupUI()
  21. {
  22. QLabel *infoLabel = NULL;
  23. if (!info.isEmpty()) {
  24. infoLabel = new QLabel(info);
  25. }
  26. m_nameEdit = new VMetaWordLineEdit(m_directory->getName());
  27. QValidator *validator = new QRegExpValidator(QRegExp(VUtils::c_fileNameRegExp),
  28. m_nameEdit);
  29. m_nameEdit->setValidator(validator);
  30. m_nameEdit->selectAll();
  31. // Created time.
  32. QString createdTimeStr = VUtils::displayDateTime(m_directory->getCreatedTimeUtc().toLocalTime());
  33. QLabel *createdTimeLabel = new QLabel(createdTimeStr);
  34. QFormLayout *topLayout = new QFormLayout();
  35. topLayout->addRow(tr("Folder &name:"), m_nameEdit);
  36. topLayout->addRow(tr("Created time:"), createdTimeLabel);
  37. m_warnLabel = new QLabel();
  38. m_warnLabel->setWordWrap(true);
  39. m_warnLabel->hide();
  40. // Ok is the default button.
  41. m_btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
  42. connect(m_btnBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
  43. connect(m_btnBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
  44. QPushButton *okBtn = m_btnBox->button(QDialogButtonBox::Ok);
  45. okBtn->setProperty("SpecialBtn", true);
  46. m_nameEdit->setMinimumWidth(okBtn->sizeHint().width() * 3);
  47. QVBoxLayout *mainLayout = new QVBoxLayout();
  48. if (infoLabel) {
  49. mainLayout->addWidget(infoLabel);
  50. }
  51. mainLayout->addLayout(topLayout);
  52. mainLayout->addWidget(m_warnLabel);
  53. mainLayout->addWidget(m_btnBox);
  54. mainLayout->setSizeConstraint(QLayout::SetFixedSize);
  55. setLayout(mainLayout);
  56. setWindowTitle(title);
  57. }
  58. void VDirInfoDialog::handleInputChanged()
  59. {
  60. bool showWarnLabel = false;
  61. QString name = m_nameEdit->getEvaluatedText();
  62. bool nameOk = !name.isEmpty();
  63. if (nameOk && name != m_directory->getName()) {
  64. // Check if the name conflicts with existing directory name.
  65. // Case-insensitive when creating note.
  66. const VDirectory *directory = m_parentDirectory->findSubDirectory(name, false);
  67. QString warnText;
  68. if (directory && directory != m_directory) {
  69. nameOk = false;
  70. warnText = tr("<span style=\"%1\">WARNING</span>: "
  71. "Name (case-insensitive) <span style=\"%2\">%3</span> already exists. "
  72. "Please choose another name.")
  73. .arg(g_config->c_warningTextStyle)
  74. .arg(g_config->c_dataTextStyle)
  75. .arg(name);
  76. } else if (!VUtils::checkFileNameLegal(name)) {
  77. // Check if evaluated name contains illegal characters.
  78. nameOk = false;
  79. warnText = tr("<span style=\"%1\">WARNING</span>: "
  80. "Name <span style=\"%2\">%3</span> contains illegal characters "
  81. "(after magic word evaluation).")
  82. .arg(g_config->c_warningTextStyle)
  83. .arg(g_config->c_dataTextStyle)
  84. .arg(name);
  85. }
  86. if (!nameOk) {
  87. showWarnLabel = true;
  88. m_warnLabel->setText(warnText);
  89. }
  90. }
  91. m_warnLabel->setVisible(showWarnLabel);
  92. QPushButton *okBtn = m_btnBox->button(QDialogButtonBox::Ok);
  93. okBtn->setEnabled(nameOk);
  94. }
  95. QString VDirInfoDialog::getNameInput() const
  96. {
  97. return m_nameEdit->getEvaluatedText();
  98. }