Skip to content

Commit

Permalink
wip: Render background dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
Tectu committed May 9, 2024
1 parent bf48756 commit 4da202c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 108 deletions.
26 changes: 15 additions & 11 deletions qschematic/background.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <QPen>
#include <QBrush>
#include <QPainter>
#include <QStyleOptionGraphicsItem>

using namespace QSchematic;

Expand All @@ -25,9 +26,12 @@ Background::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWi

// Background pen
QPen backgroundPen;
backgroundPen.setStyle(Qt::NoPen);

// Background brush
QBrush backgroundBrush;
backgroundBrush.setStyle(Qt::SolidPattern);
backgroundBrush.setColor(Qt::white);

// Grid pen
QPen gridPen;
Expand All @@ -40,11 +44,16 @@ Background::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWi
QBrush gridBrush;
gridBrush.setStyle(Qt::NoBrush);

// Get the rects
//
// sr = scene rect
// ep = exposed rect
const QRectF sr = rect();
const QRectF er = (option ? option->exposedRect : rect());

painter->save();
painter->setRenderHint(QPainter::Antialiasing, m_settings.antialiasing);

qDebug() << rect();

// Draw background
painter->setPen(backgroundPen);
painter->setBrush(backgroundBrush);
Expand All @@ -55,17 +64,12 @@ Background::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWi
qreal left = int(rect().left()) - (int(rect().left()) % m_settings.gridSize);
qreal top = int(rect().top()) - (int(rect().top()) % m_settings.gridSize);

// Create a list of points
QVector<QPointF> points;
for (qreal x = left; x < rect().right(); x += m_settings.gridSize) {
for (qreal y = top; y < rect().bottom(); y += m_settings.gridSize)
points.append(QPointF(x,y));
}

// Draw the actual grid points
painter->setPen(gridPen);
painter->setBrush(gridBrush);
painter->drawPoints(points.data(), points.size());
for (qreal x = left; x < sr.right(); x += m_settings.gridSize) {
for (qreal y = top; y < sr.bottom(); y += m_settings.gridSize)
painter->drawPoint(x, y);
}
}

// Mark the origin if supposed to
Expand Down
101 changes: 17 additions & 84 deletions qschematic/scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,14 @@ Scene::Scene(QObject* parent) :
_popup->setPos(_lastMousePos + QPointF{ 5, 5 });
});

// Background
_background = new Background;
QGraphicsScene::addItem(_background);

// Stuff
connect(this, &QGraphicsScene::sceneRectChanged, [this](const QRectF& rect){
renderCachedBackground();

if (_background)
_background->setRect(rect);
;//_background->setRect(rect);
});

// Prepare the background
renderCachedBackground();
// Background
setupBackground();
}

Scene::~Scene()
Expand Down Expand Up @@ -178,6 +172,10 @@ Scene::from_container(const gpds::container& container)
void
Scene::setSettings(const Settings& settings)
{
// Update background
if (_background)
_background->setSettings(settings);

// Update settings of all items
for (auto& item : items())
item->setSettings(settings);
Expand All @@ -189,7 +187,6 @@ Scene::setSettings(const Settings& settings)
_settings = settings;

// Redraw
renderCachedBackground();
update();
}

Expand Down Expand Up @@ -281,8 +278,11 @@ Scene::clear()
// Now that all the top-level items are safeguarded we can call the underlying scene's clear()
QGraphicsScene::clear();

// NO longer dirty
// No longer dirty
clearIsDirty();

// Setup the background again
setupBackground();
}

bool
Expand Down Expand Up @@ -941,14 +941,6 @@ Scene::dropEvent(QGraphicsSceneDragDropEvent* event)
}
}

void
Scene::drawBackground(QPainter* painter, const QRectF& rect)
{
const QPointF& pixmapTopleft = rect.topLeft() - sceneRect().topLeft();

painter->drawPixmap(rect, _backgroundPixmap, QRectF(pixmapTopleft.x(), pixmapTopleft.y(), rect.width(), rect.height()));
}

