vfileinfodialog.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include <QtWidgets>
  2. #include "vfileinfodialog.h"
  3. #include "vdirectory.h"
  4. #include "vfile.h"
  5. #include "vconfigmanager.h"
  6. #include "utils/vutils.h"
  7. extern VConfigManager *g_config;
  8. VFileInfoDialog::VFileInfoDialog(const QString &title, const QString &info,
  9. VDirectory *directory, const VFile *file,
  10. QWidget *parent)
  11. : QDialog(parent), infoLabel(NULL), title(title), info(info),
  12. m_directory(directory), m_file(file)
  13. {
  14. setupUI();
  15. connect(nameEdit, &QLineEdit::textChanged, this, &VFileInfoDialog::handleInputChanged);
  16. handleInputChanged();
  17. }
  18. void VFileInfoDialog::setupUI()
  19. {
  20. if (!info.isEmpty()) {
  21. infoLabel = new QLabel(info);
  22. }
  23. nameLabel = new QLabel(tr("Note &name:"));
  24. nameEdit = new QLineEdit(m_file->getName());
  25. nameEdit->selectAll();
  26. nameLabel->setBuddy(nameEdit);
  27. m_warnLabel = new QLabel();
  28. m_warnLabel->setWordWrap(true);
  29. m_warnLabel->hide();
  30. // Ok is the default button.
  31. m_btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
  32. connect(m_btnBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
  33. connect(m_btnBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
  34. QHBoxLayout *topLayout = new QHBoxLayout();
  35. topLayout->addWidget(nameLabel);
  36. topLayout->addWidget(nameEdit);
  37. QPushButton *okBtn = m_btnBox->button(QDialogButtonBox::Ok);
  38. nameEdit->setMinimumWidth(okBtn->sizeHint().width() * 3);
  39. QVBoxLayout *mainLayout = new QVBoxLayout();
  40. if (infoLabel) {
  41. mainLayout->addWidget(infoLabel);
  42. }
  43. mainLayout->addLayout(topLayout);
  44. mainLayout->addWidget(m_warnLabel);
  45. mainLayout->addWidget(m_btnBox);
  46. mainLayout->setSizeConstraint(QLayout::SetFixedSize);
  47. setLayout(mainLayout);
  48. setWindowTitle(title);
  49. }
  50. void VFileInfoDialog::handleInputChanged()
  51. {
  52. bool showWarnLabel = false;
  53. QString name = nameEdit->text();
  54. bool nameOk = !name.isEmpty();
  55. if (nameOk && name != m_file->getName()) {
  56. // Check if the name conflicts with existing note name.
  57. // Case-insensitive when creating note.
  58. if (m_directory->findFile(name, false)) {
  59. nameOk = false;
  60. showWarnLabel = true;
  61. QString nameConflictText = tr("<span style=\"%1\">WARNING</span>: Name (case-insensitive) already exists. "
  62. "Please choose another name.")
  63. .arg(g_config->c_warningTextStyle);
  64. m_warnLabel->setText(nameConflictText);
  65. } else if (VUtils::docTypeFromName(name) != m_file->getDocType()) {
  66. // Check if the name change the doc type.
  67. nameOk = false;
  68. showWarnLabel = true;
  69. QString nameConflictText = tr("<span style=\"%1\">WARNING</span>: Changing type of the note is not supported. "
  70. "Please use the same suffix as the old one.")
  71. .arg(g_config->c_warningTextStyle);
  72. m_warnLabel->setText(nameConflictText);
  73. }
  74. }
  75. m_warnLabel->setVisible(showWarnLabel);
  76. QPushButton *okBtn = m_btnBox->button(QDialogButtonBox::Ok);
  77. okBtn->setEnabled(nameOk);
  78. }
  79. QString VFileInfoDialog::getNameInput() const
  80. {
  81. return nameEdit->text();
  82. }