vnewfiledialog.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include <QtWidgets>
  2. #include "vnewfiledialog.h"
  3. #include "vconfigmanager.h"
  4. #include "vdirectory.h"
  5. extern VConfigManager *g_config;
  6. VNewFileDialog::VNewFileDialog(const QString &title, const QString &info,
  7. const QString &defaultName, VDirectory *directory,
  8. QWidget *parent)
  9. : QDialog(parent), title(title), info(info),
  10. defaultName(defaultName), m_directory(directory)
  11. {
  12. setupUI();
  13. connect(nameEdit, &QLineEdit::textChanged, this, &VNewFileDialog::handleInputChanged);
  14. handleInputChanged();
  15. }
  16. void VNewFileDialog::setupUI()
  17. {
  18. QLabel *infoLabel = NULL;
  19. if (!info.isEmpty()) {
  20. infoLabel = new QLabel(info);
  21. }
  22. // Name.
  23. QLabel *nameLabel = new QLabel(tr("Note &name:"));
  24. nameEdit = new QLineEdit(defaultName);
  25. int dotIndex = defaultName.lastIndexOf('.');
  26. nameEdit->setSelection(0, (dotIndex == -1) ? defaultName.size() : dotIndex);
  27. nameLabel->setBuddy(nameEdit);
  28. // InsertTitle.
  29. m_insertTitleCB = new QCheckBox(tr("Insert note name as title (for Markdown only)"));
  30. m_insertTitleCB->setToolTip(tr("Insert note name into the new note as a title"));
  31. m_insertTitleCB->setChecked(g_config->getInsertTitleFromNoteName());
  32. connect(m_insertTitleCB, &QCheckBox::stateChanged,
  33. this, [this](int p_state) {
  34. g_config->setInsertTitleFromNoteName(p_state == Qt::Checked);
  35. });
  36. QFormLayout *topLayout = new QFormLayout();
  37. topLayout->addRow(nameLabel, nameEdit);
  38. topLayout->addWidget(m_insertTitleCB);
  39. m_warnLabel = new QLabel();
  40. m_warnLabel->setWordWrap(true);
  41. m_warnLabel->hide();
  42. // Ok is the default button.
  43. m_btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
  44. connect(m_btnBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
  45. connect(m_btnBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
  46. QPushButton *okBtn = m_btnBox->button(QDialogButtonBox::Ok);
  47. nameEdit->setMinimumWidth(okBtn->sizeHint().width() * 3);
  48. QVBoxLayout *mainLayout = new QVBoxLayout();
  49. if (infoLabel) {
  50. mainLayout->addWidget(infoLabel);
  51. }
  52. mainLayout->addLayout(topLayout);
  53. mainLayout->addWidget(m_warnLabel);
  54. mainLayout->addWidget(m_btnBox);
  55. mainLayout->setSizeConstraint(QLayout::SetFixedSize);
  56. setLayout(mainLayout);
  57. setWindowTitle(title);
  58. }
  59. void VNewFileDialog::handleInputChanged()
  60. {
  61. bool showWarnLabel = false;
  62. QString name = nameEdit->text();
  63. bool nameOk = !name.isEmpty();
  64. if (nameOk) {
  65. // Check if the name conflicts with existing note name.
  66. // Case-insensitive when creating note.
  67. if (m_directory->findFile(name, false)) {
  68. nameOk = false;
  69. showWarnLabel = true;
  70. QString nameConflictText = tr("<span style=\"%1\">WARNING</span>: Name (case-insensitive) already exists. "
  71. "Please choose another name.")
  72. .arg(g_config->c_warningTextStyle);
  73. m_warnLabel->setText(nameConflictText);
  74. }
  75. }
  76. m_warnLabel->setVisible(showWarnLabel);
  77. QPushButton *okBtn = m_btnBox->button(QDialogButtonBox::Ok);
  78. okBtn->setEnabled(nameOk);
  79. }
  80. QString VNewFileDialog::getNameInput() const
  81. {
  82. return nameEdit->text();
  83. }
  84. bool VNewFileDialog::getInsertTitleInput() const
  85. {
  86. return m_insertTitleCB->isChecked();
  87. }