kexi

objecttree.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2003 Lucijan Busch <lucijan@gmx.at>
3 Copyright (C) 2006 Jaroslaw Staniek <js@iidea.pl>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20
21#include <kdebug.h>
22#include <tqwidget.h>
23#include <tqvariant.h>
24#include <tqdom.h>
25#include <tqtextstream.h>
26
27#include "form.h"
28#include "container.h"
29#include "objecttree.h"
30
31
32using namespace KFormDesigner;
33
37
38
39ObjectTreeItem::ObjectTreeItem(const TQString &classn, const TQString &name, TQWidget *widget,
40 Container *parentContainer, Container *container)
41 : m_enabled(true), m_row(-1), m_col(-1), m_rowspan(-1), m_colspan(-1), m_span(false)
42{
43 m_className = classn;
44 m_name = name;
45 m_widget = widget;
46 m_container = container;
47 m_eater = new EventEater(widget, parentContainer);
48 m_parent = 0;
49 m_subprops = 0;
50}
51
52ObjectTreeItem::~ObjectTreeItem()
53{
54// kdDebug() << "ObjectTreeItem deleted: " << name() << endl;
55 delete m_subprops;
56}
57
58void
59ObjectTreeItem::rename(const TQString &name)
60{
61 m_name = name;
62}
63
64void
65ObjectTreeItem::addChild(ObjectTreeItem *c)
66{
67 m_children.append(c);
68 c->setParent(this);
69}
70
71void
72ObjectTreeItem::removeChild(ObjectTreeItem *c)
73{
74 m_children.remove(c);
75}
76
77void
78ObjectTreeItem::addModifiedProperty(const TQCString &property, const TQVariant &oldValue)
79{
80 if(property == "name")
81 return;
82
83 if(!m_props.contains(property)) {
84 m_props.insert(property, oldValue);
85 kdDebug() << "ObjectTree::adModProperty(): Added this property in the list: " << property << " oldValue: " << oldValue << endl;
86 }
87}
88
89void
90ObjectTreeItem::addSubproperty(const TQCString &property, const TQVariant& value)
91{
92 if (!m_subprops)
93 m_subprops = new TQStringVariantMap();
94 if (!m_props.contains(property))
95 m_subprops->insert( property, value );
96}
97
98void
99ObjectTreeItem::storeUnknownProperty(TQDomElement &el)
100{
101 if(!el.isNull()) {
102 TQTextStream ts(m_unknownProps, IO_WriteOnly|IO_Append );
103 el.save(ts, 0);
104 }
105}
106
107void
108ObjectTreeItem::setPixmapName(const TQCString &property, const TQString &name)
109{
110 m_pixmapNames[property] = name;
111}
112
113TQString
114ObjectTreeItem::pixmapName(const TQCString &property)
115{
116 if(m_pixmapNames.contains(property))
117 return m_pixmapNames[property];
118 return TQString();
119}
120
121void
122ObjectTreeItem::setGridPos(int row, int col, int rowspan, int colspan)
123{
124 m_row = row; m_col = col;
125 m_rowspan = rowspan;
126 m_colspan = colspan;
127 if(colspan || rowspan)
128 m_span = true;
129 else
130 m_span = false;
131}
132
136
137ObjectTree::ObjectTree(const TQString &classn, const TQString &name, TQWidget *widget, Container *container)
138 : ObjectTreeItem(classn, name, widget, container, container)
139{
140}
141
142ObjectTree::~ObjectTree()
143{
144// for(ObjectTreeItem *it = children()->first(); it; it = children()->next())
145// removeItem(it->name());
146 while (children()->first()) {
147 removeItem(children()->first());
148 }
149}
150
151bool
152ObjectTree::rename(const TQString &oldname, const TQString &newname)
153{
154 if(oldname == m_name)
155 {
156 ObjectTreeItem::rename(newname);
157 return true;
158 }
159
160 ObjectTreeItem *it = lookup(oldname);
161 if(!it)
162 return false;
163
164 it->rename(newname);
165 m_treeDict.remove(oldname);
166 m_treeDict.insert(newname, it);
167
168 return true;
169}
170
171bool
172ObjectTree::reparent(const TQString &name, const TQString &newparent)
173{
174 ObjectTreeItem *item = lookup(name);
175 if(!item) return false;
176 ObjectTreeItem *parent = lookup(newparent);
177 if(!parent) return false;
178
179 item->parent()->removeChild(item);
180 parent->addChild(item);
181 return true;
182}
183
185ObjectTree::lookup(const TQString &name)
186{
187 if(name == this->name())
188 return this;
189 else
190 return m_treeDict[name];
191}
192
193void
194ObjectTree::addItem(ObjectTreeItem *parent, ObjectTreeItem *c)
195{
196 m_treeDict.insert(c->name(), c);
197
198 if(!parent)
199 parent = this;
200 parent->addChild(c);
201 m_container->form()->emitChildAdded(c);
202
203 kdDebug() << "ObjectTree::addItem(): adding " << c->name() << " to " << parent->name() << endl;
204}
205
206void
207ObjectTree::removeItem(const TQString &name)
208{
209 ObjectTreeItem *c = lookup(name);
210 removeItem(c);
211}
212
213void
214ObjectTree::removeItem(ObjectTreeItem *c)
215{
216 if (m_container && m_container->form())
217 m_container->form()->emitChildRemoved(c);
218
219 for(ObjectTreeItem *it = c->children()->first(); it; it = c->children()->next())
220 removeItem(it->name());
221
222 m_treeDict.remove(c->name());
223 c->parent()->removeChild(c);
224 delete c;
225}
226
227TQCString
228ObjectTree::generateUniqueName(const TQCString &prefix, bool numberSuffixRequired)
229{
230 /* old way of naming widgets
231 int appendix = m_names[c] + 1;
232 TQString name(c);
233 name.append(TQString::number(appendix));
234 m_names[c] = appendix;*/
235 if (!numberSuffixRequired && !lookup(prefix))
236 return prefix;
237 TQString name( prefix );
238 int i = 2; //start from 2, i.e. we have: "widget", "widget2", etc.
239 while(lookup(name + TQString::number(i)))
240 i++;
241
242 return (name + TQString::number(i)).latin1();
243}
244
This class makes a container out of any TQWidget.
Definition container.h:74
This class is used to filter the events from any widget (and all its subwidgets) and direct it to the...
Definition container.h:48
An item representing a widget Holds the properties of a widget (classname, name, parent,...
Definition objecttree.h:59
Container * container() const
Definition objecttree.h:76
void addModifiedProperty(const TQCString &property, const TQVariant &oldValue)
void addSubproperty(const TQCString &property, const TQVariant &value)
bool rename(const TQString &oldname, const TQString &newname)
TQCString generateUniqueName(const TQCString &prefix, bool numberSuffixRequired=true)
ObjectTree(const TQString &className=TQString(), const TQString &name=TQString(), TQWidget *widget=0, Container *container=0)
ObjectTree /////////.
ObjectTreeItem * lookup(const TQString &name)
bool reparent(const TQString &name, const TQString &newparent)