KProperty

KPropertyStringEditor.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
3 Copyright (C) 2004 Alexander Dymo <cloudtemple@mskat.net>
4 Copyright (C) 2008-2016 Jarosław Staniek <staniek@kde.org>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20*/
21
22#include "KPropertyStringEditor.h"
23#include "KPropertyMultiLineStringEditor.h"
24#include "KPropertyUtils.h"
25#include "KPropertyUtils_p.h"
26#include "KPropertyEditorDataModel_p.h"
27
28namespace {
29 bool isMultiLine(const KProperty *property) {
30 return property->option("multiLine", false).toBool();
31 }
32}
33
34class Q_DECL_HIDDEN KPropertyStringEditor::Private
35{
36public:
37 Private() {
38 }
39 bool slotTextChangedEnabled = true;
40};
41
42
43KPropertyStringEditor::KPropertyStringEditor(QWidget *parent)
44 : QLineEdit(parent), d(new Private)
45{
46 setFrame(false);
47 setContentsMargins(0,1,0,0);
48 setClearButtonEnabled(true);
49 connect(this, SIGNAL(textChanged(const QString&)), this, SLOT(slotTextChanged(const QString&)));
50}
51
52KPropertyStringEditor::~KPropertyStringEditor()
53{
54 delete d;
55}
56
57QString KPropertyStringEditor::value() const
58{
59 return text();
60}
61
62void KPropertyStringEditor::setValue(const QString& value)
63{
64 d->slotTextChangedEnabled = false;
65 setText(value);
66 d->slotTextChangedEnabled = true;
67/* deselect();
68 end(false);*/
69}
70
71void KPropertyStringEditor::slotTextChanged( const QString & text )
72{
73 Q_UNUSED(text)
74 if (!d->slotTextChangedEnabled)
75 return;
76 emit commitData(this);
77}
78
79KPropertyStringDelegate::KPropertyStringDelegate()
80{
81}
82
83QWidget* KPropertyStringDelegate::createEditor( int type, QWidget *parent,
84 const QStyleOptionViewItem & option, const QModelIndex & index ) const
85{
86 Q_UNUSED(type);
87 Q_UNUSED(option);
88 KProperty *property = KPropertyUtils::propertyForIndex(index);
89 if (!property) {
90 return nullptr;
91 }
92 if (isMultiLine(property)) {
93 return new KPropertyMultiLineStringEditor(parent);
94 } else {
95 return new KPropertyStringEditor(parent);
96 }
97}
98
99void KPropertyStringDelegate::paint(QPainter *painter,
100 const QStyleOptionViewItem &option, const QModelIndex &index) const
101{
102 KProperty *property = KPropertyUtils::propertyForIndex(index);
103 if (!property) {
104 return;
105 }
106 QString string(index.data(Qt::EditRole).toString());
107 if (string.isEmpty()) {
108 return;
109 }
111 QRect r(option.rect);
112 r.setLeft(r.left() + 2);
113 r.setTop(r.top() + 1);
114 if (isMultiLine(property)) {
115 align |= Qt::AlignTop;
116 r.setLeft(r.left() + 1);
117 const KPropertyEditorDataModel *editorModel
118 = qobject_cast<const KPropertyEditorDataModel*>(index.model());
119 KPropertySet *propertySet = nullptr;
120 if (editorModel) {
121 propertySet = editorModel->propertySet();
122 }
123 const bool readOnly = property->isReadOnly() || (propertySet && propertySet->isReadOnly());
124 QBrush fillBrush;
125 if ((!(option.state & QStyle::State_Editing) && (option.state & QStyle::State_Selected))
126 || ((option.state & QStyle::State_Selected) && readOnly))
127 {
128 fillBrush = option.palette.highlight();
129 } else {
130 fillBrush = option.palette.window();
131 }
132 painter->fillRect(option.rect, fillBrush);
133 } else {
134 const int newLineIndex = string.indexOf(QLatin1Char('\n'));
135 if (newLineIndex >= 0) {
136 string.truncate(newLineIndex);
137 if (string.isEmpty()) {
138 return;
139 }
140 }
141 align |= Qt::AlignVCenter;
142 }
143 const KPropertyUtilsPrivate::PainterSaver saver(painter);
144 painter->drawText(r, align, string);
145}
146
147QString KPropertyStringDelegate::valueToString(const QVariant& value, const QLocale &locale) const
148{
149 if (locale.language() == QLocale::C) {
150 return value.toString();
151 }
152 return valueToLocalizedString(value);
153}
Editor for string type supporting multiple lines of plain text.
Set of properties.
bool isReadOnly() const
static QString valueToLocalizedString(const QVariant &value)
The base class representing a single property.
Definition KProperty.h:96
Language language() const const
QVariant data(int role) const const
const QAbstractItemModel * model() const const
void drawText(const QPoint &position, const QString &text)
void fillRect(const QRect &rectangle, QGradient::Preset preset)
typedef Alignment
EditRole
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 13 2024 11:57:19 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.