1
/***************************************************************************
2
* Copyright (C) 2005 by Adam Treat *
5
* Copyright (C) 2004 by Scott Wheeler *
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. *
13
***************************************************************************/
15
#include <kiconloader.h>
23
#include "datatable.h"
24
#include "datareport.h"
26
ViewMode::ViewMode( DataTableBox *b ) :
30
m_needsRefresh( false )
32
m_dataTableBox->viewport() ->installEventFilter( this );
38
void ViewMode::paintCell( DataTableBox::Item *item,
40
const QColorGroup &colorGroup,
41
int column, int width, int )
44
const QPixmap *pm = item->pixmap( column );
49
if ( width < pm ->width() )
55
QFontMetrics fm = painter->fontMetrics();
57
int y = item->listView() ->itemMargin() + border;
59
if ( item->isSelected() )
61
painter->eraseRect( 0, 0, width, item->height() );
63
QPen oldPen = painter->pen();
67
newPen.setJoinStyle( RoundJoin );
68
newPen.setColor( QColorGroup::Highlight );
70
painter->setPen( newPen );
71
painter->drawRect( border, border, width - border * 2, item->height() - border * 2 + 1 );
72
painter->setPen( oldPen );
74
painter->fillRect( border, border, width - border * 2, item->height() - border * 2 + 1,
75
colorGroup.brush( QColorGroup::Highlight ) );
76
painter->setPen( colorGroup.highlightedText() );
79
painter->eraseRect( 0, 0, width, item->height() );
83
int x = ( width - pm->width() ) / 2;
84
x = QMAX( x, item->listView() ->itemMargin() );
85
painter->drawPixmap( x, y, *pm );
87
y += pm->height() + fm.height() - fm.descent();
88
for ( QStringList::Iterator it = m_lines[ item ].begin(); it != m_lines[ item ].end(); ++it )
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();
96
if ( item == item->listView() ->dropItem() )
97
paintDropIndicator( painter, width, item->height() );
100
bool ViewMode::eventFilter( QObject *watched, QEvent *e )
102
if ( m_visible && watched == m_dataTableBox->viewport() && e->type() == QEvent::Resize )
104
QResizeEvent * re = static_cast<QResizeEvent *>( e );
105
if ( re->size().width() != re->oldSize().width() )
106
m_needsRefresh = true;
109
if ( e->type() == QEvent::Hide )
110
m_needsRefresh = true;
112
return QObject::eventFilter( watched, e );
115
void ViewMode::setShown( bool shown )
117
dataTableBox() ->setTreeStepSize( 0 );
122
m_needsRefresh = true;
126
void ViewMode::updateIcons( int size )
128
for ( QListViewItemIterator it( m_dataTableBox ); it.current(); ++it )
130
DataTableBox::Item *i = static_cast<DataTableBox::Item *>( *it );
131
i->setPixmap( 0, SmallIcon( i->iconName(), size ) );
135
void ViewMode::setupItem( DataTableBox::Item *item ) const
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() );
144
void ViewMode::updateHeights()
146
const int width = m_dataTableBox->width() - m_dataTableBox->verticalScrollBar() ->width() - border * 2;
148
const int baseHeight = 2 * m_dataTableBox->itemMargin() + 32 + border * 2;
149
const QFontMetrics fm = m_dataTableBox->fontMetrics();
151
for ( QListViewItemIterator it( m_dataTableBox ); it.current(); ++it )
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 );
159
m_needsRefresh = false;
162
void ViewMode::paintDropIndicator( QPainter *painter, int width, int height ) // static
164
static const int border = 1;
165
static const int lineWidth = 2;
167
QPen oldPen = painter->pen();
168
QPen newPen = oldPen;
170
newPen.setWidth( lineWidth );
171
newPen.setStyle( DotLine );
173
painter->setPen( newPen );
174
painter->drawRect( border, border, width - border * 2, height - border * 2 );
175
painter->setPen( oldPen );
178
QStringList ViewMode::lines( const DataTableBox::Item *item,
179
const QFontMetrics &fm,
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.
186
return QStringList();
190
if ( DataTable * dataTable = dynamic_cast<DataTable*>( item->itemWidget() ) )
193
lst.append( item->text() );
195
lst.append( QString::number( dataTable->dataTableView()->numRows() ) );
197
line = lst.join( "" );
199
else if ( DataReport * dataReport = dynamic_cast<DataReport*>( item->itemWidget() ) )
202
lst.append( item->text() );
204
lst.append( QString::number( dataReport->numRows() ) );
206
line = lst.join( "" );
210
while ( !line.isEmpty() )
212
int textLength = line.length();
213
while ( textLength > 0 &&
214
fm.width( line.mid( 0, textLength ).stripWhiteSpace() ) +
215
item->listView() ->itemMargin() * 2 > width )
217
int i = line.findRev( QRegExp( "\\W" ), textLength - 1 );
224
l.append( line.mid( 0, textLength ).stripWhiteSpace() );
225
line = line.mid( textLength );
230
CompactViewMode::CompactViewMode( DataTableBox *b ) :
235
CompactViewMode::~CompactViewMode()
238
void CompactViewMode::paintCell( DataTableBox::Item *item,
240
const QColorGroup &colorGroup,
241
int column, int width, int )
243
const QPixmap *pm = item->pixmap( column );
248
if ( width < pm ->width() )
251
if ( m_needsRefresh )
254
QFontMetrics fm = painter->fontMetrics();
256
int x = item->listView() ->itemMargin() + border;
257
int y = item->listView() ->itemMargin() + border;
259
if ( item->isSelected() )
261
painter->eraseRect( 0, 0, width, item->height() );
263
QPen oldPen = painter->pen();
264
QPen newPen = oldPen;
266
newPen.setWidth( 5 );
267
newPen.setJoinStyle( RoundJoin );
268
newPen.setColor( QColorGroup::Highlight );
270
painter->setPen( newPen );
271
painter->drawRect( border, border, width - border * 2, item->height() - border * 2 + 1 );
272
painter->setPen( oldPen );
274
painter->fillRect( border, border, width - border * 2, item->height() - border * 2 + 1,
275
colorGroup.brush( QColorGroup::Highlight ) );
276
painter->setPen( colorGroup.highlightedText() );
279
painter->eraseRect( 0, 0, width, item->height() );
283
painter->drawPixmap( x, y, *pm );
284
x += pm->width() + border;
286
for ( QStringList::Iterator it = m_lines[ item ].begin(); it != m_lines[ item ].end(); ++it )
288
y += fm.height() - fm.descent();
289
painter->drawText( x, y, *it );
292
if ( item == item->listView() ->dropItem() )
293
paintDropIndicator( painter, width, item->height() );
296
void CompactViewMode::setupItem( DataTableBox::Item *item ) const
298
item->KListViewItem::setup();
301
void CompactViewMode::setShown( bool shown )
303
dataTableBox() ->setTreeStepSize( 20 );
313
void CompactViewMode::updateHeights()
315
const int width = m_dataTableBox->width() - m_dataTableBox->verticalScrollBar() ->width() - border * 2;
317
const int baseHeight = 2 * m_dataTableBox->itemMargin() + 16 + border * 2;
318
const QFontMetrics fm = m_dataTableBox->fontMetrics();
320
for ( QListViewItemIterator it( m_dataTableBox ); it.current(); ++it )
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 );
328
m_needsRefresh = false;
331
TreeViewMode::TreeViewMode( DataTableBox *b ) : CompactViewMode( b )
334
TreeViewMode::~TreeViewMode()
337
void TreeViewMode::setShown( bool show )
339
CompactViewMode::setShown( show );
340
dataTableBox() ->setRootIsDecorated( show );
343
#include "viewmode.moc"