~ubuntu-branches/ubuntu/precise/koffice/precise

« back to all changes in this revision

Viewing changes to kexi/widget/propertyeditor/kexipropertyeditoritem.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2006-04-20 21:38:53 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20060420213853-j5lxluqvymxt2zny
Tags: 1:1.5.0-0ubuntu2
UbuntuĀ uploadĀ 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* This file is part of the KDE project
2
 
   Copyright (C) 2002   Lucijan Busch <lucijan@gmx.at>
3
 
   Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
4
 
   Copyright (C) 2004 Jaroslaw Staniek <js@iidea.pl>
5
 
 
6
 
   This program is free software; you can redistribute it and/or
7
 
   modify it under the terms of the GNU Library General Public
8
 
   License as published by the Free Software Foundation; either
9
 
   version 2 of the License, or (at your option) any later version.
10
 
 
11
 
   This program is distributed in the hope that it will be useful,
12
 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 
   Library General Public License for more details.
15
 
 
16
 
   You should have received a copy of the GNU Library General Public License
17
 
   along with this program; see the file COPYING.  If not, write to
18
 
   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19
 
   Boston, MA 02111-1307, USA.
20
 
*/
21
 
 
22
 
#include <qsize.h>
23
 
#include <qpainter.h>
24
 
#include <qpixmap.h>
25
 
#include <qcolor.h>
26
 
#include <qfont.h>
27
 
#include <qcursor.h>
28
 
 
29
 
#include <qpoint.h>
30
 
#include <kdebug.h>
31
 
#include <kiconloader.h>
32
 
 
33
 
#include <klocale.h>
34
 
 
35
 
#include "kexipropertyeditoritem.h"
36
 
#include "kexipropertyeditor.h"
37
 
 
38
 
#if 0
39
 
// Helper class for QSizePolicy Editor
40
 
 
41
 
class spHelper
42
 
{
43
 
        public:
44
 
        spHelper() {;}
45
 
        ~spHelper() {;}
46
 
 
47
 
        static QStringList      list();
48
 
        static QString          valueToKey(int key);
49
 
        static QSizePolicy::SizeType            keyToValue(const QString &key);
50
 
};
51
 
 
52
 
QString
53
 
spHelper::valueToKey(int key)
54
 
{
55
 
        switch(key)
56
 
        {
57
 
                case QSizePolicy::Fixed: return QString("Fixed");
58
 
                case QSizePolicy::Minimum: return QString("Minimum");
59
 
                case QSizePolicy::Maximum: return QString("Maximum");
60
 
                case QSizePolicy::Preferred: return QString("Preferred");
61
 
                case QSizePolicy::MinimumExpanding: return QString("MinimumExpanding");
62
 
                case QSizePolicy::Expanding: return QString("Expanding");
63
 
                case QSizePolicy::Ignored: return QString("Ignored");
64
 
                default: return QString();
65
 
        }
66
 
}
67
 
 
68
 
QStringList
69
 
spHelper::list()
70
 
{
71
 
        QStringList list;
72
 
        list << "Fixed" << "Minimum" << "Maximum" << "Preferred" << "Expanding"
73
 
                << "MinimumExpanding" << "Ignored";
74
 
        return list;
75
 
}
76
 
 
77
 
QSizePolicy::SizeType
78
 
spHelper::keyToValue(const QString &key)
79
 
{
80
 
        if(key == "Fixed") return QSizePolicy::Fixed;
81
 
        if(key == "Minimum") return QSizePolicy::Minimum;
82
 
        if(key == "Maximum") return QSizePolicy::Maximum;
83
 
        if(key == "Preferred") return QSizePolicy::Preferred;
84
 
        if(key == "MinimumExpanding") return QSizePolicy::MinimumExpanding;
85
 
        if(key == "Expanding") return QSizePolicy::Expanding;
86
 
        if(key == "Ignored") return QSizePolicy::Ignored;
87
 
 
88
 
        return QSizePolicy::Expanding;
89
 
}
90
 
#endif
91
 
 
92
 
// ======== KexiPropertyEditorItem ============================
93
 
 
94
 
KexiPropertyEditorItem::KexiPropertyEditorItem(KexiPropertyEditorItem *par, KexiProperty *property,
95
 
        KexiPropertyEditorItem *after)
96
 
 : KListViewItem(par, after, property->desc().isEmpty() ? property->name() : property->desc()
97
 
        , property->valueText())
