KProperty

sizepolicyedit.cpp
1 /* This file is part of the KDE project
2  Copyright (C) 2008-2015 Jarosław Staniek <[email protected]>
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 
26 class SizePolicyListData : public KPropertyListData
27 {
28 public:
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 
40 private:
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 
68 Q_GLOBAL_STATIC(SizePolicyListData, s_sizePolicyListData)
69 
70 //---------
71 
72 KPropertySizePolicyDelegate::KPropertySizePolicyDelegate()
73 {
74 }
75 
76 QString 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
94 const KPropertyListData& KPropertySizePolicyDelegate::listData()
95 {
96  return *s_sizePolicyListData;
97 }
98 
99 //------------
100 
101 KSizePolicyComposedProperty::KSizePolicyComposedProperty(KProperty *property)
102  : KComposedPropertyInterface(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 
118 void 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 
128 void 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 
144 bool KSizePolicyComposedProperty::valuesEqual(const QVariant &first, const QVariant &second)
145 {
146  return first.value<QSizePolicy>() == second.value<QSizePolicy>();
147 }
QString toString(qlonglong i) const const
KProperty * parent() const
Definition: KProperty.cpp:673
string value from a list
Definition: KProperty.h:151
T value() const const
QVariantList names() const
The list of user-visible translated name elements.
QString tr(const char *sourceText, const char *disambiguation, int n)
An interface for for composed property handlers.
int toInt(bool *ok) const const
QLocale::Language language() const const
QByteArray name() const
Definition: KProperty.cpp:324
The base class representing a single property.
Definition: KProperty.h:95
QVariant value() const
Definition: KProperty.cpp:394
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString fromLatin1(const char *str, int size)
QVariantList keys() const
A list containing all possible keys for a property.
bool setValue(const QVariant &value, ValueOptions options=ValueOptions())
Sets value of the property.
Definition: KProperty.cpp:405
A data container for properties of list type.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun Aug 9 2020 23:10:57 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.