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