diff --git a/src/QBarChart/QBarChart.pro b/src/QBarChart/QBarChart.pro new file mode 100644 index 0000000..6b49c66 --- /dev/null +++ b/src/QBarChart/QBarChart.pro @@ -0,0 +1,30 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2012-01-11T15:40:15 +# +#------------------------------------------------- + +QT += core gui + +TARGET = QBarChart +TEMPLATE = app + + +SOURCES += main.cpp\ + widget.cpp \ + qbarchart.cpp \ + qbarchartcontrol.cpp \ + qbarchartmodel.cpp \ + qbarchartbar.cpp \ + qbarchartview.cpp \ + qbarchartgrid.cpp \ + qchartview.cpp + +HEADERS += widget.h \ + qbarchart.h \ + qbarchartcontrol.h \ + qbarchartmodel.h \ + qbarchartbar.h \ + qbarchartview.h \ + qbarchartgrid.h \ + qchartview.h diff --git a/src/QBarChart/main.cpp b/src/QBarChart/main.cpp new file mode 100644 index 0000000..c0d5f93 --- /dev/null +++ b/src/QBarChart/main.cpp @@ -0,0 +1,39 @@ +#include +#include +//QGraphicsScene *scene, QWidget *parent = 0#include +//#include "widget.h" +//#include "qbarchart.h" +#include "qchartview.h" + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + + QGraphicsScene scene; + //QGraphicsView view(&scene); + QChartView view(&scene); +/* + QBarChart *barChart = new QBarChart(); + barChart->setSize(640,480); + scene.addItem(barChart); + + QList data; + data << 1; + data << 1; + data << 2; + data << 3; + data << 5; + data << 8; + data << 13; + data << 21; + data << 42; + barChart->addSeries(data); +*/ + view.resize(640, 480); + QPalette p(view.palette()); + p.setColor(QPalette::Base, QColor(0,0,0)); + view.setPalette(p); + view.show(); + + return a.exec(); +} diff --git a/src/QBarChart/qbarchart.cpp b/src/QBarChart/qbarchart.cpp new file mode 100644 index 0000000..bfa3b29 --- /dev/null +++ b/src/QBarChart/qbarchart.cpp @@ -0,0 +1,92 @@ +#include "qbarchart.h" +#include "qbarchartbar.h" +#include + +QBarChart::QBarChart(QGraphicsItem *parent) : + QGraphicsItem(parent) + ,mBarDefaultWidth( 1 ) +{ +} + +void QBarChart::addSeries(QList data) +{ + mData.clear(); + mData.append(data); + dataChanged(); +} + +void QBarChart::setSize( int h, int w ) +{ + mHeight = h; + mWidth = w; + dataChanged(); // TODO: separate data from grid +} + +void QBarChart::setBarWidth( int w ) +{ + mBarDefaultWidth = w; +} + +void QBarChart::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) +{ + qDebug() << "QBarChart::paint"; +} + +QRectF QBarChart::boundingRect() const +{ + return QRectF(0,0,mWidth,mHeight); // TODO: size from layout +} + + +void QBarChart::dataChanged() +{ + int count = mData.count(); + + if (count <= 0) { + // No items + return; + } + + mMax = mData.at(0); + mMin = mData.at(0); + + // Find out min and max values. + for (int i=0; i mMax) { + mMax = mData.at(i); + } else if (mData.at(i) < mMin) { + mMin = mData.at(i); + } + } + + // Create items for data, delete old ones + foreach (QGraphicsItem* i, childItems()) { + delete i; + } + + gridChanged(); + + int posStep = (mWidth / count) / 2; // TODO: Why I need magical 2 here to get correct step? + + for (int i=0; isetSize(barHeight, mBarDefaultWidth * 3 * i); // TODO: widht settable by style or something. + bar->setColor(QColor(255 - i*25, i*10, i*15)); + bar->setPos(i*posStep, 0); + childItems().append(bar); + } +} + +void QBarChart::gridChanged() +{ + // Scale of grid has changed + QBarChartGrid *grid = new QBarChartGrid(this); + grid->setSize(mHeight, mWidth); + grid->setLimits(mMin,mMax); + grid->setHorizontalLineCount(5); + childItems().append(grid); +} + diff --git a/src/QBarChart/qbarchart.h b/src/QBarChart/qbarchart.h new file mode 100644 index 0000000..092bc9f --- /dev/null +++ b/src/QBarChart/qbarchart.h @@ -0,0 +1,56 @@ +#ifndef QBARCHART_H +#define QBARCHART_H + +#include + +#include "qbarchartbar.h" +#include "qbarchartcontrol.h" +#include "qbarchartview.h" +#include "qbarchartgrid.h" + +class QBarChart : public QGraphicsItem +{ + +public: + explicit QBarChart(QGraphicsItem *parent = 0); + + // Data "api" + void addSeries(QList data); // TODO: maybe some better structure + + // Layout "api" + void setSize( int h, int w ); + void setPos(qreal x, qreal y); + void setBarWidth( int w ); + + // From QGraphicsItem + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); + QRectF boundingRect() const; + +private: + + void dataChanged(); + void gridChanged(); + +private: + + // Data + QList mData; // use ints for now.. + int mMin; // Min and max values of data. (updated when data is changed, used when drawing) + int mMax; + + // View components + QList mItems; // 2 first items in list are x and y axis, in that order + QBarChartGrid *mGrid; + + int mHeight; // Layout spesific + int mWidth; + int mBarDefaultWidth; + + // Controll stuff + QBarChartControl mBarControl; + + + +}; + +#endif // QBARCHART_H diff --git a/src/QBarChart/qbarchartbar.cpp b/src/QBarChart/qbarchartbar.cpp new file mode 100644 index 0000000..5336667 --- /dev/null +++ b/src/QBarChart/qbarchartbar.cpp @@ -0,0 +1,39 @@ +#include "qbarchartbar.h" +#include +#include + +QBarChartBar::QBarChartBar(QGraphicsItem *parent) + : QGraphicsItem(parent) +{ +} + +void QBarChartBar::setSize( int h, int w ) +{ + mHeight = h; + mWidth = w; +} + +void QBarChartBar::setColor( QColor col ) +{ + mColor = col; +} + +void QBarChartBar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) +{ + qDebug() << "QBarChartBar::paint" << scenePos(); + // Set color for bar. TODO: gradients, textures etc + QPen pen = painter->pen(); + pen.setColor( mColor ); + pen.setWidth( mWidth ); + painter->setPen(pen); + + // Draw bar + painter->drawLine(scenePos().x(), scenePos().y() + parentItem()->boundingRect().height(), + scenePos().x(), scenePos().y() + parentItem()->boundingRect().height() - mHeight ); +} + +QRectF QBarChartBar::boundingRect() const +{ + QRectF r(scenePos().x(), scenePos().y(), scenePos().x() + mWidth, scenePos().y() + mHeight ); + return r; +} diff --git a/src/QBarChart/qbarchartbar.h b/src/QBarChart/qbarchartbar.h new file mode 100644 index 0000000..7d75eaa --- /dev/null +++ b/src/QBarChart/qbarchartbar.h @@ -0,0 +1,31 @@ +#ifndef QBARCHARTBAR_H +#define QBARCHARTBAR_H + +#include + +// Single bar item of chart + +class QBarChartBar : public QGraphicsItem +{ +public: + QBarChartBar(QGraphicsItem *parent=0); + + // Layout Stuff + void setSize( int h, int w ); // Size of bar. in screen coordinates. + void setColor( QColor col ); // Color of bar + +public: + // From QGraphicsItem + + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); + QRectF boundingRect() const; + +private: + + int mHeight; + int mWidth; + QPointF mPos; + QColor mColor; +}; + +#endif // QBARCHARTBAR_H diff --git a/src/QBarChart/qbarchartcontrol.cpp b/src/QBarChart/qbarchartcontrol.cpp new file mode 100644 index 0000000..b110f74 --- /dev/null +++ b/src/QBarChart/qbarchartcontrol.cpp @@ -0,0 +1,6 @@ +#include "qbarchartcontrol.h" + +QBarChartControl::QBarChartControl(QObject *parent) : + QObject(parent) +{ +} diff --git a/src/QBarChart/qbarchartcontrol.h b/src/QBarChart/qbarchartcontrol.h new file mode 100644 index 0000000..ea7ca8a --- /dev/null +++ b/src/QBarChart/qbarchartcontrol.h @@ -0,0 +1,18 @@ +#ifndef QBARCHARTCONTROL_H +#define QBARCHARTCONTROL_H + +#include + +class QBarChartControl : public QObject +{ + Q_OBJECT +public: + explicit QBarChartControl(QObject *parent = 0); + +signals: + +public slots: + +}; + +#endif // QBARCHARTCONTROL_H diff --git a/src/QBarChart/qbarchartgrid.cpp b/src/QBarChart/qbarchartgrid.cpp new file mode 100644 index 0000000..6be2801 --- /dev/null +++ b/src/QBarChart/qbarchartgrid.cpp @@ -0,0 +1,52 @@ +#include "qbarchartgrid.h" +#include +#include + +QBarChartGrid::QBarChartGrid(QGraphicsItem *parent) : + QGraphicsItem(parent) +{ +} + + +void QBarChartGrid::setLimits( int min, int max ) +{ + mMin = min; + mMax = max; +} + +void QBarChartGrid::setHorizontalLineCount(int count) +{ + mHorizontalLineCount = count; +} + +void QBarChartGrid::setSize( int h, int w ) +{ + mHeight = h; + mWidth = w; +} + +void QBarChartGrid::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) +{ + qDebug() << "QBarChartGrid::paint w =" << mWidth << "h =" << mHeight; + + QPen p = painter->pen(); + p.setColor( QColor(0,255,0) ); + p.setWidth( 1 ); + painter->setPen(p); + + // Draw horizontal lines + if ( mHorizontalLineCount >0 ) { + int step = mHeight / mHorizontalLineCount; + if (step > 0) { + for (int y=0; ydrawLine(0,y,mWidth,y); + } + } + } +} + +QRectF QBarChartGrid::boundingRect() const +{ + QRectF r(scenePos().x(), scenePos().y(), scenePos().x() + mWidth, scenePos().y() + mHeight ); + return r; +} diff --git a/src/QBarChart/qbarchartgrid.h b/src/QBarChart/qbarchartgrid.h new file mode 100644 index 0000000..29becc2 --- /dev/null +++ b/src/QBarChart/qbarchartgrid.h @@ -0,0 +1,33 @@ +#ifndef QBARCHARTGRID_H +#define QBARCHARTGRID_H + +#include + +class QBarChartGrid : public QGraphicsItem +{ + +public: + QBarChartGrid(QGraphicsItem *parent = 0); + + // Data api: + void setLimits( int min, int max ); + void setHorizontalLineCount( int count ); + + // Layout api: + void setSize( int h, int w ); + + // From QGraphicsItem + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); + QRectF boundingRect() const; + +private: + + int mWidth; + int mHeight; + + int mMin; + int mMax; + int mHorizontalLineCount; // Draw this many horizontal lines to grid +}; + +#endif // QBARCHARTGRID_H diff --git a/src/QBarChart/qbarchartmodel.cpp b/src/QBarChart/qbarchartmodel.cpp new file mode 100644 index 0000000..5f72e83 --- /dev/null +++ b/src/QBarChart/qbarchartmodel.cpp @@ -0,0 +1,6 @@ +#include "qbarchartmodel.h" + +QBarChartModel::QBarChartModel(QObject *parent) : + QAbstractItemModel(parent) +{ +} diff --git a/src/QBarChart/qbarchartmodel.h b/src/QBarChart/qbarchartmodel.h new file mode 100644 index 0000000..ff86ca5 --- /dev/null +++ b/src/QBarChart/qbarchartmodel.h @@ -0,0 +1,18 @@ +#ifndef QBARCHARTMODEL_H +#define QBARCHARTMODEL_H + +#include + +class QBarChartModel : public QAbstractItemModel +{ + Q_OBJECT +public: + explicit QBarChartModel(QObject *parent = 0); + +signals: + +public slots: + +}; + +#endif // QBARCHARTMODEL_H diff --git a/src/QBarChart/qbarchartview.cpp b/src/QBarChart/qbarchartview.cpp new file mode 100644 index 0000000..5e7c247 --- /dev/null +++ b/src/QBarChart/qbarchartview.cpp @@ -0,0 +1,5 @@ +#include "qbarchartview.h" + +QBarChartView::QBarChartView() +{ +} diff --git a/src/QBarChart/qbarchartview.h b/src/QBarChart/qbarchartview.h new file mode 100644 index 0000000..9b4b2e6 --- /dev/null +++ b/src/QBarChart/qbarchartview.h @@ -0,0 +1,10 @@ +#ifndef QBARCHARTVIEW_H +#define QBARCHARTVIEW_H + +class QBarChartView +{ +public: + QBarChartView(); +}; + +#endif // QBARCHARTVIEW_H diff --git a/src/QBarChart/qbarchartxaxis.cpp b/src/QBarChart/qbarchartxaxis.cpp new file mode 100644 index 0000000..f9ac174 --- /dev/null +++ b/src/QBarChart/qbarchartxaxis.cpp @@ -0,0 +1,18 @@ +#include "qbarchartxaxis.h" + +QBarChartXAxis::QBarChartXAxis(QGraphicsItem *parent) : + QGraphicsItem(parent) +{ +} + + +void QBarChartXAxis::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) +{ + +} + +QRectF QBarChartXAxis::boundingRect() const +{ + QRectF r(scenePos().x(), scenePos().y(), scenePos().x() + mWidth, scenePos().y() + mHeight ); + return r; +} diff --git a/src/QBarChart/qbarchartxaxis.h b/src/QBarChart/qbarchartxaxis.h new file mode 100644 index 0000000..07d50f5 --- /dev/null +++ b/src/QBarChart/qbarchartxaxis.h @@ -0,0 +1,22 @@ +#ifndef QBARCHARTXAXIS_H +#define QBARCHARTXAXIS_H + +#include + +class QBarChartXAxis : public QGraphicsItem +{ + +public: + QBarChartXAxis(QGraphicsItem *parent = 0); + + // From QGraphicsItem + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); + QRectF boundingRect() const; + +private: + + int mWidth; + int mHeight; +}; + +#endif // QBARCHARTXAXIS_H diff --git a/src/QBarChart/qchartview.cpp b/src/QBarChart/qchartview.cpp new file mode 100644 index 0000000..4019cf2 --- /dev/null +++ b/src/QBarChart/qchartview.cpp @@ -0,0 +1,30 @@ +#include "qchartview.h" +#include "qbarchart.h" +#include + +QChartView::QChartView(QGraphicsScene *scene, QWidget *parent ) : + QGraphicsView(scene,parent) +{ + mChart = new QBarChart(); + mChart->setSize(640,480); + scene->addItem(mChart); + + QList data; + data << 1; + data << 1; + data << 2; + data << 3; + data << 5; + data << 8; + data << 13; + data << 21; + data << 42; + mChart->addSeries(data); + +} + + +void QChartView::resizeEvent(QResizeEvent *event) +{ + mChart->setSize(event->size().height(), event->size().width()); +} diff --git a/src/QBarChart/qchartview.h b/src/QBarChart/qchartview.h new file mode 100644 index 0000000..624fc0c --- /dev/null +++ b/src/QBarChart/qchartview.h @@ -0,0 +1,25 @@ +#ifndef QCHARTVIEW_H +#define QCHARTVIEW_H + +#include +#include "qbarchart.h" + +class QChartView : public QGraphicsView +{ + Q_OBJECT +public: + explicit QChartView(QGraphicsScene *scene, QWidget *parent = 0); + +signals: + +public slots: + +protected: + void resizeEvent(QResizeEvent *event); + +private: + + QBarChart *mChart; +}; + +#endif // QCHARTVIEW_H diff --git a/src/QBarChart/widget.cpp b/src/QBarChart/widget.cpp new file mode 100644 index 0000000..d594626 --- /dev/null +++ b/src/QBarChart/widget.cpp @@ -0,0 +1,11 @@ +#include "widget.h" + +Widget::Widget(QWidget *parent) + : QWidget(parent) +{ +} + +Widget::~Widget() +{ + +} diff --git a/src/QBarChart/widget.h b/src/QBarChart/widget.h new file mode 100644 index 0000000..41c3a9d --- /dev/null +++ b/src/QBarChart/widget.h @@ -0,0 +1,15 @@ +#ifndef WIDGET_H +#define WIDGET_H + +#include + +class Widget : public QWidget +{ + Q_OBJECT + +public: + Widget(QWidget *parent = 0); + ~Widget(); +}; + +#endif // WIDGET_H