Skip to content

Commit e8fe072

Browse files
feloxxtamlok
andauthored
QuickAccess: add quick note (#2373)
* feature_quick_create_note * adj name and complete quick create * del unused * del unused * adj name * adj to const * adj name 2 * adj * fix --------- Co-authored-by: Le Tan <[email protected]>
1 parent f773bc0 commit e8fe072

29 files changed

+719
-129
lines changed

src/core/notebookconfigmgr/vxnotebookconfigmgr.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,10 @@ void VXNotebookConfigMgr::addChildNode(Node *p_parent, const QSharedPointer<Node
461461
QSharedPointer<Node> VXNotebookConfigMgr::loadNodeByPath(const QSharedPointer<Node> &p_root, const QString &p_relativePath)
462462
{
463463
auto p = PathUtils::cleanPath(p_relativePath);
464+
if (p == ".") {
465+
return p_root;
466+
}
467+
464468
auto paths = p.split('/', QString::SkipEmptyParts);
465469
auto node = p_root;
466470
for (auto &pa : paths) {

src/core/sessionconfig.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,34 @@ QJsonObject SessionConfig::NotebookItem::toJson() const
3838
return jobj;
3939
}
4040

41+
bool SessionConfig::QuickNoteScheme::operator==(const QuickNoteScheme &p_other) const
42+
{
43+
return m_name == p_other.m_name &&
44+
m_folderPath == p_other.m_folderPath &&
45+
m_noteName == p_other.m_noteName &&
46+
m_template == p_other.m_template;
47+
}
48+
49+
void SessionConfig::QuickNoteScheme::fromJson(const QJsonObject &p_jobj)
50+
{
51+
m_name = p_jobj[QStringLiteral("name")].toString();
52+
m_folderPath = p_jobj[QStringLiteral("folder_path")].toString();
53+
m_noteName = p_jobj[QStringLiteral("note_name")].toString();
54+
m_template = p_jobj[QStringLiteral("template")].toString();
55+
}
56+
57+
QJsonObject SessionConfig::QuickNoteScheme::toJson() const
58+
{
59+
QJsonObject jobj;
60+
61+
jobj[QStringLiteral("name")] = m_name;
62+
jobj[QStringLiteral("folder_path")] = m_folderPath;
63+
jobj[QStringLiteral("note_name")] = m_noteName;
64+
jobj[QStringLiteral("template")] = m_template;
65+
66+
return jobj;
67+
}
68+
4169
void SessionConfig::ExternalProgram::fromJson(const QJsonObject &p_jobj)
4270
{
4371
m_name = p_jobj[QStringLiteral("name")].toString();
@@ -97,6 +125,8 @@ void SessionConfig::init()
97125

98126
loadHistory(sessionJobj);
99127

128+
loadQuickNoteSchemes(sessionJobj);
129+
100130
if (MainConfig::isVersionChanged()) {
101131
doVersionSpecificOverride();
102132
}
@@ -235,6 +265,7 @@ QJsonObject SessionConfig::toJson() const
235265
writeByteArray(obj, QStringLiteral("notebook_explorer_session"), m_notebookExplorerSession);
236266
obj[QStringLiteral("external_programs")] = saveExternalPrograms();
237267
obj[QStringLiteral("history")] = saveHistory();
268+
obj[QStringLiteral("quick_note_schemes")] = saveQuickNoteSchemes();
238269
return obj;
239270
}
240271

@@ -458,6 +489,24 @@ QJsonArray SessionConfig::saveExternalPrograms() const
458489
return arr;
459490
}
460491

492+
void SessionConfig::loadQuickNoteSchemes(const QJsonObject &p_session)
493+
{
494+
const auto arr = p_session.value(QStringLiteral("quick_note_schemes")).toArray();
495+
m_quickNoteSchemes.resize(arr.size());
496+
for (int i = 0; i < arr.size(); ++i) {
497+
m_quickNoteSchemes[i].fromJson(arr[i].toObject());
498+
}
499+
}
500+
501+
QJsonArray SessionConfig::saveQuickNoteSchemes() const
502+
{
503+
QJsonArray arr;
504+
for (const auto &scheme : m_quickNoteSchemes) {
505+
arr.append(scheme.toJson());
506+
}
507+
return arr;
508+
}
509+
461510
const QVector<SessionConfig::ExternalProgram> &SessionConfig::getExternalPrograms() const
462511
{
463512
return m_externalPrograms;
@@ -541,3 +590,13 @@ QJsonObject SessionConfig::saveExportOption() const
541590

542591
return obj;
543592
}
593+
594+
const QVector<SessionConfig::QuickNoteScheme> &SessionConfig::getQuickNoteSchemes() const
595+
{
596+
return m_quickNoteSchemes;
597+
}
598+
599+
void SessionConfig::setQuickNoteSchemes(const QVector<QuickNoteScheme>& p_schemes)
600+
{
601+
updateConfig(m_quickNoteSchemes, p_schemes, this);
602+
}

src/core/sessionconfig.h

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,25 @@ namespace vnotex
5555
QByteArray m_locationListState;
5656
};
5757

58+
struct QuickNoteScheme
59+
{
60+
bool operator==(const QuickNoteScheme &p_other) const;
61+
62+
void fromJson(const QJsonObject &p_jobj);
63+
64+
QJsonObject toJson() const;
65+
66+
QString m_name;
67+
68+
// Where to create the quick note.
69+
QString m_folderPath;
70+
71+
// Name of the quick note. Snippet is supported.
72+
QString m_noteName;
73+
74+
QString m_template;
75+
};
76+
5877
enum OpenGL
5978
{
6079
None,
@@ -149,6 +168,9 @@ namespace vnotex
149168
void removeHistory(const QString &p_itemPath);
150169
void clearHistory();
151170

171+
const QVector<QuickNoteScheme> &getQuickNoteSchemes() const;
172+
void setQuickNoteSchemes(const QVector<QuickNoteScheme>& p_schemes);
173+
152174
private:
153175
void loadCore(const QJsonObject &p_session);
154176

@@ -166,6 +188,10 @@ namespace vnotex
166188

167189
QJsonArray saveExternalPrograms() const;
168190

191+
void loadQuickNoteSchemes(const QJsonObject &p_session);
192+
193+
QJsonArray saveQuickNoteSchemes() const;
194+
169195
void doVersionSpecificOverride();
170196

171197
void loadHistory(const QJsonObject &p_session);
@@ -215,7 +241,9 @@ namespace vnotex
215241
QVector<HistoryItem> m_history;
216242

217243
// Default folder path to open for external media like images and files.
218-
QString m_externalMediaDefaultPath;;
244+
QString m_externalMediaDefaultPath;
245+
246+
QVector<QuickNoteScheme> m_quickNoteSchemes;
219247
};
220248
} // ns vnotex
221249