98
 
{
99
 
        m_order = parent()->childCount();
100
 
//      m_value = property->value();
101
 
//      m_oldvalue = m_value;
102
 
        m_property=property;
103
 
//      m_childprop = 0;
104
 
 
105
 
        //add children
106
 
        KexiProperty::List *children = m_property->children();
107
 
        if (children) {
108
 
                m_children = new Dict();
109
 
                KexiProperty::ListIterator it(*children);
110
 
                KexiPropertyEditorItem *item = 0;
111
 
                for (;it.current();++it) {
112
 
                        item = new KexiPropertyEditorItem(this, it.current(), item);
113
 
                        m_children->insert( it.current()->name(), item );
114
 
                }
115
 
        }
116
 
        else {
117
 
                m_children = 0;
118
 
        }
119
 
 
120
 
//      m_childprop->setAutoDelete(true);
121
 
        updateValue();
122
 
 
123
 
        //3 rows per item is enough?
124
 
        setMultiLinesEnabled( true );
125
 
        setHeight(static_cast<KexiPropertyEditor*>(listView())->baseRowHeight()*3);
126
 
}
127
 
 
128
 
KexiPropertyEditorItem::KexiPropertyEditorItem(KListView *par, const QString &text)
129
 
 : KListViewItem(par, text, "")
130
 
{
131
 
        m_order = listView()->childCount();
132
 
        //      m_value = "";
133
 
        m_property= new KexiProperty();
134
 
//      m_oldvalue=m_value;
135
 
//      m_childprop = 0;
136
 
        m_children = 0;
137
 
        setSelectable(false);
138
 
        setOpen(true);
139
 
 
140
 
        //3 rows per item is enough?
141
 
        setMultiLinesEnabled( true );
142
 
        setHeight(static_cast<KexiPropertyEditor*>(par)->baseRowHeight()*3);
143
 
}
144
 
 
145
 
/*KexiPropertyEditorItem* KexiPropertyEditorItem::createChildItem(const QString& name)
146
 
{
147
 
        KexiProperty *childProp = m_property->child(name);
148
 
        if (!childProp)
149
 
                return 0;
150
 
        return new KexiPropertyEditorItem(this, childProp);
151
 
}*/
152
 
 
153
 
/*void
154
 
KexiPropertyEditorItem::setValue(QVariant value)
155
 
{
156
 
        setText(1, format(value));
157
 
        m_value = value;
158
 
}
159
 
*/
160
 
 
161
 
void
162
 
KexiPropertyEditorItem::paintCell(QPainter *p, const QColorGroup & cg, int column, int width, int align)
163
 
{
164
 
        int margin = listView()->itemMargin();
165
 
 
166
 
        if(column == 1)
167
 
        {
168
 
                switch(m_property->type())
169
 
                {
170
 
                        case QVariant::Pixmap:
171
 
                        {
172
 
                                p->fillRect(0,0,width,height(),QBrush(backgroundColor()));
173
 
                                p->drawPixmap(margin, margin, m_property->value().toPixmap());
174
 
                                break;
175
 
                        }
176
 
                        case QVariant::Color:
177
 
                        {
178
 
                                p->fillRect(0,0,width,height(), QBrush(backgroundColor()));
179
 
                                QColor ncolor = m_property->value().toColor();
180
 
                                p->setBrush(ncolor);
181
 
                                p->drawRect(margin, margin, width - 2*margin, height() - 2*margin);
182
 
                                QColorGroup nGroup(cg);
183
 
                                break;
184
 
                        }
185
 
                        case QVariant::Bool:
186
 
                        {
187
 
                                p->fillRect(0,0,width,height(), QBrush(backgroundColor()));
188
 
                                if(m_property->value().toBool())
189
 
                                {
190
 
                                        p->drawPixmap(margin, height()/2 -8, SmallIcon("button_ok"));
191
 
                                        p->drawText(QRect(margin+20,0,width,height()-1), Qt::AlignVCenter, i18n("Yes"));
192
 
                                }
193
 
                                else
194
 
                                {
195
 
                                        p->drawPixmap(margin, height()/2 -8, SmallIcon("button_no"));
196
 
                                        p->drawText(QRect(margin+20,0,width,height()-1), Qt::AlignVCenter, i18n("No"));
197
 
                                }
198
 
                                break;
199
 
                        }
200
 
 
201
 
                        default:
202
 
                        {
203
 
                                if(depth()==0)
204
 
                                        return;
205
 
                                KListViewItem::paintCell(p, cg, column, width, align);
206
 
                                break;
207
 
                        }
208
 
                }
209
 
        }
210
 
        else
211
 
        {
212
 
                if(depth()==0)
213
 
                        return;
214
 
 
215
 
                if(isSelected())
216
 
                {
217
 
                        p->fillRect(0,0,width, height(), QBrush(cg.highlight()));
218
 
                        p->setPen(cg.highlightedText());
219
 
                }
220
 
                else
221
 
                        p->fillRect(0,0,width, height(), QBrush(backgroundColor()));
222
 
 
223
 
                QFont f = listView()->font();
224
 
                p->save();
225
 
//              if(modified())
226
 
                if (m_property->changed())
227
 
                        f.setBold(true);
228
 
                p->setFont(f);
229
 
                p->drawText(QRect(margin,0,width, height()-1), Qt::AlignVCenter, text(0));
230
 
                p->restore();
231
 
 
232
 
                p->setPen( QColor(200,200,200) ); //like in table view
233
 
                p->drawLine(width-1, 0, width-1, height()-1);
234
 
        }
235
 
 
236
 
        p->setPen( QColor(200,200,200) ); //like in t.v.
237
 
        p->drawLine(-50, height()-1, width, height()-1 );
238
 
}
239
 
 
240
 
