~ubuntu-branches/ubuntu/karmic/datakiosk/karmic

« back to all changes in this revision

Viewing changes to src/datakiosk/src/viewmode.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2005-06-27 22:48:06 UTC
  • Revision ID: james.westby@ubuntu.com-20050627224806-8farkci1dc2onhbs
Tags: upstream-0.7
ImportĀ upstreamĀ versionĀ 0.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2005 by Adam Treat                                      *
 
3
 *   treat@kde.org                                                         *
 
4
 *                                                                         *
 
5
 *   Copyright (C) 2004 by Scott Wheeler                                   *
 
6
 *   wheeler@kde.org                                                       *
 
7
 *                                                                         *
 
8
 *   This program is free software; you can redistribute it and/or modify  *
 
9
 *   it under the terms of the GNU General Public License as published by  *
 
10
 *   the Free Software Foundation; either version 2 of the License, or     *
 
11
 *   (at your option) any later version.                                   *
 
12
 *                                                                         *
 
13
 ***************************************************************************/
 
14
 
 
15
#include <kiconloader.h>
 
16
#include <kdebug.h>
 
17
 
 
18
#include <qpixmap.h>
 
19
#include <qpainter.h>
 
20
#include <qregexp.h>
 
21
 
 
22
#include "viewmode.h"
 
23
#include "datatable.h"
 
24
#include "datareport.h"
 
25
 
 
26
ViewMode::ViewMode( DataTableBox *b ) :
 
27
        QObject( b ),
 
28
        m_dataTableBox( b ),
 
29
        m_visible( false ),
 
30
        m_needsRefresh( false )
 
31
{
 
32
    m_dataTableBox->viewport() ->installEventFilter( this );
 
33
}
 
34
 
 
35
ViewMode::~ViewMode()
 
36
{}
 
37
 
 
38
void ViewMode::paintCell( DataTableBox::Item *item,
 
39
                          QPainter *painter,
 
40
                          const QColorGroup &colorGroup,
 
41
                          int column, int width, int )
 
42
{
 
43
 
 
44
    const QPixmap *pm = item->pixmap( column );
 
45
 
 
46
    if ( !pm )
 
47
        return;
 
48
 
 
49
    if ( width < pm ->width() )
 
50
        return ;
 
51
 
 
52
    if ( m_needsRefresh )
 
53
        updateHeights();
 
54
 
 
55
    QFontMetrics fm = painter->fontMetrics();
 
56
 
 
57
    int y = item->listView() ->itemMargin() + border;
 
58
 
 
59
    if ( item->isSelected() )
 
60
    {
 
61
        painter->eraseRect( 0, 0, width, item->height() );
 
62
 
 
63
        QPen oldPen = painter->pen();
 
64
        QPen newPen = oldPen;
 
65
 
 
66
        newPen.setWidth( 5 );
 
67
        newPen.setJoinStyle( RoundJoin );
 
68
        newPen.setColor( QColorGroup::Highlight );
 
69
 
 
70
        painter->setPen( newPen );
 
71
        painter->drawRect( border, border, width - border * 2, item->height() - border * 2 + 1 );
 
72
        painter->setPen( oldPen );
 
73
 
 
74
        painter->fillRect( border, border, width - border * 2, item->height() - border * 2 + 1,
 
75
                           colorGroup.brush( QColorGroup::Highlight ) );
 
76
        painter->setPen( colorGroup.highlightedText() );
 
77
    }
 
78
    else
 
79
        painter->eraseRect( 0, 0, width, item->height() );
 
80
 
 
81
    if ( !pm->isNull() )
 
82
    {
 
83
        int x = ( width - pm->width() ) / 2;
 
84
        x = QMAX( x, item->listView() ->itemMargin() );
 
85
        painter->drawPixmap( x, y, *pm );
 
86
    }
 
87
    y += pm->height() + fm.height() - fm.descent();
 
88
    for ( QStringList::Iterator it = m_lines[ item ].begin(); it != m_lines[ item ].end(); ++it )
 
89
    {
 
90
        int x = ( width - fm.width( *it ) ) / 2;
 
91
        x = QMAX( x, item->listView() ->itemMargin() );
 
92
        painter->drawText( x, y, *it );
 
93
        y += fm.height() - fm.descent();
 
94
    }
 
95
 
 
96
    if ( item == item->listView() ->dropItem() )
 
97
        paintDropIndicator( painter, width, item->height() );
 
98
}
 
99
 
 
100
bool ViewMode::eventFilter( QObject *watched, QEvent *e )
 
101
{
 
102
    if ( m_visible && watched == m_dataTableBox->viewport() && e->type() == QEvent::Resize )
 
103
    {
 
104
        QResizeEvent * re = static_cast<QResizeEvent *>( e );
 
105
        if ( re->size().width() != re->oldSize().width() )
 
106
            m_needsRefresh = true;
 
107
    }
 
108
 
 
109
    if ( e->type() == QEvent::Hide )
 
110
        m_needsRefresh = true;
 
111
 
 
112
    return QObject::eventFilter( watched, e );
 
113
}
 