src/core/templatemgr.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include <QDir>
44

5+
#include <utils/fileutils.h>
6+
57
#include "configmgr.h"
68

79
using namespace vnotex;
@@ -20,5 +22,17 @@ QStringList TemplateMgr::getTemplates() const
2022

2123
QString TemplateMgr::getTemplateFilePath(const QString &p_name) const
2224
{
25+
if (p_name.isEmpty()) {
26+
return QString();
27+
}
2328
return QDir(getTemplateFolder()).filePath(p_name);
2429
}
30+
31+
QString TemplateMgr::getTemplateContent(const QString &p_name) const
32+
{
33+
const auto filePath = getTemplateFilePath(p_name);
34+
if (filePath.isEmpty()) {
35+
return QString();
36+
}
37+
return FileUtils::readTextFile(filePath);
38+
}

src/core/templatemgr.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ namespace vnotex
2424

2525
QString getTemplateFilePath(const QString &p_name) const;
2626

27+
QString getTemplateContent(const QString &p_name) const;
28+
2729
private:
2830
TemplateMgr() = default;
2931
};

src/core/vnotex.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ namespace vnotex
7979
// The handler should determine in which folder this note belongs to.
8080
void newNoteRequested();
8181

82+
// Requested to new a quick note (maybe in current folder).
83+
void newQuickNoteRequested();
84+
8285
// Requested to new a folder in current notebook.
8386
void newFolderRequested();
8487

src/widgets/dialogs/newnotedialog.cpp

Lines changed: 40 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <utils/fileutils.h>
1515
#include "exception.h"
1616
#include "nodeinfowidget.h"
17+
#include "notetemplateselector.h"
1718
#include <utils/widgetutils.h>
1819
#include <core/templatemgr.h>
1920
#include <core/configmgr.h>
@@ -43,27 +44,8 @@ void NewNoteDialog::setupUI(const Node *p_node)
4344

4445
auto infoLayout = m_infoWidget->getMainLayout();
4546

46-
{
47-
auto templateLayout = new QHBoxLayout();
48-
templateLayout->setContentsMargins(0, 0, 0, 0);
49-
infoLayout->addRow(tr("Template:"), templateLayout);
50-
51-
setupTemplateComboBox(m_infoWidget);
52-
templateLayout->addWidget(m_templateComboBox);
53-
54-
templateLayout->addStretch();
55-
56-
auto manageBtn = new QPushButton(tr("Manage"), m_infoWidget);
57-
templateLayout->addWidget(manageBtn);
58-
connect(manageBtn, &QPushButton::clicked,
59-
this, []() {
60-
WidgetUtils::openUrlByDesktop(QUrl::fromLocalFile(TemplateMgr::getInst().getTemplateFolder()));
61-
});
62-
63-
m_templateTextEdit = WidgetsFactory::createPlainTextConsole(m_infoWidget);
64-
infoLayout->addRow("", m_templateTextEdit);
65-
m_templateTextEdit->hide();
66-
}
47+
m_templateSelector = new NoteTemplateSelector(m_infoWidget);
48+
infoLayout->addRow(tr("Template:"), m_templateSelector);
6749

6850
setDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
6951

@@ -106,41 +88,56 @@ bool NewNoteDialog::validateNameInput(QString &p_msg)
10688