void
241
 
KexiPropertyEditorItem::paintBranches(QPainter *p, const QColorGroup &cg, int w, int y, int h)
242
 
{
243
 
        p->eraseRect(0,0,w,h);
244
 
        KListViewItem *item = (KListViewItem*)firstChild();
245
 
//      const bool invertAlternate = parent() && parent()->parent() && ((childCount() % 2) == 0);
246
 
        if(!item)
247
 
                return;
248
 
 
249
 
        p->save();
250
 
        p->translate(0,y);
251
 
        while(item)
252
 
        {
253
 
                /*if (invertAlternate) {
254
 
                        if (item->isAlternate())
255
 
                                p->fillRect(0,0,w, item->height(), 
256
 
                                QBrush(listView()->viewport()->colorGroup().base()));
257
 
                        else
258
 
                                p->fillRect(0,0,w, item->height(), 
259
 
                                        QBrush(static_cast<KListView*>(listView())->alternateBackground()));
260
 
                }
261
 
                else*/
262
 
                        p->fillRect(0,0,w, item->height(), QBrush(item->backgroundColor()));
263
 
                p->fillRect(-50,0,50, item->height(), QBrush(item->backgroundColor()));
264
 
                p->save();
265
 
                p->setPen( QColor(200,200,200) ); //like in t.v.
266
 
                p->drawLine(-50, item->height()-1, w, item->height()-1 );
267
 
                p->restore();
268
 
 
269
 
                if(item->isSelected())
270
 
                {
271
 
                        p->fillRect(0,0,w, item->height(), QBrush(cg.highlight()));
272
 
                        p->fillRect(-50,0,50, item->height(), QBrush(cg.highlight()));
273
 
                }
274
 
 
275
 
                if(item->firstChild())
276
 
                {
277
 
//                      p->drawRect(2, item->height()/2 -4, 9, 9);
278
 
                        const int marg = (item->height()-KexiPropertyEditorItem_BranchBoxSize)/2;
279
 
                        p->save();
280
 
                        p->setPen( QColor(200,200,200) );
281
 
                        p->drawRect(2, marg, 
282
 
                                KexiPropertyEditorItem_BranchBoxSize, KexiPropertyEditorItem_BranchBoxSize);
283
 
                        p->fillRect(2+1, marg+1, 
284
 
                                KexiPropertyEditorItem_BranchBoxSize-2, KexiPropertyEditorItem_BranchBoxSize-2, white);
285
 
                        p->restore();
286
 
//                      p->drawLine(4, item->height()/2, 8, item->height()/2);
287
 
                        p->drawLine(2+2, marg+KexiPropertyEditorItem_BranchBoxSize/2,
288
 
                                KexiPropertyEditorItem_BranchBoxSize-1, marg+KexiPropertyEditorItem_BranchBoxSize/2);
289
 
                        if(!item->isOpen())
290
 
                                p->drawLine(2+KexiPropertyEditorItem_BranchBoxSize/2, marg+2, 
291
 
                                        2+KexiPropertyEditorItem_BranchBoxSize/2, marg+KexiPropertyEditorItem_BranchBoxSize-3);
292
 
//                              p->drawLine(6, item->height()/2 - 2, 6, item->height()/2 +2);
293
 
                }
294
 
 
295
 
                if (!static_cast<KexiPropertyEditorItem*>(item)->m_property->icon().isEmpty()) {
296
 
                        //draw icon
297
 
                        int margin = listView()->itemMargin();
298
 
                        QPixmap pix = SmallIcon(static_cast<KexiPropertyEditorItem*>(item)->m_property->icon());
299
 
                        p->drawPixmap(1,(item->height()-pix.height())/2,pix);
300
 
                }
301
 
 
302
 
                p->translate(0, item->totalHeight());
303
 
                item = (KListViewItem*)item->nextSibling();
304
 
        }
305
 
        p->restore();
306
 
}
307
 
 
308
 
