kexi

objecttree.cpp

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