diff --git a/qschematic/background.cpp b/qschematic/background.cpp index aceaa53..d2ec8ba 100644 --- a/qschematic/background.cpp +++ b/qschematic/background.cpp @@ -3,6 +3,7 @@ #include #include #include +#include using namespace QSchematic; @@ -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; @@ -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); @@ -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 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 diff --git a/qschematic/scene.cpp b/qschematic/scene.cpp index 5b649bb..5b70eeb 100644 --- a/qschematic/scene.cpp +++ b/qschematic/scene.cpp @@ -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() @@ -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); @@ -189,7 +187,6 @@ Scene::setSettings(const Settings& settings) _settings = settings; // Redraw - renderCachedBackground(); update(); } @@ -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 @@ -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, const QVector2D& moveBy) const { @@ -957,73 +949,14 @@ Scene::itemsMoveSnap(const std::shared_ptr& 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 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 diff --git a/qschematic/scene.hpp b/qschematic/scene.hpp index 574f6a6..d3ec624 100644 --- a/qschematic/scene.hpp +++ b/qschematic/scene.hpp @@ -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 @@ -223,21 +222,11 @@ namespace QSchematic QVector2D itemsMoveSnap(const std::shared_ptr& 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(); @@ -274,7 +263,6 @@ namespace QSchematic // ItemUtils::ItemsCustodian _items; // ItemUtils::ItemsCustodian m_nets; - QPixmap _backgroundPixmap; std::function()> _wireFactory; int _mode = NormalMode; std::shared_ptr _newWire;