vnewnotebookdialog.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. #include <QtWidgets>
  2. #include <QDir>
  3. #include "vnewnotebookdialog.h"
  4. #include "vconfigmanager.h"
  5. #include "utils/vutils.h"
  6. #include "vnotebook.h"
  7. extern VConfigManager vconfig;
  8. VNewNotebookDialog::VNewNotebookDialog(const QString &title, const QString &info,
  9. const QString &defaultName, const QString &defaultPath,
  10. const QVector<VNotebook *> &p_notebooks,
  11. QWidget *parent)
  12. : QDialog(parent), infoLabel(NULL),
  13. title(title), info(info), defaultName(defaultName), defaultPath(defaultPath),
  14. m_importNotebook(false), m_manualPath(false), m_manualName(false),
  15. m_notebooks(p_notebooks)
  16. {
  17. setupUI();
  18. connect(nameEdit, &QLineEdit::textChanged, this, &VNewNotebookDialog::handleInputChanged);
  19. connect(pathEdit, &QLineEdit::textChanged, this, &VNewNotebookDialog::handleInputChanged);
  20. connect(browseBtn, &QPushButton::clicked, this, &VNewNotebookDialog::handleBrowseBtnClicked);
  21. handleInputChanged();
  22. }
  23. void VNewNotebookDialog::setupUI()
  24. {
  25. if (!info.isEmpty()) {
  26. infoLabel = new QLabel(info);
  27. infoLabel->setWordWrap(true);
  28. }
  29. nameLabel = new QLabel(tr("Notebook &name:"));
  30. nameEdit = new QLineEdit(defaultName);
  31. nameLabel->setBuddy(nameEdit);
  32. QLabel *pathLabel = new QLabel(tr("Notebook &root folder:"));
  33. pathEdit = new QLineEdit(defaultPath);
  34. pathLabel->setBuddy(pathEdit);
  35. browseBtn = new QPushButton(tr("&Browse"));
  36. QLabel *imageFolderLabel = new QLabel(tr("&Image folder:"));
  37. m_imageFolderEdit = new QLineEdit();
  38. m_imageFolderEdit->setPlaceholderText(tr("Use global configuration (%1)")
  39. .arg(vconfig.getImageFolder()));
  40. imageFolderLabel->setBuddy(m_imageFolderEdit);
  41. QString imageFolderTip = tr("Set the name of the folder for all the notes of this notebook to store images "
  42. "(empty to use global configuration)");
  43. m_imageFolderEdit->setToolTip(imageFolderTip);
  44. imageFolderLabel->setToolTip(imageFolderTip);
  45. QValidator *validator = new QRegExpValidator(QRegExp(VUtils::c_fileNameRegExp), m_imageFolderEdit);
  46. m_imageFolderEdit->setValidator(validator);
  47. QGridLayout *topLayout = new QGridLayout();
  48. topLayout->addWidget(nameLabel, 0, 0);
  49. topLayout->addWidget(nameEdit, 0, 1, 1, 2);
  50. topLayout->addWidget(pathLabel, 1, 0);
  51. topLayout->addWidget(pathEdit, 1, 1);
  52. topLayout->addWidget(browseBtn, 1, 2);
  53. topLayout->addWidget(imageFolderLabel, 2, 0);
  54. topLayout->addWidget(m_imageFolderEdit, 2, 1);
  55. // Warning label.
  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. pathEdit->setMinimumWidth(okBtn->sizeHint().width() * 3);
  65. QVBoxLayout *mainLayout = new QVBoxLayout(this);
  66. if (infoLabel) {
  67. mainLayout->addWidget(infoLabel);
  68. }
  69. mainLayout->addLayout(topLayout);
  70. mainLayout->addWidget(m_warnLabel);
  71. mainLayout->addWidget(m_btnBox);
  72. // Will set the parent of above widgets properly.
  73. setLayout(mainLayout);
  74. mainLayout->setSizeConstraint(QLayout::SetFixedSize);
  75. setWindowTitle(title);
  76. }
  77. QString VNewNotebookDialog::getNameInput() const
  78. {
  79. return nameEdit->text();
  80. }
  81. QString VNewNotebookDialog::getPathInput() const
  82. {
  83. // absoluteFilePath() to convert the drive to upper case.
  84. // cleanPath() to remove duplicate separator, '.', and '..'.
  85. return QDir::cleanPath(QFileInfo(pathEdit->text()).absoluteFilePath());
  86. }
  87. QString VNewNotebookDialog::getImageFolder() const
  88. {
  89. if (m_imageFolderEdit->isEnabled()) {
  90. return m_imageFolderEdit->text();
  91. } else {
  92. return QString();
  93. }
  94. }
  95. void VNewNotebookDialog::handleBrowseBtnClicked()
  96. {
  97. static QString defaultPath;
  98. if (defaultPath.isEmpty()) {
  99. defaultPath = vconfig.getVnoteNotebookFolderPath();
  100. if (!QFileInfo::exists(defaultPath)) {
  101. defaultPath = QDir::homePath();
  102. }
  103. }
  104. QString dirPath = QFileDialog::getExistingDirectory(this, tr("Select Root Folder Of The Notebook"),
  105. defaultPath,
  106. QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
  107. if (!dirPath.isEmpty()) {
  108. m_manualPath = true;
  109. pathEdit->setText(dirPath);
  110. defaultPath = VUtils::basePathFromPath(dirPath);
  111. }
  112. }
  113. bool VNewNotebookDialog::isImportExistingNotebook() const
  114. {
  115. return m_importNotebook;
  116. }
  117. void VNewNotebookDialog::showEvent(QShowEvent *event)
  118. {
  119. nameEdit->setFocus();
  120. QDialog::showEvent(event);
  121. }
  122. void VNewNotebookDialog::handleInputChanged()
  123. {
  124. QString warnText = tr("<span style=\"%1\">WARNING</span>: The folder chosen is NOT empty! "
  125. "It is highly recommended to use an EMPTY and EXCLUSIVE folder for a new notebook.")
  126. .arg(vconfig.c_warningTextStyle);
  127. QString infoText = tr("<span style=\"%1\">INFO</span>: The folder chosen seems to be a root "
  128. "folder of a notebook created by VNote before. "
  129. "VNote will try to import it by reading the configuration file.")
  130. .arg("font-weight:bold;");
  131. bool pathOk = false;
  132. bool configExist = false;
  133. bool showWarnLabel = false;
  134. // User has input some texts.
  135. if (pathEdit->isModified()) {
  136. m_manualPath = true;
  137. }
  138. if (nameEdit->isModified()) {
  139. m_manualName = true;
  140. }
  141. if (autoComplete()) {
  142. return;
  143. }
  144. QString path = pathEdit->text();
  145. if (!path.isEmpty()) {
  146. if (QFileInfo::exists(path)) {
  147. QDir dir(path);
  148. QStringList files = dir.entryList(QDir::NoDotAndDotDot | QDir::AllEntries | QDir::Hidden);
  149. if (files.isEmpty()) {
  150. pathOk = true;
  151. } else {
  152. // Folder is not empty.
  153. configExist = VConfigManager::directoryConfigExist(path);
  154. showWarnLabel = true;
  155. }
  156. } else {
  157. pathOk = true;
  158. }
  159. }
  160. if (configExist) {
  161. pathOk = true;
  162. m_warnLabel->setText(infoText);
  163. } else {
  164. m_warnLabel->setText(warnText);
  165. }
  166. // Try to validate if this is a legal path on the OS.
  167. if (pathOk) {
  168. pathOk = VUtils::checkPathLegal(path);
  169. if (!pathOk) {
  170. showWarnLabel = true;
  171. QString tmp = tr("<span style=\"%1\">WARNING</span>: The path seems to be illegal. "
  172. "Please choose another one.")
  173. .arg(vconfig.c_warningTextStyle);
  174. m_warnLabel->setText(tmp);
  175. }
  176. }
  177. if (pathOk) {
  178. // Check if this path has been in VNote.
  179. int idx = -1;
  180. for (idx = 0; idx < m_notebooks.size(); ++idx) {
  181. if (VUtils::equalPath(m_notebooks[idx]->getPath(), path)) {
  182. break;
  183. }
  184. }
  185. if (idx < m_notebooks.size()) {
  186. pathOk = false;
  187. showWarnLabel = true;
  188. QString existText = tr("<span style=\"%1\">WARNING</span>: The folder chosen has already been a root folder "
  189. "of existing notebook <span style=\"%2\">%3</span> in VNote.")
  190. .arg(vconfig.c_warningTextStyle)
  191. .arg(vconfig.c_dataTextStyle)
  192. .arg(m_notebooks[idx]->getName());
  193. m_warnLabel->setText(existText);
  194. }
  195. }
  196. QString name = nameEdit->text();
  197. bool nameOk = !name.isEmpty();
  198. if (pathOk && nameOk) {
  199. // Check if the name conflicts with existing notebook name.
  200. int idx = -1;
  201. for (idx = 0; idx < m_notebooks.size(); ++idx) {
  202. if (m_notebooks[idx]->getName() == name) {
  203. break;
  204. }
  205. }
  206. if (idx < m_notebooks.size()) {
  207. nameOk = false;
  208. showWarnLabel = true;
  209. QString nameConflictText = tr("<span style=\"%1\">WARNING</span>: Name already exists. "
  210. "Please choose another name.")
  211. .arg(vconfig.c_warningTextStyle);
  212. m_warnLabel->setText(nameConflictText);
  213. }
  214. }
  215. m_warnLabel->setVisible(showWarnLabel);
  216. m_importNotebook = configExist;
  217. m_imageFolderEdit->setEnabled(!m_importNotebook);
  218. QPushButton *okBtn = m_btnBox->button(QDialogButtonBox::Ok);
  219. okBtn->setEnabled(nameOk && pathOk);
  220. }
  221. bool VNewNotebookDialog::autoComplete()
  222. {
  223. if (m_manualPath) {
  224. return false;
  225. }
  226. QString vnoteFolder = vconfig.getVnoteNotebookFolderPath();
  227. QString pathText = pathEdit->text();
  228. if (!pathText.isEmpty()
  229. && !VUtils::equalPath(vnoteFolder, VUtils::basePathFromPath(pathText))) {
  230. return false;
  231. }
  232. bool ret = false;
  233. QString nameText = nameEdit->text();
  234. if (nameText.isEmpty()) {
  235. if (m_manualName) {
  236. return false;
  237. }
  238. // Get a folder name under vnoteFolder and set it as the name of the notebook.
  239. QString name = "vnotebook";
  240. name = VUtils::getFileNameWithSequence(vnoteFolder, name);
  241. nameEdit->setText(name);
  242. ret = true;
  243. } else {
  244. // Use the name as the folder name under vnoteFolder.
  245. QString autoPath = QDir::cleanPath(QDir(vnoteFolder).filePath(nameText));
  246. if (autoPath != pathText) {
  247. pathEdit->setText(autoPath);
  248. ret = true;
  249. }
  250. }
  251. return ret;
  252. }