Skip to content

Commit

Permalink
Initial port
Browse files Browse the repository at this point in the history
  • Loading branch information
eligijuspfods committed Aug 2, 2017
1 parent 06f91ca commit ecb12a6
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 40 deletions.
87 changes: 55 additions & 32 deletions src/core.cpp
Expand Up @@ -343,8 +343,8 @@
/*!
Constructs a QCustomPlot and sets reasonable default values.
*/
QCustomPlot::QCustomPlot(QWidget *parent) :
QWidget(parent),
QCustomPlot::QCustomPlot(QQuickItem *parent) :
QQuickPaintedItem(parent),
xAxis(0),
yAxis(0),
xAxis2(0),
Expand All @@ -367,6 +367,8 @@ QCustomPlot::QCustomPlot(QWidget *parent) :
mSelectionRectMode(QCP::srmNone),
mSelectionRect(0),
mOpenGl(false),
mLocale(QLocale::system()),
mFont(QGuiApplication::font()),
mMouseHasMoved(false),
mMouseEventLayerable(0),
mReplotting(false),
Expand All @@ -375,17 +377,13 @@ QCustomPlot::QCustomPlot(QWidget *parent) :
mOpenGlAntialiasedElementsBackup(QCP::aeNone),
mOpenGlCacheLabelsBackup(true)
{
setAttribute(Qt::WA_NoMousePropagation);
setAttribute(Qt::WA_OpaquePaintEvent);
setFocusPolicy(Qt::ClickFocus);
setMouseTracking(true);
QLocale currentLocale = locale();
currentLocale.setNumberOptions(QLocale::OmitGroupSeparator);
setLocale(currentLocale);
#ifdef QCP_DEVICEPIXELRATIO_SUPPORTED
setBufferDevicePixelRatio(QWidget::devicePixelRatio());
setBufferDevicePixelRatio(devicePixelRatio());
#endif

mOpenGlAntialiasedElementsBackup = mAntialiasedElements;
mOpenGlCacheLabelsBackup = mPlottingHints.testFlag(QCP::phCacheLabels);
// create initial layers:
Expand Down Expand Up @@ -429,7 +427,7 @@ QCustomPlot::QCustomPlot(QWidget *parent) :
// create selection rect instance:
mSelectionRect = new QCPSelectionRect(this);
mSelectionRect->setLayer(QLatin1String("overlay"));

setViewport(rect()); // needs to be called after mPlotLayout has been created

replot(rpQueuedReplot);
Expand Down Expand Up @@ -859,7 +857,13 @@ void QCustomPlot::setOpenGl(bool enabled, int multisampling)
*/
void QCustomPlot::setViewport(const QRect &rect)
{
mViewport = rect;
QRect localRect = rect;
if (localRect.height() == 0)
localRect.setHeight(100);
if (localRect.width() == 0)
localRect.setWidth(100);

mViewport = localRect;
if (mPlotLayout)
mPlotLayout->setOuterRect(mViewport);
}
Expand Down Expand Up @@ -1907,11 +1911,8 @@ void QCustomPlot::replot(QCustomPlot::RefreshPriority refreshPriority)
layer->drawToPaintBuffer();
for (int i=0; i<mPaintBuffers.size(); ++i)
mPaintBuffers.at(i)->setInvalidated(false);

if ((refreshPriority == rpRefreshHint && mPlottingHints.testFlag(QCP::phImmediateRefresh)) || refreshPriority==rpImmediateRefresh)
repaint();
else
update();

update();

emit afterReplot();
mReplotting = false;
Expand Down Expand Up @@ -2204,32 +2205,32 @@ QSize QCustomPlot::sizeHint() const
Event handler for when the QCustomPlot widget needs repainting. This does not cause a \ref replot, but
draws the internal buffer on the widget surface.
*/
void QCustomPlot::paintEvent(QPaintEvent *event)
void QCustomPlot::paint(QPainter *painter)
{
Q_UNUSED(event);
QCPPainter painter(this);
if (painter.isActive())
{
painter.setRenderHint(QPainter::HighQualityAntialiasing); // to make Antialiasing look good if using the OpenGL graphicssystem
if (mBackgroundBrush.style() != Qt::NoBrush)
painter.fillRect(mViewport, mBackgroundBrush);
drawBackground(&painter);
for (int bufferIndex = 0; bufferIndex < mPaintBuffers.size(); ++bufferIndex)
mPaintBuffers.at(bufferIndex)->draw(&painter);
}
if (painter->isActive())
{
painter->setRenderHint(QPainter::HighQualityAntialiasing); // to make Antialiasing look good if using the OpenGL graphicssystem
if (mBackgroundBrush.style() != Qt::NoBrush)
painter->fillRect(mViewport, mBackgroundBrush);
drawBackground(painter);
for (int bufferIndex = 0; bufferIndex < mPaintBuffers.size(); ++bufferIndex)
mPaintBuffers.at(bufferIndex)->draw(painter);
}
}


/*! \internal
Event handler for a resize of the QCustomPlot widget. The viewport (which becomes the outer rect
of mPlotLayout) is resized appropriately. Finally a \ref replot is performed.
*/
void QCustomPlot::resizeEvent(QResizeEvent *event)
void QCustomPlot::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
{
Q_UNUSED(event)
// resize and repaint the buffer:
setViewport(rect());
replot(rpQueuedRefresh); // queued refresh is important here, to prevent painting issues in some contexts (e.g. MDI subwindow)
Q_UNUSED(newGeometry)
Q_UNUSED(oldGeometry)
// resize and repaint the buffer:
setViewport(rect());
replot(rpQueuedRefresh); // queued refresh is important here, to prevent painting issues in some contexts (e.g. MDI subwindow)
}

/*! \internal
Expand Down Expand Up @@ -2522,6 +2523,28 @@ void QCustomPlot::drawBackground(QCPPainter *painter)
}
}

void QCustomPlot::drawBackground(QPainter *painter)
{
// Note: background color is handled in individual replot/save functions

// draw background pixmap (on top of fill, if brush specified):
if (!mBackgroundPixmap.isNull())
{
if (mBackgroundScaled)
{
// check whether mScaledBackground needs to be updated:
QSize scaledSize(mBackgroundPixmap.size());
scaledSize.scale(mViewport.size(), mBackgroundScaledMode);
if (mScaledBackgroundPixmap.size() != scaledSize)
mScaledBackgroundPixmap = mBackgroundPixmap.scaled(mViewport.size(), mBackgroundScaledMode, Qt::SmoothTransformation);
painter->drawPixmap(mViewport.topLeft(), mScaledBackgroundPixmap, QRect(0, 0, mViewport.width(), mViewport.height()) & mScaledBackgroundPixmap.rect());
} else
{
painter->drawPixmap(mViewport.topLeft(), mBackgroundPixmap, QRect(0, 0, mViewport.width(), mViewport.height()));
}
}
}

/*! \internal
Goes through the layers and makes sure this QCustomPlot instance holds the correct number of
Expand Down
26 changes: 19 additions & 7 deletions src/core.h
Expand Up @@ -40,7 +40,7 @@ class QCPLegend;
class QCPAbstractLegendItem;
class QCPSelectionRect;

class QCP_LIB_DECL QCustomPlot : public QWidget
class QCP_LIB_DECL QCustomPlot : public QQuickPaintedItem
{
Q_OBJECT
/// \cond INCLUDE_QPROPERTIES
Expand Down Expand Up @@ -78,7 +78,7 @@ class QCP_LIB_DECL QCustomPlot : public QWidget
};
Q_ENUMS(RefreshPriority)

explicit QCustomPlot(QWidget *parent = 0);
explicit QCustomPlot(QQuickItem *parent = 0);
virtual ~QCustomPlot();

// getters:
Expand All @@ -99,6 +99,11 @@ class QCP_LIB_DECL QCustomPlot : public QWidget
QCP::SelectionRectMode selectionRectMode() const { return mSelectionRectMode; }
QCPSelectionRect *selectionRect() const { return mSelectionRect; }
bool openGl() const { return mOpenGl; }
// Custom
QLocale locale() const { return mLocale; }
int devicePixelRatio() const { return 1; } //QScreen pixel ratio
QFont font() const { return mFont; }
QRect rect() const { return QRect(0, 0, width(), height()); }

// setters:
void setViewport(const QRect &rect);
Expand All @@ -123,6 +128,9 @@ class QCP_LIB_DECL QCustomPlot : public QWidget
void setSelectionRectMode(QCP::SelectionRectMode mode);
void setSelectionRect(QCPSelectionRect *selectionRect);
void setOpenGl(bool enabled, int multisampling=16);
// Custom
void setLocale(const QLocale &locale) { mLocale = locale; }
void setFont(const QFont &font) { mFont = font; }

// non-property methods:
// plottable interface:
Expand Down Expand Up @@ -191,7 +199,7 @@ class QCP_LIB_DECL QCustomPlot : public QWidget

QCPAxis *xAxis, *yAxis, *xAxis2, *yAxis2;
QCPLegend *legend;

signals:
void mouseDoubleClick(QMouseEvent *event);
void mousePress(QMouseEvent *event);
Expand Down Expand Up @@ -237,6 +245,9 @@ class QCP_LIB_DECL QCustomPlot : public QWidget
QCP::SelectionRectMode mSelectionRectMode;
QCPSelectionRect *mSelectionRect;
bool mOpenGl;
//Custom
QLocale mLocale;
QFont mFont;

// non-property members:
QList<QSharedPointer<QCPAbstractPaintBuffer> > mPaintBuffers;
Expand All @@ -256,10 +267,10 @@ class QCP_LIB_DECL QCustomPlot : public QWidget
#endif

// reimplemented virtual methods:
virtual QSize minimumSizeHint() const Q_DECL_OVERRIDE;
virtual QSize sizeHint() const Q_DECL_OVERRIDE;
virtual void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
virtual void resizeEvent(QResizeEvent *event) Q_DECL_OVERRIDE;
virtual QSize minimumSizeHint() const;
virtual QSize sizeHint() const;
virtual void paint(QPainter *painter) Q_DECL_FINAL;
virtual void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) Q_DECL_OVERRIDE;
virtual void mouseDoubleClickEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
virtual void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
virtual void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
Expand All @@ -283,6 +294,7 @@ class QCP_LIB_DECL QCustomPlot : public QWidget
QCPLayerable *layerableAt(const QPointF &pos, bool onlySelectable, QVariant *selectionDetails=0) const;
QList<QCPLayerable*> layerableListAt(const QPointF &pos, bool onlySelectable, QList<QVariant> *selectionDetails=0) const;
void drawBackground(QCPPainter *painter);
void drawBackground(QPainter *painter);
void setupPaintBuffers();
QCPAbstractPaintBuffer *createPaintBuffer();
bool hasInvalidatedPaintBuffers();
Expand Down
1 change: 1 addition & 0 deletions src/global.h
Expand Up @@ -66,6 +66,7 @@
#include <qmath.h>
#include <limits>
#include <algorithm>
#include <QtQuick/QQuickPaintedItem>
#ifdef QCP_OPENGL_FBO
# include <QtGui/QOpenGLContext>
# include <QtGui/QOpenGLFramebufferObject>
Expand Down
24 changes: 24 additions & 0 deletions src/paintbuffer.cpp
Expand Up @@ -227,6 +227,15 @@ void QCPPaintBufferPixmap::draw(QCPPainter *painter) const
qDebug() << Q_FUNC_INFO << "invalid or inactive painter passed";
}

/* inherits documentation from base class */
void QCPPaintBufferPixmap::draw(QPainter *painter) const
{
if (painter && painter->isActive())
painter->drawPixmap(0, 0, mBuffer);
else
qDebug() << Q_FUNC_INFO << "invalid or inactive painter passed";
}

/* inherits documentation from base class */
void QCPPaintBufferPixmap::clear(const QColor &color)
{
Expand Down Expand Up @@ -436,6 +445,21 @@ void QCPPaintBufferGlFbo::draw(QCPPainter *painter) const
painter->drawImage(0, 0, mGlFrameBuffer->toImage());
}

/* inherits documentation from base class */
void QCPPaintBufferGlFbo::draw(QPainter *painter) const
{
if (!painter || !painter->isActive())
{
qDebug() << Q_FUNC_INFO << "invalid or inactive painter passed";
return;
}
if (!mGlFrameBuffer)
{
qDebug() << Q_FUNC_INFO << "OpenGL frame buffer object doesn't exist, reallocateBuffer was not called?";
return;
}
painter->drawImage(0, 0, mGlFrameBuffer->toImage());
}
/* inherits documentation from base class */
void QCPPaintBufferGlFbo::clear(const QColor &color)
{
Expand Down
3 changes: 3 additions & 0 deletions src/paintbuffer.h
Expand Up @@ -50,6 +50,7 @@ class QCP_LIB_DECL QCPAbstractPaintBuffer
virtual QCPPainter *startPainting() = 0;
virtual void donePainting() {}
virtual void draw(QCPPainter *painter) const = 0;
virtual void draw(QPainter *painter) const = 0;
virtual void clear(const QColor &color) = 0;

protected:
Expand All @@ -74,6 +75,7 @@ class QCP_LIB_DECL QCPPaintBufferPixmap : public QCPAbstractPaintBuffer
// reimplemented virtual methods:
virtual QCPPainter *startPainting() Q_DECL_OVERRIDE;
virtual void draw(QCPPainter *painter) const Q_DECL_OVERRIDE;
virtual void draw(QPainter *painter) const Q_DECL_OVERRIDE;
void clear(const QColor &color) Q_DECL_OVERRIDE;

protected:
Expand Down Expand Up @@ -119,6 +121,7 @@ class QCP_LIB_DECL QCPPaintBufferGlFbo : public QCPAbstractPaintBuffer
virtual QCPPainter *startPainting() Q_DECL_OVERRIDE;
virtual void donePainting() Q_DECL_OVERRIDE;
virtual void draw(QCPPainter *painter) const Q_DECL_OVERRIDE;
virtual void draw(QPainter *painter) const Q_DECL_OVERRIDE;
void clear(const QColor &color) Q_DECL_OVERRIDE;

protected:
Expand Down
2 changes: 1 addition & 1 deletion src/qcp-staticlib.pro
Expand Up @@ -9,7 +9,7 @@
# (note that qmake understands "*.h" if you choose the latter option.)
#

QT += core gui opengl
QT += core gui opengl quick
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets printsupport

TEMPLATE = lib
Expand Down

0 comments on commit ecb12a6

Please sign in to comment.