Files
c-evo-evo/common/include/Cevo/MetaCanvas.h
T

45 lines
1.3 KiB
C++

#pragma once
#include <QPainter>
#include <QPicture>
#include <QPointF>
#include <QTransform>
namespace Cevo {
// Equivalent of MetaCanvas.pas's TMetaCanvas (+ its ~15 TCanvasObject
// subclasses): a "recorded, replayable, zoomable/movable" canvas. Backed by
// Qt's own QPicture, which already records and replays arbitrary QPainter
// commands - so drawing uses Qt's native QPainter API (painter()) rather
// than reimplementing a parallel set of Do*/TCanvasXxx wrapper classes.
// zoom()/move() accumulate into a QTransform applied at drawTo() time,
// instead of mutating each recorded primitive's stored geometry in place
// (the original's approach, which loses precision over repeated Zoom
// calls).
//
// Usage: draw via painter(), call finishRecording() once done, then
// zoom()/move()/drawTo() any number of times; reset() clears everything to
// record again.
class MetaCanvas {
public:
MetaCanvas();
QPainter &painter() { return m_painter; }
void finishRecording();
void zoom(double factor);
void move(QPointF delta);
void reset();
// Replays every recorded command onto `target`, with the accumulated
// zoom/move transform applied.
void drawTo(QPainter &target) const;
private:
QPicture m_picture;
QPainter m_painter;
QTransform m_transform;
};
} // namespace Cevo