10789
void NewNoteDialog::acceptedButtonClicked()
10890
{
109-
s_lastTemplate = m_templateComboBox->currentData().toString();
91+
s_lastTemplate = m_templateSelector->getCurrentTemplate();
11092

11193
{
11294
auto fileType = FileTypeHelper::getInst().getFileTypeByName(m_infoWidget->getFileType()).m_type;
11395
ConfigMgr::getInst().getWidgetConfig().setNewNoteDefaultFileType(static_cast<int>(fileType));
11496
}
11597

116-
if (validateInputs() && newNote()) {
98+
if (validateInputs()) {
99+
Notebook *notebook = const_cast<Notebook *>(m_infoWidget->getNotebook());
100+
Node *parentNode = const_cast<Node *>(m_infoWidget->getParentNode());
101+
QString errMsg;
102+
m_newNode = newNote(notebook,
103+
parentNode,
104+
m_infoWidget->getName(),
105+
m_templateSelector->getTemplateContent(),
106+
errMsg);
107+
if (!m_newNode) {
108+
setInformationText(errMsg, ScrollDialog::InformationLevel::Error);
109+
return;
110+
}
117111
accept();
118112
}
119113
}
120114

121-
bool NewNoteDialog::newNote()
115+
QSharedPointer<Node> NewNoteDialog::newNote(Notebook *p_notebook,
116+
Node *p_parentNode,
117+
const QString &p_name,
118+
const QString &p_templateContent,
119+
QString &p_errMsg)
122120
{
123-
m_newNode.clear();
121+
Q_ASSERT(p_notebook && p_parentNode);
122+
123+
QSharedPointer<Node> newNode;
124+
p_errMsg.clear();
124125

125-
Notebook *notebook = const_cast<Notebook *>(m_infoWidget->getNotebook());
126-
Node *parentNode = const_cast<Node *>(m_infoWidget->getParentNode());
127126
try {
128-
m_newNode = notebook->newNode(parentNode,
127+
newNode = p_notebook->newNode(p_parentNode,
129128
Node::Flag::Content,
130-
m_infoWidget->getName(),
131-
getTemplateContent());
129+
p_name,
130+
evaluateTemplateContent(p_templateContent, p_name));
132131
} catch (Exception &p_e) {
133-
QString msg = tr("Failed to create note under (%1) in (%2) (%3).").arg(parentNode->getName(),
134-
notebook->getName(),
135-
p_e.what());
136-
qCritical() << msg;
137-
setInformationText(msg, ScrollDialog::InformationLevel::Error);
138-
return false;
132+
p_errMsg = tr("Failed to create note under (%1) in (%2) (%3).").arg(p_parentNode->getName(),
133+
p_notebook->getName(),
134+
p_e.what());
135+
qCritical() << p_errMsg;
136+
return nullptr;
139137
}
140138

141-
emit notebook->nodeUpdated(m_newNode.data());
142-
143-
return true;
139+
emit p_notebook->nodeUpdated(newNode.data());
140+
return newNode;
144141
}
145142

146143
const QSharedPointer<Node> &NewNoteDialog::getNewNode() const
@@ -166,66 +163,17 @@ void NewNoteDialog::initDefaultValues(const Node *p_node)
166163

167164
if (!s_lastTemplate.isEmpty()) {
168165
// Restore.
169-
int idx = m_templateComboBox->findData(s_lastTemplate);
170-
if (idx != -1) {
171-
m_templateComboBox->setCurrentIndex(idx);
172-
} else {
166+
if (!m_templateSelector->setCurrentTemplate(s_lastTemplate)) {
173167
s_lastTemplate.clear();
174168
}
175169
}
176170
}
177171

178-
void NewNoteDialog::setupTemplateComboBox(QWidget *p_parent)
179-
{
180-
m_templateComboBox = WidgetsFactory::createComboBox(p_parent);
181-
182-
// None.
183-
m_templateComboBox->addItem(tr("None"), "");
184-
185-
int idx = 1;
186-
auto templates = TemplateMgr::getInst().getTemplates();
187-
for (const auto &temp : templates) {
188-
m_templateComboBox->addItem(temp, temp);
189-
m_templateComboBox->setItemData(idx++, temp, Qt::ToolTipRole);
190-
}
191-
192-
m_templateComboBox->setCurrentIndex(0);
193-
194-
connect(m_templateComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
195-
this, &NewNoteDialog::updateCurrentTemplate);
196-
}
197-
198-
QString NewNoteDialog::getTemplateContent() const
172+
QString NewNoteDialog::evaluateTemplateContent(const QString &p_content, const QString &p_name)
199173
{
200174
int cursorOffset = 0;
201-
return SnippetMgr::getInst().applySnippetBySymbol(m_templateContent,
175+
return SnippetMgr::getInst().applySnippetBySymbol(p_content,
202176
QString(),
203177
cursorOffset,
204-
SnippetMgr::generateOverrides(m_infoWidget->getName()));
205-
}
206-
207-
void NewNoteDialog::updateCurrentTemplate()
208-
{
209-
m_templateContent.clear();
210-
m_templateTextEdit->clear();
211-
212-
auto temp = m_templateComboBox->currentData().toString();
213-
if (temp.isEmpty()) {
214-
m_templateTextEdit->hide();
215-
return;
216-
}
217-
218-
const auto filePath = TemplateMgr::getInst().getTemplateFilePath(temp);
219-
try {
220-
m_templateContent = FileUtils::readTextFile(filePath);
221-
m_templateTextEdit->setPlainText(m_templateContent);
222-
m_templateTextEdit->show();
223-
} catch (Exception &p_e) {
224-
m_templateTextEdit->hide();
225-
226-
QString msg = tr("Failed to load template (%1) (%2).")
227-
.arg(filePath, p_e.what());
228-
qCritical() << msg;
229-
setInformationText(msg, ScrollDialog::InformationLevel::Error);
230-
}
178+
SnippetMgr::generateOverrides(p_name));
231179
}

0 commit comments

Comments
 (0)