KProperty

KPropertyMultiLineStringEditor.cpp
1 /* This file is part of the KDE project
2  Copyright (C) 2016 Jarosław Staniek <[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 "KPropertyMultiLineStringEditor.h"
21 #include "KPropertyEditorView.h"
22 
23 #include <QHBoxLayout>
24 #include <QScrollBar>
25 #include <QPlainTextEdit>
26 
27 class Q_DECL_HIDDEN KPropertyMultiLineStringEditor::Private
28 {
29 public:
30  Private() {}
31  QPlainTextEdit *editor;
32  bool slotTextChangedEnabled = true;
33 };
34 
35 KPropertyMultiLineStringEditor::KPropertyMultiLineStringEditor(QWidget *parent)
36  : QWidget(parent)
37  , d(new Private)
38 {
39  setAutoFillBackground(true);
40  QHBoxLayout *lyr = new QHBoxLayout(this);
41  lyr->setContentsMargins(0, 1, 0, 0);
42  lyr->addSpacing(2);
43  d->editor = new QPlainTextEdit;
44  lyr->addWidget(d->editor);
45  d->editor->setFrameStyle(0);
46  d->editor->setTabChangesFocus(true);
47  d->editor->setContentsMargins(0,0,0,0);
48  d->editor->document()->setDocumentMargin(1);
49  connect(d->editor, &QPlainTextEdit::textChanged, this, &KPropertyMultiLineStringEditor::slotTextChanged);
50  d->editor->verticalScrollBar()->installEventFilter(this);
51 }
52 
53 KPropertyMultiLineStringEditor::~KPropertyMultiLineStringEditor()
54 {
55  delete d;
56 }
57 
58 QString KPropertyMultiLineStringEditor::value() const
59 {
60  return d->editor->toPlainText();
61 }
62 
63 void KPropertyMultiLineStringEditor::setValue(const QString& value)
64 {
65  d->slotTextChangedEnabled = false;
66  d->editor->setPlainText(value);
67  d->slotTextChangedEnabled = true;
68 }
69 
70 void KPropertyMultiLineStringEditor::slotTextChanged()
71 {
72  if (!d->slotTextChangedEnabled) {
73  return;
74  }
75  emit commitData(this);
76 }
77 
78 bool KPropertyMultiLineStringEditor::eventFilter(QObject *o, QEvent *ev)
79 {
80  const bool result = QWidget::eventFilter(o, ev);
81  if (o == d->editor->verticalScrollBar() && ev->type() == QEvent::Paint) {
82  KPropertyWidgetsFactory::paintTopGridLine(qobject_cast<QWidget*>(o));
83  }
84  return result;
85 }
86 
87 void KPropertyMultiLineStringEditor::paintEvent(QPaintEvent * event)
88 {
89  QWidget::paintEvent(event);
90  KPropertyWidgetsFactory::paintTopGridLine(this);
91 }
QEvent::Type type() const const
void setContentsMargins(int left, int top, int right, int bottom)
void textChanged()
void addSpacing(int size)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
virtual void paintEvent(QPaintEvent *event)
Editor for string type supporting multiple lines of plain text.
virtual bool eventFilter(QObject *watched, QEvent *event)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun Aug 9 2020 23:10:57 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.