114
 
 
115
void ViewMode::setShown( bool shown )
 
116
{
 
117
    dataTableBox() ->setTreeStepSize( 0 );
 
118
    m_visible = shown;
 
119
    if ( shown )
 
120
    {
 
121
        updateIcons( 32 );
 
122
        m_needsRefresh = true;
 
123
    }
 
124
}
 
125
 
 
126
void ViewMode::updateIcons( int size )
 
127
{
 
128
    for ( QListViewItemIterator it( m_dataTableBox ); it.current(); ++it )
 
129
    {
 
130
        DataTableBox::Item *i = static_cast<DataTableBox::Item *>( *it );
 
131
        i->setPixmap( 0, SmallIcon( i->iconName(), size ) );
 
132
    }
 
133
}
 
134
 
 
135
void ViewMode::setupItem( DataTableBox::Item *item ) const
 
136
{
 
137
    const DataTableBox * box = item->listView();
 
138
    const int width = box->width() - box->verticalScrollBar() ->width() - border * 2;
 
139
    const int baseHeight = 2 * box->itemMargin() + 32 + border * 2;
 
140
    const QFontMetrics fm = box->fontMetrics();
 
141
    item->setHeight( baseHeight + ( fm.height() - fm.descent() ) * lines( item, fm, width ).count() );
 
142
}
 
143
 
 
144
void ViewMode::updateHeights()
 
145
{
 
146
    const int width = m_dataTableBox->width() - m_dataTableBox->verticalScrollBar() ->width() - border * 2;
 
147
 
 
148
    const int baseHeight = 2 * m_dataTableBox->itemMargin() + 32 + border * 2;
 
149
    const QFontMetrics fm = m_dataTableBox->fontMetrics();
 
150
 
 
151
    for ( QListViewItemIterator it( m_dataTableBox ); it.current(); ++it )
 
152
    {
 
153
        DataTableBox::Item *i = static_cast<DataTableBox::Item *>( it.current() );
 
154
        m_lines[ i ] = lines( i, fm, width );
 
155
        const int height = baseHeight + ( fm.height() - fm.descent() ) * m_lines[ i ].count();
 
156
        i->setHeight( height );
 
157
    }
 
158
 
 
159
    m_needsRefresh = false;
 
160
}
 
161
 
 
162
void ViewMode::paintDropIndicator( QPainter *painter, int width, int height )       // static
 
163
{
 
164
    static const int border = 1;
 
165
    static const int lineWidth = 2;
 
166
 
 
167
    QPen oldPen = painter->pen();
 
168
    QPen newPen = oldPen;
 
169
 
 
170
    newPen.setWidth( lineWidth );
 
171
    newPen.setStyle( DotLine );
 
172
 
 
173
    painter->setPen( newPen );
 
174
    painter->drawRect( border, border, width - border * 2, height - border * 2 );
 
175
    painter->setPen( oldPen );
 
176
}
 
177
 
 
178
QStringList ViewMode::lines( const DataTableBox::Item *item,
 
179
                             const QFontMetrics &fm,
 
180
                             int width )
 
181
{
 
182
    // Here 32 is a bit arbitrary, but that's the width of the icons in this
 
183
    // mode and seems to a reasonable lower bound.
 
184
 
 
185
    if ( width < 32 )
 
186
        return QStringList();
 
187
 
 
188
 
 
189
    QString line;
 
190
    if ( DataTable * dataTable = dynamic_cast<DataTable*>( item->itemWidget() ) )
 
191
    {
 
192
        QStringList lst;
 
193
        lst.append( item->text() );
 
194
        lst.append( " (" );
 
195
        lst.append( QString::number( dataTable->dataTableView()->numRows() ) );
 
196
        lst.append( ")" );
 
197
        line = lst.join( "" );
 
198
    }
 
199
    else if ( DataReport * dataReport = dynamic_cast<DataReport*>( item->itemWidget() ) )
 
200
    {
 
201
        QStringList lst;
 
202
        lst.append( item->text() );
 
203
        lst.append( " (" );
 
204
        lst.append( QString::number( dataReport->numRows() ) );
 
205
        lst.append( ")" );
 
206
        line = lst.join( "" );
 
207
    }
 
208
    QStringList l;
 
209
 
 
210
    while ( !line.isEmpty() )
 
211
    {
 
212
        int textLength = line.length();
 
213
        while ( textLength > 0 &&
 
214
                fm.width( line.mid( 0, textLength ).stripWhiteSpace() ) +
 
215
                item->listView() ->itemMargin() * 2 > width )
 
216
        {
 
217
            int i = line.findRev( QRegExp( "\\W" ), textLength - 1 );
 
218
            if ( i > 0 )
 
219
                textLength = i;
 
220
            else
 
221
                textLength--;
 
222
        }
 
223
 
 
224
        l.append( line.mid( 0, textLength ).stripWhiteSpace() );
 
225
        line = line.mid( textLength );
 
226
    }
 
227
    return l;
 
228
}
 
