KTextAddons

richtextbrowserwidget.cpp
1/*
2 SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "richtextbrowserwidget.h"
8#include "richtextbrowser.h"
9#include "richtextbrowserfindbar.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::RichTextBrowserWidgetPrivate
22{
23public:
24 RichTextBrowserWidgetPrivate() = default;
25
27 RichTextBrowser *mEditor = nullptr;
28#if HAVE_KTEXTADDONS_TEXT_TO_SPEECH_SUPPORT
29 TextEditTextToSpeech::TextToSpeechContainerWidget *mTextToSpeechWidget = nullptr;
30#endif
31 TextAddonsWidgets::SlideContainer *mSliderContainer = nullptr;
32};
33
34RichTextBrowserWidget::RichTextBrowserWidget(RichTextBrowser *customEditor, QWidget *parent)
35 : QWidget(parent)
36 , d(new TextCustomEditor::RichTextBrowserWidgetPrivate)
37{
38 init(customEditor);
39}
40
41RichTextBrowserWidget::RichTextBrowserWidget(QWidget *parent)
42 : QWidget(parent)
43 , d(new TextCustomEditor::RichTextBrowserWidgetPrivate)
44{
45 init();
46}
47
48RichTextBrowserWidget::~RichTextBrowserWidget() = default;
49
50void RichTextBrowserWidget::clear()
51{
52 d->mEditor->clear();
53}
54
55RichTextBrowser *RichTextBrowserWidget::editor() const
56{
57 return d->mEditor;
58}
59
60void RichTextBrowserWidget::setAcceptRichText(bool b)
61{
62 d->mEditor->setAcceptRichText(b);
63}
64
65bool RichTextBrowserWidget::acceptRichText() const
66{
67 return d->mEditor->acceptRichText();
68}
69
70void RichTextBrowserWidget::setHtml(const QString &html)
71{
72 d->mEditor->setHtml(html);
73}
74
75QString RichTextBrowserWidget::toHtml() const
76{
77 return d->mEditor->toHtml();
78}
79
80void RichTextBrowserWidget::setPlainText(const QString &text)
81{
82 d->mEditor->setPlainText(text);
83}
84
85bool RichTextBrowserWidget::isEmpty() const
86{
87 return d->mEditor->document()->isEmpty();
88}
89
90QString RichTextBrowserWidget::toPlainText() const
91{
92 return d->mEditor->toPlainText();
93}
94
95void RichTextBrowserWidget::init(RichTextBrowser *customEditor)
96{
97 auto lay = new QVBoxLayout(this);
98 lay->setContentsMargins({});
99#if HAVE_KTEXTADDONS_TEXT_TO_SPEECH_SUPPORT
100 d->mTextToSpeechWidget = new TextEditTextToSpeech::TextToSpeechContainerWidget(this);
101 lay->addWidget(d->mTextToSpeechWidget);
102#endif
103 if (customEditor) {
104 d->mEditor = customEditor;
105 } else {
106 d->mEditor = new RichTextBrowser;
107 }
108#if HAVE_KTEXTADDONS_TEXT_TO_SPEECH_SUPPORT
109 connect(d->mEditor, &RichTextBrowser::say, d->mTextToSpeechWidget, &TextEditTextToSpeech::TextToSpeechContainerWidget::say);
110#endif
111 lay->addWidget(d->mEditor);
112
113 d->mSliderContainer = new TextAddonsWidgets::SlideContainer(this);
114
115 d->mFindBar = new TextCustomEditor::RichTextBrowserFindBar(d->mEditor, this);
116 d->mFindBar->setHideWhenClose(false);
117 connect(d->mFindBar, &TextCustomEditor::RichTextBrowserFindBar::displayMessageIndicator, d->mEditor, &RichTextBrowser::slotDisplayMessageIndicator);
118
119 connect(d->mFindBar, &TextCustomEditor::RichTextBrowserFindBar::hideFindBar, this, &RichTextBrowserWidget::slotHideFindBar);
120 d->mSliderContainer->setContent(d->mFindBar);
121 lay->addWidget(d->mSliderContainer);
122
123 connect(d->mEditor, &RichTextBrowser::findText, this, &RichTextBrowserWidget::slotFind);
124}
125
126void RichTextBrowserWidget::slotHideFindBar()
127{
128 d->mSliderContainer->slideOut();
129 d->mEditor->setFocus();
130}
131
132void RichTextBrowserWidget::slotFindNext()
133{
134 if (d->mEditor->searchSupport()) {
135 if (d->mFindBar->isVisible()) {
136 d->mFindBar->findNext();
137 } else {
138 slotFind();
139 }
140 }
141}
142
143void RichTextBrowserWidget::slotFind()
144{
145 if (d->mEditor->searchSupport()) {
146 if (d->mEditor->textCursor().hasSelection()) {
147 d->mFindBar->setText(d->mEditor->textCursor().selectedText());
148 }
149 d->mEditor->moveCursor(QTextCursor::Start);
150
151 d->mFindBar->showFind();
152 d->mSliderContainer->slideIn();
153 d->mFindBar->focusAndSetCursor();
154 }
155}
156
157#include "moc_richtextbrowserwidget.cpp"
This widget is design to contain one child widget, the "content" widget.
The RichTextBrowser class.
QCA_EXPORT void init()
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 18 2024 12:19:49 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.