KProperty

KPropertyMultiLineStringEditor.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2016 Jarosław Staniek <staniek@kde.org>
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
27class Q_DECL_HIDDEN KPropertyMultiLineStringEditor::Private
28{
29public:
30 Private() {}
31 QPlainTextEdit *editor;
32 bool slotTextChangedEnabled = true;
33};
34
35KPropertyMultiLineStringEditor::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
53KPropertyMultiLineStringEditor::~KPropertyMultiLineStringEditor()
54{
55 delete d;
56}
57
58QString KPropertyMultiLineStringEditor::value() const
59{
60 return d->editor->toPlainText();
61}
62
63void KPropertyMultiLineStringEditor::setValue(const QString& value)
64{
65 d->slotTextChangedEnabled = false;
66 d->editor->setPlainText(value);
67 d->slotTextChangedEnabled = true;
68}
69
70void KPropertyMultiLineStringEditor::slotTextChanged()
71{
72 if (!d->slotTextChangedEnabled) {
73 return;
74 }
75 emit commitData(this);
76}
77
78bool 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
87void KPropertyMultiLineStringEditor::paintEvent(QPaintEvent * event)
88{
90 KPropertyWidgetsFactory::paintTopGridLine(this);
91}
Editor for string type supporting multiple lines of plain text.
QScrollBar * verticalScrollBar() const const
void addSpacing(int size)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
Type type() const const
void setContentsMargins(const QMargins &margins)
virtual bool eventFilter(QObject *watched, QEvent *event)
T qobject_cast(QObject *object)
void setPlainText(const QString &text)
void textChanged()
QString toPlainText() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
virtual bool event(QEvent *event) override
virtual void paintEvent(QPaintEvent *event)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:04:09 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.