From 2c93aefe72efa0bfeb8428fd84f1be500f1f6fba 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: Tue, 5 Jan 2021 00:28:26 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A1=D1=82=D0=B8=D0=BB=D1=8C=20=D0=B8=20?= =?UTF-8?q?=D0=BD=D0=B0=D0=BF=D1=80=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D1=82=D0=B5=D0=BA=D1=81=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.cpp | 14 +++++++++++--- qtsgraph.cpp | 25 +++++++++++++++++++++++++ qtsgraph.h | 29 ++++++++++++++++++++--------- 3 files changed, 56 insertions(+), 12 deletions(-) diff --git a/main.cpp b/main.cpp index f1e25c9..bba62e0 100644 --- a/main.cpp +++ b/main.cpp @@ -11,7 +11,7 @@ int main(int argc, char *argv[]) * (int w = 640, int h = 480, int x = -1, int y = -1, QWidget *parent = nullptr) * В случае отрицательного значения x или y, окно создаётся в центре экрана. */ - QTSGraph w(800, 600); + QTSGraph w(600, 600); w.show(); return a.exec(); @@ -22,9 +22,17 @@ void QTSGraph::PaintBox() // Начало рисования SetColor(clGreen); - Line(0, 0, 800, 600); + Line(0, 0, 600, 600); SetColor(0xFF0000); - Line(800, 0, 0, 600); + Line(600, 0, 0, 600); + Rectangle(0,0,600,600); + SetColor(clBlue); + SetTextStyle(0, 45, 10); + OutTextXY(30, 30, "Hello world!"); + SetTextStyle(1, 0, 20); + OutTextXY(210, 50, "Hello world!"); + SetTextStyle(2, 0, 30); + OutTextXY(160, 550, "Hello world!"); // Конец рисования } diff --git a/qtsgraph.cpp b/qtsgraph.cpp index 75d60df..e452388 100644 --- a/qtsgraph.cpp +++ b/qtsgraph.cpp @@ -66,6 +66,7 @@ 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); + Font = QFont(); Brush = QBrush(QColor(DefaultColor),Qt::NoBrush); ResetInterval = 1000; ResetTimer = new QTimer(); @@ -102,7 +103,20 @@ void QTSGraph::OutTextXY(int x, int y, std::string caption) { QPainter painter(&Canvas); painter.setPen(Pen); + painter.setFont(Font); + double r, b, sa, ca, sb, cb, xn, yn; + b = TextDirection*3.14159/180; + r = sqrt(x * x + y * y); + sa = y / r; + ca = x / r; + sb = sin(b); + cb = cos(b); + xn = r * (ca * cb - sa * sb); + yn = r * (sa * cb + sb * ca); + painter.translate(x - xn, y - yn); + painter.rotate(TextDirection); painter.drawText(x, y, QString::fromStdString(caption)); + //painter.rotate(-TextDirection); update(); } @@ -157,6 +171,17 @@ void QTSGraph::SetPenWidth(int PenWidth) Pen.setWidth(PenWidth); } +void QTSGraph::SetTextStyle(int idFont, int Direction, int CharSize) +{ + QString f; + if(idFont == 0) f = "serif"; + else if(idFont == 1) f = "sans"; + else if(idFont == 3) f = "mono"; + TextDirection = Direction; + Font.setFamily(f); + Font.setPointSize(CharSize); +} + void QTSGraph::Line(int x1, int y1, int x2, int y2) { QPainter painter(&Canvas); diff --git a/qtsgraph.h b/qtsgraph.h index 60cb508..d48d7c2 100644 --- a/qtsgraph.h +++ b/qtsgraph.h @@ -50,6 +50,7 @@ along with Vesi. If not, see . #include #include #include +#include #define clRed 0x00FF0000 #define clGreen 0x0000FF00 @@ -110,26 +111,36 @@ public: 5 - DashDotDotLine */ void SetPenWidth(int PenWidth); + void SetTextStyle(int idFont, int Direction, int CharSize); + /* + 0 - serif + 1 - sans + 2 - mono + */ private slots: - void slotStartTimer(); void slotResetTimer(); + void slotStartTimer(); private: - QPixmap Canvas; - QTimer *StartTimer; - QTimer *ResetTimer; bool EventMouseClicked = false; bool EventKeyPressed = false; int IDPressedKey = -1; - void PaintBox(); + int ResetInterval; + int TextDirection = 0; + QBrush Brush; + QPixmap Canvas; + QFont Font; QPen Pen; - int ResetInterval; + QTimer *ResetTimer; + QTimer *StartTimer; + + void PaintBox(); protected: - void paintEvent(QPaintEvent *event) override; - void mousePressEvent(QMouseEvent *event) override; - void keyPressEvent(QKeyEvent *event) override; void closeEvent(QCloseEvent *event) override; + void keyPressEvent(QKeyEvent *event) override; + void mousePressEvent(QMouseEvent *event) override; + void paintEvent(QPaintEvent *event) override; };