diff --git a/charts.pro b/charts.pro new file mode 100644 index 0000000..b2ae450 --- /dev/null +++ b/charts.pro @@ -0,0 +1,7 @@ +TEMPLATE = subdirs +SUBDIRS += widget qmlplugin + +# install feature file +feature.path = $$[QT_INSTALL_DATA]/mkspecs/features +feature.files = $$PWD/features/charts.prf +INSTALLS += feature diff --git a/features/charts.prf b/features/charts.prf new file mode 100644 index 0000000..c4b90e9 --- /dev/null +++ b/features/charts.prf @@ -0,0 +1,5 @@ +INCLUDEPATH += $$[QT_INSTALL_HEADERS]/Charts + +contains(CHARTS, widget) { + LIBS += -lChartWidget +} diff --git a/qmlplugin/declarativechart.cpp b/qmlplugin/declarativechart.cpp new file mode 100644 index 0000000..41d471c --- /dev/null +++ b/qmlplugin/declarativechart.cpp @@ -0,0 +1,27 @@ +#include "declarativechart.h" +#include + +#if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0)) +DeclarativeChart::DeclarativeChart(QDeclarativeItem *parent) + : QDeclarativeItem(parent) +{ + // need to disable this flag to draw inside a QDeclarativeItem + setFlag(QGraphicsItem::ItemHasNoContents, false); +} +#else +DeclarativeChart::DeclarativeChart(QQuickItem *parent) + : QQuickPaintedItem(parent) +{ + +} +#endif + +#if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0)) +void DeclarativeChart::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) +#else +void DeclarativeChart::paint(QPainter *painter) +#endif +{ + drawChart(painter, boundingRect()); +} + diff --git a/qmlplugin/declarativechart.h b/qmlplugin/declarativechart.h new file mode 100644 index 0000000..e21eba5 --- /dev/null +++ b/qmlplugin/declarativechart.h @@ -0,0 +1,37 @@ +#ifndef DECLARATIVECHART_H +#define DECLARATIVECHART_H + +#include +#if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0)) + #include +#else + #include +#endif +#include +#include "../src/chart.h" + +#if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0)) +class DeclarativeChart : public QDeclarativeItem, public Chart +#else +class DeclarativeChart : public QQuickPaintedItem, public Chart +#endif +{ + Q_OBJECT + Q_PROPERTY(QColor color READ color WRITE setColor) + +public: +#if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0)) + DeclarativeChart(QDeclarativeItem *parent = 0); +#else + DeclarativeChart(QQuickItem *parent = 0); +#endif + +#if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0)) + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); +#else + void paint(QPainter *painter); +#endif +}; + + +#endif // DECLARATIVECHART_H diff --git a/qmlplugin/plugin.cpp b/qmlplugin/plugin.cpp new file mode 100644 index 0000000..1b8f64e --- /dev/null +++ b/qmlplugin/plugin.cpp @@ -0,0 +1,22 @@ +#include +#include +#include "declarativechart.h" + +QT_BEGIN_NAMESPACE + +class QmlChartPlugin : public QDeclarativeExtensionPlugin +{ + Q_OBJECT +public: + virtual void registerTypes(const char *uri) + { + Q_ASSERT(QLatin1String(uri) == QLatin1String("com.digia.charts")); + qmlRegisterType(uri, 1, 0, "Chart"); + } +}; + +QT_END_NAMESPACE + +#include "plugin.moc" + +Q_EXPORT_PLUGIN2(qmlchartplugin, QT_PREPEND_NAMESPACE(QmlChartPlugin)); diff --git a/qmlplugin/qml/canvaschart.qml b/qmlplugin/qml/canvaschart.qml new file mode 100644 index 0000000..c83cfb4 --- /dev/null +++ b/qmlplugin/qml/canvaschart.qml @@ -0,0 +1,29 @@ +import QtQuick 2.0 + +Canvas { + id: canvas + width: 200 + height: 200 + smooth:true + renderTarget:Canvas.Image + renderInThread:false + + onWidthChanged: requestPaint() + onHeightChanged: requestPaint() + + onPaint: { + var ctx = canvas.getContext('2d') + ctx.reset() + ctx.clearRect(0, 0, canvas.width, canvas.height) + ctx.strokeStyle = "red" + ctx.lineWidth = 1 + ctx.moveTo(10, 10) + ctx.lineTo(10, canvas.height-10) + ctx.lineTo(canvas.width-10, canvas.height-10) + ctx.text("Axis x", canvas.width / 2, canvas.height) + ctx.text("Insert chart here", canvas.width / 2, canvas.height / 2) + ctx.stroke(); + } +} + + diff --git a/qmlplugin/qml/canvaschart.qmlproject b/qmlplugin/qml/canvaschart.qmlproject new file mode 100644 index 0000000..d4909f8 --- /dev/null +++ b/qmlplugin/qml/canvaschart.qmlproject @@ -0,0 +1,16 @@ +import QmlProject 1.0 + +Project { + /* Include .qml, .js, and image files from current directory and subdirectories */ + QmlFiles { + directory: "." + } + JavaScriptFiles { + directory: "." + } + ImageFiles { + directory: "." + } + /* List of plugin directories passed to QML runtime */ + // importPaths: [ " ../exampleplugin " ] +} diff --git a/qmlplugin/qmldir b/qmlplugin/qmldir new file mode 100644 index 0000000..0d373d6 --- /dev/null +++ b/qmlplugin/qmldir @@ -0,0 +1 @@ +plugin qmlchartplugin diff --git a/qmlplugin/qmlplugin.pro b/qmlplugin/qmlplugin.pro new file mode 100644 index 0000000..58208e2 --- /dev/null +++ b/qmlplugin/qmlplugin.pro @@ -0,0 +1,26 @@ +TEMPLATE = lib +TARGET = qmlchartplugin + +CONFIG += qt plugin +QT += declarative + +OBJECTS_DIR = tmp +MOC_DIR = tmp + +SOURCES += \ + plugin.cpp \ + declarativechart.cpp +HEADERS += \ + declarativechart.h +include(../src/chart.pri) + +TARGETPATH = com/digia/charts + +#DESTDIR = $$[QT_INSTALL_IMPORTS]/$$TARGETPATH +target.path = $$[QT_INSTALL_IMPORTS]/$$TARGETPATH + +qmldir.files += $$PWD/qmldir +qmldir.path += $$[QT_INSTALL_IMPORTS]/$$TARGETPATH + +INSTALLS += target qmldir + diff --git a/src/chart.cpp b/src/chart.cpp new file mode 100644 index 0000000..83ff0ad --- /dev/null +++ b/src/chart.cpp @@ -0,0 +1,46 @@ +#include "chart.h" + +Chart::Chart() +{ + +} + +Chart::~Chart() +{ + +} + +QColor Chart::color() const +{ + return m_color; +} + +void Chart::setColor(const QColor &color) +{ + m_color = color; +} + +void Chart::drawChart(QPainter *painter, const QRectF& drawRect) +{ + QRectF r = drawRect; + r.adjust(10, 10, -10, -10); + + QPen pen(m_color, 2); + pen.setCapStyle(Qt::RoundCap); + painter->setPen(pen); + painter->setRenderHints(QPainter::Antialiasing, true); + painter->drawLine(r.topLeft(), r.bottomLeft()); + painter->drawLine(r.bottomLeft(), r.bottomRight()); + + painter->drawText(drawRect, "Axis x", QTextOption(Qt::AlignBottom | Qt::AlignHCenter)); + QTransform transform; + transform.translate(drawRect.width(), 0); // works badly if drawrect width != height :) + transform.rotate(90); + painter->setTransform(transform); + painter->drawText(drawRect, "Axis y", QTextOption(Qt::AlignBottom | Qt::AlignHCenter)); + painter->resetTransform(); + + painter->drawText(drawRect, QT_VERSION_STR, QTextOption(Qt::AlignTop | Qt::AlignRight)); + painter->drawText(drawRect, "Insert chart here", QTextOption(Qt::AlignCenter)); + +} diff --git a/src/chart.h b/src/chart.h new file mode 100644 index 0000000..7855831 --- /dev/null +++ b/src/chart.h @@ -0,0 +1,24 @@ +#ifndef CHART_H +#define CHART_H +#include +#include +#include + +class QPainter; + +class Chart +{ +public: + Chart(); + virtual ~Chart(); + + QColor color() const; + void setColor(const QColor &color); + + void drawChart(QPainter *painter, const QRectF& drawRect); + +private: + QColor m_color; +}; + +#endif diff --git a/src/chart.pri b/src/chart.pri new file mode 100644 index 0000000..a0267e2 --- /dev/null +++ b/src/chart.pri @@ -0,0 +1,2 @@ +HEADERS += $$PWD/chart.h +SOURCES += $$PWD/chart.cpp diff --git a/test/chartwidgettest/chartwidgettest.pro b/test/chartwidgettest/chartwidgettest.pro new file mode 100644 index 0000000..7e87b0d --- /dev/null +++ b/test/chartwidgettest/chartwidgettest.pro @@ -0,0 +1,16 @@ +TARGET = chartwidgettest +TEMPLATE = app + +QT += core gui +contains(QT_MAJOR_VERSION, 5) { + QT += widgets +} + +CONFIG += charts +CHARTS += widget + +OBJECTS_DIR = tmp +MOC_DIR = tmp + +SOURCES += main.cpp + diff --git a/test/chartwidgettest/main.cpp b/test/chartwidgettest/main.cpp new file mode 100644 index 0000000..8c9e9a8 --- /dev/null +++ b/test/chartwidgettest/main.cpp @@ -0,0 +1,19 @@ +#include +#if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0)) +#include +#else +#include +#endif +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + + ChartWidget w; + w.resize(QSize(200,200)); + w.setColor(Qt::red); + w.show(); + + return a.exec(); +} diff --git a/test/qmlcharttest/charttest.qml b/test/qmlcharttest/charttest.qml new file mode 100644 index 0000000..c4a9672 --- /dev/null +++ b/test/qmlcharttest/charttest.qml @@ -0,0 +1,11 @@ +//import QtQuick 1.1 +import QtQuick 2.0 +import com.digia.charts 1.0 + +Chart { + width: 200 + height: 200 + color: "red" +} + + diff --git a/widget/chartwidget.cpp b/widget/chartwidget.cpp new file mode 100644 index 0000000..54f68cd --- /dev/null +++ b/widget/chartwidget.cpp @@ -0,0 +1,38 @@ +#include "chartwidget.h" +#include "../src/chart.h" +#include + +class ChartWidgetPrivate : public Chart +{ +public: + //Q_DECLARE_PUBLIC(ChartWidget) + ChartWidgetPrivate() {} +}; + +ChartWidget::ChartWidget(QWidget *parent) + : QWidget(parent) +{ + d_ptr = new ChartWidgetPrivate(); +} + +ChartWidget::~ChartWidget() +{ + delete d_ptr; + d_ptr = 0; +} + +QColor ChartWidget::color() const +{ + return d_ptr->color(); +} + +void ChartWidget::setColor(const QColor &color) +{ + d_ptr->setColor(color); +} + +void ChartWidget::paintEvent(QPaintEvent *) +{ + QPainter painter(this); + d_ptr->drawChart(&painter, rect()); +} diff --git a/widget/chartwidget.h b/widget/chartwidget.h new file mode 100644 index 0000000..3ecd186 --- /dev/null +++ b/widget/chartwidget.h @@ -0,0 +1,40 @@ +#ifndef CHARTWIDGET_H +#define CHARTWIDGET_H + +#include +#if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0)) + #include +#else + #include +#endif + +#if defined(CHARTWIDGET_LIBRARY) +# define CHARTWIDGET_EXPORT Q_DECL_EXPORT +#else +# define CHARTWIDGET_EXPORT Q_DECL_IMPORT +#endif + +class ChartWidgetPrivate; + +class CHARTWIDGET_EXPORT ChartWidget : public QWidget +{ + Q_OBJECT + Q_PROPERTY(QColor color READ color WRITE setColor) + +public: + explicit ChartWidget(QWidget *parent = 0); + ~ChartWidget(); + + QColor color() const; + void setColor(const QColor &color); + +protected: + virtual void paintEvent(QPaintEvent *); + +private: + Q_DISABLE_COPY(ChartWidget) + Q_DECLARE_PRIVATE(ChartWidget) + ChartWidgetPrivate* d_ptr; +}; + +#endif diff --git a/widget/widget.pro b/widget/widget.pro new file mode 100644 index 0000000..81f4125 --- /dev/null +++ b/widget/widget.pro @@ -0,0 +1,25 @@ +TARGET = ChartWidget +TEMPLATE = lib + +QT += core gui +contains(QT_MAJOR_VERSION, 5) { + QT += widgets +} + +SOURCES += \ + chartwidget.cpp +HEADERS += \ + chartwidget.h +include(../src/chart.pri) + +OBJECTS_DIR = tmp +MOC_DIR = tmp + +DEFINES += CHARTWIDGET_LIBRARY + +public_headers.path = $$[QT_INSTALL_HEADERS]/Charts +public_headers.files = chartwidget.h + +target.path=$$[QT_INSTALL_LIBS] + +INSTALLS += target public_headers