File indexing completed on 2024-04-21 04:41:01

0001 /* This file is part of the KDE project
0002    Copyright (C) 2016 Jarosław Staniek <staniek@kde.org>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "KPropertyMultiLineStringEditor.h"
0021 #include "KPropertyEditorView.h"
0022 
0023 #include <QHBoxLayout>
0024 #include <QScrollBar>
0025 #include <QPlainTextEdit>
0026 
0027 class Q_DECL_HIDDEN KPropertyMultiLineStringEditor::Private
0028 {
0029 public:
0030     Private() {}
0031     QPlainTextEdit *editor;
0032     bool slotTextChangedEnabled = true;
0033 };
0034 
0035 KPropertyMultiLineStringEditor::KPropertyMultiLineStringEditor(QWidget *parent)
0036  : QWidget(parent)
0037  , d(new Private)
0038 {
0039     setAutoFillBackground(true);
0040     QHBoxLayout *lyr = new QHBoxLayout(this);
0041     lyr->setContentsMargins(0, 1, 0, 0);
0042     lyr->addSpacing(2);
0043     d->editor = new QPlainTextEdit;
0044     lyr->addWidget(d->editor);
0045     d->editor->setFrameStyle(0);
0046     d->editor->setTabChangesFocus(true);
0047     d->editor->setContentsMargins(0,0,0,0);
0048     d->editor->document()->setDocumentMargin(1);
0049     connect(d->editor, &QPlainTextEdit::textChanged, this, &KPropertyMultiLineStringEditor::slotTextChanged);
0050     d->editor->verticalScrollBar()->installEventFilter(this);
0051 }
0052 
0053 KPropertyMultiLineStringEditor::~KPropertyMultiLineStringEditor()
0054 {
0055     delete d;
0056 }
0057 
0058 QString KPropertyMultiLineStringEditor::value() const
0059 {
0060     return d->editor->toPlainText();
0061 }
0062 
0063 void KPropertyMultiLineStringEditor::setValue(const QString& value)
0064 {
0065     d->slotTextChangedEnabled = false;
0066     d->editor->setPlainText(value);
0067     d->slotTextChangedEnabled = true;
0068 }
0069 
0070 void KPropertyMultiLineStringEditor::slotTextChanged()
0071 {
0072     if (!d->slotTextChangedEnabled) {
0073         return;
0074     }
0075     emit commitData(this);
0076 }
0077 
0078 bool KPropertyMultiLineStringEditor::eventFilter(QObject *o, QEvent *ev)
0079 {
0080     const bool result = QWidget::eventFilter(o, ev);
0081     if (o == d->editor->verticalScrollBar() && ev->type() == QEvent::Paint) {
0082         KPropertyWidgetsFactory::paintTopGridLine(qobject_cast<QWidget*>(o));
0083     }
0084     return result;
0085 }
0086 
0087 void KPropertyMultiLineStringEditor::paintEvent(QPaintEvent * event)
0088 {
0089     QWidget::paintEvent(event);
0090     KPropertyWidgetsFactory::paintTopGridLine(this);
0091 }