File indexing completed on 2024-04-28 05:51:10
0001 /* 0002 * SPDX-FileCopyrightText: 2002-2003 Jesper K. Pedersen <blackie@kde.org> 0003 * 0004 * SPDX-License-Identifier: LGPL-2.0-only 0005 **/ 0006 0007 #include "kwidgetstreamer.h" 0008 0009 #include <QObject> 0010 #include <QVariant> 0011 0012 #include "kmultiformlistbox.h" 0013 0014 void KWidgetStreamer::toStream(const QObject *from, QDataStream &stream) 0015 { 0016 if (from->inherits("KMultiFormListBox")) { 0017 // Hmm, we'll trust Qt that this dynamic_cast won't fail! 0018 dynamic_cast<const KMultiFormListBox *>(from)->toStream(stream); 0019 } 0020 0021 propertyToStream(from, stream); 0022 } 0023 0024 void KWidgetStreamer::fromStream(QDataStream &stream, QObject *to) 0025 { 0026 if (to->inherits("KMultiFormListBox")) { 0027 // Hmm, we'll trust Qt that this dynamic_cast won't fail! 0028 dynamic_cast<KMultiFormListBox *>(to)->fromStream(stream); 0029 } 0030 0031 propertyFromStream(stream, to); 0032 } 0033 0034 void KWidgetStreamer::propertyToStream(const QObject *from, QDataStream &stream) 0035 { 0036 // Only handle widgets. Alternatives to widgets are layouts, validators, timers, etc. 0037 if (!from->inherits("QWidget")) { 0038 return; 0039 } 0040 0041 // Stream in all the children (if any) 0042 const QList<QObject *> children = from->children(); 0043 // unsigned int count; 0044 0045 // stream >> count; 0046 if (children.count() > 0) { 0047 stream << children.count(); 0048 for (int i = 0; i < children.size(); ++i) { 0049 toStream(children.at(i), stream); 0050 } 0051 } else { 0052 stream << (unsigned int)0; 0053 } 0054 0055 // Now stream out properties 0056 for (PropertyMapIt mapIt = _map.constBegin(); mapIt != _map.constEnd(); ++mapIt) { 0057 QString tp = mapIt.key(); 0058 PropertyList list = mapIt.value(); 0059 if (from->inherits(tp.toLocal8Bit().data())) { 0060 for (PropertyListIt it = list.begin(); it != list.end(); ++it) { 0061 QVariant prop = from->property(it->toLocal8Bit().data()); 0062 if (!prop.isValid()) { 0063 qWarning("Invalid property: %s:%s", qPrintable(tp), qPrintable(*it)); 0064 } 0065 0066 stream << prop; 0067 } 0068 } 0069 } 0070 } 0071 0072 void KWidgetStreamer::propertyFromStream(QDataStream &stream, QObject *to) 0073 { 0074 // Only handle widgets. Alternatives to widgets are layouts, validators, timers, etc. 0075 if (!to->inherits("QWidget")) { 0076 return; 0077 } 0078 0079 // Stream in all the children (if any) 0080 const QList<QObject *> children = to->children(); 0081 // const QObjectList* children = to->children(); 0082 0083 /* unsigned int count; 0084 0085 stream >> count; 0086 0087 Q_ASSERT( count == 0 ); 0088 */ 0089 0090 for (int i = 0; i < children.size(); ++i) { 0091 fromStream(stream, children.at(i)); 0092 } 0093 0094 // Now stream in properties 0095 for (PropertyMapIt mapIt = _map.constBegin(); mapIt != _map.constEnd(); ++mapIt) { 0096 QString tp = mapIt.key(); 0097 PropertyList list = mapIt.value(); 0098 if (to->inherits(tp.toLocal8Bit().data())) { 0099 for (PropertyListIt it = list.begin(); it != list.end(); ++it) { 0100 QVariant value; 0101 stream >> value; 0102 to->setProperty(it->toLocal8Bit().data(), value); 0103 } 0104 } 0105 } 0106 } 0107 0108 KWidgetStreamer::KWidgetStreamer() 0109 { 0110 QStringList l; 0111 0112 // QCheckBox 0113 l.clear(); 0114 l << QStringLiteral("enabled") << QStringLiteral("checked") << QStringLiteral("tristate"); 0115 _map.insert(QStringLiteral("QCheckBox"), l); 0116 0117 // QComboBox 0118 l.clear(); 0119 l << QStringLiteral("enabled") << QStringLiteral("editable") << QStringLiteral("currentItem") << QStringLiteral("maxCount") 0120 << QStringLiteral("insertionPolicy") << QStringLiteral("autoCompletion"); 0121 _map.insert(QStringLiteral("QComboBox"), l); 0122 0123 // QDial 0124 l.clear(); 0125 l << QStringLiteral("enabled") << QStringLiteral("tracking") << QStringLiteral("wrapping") << QStringLiteral("value"); 0126 _map.insert(QStringLiteral("QDial"), l); 0127 0128 // QLCDNumber 0129 l.clear(); 0130 l << QStringLiteral("enabled") << QStringLiteral("digitCount") << QStringLiteral("mode") << QStringLiteral("segmentStyle") << QStringLiteral("value"); 0131 _map.insert(QStringLiteral("QLCDNumber"), l); 0132 0133 // QLineEdit 0134 l.clear(); 0135 l << QStringLiteral("enabled") << QStringLiteral("text") << QStringLiteral("maxLength") << QStringLiteral("echoMode") << QStringLiteral("alignment"); 0136 _map.insert(QStringLiteral("QLineEdit"), l); 0137 0138 // QMultiLineEdit 0139 l.clear(); 0140 l << QStringLiteral("enabled") << QStringLiteral("plainText"); 0141 _map.insert(QStringLiteral("QTextEdit"), l); 0142 0143 // QRadioButton 0144 l.clear(); 0145 l << QStringLiteral("enabled") << QStringLiteral("checked"); 0146 _map.insert(QStringLiteral("QRadioButton"), l); 0147 0148 // QSlider 0149 l.clear(); 0150 l << QStringLiteral("enabled") << QStringLiteral("value"); 0151 _map.insert(QStringLiteral("QSlider"), l); 0152 0153 // QSpinBox 0154 l.clear(); 0155 l << QStringLiteral("enabled") << QStringLiteral("value"); 0156 _map.insert(QStringLiteral("QSpinBox"), l); 0157 }