summaryrefslogtreecommitdiffstats
path: root/kregexpeditor/KWidgetStreamer/kwidgetstreamer.cpp
blob: 113e3d1ef840053cd0b215eee0977b4da2e02734 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
 *  Copyright (c) 2002-2003 Jesper K. Pedersen <blackie@kde.org>
 *
 *  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 <tqobjectlist.h>
#include <tqvariant.h>


void KWidgetStreamer::toStream(const TQObject* from, TQDataStream& stream )
{
  if ( from->inherits("KMultiFormListBox") ) {
    // Hmm, we'll trust Qt that this dynamic_cast won't fail!
    dynamic_cast<const KMultiFormListBox*>(from)->toStream( stream );
  }

  propertyToStream( from, stream );
}

void KWidgetStreamer::fromStream( TQDataStream& stream, TQObject* to )
{
  if ( to->inherits("KMultiFormListBox") ) {
    // Hmm, we'll trust Qt that this dynamic_cast won't fail!
    dynamic_cast<KMultiFormListBox*>(to)->fromStream( stream );
  }

  propertyFromStream( stream, to );
}


void KWidgetStreamer::propertyToStream( const TQObject* from, TQDataStream& stream )
{
  // Only handle widgets. Alternatives to widgets are layouts, validators, timers, etc.
  if ( ! from->inherits(TQWIDGET_OBJECT_NAME_STRING) )
    return;

  // Serializing all the children (if any).
  const TQObjectList* children = from->children();
  if ( children ) {
    stream <<  children->count();
    for ( TQObjectListIt it = TQObjectListIt(*children); *it; ++it ) {
      toStream( *it, stream );
    }
  }
  else {
    stream << (unsigned int) 0;
  }

  // Now stream out properties
  for ( PropertyMapIt mapIt = _map.begin(); mapIt != _map.end(); mapIt++ ) {
    TQString tp = mapIt.key();
    PropertyList list = mapIt.data();
    if ( from->inherits( tp.latin1() ) ) {
      for ( PropertyListIt it = list.begin(); it != list.end(); ++it ) {
        TQVariant prop = from->property( (*it).latin1() );
        if ( ! prop.isValid() )
          qWarning("Invalid property: %s:%s", tp.latin1(), (*it).latin1() );

        stream <<  prop ;
      }
    }
  }
}


void KWidgetStreamer::propertyFromStream( TQDataStream& stream, TQObject* to )
{
  // Only handle widgets. Alternatives to widgets are layouts, validators, timers, etc.
  if ( ! to->inherits(TQWIDGET_OBJECT_NAME_STRING) )
    return;

  // Stream in all the children (if any)
  const TQObjectList* children = to->children();
  unsigned int count;

  stream >> count;
  if ( children ) {
    Q_ASSERT( count == children->count() );
    for ( TQObjectListIt it = TQObjectListIt(*children); *it; ++it )
      fromStream( stream, *it );
  }
  else {
    Q_ASSERT( count == 0 );
  }

  // Now stream in properties
  for ( PropertyMapIt mapIt = _map.begin(); mapIt != _map.end(); mapIt++ ) {
    TQString tp = mapIt.key();
    PropertyList list = mapIt.data();
    if ( to->inherits( tp.latin1() ) ) {
      for ( PropertyListIt it = list.begin(); it != list.end(); ++it ) {
         TQVariant value;
        stream >> value;
        to->setProperty((*it).latin1(), value);
      }
    }
  }
}

KWidgetStreamer::KWidgetStreamer ()
{
  TQStringList l;

  // QCheckBox
  l.clear();
  l << TQString::fromLatin1("enabled")
    << TQString::fromLatin1("checked") << TQString::fromLatin1("tristate");
  _map.insert(TQString::fromLatin1(TQCHECKBOX_OBJECT_NAME_STRING), l);

  // QComboBox
  l.clear();
  l << TQString::fromLatin1("enabled")
    << TQString::fromLatin1("editable") << TQString::fromLatin1("currentItem")
    << TQString::fromLatin1("maxCount") << TQString::fromLatin1("insertionPolicy")
    << TQString::fromLatin1("autoCompletion");
  _map.insert(TQString::fromLatin1(TQCOMBOBOX_OBJECT_NAME_STRING), l);

  // QDial
  l.clear();
  l << TQString::fromLatin1("enabled")
    << TQString::fromLatin1("tracking") << TQString::fromLatin1("wrapping")
    << TQString::fromLatin1("value");
  _map.insert(TQString::fromLatin1(TQDIAL_OBJECT_NAME_STRING), l);

  // QLCDNumber
  l.clear();
  l << TQString::fromLatin1("enabled")
    << TQString::fromLatin1("numDigits") << TQString::fromLatin1("mode")
    << TQString::fromLatin1("segmentStyle") << TQString::fromLatin1("value");
  _map.insert(TQString::fromLatin1(TQLCDNUMBER_OBJECT_NAME_STRING), l);

  // QLineEdit
  l.clear();
  l << TQString::fromLatin1("enabled")
    << TQString::fromLatin1("text") << TQString::fromLatin1("maxLength")
    << TQString::fromLatin1("echoMode") << TQString::fromLatin1("alignment");
  _map.insert(TQString::fromLatin1(TQLINEEDIT_OBJECT_NAME_STRING), l);

  // QMultiLineEdit
  l.clear();
  l << TQString::fromLatin1("enabled")
    << TQString::fromLatin1("text")
    << TQString::fromLatin1("alignment");
  _map.insert(TQString::fromLatin1(TQTEXTEDIT_OBJECT_NAME_STRING), l);

  // QRadioButton
  l.clear();
  l << TQString::fromLatin1("enabled")
    << TQString::fromLatin1("checked");
  _map.insert(TQString::fromLatin1(TQRADIOBUTTON_OBJECT_NAME_STRING), l);

  // QSlider
  l.clear();
  l << TQString::fromLatin1("enabled")
    << TQString::fromLatin1("value");
  _map.insert(TQString::fromLatin1(TQSLIDER_OBJECT_NAME_STRING), l);

  // QSpinBox
  l.clear();
  l << TQString::fromLatin1("enabled")
    << TQString::fromLatin1("value");
  _map.insert(TQString::fromLatin1(TQSPINBOX_OBJECT_NAME_STRING), l);
}