Skip to content

Commit

Permalink
#3181 linkdialog: use highlighting states of QOwnNotesMarkdownTextEdi…
Browse files Browse the repository at this point in the history
…t to get list of headings

Signed-off-by: Patrizio Bekerle <[email protected]>
  • Loading branch information
pbek committed Feb 2, 2025
1 parent 158eb5c commit 1ad4e4a
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 27 deletions.
11 changes: 9 additions & 2 deletions src/dialogs/linkdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "services/settingsservice.h"
#include "ui_linkdialog.h"
#include "widgets/navigationwidget.h"

LinkDialog::LinkDialog(int page, const QString &dialogTitle, QWidget *parent)
: MasterDialog(parent), ui(new Ui::LinkDialog) {
Expand All @@ -25,6 +26,7 @@ LinkDialog::LinkDialog(int page, const QString &dialogTitle, QWidget *parent)
ui->tabWidget->setCurrentIndex(page);
on_tabWidget_currentChanged(page);
ui->downloadProgressBar->hide();
_markdownTextEdit = new QOwnNotesMarkdownTextEdit();
_networkManager = new QNetworkAccessManager(this);
QObject::connect(_networkManager, SIGNAL(finished(QNetworkReply *)), this,
SLOT(slotReplyFinished(QNetworkReply *)));
Expand Down Expand Up @@ -65,7 +67,7 @@ LinkDialog::LinkDialog(int page, const QString &dialogTitle, QWidget *parent)
setupFileUrlMenu();
}

LinkDialog::~LinkDialog() { delete ui; }
LinkDialog::~LinkDialog() { delete ui; delete _markdownTextEdit; }

void LinkDialog::on_searchLineEdit_textChanged(const QString &arg1) {
// search notes when at least 2 characters were entered
Expand Down Expand Up @@ -397,9 +399,14 @@ void LinkDialog::on_headingSearchLineEdit_textChanged(const QString &arg1) {

void LinkDialog::loadNoteHeadings() const {
auto note = getSelectedNote();
_markdownTextEdit->setPlainText(note.getNoteText());
auto nodes = NavigationWidget::parseDocument(_markdownTextEdit->document());
QStringList headingTexts;
std::transform(nodes.begin(), nodes.end(), std::back_inserter(headingTexts),
[](const Node& node) { return node.text; });

ui->headingListWidget->clear();
ui->headingListWidget->addItems(note.getHeadingList());
ui->headingListWidget->addItems(headingTexts);
}

void LinkDialog::on_notesListWidget_currentItemChanged(QListWidgetItem *current,
Expand Down
2 changes: 2 additions & 0 deletions src/dialogs/linkdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <QNetworkAccessManager>

#include "masterdialog.h"
#include "widgets/qownnotesmarkdowntextedit.h"

namespace Ui {
class LinkDialog;
Expand Down Expand Up @@ -54,6 +55,7 @@ class LinkDialog : public MasterDialog {
bool eventFilter(QObject *obj, QEvent *event) override;
QString selectedNoteText;
QNetworkAccessManager *_networkManager;
QOwnNotesMarkdownTextEdit *_markdownTextEdit;
void setupFileUrlMenu();
void loadNoteHeadings() const;
void doAccept();
Expand Down
21 changes: 0 additions & 21 deletions src/entities/note.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4250,27 +4250,6 @@ QVector<CommandSnippet> Note::getParsedCommandSnippets() const {

void Note::resetNoteTextHtmlConversionHash() { _noteTextHtmlConversionHash = QLatin1String(""); }

/**
* Get a list of all headings in a note starting with #
*
* @return
*/
QStringList Note::getHeadingList() {
QStringList headingList;

static const QRegularExpression re(QStringLiteral(R"(^#+ (.+)$)"),
QRegularExpression::MultilineOption);
QRegularExpressionMatchIterator i = re.globalMatch(_noteText);

while (i.hasNext()) {
QRegularExpressionMatch match = i.next();
// Trim the heading text, in case there are trailing carriage return characters leaking in Windows
headingList << match.captured(1).trimmed();
}

return headingList;
}

bool Note::applyIgnoredNotesSetting(QStringList &fileNames) {
const SettingsService settings;
const QStringList ignoredFileRegExpList =
Expand Down
2 changes: 0 additions & 2 deletions src/entities/note.h
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,6 @@ class Note {

static QString removeNameSearchPrefix(QString searchTerm);

QStringList getHeadingList();

static bool applyIgnoredNotesSetting(QStringList &fileNames);

QSet<Note> findBacklinks() const;
Expand Down
3 changes: 2 additions & 1 deletion src/widgets/navigationwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ QVector<Node> NavigationWidget::parseDocument(const QTextDocument *const documen
continue;
}
static const QRegularExpression re(QStringLiteral("^#+\\s+"));
QString text = block.text().remove(re);
// Trim the heading text, in case there are trailing carriage return characters leaking in Windows
QString text = block.text().remove(re).trimmed();

if (text.isEmpty()) {
continue;
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/qownnotesmarkdowntextedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ QOwnNotesMarkdownTextEdit::QOwnNotesMarkdownTextEdit(QWidget *parent)
// We need to set the internal variable to true, because we start with a highlighter
_highlightingEnabled = true;
_highlighter = nullptr;
if (parent->objectName() != QStringLiteral("LogWidget")) {
if (!parent || parent->objectName() != QStringLiteral("LogWidget")) {
_highlighter = new QOwnNotesMarkdownHighlighter(document());

setStyles();
Expand Down

0 comments on commit 1ad4e4a

Please sign in to comment.