Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QuickAccess: add quick note #2373

Merged
merged 9 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/core/notebookconfigmgr/vxnotebookconfigmgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,10 @@ void VXNotebookConfigMgr::addChildNode(Node *p_parent, const QSharedPointer<Node
QSharedPointer<Node> VXNotebookConfigMgr::loadNodeByPath(const QSharedPointer<Node> &p_root, const QString &p_relativePath)
{
auto p = PathUtils::cleanPath(p_relativePath);
if (p == ".") {
return p_root;
}

auto paths = p.split('/', QString::SkipEmptyParts);
auto node = p_root;
for (auto &pa : paths) {
Expand Down
59 changes: 59 additions & 0 deletions src/core/sessionconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,34 @@ QJsonObject SessionConfig::NotebookItem::toJson() const
return jobj;
}

bool SessionConfig::QuickNoteScheme::operator==(const QuickNoteScheme &p_other) const
{
return m_name == p_other.m_name &&
m_folderPath == p_other.m_folderPath &&
m_noteName == p_other.m_noteName &&
m_template == p_other.m_template;
}

void SessionConfig::QuickNoteScheme::fromJson(const QJsonObject &p_jobj)
{
m_name = p_jobj[QStringLiteral("name")].toString();
m_folderPath = p_jobj[QStringLiteral("folder_path")].toString();
m_noteName = p_jobj[QStringLiteral("note_name")].toString();
m_template = p_jobj[QStringLiteral("template")].toString();
}

QJsonObject SessionConfig::QuickNoteScheme::toJson() const
{
QJsonObject jobj;

jobj[QStringLiteral("name")] = m_name;
jobj[QStringLiteral("folder_path")] = m_folderPath;
jobj[QStringLiteral("note_name")] = m_noteName;
jobj[QStringLiteral("template")] = m_template;

return jobj;
}

void SessionConfig::ExternalProgram::fromJson(const QJsonObject &p_jobj)
{
m_name = p_jobj[QStringLiteral("name")].toString();
Expand Down Expand Up @@ -97,6 +125,8 @@ void SessionConfig::init()

loadHistory(sessionJobj);

loadQuickNoteSchemes(sessionJobj);

if (MainConfig::isVersionChanged()) {
doVersionSpecificOverride();
}
Expand Down Expand Up @@ -235,6 +265,7 @@ QJsonObject SessionConfig::toJson() const
writeByteArray(obj, QStringLiteral("notebook_explorer_session"), m_notebookExplorerSession);
obj[QStringLiteral("external_programs")] = saveExternalPrograms();
obj[QStringLiteral("history")] = saveHistory();
obj[QStringLiteral("quick_note_schemes")] = saveQuickNoteSchemes();
return obj;
}

Expand Down Expand Up @@ -458,6 +489,24 @@ QJsonArray SessionConfig::saveExternalPrograms() const
return arr;
}

void SessionConfig::loadQuickNoteSchemes(const QJsonObject &p_session)
{
const auto arr = p_session.value(QStringLiteral("quick_note_schemes")).toArray();
m_quickNoteSchemes.resize(arr.size());
for (int i = 0; i < arr.size(); ++i) {
m_quickNoteSchemes[i].fromJson(arr[i].toObject());
}
}

QJsonArray SessionConfig::saveQuickNoteSchemes() const
{
QJsonArray arr;
for (const auto &scheme : m_quickNoteSchemes) {
arr.append(scheme.toJson());
}
return arr;
}

const QVector<SessionConfig::ExternalProgram> &SessionConfig::getExternalPrograms() const
{
return m_externalPrograms;
Expand Down Expand Up @@ -541,3 +590,13 @@ QJsonObject SessionConfig::saveExportOption() const

return obj;
}

const QVector<SessionConfig::QuickNoteScheme> &SessionConfig::getQuickNoteSchemes() const
{
return m_quickNoteSchemes;
}

void SessionConfig::setQuickNoteSchemes(const QVector<QuickNoteScheme>& p_schemes)
{
updateConfig(m_quickNoteSchemes, p_schemes, this);
}
30 changes: 29 additions & 1 deletion src/core/sessionconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,25 @@ namespace vnotex
QByteArray m_locationListState;
};

