KPimTextEdit

plaintexteditorwidget.cpp
1 /*
2  Copyright (C) 2013-2020 Laurent Montel <[email protected]>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "plaintexteditorwidget.h"
21 #include "plaintexteditor.h"
22 #include "plaintexteditfindbar.h"
23 #include "texttospeech/texttospeechwidget.h"
24 #include "slidecontainer.h"
25 
26 #include <QVBoxLayout>
27 #include <QTextCursor>
28 
29 using namespace KPIMTextEdit;
30 class Q_DECL_HIDDEN KPIMTextEdit::PlainTextEditorWidgetPrivate
31 {
32 public:
33  PlainTextEditorWidgetPrivate()
34  {
35  }
36 
37  KPIMTextEdit::PlainTextEditFindBar *mFindBar = nullptr;
38  PlainTextEditor *mEditor = nullptr;
39  KPIMTextEdit::TextToSpeechWidget *mTextToSpeechWidget = nullptr;
40  KPIMTextEdit::SlideContainer *mSliderContainer = nullptr;
41 };
42 
43 PlainTextEditorWidget::PlainTextEditorWidget(PlainTextEditor *customEditor, QWidget *parent)
44  : QWidget(parent)
45  , d(new KPIMTextEdit::PlainTextEditorWidgetPrivate)
46 {
47  init(customEditor);
48 }
49 
50 PlainTextEditorWidget::PlainTextEditorWidget(QWidget *parent)
51  : QWidget(parent)
52  , d(new KPIMTextEdit::PlainTextEditorWidgetPrivate)
53 {
54  init();
55 }
56 
57 PlainTextEditorWidget::~PlainTextEditorWidget()
58 {
59  delete d;
60 }
61 
62 PlainTextEditor *PlainTextEditorWidget::editor() const
63 {
64  return d->mEditor;
65 }
66 
67 void PlainTextEditorWidget::clear()
68 {
69  d->mEditor->clear();
70 }
71 
72 void PlainTextEditorWidget::setSpellCheckingConfigFileName(const QString &_fileName)
73 {
74  d->mEditor->setSpellCheckingConfigFileName(_fileName);
75 }
76 
77 void PlainTextEditorWidget::setPlainText(const QString &text)
78 {
79  d->mEditor->setPlainText(text);
80 }
81 
82 bool PlainTextEditorWidget::isEmpty() const
83 {
84  return d->mEditor->document()->isEmpty();
85 }
86 
87 QString PlainTextEditorWidget::toPlainText() const
88 {
89  return d->mEditor->toPlainText();
90 }
91 
92 void PlainTextEditorWidget::init(PlainTextEditor *customEditor)
93 {
94  QVBoxLayout *lay = new QVBoxLayout(this);
95  lay->setContentsMargins(0, 0, 0, 0);
96  d->mTextToSpeechWidget = new KPIMTextEdit::TextToSpeechWidget(this);
97  lay->addWidget(d->mTextToSpeechWidget);
98  if (customEditor) {
99  d->mEditor = customEditor;
100  } else {
101  d->mEditor = new PlainTextEditor;
102  }
103  lay->addWidget(d->mEditor);
104  connect(d->mEditor, &PlainTextEditor::say, d->mTextToSpeechWidget, &KPIMTextEdit::TextToSpeechWidget::say);
105 
106  d->mSliderContainer = new KPIMTextEdit::SlideContainer(this);
107 
108  d->mFindBar = new KPIMTextEdit::PlainTextEditFindBar(d->mEditor, this);
109  d->mFindBar->setHideWhenClose(false);
110  connect(d->mFindBar, &KPIMTextEdit::PlainTextEditFindBar::displayMessageIndicator, d->mEditor, &PlainTextEditor::slotDisplayMessageIndicator);
111  connect(d->mFindBar, &KPIMTextEdit::PlainTextEditFindBar::hideFindBar, this, &PlainTextEditorWidget::slotHideFindBar);
112  d->mSliderContainer->setContent(d->mFindBar);
113 
114  lay->addWidget(d->mSliderContainer);
115 
116  connect(d->mEditor, &PlainTextEditor::findText, this, &PlainTextEditorWidget::slotFind);
117 
118  connect(d->mEditor, &PlainTextEditor::replaceText, this, &PlainTextEditorWidget::slotReplace);
119 }
120 
121 void PlainTextEditorWidget::slotHideFindBar()
122 {
123  d->mSliderContainer->slideOut();
124  d->mEditor->setFocus();
125 }
126 
127 bool PlainTextEditorWidget::isReadOnly() const
128 {
129  return d->mEditor->isReadOnly();
130 }
131 
132 void PlainTextEditorWidget::setReadOnly(bool readOnly)
133 {
134  d->mEditor->setReadOnly(readOnly);
135 }
136 
137 void PlainTextEditorWidget::slotReplace()
138 {
139  if (d->mEditor->searchSupport()) {
140  if (d->mEditor->textCursor().hasSelection()) {
141  d->mFindBar->setText(d->mEditor->textCursor().selectedText());
142  }
143  d->mFindBar->showReplace();
144  d->mSliderContainer->slideIn();
145  d->mFindBar->focusAndSetCursor();
146  }
147 }
148 
149 void PlainTextEditorWidget::slotFind()
150 {
151  if (d->mEditor->searchSupport()) {
152  if (d->mEditor->textCursor().hasSelection()) {
153  d->mFindBar->setText(d->mEditor->textCursor().selectedText());
154  }
155  d->mEditor->moveCursor(QTextCursor::Start);
156  d->mFindBar->showFind();
157  d->mSliderContainer->slideIn();
158  d->mFindBar->focusAndSetCursor();
159  }
160 }
void setContentsMargins(int left, int top, int right, int bottom)
The TextToSpeechWidget class.
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
This widget is design to contain one child widget, the "content" widget.
QCA_EXPORT void init()
The PlainTextEditor class.
The PlainTextEditFindBar class.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Aug 7 2020 23:05:30 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.