KProperty

sizepolicyedit.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2008-2015 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 "sizepolicyedit.h"
21#include "KPropertyCoreUtils_p.h"
22#include "KPropertyListData.h"
23
24#include <QSizePolicy>
25
26class SizePolicyListData : public KPropertyListData
27{
28public:
29 SizePolicyListData() : KPropertyListData(keysInternal(), stringsInternal())
30 {
31 }
32
33 QString nameForPolicy(QSizePolicy::Policy p) {
34 const int index = keys().indexOf(static_cast<int>(p));
35 if (index == -1)
36 return names()[0].toString();
37 return names()[index].toString();
38 }
39
40private:
41 static QList<QVariant> keysInternal() {
43 keys
51 return keys;
52 }
53
54 static QStringList stringsInternal() {
55 QStringList strings;
56 strings
57 << QObject::tr("Fixed", "Size Policy")
58 << QObject::tr("Minimum", "Size Policy")
59 << QObject::tr("Maximum", "Size Policy")
60 << QObject::tr("Preferred", "Size Policy")
61 << QObject::tr("Expanding", "Size Policy")
62 << QObject::tr("Minimum Expanding", "Size Policy")
63 << QObject::tr("Ignored", "Size Policy");
64 return strings;
65 }
66};
67
68Q_GLOBAL_STATIC(SizePolicyListData, s_sizePolicyListData)
69
70//---------
71
72KPropertySizePolicyDelegate::KPropertySizePolicyDelegate()
73{
74}
75
76QString KPropertySizePolicyDelegate::valueToString(const QVariant& value, const QLocale &locale) const
77{
78 const QSizePolicy sp(value.value<QSizePolicy>());
79 if (locale.language() == QLocale::C) {
80 return QString::fromLatin1("%1, %2, %3, %4")
81 .arg(KPropertyUtils::keyForEnumValue("SizePolicy", sp.horizontalPolicy()))
82 .arg(KPropertyUtils::keyForEnumValue("SizePolicy", sp.verticalPolicy()))
83 .arg(sp.horizontalStretch())
84 .arg(sp.verticalStretch());
85 }
86 return QObject::tr("%1, %2, %3, %4", "Size Policy")
87 .arg(s_sizePolicyListData->nameForPolicy(sp.horizontalPolicy()))
88 .arg(s_sizePolicyListData->nameForPolicy(sp.verticalPolicy()))
89 .arg(locale.toString(sp.horizontalStretch()))
90 .arg(locale.toString(sp.verticalStretch()));
91}
92
93//static
94const KPropertyListData& KPropertySizePolicyDelegate::listData()
95{
96 return *s_sizePolicyListData;
97}
98
99//------------
100
101KSizePolicyComposedProperty::KSizePolicyComposedProperty(KProperty *property)
103{
104 (void)new KProperty("hor_policy", new SizePolicyListData(),
105 QVariant(), QObject::tr("Hor. Policy"), QObject::tr("Horizontal Policy"),
106 KProperty::ValueFromList, property);
107 (void)new KProperty("vert_policy", new SizePolicyListData(),
108 QVariant(), QObject::tr("Vert. Policy"), QObject::tr("Vertical Policy"),
109 KProperty::ValueFromList, property);
110 (void)new KProperty("hor_stretch", QVariant(),
111 QObject::tr("Hor. Stretch"), QObject::tr("Horizontal Stretch"),
112 KProperty::UInt, property);
113 (void)new KProperty("vert_stretch", QVariant(),
114 QObject::tr("Vert. Stretch"), QObject::tr("Vertical Stretch"),
115 KProperty::UInt, property);
116}
117
118void KSizePolicyComposedProperty::setValue(KProperty *property,
119 const QVariant &value, KProperty::ValueOptions valueOptions)
120{
121 const QSizePolicy sp( value.value<QSizePolicy>() );
122 property->child("hor_policy")->setValue(sp.horizontalPolicy(), valueOptions);
123 property->child("vert_policy")->setValue(sp.verticalPolicy(), valueOptions);
124 property->child("hor_stretch")->setValue(sp.horizontalStretch(), valueOptions);
125 property->child("vert_stretch")->setValue(sp.verticalStretch(), valueOptions);
126}
127
128void KSizePolicyComposedProperty::childValueChanged(KProperty *child,
129 const QVariant &value, KProperty::ValueOptions valueOptions)
130{
131 QSizePolicy sp( child->parent()->value().value<QSizePolicy>() );
132 if (child->name() == "hor_policy")
133 sp.setHorizontalPolicy(static_cast<QSizePolicy::Policy>(value.toInt()));
134 else if (child->name() == "vert_policy")
135 sp.setVerticalPolicy(static_cast<QSizePolicy::Policy>(value.toInt()));
136 else if (child->name() == "hor_stretch")
137 sp.setHorizontalStretch(value.toInt());
138 else if (child->name() == "vert_stretch")
139 sp.setVerticalStretch(value.toInt());
140
141 child->parent()->setValue(sp, valueOptions);
142}
143
144bool KSizePolicyComposedProperty::valuesEqual(const QVariant &first, const QVariant &second)
145{
146 return first.value<QSizePolicy>() == second.value<QSizePolicy>();
147}
An interface for for composed property handlers.
A data container for properties of list type.
QVariantList keys() const
A list containing all possible keys for a property.
QVariantList names() const
The list of user-visible translated name elements.
The base class representing a single property.
Definition KProperty.h:96
QVariant value() const
bool setValue(const QVariant &value, ValueOptions options=ValueOptions())
Sets value of the property.
QByteArray name() const
@ ValueFromList
string value from a list
Definition KProperty.h:151
KProperty * parent() const
QLocale::Language language() const const
QString toString(qlonglong i) const const
QString tr(const char *sourceText, const char *disambiguation, int n)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString fromLatin1(const char *str, int size)
int toInt(bool *ok) const const
T value() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sun Feb 25 2024 18:41:55 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.