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     m_subprops->insert( property, value );
00095 }
00096 
00097 void
00098 ObjectTreeItem::storeUnknownProperty(QDomElement &el)
00099 {
00100     if(!el.isNull()) {
00101         QTextStream ts(m_unknownProps, IO_WriteOnly|IO_Append );
00102         el.save(ts, 0);
00103     }
00104 }
00105 
00106 void
00107 ObjectTreeItem::setPixmapName(const QCString &property, const QString &name)
00108 {
00109     m_pixmapNames[property] = name;
00110 }
00111 
00112 QString
00113 ObjectTreeItem::pixmapName(const QCString &property)
00114 {
00115     if(m_pixmapNames.contains(property))
00116         return m_pixmapNames[property];
00117     return QString::null;
00118 }
00119 
00120 void
00121 ObjectTreeItem::setGridPos(int row, int col, int rowspan, int colspan)
00122 {
00123     m_row = row;  m_col = col;
00124     m_rowspan = rowspan;
00125     m_colspan = colspan;
00126     if(colspan || rowspan)
00127         m_span = true;
00128     else
00129         m_span = false;
00130 }
00131 
00135 
00136 ObjectTree::ObjectTree(const QString &classn, const QString &name, QWidget *widget, Container *container)
00137  : ObjectTreeItem(classn, name, widget, container, container)
00138 {
00139 }
00140 
00141 ObjectTree::~ObjectTree()
00142 {
00143 //  for(ObjectTreeItem *it = children()->first(); it; it = children()->next())
00144 //      removeItem(it->name());
00145     while (children()->first()) {
00146         removeItem(children()->first());
00147     }
00148 }
00149 
00150 bool
00151 ObjectTree::rename(const QString &oldname, const QString &newname)
00152 {
00153     if(oldname == m_name)
00154     {
00155         ObjectTreeItem::rename(newname);
00156         return true;
00157     }
00158 
00159     ObjectTreeItem *it = lookup(oldname);
00160     if(!it)
00161         return false;
00162 
00163     it->rename(newname);
00164     m_treeDict.remove(oldname);
00165     m_treeDict.insert(newname, it);
00166 
00167     return true;
00168 }
00169 
00170 bool
00171 ObjectTree::reparent(const QString &name, const QString &newparent)
00172 {
00173     ObjectTreeItem *item = lookup(name);
00174     if(!item)   return false;
00175     ObjectTreeItem *parent = lookup(newparent);
00176     if(!parent)   return false;
00177 
00178     item->parent()->removeChild(item);
00179     parent->addChild(item);
00180     return true;
00181 }
00182 
00183 ObjectTreeItem*
00184 ObjectTree::lookup(const QString &name)
00185 {
00186     if(name == this->name())
00187         return this;
00188     else
00189         return m_treeDict[name];
00190 }
00191 
00192 void
00193 ObjectTree::addItem(ObjectTreeItem *parent, ObjectTreeItem *c)
00194 {
00195     m_treeDict.insert(c->name(), c);
00196 
00197     if(!parent)
00198         parent = this;
00199     parent->addChild(c);
00200     m_container->form()->emitChildAdded(c);
00201 
00202     kdDebug() << "ObjectTree::addItem(): adding " << c->name() << " to " << parent->name() << endl;
00203 }
00204 
00205 void
00206 ObjectTree::removeItem(const QString &name)
00207 {
00208     ObjectTreeItem *c = lookup(name);
00209     removeItem(c);
00210 }
00211 
00212 void
00213 ObjectTree::removeItem(ObjectTreeItem *c)
00214 {
00215     if (m_container && m_container->form())
00216         m_container->form()->emitChildRemoved(c);
00217 
00218     for(ObjectTreeItem *it = c->children()->first(); it; it = c->children()->next())
00219         removeItem(it->name());
00220 
00221     m_treeDict.remove(c->name());
00222     c->parent()->removeChild(c);
00223     delete c;
00224 }
00225 
00226 QCString
00227 ObjectTree::generateUniqueName(const QCString &prefix, bool numberSuffixRequired)
00228 {
00229     /* old way of naming widgets
00230     int appendix = m_names[c] + 1;
00231     QString name(c);
00232     name.append(QString::number(appendix));
00233     m_names[c] = appendix;*/
00234     if (!numberSuffixRequired && !lookup(prefix))
00235         return prefix;
00236     QString name( prefix );
00237     int i = 2; //start from 2, i.e. we have: "widget", "widget2", etc.
00238     while(lookup(name + QString::number(i)))
00239         i++;
00240 
00241     return (name + QString::number(i)).latin1();
00242 }
00243 
KDE Home | KDE Accessibility Home | Description of Access Keys