summaryrefslogtreecommitdiffstats
path: root/examples/table/statistics
diff options
context:
space:
mode:
Diffstat (limited to 'examples/table/statistics')
-rw-r--r--examples/table/statistics/main.cpp20
-rw-r--r--examples/table/statistics/statistics.cpp171
-rw-r--r--examples/table/statistics/statistics.doc27
-rw-r--r--examples/table/statistics/statistics.h52
-rw-r--r--examples/table/statistics/statistics.pro10
5 files changed, 280 insertions, 0 deletions
diff --git a/examples/table/statistics/main.cpp b/examples/table/statistics/main.cpp
new file mode 100644
index 0000000..794bf1c
--- /dev/null
+++ b/examples/table/statistics/main.cpp
@@ -0,0 +1,20 @@
+/****************************************************************************
+**
+** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved.
+**
+** This file is part of an example program for Qt. This example
+** program may be used, distributed and modified without limitation.
+**
+*****************************************************************************/
+
+#include "statistics.h"
+#include <qapplication.h>
+int main( int argc, char **argv )
+{
+ QApplication a(argc,argv);
+
+ Table t;
+ a.setMainWidget( &t );
+ t.show();
+ return a.exec();
+}
diff --git a/examples/table/statistics/statistics.cpp b/examples/table/statistics/statistics.cpp
new file mode 100644
index 0000000..aaac782
--- /dev/null
+++ b/examples/table/statistics/statistics.cpp
@@ -0,0 +1,171 @@
+/****************************************************************************
+**
+** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved.
+**
+** This file is part of an example program for Qt. This example
+** program may be used, distributed and modified without limitation.
+**
+*****************************************************************************/
+
+#include "statistics.h"
+
+#include <qdir.h>
+#include <qstringlist.h>
+#include <qheader.h>
+#include <qcombobox.h>
+#include <stdlib.h>
+
+const char* dirs[] = {
+ "kernel",
+ "tools",
+ "widgets",
+ "dialogs",
+ "xml",
+ "table",
+ "network",
+ "opengl",
+ "canvas",
+ 0
+};
+
+Table::Table()
+ : QTable( 10, 100, 0, "table" )
+{
+ setSorting( TRUE );
+ horizontalHeader()->setLabel( 0, tr( "File" ) );
+ horizontalHeader()->setLabel( 1, tr( "Size (bytes)" ) );
+ horizontalHeader()->setLabel( 2, tr( "Use in Sum" ) );
+ initTable();
+ adjustColumn( 0 );
+
+ // if the user edited something we might need to recalculate the sum
+ connect( this, SIGNAL( valueChanged( int, int ) ),
+ this, SLOT( recalcSum( int, int ) ) );
+}
+
+void Table::initTable()
+{
+ // read all the Qt source and header files into a list
+ QStringList all;
+ int i = 0;
+ QString srcdir( "../../../src/" );
+ while ( dirs[ i ] ) {
+ QDir dir( srcdir + dirs[ i ] );
+ QStringList lst = dir.entryList( "*.cpp; *.h" );
+ for ( QStringList::Iterator it = lst.begin(); it != lst.end(); ++it ) {
+ if ( ( *it ).contains( "moc" ) )
+ continue;
+ all << (QString( dirs[ i ] ) + "/" + *it);
+ }
+ ++i;
+ }
+
+ // set the number of rows we'll need for the table
+ setNumRows( all.count() + 1 );
+ i = 0;
+ int sum = 0;
+
+ // insert the data into the table
+ for ( QStringList::Iterator it = all.begin(); it != all.end(); ++it ) {
+ setText( i, 0, *it );
+ QFile f( srcdir + *it );
+ setText( i, 1, QString::number( (ulong)f.size() ) );
+ ComboItem *ci = new ComboItem( this, QTableItem::WhenCurrent );
+ setItem( i++, 2, ci );
+ sum += f.size();
+ }
+
+ // last row should show the sum
+ TableItem *i1 = new TableItem( this, QTableItem::Never, tr( "Sum" ) );
+ setItem( i, 0, i1 );
+ TableItem *i2 = new TableItem( this, QTableItem::Never, QString::number( sum ) );
+ setItem( i, 1, i2 );
+}
+
+void Table::recalcSum( int, int col )
+{
+ // only recalc if a value in the second or third column changed
+ if ( col < 1 || col > 2 )
+ return;
+
+ // recalc sum
+ int sum = 0;
+ for ( int i = 0; i < numRows() - 1; ++i ) {
+ if ( text( i, 2 ) == "No" )
+ continue;
+ sum += text( i, 1 ).toInt();
+ }
+
+ // insert calculated data
+ TableItem *i1 = new TableItem( this, QTableItem::Never, tr( "Sum" ) );
+ setItem( numRows() - 1, 0, i1 );
+ TableItem *i2 = new TableItem( this, QTableItem::Never, QString::number( sum ) );
+ setItem( numRows() - 1, 1, i2 );
+}
+
+void Table::sortColumn( int col, bool ascending, bool /*wholeRows*/ )
+{
+ // sum row should not be sorted, so get rid of it for now
+ clearCell( numRows() - 1, 0 );
+ clearCell( numRows() - 1, 1 );
+ // do sort
+ QTable::sortColumn( col, ascending, TRUE );
+ // re-insert sum row
+ recalcSum( 0, 1 );
+}
+
+
+
+void TableItem::paint( QPainter *p, const QColorGroup &cg, const QRect &cr, bool selected )
+{
+ QColorGroup g( cg );
+ // last row is the sum row - we want to make it more visible by
+ // using a red background
+ if ( row() == table()->numRows() - 1 )
+ g.setColor( QColorGroup::Base, red );
+ QTableItem::paint( p, g, cr, selected );
+}
+
+
+
+
+ComboItem::ComboItem( QTable *t, EditType et )
+ : QTableItem( t, et, "Yes" ), cb( 0 )
+{
+ // we do not want this item to be replaced
+ setReplaceable( FALSE );
+}
+
+QWidget *ComboItem::createEditor() const
+{
+ // create an editor - a combobox in our case
+ ( (ComboItem*)this )->cb = new QComboBox( table()->viewport() );
+ QObject::connect( cb, SIGNAL( activated( int ) ), table(), SLOT( doValueChanged() ) );
+ cb->insertItem( "Yes" );
+ cb->insertItem( "No" );
+ // and initialize it
+ cb->setCurrentItem( text() == "No" ? 1 : 0 );
+ return cb;
+}
+
+void ComboItem::setContentFromEditor( QWidget *w )
+{
+ // the user changed the value of the combobox, so synchronize the
+ // value of the item (its text), with the value of the combobox
+ if ( w->inherits( "QComboBox" ) )
+ setText( ( (QComboBox*)w )->currentText() );
+ else
+ QTableItem::setContentFromEditor( w );
+}
+
+void ComboItem::setText( const QString &s )
+{
+ if ( cb ) {
+ // initialize the combobox from the text
+ if ( s == "No" )
+ cb->setCurrentItem( 1 );
+ else
+ cb->setCurrentItem( 0 );
+ }
+ QTableItem::setText( s );
+}
diff --git a/examples/table/statistics/statistics.doc b/examples/table/statistics/statistics.doc
new file mode 100644
index 0000000..4cd47d8
--- /dev/null
+++ b/examples/table/statistics/statistics.doc
@@ -0,0 +1,27 @@
+/*! \page statistics-example.html
+
+ \ingroup table-examples
+ \title Table Example
+
+ Another QTable example.
+
+ <hr>
+
+ Header file:
+
+ \include table/statistics/statistics.h
+
+ <hr>
+
+ Implementation:
+
+ \include table/statistics/statistics.cpp
+
+ <hr>
+
+ Main:
+
+ \include table/statistics/main.cpp
+*/
+
+
diff --git a/examples/table/statistics/statistics.h b/examples/table/statistics/statistics.h
new file mode 100644
index 0000000..b62946d
--- /dev/null
+++ b/examples/table/statistics/statistics.h
@@ -0,0 +1,52 @@
+/****************************************************************************
+**
+** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved.
+**
+** This file is part of an example program for Qt. This example
+** program may be used, distributed and modified without limitation.
+**
+*****************************************************************************/
+
+#ifndef STATISTICS_H
+#define STATISTICS_H
+
+#include <qtable.h>
+#include <qcombobox.h>
+
+class TableItem : public QTableItem
+{
+public:
+ TableItem( QTable *t, EditType et, const QString &txt ) : QTableItem( t, et, txt ) {}
+ void paint( QPainter *p, const QColorGroup &cg, const QRect &cr, bool selected );
+};
+
+class ComboItem : public QTableItem
+{
+public:
+ ComboItem( QTable *t, EditType et );
+ QWidget *createEditor() const;
+ void setContentFromEditor( QWidget *w );
+ void setText( const QString &s );
+
+private:
+ QComboBox *cb;
+
+};
+
+class Table : public QTable
+{
+ Q_OBJECT
+
+public:
+ Table();
+ void sortColumn( int col, bool ascending, bool wholeRows );
+
+private slots:
+ void recalcSum( int row, int col );
+
+private:
+ void initTable();
+
+};
+
+#endif
diff --git a/examples/table/statistics/statistics.pro b/examples/table/statistics/statistics.pro
new file mode 100644
index 0000000..d172d52
--- /dev/null
+++ b/examples/table/statistics/statistics.pro
@@ -0,0 +1,10 @@
+TEMPLATE = app
+TARGET = statistics
+
+CONFIG += qt warn_on release
+DEPENDPATH = ../../include
+
+REQUIRES = table full-config
+
+HEADERS = statistics.h
+SOURCES = statistics.cpp main.cpp