From fe6ba051de9aedcb2b47e113ee30425be01f7fcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D1=80=D0=BE=D1=81?= =?UTF-8?q?=D0=BA=D1=83=D1=80=D0=BD=D1=91=D0=B2?= Date: Mon, 4 Jan 2021 19:49:03 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A6=D0=B2=D0=B5=D1=82=20=D0=BF=D0=B8=D0=BA?= =?UTF-8?q?=D1=81=D0=B5=D0=BB=D1=8F,=20=D0=B7=D0=B0=D0=BB=D0=B8=D0=B2?= =?UTF-8?q?=D0=BA=D0=B0,=20=D1=81=D0=B1=D1=80=D0=BE=D1=81=20=D0=BF=D0=BE?= =?UTF-8?q?=D0=BB=D0=BE=D0=B6=D0=B5=D0=BD=D0=B8=D0=B9=20=D0=BD=D0=B0=D0=B6?= =?UTF-8?q?=D0=B0=D1=82=D0=B8=D0=B9=20=D0=BC=D1=8B=D1=88=D0=B8=20=D0=B8=20?= =?UTF-8?q?=D0=BA=D0=BB=D0=B0=D0=B2=D0=B8=D0=B0=D1=82=D1=83=D1=80=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/main.cpp | 38 +++++++++++++++++++++++++++++++++++++- qtsgraph.cpp | 25 ++++++++++++++++++++++++- qtsgraph.h | 26 ++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 2 deletions(-) diff --git a/example/main.cpp b/example/main.cpp index 749b932..d2f9dfb 100644 --- a/example/main.cpp +++ b/example/main.cpp @@ -1,5 +1,6 @@ #include "../qtsgraph.h" #include +#include using namespace std; using namespace Qt; @@ -24,12 +25,12 @@ void QTSGraph::PaintBox() SetColor(clRed); Circle(300, 300, 100); + OutTextXY(20, 10, "Нажмите Enter или другую клавишу"); int k; k = ReadKey(); OutTextXY(20, 40, "Код нажатой клавиши: " + to_string(k)); if(k == 16777220) { - PutPixel(300, 300, clRed, 3); for(int i = 0; i <= 5; i++) { SetPenStyle(1, 1); @@ -48,7 +49,17 @@ void QTSGraph::PaintBox() PutPixel(100, 100, 0x00FF0000, 10); Delay(1000); PutPixel(300, 100); + PutPixel(301, 100); + PutPixel(302, 100); + PutPixel(298, 100); + PutPixel(299, 100); + PutPixel(300, 101); + PutPixel(300, 102); + PutPixel(300, 99); + PutPixel(300, 98); + PutPixel(300, 300, clRed, 3); int x = 1; + OutTextXY(20, 70, "Можно прервать, если кликнуть мышью."); while(!MouseClicked() && x < 1024) { PutPixel(x, 50, 0x555555 + x * 16, 5); @@ -56,6 +67,31 @@ void QTSGraph::PaintBox() Delay(10); } } + SetPenStyle(1, 1); + SetColor(clBlack); + SetFillStyle(3, clRed); + Rectangle(500, 100, 800, 300); + SetFillStyle(CrossPattern, clBlue); + Rectangle(500, 400, 800, 600); + SetPenStyle(1, 1); + SetColor(clBlack); + SetFillStyle(1, clBlack); + Rectangle(10, 600, 200, 700); + stringstream ss; + ss << "#" << hex << GetPixel(100,720); + SetColor(clBlack); + OutTextXY(10, 590, ss.str()); + ss.str(""); + ss << "#" << hex << GetPixel(50,650); + SetColor(clWhite); + OutTextXY(30, 620, ss.str()); + ss.str(""); + SetColor(clRed); + SetFillStyle(1, clRed); + Rectangle(200, 600, 400, 700); + ss << "#" << hex << GetPixel(250,650); + SetColor(clWhite); + OutTextXY(230, 620, ss.str()); // Конец рисования } diff --git a/qtsgraph.cpp b/qtsgraph.cpp index e4fc031..7a009ea 100644 --- a/qtsgraph.cpp +++ b/qtsgraph.cpp @@ -7,6 +7,10 @@ void QTSGraph::Delay(int ms) QCoreApplication::processEvents(QEventLoop::AllEvents, 50); } +QRgb QTSGraph::GetPixel(int x, int y) +{ + return Canvas.toImage().pixelColor(x, y).rgba(); +} QTSGraph::QTSGraph(int w, int h, int x, int y, QWidget *parent) : QMainWindow(parent) { @@ -24,6 +28,11 @@ QTSGraph::QTSGraph(int w, int h, int x, int y, QWidget *parent) Canvas.fill(Qt::white); QRgb DefaultColor = 0x00000000; Pen = QPen(QBrush(QColor(DefaultColor)), 1); + Brush = QBrush(QColor(DefaultColor),Qt::NoBrush); + ResetTimer = new QTimer(); + connect(ResetTimer, SIGNAL(timeout()), this, SLOT(slotResetTimer())); + ResetTimer->start(500); + StartTimer = new QTimer(); connect(StartTimer, SIGNAL(timeout()), this, SLOT(slotStartTimer())); StartTimer->start(500); @@ -38,6 +47,7 @@ void QTSGraph::Circle(int x, int y, int radius) { QPainter painter(&Canvas); painter.setPen(Pen); + painter.setBrush(Brush); painter.drawEllipse(x - radius, y - radius, radius * 2, radius * 2); update(); } @@ -67,7 +77,7 @@ void QTSGraph::PutPixel(int x, int y, QRgb c, int PenWidth) int QTSGraph::ReadKey() { - if(IDPressedKey == -1) + if(!EventKeyPressed || IDPressedKey == -1) { while(!KeyPressed()) Delay(100); @@ -81,6 +91,7 @@ void QTSGraph::Rectangle(int x1, int y1, int x2, int y2) { QPainter painter(&Canvas); painter.setPen(Pen); + painter.setBrush(Brush); painter.drawRect(x1, y1, x2 - x1, y2 - y1); update(); } @@ -90,6 +101,12 @@ void QTSGraph::SetColor(QRgb c) Pen.setColor(QColor(c)); } +void QTSGraph::SetFillStyle(int Pattern, QRgb Color) +{ + Brush.setStyle(Qt::BrushStyle(Pattern)); + Brush.setColor(QColor(Color)); +} + void QTSGraph::SetPenStyle(int PenWidth, int PenStyle) { Pen.setWidth(PenWidth); @@ -122,6 +139,12 @@ void QTSGraph::slotStartTimer() PaintBox(); } +void QTSGraph::slotResetTimer() +{ + EventKeyPressed = false; + EventMouseClicked = false; +} + void QTSGraph::paintEvent(QPaintEvent *event) { QPainter p(this); diff --git a/qtsgraph.h b/qtsgraph.h index 901ed2c..49c3877 100644 --- a/qtsgraph.h +++ b/qtsgraph.h @@ -11,6 +11,7 @@ #include #include #include +#include #define clRed 0x00FF0000 #define clGreen 0x0000FF00 @@ -28,6 +29,7 @@ public: void Circle(int x, int y, int radius); void Delay(int ms = 1000); + QRgb GetPixel(int x, int y); void Line(int x1, int y1, int x2, int y2); bool KeyPressed(); bool MouseClicked(); @@ -36,6 +38,27 @@ public: int ReadKey(); void Rectangle(int x1, int y1, int x2, int y2); void SetColor(QRgb c); + void SetFillStyle(int Pattern, QRgb Color); // Стиль и цыет заливки + /* + 0 - NoBrush + 1 - SolidPattern + 2 - Dense1Pattern + 3 - Dense2Pattern + 4 - Dense3Pattern + 5 - Dense4Pattern + 6 - Dense5Pattern + 7 - Dense6Pattern + 8 - Dense7Pattern + 9 - HorPattern + 10 - VerPattern + 11 - CrossPattern + 12 - BDiagPattern + 13 - FDiagPattern + 14 - DiagCrossPattern + 15 - LinearGradientPattern + 16 - RadialGradientPattern + 17 - ConicalGradientPattern + */ void SetPenStyle(int PenWidth, int PenStyle = 1); // Толщина и стиль линии /* 0 - NoPen @@ -49,14 +72,17 @@ public: private slots: void slotStartTimer(); + void slotResetTimer(); private: QPixmap Canvas; QTimer *StartTimer; + QTimer *ResetTimer; bool EventMouseClicked = false; bool EventKeyPressed = false; int IDPressedKey = -1; void PaintBox(); + QBrush Brush; QPen Pen; protected: