Commit fb117ee6 authored by Igor Poboiko's avatar Igor Poboiko
Browse files

Add support for QTextBlockFormat::marker

This patch enables use of QTextBlockFormat::marker introduced in Qt 5.14.
Using it, any list element can be marked with interactive checkbox.

It adds a `KToggleAction` with which user can toggle this checkbox.

Brief explanation of the logic:
 - If the cursor is already on a list, we just toggle the marker
 - If the cursor is not on a list, we have to add a list first
   (having a marker without a list just does nothing)
 - If the cursor was on the single-element list, and we have disabled
   the marker, disable the list too.
parent 64344901
Pipeline #21246 passed with stage
in 21 minutes and 42 seconds
......@@ -86,6 +86,7 @@ public:
KToggleAction *action_format_painter = nullptr;
KSelectAction *action_heading_level = nullptr;
KToggleAction *action_list_checkbox = nullptr;
bool richTextEnabled = false;
};
......@@ -487,6 +488,18 @@ void RichTextComposerActions::createActions(KActionCollection *ac)
ac->addAction(QStringLiteral("format_heading_level"), d->action_heading_level);
}
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
d->action_list_checkbox = new KToggleAction(QIcon::fromTheme(QStringLiteral("checkbox")),
i18nc("@action", "Checkbox"), this);
d->richTextActionList.append(d->action_list_checkbox);
d->action_list_checkbox->setObjectName(QStringLiteral("format_list_checkbox"));
connect(d->action_list_checkbox, &KToggleAction::toggled,
d->composerControler, &RichTextComposerControler::addCheckbox);
if (ac) {
ac->addAction(QStringLiteral("format_list_checkbox"), d->action_list_checkbox);
}
#endif
disconnect(d->composerControler->richTextComposer(), &QTextEdit::currentCharFormatChanged,
this, &RichTextComposerActions::slotUpdateCharFormatActions);
disconnect(d->composerControler->richTextComposer(), &QTextEdit::cursorPositionChanged,
......@@ -576,6 +589,9 @@ void RichTextComposerActions::slotUpdateMiscActions()
d->action_direction_ltr->setChecked(direction == Qt::LeftToRight);
d->action_direction_rtl->setChecked(direction == Qt::RightToLeft);
d->action_heading_level->setCurrentItem(richTextComposer->textCursor().blockFormat().headingLevel());
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
d->action_list_checkbox->setChecked(richTextComposer->textCursor().blockFormat().marker() != QTextBlockFormat::MarkerType::NoMarker);
#endif
}
void RichTextComposerActions::uncheckActionFormatPainter()
......
......@@ -36,6 +36,7 @@
#include <QClipboard>
#include <QIcon>
#include <QTextDocumentFragment>
#include <QTextList>
#include "textutils.h"
#include "insertimagedialog.h"
......@@ -157,6 +158,28 @@ bool RichTextComposerControler::painterActive() const
return d->painterActive;
}
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
void RichTextComposerControler::addCheckbox(bool add)
{
QTextBlockFormat fmt;
fmt.setMarker(add ? QTextBlockFormat::MarkerType::Unchecked : QTextBlockFormat::MarkerType::NoMarker);
QTextCursor cursor = richTextComposer()->textCursor();
cursor.beginEditBlock();
if (add && !cursor.currentList()) {
// Checkbox only works with lists, so if we are not at list, add a new one
setListStyle(1);
} else if (!add && cursor.currentList() && cursor.currentList()->count() == 1) {
// If this is a single-element list with a checkbox, and user disables
// a checkbox, assume user don't want a list too
// (so when cursor is not on a list, and enables checkbox and disables
// it right after, he returns to the same state with no list)
setListStyle(0);
}
cursor.mergeBlockFormat(fmt);
cursor.endEditBlock();
}
#endif
void RichTextComposerControler::setFontForWholeText(const QFont &font)
{
QTextCharFormat fmt;
......
......@@ -67,6 +67,9 @@ public:
Q_REQUIRED_RESULT QString toCleanPlainText(const QString &plainText = QString()) const;
void addQuotes(const QString &defaultQuote);
public Q_SLOTS:
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
void addCheckbox(bool add = true);
#endif
void insertHorizontalRule();
void setHeadingLevel(int level);
void alignLeft();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment