From 2bda8f7717adf28da4af0d34fb82f63d2868c31d Mon Sep 17 00:00:00 2001 From: toma Date: Wed, 25 Nov 2009 17:56:58 +0000 Subject: Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features. BUG:215923 git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdeutils@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- kregexpeditor/KWidgetStreamer/Makefile.am | 9 ++ kregexpeditor/KWidgetStreamer/kwidgetstreamer.cpp | 177 ++++++++++++++++++++++ kregexpeditor/KWidgetStreamer/kwidgetstreamer.h | 74 +++++++++ 3 files changed, 260 insertions(+) create mode 100644 kregexpeditor/KWidgetStreamer/Makefile.am create mode 100644 kregexpeditor/KWidgetStreamer/kwidgetstreamer.cpp create mode 100644 kregexpeditor/KWidgetStreamer/kwidgetstreamer.h (limited to 'kregexpeditor/KWidgetStreamer') diff --git a/kregexpeditor/KWidgetStreamer/Makefile.am b/kregexpeditor/KWidgetStreamer/Makefile.am new file mode 100644 index 0000000..c9b1260 --- /dev/null +++ b/kregexpeditor/KWidgetStreamer/Makefile.am @@ -0,0 +1,9 @@ +AM_CPPFLAGS = -DQT_NO_CAST_ASCII +noinst_LTLIBRARIES = libkwidgetstreamer.la + +INCLUDES= -I$(srcdir)/../KMultiFormListBox $(all_includes) + +include_HEADERS = kwidgetstreamer.h +libkwidgetstreamer_la_SOURCES = kwidgetstreamer.cpp + +METASOURCES = AUTO diff --git a/kregexpeditor/KWidgetStreamer/kwidgetstreamer.cpp b/kregexpeditor/KWidgetStreamer/kwidgetstreamer.cpp new file mode 100644 index 0000000..e91813a --- /dev/null +++ b/kregexpeditor/KWidgetStreamer/kwidgetstreamer.cpp @@ -0,0 +1,177 @@ +/* + * Copyright (c) 2002-2003 Jesper K. Pedersen + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License version 2 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + **/ +#include "kwidgetstreamer.h" +#include "kmultiformlistbox.h" +#include +#include + + +void KWidgetStreamer::toStream(const QObject* from, QDataStream& stream ) +{ + if ( from->inherits("KMultiFormListBox") ) { + // Hmm, we'll trust Qt that this dynamic_cast won't fail! + dynamic_cast(from)->toStream( stream ); + } + + propertyToStream( from, stream ); +} + +void KWidgetStreamer::fromStream( QDataStream& stream, QObject* to ) +{ + if ( to->inherits("KMultiFormListBox") ) { + // Hmm, we'll trust Qt that this dynamic_cast won't fail! + dynamic_cast(to)->fromStream( stream ); + } + + propertyFromStream( stream, to ); +} + + +void KWidgetStreamer::propertyToStream( const QObject* from, QDataStream& stream ) +{ + // Only handle widgets. Alternatives to widgets are layouts, validators, timers, etc. + if ( ! from->inherits("QWidget") ) + return; + + // Serializing all the children (if any). + const QObjectList* children = from->children(); + if ( children ) { + stream << children->count(); + for ( QObjectListIt it = QObjectListIt(*children); *it; ++it ) { + toStream( *it, stream ); + } + } + else { + stream << (unsigned int) 0; + } + + // Now stream out properties + for ( PropertyMapIt mapIt = _map.begin(); mapIt != _map.end(); mapIt++ ) { + QString tp = mapIt.key(); + PropertyList list = mapIt.data(); + if ( from->inherits( tp.latin1() ) ) { + for ( PropertyListIt it = list.begin(); it != list.end(); ++it ) { + QVariant prop = from->property( (*it).latin1() ); + if ( ! prop.isValid() ) + qWarning("Invalid property: %s:%s", tp.latin1(), (*it).latin1() ); + + stream << prop ; + } + } + } +} + + +void KWidgetStreamer::propertyFromStream( QDataStream& stream, QObject* to ) +{ + // Only handle widgets. Alternatives to widgets are layouts, validators, timers, etc. + if ( ! to->inherits("QWidget") ) + return; + + // Stream in all the children (if any) + const QObjectList* children = to->children(); + unsigned int count; + + stream >> count; + if ( children ) { + Q_ASSERT( count == children->count() ); + for ( QObjectListIt it = QObjectListIt(*children); *it; ++it ) + fromStream( stream, *it ); + } + else { + Q_ASSERT( count == 0 ); + } + + // Now stream in properties + for ( PropertyMapIt mapIt = _map.begin(); mapIt != _map.end(); mapIt++ ) { + QString tp = mapIt.key(); + PropertyList list = mapIt.data(); + if ( to->inherits( tp.latin1() ) ) { + for ( PropertyListIt it = list.begin(); it != list.end(); ++it ) { + QVariant value; + stream >> value; + to->setProperty((*it).latin1(), value); + } + } + } +} + +KWidgetStreamer::KWidgetStreamer () +{ + QStringList l; + + // QCheckBox + l.clear(); + l << QString::fromLatin1("enabled") + << QString::fromLatin1("checked") << QString::fromLatin1("tristate"); + _map.insert(QString::fromLatin1("QCheckBox"), l); + + // QComboBox + l.clear(); + l << QString::fromLatin1("enabled") + << QString::fromLatin1("editable") << QString::fromLatin1("currentItem") + << QString::fromLatin1("maxCount") << QString::fromLatin1("insertionPolicy") + << QString::fromLatin1("autoCompletion"); + _map.insert(QString::fromLatin1("QComboBox"), l); + + // QDial + l.clear(); + l << QString::fromLatin1("enabled") + << QString::fromLatin1("tracking") << QString::fromLatin1("wrapping") + << QString::fromLatin1("value"); + _map.insert(QString::fromLatin1("QDial"), l); + + // QLCDNumber + l.clear(); + l << QString::fromLatin1("enabled") + << QString::fromLatin1("numDigits") << QString::fromLatin1("mode") + << QString::fromLatin1("segmentStyle") << QString::fromLatin1("value"); + _map.insert(QString::fromLatin1("QLCDNumber"), l); + + // QLineEdit + l.clear(); + l << QString::fromLatin1("enabled") + << QString::fromLatin1("text") << QString::fromLatin1("maxLength") + << QString::fromLatin1("echoMode") << QString::fromLatin1("alignment"); + _map.insert(QString::fromLatin1("QLineEdit"), l); + + // QMultiLineEdit + l.clear(); + l << QString::fromLatin1("enabled") + << QString::fromLatin1("text") + << QString::fromLatin1("alignment"); + _map.insert(QString::fromLatin1("QTextEdit"), l); + + // QRadioButton + l.clear(); + l << QString::fromLatin1("enabled") + << QString::fromLatin1("checked"); + _map.insert(QString::fromLatin1("QRadioButton"), l); + + // QSlider + l.clear(); + l << QString::fromLatin1("enabled") + << QString::fromLatin1("value"); + _map.insert(QString::fromLatin1("QSlider"), l); + + // QSpinBox + l.clear(); + l << QString::fromLatin1("enabled") + << QString::fromLatin1("value"); + _map.insert(QString::fromLatin1("QSpinBox"), l); +} diff --git a/kregexpeditor/KWidgetStreamer/kwidgetstreamer.h b/kregexpeditor/KWidgetStreamer/kwidgetstreamer.h new file mode 100644 index 0000000..9534a92 --- /dev/null +++ b/kregexpeditor/KWidgetStreamer/kwidgetstreamer.h @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2002-2003 Jesper K. Pedersen + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License version 2 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + **/ +#ifndef __kwidgetstreamer +#define __kwidgetstreamer +#include +#include +#include +#include + +/** + This class defines methods for streaming widget data. + + For each widget type rules are defined telling which attributes should be + streamed. If you need to stream other attributes or to avoid streaming + certain attributes, then this may be obtained by editing the property + map, which is obtained by calling @ref propertyMap. For further control + inherit from this class and override @ref toStream and @ref fromStream. + + The following example shows how you can avoid streaming + numDigits for a QLCDNumber. The same approach applies if you + want to add extra properties to be streamed. +
+   KWidgetStreamer streamer;
+   KWidgetStreamer::PropertyMap& map = streamer.propertyMap();
+   KWidgetStreamer::PropertyList& list = *map.find("QLCDNumber");
+   list.remove("numDigits");
+   
+**/ +class KWidgetStreamer +{ + +public: + typedef QStringList PropertyList; + typedef QMap< QString, PropertyList > PropertyMap; + typedef QMap< QString, PropertyList >::ConstIterator PropertyMapIt; + typedef QStringList::Iterator PropertyListIt; + + KWidgetStreamer(); + virtual ~KWidgetStreamer() {} + + virtual void toStream(const QObject* from, QDataStream& stream ); + virtual void fromStream(QDataStream& stream, QObject* to); + + PropertyMap& propertyMap() { return _map; } + + +protected: + void propertyToStream( const QObject* from, QDataStream& stream ); + void propertyFromStream( QDataStream& stream, QObject* to ); + +private: + PropertyMap _map; + +}; + + + +#endif /* __kwidgetstreamer */ + -- cgit v1.2.3