void
309
 
KexiPropertyEditorItem::setup()
310
 
{
311
 
        KListViewItem::setup();
312
 
        if(depth()==0)
313
 
                setHeight(0);
314
 
}
315
 
 
316
 
#if 0
317
 
QVariant
318
 
KexiPropertyEditorItem::getComposedValue()
319
 
{
320
 
        switch(m_property->type())
321
 
        {
322
 
                case QVariant::Size:
323
 
                {
324
 
                        QSize s;
325
 
                        QVariant v;
326
 
                        v = (*m_children)["width"]->property()->value();
327
 
                        s.setWidth(v.toInt());
328
 
                        (*m_children)["width"]->property()->setValue(v.toInt());
329
 
 
330
 
                        v = (*m_children)["height"]->property()->value();
331
 
                        s.setHeight(v.toInt());
332
 
                        (*m_children)["height"]->property()->setValue(v.toInt());
333
 
 
334
 
                        setValue(s);
335
 
                        return s;
336
 
                }
337
 
                case QVariant::Point:
338
 
                {
339
 
                        QPoint p;
340
 
                        QVariant v;
341
 
                        v = (*m_children)["x"]->value();
342
 
                        p.setX(v.toInt());
343
 
                        (*m_children)["x"]->property()->setValue(v.toInt());
344
 
 
345
 
                        v = (*m_children)["y"]->value();
346
 
                        p.setY(v.toInt());
347
 
                        (*m_children)["y"]->property()->setValue(v.toInt());
348
 
 
349
 
                        setValue(p);
350
 
                        return p;
351
 
                }
352
 
                case QVariant::Rect:
353
 
                {
354
 
                        QRect r;
355
 
                        QVariant v;
356
 
                        v = (*m_children)["x"]->value();
357
 
                        r.setX(v.toInt());
358
 
                        (*m_children)["x"]->property()->setValue(v.toInt());
359
 
 
360
 
                        v = (*m_children)["y"]->value();
361
 
                        r.setY(v.toInt());
362
 
                        (*m_children)["y"]->property()->setValue(v.toInt());
363
 
 
364
 
                        v = (*m_children)["width"]->value();
365
 
                        r.setWidth(v.toInt());
366
 
                        (*m_children)["width"]->property()->setValue(v.toInt());
367
 
 
368
 
                        v = (*m_children)["height"]->value();
369
 
                        r.setHeight(v.toInt());
370
 
                        (*m_children)["height"]->property()->setValue(v.toInt());
371
 
 
372
 
                        setValue(r);
373
 
                        return r;
374
 
                }
375
 
                case QVariant::SizePolicy:
376
 
                {
377
 
                        QSizePolicy p;
378
 
                        QVariant v;
379
 
                        v = (*m_children)["hSize"]->value();
380
 
                        p.setHorData(spHelper::keyToValue(v.toString()));
381
 
                        (*m_children)["hSize"]->property()->setValue(v.toString());
382
 
 
383
 
                        v = (*m_children)["vSize"]->value();
384
 
                        p.setVerData(spHelper::keyToValue(v.toString()));
385
 
                        (*m_children)["vSize"]->property()->setValue(v.toString());
386
 
 
387
 
                        v = (*m_children)["hStretch"]->value();
388
 
                        p.setHorStretch(v.toInt());
389
 
                        (*m_children)["hStretch"]->property()->setValue(v.toInt());
390
 
 
391
 
                        v = (*m_children)["vStretch"]->value();
392
 
                        p.setVerStretch(v.toInt());
393
 
                        (*m_children)["vStretch"]->property()->setValue(v.toInt());
394
 
 
395
 
                        setValue(p);
396
 
                        return p;
397
 
                }
398
 
 
399
 
                default:
400
 
                {
401
 
                        return 0;
402
 
                }
403
 
 
404
 
        }
405
 
}
406
 
