Skip to content

Commit

Permalink
src/gui/widgets/glossarylabel: avoid double bulleting struc content
Browse files Browse the repository at this point in the history
Prevent bullet mode from double bulleting structured content. In
Yomitan's new JMDict builds, they like to mix up standard strings with
line breaks and structured content to make dictionaries more useful.
Memento handles this poorly in bullet mode as it will wrap structured
content in a needly <ul>. This fixes the issue by not wrapping
structured content in <ul> in bullet mode.
  • Loading branch information
ripose-jp committed Jun 9, 2024
1 parent 906acb5 commit 43dc3d9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
52 changes: 38 additions & 14 deletions src/gui/widgets/definition/glossarylabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,20 @@ void GlossaryLabel::setContents(const QJsonArray &definitions, QString basepath)
basepath.prepend("file://");
basepath += '/';

const bool shouldUseBullets =
m_style == Constants::GlossaryStyle::Bullet &&
!containsStructuredContent(definitions);

QString content = "<html><head/><body>";
if (m_style == Constants::GlossaryStyle::Bullet)
if (shouldUseBullets)
{
content += "<ul>";
}
for (int i = 0; i < definitions.size(); ++i)
{
const QJsonValue &val = definitions[i];

if (m_style == Constants::GlossaryStyle::Bullet)
if (shouldUseBullets)
{
content += "<li>";
}
Expand All @@ -137,10 +141,10 @@ void GlossaryLabel::setContents(const QJsonArray &definitions, QString basepath)
.toString()
.replace(
'\n',
m_style == Constants::GlossaryStyle::Bullet ?
"</li><li>" : "<br>"
shouldUseBullets ? "</li><li>" : "<br>"
);
break;

case QJsonValue::Type::Object:
{
QJsonObject obj = val.toObject();
Expand All @@ -158,21 +162,29 @@ void GlossaryLabel::setContents(const QJsonArray &definitions, QString basepath)
}
break;
}

default:
break;
}

if (m_style == Constants::GlossaryStyle::Bullet)
if (shouldUseBullets)
{
content += "</li>";
}
else if (i < definitions.size() - 1)
else if (i >= definitions.size() - 1)
{
/* Avoid putting <br> or | after the fine line */
}
else if (m_style == Constants::GlossaryStyle::LineBreak)
{
content +=
m_style == Constants::GlossaryStyle::LineBreak ? "<br>" : " | ";
content += "<br>";
}
else if (m_style == Constants::GlossaryStyle::Pipe)
{
content += " | ";
}
}
if (m_style == Constants::GlossaryStyle::Bullet)
if (shouldUseBullets)
{
content += "</ul>";
}
Expand Down Expand Up @@ -546,6 +558,20 @@ void GlossaryLabel::addText(const QJsonObject &obj, QString &out) const
#undef KEY_TEXT

/* End Other Object Parsers */
/* Begin Helpers */

bool GlossaryLabel::containsStructuredContent(const QJsonArray &definitions)
{
return std::any_of(
std::begin(definitions), std::end(definitions),
[] (const QJsonValue &value) -> bool
{
return value.type() == QJsonValue::Object;
}
);
}

/* End Helpers */
/* Begin Event Handlers */

void GlossaryLabel::adjustSize()
Expand Down Expand Up @@ -591,11 +617,11 @@ void GlossaryLabel::resizeEvent(QResizeEvent *event)
adjustSize();
}

/* Prevents large searches from being executed and freezing everything up */
#define MAX_SEARCH_SIZE 40

void GlossaryLabel::mouseMoveEvent(QMouseEvent *event)
{
/* Prevents large searches from being executed and freezing everything up */
constexpr qsizetype MAX_SEARCH_SIZE{40};

QTextEdit::mouseMoveEvent(event);

if (!(QGuiApplication::keyboardModifiers() & m_searchModifier))
Expand Down Expand Up @@ -645,8 +671,6 @@ void GlossaryLabel::mouseMoveEvent(QMouseEvent *event)
QThreadPool::globalInstance()->start(worker);
}

#undef MAX_SEARCH_SIZE

void GlossaryLabel::mousePressEvent(QMouseEvent *event)
{
QTextEdit::mousePressEvent(event);
Expand Down
8 changes: 8 additions & 0 deletions src/gui/widgets/definition/glossarylabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,14 @@ private Q_SLOTS:
*/
void addText(const QJsonObject &obj, QString &out) const;

/**
* Determines if an array contains structured content.
* @param definitions The array of definitions.
* @return True if the array contains structured content, false otherwise.
*/
[[nodiscard]]
static bool containsStructuredContent(const QJsonArray &definitions);

/* The modifier that triggers searches */
Qt::KeyboardModifier m_searchModifier = Qt::KeyboardModifier::ShiftModifier;

Expand Down

0 comments on commit 43dc3d9

Please sign in to comment.