From 0df30ff22e50aa301791fc72f106ab15ce385a6a Mon Sep 17 00:00:00 2001 From: Shawn Rutledge Date: Mon, 18 Feb 2019 15:40:04 +0100 Subject: Add task list checkbox manipulation features to the TextEdit example Change-Id: I5f0b8cb94d1af609ec531f9765d58be65b1129a3 Reviewed-by: Gatis Paeglis --- .../textedit/images/mac/checkbox-checked.png | Bin 0 -> 1167 bytes .../richtext/textedit/images/mac/checkbox.png | Bin 0 -> 779 bytes .../textedit/images/win/checkbox-checked.png | Bin 0 -> 1167 bytes .../richtext/textedit/images/win/checkbox.png | Bin 0 -> 779 bytes examples/widgets/richtext/textedit/textedit.cpp | 64 ++++++++++++++++++--- examples/widgets/richtext/textedit/textedit.h | 2 + examples/widgets/richtext/textedit/textedit.qrc | 4 ++ 7 files changed, 61 insertions(+), 9 deletions(-) create mode 100644 examples/widgets/richtext/textedit/images/mac/checkbox-checked.png create mode 100644 examples/widgets/richtext/textedit/images/mac/checkbox.png create mode 100644 examples/widgets/richtext/textedit/images/win/checkbox-checked.png create mode 100644 examples/widgets/richtext/textedit/images/win/checkbox.png (limited to 'examples/widgets/richtext') diff --git a/examples/widgets/richtext/textedit/images/mac/checkbox-checked.png b/examples/widgets/richtext/textedit/images/mac/checkbox-checked.png new file mode 100644 index 0000000000..a072d7fb5c Binary files /dev/null and b/examples/widgets/richtext/textedit/images/mac/checkbox-checked.png differ diff --git a/examples/widgets/richtext/textedit/images/mac/checkbox.png b/examples/widgets/richtext/textedit/images/mac/checkbox.png new file mode 100644 index 0000000000..4064909712 Binary files /dev/null and b/examples/widgets/richtext/textedit/images/mac/checkbox.png differ diff --git a/examples/widgets/richtext/textedit/images/win/checkbox-checked.png b/examples/widgets/richtext/textedit/images/win/checkbox-checked.png new file mode 100644 index 0000000000..a072d7fb5c Binary files /dev/null and b/examples/widgets/richtext/textedit/images/win/checkbox-checked.png differ diff --git a/examples/widgets/richtext/textedit/images/win/checkbox.png b/examples/widgets/richtext/textedit/images/win/checkbox.png new file mode 100644 index 0000000000..4064909712 Binary files /dev/null and b/examples/widgets/richtext/textedit/images/win/checkbox.png differ diff --git a/examples/widgets/richtext/textedit/textedit.cpp b/examples/widgets/richtext/textedit/textedit.cpp index 7b8c242cfc..683e441fce 100644 --- a/examples/widgets/richtext/textedit/textedit.cpp +++ b/examples/widgets/richtext/textedit/textedit.cpp @@ -343,6 +343,15 @@ void TextEdit::setupTextActions() actionTextColor = menu->addAction(pix, tr("&Color..."), this, &TextEdit::textColor); tb->addAction(actionTextColor); + menu->addSeparator(); + + const QIcon checkboxIcon = QIcon::fromTheme("status-checkbox-checked", QIcon(rsrcPath + "/checkbox-checked.png")); + actionToggleCheckState = menu->addAction(checkboxIcon, tr("Chec&ked"), this, &TextEdit::setChecked); + actionToggleCheckState->setShortcut(Qt::CTRL + Qt::Key_K); + actionToggleCheckState->setCheckable(true); + actionToggleCheckState->setPriority(QAction::LowPriority); + tb->addAction(actionToggleCheckState); + tb = addToolBar(tr("Format Actions")); tb->setAllowedAreas(Qt::TopToolBarArea | Qt::BottomToolBarArea); addToolBarBreak(Qt::TopToolBarArea); @@ -354,6 +363,8 @@ void TextEdit::setupTextActions() comboStyle->addItem("Bullet List (Disc)"); comboStyle->addItem("Bullet List (Circle)"); comboStyle->addItem("Bullet List (Square)"); + comboStyle->addItem("Task List (Unchecked)"); + comboStyle->addItem("Task List (Checked)"); comboStyle->addItem("Ordered List (Decimal)"); comboStyle->addItem("Ordered List (Alpha lower)"); comboStyle->addItem("Ordered List (Alpha upper)"); @@ -617,6 +628,7 @@ void TextEdit::textStyle(int styleIndex) { QTextCursor cursor = textEdit->textCursor(); QTextListFormat::Style style = QTextListFormat::ListStyleUndefined; + QTextBlockFormat::MarkerType marker = QTextBlockFormat::NoMarker; switch (styleIndex) { case 1: @@ -629,18 +641,32 @@ void TextEdit::textStyle(int styleIndex) style = QTextListFormat::ListSquare; break; case 4: - style = QTextListFormat::ListDecimal; + if (cursor.currentList()) + style = cursor.currentList()->format().style(); + else + style = QTextListFormat::ListDisc; + marker = QTextBlockFormat::Unchecked; break; case 5: - style = QTextListFormat::ListLowerAlpha; + if (cursor.currentList()) + style = cursor.currentList()->format().style(); + else + style = QTextListFormat::ListDisc; + marker = QTextBlockFormat::Checked; break; case 6: - style = QTextListFormat::ListUpperAlpha; + style = QTextListFormat::ListDecimal; break; case 7: - style = QTextListFormat::ListLowerRoman; + style = QTextListFormat::ListLowerAlpha; break; case 8: + style = QTextListFormat::ListUpperAlpha; + break; + case 9: + style = QTextListFormat::ListLowerRoman; + break; + case 10: style = QTextListFormat::ListUpperRoman; break; default: @@ -665,6 +691,8 @@ void TextEdit::textStyle(int styleIndex) cursor.mergeCharFormat(fmt); textEdit->mergeCurrentCharFormat(fmt); } else { + blockFmt.setMarker(marker); + cursor.setBlockFormat(blockFmt); QTextListFormat listFmt; if (cursor.currentList()) { listFmt = cursor.currentList()->format(); @@ -703,6 +731,11 @@ void TextEdit::textAlign(QAction *a) textEdit->setAlignment(Qt::AlignJustify); } +void TextEdit::setChecked(bool checked) +{ + textStyle(checked ? 5 : 4); +} + void TextEdit::currentCharFormatChanged(const QTextCharFormat &format) { fontChanged(format.font()); @@ -725,24 +758,37 @@ void TextEdit::cursorPositionChanged() comboStyle->setCurrentIndex(3); break; case QTextListFormat::ListDecimal: - comboStyle->setCurrentIndex(4); + comboStyle->setCurrentIndex(6); break; case QTextListFormat::ListLowerAlpha: - comboStyle->setCurrentIndex(5); + comboStyle->setCurrentIndex(7); break; case QTextListFormat::ListUpperAlpha: - comboStyle->setCurrentIndex(6); + comboStyle->setCurrentIndex(8); break; case QTextListFormat::ListLowerRoman: - comboStyle->setCurrentIndex(7); + comboStyle->setCurrentIndex(9); break; case QTextListFormat::ListUpperRoman: - comboStyle->setCurrentIndex(8); + comboStyle->setCurrentIndex(10); break; default: comboStyle->setCurrentIndex(-1); break; } + switch (textEdit->textCursor().block().blockFormat().marker()) { + case QTextBlockFormat::NoMarker: + actionToggleCheckState->setChecked(false); + break; + case QTextBlockFormat::Unchecked: + comboStyle->setCurrentIndex(4); + actionToggleCheckState->setChecked(false); + break; + case QTextBlockFormat::Checked: + comboStyle->setCurrentIndex(5); + actionToggleCheckState->setChecked(true); + break; + } } else { int headingLevel = textEdit->textCursor().blockFormat().headingLevel(); comboStyle->setCurrentIndex(headingLevel ? headingLevel + 8 : 0); diff --git a/examples/widgets/richtext/textedit/textedit.h b/examples/widgets/richtext/textedit/textedit.h index ae0b13a4cc..c253548a4f 100644 --- a/examples/widgets/richtext/textedit/textedit.h +++ b/examples/widgets/richtext/textedit/textedit.h @@ -96,6 +96,7 @@ private slots: void textStyle(int styleIndex); void textColor(); void textAlign(QAction *a); + void setChecked(bool checked); void currentCharFormatChanged(const QTextCharFormat &format); void cursorPositionChanged(); @@ -125,6 +126,7 @@ private: QAction *actionAlignCenter; QAction *actionAlignRight; QAction *actionAlignJustify; + QAction *actionToggleCheckState; QAction *actionUndo; QAction *actionRedo; #ifndef QT_NO_CLIPBOARD diff --git a/examples/widgets/richtext/textedit/textedit.qrc b/examples/widgets/richtext/textedit/textedit.qrc index 7d6efd7d67..8016a07ca0 100644 --- a/examples/widgets/richtext/textedit/textedit.qrc +++ b/examples/widgets/richtext/textedit/textedit.qrc @@ -1,6 +1,8 @@ images/logo32.png + images/mac/checkbox.png + images/mac/checkbox-checked.png images/mac/editcopy.png images/mac/editcut.png images/mac/editpaste.png @@ -20,6 +22,8 @@ images/mac/textunder.png images/mac/zoomin.png images/mac/zoomout.png + images/win/checkbox.png + images/win/checkbox-checked.png images/win/editcopy.png images/win/editcut.png images/win/editpaste.png -- cgit v1.2.3