kexi

objecttree.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2003 Lucijan Busch <lucijan@gmx.at>
00003    Copyright (C) 2006 Jaroslaw Staniek <js@iidea.pl>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include <kdebug.h>
00022 #include <qwidget.h>
00023 #include <qvariant.h>
00024 #include <qdom.h>
00025 #include <qtextstream.h>
00026 
00027 #include "form.h"
00028 #include "container.h"
00029 #include "objecttree.h"
00030 
00031 
00032 using namespace KFormDesigner;
00033 
00037 
00038 
00039 ObjectTreeItem::ObjectTreeItem(const QString &classn, const QString &name, QWidget *widget,
00040  Container *parentContainer, Container *container)
00041  : m_enabled(true), m_row(-1), m_col(-1), m_rowspan(-1), m_colspan(-1), m_span(false)
00042 {
00043     m_className = classn;
00044     m_name = name;
00045     m_widget = widget;
00046     m_container = container;
00047     m_eater = new EventEater(widget, parentContainer);
00048     m_parent = 0;
00049     m_subprops = 0;
00050 }
00051 
00052 ObjectTreeItem::~ObjectTreeItem()
00053 {
00054 //  kdDebug() << "ObjectTreeItem deleted: " << name() << endl;
00055     delete m_subprops;
00056 }
00057 
00058 void
00059 ObjectTreeItem::rename(const QString &name)
00060 {
00061     m_name = name;
00062 }
00063 
00064 void
00065 ObjectTreeItem::addChild(ObjectTreeItem *c)
00066 {
00067     m_children.append(c);
00068     c->setParent(this);
00069 }
00070 
00071 void
00072 ObjectTreeItem::removeChild(ObjectTreeItem *c)
00073 {
00074     m_children.remove(c);
00075 }
00076 
00077 void
00078 ObjectTreeItem::addModifiedProperty(const QCString &property, const QVariant &oldValue)
00079 {
00080     if(property == "name")
00081         return;
00082 
00083     if(!m_props.contains(property)) {
00084         m_props.insert(property, oldValue);
00085         kdDebug() << "ObjectTree::adModProperty(): Added this property in the list: " << property << " oldValue: " << oldValue << endl;
00086     }
00087 }
00088 
00089 void
00090 ObjectTreeItem::addSubproperty(const QCString &property, const QVariant& value)
00091 {
00092     if (!m_subprops)
00093         m_subprops = new QMap<QString, QVariant>();
00094     if (!m_props.contains(property))
00095         m_subprops->insert( property, value );
00096 }
00097 
00098 void
00099 ObjectTreeItem::storeUnknownProperty(QDomElement &el)
00100 {
00101     if(!el.isNull()) {
00102         QTextStream ts(m_unknownProps, IO_WriteOnly|IO_Append );
00103         el.save(ts, 0);
00104     }
00105 }
00106 
00107 void
00108 ObjectTreeItem::setPixmapName(const QCString &property, const QString &name)
00109 {
00110     m_pixmapNames[property] = name;
00111 }
00112 
00113 QString
00114 ObjectTreeItem::pixmapName(const QCString &property)
00115 {
00116     if(m_pixmapNames.contains(property))
00117         return m_pixmapNames[property];
00118     return QString::null;
00119 }
00120 
00121 void
00122 ObjectTreeItem::setGridPos(int row, int col, int rowspan, int colspan)
00123 {
00124     m_row = row;  m_col = col;
00125     m_rowspan = rowspan;
00126     m_colspan = colspan;
00127     if(colspan || rowspan)
00128         m_span = true;
00129     else
00130         m_span = false;
00131 }
00132 
00136 
00137 ObjectTree::ObjectTree(const QString &classn, const QString &name, QWidget *widget, Container *container)
00138  : ObjectTreeItem(classn, name, widget, container, container)
00139 {
00140 }
00141 
00142 ObjectTree::~ObjectTree()
00143 {
00144 //  for(ObjectTreeItem *it = children()->first(); it; it = children()->next())
00145 //      removeItem(it->name());
00146     while (children()->first()) {
00147         removeItem(children()->first());
00148     }
00149 }
00150 
00151 bool
00152 ObjectTree::rename(const QString &oldname, const QString &newname)
00153 {
00154     if(oldname == m_name)
00155     {
00156         ObjectTreeItem::rename(newname);
00157         return true;
00158     }
00159 
00160     ObjectTreeItem *it = lookup(oldname);
00161     if(!it)
00162         return false;
00163 
00164     it->rename(newname);
00165     m_treeDict.remove(oldname);
00166     m_treeDict.insert(newname, it);
00167 
00168     return true;
00169 }
00170 
00171 bool
00172 ObjectTree::reparent(const QString &name, const QString &newparent)
00173 {
00174     ObjectTreeItem *item = lookup(name);
00175     if(!item)   return false;
00176     ObjectTreeItem *parent = lookup(newparent);
00177     if(!parent)   return false;
00178 
00179     item->parent()->removeChild(item);
00180     parent->addChild(item);
00181     return true;
00182 }
00183 
00184 ObjectTreeItem*
00185 ObjectTree::lookup(const QString &name)
00186 {
00187     if(name == this->name())
00188         return this;
00189     else
00190         return m_treeDict[name];
00191 }
00192 
00193 void
00194 ObjectTree::addItem(ObjectTreeItem *parent, ObjectTreeItem *c)
00195 {
00196     m_treeDict.insert(c->name(), c);
00197 
00198     if(!parent)
00199         parent = this;
00200     parent->addChild(c);
00201     m_container->form()->emitChildAdded(c);
00202 
00203     kdDebug() << "ObjectTree::addItem(): adding " << c->name() << " to " << parent->name() << endl;
00204 }
00205 
00206 void
00207 ObjectTree::removeItem(const QString &name)
00208 {
00209     ObjectTreeItem *c = lookup(name);
00210     removeItem(c);
00211 }
00212 
00213 void
00214 ObjectTree::removeItem(ObjectTreeItem *c)
00215 {
00216     if (m_container && m_container->form())
00217         m_container->form()->emitChildRemoved(c);
00218 
00219     for(ObjectTreeItem *it = c->children()->first(); it; it = c->children()->next())
00220         removeItem(it->name());
00221 
00222     m_treeDict.remove(c->name());
00223     c->parent()->removeChild(c);
00224     delete c;
00225 }
00226 
00227 QCString
00228 ObjectTree::generateUniqueName(const QCString &prefix, bool numberSuffixRequired)
00229 {
00230     /* old way of naming widgets
00231     int appendix = m_names[c] + 1;
00232     QString name(c);
00233     name.append(QString::number(appendix));
00234     m_names[c] = appendix;*/
00235     if (!numberSuffixRequired && !lookup(prefix))
00236         return prefix;
00237     QString name( prefix );
00238     int i = 2; //start from 2, i.e. we have: "widget", "widget2", etc.
00239     while(lookup(name + QString::number(i)))
00240         i++;
00241 
00242     return (name + QString::number(i)).latin1();
00243 }
00244 
KDE Home | KDE Accessibility Home | Description of Access Keys