File indexing completed on 2024-05-12 04:20:13
0001 /** 0002 * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved. 0003 * 0004 * This file is part of the KD Chart library. 0005 * 0006 * SPDX-License-Identifier: GPL-2.0-or-later 0007 */ 0008 0009 #include <QAbstractProxyModel> 0010 0011 #include <QApplication> 0012 0013 #include <QSqlTableModel> 0014 #include <QSqlQuery> 0015 #include <QSqlError> 0016 #include <QSqlRecord> 0017 #include <QSqlField> 0018 #include <KChartChart> 0019 #include <KChartBarDiagram> 0020 #include <KChartHeaderFooter> 0021 #include <KChartPosition> 0022 #include <KChartBackgroundAttributes> 0023 #include <KChartFrameAttributes> 0024 0025 #include <QPixmap> 0026 0027 using namespace KChart; 0028 0029 /** 0030 * Proxymodel that transposes columns and rows. 0031 */ 0032 class TransposeProxyModel : public QAbstractProxyModel{ 0033 public: 0034 explicit TransposeProxyModel(QObject* parent = nullptr) : QAbstractProxyModel(parent) {} 0035 ~TransposeProxyModel() override {} 0036 QModelIndex mapFromSource ( const QModelIndex & sourceIndex ) const override { return index(sourceIndex.column(), sourceIndex.row()); } 0037 QModelIndex mapToSource ( const QModelIndex & proxyIndex ) const override { return sourceModel()->index(proxyIndex.column(), proxyIndex.row()); } 0038 QModelIndex index(int r, int c, const QModelIndex &ind=QModelIndex()) const override { Q_UNUSED(ind) return createIndex(r,c); } 0039 QModelIndex parent(const QModelIndex&) const override { return QModelIndex(); } 0040 int rowCount(const QModelIndex &) const override { return sourceModel()->columnCount(); } 0041 int columnCount(const QModelIndex &) const override { return sourceModel()->rowCount(); } 0042 QVariant data(const QModelIndex &ind, int role) const override { return sourceModel()->data(mapToSource(ind), role); } 0043 }; 0044 0045 /** 0046 * The example that creates the SQL-model, adds data to it and display the data in a model. 0047 */ 0048 class ChartWidget : public QWidget { 0049 Q_OBJECT 0050 public: 0051 explicit ChartWidget(QWidget* parent = nullptr) 0052 : QWidget(parent) 0053 , m_model(nullptr) 0054 { 0055 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); 0056 db.setHostName("localhost"); 0057 db.setDatabaseName(":memory:"); // in memory rather then in a file 0058 //db.setUserName(""); 0059 //db.setPassword(""); 0060 bool ok = db.open(); 0061 Q_ASSERT(ok); 0062 Q_UNUSED(ok) // release mode 0063 0064 QSqlQuery createTableQuery = db.exec("CREATE TABLE IF NOT EXISTS MyTable (col1 INT NOT NULL PRIMARY KEY, col2 INT);"); 0065 Q_ASSERT(!createTableQuery.lastError().isValid()); 0066 0067 m_model = new QSqlTableModel(this, db); 0068 m_model->setTable("MyTable"); 0069 m_model->setEditStrategy(QSqlTableModel::OnRowChange); 0070 m_model->setSort(0, Qt::AscendingOrder); 0071 0072 ok = m_model->select(); 0073 Q_ASSERT(ok); 0074 0075 m_model->setHeaderData(0, Qt::Horizontal, tr("Column 1")); 0076 m_model->setHeaderData(1, Qt::Horizontal, tr("Column 2")); 0077 0078 for (int row = 0; row < 3; ++row) { 0079 QSqlRecord rec; 0080 for (int column = 0; column < 2; ++column) { 0081 QSqlField field(column==0?"col1":"col2",QVariant::Int); 0082 field.setValue(row+1 * column); 0083 rec.append(field); 0084 } 0085 ok = m_model->insertRecord(-1, rec); 0086 Q_ASSERT(ok); 0087 } 0088 0089 BarDiagram* diagram = new BarDiagram; 0090 0091 TransposeProxyModel* proxymodel = new TransposeProxyModel(this); 0092 proxymodel->setSourceModel(m_model); 0093 diagram->setModel(proxymodel); 0094 0095 m_chart.coordinatePlane()->replaceDiagram(diagram); 0096 0097 // Add at one Header and set it up 0098 HeaderFooter* header = new HeaderFooter( &m_chart ); 0099 header->setPosition( Position::North ); 0100 header->setText( "A Simple Bar Chart" ); 0101 m_chart.addHeaderFooter( header ); 0102 0103 // Configure the plane Frame attributes 0104 FrameAttributes pfa; 0105 pfa.setPen( QPen ( QBrush( Qt::blue ), 2 ) ); 0106 pfa.setVisible( true ); 0107 diagram->coordinatePlane()->setFrameAttributes( pfa ); 0108 0109 // Configure the header Frame attributes 0110 FrameAttributes hfa; 0111 hfa.setPen( QPen ( QBrush( Qt::darkGray ), 2 ) ); 0112 hfa.setPadding( 2 ); 0113 hfa.setVisible( true ); 0114 header->setFrameAttributes( hfa ); 0115 0116 QVBoxLayout* l = new QVBoxLayout(this); 0117 l->addWidget(&m_chart); 0118 setLayout(l); 0119 } 0120 0121 private: 0122 Chart m_chart; 0123 QSqlTableModel *m_model; 0124 }; 0125 0126 int main( int argc, char** argv ) { 0127 QApplication app( argc, argv ); 0128 0129 ChartWidget w; 0130 w.show(); 0131 0132 return app.exec(); 0133 } 0134 0135 #include "main.moc"