#ifndef COMPONENTEDITOR_H
#define COMPONENTEDITOR_H
#include <QObject>
#include <QPainter>
#include <QColor>
#include <QRectF>
#include <QPainter>
#include <QImage>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsSvgItem>
#include <QWheelEvent>
#include <QDebug>
#include <qmath.h>
const QString COMP_SVG
= "/Users/foobarbaz/Source/ThisApp/gui/Component/Resources/SVG/dip-pkg.svg";
Q_OBJECT
Q_PROPERTY(unsigned int gridSize READ gridSize WRITE setGridSize DESIGNABLE true)
Q_PROPERTY(QColor gridColor READ gridColor WRITE setGridColor DESIGNABLE
true)
public:
explicit ComponentEditor
(QWidget *parent
= 0);
~ComponentEditor();
unsigned int gridSize();
void setGridSize(unsigned int p_size);
void setGridColor
(QColor p_color
);
public slots:
void setDrag(DragMode p_mode);
protected:
private:
unsigned int m_grid;
};
#endif // COMPONENTEDITOR_H
#ifndef COMPONENTEDITOR_H
#define COMPONENTEDITOR_H
#include <QObject>
#include <QPainter>
#include <QColor>
#include <QRectF>
#include <QPainter>
#include <QImage>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsSvgItem>
#include <QWheelEvent>
#include <QDebug>
#include <qmath.h>
const QString COMP_SVG = "/Users/foobarbaz/Source/ThisApp/gui/Component/Resources/SVG/dip-pkg.svg";
class ComponentEditor : public QGraphicsView {
Q_OBJECT
Q_PROPERTY(unsigned int gridSize READ gridSize WRITE setGridSize DESIGNABLE true)
Q_PROPERTY(QColor gridColor READ gridColor WRITE setGridColor DESIGNABLE true)
public:
explicit ComponentEditor(QWidget *parent = 0);
~ComponentEditor();
unsigned int gridSize();
void setGridSize(unsigned int p_size);
QColor gridColor();
void setGridColor(QColor p_color);
public slots:
void setDrag(DragMode p_mode);
void wheelEvent(QWheelEvent *p_event);
protected:
void drawBackground(QPainter *p_painter, const QRectF &p_rect);
private:
QGraphicsScene* m_scene;
QGraphicsSvgItem* m_compSvg;
unsigned int m_grid;
QColor m_color;
};
#endif // COMPONENTEDITOR_H
To copy to clipboard, switch view to plain text mode
#include "componenteditor.h"
m_scene->setSceneRect(0, 0, 10000, 10000);
setScene(m_scene);
scale(1, 1);
m_grid = 100;
m_color = Qt::blue;
QPointF center
= m_scene
->sceneRect
().
center();
m_compSvg->setPos(center);
m_scene->addItem(m_compSvg);
centerOn(m_compSvg);
}
ComponentEditor::~ComponentEditor() {
}
void ComponentEditor::setDrag(DragMode p_mode) {
}
void ComponentEditor
::drawBackground(QPainter *p_painter,
const QRectF &p_rect
) {
// p_painter->setWorldMatrixEnabled(true);
p_painter->setPen(m_color);
qreal left = int(rect.left());
qreal top = int(rect.top());
QVarLengthArray<QLineF, 100> linesX;
for (qreal x = left; x < rect.right(); x += m_grid )
linesX.
append(QLineF(x, rect.
top(), x, rect.
bottom()));
QVarLengthArray<QLineF, 100> linesY;
for (qreal y = top; y < rect.bottom(); y += m_grid )
linesY.
append(QLineF(rect.
left(), y, rect.
right(), y
));
p_painter->drawLines(linesX.data(), linesX.size());
p_painter->drawLines(linesY.data(), linesY.size());
}
unsigned int ComponentEditor::gridSize() {
return m_grid;
}
void ComponentEditor::setGridSize(unsigned int p_size) {
m_grid = p_size;
}
QColor ComponentEditor
::gridColor() { return m_color;
}
void ComponentEditor
::setGridColor(QColor p_color
) { m_color = p_color;
}
// this is run when the user uses the mouse wheel to scale up/down
// the view
void ComponentEditor
::wheelEvent(QWheelEvent *p_event
) {
qreal factor = qPow(1.2, p_event->delta() / 240.0);
scale(factor, factor);
// debug: new scene size
QRect sRect
(0,
0, width
(), height
());
QRectF realRect
= mapFromScene
(sRect
).
boundingRect();
qDebug() << "CE: Scene" << realRect.width() << realRect.height() << factor;
p_event->accept();
}
#include "componenteditor.h"
ComponentEditor::ComponentEditor(QWidget *parent) : QGraphicsView(parent) {
m_scene = new QGraphicsScene();
setDragMode(QGraphicsView::ScrollHandDrag);
m_scene->setSceneRect(0, 0, 10000, 10000);
setScene(m_scene);
scale(1, 1);
m_grid = 100;
m_color = Qt::blue;
QPointF center = m_scene->sceneRect().center();
m_compSvg = new QGraphicsSvgItem(COMP_SVG);
m_compSvg->setPos(center);
m_scene->addItem(m_compSvg);
centerOn(m_compSvg);
}
ComponentEditor::~ComponentEditor() {
}
void ComponentEditor::setDrag(DragMode p_mode) {
QGraphicsView::setDragMode(p_mode);
}
void ComponentEditor::drawBackground(QPainter *p_painter, const QRectF &p_rect) {
QRectF rect = sceneRect();
// p_painter->setWorldMatrixEnabled(true);
p_painter->setPen(m_color);
qreal left = int(rect.left());
qreal top = int(rect.top());
QVarLengthArray<QLineF, 100> linesX;
for (qreal x = left; x < rect.right(); x += m_grid )
linesX.append(QLineF(x, rect.top(), x, rect.bottom()));
QVarLengthArray<QLineF, 100> linesY;
for (qreal y = top; y < rect.bottom(); y += m_grid )
linesY.append(QLineF(rect.left(), y, rect.right(), y));
p_painter->drawLines(linesX.data(), linesX.size());
p_painter->drawLines(linesY.data(), linesY.size());
}
unsigned int ComponentEditor::gridSize() {
return m_grid;
}
void ComponentEditor::setGridSize(unsigned int p_size) {
m_grid = p_size;
}
QColor ComponentEditor::gridColor() {
return m_color;
}
void ComponentEditor::setGridColor(QColor p_color) {
m_color = p_color;
}
// this is run when the user uses the mouse wheel to scale up/down
// the view
void ComponentEditor::wheelEvent(QWheelEvent *p_event) {
qreal factor = qPow(1.2, p_event->delta() / 240.0);
scale(factor, factor);
// debug: new scene size
QRect sRect(0, 0, width(), height());
QRectF realRect = mapFromScene(sRect).boundingRect();
qDebug() << "CE: Scene" << realRect.width() << realRect.height() << factor;
p_event->accept();
}
To copy to clipboard, switch view to plain text mode
Bookmarks