struct QuickNoteScheme
{
bool operator==(const QuickNoteScheme &p_other) const;

void fromJson(const QJsonObject &p_jobj);

QJsonObject toJson() const;

QString m_name;

// Where to create the quick note.
QString m_folderPath;

// Name of the quick note. Snippet is supported.
QString m_noteName;

QString m_template;
};

enum OpenGL
{
None,
Expand Down Expand Up @@ -149,6 +168,9 @@ namespace vnotex
void removeHistory(const QString &p_itemPath);
void clearHistory();

const QVector<QuickNoteScheme> &getQuickNoteSchemes() const;
void setQuickNoteSchemes(const QVector<QuickNoteScheme>& p_schemes);

private:
void loadCore(const QJsonObject &p_session);

Expand All @@ -166,6 +188,10 @@ namespace vnotex

QJsonArray saveExternalPrograms() const;

void loadQuickNoteSchemes(const QJsonObject &p_session);

QJsonArray saveQuickNoteSchemes() const;

void doVersionSpecificOverride();

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

// Default folder path to open for external media like images and files.
QString m_externalMediaDefaultPath;;
QString m_externalMediaDefaultPath;

QVector<QuickNoteScheme> m_quickNoteSchemes;
};
} // ns vnotex

Expand Down
14 changes: 14 additions & 0 deletions src/core/templatemgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <QDir>

#include <utils/fileutils.h>

#include "configmgr.h"

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

QString TemplateMgr::getTemplateFilePath(const QString &p_name) const
{
if (p_name.isEmpty()) {
return QString();
}
return QDir(getTemplateFolder()).filePath(p_name);
}

QString TemplateMgr::getTemplateContent(const QString &p_name) const
{
const auto filePath = getTemplateFilePath(p_name);
if (filePath.isEmpty()) {
return QString();
}
return FileUtils::readTextFile(filePath);
}
2 changes: 2 additions & 0 deletions src/core/templatemgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ namespace vnotex

QString getTemplateFilePath(const QString &p_name) const;

QString getTemplateContent(const QString &p_name) const;

private:
TemplateMgr() = default;
};
Expand Down
3 changes: 3 additions & 0 deletions src/core/vnotex.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ namespace vnotex
// The handler should determine in which folder this note belongs to.
void newNoteRequested();

// Requested to new a quick note (maybe in current folder).
void newQuickNoteRequested();

// Requested to new a folder in current notebook.
void newFolderRequested();

Expand Down
132 changes: 40 additions & 92 deletions src/widgets/dialogs/newnotedialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <utils/fileutils.h>
#include "exception.h"
#include "nodeinfowidget.h"
#include "notetemplateselector.h"
#include <utils/widgetutils.h>
#include <core/templatemgr.h>
#include <core/configmgr.h>
Expand Down Expand Up @@ -43,27 +44,8 @@ void NewNoteDialog::setupUI(const Node *p_node)

auto infoLayout = m_infoWidget->getMainLayout();

{
auto templateLayout = new QHBoxLayout();
templateLayout->setContentsMargins(0, 0, 0, 0);
infoLayout->addRow(tr("Template:"), templateLayout);

setupTemplateComboBox(m_infoWidget);
templateLayout->addWidget(m_templateComboBox);

templateLayout->addStretch();

auto manageBtn = new QPushButton(tr("Manage"), m_infoWidget);
templateLayout->addWidget(manageBtn);
connect(manageBtn, &QPushButton::clicked,
this, []() {
WidgetUtils::openUrlByDesktop(QUrl::fromLocalFile(TemplateMgr::getInst().getTemplateFolder()));
});

m_templateTextEdit = WidgetsFactory::createPlainTextConsole(m_infoWidget);
infoLayout->addRow("", m_templateTextEdit);
m_templateTextEdit->hide();
}
m_templateSelector = new NoteTemplateSelector(m_infoWidget);
infoLayout->addRow(tr("Template:"), m_templateSelector);

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

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

