KProperty

KPropertyStringEditor.cpp
1 /* This file is part of the KDE project
2  Copyright (C) 2004 Cedric Pasteur <[email protected]>
3  Copyright (C) 2004 Alexander Dymo <[email protected]>
4  Copyright (C) 2008-2016 Jarosław Staniek <[email protected]>
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 
28 namespace {
29  bool isMultiLine(const KProperty *property) {
30  return property->option("multiLine", false).toBool();
31  }
32 }
33 
34 class Q_DECL_HIDDEN KPropertyStringEditor::Private
35 {
36 public:
37  Private() {
38  }
39  bool slotTextChangedEnabled = true;
40 };
41 
42 
43 KPropertyStringEditor::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 
52 KPropertyStringEditor::~KPropertyStringEditor()
53 {
54  delete d;
55 }
56 
57 QString KPropertyStringEditor::value() const
58 {
59  return text();
60 }
61 
62 void KPropertyStringEditor::setValue(const QString& value)
63 {
64  d->slotTextChangedEnabled = false;
65  setText(value);
66  d->slotTextChangedEnabled = true;
67 /* deselect();
68  end(false);*/
69 }
70 
71 void KPropertyStringEditor::slotTextChanged( const QString & text )
72 {
73  Q_UNUSED(text)
74  if (!d->slotTextChangedEnabled)
75  return;
76  emit commitData(this);
77 }
78 
79 KPropertyStringDelegate::KPropertyStringDelegate()
80 {
81 }
82 
83 QWidget* 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 
99 void 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 
147 QString 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 }
void fillRect(const QRectF &rectangle, const QBrush &brush)
bool isEmpty() const
typedef Alignment
Editor for string type supporting multiple lines of plain text.
EditRole
bool isReadOnly() const
void drawText(const QPointF &position, const QString &text)
QLocale::Language language() const const
Set of properties.
Definition: KPropertySet.h:119
The base class representing a single property.
Definition: KProperty.h:95
const QAbstractItemModel * model() const const
QVariant data(int role) const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
T qobject_cast(QObject *object)
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Tue Jul 14 2020 23:03:15 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.