summaryrefslogtreecommitdiffstats
path: root/lib/widgets/fancylistviewitem.h
blob: e61beed282be5171876c3caea68bdf26f7db7f17 (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
/* This file is part of the KDE project
   Copyright (C) 2006 David Nolden <david.nolden.kdevelop@art-master.de>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library 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., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#ifndef FANCYLISTVIEWITEM
#define FANCYLISTVIEWITEM

#include <tqvaluevector.h>
#include <tqpainter.h>
#include <tqfont.h>
#include <tqlistview.h>
#include <klistview.h>

class TextPaintStyleStore {
    public:
        class Item {
            public:
            TQFont font;
            TQColor color;
            TQColor background;
            
            Item(const TQFont& f = TQFont(), const TQColor& c = TQColor(), const TQColor b = TQColor() ) : font(f), color(c), background(b) {
            }
            
            bool bgValid() {
                return background.isValid();
            }
            
            bool colValid() {
                return color.isValid();
            }
        };
        
        typedef TQMap<int, Item> Store ;
    
        TextPaintStyleStore( TQFont defaultFont=TQFont() ) {
            m_styles.insert( 0, Item( defaultFont ) );
        }
        
        Item& getStyle( int num ) {
            Store::Iterator it = m_styles.find( num );
            if( it != m_styles.end() ) return *it;
            return m_styles[0];
        }
        
        void addStyle( int num, Item& style ) {
            m_styles[num] = style;
        }
        
        void addStyle( int num, const TQFont& font ) {
            m_styles[num] = Item( font );
        }
        
        bool hasStyle( int num ) {
            Store::Iterator it = m_styles.find( num );
            return ( it != m_styles.end() );
        }
        
    private:
        Store m_styles;
};

class TextPaintItem {
    public:
        struct Item {
            TQString text;
            int style;
            
            Item( const TQString& t = "", int st = 0 ) : text(t), style(st) {
            }
            
        };
        typedef TQValueList<Item> Chain;
        
        Chain& items() {
            return m_chain;
        }
        
        TextPaintItem(const TQString& text="") {
            addItem( text );
        }
        
        Item& addItem(const TQString& item, int style = 0) {
            m_chain.append( Item(item, style) );
            return m_chain.back();
        }
        
        void clear() {
            m_chain.clear();
        }
        
        operator TQString () const {
            TQString ret;
            Chain::const_iterator it = m_chain.begin();
            while(it != m_chain.end()) {
                ret += (*it).text;
                ++it;
            }
            return ret;
        }
        
    private:
        Chain m_chain;
};

///does not support multiple column, a "column" represents a part of the real first column
///KListViewItem is only needed for the background-color

class FancyListViewItem : public KListViewItem
{
    public:
        FancyListViewItem(TextPaintStyleStore& styles, TQListView *tqparent, const TQString &label1, const TQString &label2="") : KListViewItem(tqparent, label1, label2), m_styles(styles) {
            init(label1, label2);
        }
        
        FancyListViewItem(TextPaintStyleStore& styles, TQListViewItem *tqparent, const TQString &label1, const TQString &label2="") : KListViewItem(tqparent, label1, label2), m_styles(styles) {
            init(label1, label2);
        }
        
        virtual void paintCell(TQPainter *painter, const TQColorGroup &cg, int column, int width, int align);
        virtual int width(const TQFontMetrics &fm, const TQListView *lv, int column);
        virtual void setText ( int column, const TQString & text );
        virtual TQString text(int column) const;
        
        inline void clear() {
            m_items.clear();
        }
        
        inline TextPaintItem& item(int column = 0) {
            if(m_items.isEmpty()) {
                m_items.append( TextPaintItem() );
            }
            
            return m_items[column];
        }
        
        void setItem(int column, TextPaintItem item);
    private:
        virtual TQColor backgroundColor(int col);
        void init(const TQString& label1, const TQString& label2);
        int textWidth(const TQFont& font, const TQString& text);
        TQValueVector<TextPaintItem> m_items;
    protected:
        TextPaintStyleStore& m_styles;
};

#endif