diff --git a/main.cpp b/main.cpp index b36a9f3..92e3112 100644 --- a/main.cpp +++ b/main.cpp @@ -12,6 +12,7 @@ int main(int argc, char *argv[]) QTSGraph w(600, 600); //w.SwapYAxis = true; + //w.MoveOtoCenter = true; w.show(); return a.exec(); } @@ -20,6 +21,7 @@ void QTSGraph::PaintBox() { // Начало рисования + //ShowAxes(); SetColor(clGreen); Line(0, 0, 600, 600); SetColor(0xFF0000); diff --git a/qtsgraph.cpp b/qtsgraph.cpp index b92b705..40a4050 100644 --- a/qtsgraph.cpp +++ b/qtsgraph.cpp @@ -50,16 +50,17 @@ void QTSGraph::Ellipse(int x1, int y1, int x2, int y2) QPainter painter(&Canvas); painter.setPen(Pen); painter.setBrush(Brush); + ChangeCoord(&x1, &y1); + ChangeCoord(&x2, &y2); if(y1 > y2) std::swap(y1, y2); if(x1 > x2) std::swap(x1, x2); - if(SwapYAxis) painter.drawEllipse(x1, Canvas.height() - y1 - abs(y2 - y1) - 1, abs(x2-x1), abs(y2-y1)); - else painter.drawEllipse(x1, y1, abs(x2-x1), abs(y2-y1)); + painter.drawEllipse(x1, y1, abs(x2-x1), abs(y2-y1)); update(); } QRgb QTSGraph::GetPixel(int x, int y) { - if(SwapYAxis) y = Canvas.height() - y - 1; + ChangeCoord(&x, &y); return Canvas.toImage().pixelColor(x, y).rgba() % 0x1000000; } @@ -95,6 +96,7 @@ QTSGraph::QTSGraph(int w, int h, int x, int y, QWidget *parent) StartTimer = new QTimer(); connect(StartTimer, SIGNAL(timeout()), this, SLOT(slotStartTimer())); StartTimer->start(500); + setMouseTracking(true); } QTSGraph::~QTSGraph() @@ -117,8 +119,8 @@ void QTSGraph::Circle(int x, int y, int radius) QPainter painter(&Canvas); painter.setPen(Pen); painter.setBrush(Brush); - if(SwapYAxis) painter.drawEllipse(x - radius, Canvas.height() - (y - radius) - (radius * 2) - 1, radius * 2, radius * 2); - else painter.drawEllipse(x - radius, y - radius, radius * 2, radius * 2); + ChangeCoord(&x, &y); + painter.drawEllipse(x - radius, y - radius, radius * 2, radius * 2); update(); } @@ -136,7 +138,7 @@ void QTSGraph::OutTextXY(int x, int y, std::string caption) painter.setPen(Pen); painter.setFont(Font); double r, b, sa, ca, sb, cb, xn, yn; - if(SwapYAxis) y = Canvas.height() - y - 1; + ChangeCoord(&x, &y); b = TextDirection * 3.14159 / 180; r = sqrt(x * x + y * y); sa = y / r; @@ -155,8 +157,8 @@ void QTSGraph::PutPixel(int x, int y, QRgb c, int PenWidth) { QPainter painter(&Canvas); painter.setPen(QPen(QBrush(QColor(c)), PenWidth)); - if(SwapYAxis) painter.drawPoint(x, Canvas.height() - y - 1); - else painter.drawPoint(x, y); + ChangeCoord(&x, &y); + painter.drawPoint(x, y); update(); } @@ -185,11 +187,27 @@ int QTSGraph::ReadMouseButton() } TPixel QTSGraph::ReadMousePosition() +{ + TPixel t; + t.x = MouseMovePosition.x(); + t.y = MouseMovePosition.y(); + t.color = GetPixel(t.x, t.y); + if(SwapYAxis) t.y = Canvas.height() - t.y - 1; + if(MoveOtoCenter) + { + t.x -= Canvas.width() / 2; + t.y -= Canvas.height() / 2; + } + return t; +} + +TPixel QTSGraph::GetLastMouseClickPosition() { TPixel t; t.x = LastMouseClickPosition.x(); t.y = LastMouseClickPosition.y(); t.color = GetPixel(t.x, t.y); + ChangeCoord(&t.x, &t.y); return t; } @@ -198,10 +216,11 @@ void QTSGraph::Rectangle(int x1, int y1, int x2, int y2) QPainter painter(&Canvas); painter.setPen(Pen); painter.setBrush(Brush); + ChangeCoord(&x1, &y1); + ChangeCoord(&x2, &y2); if(y1 > y2) std::swap(y1, y2); if(x1 > x2) std::swap(x1, x2); - if(SwapYAxis) painter.drawRect(x1, Canvas.height() - y1 - abs(y2 - y1) - 1, x2 - x1, y2 - y1); - else painter.drawRect(x1, y1, x2 - x1, y2 - y1); + painter.drawRect(x1, y1, x2 - x1, y2 - y1); update(); } @@ -247,8 +266,9 @@ void QTSGraph::Line(int x1, int y1, int x2, int y2) { QPainter painter(&Canvas); painter.setPen(Pen); - if(SwapYAxis) painter.drawLine(x1, Canvas.height() - y1 - 1, x2, Canvas.height() - y2 - 1); - else painter.drawLine(x1, y1, x2, y2); + ChangeCoord(&x1, &y1); + ChangeCoord(&x2, &y2); + painter.drawLine(x1, y1, x2, y2); update(); } @@ -266,6 +286,16 @@ void QTSGraph::slotStartTimer() PaintBox(); } +void QTSGraph::ChangeCoord(int *x, int *y) +{ + if(MoveOtoCenter) + { + *x += Canvas.width() / 2; + *y += Canvas.height() / 2; + } + if(SwapYAxis) *y = Canvas.height() - *y - 1; +} + void QTSGraph::slotResetTimer() { ResetTimer->stop(); @@ -281,44 +311,70 @@ void QTSGraph::paintEvent(QPaintEvent *event) int mult = 1; if(AxesVisible) { - p.setPen(QPen(QBrush(QColor(Qt::red)), 3)); - if(SwapYAxis) + if(MoveOtoCenter) { - p.drawLine(0, Canvas.height() - 1, Canvas.width() - 1, Canvas.height() - 1); - p.drawLine(Canvas.width() - 10, Canvas.height() - 10, Canvas.width() - 1, Canvas.height() - 1); - p.drawText(Canvas.width() - 10, Canvas.height() - 15, "X"); - p.drawLine(0, Canvas.height() - 1, 0, 0); - p.drawLine(10, 10, 0, 0); - p.drawText(12, 12, "Y"); - correctY = 1; + p.setPen(QPen(QBrush(QColor(Qt::red)), 1)); + if(SwapYAxis) + { + p.drawLine(Canvas.width() / 2, 0, Canvas.width() / 2, Canvas.height() - 1); + p.drawLine(Canvas.width() / 2, 0, Canvas.width() / 2 + 10, 10); + p.drawLine(Canvas.width() / 2, 0, Canvas.width() / 2 - 10, 10); + p.drawText(Canvas.width() / 2 - 20, 12, "Y"); + correctY = 1; + } + else + { + p.drawLine(Canvas.width() / 2, 0, Canvas.width() / 2, Canvas.height() - 1); + p.drawLine(Canvas.width() / 2, Canvas.height() - 1, Canvas.width() / 2 + 10, Canvas.height() - 1 - 10); + p.drawLine(Canvas.width() / 2, Canvas.height() - 1, Canvas.width() / 2 - 10, Canvas.height() - 1 - 10); + p.drawText(Canvas.width() / 2 - 20, Canvas.height() - 2, "Y"); + } + p.drawLine(0, Canvas.height() / 2, Canvas.width() - 1, Canvas.height() / 2); + p.drawLine(Canvas.width() - 1, Canvas.height() / 2, Canvas.width() - 1 - 10, Canvas.height() / 2 + 10); + p.drawLine(Canvas.width() - 1, Canvas.height() / 2, Canvas.width() - 1 - 10, Canvas.height() / 2 - 10); + p.drawText(Canvas.width() - 1 - 10, Canvas.height() / 2 + 20, "X"); } else { - p.drawLine(0, 0, Canvas.width() - 1, 0); - p.drawLine(Canvas.width() - 10, 10, Canvas.width() - 1, 0); - p.drawText(Canvas.width() - 10, 22, "X"); - p.drawLine(0, Canvas.height() - 1, 0, 0); - p.drawLine(10, Canvas.height() - 10, 0, Canvas.height() - 1); - p.drawText(13, Canvas.height() - 2, "Y"); + p.setPen(QPen(QBrush(QColor(Qt::red)), 3)); + if(SwapYAxis) + { + p.drawLine(0, Canvas.height() - 1, Canvas.width() - 1, Canvas.height() - 1); + p.drawLine(Canvas.width() - 10, Canvas.height() - 10, Canvas.width() - 1, Canvas.height() - 1); + p.drawText(Canvas.width() - 10, Canvas.height() - 15, "X"); + p.drawLine(0, Canvas.height() - 1, 0, 0); + p.drawLine(10, 10, 0, 0); + p.drawText(12, 12, "Y"); + correctY = 1; + } + else + { + p.drawLine(0, 0, Canvas.width() - 1, 0); + p.drawLine(Canvas.width() - 10, 10, Canvas.width() - 1, 0); + p.drawText(Canvas.width() - 10, 22, "X"); + p.drawLine(0, Canvas.height() - 1, 0, 0); + p.drawLine(10, Canvas.height() - 10, 0, Canvas.height() - 1); + p.drawText(13, Canvas.height() - 2, "Y"); + } + p.setPen(QPen(QBrush(QColor(Qt::lightGray)), 1)); + p.drawLine(Canvas.width() / 2, 0, Canvas.width() / 2, Canvas.height() - 1); + p.drawLine(0, Canvas.height() / 2 - correctY, Canvas.width() - 1, Canvas.height() / 2 - correctY); + p.drawText(Canvas.width() / 2 + 2, 12, QString::number(Canvas.width() / 2)); + p.drawText(2, Canvas.height() / 2 - 2, QString::number(Canvas.height() / 2)); + + p.setPen(QPen(QBrush(QColor(0xDDDDDD)), 1)); + p.drawLine(Canvas.width() / 4, 0, Canvas.width() / 4, Canvas.height() - 1); + p.drawLine(0, Canvas.height() / 4 - correctY, Canvas.width() - 1, Canvas.height() / 4 - correctY); + p.drawText(Canvas.width() / 4 + 2, 12, QString::number(Canvas.width() / 4)); + if(correctY) mult = 3; + p.drawText(2, Canvas.height() / 4 - 2, QString::number(mult * Canvas.height() / 4)); + + p.drawLine(3 * Canvas.width() / 4, 0, 3 * Canvas.width() / 4, Canvas.height() - 1); + p.drawLine(0, 3 * Canvas.height() / 4 - correctY, Canvas.width() - 1, 3 * Canvas.height() / 4 - correctY); + p.drawText(3 * Canvas.width() / 4 + 2, 12, QString::number(3 * Canvas.width() / 4)); + mult = correctY ? 1 : 3; + p.drawText(2, 3 * Canvas.height() / 4 - 2, QString::number(mult * Canvas.height() / 4)); } - p.setPen(QPen(QBrush(QColor(Qt::lightGray)), 1)); - p.drawLine(Canvas.width() / 2, 0, Canvas.width() / 2, Canvas.height() - 1); - p.drawLine(0, Canvas.height() / 2 - correctY, Canvas.width() - 1, Canvas.height() / 2 - correctY); - p.drawText(Canvas.width() / 2 + 2, 12, QString::number(Canvas.width() / 2)); - p.drawText(2, Canvas.height() / 2 - 2, QString::number(Canvas.height() / 2)); - - p.setPen(QPen(QBrush(QColor(0xDDDDDD)), 1)); - p.drawLine(Canvas.width() / 4, 0, Canvas.width() / 4, Canvas.height() - 1); - p.drawLine(0, Canvas.height() / 4 - correctY, Canvas.width() - 1, Canvas.height() / 4 - correctY); - p.drawText(Canvas.width() / 4 + 2, 12, QString::number(Canvas.width() / 4)); - if(correctY) mult = 3; - p.drawText(2, Canvas.height() / 4 - 2, QString::number(mult * Canvas.height() / 4)); - - p.drawLine(3 * Canvas.width() / 4, 0, 3 * Canvas.width() / 4, Canvas.height() - 1); - p.drawLine(0, 3 * Canvas.height() / 4 - correctY, Canvas.width() - 1, 3 * Canvas.height() / 4 - correctY); - p.drawText(3 * Canvas.width() / 4 + 2, 12, QString::number(3 * Canvas.width() / 4)); - mult = correctY ? 1 : 3; - p.drawText(2, 3 * Canvas.height() / 4 - 2, QString::number(mult * Canvas.height() / 4)); } } @@ -345,6 +401,12 @@ void QTSGraph::mousePressEvent(QMouseEvent *event) ResetTimer->start(ResetInterval); } +void QTSGraph::mouseMoveEvent(QMouseEvent *event) +{ + MouseMovePosition = event->pos(); + update(); +} + void QTSGraph::keyPressEvent(QKeyEvent *event) { ResetTimer->stop(); diff --git a/qtsgraph.h b/qtsgraph.h index ce2030d..d10b52e 100644 --- a/qtsgraph.h +++ b/qtsgraph.h @@ -76,6 +76,7 @@ public: ~QTSGraph(); bool SwapYAxis = false; + bool MoveOtoCenter = false; void ShowAxes(); void HideAxes(); @@ -92,6 +93,7 @@ public: int ReadKey(); int ReadMouseButton(); TPixel ReadMousePosition(); + TPixel GetLastMouseClickPosition(); void Rectangle(int x1, int y1, int x2, int y2); void SetColor(const QColor &c = Qt::black); void SetColor(const QRgb c = 0x00000000); @@ -146,6 +148,7 @@ private: int ResetInterval; int TextDirection = 0; QPoint LastMouseClickPosition; + QPoint MouseMovePosition; QBrush Brush; QPixmap Canvas; @@ -154,11 +157,14 @@ private: QTimer *ResetTimer; QTimer *StartTimer; + void ChangeCoord(int* x, int* y); + void PaintBox(); protected: void closeEvent(QCloseEvent *event) override; void keyPressEvent(QKeyEvent *event) override; void mousePressEvent(QMouseEvent *event) override; + void mouseMoveEvent(QMouseEvent *event) override; void paintEvent(QPaintEvent *event) override; };