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

0001 /* This file is part of the KDE project
0002    Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
0003    Copyright (C) 2004  Alexander Dymo <cloudtemple@mskat.net>
0004    Copyright (C) 2008-2016 Jarosław Staniek <staniek@kde.org>
0005 
0006    This library is free software; you can redistribute it and/or
0007    modify it under the terms of the GNU Library General Public
0008    License as published by the Free Software Foundation; either
0009    version 2 of the License, or (at your option) any later version.
0010 
0011    This library is distributed in the hope that it will be useful,
0012    but WITHOUT ANY WARRANTY; without even the implied warranty of
0013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014    Library General Public License for more details.
0015 
0016    You should have received a copy of the GNU Library General Public License
0017    along with this library; see the file COPYING.LIB.  If not, write to
0018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0019  * Boston, MA 02110-1301, USA.
0020 */
0021 
0022 #include "KPropertyStringEditor.h"
0023 #include "KPropertyMultiLineStringEditor.h"
0024 #include "KPropertyUtils.h"
0025 #include "KPropertyUtils_p.h"
0026 #include "KPropertyEditorDataModel_p.h"
0027 
0028 namespace {
0029     bool isMultiLine(const KProperty *property) {
0030         return property->option("multiLine", false).toBool();
0031     }
0032 }
0033 
0034 class Q_DECL_HIDDEN KPropertyStringEditor::Private
0035 {
0036 public:
0037     Private() {
0038     }
0039     bool slotTextChangedEnabled = true;
0040 };
0041 
0042 
0043 KPropertyStringEditor::KPropertyStringEditor(QWidget *parent)
0044  : QLineEdit(parent), d(new Private)
0045 {
0046     setFrame(false);
0047     setContentsMargins(0,1,0,0);
0048     setClearButtonEnabled(true);
0049     connect(this, SIGNAL(textChanged(const QString&)), this, SLOT(slotTextChanged(const QString&)));
0050 }
0051 
0052 KPropertyStringEditor::~KPropertyStringEditor()
0053 {
0054     delete d;
0055 }
0056 
0057 QString KPropertyStringEditor::value() const
0058 {
0059     return text();
0060 }
0061 
0062 void KPropertyStringEditor::setValue(const QString& value)
0063 {
0064     d->slotTextChangedEnabled = false;
0065     setText(value);
0066     d->slotTextChangedEnabled = true;
0067 /*    deselect();
0068     end(false);*/
0069 }
0070 
0071 void KPropertyStringEditor::slotTextChanged( const QString & text )
0072 {
0073     Q_UNUSED(text)
0074     if (!d->slotTextChangedEnabled)
0075         return;
0076     emit commitData(this);
0077 }
0078 
0079 KPropertyStringDelegate::KPropertyStringDelegate()
0080 {
0081 }
0082 
0083 QWidget* KPropertyStringDelegate::createEditor( int type, QWidget *parent,
0084     const QStyleOptionViewItem & option, const QModelIndex & index ) const
0085 {
0086     Q_UNUSED(type);
0087     Q_UNUSED(option);
0088     KProperty *property = KPropertyUtils::propertyForIndex(index);
0089     if (!property) {
0090         return nullptr;
0091     }
0092     if (isMultiLine(property)) {
0093         return new KPropertyMultiLineStringEditor(parent);
0094     } else {
0095         return new KPropertyStringEditor(parent);
0096     }
0097 }
0098 
0099 void KPropertyStringDelegate::paint(QPainter *painter,
0100     const QStyleOptionViewItem &option, const QModelIndex &index) const
0101 {
0102     KProperty *property = KPropertyUtils::propertyForIndex(index);
0103     if (!property) {
0104         return;
0105     }
0106     QString string(index.data(Qt::EditRole).toString());
0107     if (string.isEmpty()) {
0108         return;
0109     }
0110     Qt::Alignment align = Qt::AlignLeft;
0111     QRect r(option.rect);
0112     r.setLeft(r.left() + 2);
0113     r.setTop(r.top() + 1);
0114     if (isMultiLine(property)) {
0115         align |= Qt::AlignTop;
0116         r.setLeft(r.left() + 1);
0117         const KPropertyEditorDataModel *editorModel
0118                 = qobject_cast<const KPropertyEditorDataModel*>(index.model());
0119         KPropertySet *propertySet = nullptr;
0120         if (editorModel) {
0121             propertySet = editorModel->propertySet();
0122         }
0123         const bool readOnly = property->isReadOnly() || (propertySet && propertySet->isReadOnly());
0124         QBrush fillBrush;
0125         if ((!(option.state & QStyle::State_Editing) && (option.state & QStyle::State_Selected))
0126             || ((option.state & QStyle::State_Selected) && readOnly))
0127         {
0128             fillBrush = option.palette.highlight();
0129         } else {
0130             fillBrush = option.palette.window();
0131         }
0132         painter->fillRect(option.rect, fillBrush);
0133     } else {
0134         const int newLineIndex = string.indexOf(QLatin1Char('\n'));
0135         if (newLineIndex >= 0) {
0136             string.truncate(newLineIndex);
0137             if (string.isEmpty()) {
0138                 return;
0139             }
0140         }
0141         align |= Qt::AlignVCenter;
0142     }
0143     const KPropertyUtilsPrivate::PainterSaver saver(painter);
0144     painter->drawText(r, align, string);
0145 }
0146 
0147 QString KPropertyStringDelegate::valueToString(const QVariant& value, const QLocale &locale) const
0148 {
0149     if (locale.language() == QLocale::C) {
0150         return value.toString();
0151     }
0152     return valueToLocalizedString(value);
0153 }