void NewNoteDialog::acceptedButtonClicked()
{
s_lastTemplate = m_templateComboBox->currentData().toString();
s_lastTemplate = m_templateSelector->getCurrentTemplate();

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

if (validateInputs() && newNote()) {
if (validateInputs()) {
Notebook *notebook = const_cast<Notebook *>(m_infoWidget->getNotebook());
Node *parentNode = const_cast<Node *>(m_infoWidget->getParentNode());
QString errMsg;
m_newNode = newNote(notebook,
parentNode,
m_infoWidget->getName(),
m_templateSelector->getTemplateContent(),
errMsg);
if (!m_newNode) {
setInformationText(errMsg, ScrollDialog::InformationLevel::Error);
return;
}
accept();
}
}

bool NewNoteDialog::newNote()
QSharedPointer<Node> NewNoteDialog::newNote(Notebook *p_notebook,
Node *p_parentNode,
const QString &p_name,
const QString &p_templateContent,
QString &p_errMsg)
{
m_newNode.clear();
Q_ASSERT(p_notebook && p_parentNode);

QSharedPointer<Node> newNode;
p_errMsg.clear();

Notebook *notebook = const_cast<Notebook *>(m_infoWidget->getNotebook());
Node *parentNode = const_cast<Node *>(m_infoWidget->getParentNode());
try {
m_newNode = notebook->newNode(parentNode,
newNode = p_notebook->newNode(p_parentNode,
Node::Flag::Content,
m_infoWidget->getName(),
getTemplateContent());
p_name,
evaluateTemplateContent(p_templateContent, p_name));
} catch (Exception &p_e) {
QString msg = tr("Failed to create note under (%1) in (%2) (%3).").arg(parentNode->getName(),
notebook->getName(),
p_e.what());
qCritical() << msg;
setInformationText(msg, ScrollDialog::InformationLevel::Error);
return false;
p_errMsg = tr("Failed to create note under (%1) in (%2) (%3).").arg(p_parentNode->getName(),
p_notebook->getName(),
p_e.what());
qCritical() << p_errMsg;
return nullptr;
}

emit notebook->nodeUpdated(m_newNode.data());

return true;
emit p_notebook->nodeUpdated(newNode.data());
return newNode;
}

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

if (!s_lastTemplate.isEmpty()) {
// Restore.
int idx = m_templateComboBox->findData(s_lastTemplate);
if (idx != -1) {
m_templateComboBox->setCurrentIndex(idx);
} else {
if (!m_templateSelector->setCurrentTemplate(s_lastTemplate)) {
s_lastTemplate.clear();
}
}
}

void NewNoteDialog::setupTemplateComboBox(QWidget *p_parent)
{
m_templateComboBox = WidgetsFactory::createComboBox(p_parent);

// None.
m_templateComboBox->addItem(tr("None"), "");

int idx = 1;
auto templates = TemplateMgr::getInst().getTemplates();
for (const auto &temp : templates) {
m_templateComboBox->addItem(temp, temp);
m_templateComboBox->setItemData(idx++, temp, Qt::ToolTipRole);
}

m_templateComboBox->setCurrentIndex(0);

connect(m_templateComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
this, &NewNoteDialog::updateCurrentTemplate);
}

QString NewNoteDialog::getTemplateContent() const
QString NewNoteDialog::evaluateTemplateContent(const QString &p_content, const QString &p_name)
{
int cursorOffset = 0;
return SnippetMgr::getInst().applySnippetBySymbol(m_templateContent,
return SnippetMgr::getInst().applySnippetBySymbol(p_content,
QString(),
cursorOffset,
SnippetMgr::generateOverrides(m_infoWidget->getName()));
}

void NewNoteDialog::updateCurrentTemplate()
{
m_templateContent.clear();
m_templateTextEdit->clear();

auto temp = m_templateComboBox->currentData().toString();
if (temp.isEmpty()) {
m_templateTextEdit->hide();
return;
}

const auto filePath = TemplateMgr::getInst().getTemplateFilePath(temp);
try {
m_templateContent = FileUtils::readTextFile(filePath);
m_templateTextEdit->setPlainText(m_templateContent);
m_templateTextEdit->show();
} catch (Exception &p_e) {
m_templateTextEdit->hide();

QString msg = tr("Failed to load template (%1) (%2).")
.arg(filePath, p_e.what());
qCritical() << msg;
setInformationText(msg, ScrollDialog::InformationLevel::Error);
}
SnippetMgr::generateOverrides(p_name));
}
Loading
Loading