kexi

objecttreeview.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include <kdebug.h>
21
22#include <tqpainter.h>
23
24#include <kiconloader.h>
25#include <tdelocale.h>
26
27#include "objecttree.h"
28#include "form.h"
29#include "container.h"
30#include "formmanager.h"
31#include "widgetlibrary.h"
32
33#include "objecttreeview.h"
34
35using namespace KFormDesigner;
36
37ObjectTreeViewItem::ObjectTreeViewItem(ObjectTreeViewItem *parent, ObjectTreeItem *item)
38 : TDEListViewItem(parent, item->name(), item->className())
39{
40 m_item = item;
41}
42
43ObjectTreeViewItem::ObjectTreeViewItem(TDEListView *list, ObjectTreeItem *item)
44 : TDEListViewItem(list, item ? item->name() : TQString(), item ? item->className() : TQString())
45{
46 m_item = item;
47}
48
49ObjectTreeViewItem::~ObjectTreeViewItem()
50{
51}
52
53TQString
55{
56 if(m_item)
57 return m_item->name();
58 else
59 return TQString();
60}
61
62void
63ObjectTreeViewItem::paintCell(TQPainter *p, const TQColorGroup & cg, int column, int width, int align)
64{
65 int margin = listView()->itemMargin();
66 if(column == 1)
67 {
68 if(!m_item)
69 return;
70 TDEListViewItem::paintCell(p, cg, column, width, align);
71 }
72 else
73 {
74 if(!m_item)
75 return;
76
77 p->fillRect(0,0,width, height(), TQBrush(backgroundColor()));
78
79 if(isSelected())
80 {
81 p->fillRect(0,0,width, height(), TQBrush(cg.highlight()));
82 p->setPen(cg.highlightedText());
83 }
84
85 TQFont f = listView()->font();
86 p->save();
87 if(isSelected())
88 f.setBold(true);
89 p->setFont(f);
90 if(depth() == 0) // for edit tab order dialog
91 {
92 TQString iconName
93 = ((ObjectTreeView*)listView())->iconNameForClass(m_item->widget()->className());
94 p->drawPixmap(margin, (height() - IconSize(TDEIcon::Small))/2 , SmallIcon(iconName));
95 p->drawText(
96 TQRect(2*margin + IconSize(TDEIcon::Small),0,width, height()-1),
97 TQt::AlignVCenter, m_item->name());
98 }
99 else
100 p->drawText(TQRect(margin,0,width, height()-1), TQt::AlignVCenter, m_item->name());
101 p->restore();
102
103 p->setPen( TQColor(200,200,200) ); //like in t.v.
104 p->drawLine(width-1, 0, width-1, height()-1);
105 }
106
107 p->setPen( TQColor(200,200,200) ); //like in t.v.
108 p->drawLine(-150, height()-1, width, height()-1 );
109}
110
111void
112ObjectTreeViewItem::paintBranches(TQPainter *p, const TQColorGroup &cg, int w, int y, int h)
113{
114 p->eraseRect(0,0,w,h);
115 ObjectTreeViewItem *item = (ObjectTreeViewItem*)firstChild();
116 if(!item || !item->m_item || !item->m_item->widget())
117 return;
118
119 p->save();
120 p->translate(0,y);
121 while(item)
122 {
123 p->fillRect(0,0,w, item->height(), TQBrush(item->backgroundColor()));
124 p->fillRect(-150,0,150, item->height(), TQBrush(item->backgroundColor()));
125 p->save();
126 p->setPen( TQColor(200,200,200) ); //like in t.v.
127 p->drawLine(-150, item->height()-1, w, item->height()-1 );
128 p->restore();
129
130 if(item->isSelected())
131 {
132 p->fillRect(0,0,w, item->height(), TQBrush(cg.highlight()));
133 p->fillRect(-150,0,150, item->height(), TQBrush(cg.highlight()));
134 }
135
136 TQString iconName
137 = ((ObjectTreeView*)listView())->iconNameForClass(item->m_item->widget()->className());
138 p->drawPixmap(
139 (w - IconSize(TDEIcon::Small))/2, (item->height() - IconSize(TDEIcon::Small))/2 ,
140 SmallIcon(iconName));
141
142 p->translate(0, item->totalHeight());
143 item = (ObjectTreeViewItem*)item->nextSibling();
144 }
145 p->restore();
146}
147
148void
150{
151 TDEListViewItem::setup();
152 if(!m_item)
153 setHeight(0);
154}
155
156void
157ObjectTreeViewItem::setOpen( bool o )
158{
159 //don't allow to collapse the node, user may be tricked because we're not displaying [+] marks
160 if (o)
161 TDEListViewItem::setOpen(o);
162}
163
164// ObjectTreeView itself ----------------
165
166ObjectTreeView::ObjectTreeView(TQWidget *parent, const char *name, bool tabStop)
167 : TDEListView(parent, name)
168 , m_form(0)
169{
170 addColumn(i18n("Name"), 130);
171 addColumn(i18n("Widget's type", "Type"), 100);
172
173 installEventFilter(this);
174
175 connect((TQObject*)header(), TQ_SIGNAL(sectionHandleDoubleClicked(int)), this, TQ_SLOT(slotColumnSizeChanged(int)));
176 if(!tabStop)
177 {
178 setSelectionModeExt(Extended);
179 connect(this, TQ_SIGNAL(selectionChanged()), this, TQ_SLOT(slotSelectionChanged()));
180 connect(this, TQ_SIGNAL(contextMenu(TDEListView *, TQListViewItem *, const TQPoint&)), this, TQ_SLOT(displayContextMenu(TDEListView*, TQListViewItem*, const TQPoint&)));
181 }
182
183 setFullWidth(true);
184 setAllColumnsShowFocus(true);
185 setItemMargin(3);
186 setSorting(-1);
187}
188
189ObjectTreeView::~ObjectTreeView()
190{
191}
192
193TQSize
194ObjectTreeView::sizeHint() const
195{
196 return TQSize( TQFontMetrics(font()).width(columnText(0)+columnText(1)+" "),
197 TDEListView::sizeHint().height());
198}
199
200TQString
201ObjectTreeView::iconNameForClass(const TQCString &classname)
202{
203 return m_form->library()->iconName(classname);
204}
205
206void
207ObjectTreeView::slotColumnSizeChanged(int)
208{
209 setColumnWidth(1, viewport()->width() - columnWidth(0));
210}
211
212void
213ObjectTreeView::displayContextMenu(TDEListView *list, TQListViewItem *item, const TQPoint &)
214{
215 if(list != this || !m_form || !item)
216 return;
217
218 TQWidget *w = ((ObjectTreeViewItem*)item)->m_item->widget();
219 if(!w)
220 return;
221
222 FormManager::self()->createContextMenu(w, m_form->activeContainer());
223}
224
226ObjectTreeView::findItem(const TQString &name)
227{
228 TQListViewItemIterator it(this);
229 while(it.current())
230 {
231 ObjectTreeViewItem *item = static_cast<ObjectTreeViewItem*>(it.current());
232 if(item->name() == name)
233 {
234 return item;
235 }
236 it++;
237 }
238 return 0;
239}
240
241void
243{
244 blockSignals(true); // to avoid recursion
245
246 if(!w)
247 {
248 clearSelection();
249 blockSignals(false);
250 return;
251 }
252
253 if(selectedItems().count() == 0)
254 add = false;
255
256 if(!add)
257 clearSelection();
258
259
260 TQListViewItem *item = (TQListViewItem*) findItem(w->name());
261 if(!add)
262 {
263 setCurrentItem(item);
264 setSelectionAnchor(item);
265 setSelected(item, true);
266 }
267 else
268 setSelected(item, true);
269
270 blockSignals(false);
271}
272
273void
275{
276 const bool hadFocus = hasFocus();
277 TQPtrList<TQListViewItem> list = selectedItems();
278 m_form->selectFormWidget();
279 for(TQListViewItem *item = list.first(); item; item = list.next())
280 {
281 ObjectTreeViewItem *it = static_cast<ObjectTreeViewItem*>(item);
282 TQWidget *w = it->objectTree()->widget();
283 if(w && (m_form->selectedWidgets()->findRef(w) == -1))
284 m_form->setSelectedWidget(w, true, true);
285 }
286 if (hadFocus)
287 setFocus(); //restore focus
288}
289
290void
292{
293 ObjectTreeViewItem *parent=0;
294
295 parent = findItem(item->parent()->name());
296 if(!parent)
297 return;
298
299 loadTree(item, parent);
300}
301
302void
304{
305 if(!item)
306 return;
307 ObjectTreeViewItem *it = findItem(item->name());
308 delete it;
309}
310
311void
312ObjectTreeView::renameItem(const TQCString &oldname, const TQCString &newname)
313{
314 if(findItem(newname))
315 return;
316 ObjectTreeViewItem *item = findItem(oldname);
317 if(!item)
318 return;
319 item->setText(0, newname);
320}
321
322void
324{
325 if (m_form)
326 disconnect(m_form, TQ_SIGNAL(destroying()), this, TQ_SLOT(slotBeforeFormDestroyed()));
327 m_form = form;
328 m_topItem = 0;
329 clear();
330
331 if(!m_form)
332 return;
333
334 connect(m_form, TQ_SIGNAL(destroying()), this, TQ_SLOT(slotBeforeFormDestroyed()));
335
336 // Creates the hidden top Item
337 m_topItem = new ObjectTreeViewItem(this);
338 m_topItem->setSelectable(false);
339 m_topItem->setOpen(true);
340
341 ObjectTree *tree = m_form->objectTree();
342 loadTree(tree, m_topItem);
343
344 if(!form->selectedWidgets()->isEmpty())
345 setSelectedWidget(form->selectedWidgets()->first());
346 else
347 setSelectedWidget(form->widget());
348}
349
350void
355
358{
359 if(!item)
360 return 0;
361 ObjectTreeViewItem *treeItem = new ObjectTreeViewItem(parent, item);
362 treeItem->setOpen(true);
363
364 // The item is inserted by default at the beginning, but we want it to be at the end, so we move it
365 TQListViewItem *last = parent->firstChild();
366 while(last->nextSibling())
367 last = last->nextSibling();
368 treeItem->moveItem(last);
369
370 ObjectTreeList *list = item->children();
371 for(ObjectTreeItem *it = list->first(); it; it = list->next())
372 loadTree(it, treeItem);
373
374 return treeItem;
375}
376
377#include "objecttreeview.moc"
static FormManager * self()
Access to FormManager singleton.
void createContextMenu(TQWidget *w, Container *container, bool popupAtCursor=true)
A simple class representing a form.
Definition form.h:136
WidgetList * selectedWidgets() const
Definition form.h:192
TQWidget * widget() const
Definition form.cpp:114
An item representing a widget Holds the properties of a widget (classname, name, parent,...
Definition objecttree.h:59
An item in ObjectTreeView associated with an ObjectTreeItem.
virtual void paintBranches(TQPainter *p, const TQColorGroup &cg, int w, int y, int h)
Reimplemented to draw custom contents (copied from Property Editor).
virtual void paintCell(TQPainter *p, const TQColorGroup &cg, int column, int width, int align)
Reimplemented to draw custom contents (copied from Property Editor).
virtual void setup()
Reimplemented to draw custom contents (copied from Property Editor).
ObjectTreeItem * objectTree() const
void displayContextMenu(TDEListView *list, TQListViewItem *item, const TQPoint &p)
TQString iconNameForClass(const TQCString &classname)
void setSelectedWidget(TQWidget *w, bool add=false)
void addItem(ObjectTreeItem *item)
void renameItem(const TQCString &oldname, const TQCString &newname)
ObjectTreeViewItem * findItem(const TQString &name)
void removeItem(ObjectTreeItem *item)
ObjectTreeViewItem * loadTree(ObjectTreeItem *item, ObjectTreeViewItem *parent)
Internal function to fill the list.
Represents all the objects available within a form. This class holds ObjectTreeItem for each widget i...
Definition objecttree.h:140
TQPtrList< ObjectTreeItem > ObjectTreeList
An list of ObjectTreeItem pointers.
Definition objecttree.h:42