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

0001 /* This file is part of the KDE project
0002    Copyright (C) 2008-2015 Jarosław Staniek <staniek@kde.org>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "sizepolicyedit.h"
0021 #include "KPropertyCoreUtils_p.h"
0022 #include "KPropertyListData.h"
0023 
0024 #include <QSizePolicy>
0025 
0026 class SizePolicyListData : public KPropertyListData
0027 {
0028 public:
0029     SizePolicyListData() : KPropertyListData(keysInternal(), stringsInternal())
0030     {
0031     }
0032 
0033     QString nameForPolicy(QSizePolicy::Policy p) {
0034         const int index = keys().indexOf(static_cast<int>(p));
0035         if (index == -1)
0036             return names()[0].toString();
0037         return names()[index].toString();
0038     }
0039 
0040 private:
0041     static QList<QVariant> keysInternal() {
0042         QList<QVariant> keys;
0043         keys
0044          << QSizePolicy::Fixed
0045          << QSizePolicy::Minimum
0046          << QSizePolicy::Maximum
0047          << QSizePolicy::Preferred
0048          << QSizePolicy::Expanding
0049          << QSizePolicy::MinimumExpanding
0050          << QSizePolicy::Ignored;
0051         return keys;
0052     }
0053 
0054     static QStringList stringsInternal() {
0055         QStringList strings;
0056         strings
0057          << QObject::tr("Fixed", "Size Policy")
0058          << QObject::tr("Minimum", "Size Policy")
0059          << QObject::tr("Maximum", "Size Policy")
0060          << QObject::tr("Preferred", "Size Policy")
0061          << QObject::tr("Expanding", "Size Policy")
0062          << QObject::tr("Minimum Expanding", "Size Policy")
0063          << QObject::tr("Ignored", "Size Policy");
0064         return strings;
0065     }
0066 };
0067 
0068 Q_GLOBAL_STATIC(SizePolicyListData, s_sizePolicyListData)
0069 
0070 //---------
0071 
0072 KPropertySizePolicyDelegate::KPropertySizePolicyDelegate()
0073 {
0074 }
0075 
0076 QString KPropertySizePolicyDelegate::valueToString(const QVariant& value, const QLocale &locale) const
0077 {
0078     const QSizePolicy sp(value.value<QSizePolicy>());
0079     if (locale.language() == QLocale::C) {
0080         return QString::fromLatin1("%1, %2, %3, %4")
0081                 .arg(KPropertyUtils::keyForEnumValue("SizePolicy", sp.horizontalPolicy()))
0082                 .arg(KPropertyUtils::keyForEnumValue("SizePolicy", sp.verticalPolicy()))
0083                 .arg(sp.horizontalStretch())
0084                 .arg(sp.verticalStretch());
0085     }
0086     return QObject::tr("%1, %2, %3, %4", "Size Policy")
0087         .arg(s_sizePolicyListData->nameForPolicy(sp.horizontalPolicy()))
0088         .arg(s_sizePolicyListData->nameForPolicy(sp.verticalPolicy()))
0089         .arg(locale.toString(sp.horizontalStretch()))
0090         .arg(locale.toString(sp.verticalStretch()));
0091 }
0092 
0093 //static
0094 const KPropertyListData& KPropertySizePolicyDelegate::listData()
0095 {
0096     return *s_sizePolicyListData;
0097 }
0098 
0099 //------------
0100 
0101 KSizePolicyComposedProperty::KSizePolicyComposedProperty(KProperty *property)
0102         : KComposedPropertyInterface(property)
0103 {
0104     (void)new KProperty("hor_policy", new SizePolicyListData(),
0105         QVariant(), QObject::tr("Hor. Policy"), QObject::tr("Horizontal Policy"),
0106         KProperty::ValueFromList, property);
0107     (void)new KProperty("vert_policy", new SizePolicyListData(),
0108         QVariant(), QObject::tr("Vert. Policy"), QObject::tr("Vertical Policy"),
0109         KProperty::ValueFromList, property);
0110     (void)new KProperty("hor_stretch", QVariant(),
0111         QObject::tr("Hor. Stretch"), QObject::tr("Horizontal Stretch"),
0112         KProperty::UInt, property);
0113     (void)new KProperty("vert_stretch", QVariant(),
0114         QObject::tr("Vert. Stretch"), QObject::tr("Vertical Stretch"),
0115         KProperty::UInt, property);
0116 }
0117 
0118 void KSizePolicyComposedProperty::setValue(KProperty *property,
0119     const QVariant &value, KProperty::ValueOptions valueOptions)
0120 {
0121     const QSizePolicy sp( value.value<QSizePolicy>() );
0122     property->child("hor_policy")->setValue(sp.horizontalPolicy(), valueOptions);
0123     property->child("vert_policy")->setValue(sp.verticalPolicy(), valueOptions);
0124     property->child("hor_stretch")->setValue(sp.horizontalStretch(), valueOptions);
0125     property->child("vert_stretch")->setValue(sp.verticalStretch(), valueOptions);
0126 }
0127 
0128 void KSizePolicyComposedProperty::childValueChanged(KProperty *child,
0129     const QVariant &value, KProperty::ValueOptions valueOptions)
0130 {
0131     QSizePolicy sp( child->parent()->value().value<QSizePolicy>() );
0132     if (child->name() == "hor_policy")
0133         sp.setHorizontalPolicy(static_cast<QSizePolicy::Policy>(value.toInt()));
0134     else if (child->name() == "vert_policy")
0135         sp.setVerticalPolicy(static_cast<QSizePolicy::Policy>(value.toInt()));
0136     else if (child->name() == "hor_stretch")
0137         sp.setHorizontalStretch(value.toInt());
0138     else if (child->name() == "vert_stretch")
0139         sp.setVerticalStretch(value.toInt());
0140 
0141     child->parent()->setValue(sp, valueOptions);
0142 }
0143 
0144 bool KSizePolicyComposedProperty::valuesEqual(const QVariant &first, const QVariant &second)
0145 {
0146     return first.value<QSizePolicy>() == second.value<QSizePolicy>();
0147 }