Files
c-evo-evo/widget/include/Dpi/DpiGraphics.h
T
2026-10-22 18:11:04 +02:00

233 lines
6.8 KiB
C++

// This file is part of cevo-evo.
//
// This software is governed by the CeCILL license under French law and
// abiding by the rules of distribution of free software. You can use,
// modify and/or redistribute the software under the terms of the CeCILL
// license as circulated by CEA, CNRS and INRIA at the following URL
// "http://www.cecill.info".
//
// As a counterpart to the access to the source code and rights to copy,
// modify and redistribute granted by the license, users are provided only
// with a limited warranty and the software's author, the holder of the
// economic rights, and the successive licensors have only limited
// liability.
//
// In this respect, the user's attention is drawn to the risks associated
// with loading, using, modifying and/or developing or reproducing the
// software by the user in light of its specific status of free software,
// that may mean that it is complicated to manipulate, and that also
// therefore means that it is reserved for developers and experienced
// professionals having in-depth computer knowledge. Users are therefore
// encouraged to load and test the software's suitability as regards their
// requirements in conditions enabling the security of their systems and/or
// data to be ensured and, more generally, to use and operate it in the
// same conditions as regards security.
//
// The fact that you are presently reading this means that you have had
// knowledge of the CeCILL license and that you accept its terms.
#pragma once
#include <QBrush>
#include <QColor>
#include <QFont>
#include <QImage>
#include <QPaintDevice>
#include <QPainter>
#include <QPen>
#include <QPolygon>
#include <QSize>
#include <QString>
#include <functional>
namespace Dpi {
// TFont equivalent. Qt already renders fonts DPI-correctly, so sizes here are
// plain point sizes with no extra scaling applied.
class Font {
public:
Font();
QString name() const;
void setName(const QString &value);
bool bold() const;
void setBold(bool value);
bool italic() const;
void setItalic(bool value);
bool underline() const;
void setUnderline(bool value);
int size() const;
void setSize(int value);
int height() const;
void setHeight(int value);
QColor color() const;
void setColor(const QColor &value);
QSize getTextSize(const QString &text) const;
int getTextWidth(const QString &text) const;
int getTextHeight(const QString &text) const;
void assign(const Font &source);
QFont toQFont() const { return m_font; }
// TNotifyEvent equivalent.
std::function<void()> onChange;
private:
void changed();
QFont m_font;
QColor m_color;
};
// TPen equivalent.
class Pen {
public:
Pen();
QColor color() const { return m_color; }
void setColor(const QColor &value) { m_color = value; }
Qt::PenStyle style() const { return m_style; }
void setStyle(Qt::PenStyle value) { m_style = value; }
int width() const { return m_width; }
void setWidth(int value) { m_width = value; }
void assign(const Pen &source);
QPen toQPen() const;
private:
QColor m_color;
Qt::PenStyle m_style;
int m_width;
};
// TBrush equivalent.
class Brush {
public:
Brush();
QColor color() const { return m_color; }
void setColor(const QColor &value) { m_color = value; }
Qt::BrushStyle style() const { return m_style; }
void setStyle(Qt::BrushStyle value) { m_style = value; }
void assign(const Brush &source);
QBrush toQBrush() const;
private:
QColor m_color;
Qt::BrushStyle m_style;
};
// TCanvas equivalent, bound to a QPaintDevice (widget, pixmap or image).
// Unlike VCL/LCL, Qt only allows painting through a QPainter that is either
// explicitly begin()/end()-ed (for off-screen devices such as QImage) or
// handed to us already active from within a paintEvent (see bind()).
class Canvas {
public:
Canvas();
explicit Canvas(QPaintDevice *device);
~Canvas();
void setDevice(QPaintDevice *device);
QPaintDevice *device() const { return m_device; }
void begin();
void end();
bool active() const { return m_painter != nullptr; }
// Use an already-active, externally owned painter (typically supplied by
// QWidget::paintEvent). The canvas does not take ownership of it.
void bind(QPainter *painter);
void unbind();
Pen &pen() { return m_pen; }
Brush &brush() { return m_brush; }
Font &font() { return m_font; }
int width() const;
int height() const;
QColor pixel(int x, int y) const;
void setPixel(int x, int y, const QColor &color);
void moveTo(int x, int y);
void lineTo(int x, int y);
void line(int x1, int y1, int x2, int y2);
void rectangle(int x1, int y1, int x2, int y2);
void ellipse(int x1, int y1, int x2, int y2);
void roundRect(int x1, int y1, int x2, int y2, int rx, int ry);
void polygon(const QPolygon &points);
void polyline(const QPolygon &points);
void textOut(int x, int y, const QString &text);
int textWidth(const QString &text) const;
int textHeight(const QString &text) const;
QSize textExtent(const QString &text) const;
void fillRect(const QRect &rect);
void frameRect(const QRect &rect);
void draw(int x, int y, const QImage &image);
void stretchDraw(const QRect &rect, const QImage &image);
void copyRect(const QRect &dest, Canvas &source, const QRect &sourceRect);
private:
QPainter *painter();
void applyPenBrush();
QPaintDevice *m_device = nullptr;
QPainter *m_painter = nullptr;
bool m_ownsPainter = false;
QPoint m_penPos;
Pen m_pen;
Brush m_brush;
Font m_font;
};
// TBitmap equivalent, backed by a QImage in ARGB32 format for transparency
// support.
class Bitmap {
public:
Bitmap();
Bitmap(int width, int height);
void setSize(int width, int height);
int width() const { return m_image.width(); }
int height() const { return m_image.height(); }
QImage &image() { return m_image; }
const QImage &image() const { return m_image; }
Canvas &canvas() { return m_canvas; }
bool transparent() const { return m_transparent; }
void setTransparent(bool value) { m_transparent = value; }
QColor transparentColor() const { return m_transparentColor; }
void setTransparentColor(const QColor &value);
void loadFromFile(const QString &fileName);
void saveToFile(const QString &fileName) const;
void assign(const Bitmap &source);
private:
QImage m_image;
Canvas m_canvas;
bool m_transparent = false;
QColor m_transparentColor;
};
// TPicture equivalent: a simple container that owns a Bitmap.
class Picture {
public:
Bitmap &bitmap() { return m_bitmap; }
const Bitmap &bitmap() const { return m_bitmap; }
void loadFromFile(const QString &fileName);
private:
Bitmap m_bitmap;
};
} // namespace Dpi