feat: add background grid

This commit is contained in:
parker
2025-06-20 03:23:07 +01:00
parent b829ba639b
commit 7394f72f51
5 changed files with 58 additions and 1 deletions

View File

@@ -8,9 +8,11 @@ Network::Network(QWidget* parent)
{ {
mainLayout_ = new QVBoxLayout(parent); mainLayout_ = new QVBoxLayout(parent);
mainLayout_->setContentsMargins(0,0,0,0);
this->setLayout(mainLayout_); this->setLayout(mainLayout_);
NetworkGraphicsScene* scene = new NetworkGraphicsScene(); NetworkGraphicsScene* scene = new NetworkGraphicsScene();
NetworkGraphicsView* view = new NetworkGraphicsView(parent, scene); NetworkGraphicsView* view = new NetworkGraphicsView(parent, scene);

View File

@@ -9,8 +9,44 @@ NetworkGraphicsScene::NetworkGraphicsScene()
{ {
sceneWidth_ = 64000; sceneWidth_ = 64000;
sceneHeight_ = 64000; sceneHeight_ = 64000;
gridSize_ = 20;
setSceneRect(sceneWidth_/-2.0f, sceneHeight_/-2.0f, sceneWidth_, sceneHeight_); setSceneRect(sceneWidth_/-2.0f, sceneHeight_/-2.0f, sceneWidth_, sceneHeight_);
setBackgroundBrush(QColor("#1b1b1b"));
} }
void NetworkGraphicsScene::drawBackground(QPainter *painter, const QRectF &rect)
{
QGraphicsScene::drawBackground(painter, rect);
int top = ceil(rect.top());
int bottom = floor(rect.bottom());
int left = floor(rect.left());
int right = ceil(rect.right());
std::cout << "top: " << top << "\n";
std::cout << "bottom: " << bottom << "\n";
std::cout << "left: " << left << "\n";
std::cout << "right: " << right << "\n";
QPen gridPen(QColor("#323232"));
painter->setPen(gridPen);
QList<QLine> lines;
for (int y = top - (top % gridSize_); y <= bottom; y += gridSize_) {
lines.append(QLine(left, y, right, y));
}
for (int x = left - (left % gridSize_); x <= right; x += gridSize_) {
lines.append(QLine(x, bottom, x, top));
}
painter->drawLines(lines);
}

View File

@@ -10,5 +10,7 @@ public:
private: private:
uint sceneWidth_; uint sceneWidth_;
uint sceneHeight_; uint sceneHeight_;
uint gridSize_;
protected: protected:
void drawBackground(QPainter *painter, const QRectF &rect) override;
}; };

View File

@@ -10,16 +10,32 @@ NetworkGraphicsView::NetworkGraphicsView(QWidget *parent, QGraphicsScene* scene)
{ {
setScene(scene); setScene(scene);
initUI();
QPen greenPen = QPen(Qt::green); QPen greenPen = QPen(Qt::green);
greenPen.setWidth(6); greenPen.setWidth(6);
auto* rect1 = scene->addRect(50, 50, 100, 100, greenPen); auto* rect1 = scene->addRect(50, 50, 100, 100, greenPen);
rect1->setFlag(QGraphicsItem::ItemIsMovable); rect1->setFlag(QGraphicsItem::ItemIsMovable);
auto* rect2 = scene->addRect(80, 120, 100, 100, greenPen); auto* rect2 = scene->addRect(80, 120, 100, 100, greenPen);
rect2->setFlag(QGraphicsItem::ItemIsMovable); rect2->setFlag(QGraphicsItem::ItemIsMovable);
auto* rect3 = scene->addRect(80, -120, 100, 100, greenPen);
rect3->setFlag(QGraphicsItem::ItemIsMovable);
}
void NetworkGraphicsView::initUI()
{
// zoom from mouse
setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
// disable scroll bars
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::TextAntialiasing);
//
setViewportUpdateMode(ViewportUpdateMode::FullViewportUpdate);
} }

View File

@@ -10,6 +10,7 @@ public:
NetworkGraphicsView(QWidget *parent = nullptr, QGraphicsScene* scene = nullptr); NetworkGraphicsView(QWidget *parent = nullptr, QGraphicsScene* scene = nullptr);
private: private:
QPointF panStartPos; QPointF panStartPos;
void initUI();
protected: protected:
void mouseMoveEvent(QMouseEvent *mouseEvent) override; void mouseMoveEvent(QMouseEvent *mouseEvent) override;