Commit 0e2e5da3 authored by Laurent Montel's avatar Laurent Montel 😁
Browse files

Adapt patch for adding Heading

Based on patch from Igor Poboiko
parent ff4ab027
cmake_minimum_required(VERSION 3.5)
set(PIM_VERSION "5.14.41")
set(PIM_VERSION "5.14.42")
project(KPimTextEdit VERSION ${PIM_VERSION})
......
......@@ -85,7 +85,8 @@ void RichTextComposerActionsTest::shouldHaveActions()
<< QStringLiteral("insert_table")
<< QStringLiteral("delete_line")
<< QStringLiteral("format_reset")
<< QStringLiteral("format_painter");
<< QStringLiteral("format_painter")
<< QStringLiteral("format_heading_level");
QStringList actionNoRichText;
actionNoRichText << QStringLiteral("paste_quoted")
......
......@@ -404,6 +404,19 @@ void RichTextComposer::evaluateListSupport(QKeyEvent *event)
KPIMTextEdit::RichTextEditor::keyPressEvent(event);
}
// Match the behavior of office suites: newline after header switches to normal text
if ((event->key() == Qt::Key_Return)
&& (textCursor().blockFormat().headingLevel() > 0)
&& (textCursor().atBlockEnd())) {
d->composerControler->setHeadingLevel(0);
}
// If a line was merged with previous one, with different heading level,
// the style should also be adjusted accordingly (i.e. merged)
if ((event->key() == Qt::Key_Backspace) || (event->key() == Qt::Key_Delete)) {
d->composerControler->setHeadingLevel(textCursor().blockFormat().headingLevel());
}
if (textCursor().currentList()) {
d->composerControler->nestedListHelper()->handleAfterKeyPressEvent(event);
}
......
......@@ -85,6 +85,7 @@ public:
QAction *action_format_reset = nullptr;
KToggleAction *action_format_painter = nullptr;
KSelectAction *action_heading_level = nullptr;
bool richTextEnabled = false;
};
......@@ -464,6 +465,28 @@ void RichTextComposerActions::createActions(KActionCollection *ac)
if (ac) {
ac->addAction(QStringLiteral("format_painter"), d->action_format_painter);
}
d->action_heading_level = new KSelectAction(i18nc("@title:menu", "Heading Level"), this);
const QStringList headingLevels = {i18nc("@item:inmenu no heading", "Basic text"),
i18nc("@item:inmenu heading level 1 (largest)", "Title"),
i18nc("@item:inmenu heading level 2", "Subtitle"),
i18nc("@item:inmenu heading level 3", "Section"),
i18nc("@item:inmenu heading level 4", "Subsection"),
i18nc("@item:inmenu heading level 5", "Paragraph"),
i18nc("@item:inmenu heading level 6 (smallest)", "Subparagraph")};
d->action_heading_level->setItems(headingLevels);
d->action_heading_level->setCurrentItem(0);
d->richTextActionList.append(d->action_heading_level);
d->action_heading_level->setObjectName(QStringLiteral("format_heading_level"));
connect(d->action_heading_level, QOverload<int>::of(&KSelectAction::triggered),
this, &RichTextComposerActions::setHeadingLevel);
if (ac) {
ac->addAction(QStringLiteral("format_heading_level"), d->action_heading_level);
}
connect(d->action_format_painter, &QAction::toggled,
d->composerControler, &RichTextComposerControler::slotFormatPainter);
......@@ -486,6 +509,13 @@ void RichTextComposerActions::updateActionStates()
slotUpdateCharFormatActions(d->composerControler->richTextComposer()->currentCharFormat());
}
void RichTextComposerActions::setHeadingLevel(int level)
{
d->composerControler->setHeadingLevel(level);
//Needed ?
slotUpdateMiscActions();
}
void RichTextComposerActions::setListStyle(int _styleindex)
{
d->composerControler->setListStyle(_styleindex);
......@@ -551,6 +581,7 @@ void RichTextComposerActions::slotUpdateMiscActions()
const Qt::LayoutDirection direction = d->composerControler->richTextComposer()->textCursor().blockFormat().layoutDirection();
d->action_direction_ltr->setChecked(direction == Qt::LeftToRight);
d->action_direction_rtl->setChecked(direction == Qt::RightToLeft);
d->action_heading_level->setCurrentItem(d->composerControler->richTextComposer()->textCursor().blockFormat().headingLevel());
}
void RichTextComposerActions::uncheckActionFormatPainter()
......
......@@ -55,6 +55,7 @@ private:
void slotUpdateCharFormatActions(const QTextCharFormat &format);
void slotUpdateMiscActions();
void setHeadingLevel(int level);
void setListStyle(int _styleindex);
class RichTextComposerActionsPrivate;
RichTextComposerActionsPrivate *const d;
......
......@@ -362,6 +362,46 @@ void RichTextComposerControler::setTextSubScript(bool subscript)
richTextComposer()->activateRichText();
}
void RichTextComposerControler::setHeadingLevel(int level)
{
const int boundedLevel = qBound(0, 6, level);
// Apparently, 5 is maximum for FontSizeAdjustment; otherwise level=1 and
// level=2 look the same
const int sizeAdjustment = boundedLevel > 0 ? 5 - boundedLevel: 0;
QTextCursor cursor = richTextComposer()->textCursor();
QTextBlockFormat blkfmt;
blkfmt.setHeadingLevel(boundedLevel);
cursor.mergeBlockFormat(blkfmt);
QTextCharFormat chrfmt;
chrfmt.setFontWeight(boundedLevel > 0 ? QFont::Bold : QFont::Normal);
chrfmt.setProperty(QTextFormat::FontSizeAdjustment, sizeAdjustment);
// Applying style to the current line or selection
QTextCursor selectCursor = cursor;
if (selectCursor.hasSelection()) {
QTextCursor top = selectCursor;
top.setPosition(qMin(top.anchor(), top.position()));
top.movePosition(QTextCursor::StartOfBlock);
QTextCursor bottom = selectCursor;
bottom.setPosition(qMax(bottom.anchor(), bottom.position()));
bottom.movePosition(QTextCursor::EndOfBlock);
selectCursor.setPosition(top.position(), QTextCursor::MoveAnchor);
selectCursor.setPosition(bottom.position(), QTextCursor::KeepAnchor);
} else {
selectCursor.select(QTextCursor::BlockUnderCursor);
}
selectCursor.mergeCharFormat(chrfmt);
cursor.mergeBlockCharFormat(chrfmt);
richTextComposer()->setTextCursor(cursor);
richTextComposer()->setFocus();
richTextComposer()->activateRichText();
}
void RichTextComposerControler::setChangeTextForegroundColor()
{
const QColor currentColor = richTextComposer()->textColor();
......
......@@ -69,6 +69,7 @@ public:
void addQuotes(const QString &defaultQuote);
public Q_SLOTS:
void insertHorizontalRule();
void setHeadingLevel(int level);
void alignLeft();
void alignCenter();
void alignRight();
......
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