229
 
 
230
CompactViewMode::CompactViewMode( DataTableBox *b ) :
 
231
        ViewMode( b )
 
232
{
 
233
}
 
234
 
 
235
CompactViewMode::~CompactViewMode()
 
236
{}
 
237
 
 
238
void CompactViewMode::paintCell( DataTableBox::Item *item,
 
239
                                 QPainter *painter,
 
240
                                 const QColorGroup &colorGroup,
 
241
                                 int column, int width, int )
 
242
{
 
243
    const QPixmap *pm = item->pixmap( column );
 
244
 
 
245
    if ( !pm )
 
246
        return;
 
247
 
 
248
    if ( width < pm ->width() )
 
249
        return ;
 
250
 
 
251
    if ( m_needsRefresh )
 
252
        updateHeights();
 
253
 
 
254
    QFontMetrics fm = painter->fontMetrics();
 
255
 
 
256
    int x = item->listView() ->itemMargin() + border;
 
257
    int y = item->listView() ->itemMargin() + border;
 
258
 
 
259
    if ( item->isSelected() )
 
260
    {
 
261
        painter->eraseRect( 0, 0, width, item->height() );
 
262
 
 
263
        QPen oldPen = painter->pen();
 
264
        QPen newPen = oldPen;
 
265
 
 
266
        newPen.setWidth( 5 );
 
267
        newPen.setJoinStyle( RoundJoin );
 
268
        newPen.setColor( QColorGroup::Highlight );
 
269
 
 
270
        painter->setPen( newPen );
 
271
        painter->drawRect( border, border, width - border * 2, item->height() - border * 2 + 1 );
 
272
        painter->setPen( oldPen );
 
273
 
 
274
        painter->fillRect( border, border, width - border * 2, item->height() - border * 2 + 1,
 
275
                           colorGroup.brush( QColorGroup::Highlight ) );
 
276
        painter->setPen( colorGroup.highlightedText() );
 
277
    }
 
278
    else
 
279
        painter->eraseRect( 0, 0, width, item->height() );
 
280
 
 
281
    if ( !pm->isNull() )
 
282
    {
 
283
        painter->drawPixmap( x, y, *pm );
 
284
        x += pm->width() + border;
 
285
    }
 
286
    for ( QStringList::Iterator it = m_lines[ item ].begin(); it != m_lines[ item ].end(); ++it )
 
287
    {
 
288
        y += fm.height() - fm.descent();
 
289
        painter->drawText( x, y, *it );
 
290
    }
 
291
 
 
292
    if ( item == item->listView() ->dropItem() )
 
293
        paintDropIndicator( painter, width, item->height() );
 
294
}
 
295
 
 
296
void CompactViewMode::setupItem( DataTableBox::Item *item ) const
 
297
{
 
298
    item->KListViewItem::setup();
 
299
}
 
300
 
 
301
void CompactViewMode::setShown( bool shown )
 
302
{
 
303
    dataTableBox() ->setTreeStepSize( 20 );
 
304
    setVisible( shown );
 
305
 
 
306
    if ( shown )
 
307
    {
 
308
        updateIcons( 16 );
 
309
        updateHeights();
 
310
    }
 
311
}
 
312
 
 
313
void CompactViewMode::updateHeights()
 
314
{
 
315
    const int width = m_dataTableBox->width() - m_dataTableBox->verticalScrollBar() ->width() - border * 2;
 
316
 
 
317
    const int baseHeight = 2 * m_dataTableBox->itemMargin() + 16 + border * 2;
 
318
    const QFontMetrics fm = m_dataTableBox->fontMetrics();
 
319
 
 
320
    for ( QListViewItemIterator it( m_dataTableBox ); it.current(); ++it )
 
321
    {
 
322
        DataTableBox::Item *i = static_cast<DataTableBox::Item *>( it.current() );
 
323
        m_lines[ i ] = lines( i, fm, width );
 
324
        const int height = baseHeight + ( fm.height() - fm.descent() ) * ( m_lines[ i ].count() - 1 );
 
325
        i->setHeight( height );
 
326
    }
 
327
 
 
328
    m_needsRefresh = false;
 
329
}
 
330
 
 
331
TreeViewMode::TreeViewMode( DataTableBox *b ) : CompactViewMode( b )
 
332
{}
 
333
 
 
334
TreeViewMode::~TreeViewMode()
 
335
{}
 
336
 
 
337
void TreeViewMode::setShown( bool show )
 
338
{
 
339
    CompactViewMode::setShown( show );
 
340
    dataTableBox() ->setRootIsDecorated( show );
 
341
}
 
342
 
 
343
#include "viewmode.moc"