quickaccesspage.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. #include "quickaccesspage.h"
  2. #include <QVBoxLayout>
  3. #include <QFormLayout>
  4. #include <QGroupBox>
  5. #include <QPlainTextEdit>
  6. #include <QDebug>
  7. #include <QFileDialog>
  8. #include <QCheckBox>
  9. #include <QPushButton>
  10. #include <QComboBox>
  11. #include <QLineEdit>
  12. #include <QInputDialog>
  13. #include <core/sessionconfig.h>
  14. #include <core/coreconfig.h>
  15. #include <core/configmgr.h>
  16. #include <core/notebookmgr.h>
  17. #include <core/vnotex.h>
  18. #include <utils/widgetutils.h>
  19. #include <widgets/locationinputwithbrowsebutton.h>
  20. #include <widgets/lineeditwithsnippet.h>
  21. #include <widgets/widgetsfactory.h>
  22. #include <widgets/messageboxhelper.h>
  23. #include "../notetemplateselector.h"
  24. using namespace vnotex;
  25. QuickAccessPage::QuickAccessPage(QWidget *p_parent)
  26. : SettingsPage(p_parent)
  27. {
  28. setupUI();
  29. }
  30. void QuickAccessPage::setupUI()
  31. {
  32. auto mainLayout = new QVBoxLayout(this);
  33. auto flashPageBox = setupFlashPageGroup();
  34. mainLayout->addWidget(flashPageBox);
  35. auto quickAccessBox = setupQuickAccessGroup();
  36. mainLayout->addWidget(quickAccessBox);
  37. auto quickNoteBox = setupQuickNoteGroup();
  38. mainLayout->addWidget(quickNoteBox);
  39. mainLayout->addStretch();
  40. }
  41. void QuickAccessPage::loadInternal()
  42. {
  43. const auto &sessionConfig = ConfigMgr::getInst().getSessionConfig();
  44. m_flashPageInput->setText(sessionConfig.getFlashPage());
  45. {
  46. const auto &quickAccess = sessionConfig.getQuickAccessFiles();
  47. if (!quickAccess.isEmpty()) {
  48. m_quickAccessTextEdit->setPlainText(quickAccess.join(QChar('\n')));
  49. }
  50. }
  51. loadQuickNoteSchemes();
  52. }
  53. void QuickAccessPage::loadQuickNoteSchemes()
  54. {
  55. const auto &sessionConfig = ConfigMgr::getInst().getSessionConfig();
  56. m_quickNoteSchemes = sessionConfig.getQuickNoteSchemes();
  57. m_quickNoteCurrentIndex = -1;
  58. m_quickNoteSchemeComboBox->clear();
  59. for (const auto &scheme : m_quickNoteSchemes) {
  60. m_quickNoteSchemeComboBox->addItem(scheme.m_name);
  61. }
  62. if (m_quickNoteSchemeComboBox->count() > 0) {
  63. m_quickNoteSchemeComboBox->setCurrentIndex(0);
  64. // Manually call the handler.
  65. setCurrentQuickNote(0);
  66. }
  67. }
  68. bool QuickAccessPage::saveInternal()
  69. {
  70. auto &sessionConfig = ConfigMgr::getInst().getSessionConfig();
  71. sessionConfig.setFlashPage(m_flashPageInput->text());
  72. {
  73. auto text = m_quickAccessTextEdit->toPlainText();
  74. if (!text.isEmpty()) {
  75. sessionConfig.setQuickAccessFiles(text.split(QChar('\n')));
  76. }
  77. }
  78. saveQuickNoteSchemes();
  79. return true;
  80. }
  81. void QuickAccessPage::saveQuickNoteSchemes()
  82. {
  83. // Save current quick note scheme from inputs.
  84. saveCurrentQuickNote();
  85. auto &sessionConfig = ConfigMgr::getInst().getSessionConfig();
  86. sessionConfig.setQuickNoteSchemes(m_quickNoteSchemes);
  87. }
  88. QString QuickAccessPage::title() const
  89. {
  90. return tr("Quick Access");
  91. }
  92. QGroupBox *QuickAccessPage::setupFlashPageGroup()
  93. {
  94. auto box = new QGroupBox(tr("Flash Page"), this);
  95. auto layout = WidgetsFactory::createFormLayout(box);
  96. {
  97. m_flashPageInput = new LocationInputWithBrowseButton(box);
  98. m_flashPageInput->setToolTip(tr("Flash Page location (user could copy the path of one note and paste it here)"));
  99. const QString label(tr("Flash Page:"));
  100. layout->addRow(label, m_flashPageInput);
  101. addSearchItem(label, m_flashPageInput->toolTip(), m_flashPageInput);
  102. connect(m_flashPageInput, &LocationInputWithBrowseButton::textChanged,
  103. this, &QuickAccessPage::pageIsChanged);
  104. connect(m_flashPageInput, &LocationInputWithBrowseButton::clicked,
  105. this, [this]() {
  106. auto filePath = QFileDialog::getOpenFileName(this,
  107. tr("Select Flash Page File"),
  108. QDir::homePath());
  109. if (!filePath.isEmpty()) {
  110. m_flashPageInput->setText(filePath);
  111. }
  112. });
  113. }
  114. return box;
  115. }
  116. QGroupBox *QuickAccessPage::setupQuickAccessGroup()
  117. {
  118. auto box = new QGroupBox(tr("Quick Access"), this);
  119. auto layout = WidgetsFactory::createFormLayout(box);
  120. {
  121. m_quickAccessTextEdit = WidgetsFactory::createPlainTextEdit(box);
  122. m_quickAccessTextEdit->setToolTip(tr("Edit the files pinned to Quick Access (one file per line)"));
  123. m_quickAccessTextEdit->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
  124. const QString label(tr("Quick Access:"));
  125. layout->addRow(label, m_quickAccessTextEdit);
  126. addSearchItem(label, m_quickAccessTextEdit->toolTip(), m_quickAccessTextEdit);
  127. connect(m_quickAccessTextEdit, &QPlainTextEdit::textChanged,
  128. this, &QuickAccessPage::pageIsChanged);
  129. }
  130. return box;
  131. }
  132. QString QuickAccessPage::getDefaultQuickNoteFolderPath()
  133. {
  134. auto defaultPath = QDir::homePath();
  135. auto currentNotebook = VNoteX::getInst().getNotebookMgr().getCurrentNotebook();
  136. if (currentNotebook) {
  137. defaultPath = currentNotebook->getRootFolderAbsolutePath();
  138. }
  139. return defaultPath;
  140. }
  141. QGroupBox *QuickAccessPage::setupQuickNoteGroup()
  142. {
  143. auto box = new QGroupBox(tr("Quick Note"), this);
  144. auto mainLayout = WidgetsFactory::createFormLayout(box);
  145. {
  146. auto selectorLayout = new QHBoxLayout();
  147. // Add items in loadInternal().
  148. m_quickNoteSchemeComboBox = WidgetsFactory::createComboBox(box);
  149. selectorLayout->addWidget(m_quickNoteSchemeComboBox, 1);
  150. m_quickNoteSchemeComboBox->setPlaceholderText(tr("No scheme to show"));
  151. auto newBtn = new QPushButton(tr("New"), box);
  152. connect(newBtn, &QPushButton::clicked,
  153. this, &QuickAccessPage::newQuickNoteScheme);
  154. selectorLayout->addWidget(newBtn);
  155. auto deleteBtn = new QPushButton(tr("Delete"), box);
  156. deleteBtn->setEnabled(false);
  157. connect(deleteBtn, &QPushButton::clicked,
  158. this, &QuickAccessPage::removeQuickNoteScheme);
  159. selectorLayout->addWidget(deleteBtn);
  160. const QString label(tr("Scheme:"));
  161. mainLayout->addRow(label, selectorLayout);
  162. addSearchItem(label, m_quickNoteSchemeComboBox);
  163. connect(m_quickNoteSchemeComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
  164. this, &QuickAccessPage::pageIsChanged);
  165. connect(m_quickNoteSchemeComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
  166. this, [deleteBtn](int idx) {
  167. deleteBtn->setEnabled(idx > -1);
  168. });
  169. }
  170. m_quickNoteInfoGroupBox = new QGroupBox(box);
  171. mainLayout->addRow(m_quickNoteInfoGroupBox);
  172. auto infoLayout = WidgetsFactory::createFormLayout(m_quickNoteInfoGroupBox);
  173. {
  174. const QString label(tr("Folder path:"));
  175. m_quickNoteFolderPathInput = new LocationInputWithBrowseButton(m_quickNoteInfoGroupBox);
  176. m_quickNoteFolderPathInput->setPlaceholderText(tr("Empty to use current explored folder dynamically"));
  177. infoLayout->addRow(label, m_quickNoteFolderPathInput);
  178. addSearchItem(label, m_quickNoteFolderPathInput);
  179. connect(m_quickNoteFolderPathInput, &LocationInputWithBrowseButton::textChanged,
  180. this, &QuickAccessPage::pageIsChanged);
  181. connect(m_quickNoteFolderPathInput, &LocationInputWithBrowseButton::clicked,
  182. this, [this]() {
  183. auto folderPath = QFileDialog::getExistingDirectory(this,
  184. tr("Select Quick Note Folder"),
  185. getDefaultQuickNoteFolderPath());
  186. if (!folderPath.isEmpty()) {
  187. m_quickNoteFolderPathInput->setText(folderPath);
  188. }
  189. });
  190. }
  191. {
  192. const QString label(tr("Note name:"));
  193. m_quickNoteNoteNameLineEdit = WidgetsFactory::createLineEditWithSnippet(m_quickNoteInfoGroupBox);
  194. infoLayout->addRow(label, m_quickNoteNoteNameLineEdit);
  195. connect(m_quickNoteNoteNameLineEdit, &QLineEdit::textChanged,
  196. this, &QuickAccessPage::pageIsChanged);
  197. }
  198. {
  199. const QString label(tr("Note template:"));
  200. m_quickNoteTemplateSelector = new NoteTemplateSelector(m_quickNoteInfoGroupBox);
  201. infoLayout->addRow(label, m_quickNoteTemplateSelector);
  202. connect(m_quickNoteTemplateSelector, &NoteTemplateSelector::templateChanged,
  203. this, &QuickAccessPage::pageIsChanged);
  204. }
  205. m_quickNoteInfoGroupBox->setVisible(false);
  206. connect(m_quickNoteSchemeComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
  207. this, [this](int idx) {
  208. if (isLoading()) {
  209. return;
  210. }
  211. setCurrentQuickNote(idx);
  212. });
  213. return box;
  214. }
  215. void QuickAccessPage::newQuickNoteScheme()
  216. {
  217. bool isDuplicated = false;
  218. QString schemeName;
  219. do {
  220. schemeName = QInputDialog::getText(this, tr("Quick Note Scheme"),
  221. isDuplicated ? tr("Scheme name already exists! Try again:") : tr("Scheme name:"));
  222. if (schemeName.isEmpty()) {
  223. return;
  224. }
  225. isDuplicated = m_quickNoteSchemeComboBox->findText(schemeName) != -1;
  226. } while (isDuplicated);
  227. SessionConfig::QuickNoteScheme scheme;
  228. scheme.m_name = schemeName;
  229. scheme.m_folderPath = getDefaultQuickNoteFolderPath();
  230. scheme.m_noteName = tr("quick_note_%da%.md");
  231. m_quickNoteSchemes.push_back(scheme);
  232. m_quickNoteSchemeComboBox->addItem(schemeName);
  233. m_quickNoteSchemeComboBox->setCurrentText(schemeName);
  234. emit pageIsChanged();
  235. }
  236. void QuickAccessPage::removeQuickNoteScheme()
  237. {
  238. int idx = m_quickNoteSchemeComboBox->currentIndex();
  239. Q_ASSERT(idx > -1);
  240. auto& scheme = m_quickNoteSchemes[idx];
  241. int ret = MessageBoxHelper::questionOkCancel(MessageBoxHelper::Type::Question,
  242. tr("Delete quick note scheme (%1)?").arg(scheme.m_name));
  243. if (ret != QMessageBox::Ok) {
  244. return;
  245. }
  246. m_quickNoteCurrentIndex = -1;
  247. m_quickNoteSchemes.removeAt(idx);
  248. m_quickNoteSchemeComboBox->removeItem(idx);
  249. emit pageIsChanged();
  250. }
  251. void QuickAccessPage::saveCurrentQuickNote()
  252. {
  253. if (m_quickNoteCurrentIndex < 0) {
  254. return;
  255. }
  256. Q_ASSERT(m_quickNoteCurrentIndex < m_quickNoteSchemes.size());
  257. auto& scheme = m_quickNoteSchemes[m_quickNoteCurrentIndex];
  258. scheme.m_folderPath = m_quickNoteFolderPathInput->text();
  259. // No need to apply the snippet for now.
  260. scheme.m_noteName = m_quickNoteNoteNameLineEdit->text();
  261. scheme.m_template = m_quickNoteTemplateSelector->getCurrentTemplate();
  262. }
  263. void QuickAccessPage::loadCurrentQuickNote()
  264. {
  265. if (m_quickNoteCurrentIndex < 0) {
  266. m_quickNoteFolderPathInput->setText(QString());
  267. m_quickNoteNoteNameLineEdit->setText(QString());
  268. m_quickNoteTemplateSelector->setCurrentTemplate(QString());
  269. return;
  270. }
  271. Q_ASSERT(m_quickNoteCurrentIndex < m_quickNoteSchemes.size());
  272. const auto& scheme = m_quickNoteSchemes[m_quickNoteCurrentIndex];
  273. m_quickNoteFolderPathInput->setText(scheme.m_folderPath);
  274. m_quickNoteNoteNameLineEdit->setText(scheme.m_noteName);
  275. m_quickNoteTemplateSelector->setCurrentTemplate(scheme.m_template);
  276. }
  277. void QuickAccessPage::setCurrentQuickNote(int idx)
  278. {
  279. saveCurrentQuickNote();
  280. m_quickNoteCurrentIndex = idx;
  281. loadCurrentQuickNote();
  282. m_quickNoteInfoGroupBox->setVisible(idx > -1);
  283. }