KTextAddons

richtexteditorwidget.cpp
1/*
2 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "richtexteditorwidget.h"
8#include "richtexteditfindbar.h"
9#include "richtexteditor.h"
10
11#include "config-textcustomeditor.h"
12#include <QTextCursor>
13#include <QVBoxLayout>
14#if HAVE_KTEXTADDONS_TEXT_TO_SPEECH_SUPPORT
15#include <TextEditTextToSpeech/TextToSpeechContainerWidget>
16#endif
17#include <TextAddonsWidgets/SlideContainer>
18
19using namespace TextCustomEditor;
20
21class Q_DECL_HIDDEN TextCustomEditor::RichTextEditorWidgetPrivate
22{
23public:
24 RichTextEditorWidgetPrivate() = default;
25
26 TextCustomEditor::RichTextEditFindBar *mFindBar = nullptr;
27 RichTextEditor *mEditor = nullptr;
28#if HAVE_KTEXTADDONS_TEXT_TO_SPEECH_SUPPORT
29 TextEditTextToSpeech::TextToSpeechContainerWidget *mTextToSpeechWidget = nullptr;
30#endif
31 TextAddonsWidgets::SlideContainer *mSliderContainer = nullptr;
32};
33
34RichTextEditorWidget::RichTextEditorWidget(RichTextEditor *customEditor, QWidget *parent)
35 : QWidget(parent)
36 , d(new TextCustomEditor::RichTextEditorWidgetPrivate)
37{
38 init(customEditor);
39}
40
41RichTextEditorWidget::RichTextEditorWidget(QWidget *parent)
42 : QWidget(parent)
43 , d(new TextCustomEditor::RichTextEditorWidgetPrivate)
44{
45 init();
46}
47
48RichTextEditorWidget::~RichTextEditorWidget() = default;
49
50void RichTextEditorWidget::clear()
51{
52 d->mEditor->clear();
53}
54
55RichTextEditor *RichTextEditorWidget::editor() const
56{
57 return d->mEditor;
58}
59
60void RichTextEditorWidget::setAcceptRichText(bool b)
61{
62 d->mEditor->setAcceptRichText(b);
63}
64
65bool RichTextEditorWidget::acceptRichText() const
66{
67 return d->mEditor->acceptRichText();
68}
69
70void RichTextEditorWidget::setSpellCheckingConfigFileName(const QString &_fileName)
71{
72 d->mEditor->setSpellCheckingConfigFileName(_fileName);
73}
74
75void RichTextEditorWidget::setHtml(const QString &html)
76{
77 d->mEditor->setHtml(html);
78}
79
80QString RichTextEditorWidget::toHtml() const
81{
82 return d->mEditor->toHtml();
83}
84
85void RichTextEditorWidget::setPlainText(const QString &text)
86{
87 d->mEditor->setPlainText(text);
88}
89
90bool RichTextEditorWidget::isEmpty() const
91{
92 return d->mEditor->document()->isEmpty();
93}
94
95QString RichTextEditorWidget::toPlainText() const
96{
97 return d->mEditor->toPlainText();
98}
99
100void RichTextEditorWidget::init(RichTextEditor *customEditor)
101{
102 auto lay = new QVBoxLayout(this);
103 lay->setContentsMargins({});
104#if HAVE_KTEXTADDONS_TEXT_TO_SPEECH_SUPPORT
105 d->mTextToSpeechWidget = new TextEditTextToSpeech::TextToSpeechContainerWidget(this);
106 lay->addWidget(d->mTextToSpeechWidget);
107#endif
108 if (customEditor) {
109 d->mEditor = customEditor;
110 } else {
111 d->mEditor = new RichTextEditor;
112 }
113#if HAVE_KTEXTADDONS_TEXT_TO_SPEECH_SUPPORT
114 connect(d->mEditor, &RichTextEditor::say, d->mTextToSpeechWidget, &TextEditTextToSpeech::TextToSpeechContainerWidget::say);
115#endif
116 lay->addWidget(d->mEditor);
117
118 d->mSliderContainer = new TextAddonsWidgets::SlideContainer(this);
119
120 d->mFindBar = new TextCustomEditor::RichTextEditFindBar(d->mEditor, this);
121 d->mFindBar->setHideWhenClose(false);
122 connect(d->mFindBar, &TextCustomEditor::RichTextEditFindBar::displayMessageIndicator, d->mEditor, &RichTextEditor::slotDisplayMessageIndicator);
123
124 connect(d->mFindBar, &TextCustomEditor::RichTextEditFindBar::hideFindBar, this, &RichTextEditorWidget::slotHideFindBar);
125 d->mSliderContainer->setContent(d->mFindBar);
126 lay->addWidget(d->mSliderContainer);
127
128 connect(d->mEditor, &RichTextEditor::findText, this, &RichTextEditorWidget::slotFind);
129 connect(d->mEditor, &RichTextEditor::replaceText, this, &RichTextEditorWidget::slotReplace);
130}
131
132void RichTextEditorWidget::slotHideFindBar()
133{
134 d->mSliderContainer->slideOut();
135 d->mEditor->setFocus();
136}
137
138bool RichTextEditorWidget::isReadOnly() const
139{
140 return d->mEditor->isReadOnly();
141}
142
143void RichTextEditorWidget::setReadOnly(bool readOnly)
144{
145 d->mEditor->setReadOnly(readOnly);
146}
147
148void RichTextEditorWidget::slotReplace()
149{
150 if (d->mEditor->searchSupport()) {
151 if (d->mEditor->textCursor().hasSelection()) {
152 d->mFindBar->setText(d->mEditor->textCursor().selectedText());
153 }
154 d->mFindBar->showReplace();
155 d->mSliderContainer->slideIn();
156 d->mFindBar->focusAndSetCursor();
157 }
158}
159
160void RichTextEditorWidget::slotFindNext()
161{
162 if (d->mEditor->searchSupport()) {
163 if (d->mFindBar->isVisible()) {
164 d->mFindBar->findNext();
165 } else {
166 slotFind();
167 }
168 }
169}
170
171void RichTextEditorWidget::slotFind()
172{
173 if (d->mEditor->searchSupport()) {
174 if (d->mEditor->textCursor().hasSelection()) {
175 d->mFindBar->setText(d->mEditor->textCursor().selectedText());
176 }
177 d->mEditor->moveCursor(QTextCursor::Start);
178
179 d->mFindBar->showFind();
180 d->mSliderContainer->slideIn();
181 d->mFindBar->focusAndSetCursor();
182 }
183}
184
185#include "moc_richtexteditorwidget.cpp"
This widget is design to contain one child widget, the "content" widget.
The RichTextEditFindBar class.
The RichTextEditor class.
void init(KXmlGuiWindow *window, KGameDifficulty *difficulty=nullptr)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:29:59 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.