From b922c97c9c65ce28f4ea18106f384adf1bbf605a Mon Sep 17 00:00:00 2001 From: Shawn Rutledge Date: Sat, 4 May 2019 08:48:41 +0200 Subject: [PATCH] TextEdit example: add indent/unindent feature It was not possible to interactively create the example text without this feature; and in general it's necessary to work with nested lists. But currently it does not deal with all possible changes of list nesting, because the API seems incomplete for that purpose. Task-number: QTBUG-75589 Change-Id: I3e29ca15a2e7c37300a0103ceb90670716472ffb Reviewed-by: Gatis Paeglis --- .../images/mac/format-indent-less.png | Bin 0 -> 1201 bytes .../images/mac/format-indent-more.png | Bin 0 -> 993 bytes .../images/win/format-indent-less.png | Bin 0 -> 1201 bytes .../images/win/format-indent-more.png | Bin 0 -> 993 bytes .../widgets/richtext/textedit/textedit.cpp | 46 ++++++++++++++++++ examples/widgets/richtext/textedit/textedit.h | 5 ++ .../widgets/richtext/textedit/textedit.qrc | 4 ++ 7 files changed, 55 insertions(+) create mode 100644 examples/widgets/richtext/textedit/images/mac/format-indent-less.png create mode 100644 examples/widgets/richtext/textedit/images/mac/format-indent-more.png create mode 100644 examples/widgets/richtext/textedit/images/win/format-indent-less.png create mode 100644 examples/widgets/richtext/textedit/images/win/format-indent-more.png diff --git a/examples/widgets/richtext/textedit/images/mac/format-indent-less.png b/examples/widgets/richtext/textedit/images/mac/format-indent-less.png new file mode 100644 index 0000000000000000000000000000000000000000..e38074e78b78b384a1f7e8ab680fdd35175c0347 GIT binary patch literal 1201 zcmV;i1Wx;jP)1#w$1 zXjCZr%@it11(#rA5Tld5kx6d)ZG*GWj8c>B%)OlRyxmtH&S{Qq=8j^0$Qz#L!}HwF zb6@xM|NpQ5eTk~_Uk<~6Je*QLsBLP_xPhcbKhxqZ8MEW&%MJdcpa~~TmmEOOBlXU% z7Oo9}wGf6Di(-mP#bO38uwX5*n1eCk`(O;H7A|9yL;`8qyrrsS2vikgz=;&Lcji|p zOGH2*>KM*k02_vsmpOdfOf)ei#RSQA zhqJ}xj!w0X-9d4>s5jJ`mjAs9q^5Cu#tkH;<7izvtCLZiZrlG>bxAVJd8BsjX`ydG z@q>^lNe9;t$$O=F7{M`yQ<;!>^wwDIdhSgZ>}ZsRvrUn%&PMsB z^I2Icm3134PJA@0uP3=wR{O)*bjR19-VlP$)v_fq=YbvbUp-3De~Iols(x~QIJSHK z9cNmrn2rp4r!J^mKX2WKNRY+7d=7>1j?3`J21&-_9GtnNZpEBcJCSTR16?OMe{`Q8 zc)8_L)3`m8A5+`^&U)DdDgd-Brs*ReyjeYo#b-D%j#oVXd}T%L%jh59(RVJ*`Sue7 z8ie%^9n^Lw=aJgIr-gjJ0Dl--qES<1xFrY*N)Um80hfEG$K|zGw%nBMC+p>2Yxwg1 z2;6xx|;Fn$p z&`Ghq7Z~s$clA8>?0ZNgPIBQg1_Q&eKZ^2|bsKw}_>_5LRUjrpe(-7$4juhgyb`lU6CD1We?oKENSZEL!nnx^I+ zH;{Qu;01}OEkdHjS~Fe|$*88=_W!dYnw!-E8xAd#i|J#XJ02bii7$Q*gg@o6r?=}z z`&zLUTsOpVB3M<72-X%3AI1nlXfa~2s)&GEh(=-Ui(9q*@XkrQ>fu^>N@0Tv*!O5D`@E2rOf~K>gut`nJvkcl&};Kd5PJ&bZ-t@3^j-3Dp9{KH#$hZ&%+| z0C}I(p#v@WL7_Z{R*4uP47H#T1Y=|*F13mn!7(b0YsY)YE%XJpKvPx1KkH literal 0 HcmV?d00001 diff --git a/examples/widgets/richtext/textedit/images/mac/format-indent-more.png b/examples/widgets/richtext/textedit/images/mac/format-indent-more.png new file mode 100644 index 0000000000000000000000000000000000000000..1bdeabd354fa5274c69cd70b281c2ee9276d5f6b GIT binary patch literal 993 zcmeAS@N?(olHy`uVBq!ia0vp^3LwnE3?yBabR7dyoCO|{#S9GG!XV7ZFl&wkP>{XE z)7O>#F)Irjvt)p8j1&U{@lwp-Sh+G@Qus?`lqaVZW zV1^4(3`@NjmU%Ipj$~Ns&2ToB;aoI`3q%mc*%*dpo(w?Jz&^sjA=1zxDk!NcI2nkl zgOh83TqDP5W9OLQl$wx~n$XnRbFs`oz2||tyckY_jXD>@u)&XEZwN>PXwJq!roEvI zYXUjW#4_84Ry#%3Z;0f-n9OPBm1XXgWgAdn8wf;&&f#Uw;pHyj-E1|@BZIzvAR9Q>-Ot^x8MA`1494r zy#Igi?f?7l{@;K9@BaJ$4|>|9;l zeM7=3DyB|dw|)-<>;;1Z2M-@Tdg9d8t2b^ufBxyyw{O3G{eyw)*Kha&O=C>*c6VXu zV3qX%vh6)x978NlCnqeBSYh?+cXf63?;pQ@{rdUyCv)Rsn=YpIU9aA3`m^cFjwLr{ z9GUXu$C4>U>>{k&c$ZpEOkFxt@#RcM&XorvGZR_OG`Oa$nKQ@6cCU@iox68qV&}}A zJ7>)j$qe=ogIX2m6Yga^cdW9S3|P%(*lkL?@Z#lpfkv%>BOfL%RcUs-K1Z@O$!CYG zTEa!c?5eda+Etx?mxTvvCx%appA?WbzPn6ir|& yv^?{$NP>$=LGSoH^#fdB%He>>qavwioD3RaUB_kZ*z`V literal 0 HcmV?d00001 diff --git a/examples/widgets/richtext/textedit/images/win/format-indent-less.png b/examples/widgets/richtext/textedit/images/win/format-indent-less.png new file mode 100644 index 0000000000000000000000000000000000000000..e38074e78b78b384a1f7e8ab680fdd35175c0347 GIT binary patch literal 1201 zcmV;i1Wx;jP)1#w$1 zXjCZr%@it11(#rA5Tld5kx6d)ZG*GWj8c>B%)OlRyxmtH&S{Qq=8j^0$Qz#L!}HwF zb6@xM|NpQ5eTk~_Uk<~6Je*QLsBLP_xPhcbKhxqZ8MEW&%MJdcpa~~TmmEOOBlXU% z7Oo9}wGf6Di(-mP#bO38uwX5*n1eCk`(O;H7A|9yL;`8qyrrsS2vikgz=;&Lcji|p zOGH2*>KM*k02_vsmpOdfOf)ei#RSQA zhqJ}xj!w0X-9d4>s5jJ`mjAs9q^5Cu#tkH;<7izvtCLZiZrlG>bxAVJd8BsjX`ydG z@q>^lNe9;t$$O=F7{M`yQ<;!>^wwDIdhSgZ>}ZsRvrUn%&PMsB z^I2Icm3134PJA@0uP3=wR{O)*bjR19-VlP$)v_fq=YbvbUp-3De~Iols(x~QIJSHK z9cNmrn2rp4r!J^mKX2WKNRY+7d=7>1j?3`J21&-_9GtnNZpEBcJCSTR16?OMe{`Q8 zc)8_L)3`m8A5+`^&U)DdDgd-Brs*ReyjeYo#b-D%j#oVXd}T%L%jh59(RVJ*`Sue7 z8ie%^9n^Lw=aJgIr-gjJ0Dl--qES<1xFrY*N)Um80hfEG$K|zGw%nBMC+p>2Yxwg1 z2;6xx|;Fn$p z&`Ghq7Z~s$clA8>?0ZNgPIBQg1_Q&eKZ^2|bsKw}_>_5LRUjrpe(-7$4juhgyb`lU6CD1We?oKENSZEL!nnx^I+ zH;{Qu;01}OEkdHjS~Fe|$*88=_W!dYnw!-E8xAd#i|J#XJ02bii7$Q*gg@o6r?=}z z`&zLUTsOpVB3M<72-X%3AI1nlXfa~2s)&GEh(=-Ui(9q*@XkrQ>fu^>N@0Tv*!O5D`@E2rOf~K>gut`nJvkcl&};Kd5PJ&bZ-t@3^j-3Dp9{KH#$hZ&%+| z0C}I(p#v@WL7_Z{R*4uP47H#T1Y=|*F13mn!7(b0YsY)YE%XJpKvPx1KkH literal 0 HcmV?d00001 diff --git a/examples/widgets/richtext/textedit/images/win/format-indent-more.png b/examples/widgets/richtext/textedit/images/win/format-indent-more.png new file mode 100644 index 0000000000000000000000000000000000000000..1bdeabd354fa5274c69cd70b281c2ee9276d5f6b GIT binary patch literal 993 zcmeAS@N?(olHy`uVBq!ia0vp^3LwnE3?yBabR7dyoCO|{#S9GG!XV7ZFl&wkP>{XE z)7O>#F)Irjvt)p8j1&U{@lwp-Sh+G@Qus?`lqaVZW zV1^4(3`@NjmU%Ipj$~Ns&2ToB;aoI`3q%mc*%*dpo(w?Jz&^sjA=1zxDk!NcI2nkl zgOh83TqDP5W9OLQl$wx~n$XnRbFs`oz2||tyckY_jXD>@u)&XEZwN>PXwJq!roEvI zYXUjW#4_84Ry#%3Z;0f-n9OPBm1XXgWgAdn8wf;&&f#Uw;pHyj-E1|@BZIzvAR9Q>-Ot^x8MA`1494r zy#Igi?f?7l{@;K9@BaJ$4|>|9;l zeM7=3DyB|dw|)-<>;;1Z2M-@Tdg9d8t2b^ufBxyyw{O3G{eyw)*Kha&O=C>*c6VXu zV3qX%vh6)x978NlCnqeBSYh?+cXf63?;pQ@{rdUyCv)Rsn=YpIU9aA3`m^cFjwLr{ z9GUXu$C4>U>>{k&c$ZpEOkFxt@#RcM&XorvGZR_OG`Oa$nKQ@6cCU@iox68qV&}}A zJ7>)j$qe=ogIX2m6Yga^cdW9S3|P%(*lkL?@Z#lpfkv%>BOfL%RcUs-K1Z@O$!CYG zTEa!c?5eda+Etx?mxTvvCx%appA?WbzPn6ir|& yv^?{$NP>$=LGSoH^#fdB%He>>qavwioD3RaUB_kZ*z`V literal 0 HcmV?d00001 diff --git a/examples/widgets/richtext/textedit/textedit.cpp b/examples/widgets/richtext/textedit/textedit.cpp index 683e441fce..ced77a62ce 100644 --- a/examples/widgets/richtext/textedit/textedit.cpp +++ b/examples/widgets/richtext/textedit/textedit.cpp @@ -317,6 +317,14 @@ void TextEdit::setupTextActions() actionAlignJustify->setShortcut(Qt::CTRL + Qt::Key_J); actionAlignJustify->setCheckable(true); actionAlignJustify->setPriority(QAction::LowPriority); + const QIcon indentMoreIcon = QIcon::fromTheme("format-indent-more", QIcon(rsrcPath + "/format-indent-more.png")); + actionIndentMore = menu->addAction(indentMoreIcon, tr("&Indent"), this, &TextEdit::indent); + actionIndentMore->setShortcut(Qt::CTRL + Qt::Key_BracketRight); + actionIndentMore->setPriority(QAction::LowPriority); + const QIcon indentLessIcon = QIcon::fromTheme("format-indent-less", QIcon(rsrcPath + "/format-indent-less.png")); + actionIndentLess = menu->addAction(indentLessIcon, tr("&Unindent"), this, &TextEdit::unindent); + actionIndentLess->setShortcut(Qt::CTRL + Qt::Key_BracketLeft); + actionIndentLess->setPriority(QAction::LowPriority); // Make sure the alignLeft is always left of the alignRight QActionGroup *alignGroup = new QActionGroup(this); @@ -335,6 +343,10 @@ void TextEdit::setupTextActions() tb->addActions(alignGroup->actions()); menu->addActions(alignGroup->actions()); + tb->addAction(actionIndentMore); + tb->addAction(actionIndentLess); + menu->addAction(actionIndentMore); + menu->addAction(actionIndentLess); menu->addSeparator(); @@ -736,6 +748,40 @@ void TextEdit::setChecked(bool checked) textStyle(checked ? 5 : 4); } +void TextEdit::indent() +{ + modifyIndentation(1); +} + +void TextEdit::unindent() +{ + modifyIndentation(-1); +} + +void TextEdit::modifyIndentation(int amount) +{ + QTextCursor cursor = textEdit->textCursor(); + cursor.beginEditBlock(); + if (cursor.currentList()) { + QTextListFormat listFmt = cursor.currentList()->format(); + // See whether the line above is the list we want to move this item into, + // or whether we need a new list. + QTextCursor above(cursor); + above.movePosition(QTextCursor::Up); + if (above.currentList() && listFmt.indent() + amount == above.currentList()->format().indent()) { + above.currentList()->add(cursor.block()); + } else { + listFmt.setIndent(listFmt.indent() + amount); + cursor.createList(listFmt); + } + } else { + QTextBlockFormat blockFmt = cursor.blockFormat(); + blockFmt.setIndent(blockFmt.indent() + amount); + cursor.setBlockFormat(blockFmt); + } + cursor.endEditBlock(); +} + void TextEdit::currentCharFormatChanged(const QTextCharFormat &format) { fontChanged(format.font()); diff --git a/examples/widgets/richtext/textedit/textedit.h b/examples/widgets/richtext/textedit/textedit.h index c253548a4f..9e50166c6f 100644 --- a/examples/widgets/richtext/textedit/textedit.h +++ b/examples/widgets/richtext/textedit/textedit.h @@ -97,6 +97,8 @@ private slots: void textColor(); void textAlign(QAction *a); void setChecked(bool checked); + void indent(); + void unindent(); void currentCharFormatChanged(const QTextCharFormat &format); void cursorPositionChanged(); @@ -111,6 +113,7 @@ private: void setupTextActions(); bool maybeSave(); void setCurrentFileName(const QString &fileName); + void modifyIndentation(int amount); void mergeFormatOnWordOrSelection(const QTextCharFormat &format); void fontChanged(const QFont &f); @@ -126,6 +129,8 @@ private: QAction *actionAlignCenter; QAction *actionAlignRight; QAction *actionAlignJustify; + QAction *actionIndentLess; + QAction *actionIndentMore; QAction *actionToggleCheckState; QAction *actionUndo; QAction *actionRedo; diff --git a/examples/widgets/richtext/textedit/textedit.qrc b/examples/widgets/richtext/textedit/textedit.qrc index 8016a07ca0..1641acc207 100644 --- a/examples/widgets/richtext/textedit/textedit.qrc +++ b/examples/widgets/richtext/textedit/textedit.qrc @@ -13,6 +13,8 @@ images/mac/fileopen.png images/mac/fileprint.png images/mac/filesave.png + images/mac/format-indent-less.png + images/mac/format-indent-more.png images/mac/textbold.png images/mac/textcenter.png images/mac/textitalic.png @@ -34,6 +36,8 @@ images/win/fileopen.png images/win/fileprint.png images/win/filesave.png + images/win/format-indent-less.png + images/win/format-indent-more.png images/win/textbold.png images/win/textcenter.png images/win/textitalic.png