// This file is part of c-evo-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. #include "Dpi/DpiGraphics.h" #include namespace Dpi { // ---- Font ---- Font::Font() : m_color(Qt::black) {} QString Font::name() const { return m_font.family(); } void Font::setName(const QString &value) { m_font.setFamily(value); changed(); } bool Font::bold() const { return m_font.bold(); } void Font::setBold(bool value) { m_font.setBold(value); changed(); } bool Font::italic() const { return m_font.italic(); } void Font::setItalic(bool value) { m_font.setItalic(value); changed(); } bool Font::underline() const { return m_font.underline(); } void Font::setUnderline(bool value) { m_font.setUnderline(value); changed(); } int Font::size() const { return m_font.pointSize(); } void Font::setSize(int value) { m_font.setPointSize(value); changed(); } int Font::height() const { return QFontMetrics(m_font).height(); } void Font::setHeight(int value) { m_font.setPixelSize(value); changed(); } QColor Font::color() const { return m_color; } void Font::setColor(const QColor &value) { m_color = value; changed(); } QSize Font::getTextSize(const QString &text) const { return QFontMetrics(m_font).size(Qt::TextSingleLine, text); } int Font::getTextWidth(const QString &text) const { return QFontMetrics(m_font).horizontalAdvance(text); } int Font::getTextHeight(const QString &text) const { Q_UNUSED(text); return QFontMetrics(m_font).height(); } void Font::assign(const Font &source) { m_font = source.m_font; m_color = source.m_color; changed(); } void Font::changed() { if (onChange) onChange(); } // ---- Pen ---- Pen::Pen() : m_color(Qt::black), m_style(Qt::SolidLine), m_width(1) {} void Pen::assign(const Pen &source) { m_color = source.m_color; m_style = source.m_style; m_width = source.m_width; } QPen Pen::toQPen() const { QPen pen(m_color); pen.setStyle(m_style); pen.setWidth(m_width); return pen; } // ---- Brush ---- Brush::Brush() : m_color(Qt::white), m_style(Qt::SolidPattern) {} void Brush::assign(const Brush &source) { m_color = source.m_color; m_style = source.m_style; } QBrush Brush::toQBrush() const { return QBrush(m_color, m_style); } // ---- Canvas ---- Canvas::Canvas() = default; Canvas::Canvas(QPaintDevice *device) { setDevice(device); } Canvas::~Canvas() { end(); } void Canvas::setDevice(QPaintDevice *device) { end(); m_device = device; } void Canvas::begin() { if (m_painter || !m_device) return; m_painter = new QPainter(m_device); m_ownsPainter = true; applyPenBrush(); } void Canvas::end() { if (m_painter && m_ownsPainter) { m_painter->end(); delete m_painter; } m_painter = nullptr; m_ownsPainter = false; } void Canvas::bind(QPainter *painter) { end(); m_painter = painter; m_ownsPainter = false; m_device = painter ? painter->device() : nullptr; applyPenBrush(); } void Canvas::unbind() { if (!m_ownsPainter) { m_painter = nullptr; } } QPainter *Canvas::painter() { if (!m_painter) begin(); return m_painter; } void Canvas::applyPenBrush() { if (!m_painter) return; m_painter->setPen(m_pen.toQPen()); m_painter->setBrush(m_brush.toQBrush()); m_painter->setFont(m_font.toQFont()); } int Canvas::width() const { return m_device ? m_device->width() : 0; } int Canvas::height() const { return m_device ? m_device->height() : 0; } QColor Canvas::pixel(int x, int y) const { auto *image = dynamic_cast(m_device); return image ? image->pixelColor(x, y) : QColor(); } void Canvas::setPixel(int x, int y, const QColor &color) { auto *image = dynamic_cast(m_device); if (image) image->setPixelColor(x, y, color); } void Canvas::moveTo(int x, int y) { m_penPos = QPoint(x, y); } void Canvas::lineTo(int x, int y) { painter()->setPen(m_pen.toQPen()); painter()->drawLine(m_penPos, QPoint(x, y)); m_penPos = QPoint(x, y); } void Canvas::line(int x1, int y1, int x2, int y2) { painter()->setPen(m_pen.toQPen()); painter()->drawLine(x1, y1, x2, y2); } void Canvas::rectangle(int x1, int y1, int x2, int y2) { applyPenBrush(); painter()->drawRect(QRect(QPoint(x1, y1), QPoint(x2 - 1, y2 - 1))); } void Canvas::ellipse(int x1, int y1, int x2, int y2) { applyPenBrush(); painter()->drawEllipse(QRect(QPoint(x1, y1), QPoint(x2 - 1, y2 - 1))); } void Canvas::roundRect(int x1, int y1, int x2, int y2, int rx, int ry) { applyPenBrush(); painter()->drawRoundedRect(QRect(QPoint(x1, y1), QPoint(x2 - 1, y2 - 1)), rx, ry); } void Canvas::polygon(const QPolygon &points) { applyPenBrush(); painter()->drawPolygon(points); } void Canvas::polyline(const QPolygon &points) { painter()->setPen(m_pen.toQPen()); painter()->drawPolyline(points); } void Canvas::textOut(int x, int y, const QString &text) { painter()->setFont(m_font.toQFont()); painter()->setPen(QPen(m_font.color())); painter()->drawText(x, y + QFontMetrics(m_font.toQFont()).ascent(), text); } int Canvas::textWidth(const QString &text) const { return m_font.getTextWidth(text); } int Canvas::textHeight(const QString &text) const { return m_font.getTextHeight(text); } QSize Canvas::textExtent(const QString &text) const { return m_font.getTextSize(text); } void Canvas::fillRect(const QRect &rect) { painter()->fillRect(rect, m_brush.toQBrush()); } void Canvas::frameRect(const QRect &rect) { painter()->setPen(m_pen.toQPen()); painter()->setBrush(Qt::NoBrush); painter()->drawRect(rect.adjusted(0, 0, -1, -1)); } void Canvas::draw(int x, int y, const QImage &image) { painter()->drawImage(QPoint(x, y), image); } void Canvas::stretchDraw(const QRect &rect, const QImage &image) { painter()->drawImage(rect, image); } void Canvas::copyRect(const QRect &dest, Canvas &source, const QRect &sourceRect) { auto *image = dynamic_cast(source.device()); if (image) painter()->drawImage(dest, *image, sourceRect); } // ---- Bitmap ---- Bitmap::Bitmap() : m_image(1, 1, QImage::Format_ARGB32), m_canvas(&m_image) {} Bitmap::Bitmap(int width, int height) : m_image(width, height, QImage::Format_ARGB32), m_canvas(&m_image) { m_image.fill(Qt::transparent); } void Bitmap::setSize(int width, int height) { QImage resized(width, height, QImage::Format_ARGB32); resized.fill(Qt::transparent); m_image = resized; m_canvas.setDevice(&m_image); } void Bitmap::setTransparentColor(const QColor &value) { m_transparentColor = value; m_transparent = true; } void Bitmap::loadFromFile(const QString &fileName) { m_image.load(fileName); m_image = m_image.convertToFormat(QImage::Format_ARGB32); m_canvas.setDevice(&m_image); } void Bitmap::saveToFile(const QString &fileName) const { m_image.save(fileName); } void Bitmap::assign(const Bitmap &source) { m_image = source.m_image; m_transparent = source.m_transparent; m_transparentColor = source.m_transparentColor; m_canvas.setDevice(&m_image); } // ---- Picture ---- void Picture::loadFromFile(const QString &fileName) { m_bitmap.loadFromFile(fileName); } } // namespace Dpi