#endif
407
 
 
408
 
/* let's move this to KexiProperty?
409
 
void
410
 
KexiPropertyEditorItem::updateChildValue()
411
 
{
412
 
        if(m_property->type() == QVariant::Rect)
413
 
        {
414
 
                QRect r = m_value.toRect();
415
 
                (*m_children)["x"]->property()->setValue(r.x());
416
 
                (*m_children)["x"]->setValue(r.x());
417
 
                (*m_children)["y"]->property()->setValue(r.y());
418
 
                (*m_children)["y"]->setValue(r.y());
419
 
                (*m_children)["width"]->property()->setValue(r.width());
420
 
                (*m_children)["width"]->setValue(r.width());
421
 
                (*m_children)["height"]->property()->setValue(r.height());
422
 
                (*m_children)["height"]->setValue(r.height());
423
 
        }
424
 
}*/
425
 
 
426
 
KexiPropertyEditorItem::~KexiPropertyEditorItem()
427
 
{
428
 
        if(depth() == 0)
429
 
                delete m_property;
430
 
        delete m_children;
431
 
 
432
 
/*      switch(m_property->type())
433
 
        {
434
 
                case QVariant::Point:
435
 
                case QVariant::Rect:
436
 
                case QVariant::Size:
437
 
                case QVariant::SizePolicy:
438
 
                {
439
 
//                      delete m_childprop;
440
 
//                      delete m_children;
441
 
                }
442
 
                default:
443
 
                {
444
 
                        return;
445
 
                }
446
 
        }*/
447
 
}
448
 
 
449
 
void KexiPropertyEditorItem::updateValue(bool alsoParent)
450
 
{
451
 
        QString specialValueText;
452
 
        if (m_property->hasOptions() && m_property->isNumericType()) {
453
 
                //replace min value with minValueText if defined
454
 
                QVariant minValue( m_property->option("min") );
455
 
                QVariant minValueText( m_property->option("minValueText") );
456
 
                if (!minValue.isNull() && !minValueText.isNull() && minValue.toInt()==m_property->value().toInt()) {
457
 
                        specialValueText = minValueText.toString();
458
 
                }
459
 
        }
460
 
        setText( 1, specialValueText.isEmpty() ? m_property->valueText() : specialValueText );
461
 
        if (alsoParent && parent())
462
 
                static_cast<KexiPropertyEditorItem*>(parent())->updateValue();
463
 
}
464
 
 
465
 
void
466
 
KexiPropertyEditorItem::updateChildrenValue()
467
 
{
468
 
        //update children value
469
 
        KexiPropertyEditorItem *it = static_cast<KexiPropertyEditorItem*>(firstChild());
470
 
        while (it) {
471
 
                it->updateValue(false);
472
 
                it = static_cast<KexiPropertyEditorItem*>(it->nextSibling());
473
 
        }
474
 
}
475
 
 
476
 
/*QString KexiPropertyEditorItem::key( int column, bool ascending ) const
477
 
{
478
 
        if (!ascending)
479
 
                return QListViewItem::key(column, ascending);
480
 
 
481
 
        return QString().sprintf("%5.5d", m_order);
482
 
}*/
483
 
 
484
 
int KexiPropertyEditorItem::compare( QListViewItem *i, int col, bool ascending ) const
485
 
{
486
 
        if (!ascending)
487
 
                return -QListViewItem::key( col, ascending ).localeAwareCompare( i->key( col, ascending ) );
488
 
 
489
 
        return m_order - static_cast<KexiPropertyEditorItem*>(i)->m_order;
490
 
//              QString().sprintf("%5.5d", m_order).localeAwareCompare( QString().sprintf("%5.5d", i->m_order) );
491
 
}
492
 
 
493
 
void KexiPropertyEditorItem::paintFocus ( QPainter * , const QColorGroup & , const QRect &  )
494
 
{
495
 
}
496