summaryrefslogtreecommitdiff
path: root/libopie2/opieui/olistview.cpp
blob: a5cf6f3765f841cc7b7ef8214d2712c0640af321 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
/*
                             This file is part of the Opie Project
              =.             (C) 2003-2004 Michael 'Mickey' Lauer <mickey@Vanille.de>
            .=l.
           .>+-=
 _;:,     .>    :=|.         This program is free software; you can
.> <`_,   >  .   <=          redistribute it and/or  modify it under
:`=1 )Y*s>-.--   :           the terms of the GNU Library General Public
.="- .-=="i,     .._         License as published by the Free Software
 - .   .-<_>     .<>         Foundation; either version 2 of the License,
     ._= =}       :          or (at your option) any later version.
    .%`+i>       _;_.
    .i_,=:_.      -<s.       This program is distributed in the hope that
     +  .  -:.       =       it will be useful,  but WITHOUT ANY WARRANTY;
    : ..    .:,     . . .    without even the implied warranty of
    =_        +     =;=|`    MERCHANTABILITY or FITNESS FOR A
  _.=:.       :    :=>`:     PARTICULAR PURPOSE. See the GNU
..}^=.=       =       ;      Library General Public License for more
++=   -.     .`     .:       details.
 :     =  ...= . :.=-
 -.   .:....=;==+<;          You should have received a copy of the GNU
  -_. . .   )=.  =           Library General Public License along with
    --        :-=`           this library; see the file COPYING.LIB.
                             If not, write to the Free Software Foundation,
                             Inc., 59 Temple Place - Suite 330,
                             Boston, MA 02111-1307, USA.

*/

/* QT */

#include <qpixmap.h>

/* OPIE */

#include <opie2/odebug.h>
#include <opie2/olistview.h>

using namespace Opie::Core;


namespace Opie {
namespace Ui   {
/*======================================================================================
 * OListView
 *======================================================================================*/

OListView::OListView( QWidget *parent, const char *name, WFlags fl )
          :QListView( parent, name, fl )
{
    //FIXME: get from global settings and calculate ==> see oglobalsettings.*

    m_alternateBackground = QColor( 228, 236, 245 );
    m_columnSeparator = QPen( QColor( 50, 60, 70 ), 0, DotLine );
    m_fullWidth = true;
    connect( this, SIGNAL(expanded(QListViewItem*)), SLOT(expand(QListViewItem*)));
}

OListView::~OListView()
{
}

void OListView::setFullWidth( bool fullWidth )
{
    m_fullWidth = fullWidth;
    #if QT_VERSION >= 0x030000
    header()->setStretchEnabled( fullWidth, columns()-1 );
    #endif
}

bool OListView::fullWidth() const
{
    return m_fullWidth;
}

int OListView::addColumn( const QString& label, int width )
{
    int result = QListView::addColumn( label, width );
    #if QT_VERSION >= 0x030000
    if (m_fullWidth) {
        header()->setStretchEnabled( false, columns()-2 );
        header()->setStretchEnabled( true, columns()-1  );
    }
    #endif
    return result;
}

int OListView::addColumn( const QIconSet& iconset, const QString& label, int width )
{
    int result = QListView::addColumn( iconset, label, width );
    #if QT_VERSION >= 0x030000
    if (m_fullWidth) {
        header()->setStretchEnabled( false, columns()-2 );
        header()->setStretchEnabled( true, columns()-1 );
    }
    #endif
    return result;
}

void OListView::removeColumn( int index )
{
    QListView::removeColumn(index);
    #if QT_VERSION >= 0x030000
    if ( m_fullWidth && index == columns() )
    {
        header()->setStretchEnabled( true, columns()-1 );
    }
    #endif
}

const QColor& OListView::alternateBackground() const
{
    return m_alternateBackground;
}

void OListView::setAlternateBackground( const QColor &c )
{
    m_alternateBackground = c;
    repaint();
}

const QPen& OListView::columnSeparator() const
{
    return m_columnSeparator;
}

void OListView::setColumnSeparator( const QPen& p )
{
    m_columnSeparator = p;
    repaint();
}

void OListView::expand(QListViewItem *item)
{
 	((OListViewItem*)item)->expand();
}

OListViewItem* OListView::childFactory()
{
    return new OListViewItem( this );
}

#ifndef QT_NO_DATASTREAM
void OListView::serializeTo( QDataStream& s ) const
{
    #warning Caution... the binary format is still under construction...
    odebug << "storing OListView..." << oendl;

    // store number of columns and the labels
    s << columns();
    for ( int i = 0; i < columns(); ++i )
        s << columnText( i );

    // calculate the number of top-level items to serialize
    int items = 0;
    QListViewItem* item = firstChild();
    while ( item )
    {
        item = item->nextSibling();
        items++;
    }

    // store number of items and the items itself
    s << items;
    item = firstChild();
    for ( int i = 0; i < items; ++i )
    {
        s << *static_cast<OListViewItem*>( item );
        item = item->nextSibling();
    }

    odebug << "OListview stored." << oendl;
}

void OListView::serializeFrom( QDataStream& s )
{
    #warning Caution... the binary format is still under construction...
    odebug << "loading OListView..." << oendl;

    int cols;
    s >> cols;
    odebug << "read number of columns = " << cols << oendl;

    while ( columns() < cols ) addColumn( QString::null );

    for ( int i = 0; i < cols; ++i )
    {
        QString coltext;
        s >> coltext;
        odebug << "read text '" <<  coltext << "' for column " << i << "" << oendl;
        setColumnText( i, coltext );
    }

    int items;
    s >> items;
    odebug << "read number of items = " << items << oendl;

    for ( int i = 0; i < items; ++i )
    {
        OListViewItem* item = childFactory();
        s >> *item;
    }

    odebug << "OListView loaded." << oendl;

}


void OListView::expand()
{
    odebug << "OListView::expand" << oendl;

    QListViewItemIterator it( this );
    while ( it.current() ) {
        it.current()->setOpen( true );
        ++it;
    }
}


void OListView::collapse()
{
    odebug << "OListView::collapse" << oendl;
    QListViewItemIterator it( this );
    while ( it.current() ) {
        it.current()->setOpen( false );
        ++it;
    }
}


QDataStream& operator<<( QDataStream& s, const OListView& lv )
{
    lv.serializeTo( s );
    return s;
}

QDataStream& operator>>( QDataStream& s, OListView& lv )
{
    lv.serializeFrom( s );
    return s;
}
#endif // QT_NO_DATASTREAM

/*======================================================================================
 * OListViewItem
 *======================================================================================*/

OListViewItem::OListViewItem(QListView *parent)
  : QListViewItem(parent)
{
    init();
}


OListViewItem::OListViewItem(QListViewItem *parent)
  : QListViewItem(parent)
{
    init();
}


OListViewItem::OListViewItem(QListView *parent, QListViewItem *after)
  : QListViewItem(parent, after)
{
    init();
}


OListViewItem::OListViewItem(QListViewItem *parent, QListViewItem *after)
  : QListViewItem(parent, after)
{
    init();
}


OListViewItem::OListViewItem(QListView *parent,
    QString label1, QString label2, QString label3, QString label4,
    QString label5, QString label6, QString label7, QString label8)
  : QListViewItem(parent, label1, label2, label3, label4, label5, label6, label7, label8)
{
    init();
}


OListViewItem::OListViewItem(QListViewItem *parent,
    QString label1, QString label2, QString label3, QString label4,
    QString label5, QString label6, QString label7, QString label8)
  : QListViewItem(parent, label1, label2, label3, label4, label5, label6, label7, label8)
{
    init();
}


OListViewItem::OListViewItem(QListView *parent, QListViewItem *after,
    QString label1, QString label2, QString label3, QString label4,
    QString label5, QString label6, QString label7, QString label8)
  : QListViewItem(parent, after, label1, label2, label3, label4, label5, label6, label7, label8)
{
    init();
}


OListViewItem::OListViewItem(QListViewItem *parent, QListViewItem *after,
    QString label1, QString label2, QString label3, QString label4,
    QString label5, QString label6, QString label7, QString label8)
  : QListViewItem(parent, after, label1, label2, label3, label4, label5, label6, label7, label8)
{
    init();
}


OListViewItem::~OListViewItem()
{
}


void OListViewItem::init()
{
    m_known = false;
}


const QColor &OListViewItem::backgroundColor()
{
    return isAlternate() ? static_cast<OListView*>(listView())->alternateBackground() :
                           listView()->viewport()->colorGroup().base();
}


bool OListViewItem::isAlternate()
{
    OListView *lv = static_cast<OListView*>( listView() );

    // check if the item above is an OListViewItem
    OListViewItem *above = static_cast<OListViewItem*>( itemAbove() );
    /*if (! itemAbove()->inherits( "OListViewItem" )) return false;*/

    // check if we have a valid alternate background color
    if (!(lv && lv->alternateBackground().isValid())) return false;

    m_known = above ? above->m_known : true;
    if (m_known)
    {
       m_odd = above ? !above->m_odd : false;
    }
    else
    {
        OListViewItem *item;
        bool previous = true;
        if (parent())
        {
            item = static_cast<OListViewItem *>(parent());
            if ( item /*&& item->inherits( "OListViewItem" )*/ ) previous = item->m_odd;
            item = static_cast<OListViewItem *>(parent()->firstChild());
            /* if ( !item.inherits( "OListViewItem" ) item = 0; */
        }
        else
        {
            item = static_cast<OListViewItem *>(lv->firstChild());
        }

        while(item)
        {
            item->m_odd = previous = !previous;
            item->m_known = true;
            item = static_cast<OListViewItem *>(item->nextSibling());
            /* if (!item.inherits( "OListViewItem" ) ) break; */
        }
    }
    return m_odd;
}


void OListViewItem::paintCell(QPainter *p, const QColorGroup &cg, int column, int width, int alignment)
{
    QColorGroup _cg = cg;
    const QPixmap *pm = listView()->viewport()->backgroundPixmap();
    if (pm && !pm->isNull())
    {
        _cg.setBrush( QColorGroup::Base, QBrush(backgroundColor(), *pm) );
        p->setBrushOrigin( -listView()->contentsX(), -listView()->contentsY() );
    }
    else if ( isAlternate() )
    {
        _cg.setColor( QColorGroup::Base, static_cast<OListView*>( listView() )->alternateBackground() );
    }
    QListViewItem::paintCell( p, _cg, column, width, alignment );

    //FIXME: Use styling here!

    const QPen& pen = static_cast<OListView*>( listView() )->columnSeparator();
    p->setPen( pen );
    p->drawLine( width-1, 0, width-1, height() );
}


OListViewItem* OListViewItem::childFactory()
{
    return new OListViewItem( this );
}


#ifndef QT_NO_DATASTREAM
void OListViewItem::serializeTo( QDataStream& s ) const
{
    #warning Caution... the binary format is still under construction...
    odebug << "storing OListViewItem..." << oendl;

    // store item text
    for ( int i = 0; i < listView()->columns(); ++i )
    {
        s << text( i );
    }

    // calculate the number of children to serialize
    int items = 0;
    QListViewItem* item = firstChild();
    while ( item )
    {
        item = item->nextSibling();
        items++;
    }

    // store number of items and the items itself
    s << items;
    item = firstChild();
    for ( int i = 0; i < items; ++i )
    {
        s << *static_cast<OListViewItem*>( item );
        item = item->nextSibling();
    }

    odebug << "OListviewItem stored." << oendl;
}


void OListViewItem::serializeFrom( QDataStream& s )
{
    #warning Caution... the binary format is still under construction...
    odebug << "loading OListViewItem..." << oendl;

    for ( int i = 0; i < listView()->columns(); ++i )
    {
        QString coltext;
        s >> coltext;
        odebug << "read text '" <<  coltext << "' for column " << i << "" << oendl;
        setText( i, coltext );
    }

    int items;
    s >> items;
    odebug << "read number of items = " << items << "" << oendl;

    for ( int i = 0; i < items; ++i )
    {
        OListViewItem* item = childFactory();
        s >> (*item);
    }

    odebug << "OListViewItem loaded." << oendl;
}


QDataStream& operator<<( QDataStream& s, const OListViewItem& lvi )
{
    lvi.serializeTo( s );
    return s;
}


QDataStream& operator>>( QDataStream& s, OListViewItem& lvi )
{
    lvi.serializeFrom( s );
    return s;
}
#endif // QT_NO_DATASTREAM


/*======================================================================================
 * OCheckListItem
 *======================================================================================*/

OCheckListItem::OCheckListItem( QCheckListItem* parent, const QString& text, Type t )
               :QCheckListItem( parent, text, t )
{
    init();
}


OCheckListItem::OCheckListItem( QListViewItem* parent, const QString& text, Type t)
               :QCheckListItem( parent, text, t )
{
    init();
}


OCheckListItem::OCheckListItem( QListView* parent, const QString& text, Type t )
               :QCheckListItem( parent, text, t )
{
    init();
}


OCheckListItem::OCheckListItem( QListViewItem* parent, const QString& text, const QPixmap& p )
               :QCheckListItem( parent, text, p )
{
    init();
}


OCheckListItem::OCheckListItem( QListView* parent, const QString& text, const QPixmap& p )
               :QCheckListItem( parent, text, p )
{
    init();
}


OCheckListItem::~OCheckListItem()
{
}

void OCheckListItem::init()
{
    m_known = false;
}


const QColor &OCheckListItem::backgroundColor()
{
    return isAlternate() ? static_cast<OListView*>(listView())->alternateBackground() :
                           listView()->viewport()->colorGroup().base();
}


bool OCheckListItem::isAlternate()
{
    OListView *lv = static_cast<OListView*>( listView() );

    // check if the item above is an OCheckListItem
    OCheckListItem *above = static_cast<OCheckListItem*>( itemAbove() );
    /*if (! itemAbove()->inherits( "OCheckListItem" )) return false;*/

    // check if we have a valid alternate background color
    if (!(lv && lv->alternateBackground().isValid())) return false;

    m_known = above ? above->m_known : true;
    if (m_known)
    {
       m_odd = above ? !above->m_odd : false;
    }
    else
    {
        OCheckListItem *item;
        bool previous = true;
        if (parent())
        {
            item = static_cast<OCheckListItem *>(parent());
            if ( item /*&& item->inherits( "OCheckListItem" )*/ ) previous = item->m_odd;
            item = static_cast<OCheckListItem *>(parent()->firstChild());
            /* if ( !item.inherits( "OCheckListItem" ) item = 0; */
        }
        else
        {
            item = static_cast<OCheckListItem *>(lv->firstChild());
        }

        while(item)
        {
            item->m_odd = previous = !previous;
            item->m_known = true;
            item = static_cast<OCheckListItem *>(item->nextSibling());
            /* if (!item.inherits( "OCheckListItem" ) ) break; */
        }
    }
    return m_odd;
}


void OCheckListItem::paintCell(QPainter *p, const QColorGroup &cg, int column, int width, int alignment)
{
    QColorGroup _cg = cg;
    const QPixmap *pm = listView()->viewport()->backgroundPixmap();
    if (pm && !pm->isNull())
    {
        _cg.setBrush( QColorGroup::Base, QBrush(backgroundColor(), *pm) );
        p->setBrushOrigin( -listView()->contentsX(), -listView()->contentsY() );
    }
    else if ( isAlternate() )
    {
        _cg.setColor( QColorGroup::Base, static_cast<OListView*>( listView() )->alternateBackground() );
    }
    QCheckListItem::paintCell( p, _cg, column, width, alignment );

    //FIXME: Use styling here!

    const QPen& pen = static_cast<OListView*>( listView() )->columnSeparator();
    p->setPen( pen );
    p->drawLine( width-1, 0, width-1, height() );
}


/*======================================================================================
 * ONamedListView
 *======================================================================================*/

ONamedListView::ONamedListView( QWidget *parent, const char *name )
               :OListView( parent, name )
{
}


ONamedListView::~ONamedListView()
{
}


void ONamedListView::addColumns( const QStringList& columns )
{
    for ( QStringList::ConstIterator it = columns.begin(); it != columns.end(); ++it )
    {
        odebug << "adding column " <<  *it << "" << oendl;
        addColumn( *it );
    }
}


int ONamedListView::findColumn( const QString& text ) const
{
    //FIXME: If used excessively, this will slow down performance of updates
    //FIXME: because of the linear search over all column texts.
    //FIXME: I will optimize later by using a hash map.
    for ( int i = 0; i < columns(); ++i )
        if ( columnText( i ) == text )
            return i;
    return -1;
}


ONamedListViewItem* ONamedListView::find( int column, const QString& text, int recurse ) const
{
    return find( (ONamedListViewItem*) firstChild(), column, text, recurse );
}


ONamedListViewItem* ONamedListView::find( ONamedListViewItem* item, int column, const QString& text, int recurse ) const
{
    ONamedListViewItem* result;
    while ( item && item->text( column ) != text )
    {
        odebug << "checked " <<  item->text( column ) << "" << oendl;

        if ( recurse < 0 || recurse > 0 )
        {
            odebug << "recursion is " << recurse << " - recursing into..." << oendl;
            result = find( (ONamedListViewItem*) item->firstChild(), column, text, recurse-1 );
            if ( result ) return result;
        }


        item = (ONamedListViewItem*) item->itemBelow();
    }
    if ( item && item->text( column ) == text )
        return item;
    else
        return 0;
}


ONamedListViewItem* ONamedListView::find( const QString& column, const QString& text, int recurse ) const
{
    int col = findColumn( column );
    if ( col != -1 )
        return find( (ONamedListViewItem*) firstChild(), col, text, recurse );
    else
        return 0;
}


ONamedListViewItem* ONamedListView::find( ONamedListViewItem* item, const QString& column, const QString& text, int recurse ) const
{
    int col = findColumn( column );
    if ( col != -1 )
        return find( item, col, text, recurse );
    else
        return 0;
}


/*======================================================================================
 * ONamedListViewItem
 *======================================================================================*/

ONamedListViewItem::ONamedListViewItem( QListView* parent, const QStringList& texts )
                   :OListViewItem( parent )
{
    setText( texts );
}


ONamedListViewItem::ONamedListViewItem( QListViewItem* parent, const QStringList& texts )
                   :OListViewItem( parent )
{
    setText( texts );
}


ONamedListViewItem::ONamedListViewItem( QListView* parent, QListViewItem* after, const QStringList& texts )
                   :OListViewItem( parent, after )
{
    setText( texts );
}


ONamedListViewItem::ONamedListViewItem( QListViewItem* parent, QListViewItem* after, const QStringList& texts )
                   :OListViewItem( parent, after )
{
    setText( texts );
}


ONamedListViewItem::~ONamedListViewItem()
{
}


void ONamedListViewItem::setText( const QStringList& texts )
{
    int col = 0;
    for ( QStringList::ConstIterator it = texts.begin(); it != texts.end(); ++it )
    {
        odebug << "setting column " << col << " = text " <<  *it << "" << oendl;
        OListViewItem::setText( col++, *it );
    }

}


void ONamedListViewItem::setText( const QString& column, const QString& text )
{
    //FIXME: If used excessively, this will slow down performance of updates
    //FIXME: because of the linear search over all column texts.
    //FIXME: I will optimize later by using a hash map.
    int col = ( (ONamedListView*) listView() )->findColumn( column );
    if ( col != -1 )
        OListViewItem::setText( col, text );
    else
        owarn << "ONamedListViewItem::setText(): Warning! Columntext '" <<  column << "' not found." << oendl;
}


ONamedListViewItem* ONamedListViewItem::find( int column, const QString& text, int recurse ) const
{
    return ( (ONamedListView*) listView() )->find( (ONamedListViewItem*) firstChild(), column, text, recurse );
}


ONamedListViewItem* ONamedListViewItem::find( const QString& column, const QString& text, int recurse ) const
{
    int col = ( (ONamedListView*) listView() )->findColumn( column );
    if ( col != -1 )
        return ( (ONamedListView*) listView() )->find( (ONamedListViewItem*) firstChild(), col, text, recurse );
    else
        return 0;
}

}
}