kexi

objecttreeview.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
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 
00022 #include <qpainter.h>
00023 
00024 #include <kiconloader.h>
00025 #include <klocale.h>
00026 
00027 #include "objecttree.h"
00028 #include "form.h"
00029 #include "container.h"
00030 #include "formmanager.h"
00031 #include "widgetlibrary.h"
00032 
00033 #include "objecttreeview.h"
00034 
00035 using namespace KFormDesigner;
00036 
00037 ObjectTreeViewItem::ObjectTreeViewItem(ObjectTreeViewItem *parent, ObjectTreeItem *item)
00038  : KListViewItem(parent, item->name(), item->className())
00039 {
00040     m_item = item;
00041 }
00042 
00043 ObjectTreeViewItem::ObjectTreeViewItem(KListView *list, ObjectTreeItem *item)
00044  : KListViewItem(list, item ? item->name() : QString::null, item ? item->className() : QString::null)
00045 {
00046     m_item = item;
00047 }
00048 
00049 ObjectTreeViewItem::~ObjectTreeViewItem()
00050 {
00051 }
00052 
00053 QString
00054 ObjectTreeViewItem::name() const
00055 {
00056     if(m_item)
00057         return m_item->name();
00058     else
00059         return QString::null;
00060 }
00061 
00062 void
00063 ObjectTreeViewItem::paintCell(QPainter *p, const QColorGroup & cg, int column, int width, int align)
00064 {
00065     int margin = listView()->itemMargin();
00066     if(column == 1)
00067     {
00068         if(!m_item)
00069             return;
00070         KListViewItem::paintCell(p, cg, column, width, align);
00071     }
00072     else
00073     {
00074         if(!m_item)
00075             return;
00076 
00077         p->fillRect(0,0,width, height(), QBrush(backgroundColor()));
00078 
00079         if(isSelected())
00080         {
00081             p->fillRect(0,0,width, height(), QBrush(cg.highlight()));
00082             p->setPen(cg.highlightedText());
00083         }
00084 
00085         QFont f = listView()->font();
00086         p->save();
00087         if(isSelected())
00088             f.setBold(true);
00089         p->setFont(f);
00090         if(depth() == 0) // for edit tab order dialog
00091         {
00092             QString iconName 
00093                 = ((ObjectTreeView*)listView())->iconNameForClass(m_item->widget()->className());
00094             p->drawPixmap(margin, (height() - IconSize(KIcon::Small))/2 , SmallIcon(iconName));
00095             p->drawText(
00096                 QRect(2*margin + IconSize(KIcon::Small),0,width, height()-1), 
00097                 Qt::AlignVCenter, m_item->name());
00098         }
00099         else
00100             p->drawText(QRect(margin,0,width, height()-1), Qt::AlignVCenter, m_item->name());
00101         p->restore();
00102 
00103         p->setPen( QColor(200,200,200) ); //like in t.v.
00104         p->drawLine(width-1, 0, width-1, height()-1);
00105     }
00106 
00107     p->setPen( QColor(200,200,200) ); //like in t.v.
00108     p->drawLine(-150, height()-1, width, height()-1 );
00109 }
00110 
00111 void
00112 ObjectTreeViewItem::paintBranches(QPainter *p, const QColorGroup &cg, int w, int y, int h)
00113 {
00114     p->eraseRect(0,0,w,h);
00115     ObjectTreeViewItem *item = (ObjectTreeViewItem*)firstChild();
00116     if(!item || !item->m_item || !item->m_item->widget())
00117         return;
00118 
00119     p->save();
00120     p->translate(0,y);
00121     while(item)
00122     {
00123         p->fillRect(0,0,w, item->height(), QBrush(item->backgroundColor()));
00124         p->fillRect(-150,0,150, item->height(), QBrush(item->backgroundColor()));
00125         p->save();
00126         p->setPen( QColor(200,200,200) ); //like in t.v.
00127         p->drawLine(-150, item->height()-1, w, item->height()-1 );
00128         p->restore();
00129 
00130         if(item->isSelected())
00131         {
00132             p->fillRect(0,0,w, item->height(), QBrush(cg.highlight()));
00133             p->fillRect(-150,0,150, item->height(), QBrush(cg.highlight()));
00134         }
00135 
00136         QString iconName 
00137             = ((ObjectTreeView*)listView())->iconNameForClass(item->m_item->widget()->className());
00138         p->drawPixmap(
00139             (w - IconSize(KIcon::Small))/2, (item->height() - IconSize(KIcon::Small))/2 , 
00140             SmallIcon(iconName));
00141 
00142         p->translate(0, item->totalHeight());
00143         item = (ObjectTreeViewItem*)item->nextSibling();
00144     }
00145     p->restore();
00146 }
00147 
00148 void
00149 ObjectTreeViewItem::setup()
00150 {
00151     KListViewItem::setup();
00152     if(!m_item)
00153         setHeight(0);
00154 }
00155 
00156 void
00157 ObjectTreeViewItem::setOpen( bool o )
00158 {
00159     //don't allow to collapse the node, user may be tricked because we're not displaying [+] marks
00160     if (o)
00161         KListViewItem::setOpen(o);
00162 }
00163 
00164 // ObjectTreeView itself ----------------
00165 
00166 ObjectTreeView::ObjectTreeView(QWidget *parent, const char *name, bool tabStop)
00167  : KListView(parent, name)
00168  , m_form(0)
00169 {
00170     addColumn(i18n("Name"), 130);
00171     addColumn(i18n("Widget's type", "Type"), 100);
00172 
00173     installEventFilter(this);
00174 
00175     connect((QObject*)header(), SIGNAL(sectionHandleDoubleClicked(int)), this, SLOT(slotColumnSizeChanged(int)));
00176     if(!tabStop)
00177     {
00178         setSelectionModeExt(Extended);
00179         connect(this, SIGNAL(selectionChanged()), this, SLOT(slotSelectionChanged()));
00180         connect(this, SIGNAL(contextMenu(KListView *, QListViewItem *, const QPoint&)), this, SLOT(displayContextMenu(KListView*, QListViewItem*, const QPoint&)));
00181     }
00182 
00183     setFullWidth(true);
00184     setAllColumnsShowFocus(true);
00185     setItemMargin(3);
00186     setSorting(-1);
00187 }
00188 
00189 ObjectTreeView::~ObjectTreeView()
00190 {
00191 }
00192 
00193 QSize
00194 ObjectTreeView::sizeHint() const
00195 {
00196     return QSize( QFontMetrics(font()).width(columnText(0)+columnText(1)+"   "),
00197         KListView::sizeHint().height());
00198 }
00199 
00200 QString
00201 ObjectTreeView::iconNameForClass(const QCString &classname)
00202 {
00203     return m_form->library()->iconName(classname);
00204 }
00205 
00206 void
00207 ObjectTreeView::slotColumnSizeChanged(int)
00208 {
00209     setColumnWidth(1, viewport()->width() - columnWidth(0));
00210 }
00211 
00212 void
00213 ObjectTreeView::displayContextMenu(KListView *list, QListViewItem *item, const QPoint &)
00214 {
00215     if(list != this || !m_form || !item)
00216         return;
00217 
00218     QWidget *w = ((ObjectTreeViewItem*)item)->m_item->widget();
00219     if(!w)
00220         return;
00221 
00222     FormManager::self()->createContextMenu(w, m_form->activeContainer());
00223 }
00224 
00225 ObjectTreeViewItem*
00226 ObjectTreeView::findItem(const QString &name)
00227 {
00228     QListViewItemIterator it(this);
00229         while(it.current())
00230     {
00231         ObjectTreeViewItem *item = static_cast<ObjectTreeViewItem*>(it.current());
00232         if(item->name() == name)
00233         {
00234             return item;
00235         }
00236         it++;
00237     }
00238     return 0;
00239 }
00240 
00241 void
00242 ObjectTreeView::setSelectedWidget(QWidget *w, bool add)
00243 {
00244     blockSignals(true); // to avoid recursion
00245 
00246     if(!w)
00247     {
00248         clearSelection();
00249         blockSignals(false);
00250         return;
00251     }
00252 
00253     if(selectedItems().count() == 0)
00254         add = false;
00255 
00256     if(!add)
00257         clearSelection();
00258 
00259 
00260     QListViewItem *item = (QListViewItem*) findItem(w->name());
00261     if(!add)
00262     {
00263         setCurrentItem(item);
00264         setSelectionAnchor(item);
00265         setSelected(item, true);
00266     }
00267     else
00268         setSelected(item, true);
00269 
00270     blockSignals(false);
00271 }
00272 
00273 void
00274 ObjectTreeView::slotSelectionChanged()
00275 {
00276     const bool hadFocus = hasFocus();
00277     QPtrList<QListViewItem> list = selectedItems();
00278     m_form->selectFormWidget();
00279     for(QListViewItem *item = list.first(); item; item = list.next())
00280     {
00281         ObjectTreeViewItem *it = static_cast<ObjectTreeViewItem*>(item);
00282         QWidget *w = it->objectTree()->widget();
00283         if(w && (m_form->selectedWidgets()->findRef(w) == -1))
00284             m_form->setSelectedWidget(w, true, true);
00285     }
00286     if (hadFocus)
00287         setFocus(); //restore focus
00288 }
00289 
00290 void
00291 ObjectTreeView::addItem(ObjectTreeItem *item)
00292 {
00293     ObjectTreeViewItem *parent=0;
00294 
00295     parent = findItem(item->parent()->name());
00296     if(!parent)
00297         return;
00298 
00299     loadTree(item, parent);
00300 }
00301 
00302 void
00303 ObjectTreeView::removeItem(ObjectTreeItem *item)
00304 {
00305     if(!item)
00306         return;
00307     ObjectTreeViewItem *it = findItem(item->name());
00308     delete it;
00309 }
00310 
00311 void
00312 ObjectTreeView::renameItem(const QCString &oldname, const QCString &newname)
00313 {
00314     if(findItem(newname))
00315         return;
00316     ObjectTreeViewItem *item = findItem(oldname);
00317     if(!item)
00318         return;
00319     item->setText(0, newname);
00320 }
00321 
00322 void
00323 ObjectTreeView::setForm(Form *form)
00324 {
00325     if (m_form)
00326         disconnect(m_form, SIGNAL(destroying()), this, SLOT(slotBeforeFormDestroyed()));
00327     m_form = form;
00328     m_topItem = 0;
00329     clear();
00330 
00331     if(!m_form)
00332         return;
00333 
00334     connect(m_form, SIGNAL(destroying()), this, SLOT(slotBeforeFormDestroyed()));
00335 
00336     // Creates the hidden top Item
00337     m_topItem = new ObjectTreeViewItem(this);
00338     m_topItem->setSelectable(false);
00339     m_topItem->setOpen(true);
00340 
00341     ObjectTree *tree = m_form->objectTree();
00342     loadTree(tree, m_topItem);
00343 
00344     if(!form->selectedWidgets()->isEmpty())
00345         setSelectedWidget(form->selectedWidgets()->first());
00346     else
00347         setSelectedWidget(form->widget());
00348 }
00349 
00350 void
00351 ObjectTreeView::slotBeforeFormDestroyed()
00352 {
00353     setForm(0);
00354 }
00355 
00356 ObjectTreeViewItem*
00357 ObjectTreeView::loadTree(ObjectTreeItem *item, ObjectTreeViewItem *parent)
00358 {
00359     if(!item)
00360         return 0;
00361     ObjectTreeViewItem *treeItem = new ObjectTreeViewItem(parent, item);
00362     treeItem->setOpen(true);
00363 
00364     // The item is inserted by default at the beginning, but we want it to be at the end, so we move it
00365     QListViewItem *last = parent->firstChild();
00366     while(last->nextSibling())
00367         last = last->nextSibling();
00368     treeItem->moveItem(last);
00369 
00370     ObjectTreeList *list = item->children();
00371     for(ObjectTreeItem *it = list->first(); it; it = list->next())
00372         loadTree(it, treeItem);
00373 
00374     return treeItem;
00375 }
00376 
00377 #include "objecttreeview.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys