vfileinfodialog.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #include <QtWidgets>
  2. #include "vfileinfodialog.h"
  3. #include "vdirectory.h"
  4. #include "vnotefile.h"
  5. #include "vconfigmanager.h"
  6. #include "utils/vutils.h"
  7. #include "vlineedit.h"
  8. extern VConfigManager *g_config;
  9. VFileInfoDialog::VFileInfoDialog(const QString &title,
  10. const QString &info,
  11. VDirectory *directory,
  12. const VNoteFile *file,
  13. QWidget *parent)
  14. : QDialog(parent), m_directory(directory), m_file(file)
  15. {
  16. setupUI(title, info);
  17. connect(m_nameEdit, &QLineEdit::textChanged, this, &VFileInfoDialog::handleInputChanged);
  18. handleInputChanged();
  19. }
  20. void VFileInfoDialog::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. // File name.
  27. QString name = m_file->getName();
  28. m_nameEdit = new VLineEdit(name);
  29. QValidator *validator = new QRegExpValidator(QRegExp(VUtils::c_fileNameRegExp),
  30. m_nameEdit);
  31. m_nameEdit->setValidator(validator);
  32. int baseStart = 0, baseLength = name.size();
  33. int dotIdx = name.lastIndexOf('.');
  34. if (dotIdx != -1) {
  35. baseLength = dotIdx;
  36. }
  37. // Select without suffix.
  38. m_nameEdit->setSelection(baseStart, baseLength);
  39. // Attachment folder.
  40. QLineEdit *attachmentFolderEdit = new QLineEdit(m_file->getAttachmentFolder());
  41. attachmentFolderEdit->setPlaceholderText(tr("Will be assigned when adding attachments"));
  42. attachmentFolderEdit->setToolTip(tr("The folder to hold attachments of this note"));
  43. attachmentFolderEdit->setReadOnly(true);
  44. // Created time.
  45. QString createdTimeStr = VUtils::displayDateTime(m_file->getCreatedTimeUtc().toLocalTime());
  46. QLabel *createdTimeLabel = new QLabel(createdTimeStr);
  47. // Modified time.
  48. createdTimeStr = VUtils::displayDateTime(m_file->getModifiedTimeUtc().toLocalTime());
  49. QLabel *modifiedTimeLabel = new QLabel(createdTimeStr);
  50. modifiedTimeLabel->setToolTip(tr("Last modified time within VNote"));
  51. QFormLayout *topLayout = new QFormLayout();
  52. topLayout->addRow(tr("Note &name:"), m_nameEdit);
  53. topLayout->addRow(tr("Attachment folder:"), attachmentFolderEdit);
  54. topLayout->addRow(tr("Created time:"), createdTimeLabel);
  55. topLayout->addRow(tr("Modified time:"), modifiedTimeLabel);
  56. m_warnLabel = new QLabel();
  57. m_warnLabel->setWordWrap(true);
  58. m_warnLabel->hide();
  59. // Ok is the default button.
  60. m_btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
  61. connect(m_btnBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
  62. connect(m_btnBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
  63. QPushButton *okBtn = m_btnBox->button(QDialogButtonBox::Ok);
  64. okBtn->setProperty("SpecialBtn", true);
  65. m_nameEdit->setMinimumWidth(okBtn->sizeHint().width() * 3);
  66. QVBoxLayout *mainLayout = new QVBoxLayout();
  67. if (infoLabel) {
  68. mainLayout->addWidget(infoLabel);
  69. }
  70. mainLayout->addLayout(topLayout);
  71. mainLayout->addWidget(m_warnLabel);
  72. mainLayout->addWidget(m_btnBox);
  73. mainLayout->setSizeConstraint(QLayout::SetFixedSize);
  74. setLayout(mainLayout);
  75. setWindowTitle(p_title);
  76. }
  77. void VFileInfoDialog::handleInputChanged()
  78. {
  79. bool showWarnLabel = false;
  80. QString name = m_nameEdit->getEvaluatedText();
  81. bool nameOk = !name.isEmpty();
  82. if (nameOk && name != m_file->getName()) {
  83. // Check if the name conflicts with existing note name.
  84. // Case-insensitive when creating note.
  85. const VNoteFile *file = m_directory->findFile(name, false);
  86. QString warnText;
  87. if (file && file != m_file) {
  88. nameOk = false;
  89. warnText = tr("<span style=\"%1\">WARNING</span>: "
  90. "Name (case-insensitive) <span style=\"%2\">%3</span> already exists. "
  91. "Please choose another name.")
  92. .arg(g_config->c_warningTextStyle)
  93. .arg(g_config->c_dataTextStyle)
  94. .arg(name);
  95. } else if (m_file->getDocType() != DocType::Unknown
  96. && VUtils::docTypeFromName(name) != m_file->getDocType()) {
  97. // Check if the name change the doc type.
  98. nameOk = false;
  99. warnText = tr("<span style=\"%1\">WARNING</span>: "
  100. "Changing type of the note is not supported. "
  101. "Please use the same suffix as the old one.")
  102. .arg(g_config->c_warningTextStyle);
  103. } else if (!VUtils::checkFileNameLegal(name)) {
  104. // Check if evaluated name contains illegal characters.
  105. nameOk = false;
  106. warnText = tr("<span style=\"%1\">WARNING</span>: "
  107. "Name <span style=\"%2\">%3</span> contains illegal characters "
  108. "(after magic word evaluation).")
  109. .arg(g_config->c_warningTextStyle)
  110. .arg(g_config->c_dataTextStyle)
  111. .arg(name);
  112. }
  113. if (!nameOk) {
  114. showWarnLabel = true;
  115. m_warnLabel->setText(warnText);
  116. }
  117. }
  118. m_warnLabel->setVisible(showWarnLabel);
  119. QPushButton *okBtn = m_btnBox->button(QDialogButtonBox::Ok);
  120. okBtn->setEnabled(nameOk);
  121. }
  122. QString VFileInfoDialog::getNameInput() const
  123. {
  124. return m_nameEdit->getEvaluatedText();
  125. }