KTextAddons

plaintexteditorwidget.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 "plaintexteditorwidget.h"
8#include "config-textcustomeditor.h"
9#include "plaintexteditfindbar.h"
10#include "plaintexteditor.h"
11#include <TextAddonsWidgets/SlideContainer>
12#if HAVE_KTEXTADDONS_TEXT_TO_SPEECH_SUPPORT
13#include <TextEditTextToSpeech/TextToSpeechContainerWidget>
14#endif
15#include <QTextCursor>
16#include <QVBoxLayout>
17
18using namespace TextCustomEditor;
19class Q_DECL_HIDDEN TextCustomEditor::PlainTextEditorWidgetPrivate
20{
21public:
22 PlainTextEditorWidgetPrivate() = default;
23
25 PlainTextEditor *mEditor = nullptr;
26#if HAVE_KTEXTADDONS_TEXT_TO_SPEECH_SUPPORT
27 TextEditTextToSpeech::TextToSpeechContainerWidget *mTextToSpeechWidget = nullptr;
28#endif
29 TextAddonsWidgets::SlideContainer *mSliderContainer = nullptr;
30};
31
32PlainTextEditorWidget::PlainTextEditorWidget(PlainTextEditor *customEditor, QWidget *parent)
33 : QWidget(parent)
34 , d(new TextCustomEditor::PlainTextEditorWidgetPrivate)
35{
36 init(customEditor);
37}
38
39PlainTextEditorWidget::PlainTextEditorWidget(QWidget *parent)
40 : QWidget(parent)
41 , d(new TextCustomEditor::PlainTextEditorWidgetPrivate)
42{
43 init();
44}
45
46PlainTextEditorWidget::~PlainTextEditorWidget() = default;
47
48PlainTextEditor *PlainTextEditorWidget::editor() const
49{
50 return d->mEditor;
51}
52
53void PlainTextEditorWidget::clear()
54{
55 d->mEditor->clear();
56}
57
58void PlainTextEditorWidget::setSpellCheckingConfigFileName(const QString &_fileName)
59{
60 d->mEditor->setSpellCheckingConfigFileName(_fileName);
61}
62
63void PlainTextEditorWidget::setPlainText(const QString &text)
64{
65 d->mEditor->setPlainText(text);
66}
67
68bool PlainTextEditorWidget::isEmpty() const
69{
70 return d->mEditor->document()->isEmpty();
71}
72
73QString PlainTextEditorWidget::toPlainText() const
74{
75 return d->mEditor->toPlainText();
76}
77
78void PlainTextEditorWidget::init(PlainTextEditor *customEditor)
79{
80 auto lay = new QVBoxLayout(this);
81 lay->setContentsMargins({});
82#if HAVE_KTEXTADDONS_TEXT_TO_SPEECH_SUPPORT
83 d->mTextToSpeechWidget = new TextEditTextToSpeech::TextToSpeechContainerWidget(this);
84 lay->addWidget(d->mTextToSpeechWidget);
85#endif
86 if (customEditor) {
87 d->mEditor = customEditor;
88 } else {
89 d->mEditor = new PlainTextEditor;
90 }
91 lay->addWidget(d->mEditor);
92#if HAVE_KTEXTADDONS_TEXT_TO_SPEECH_SUPPORT
93 connect(d->mEditor, &PlainTextEditor::say, d->mTextToSpeechWidget, &TextEditTextToSpeech::TextToSpeechContainerWidget::say);
94#endif
95 d->mSliderContainer = new TextAddonsWidgets::SlideContainer(this);
96
97 d->mFindBar = new TextCustomEditor::PlainTextEditFindBar(d->mEditor, this);
98 d->mFindBar->setHideWhenClose(false);
99 connect(d->mFindBar, &TextCustomEditor::PlainTextEditFindBar::displayMessageIndicator, d->mEditor, &PlainTextEditor::slotDisplayMessageIndicator);
100 connect(d->mFindBar, &TextCustomEditor::PlainTextEditFindBar::hideFindBar, this, &PlainTextEditorWidget::slotHideFindBar);
101 d->mSliderContainer->setContent(d->mFindBar);
102
103 lay->addWidget(d->mSliderContainer);
104
105 connect(d->mEditor, &PlainTextEditor::findText, this, &PlainTextEditorWidget::slotFind);
106
107 connect(d->mEditor, &PlainTextEditor::replaceText, this, &PlainTextEditorWidget::slotReplace);
108}
109
110void PlainTextEditorWidget::slotHideFindBar()
111{
112 d->mSliderContainer->slideOut();
113 d->mEditor->setFocus();
114}
115
116bool PlainTextEditorWidget::isReadOnly() const
117{
118 return d->mEditor->isReadOnly();
119}
120
121void PlainTextEditorWidget::setReadOnly(bool readOnly)
122{
123 d->mEditor->setReadOnly(readOnly);
124}
125
126void PlainTextEditorWidget::slotReplace()
127{
128 if (d->mEditor->searchSupport()) {
129 if (d->mEditor->textCursor().hasSelection()) {
130 d->mFindBar->setText(d->mEditor->textCursor().selectedText());
131 }
132 d->mFindBar->showReplace();
133 d->mSliderContainer->slideIn();
134 d->mFindBar->focusAndSetCursor();
135 }
136}
137
138void PlainTextEditorWidget::slotFind()
139{
140 if (d->mEditor->searchSupport()) {
141 if (d->mEditor->textCursor().hasSelection()) {
142 d->mFindBar->setText(d->mEditor->textCursor().selectedText());
143 }
144 d->mEditor->moveCursor(QTextCursor::Start);
145 d->mFindBar->showFind();
146 d->mSliderContainer->slideIn();
147 d->mFindBar->focusAndSetCursor();
148 }
149}
150
151#include "moc_plaintexteditorwidget.cpp"
This widget is design to contain one child widget, the "content" widget.
The PlainTextEditFindBar class.
The PlainTextEditor 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 Sat Dec 21 2024 16:56:02 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.