| 1 | /**************************************************************************** | 
|---|
| 2 | ** $Id: statistics.cpp 160 2006-12-11 20:15:57Z dmik $ | 
|---|
| 3 | ** | 
|---|
| 4 | ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved. | 
|---|
| 5 | ** | 
|---|
| 6 | ** This file is part of an example program for Qt.  This example | 
|---|
| 7 | ** program may be used, distributed and modified without limitation. | 
|---|
| 8 | ** | 
|---|
| 9 | *****************************************************************************/ | 
|---|
| 10 |  | 
|---|
| 11 | #include "statistics.h" | 
|---|
| 12 |  | 
|---|
| 13 | #include <qdir.h> | 
|---|
| 14 | #include <qstringlist.h> | 
|---|
| 15 | #include <qheader.h> | 
|---|
| 16 | #include <qcombobox.h> | 
|---|
| 17 | #include <stdlib.h> | 
|---|
| 18 |  | 
|---|
| 19 | const char* dirs[] = { | 
|---|
| 20 | "kernel", | 
|---|
| 21 | "tools", | 
|---|
| 22 | "widgets", | 
|---|
| 23 | "dialogs", | 
|---|
| 24 | "xml", | 
|---|
| 25 | "table", | 
|---|
| 26 | "network", | 
|---|
| 27 | "opengl", | 
|---|
| 28 | "canvas", | 
|---|
| 29 | 0 | 
|---|
| 30 | }; | 
|---|
| 31 |  | 
|---|
| 32 | Table::Table() | 
|---|
| 33 | : QTable( 10, 100, 0, "table" ) | 
|---|
| 34 | { | 
|---|
| 35 | setSorting( TRUE ); | 
|---|
| 36 | horizontalHeader()->setLabel( 0, tr( "File" ) ); | 
|---|
| 37 | horizontalHeader()->setLabel( 1, tr( "Size (bytes)" ) ); | 
|---|
| 38 | horizontalHeader()->setLabel( 2, tr( "Use in Sum" ) ); | 
|---|
| 39 | initTable(); | 
|---|
| 40 | adjustColumn( 0 ); | 
|---|
| 41 |  | 
|---|
| 42 | // if the user edited something we might need to recalculate the sum | 
|---|
| 43 | connect( this, SIGNAL( valueChanged( int, int ) ), | 
|---|
| 44 | this, SLOT( recalcSum( int, int ) ) ); | 
|---|
| 45 | } | 
|---|
| 46 |  | 
|---|
| 47 | void Table::initTable() | 
|---|
| 48 | { | 
|---|
| 49 | // read all the Qt source and header files into a list | 
|---|
| 50 | QStringList all; | 
|---|
| 51 | int i = 0; | 
|---|
| 52 | QString srcdir( "../../../src/" ); | 
|---|
| 53 | while ( dirs[ i ] ) { | 
|---|
| 54 | QDir dir( srcdir + dirs[ i ] ); | 
|---|
| 55 | QStringList lst = dir.entryList( "*.cpp; *.h" ); | 
|---|
| 56 | for ( QStringList::Iterator it = lst.begin(); it != lst.end(); ++it ) { | 
|---|
| 57 | if ( ( *it ).contains( "moc" ) ) | 
|---|
| 58 | continue; | 
|---|
| 59 | all << (QString( dirs[ i ] ) + "/" + *it); | 
|---|
| 60 | } | 
|---|
| 61 | ++i; | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 | // set the number of rows we'll need for the table | 
|---|
| 65 | setNumRows( all.count() + 1 ); | 
|---|
| 66 | i = 0; | 
|---|
| 67 | int sum = 0; | 
|---|
| 68 |  | 
|---|
| 69 | // insert the data into the table | 
|---|
| 70 | for ( QStringList::Iterator it = all.begin(); it != all.end(); ++it ) { | 
|---|
| 71 | setText( i, 0, *it ); | 
|---|
| 72 | QFile f( srcdir + *it ); | 
|---|
| 73 | setText( i, 1, QString::number( (ulong)f.size() ) ); | 
|---|
| 74 | ComboItem *ci = new ComboItem( this, QTableItem::WhenCurrent ); | 
|---|
| 75 | setItem( i++, 2, ci ); | 
|---|
| 76 | sum += f.size(); | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | // last row should show the sum | 
|---|
| 80 | TableItem *i1 = new TableItem( this, QTableItem::Never, tr( "Sum" ) ); | 
|---|
| 81 | setItem( i, 0, i1 ); | 
|---|
| 82 | TableItem *i2 = new TableItem( this, QTableItem::Never, QString::number( sum ) ); | 
|---|
| 83 | setItem( i, 1, i2 ); | 
|---|
| 84 | } | 
|---|
| 85 |  | 
|---|
| 86 | void Table::recalcSum( int, int col ) | 
|---|
| 87 | { | 
|---|
| 88 | // only recalc if a value in the second or third column changed | 
|---|
| 89 | if ( col < 1 || col > 2 ) | 
|---|
| 90 | return; | 
|---|
| 91 |  | 
|---|
| 92 | // recalc sum | 
|---|
| 93 | int sum = 0; | 
|---|
| 94 | for ( int i = 0; i < numRows() - 1; ++i ) { | 
|---|
| 95 | if ( text( i, 2 ) == "No" ) | 
|---|
| 96 | continue; | 
|---|
| 97 | sum += text( i, 1 ).toInt(); | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | // insert calculated data | 
|---|
| 101 | TableItem *i1 = new TableItem( this, QTableItem::Never, tr( "Sum" ) ); | 
|---|
| 102 | setItem( numRows() - 1, 0, i1 ); | 
|---|
| 103 | TableItem *i2 = new TableItem( this, QTableItem::Never, QString::number( sum ) ); | 
|---|
| 104 | setItem( numRows() - 1, 1, i2 ); | 
|---|
| 105 | } | 
|---|
| 106 |  | 
|---|
| 107 | void Table::sortColumn( int col, bool ascending, bool /*wholeRows*/ ) | 
|---|
| 108 | { | 
|---|
| 109 | // sum row should not be sorted, so get rid of it for now | 
|---|
| 110 | clearCell( numRows() - 1, 0 ); | 
|---|
| 111 | clearCell( numRows() - 1, 1 ); | 
|---|
| 112 | // do sort | 
|---|
| 113 | QTable::sortColumn( col, ascending, TRUE ); | 
|---|
| 114 | // re-insert sum row | 
|---|
| 115 | recalcSum( 0, 1 ); | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 |  | 
|---|
| 119 |  | 
|---|
| 120 | void TableItem::paint( QPainter *p, const QColorGroup &cg, const QRect &cr, bool selected ) | 
|---|
| 121 | { | 
|---|
| 122 | QColorGroup g( cg ); | 
|---|
| 123 | // last row is the sum row - we want to make it more visible by | 
|---|
| 124 | // using a red background | 
|---|
| 125 | if ( row() == table()->numRows() - 1 ) | 
|---|
| 126 | g.setColor( QColorGroup::Base, red ); | 
|---|
| 127 | QTableItem::paint( p, g, cr, selected ); | 
|---|
| 128 | } | 
|---|
| 129 |  | 
|---|
| 130 |  | 
|---|
| 131 |  | 
|---|
| 132 |  | 
|---|
| 133 | ComboItem::ComboItem( QTable *t, EditType et ) | 
|---|
| 134 | : QTableItem( t, et, "Yes" ), cb( 0 ) | 
|---|
| 135 | { | 
|---|
| 136 | // we do not want this item to be replaced | 
|---|
| 137 | setReplaceable( FALSE ); | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 | QWidget *ComboItem::createEditor() const | 
|---|
| 141 | { | 
|---|
| 142 | // create an editor - a combobox in our case | 
|---|
| 143 | ( (ComboItem*)this )->cb = new QComboBox( table()->viewport() ); | 
|---|
| 144 | QObject::connect( cb, SIGNAL( activated( int ) ), table(), SLOT( doValueChanged() ) ); | 
|---|
| 145 | cb->insertItem( "Yes" ); | 
|---|
| 146 | cb->insertItem( "No" ); | 
|---|
| 147 | // and initialize it | 
|---|
| 148 | cb->setCurrentItem( text() == "No" ? 1 : 0 ); | 
|---|
| 149 | return cb; | 
|---|
| 150 | } | 
|---|
| 151 |  | 
|---|
| 152 | void ComboItem::setContentFromEditor( QWidget *w ) | 
|---|
| 153 | { | 
|---|
| 154 | // the user changed the value of the combobox, so synchronize the | 
|---|
| 155 | // value of the item (its text), with the value of the combobox | 
|---|
| 156 | if ( w->inherits( "QComboBox" ) ) | 
|---|
| 157 | setText( ( (QComboBox*)w )->currentText() ); | 
|---|
| 158 | else | 
|---|
| 159 | QTableItem::setContentFromEditor( w ); | 
|---|
| 160 | } | 
|---|
| 161 |  | 
|---|
| 162 | void ComboItem::setText( const QString &s ) | 
|---|
| 163 | { | 
|---|
| 164 | if ( cb ) { | 
|---|
| 165 | // initialize the combobox from the text | 
|---|
| 166 | if ( s == "No" ) | 
|---|
| 167 | cb->setCurrentItem( 1 ); | 
|---|
| 168 | else | 
|---|
| 169 | cb->setCurrentItem( 0 ); | 
|---|
| 170 | } | 
|---|
| 171 | QTableItem::setText( s ); | 
|---|
| 172 | } | 
|---|