QVector2D
Scene::itemsMoveSnap(const std::shared_ptr<Items::Item>& items, const QVector2D& moveBy) const
{
Expand All @@ -957,73 +949,14 @@ Scene::itemsMoveSnap(const std::shared_ptr<Items::Item>& items, const QVector2D&
return moveBy;
}

QPixmap
Scene::renderBackground(const QRect& rect) const
{
// Create pixmap
QPixmap pixmap(rect.width(), rect.height());

// Grid pen
QPen gridPen;
gridPen.setStyle(Qt::SolidLine);
gridPen.setColor(Qt::gray);
gridPen.setCapStyle(Qt::RoundCap);
gridPen.setWidth(_settings.gridPointSize);

// Grid brush
QBrush gridBrush;
gridBrush.setStyle(Qt::NoBrush);

// Create a painter
QPainter painter(&pixmap);
painter.setRenderHint(QPainter::Antialiasing, _settings.antialiasing);

// Draw background
pixmap.fill(Qt::white);

// Draw the grid if supposed to
if (_settings.showGrid && (_settings.gridSize > 0)) {
qreal left = int(rect.left()) - (int(rect.left()) % _settings.gridSize);
qreal top = int(rect.top()) - (int(rect.top()) % _settings.gridSize);

// Create a list of points
QVector<QPointF> points;
for (qreal x = left; x < rect.right(); x += _settings.gridSize) {
for (qreal y = top; y < rect.bottom(); y += _settings.gridSize)
points.append(QPointF(x,y));
}

// Draw the actual grid points
painter.setPen(gridPen);
painter.setBrush(gridBrush);
painter.drawPoints(points.data(), points.size());
}

// Mark the origin if supposed to
if (_settings.debug) {
painter.setPen(Qt::NoPen);
painter.setBrush(QBrush(Qt::red));
painter.drawEllipse(-6, -6, 12, 12);
}

painter.end();

return pixmap;
}

void
Scene::renderCachedBackground()
Scene::setupBackground()
{
// Create the pixmap
const QRect& rect = sceneRect().toRect();
if (rect.isNull() || !rect.isValid())
return;

// Render background
_backgroundPixmap = std::move(renderBackground(rect));

// Update
update();
_background = new Background(nullptr);
_background->setRect(sceneRect());
_background->setZValue(-10'000);
_background->setSettings(_settings);
QGraphicsScene::addItem(_background);
}

void
Expand Down
14 changes: 1 addition & 13 deletions qschematic/scene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ namespace QSchematic
void dragMoveEvent(QGraphicsSceneDragDropEvent* event) override;
void dragLeaveEvent(QGraphicsSceneDragDropEvent* event) override;
void dropEvent(QGraphicsSceneDragDropEvent* event) override;
void drawBackground(QPainter* painter, const QRectF& rect) override;

/**
* This gets called just before the item is actually being moved by moveBy. Subclasses may
Expand All @@ -223,21 +222,11 @@ namespace QSchematic
QVector2D
itemsMoveSnap(const std::shared_ptr<Items::Item>& item, const QVector2D& moveBy) const;

/**
* Renders the background.
*
* @param rect The scene rectangle. This is guaranteed to be non-null & valid.
*/
[[nodiscard]]
virtual
QPixmap
renderBackground(const QRect& rect) const;

private Q_SLOTS:
void wirePointMoved(wire& rawWire, int index);

private:
void renderCachedBackground();
void setupBackground();
void setupNewItem(Items::Item& item);
void updateNodeConnections(const Items::Node* node);
void generateConnections();
Expand Down Expand Up @@ -274,7 +263,6 @@ namespace QSchematic
// ItemUtils::ItemsCustodian<Item> _items;
// ItemUtils::ItemsCustodian<WireNet> m_nets;

QPixmap _backgroundPixmap;
std::function<std::shared_ptr<Items::Wire>()> _wireFactory;
int _mode = NormalMode;
std::shared_ptr<Items::Wire> _newWire;
Expand Down

0 comments on commit 4da202c

Please sign in to comment.