From a91a2886d1ebb6b477ca2b97f4e634d366299ea6 Mon Sep 17 00:00:00 2001 From: Eremey Remzin Date: Sun, 14 Nov 2021 15:30:34 +0300 Subject: [PATCH 01/19] client was added --- CMakeLists.txt | 2 ++ project/client/CMakeLists.txt | 2 ++ project/client/src/main.cpp | 7 +++++++ 3 files changed, 11 insertions(+) create mode 100644 project/client/CMakeLists.txt create mode 100644 project/client/src/main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index d2919ae..57f58a0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,4 +7,6 @@ set(CMAKE_CXX_STANDARD 20) file(GLOB_RECURSE HEADER_FILES project/include/*.h) file(GLOB_RECURSE SOURCE_FILES project/src/*.cpp) +add_subdirectory(project/client) + add_executable(main ${HEADER_FILES} ${SOURCE_FILES}) \ No newline at end of file diff --git a/project/client/CMakeLists.txt b/project/client/CMakeLists.txt new file mode 100644 index 0000000..57ce013 --- /dev/null +++ b/project/client/CMakeLists.txt @@ -0,0 +1,2 @@ +add_executable(client src/main.cpp) + diff --git a/project/client/src/main.cpp b/project/client/src/main.cpp new file mode 100644 index 0000000..cbb891c --- /dev/null +++ b/project/client/src/main.cpp @@ -0,0 +1,7 @@ +// +// Created by eremey on 14.11.2021. +// + +int main() { + return 0; +} \ No newline at end of file From e68ec34aa9e87c43d2b5d3b67cdc321c50a4138b Mon Sep 17 00:00:00 2001 From: Eremey Remzin Date: Sun, 14 Nov 2021 15:40:34 +0300 Subject: [PATCH 02/19] QT was added --- project/client/CMakeLists.txt | 16 ++++++++++++++++ project/client/src/main.cpp | 12 ++++++------ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/project/client/CMakeLists.txt b/project/client/CMakeLists.txt index 57ce013..6682241 100644 --- a/project/client/CMakeLists.txt +++ b/project/client/CMakeLists.txt @@ -1,2 +1,18 @@ +set(CMAKE_AUTOMOC ON) +set(CMAKE_AUTORCC ON) +set(CMAKE_AUTOUIC ON) + +find_package(Qt5 COMPONENTS + Core + Gui + Widgets + REQUIRED) + add_executable(client src/main.cpp) +target_link_libraries(client + Qt5::Core + Qt5::Gui + Qt5::Widgets + ) + diff --git a/project/client/src/main.cpp b/project/client/src/main.cpp index cbb891c..150cc35 100644 --- a/project/client/src/main.cpp +++ b/project/client/src/main.cpp @@ -1,7 +1,7 @@ -// -// Created by eremey on 14.11.2021. -// +#include -int main() { - return 0; -} \ No newline at end of file +int main(int argc, char *argv[]) { + QApplication a(argc, argv); + + return QApplication::exec(); +} From c602bd1904e2f861427f0f4d48f2b405b41163a9 Mon Sep 17 00:00:00 2001 From: Eremey Remzin Date: Sun, 14 Nov 2021 16:11:56 +0300 Subject: [PATCH 03/19] class MainWindow was added --- project/client/CMakeLists.txt | 3 +++ project/client/src/main.cpp | 2 ++ project/client/widgets/main_window/CMakeLists.txt | 2 ++ project/client/widgets/main_window/include/MainWindow.h | 7 +++++++ project/client/widgets/main_window/src/MainWindow.cpp | 4 ++++ 5 files changed, 18 insertions(+) create mode 100644 project/client/widgets/main_window/CMakeLists.txt create mode 100644 project/client/widgets/main_window/include/MainWindow.h create mode 100644 project/client/widgets/main_window/src/MainWindow.cpp diff --git a/project/client/CMakeLists.txt b/project/client/CMakeLists.txt index 6682241..0b19c8b 100644 --- a/project/client/CMakeLists.txt +++ b/project/client/CMakeLists.txt @@ -15,4 +15,7 @@ target_link_libraries(client Qt5::Widgets ) +add_subdirectory(widgets/main_window) +target_link_libraries(client MainWindow) +target_include_directories(client PUBLIC widgets/main_window/include) diff --git a/project/client/src/main.cpp b/project/client/src/main.cpp index 150cc35..575c2a8 100644 --- a/project/client/src/main.cpp +++ b/project/client/src/main.cpp @@ -1,3 +1,5 @@ +#include "MainWindow.h" + #include int main(int argc, char *argv[]) { diff --git a/project/client/widgets/main_window/CMakeLists.txt b/project/client/widgets/main_window/CMakeLists.txt new file mode 100644 index 0000000..4192827 --- /dev/null +++ b/project/client/widgets/main_window/CMakeLists.txt @@ -0,0 +1,2 @@ +add_library(MainWindow SHARED src/MainWindow.cpp) +target_include_directories(MainWindow PUBLIC include) \ No newline at end of file diff --git a/project/client/widgets/main_window/include/MainWindow.h b/project/client/widgets/main_window/include/MainWindow.h new file mode 100644 index 0000000..6d55678 --- /dev/null +++ b/project/client/widgets/main_window/include/MainWindow.h @@ -0,0 +1,7 @@ +// +// Created by eremey on 14.11.2021. +// +#pragma once + +class MainWindow{ +}; \ No newline at end of file diff --git a/project/client/widgets/main_window/src/MainWindow.cpp b/project/client/widgets/main_window/src/MainWindow.cpp new file mode 100644 index 0000000..57085c6 --- /dev/null +++ b/project/client/widgets/main_window/src/MainWindow.cpp @@ -0,0 +1,4 @@ +// +// Created by eremey on 14.11.2021. +// +#include "MainWindow.h" From 2162cf9f0772b71e4972807911fb6e3a015f286a Mon Sep 17 00:00:00 2001 From: Eremey Remzin Date: Sun, 14 Nov 2021 17:06:11 +0300 Subject: [PATCH 04/19] class MainWindow was changed --- project/client/CMakeLists.txt | 8 +++----- project/client/MainWindow.cpp | 6 ++++++ project/client/include/MainWindow.h | 12 ++++++++++++ project/client/src/main.cpp | 11 ++++++++--- project/client/widgets/main_window/CMakeLists.txt | 2 -- .../client/widgets/main_window/include/MainWindow.h | 7 ------- .../client/widgets/main_window/src/MainWindow.cpp | 4 ---- 7 files changed, 29 insertions(+), 21 deletions(-) create mode 100644 project/client/MainWindow.cpp create mode 100644 project/client/include/MainWindow.h delete mode 100644 project/client/widgets/main_window/CMakeLists.txt delete mode 100644 project/client/widgets/main_window/include/MainWindow.h delete mode 100644 project/client/widgets/main_window/src/MainWindow.cpp diff --git a/project/client/CMakeLists.txt b/project/client/CMakeLists.txt index 0b19c8b..a39ddec 100644 --- a/project/client/CMakeLists.txt +++ b/project/client/CMakeLists.txt @@ -8,14 +8,12 @@ find_package(Qt5 COMPONENTS Widgets REQUIRED) -add_executable(client src/main.cpp) + +add_executable(client src/main.cpp MainWindow.cpp include/MainWindow.h) target_link_libraries(client Qt5::Core Qt5::Gui Qt5::Widgets ) -add_subdirectory(widgets/main_window) -target_link_libraries(client MainWindow) -target_include_directories(client PUBLIC widgets/main_window/include) - +target_include_directories(client PUBLIC include) diff --git a/project/client/MainWindow.cpp b/project/client/MainWindow.cpp new file mode 100644 index 0000000..d94dcc0 --- /dev/null +++ b/project/client/MainWindow.cpp @@ -0,0 +1,6 @@ +#include "MainWindow.h" + +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent) { + +} \ No newline at end of file diff --git a/project/client/include/MainWindow.h b/project/client/include/MainWindow.h new file mode 100644 index 0000000..54fe40c --- /dev/null +++ b/project/client/include/MainWindow.h @@ -0,0 +1,12 @@ +#pragma once + +#include +#include + +class MainWindow : public QMainWindow { + + Q_OBJECT + +public: + MainWindow(QWidget *parent = 0); +}; \ No newline at end of file diff --git a/project/client/src/main.cpp b/project/client/src/main.cpp index 575c2a8..fb1ced9 100644 --- a/project/client/src/main.cpp +++ b/project/client/src/main.cpp @@ -3,7 +3,12 @@ #include int main(int argc, char *argv[]) { - QApplication a(argc, argv); + QApplication app(argc, argv); - return QApplication::exec(); -} + MainWindow window; + window.resize(800, 800); + window.setWindowTitle("DeepPic"); + window.showMaximized(); + + return app.exec(); +} \ No newline at end of file diff --git a/project/client/widgets/main_window/CMakeLists.txt b/project/client/widgets/main_window/CMakeLists.txt deleted file mode 100644 index 4192827..0000000 --- a/project/client/widgets/main_window/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -add_library(MainWindow SHARED src/MainWindow.cpp) -target_include_directories(MainWindow PUBLIC include) \ No newline at end of file diff --git a/project/client/widgets/main_window/include/MainWindow.h b/project/client/widgets/main_window/include/MainWindow.h deleted file mode 100644 index 6d55678..0000000 --- a/project/client/widgets/main_window/include/MainWindow.h +++ /dev/null @@ -1,7 +0,0 @@ -// -// Created by eremey on 14.11.2021. -// -#pragma once - -class MainWindow{ -}; \ No newline at end of file diff --git a/project/client/widgets/main_window/src/MainWindow.cpp b/project/client/widgets/main_window/src/MainWindow.cpp deleted file mode 100644 index 57085c6..0000000 --- a/project/client/widgets/main_window/src/MainWindow.cpp +++ /dev/null @@ -1,4 +0,0 @@ -// -// Created by eremey on 14.11.2021. -// -#include "MainWindow.h" From 6572a619d496845f63a87e1169622b4a05f35444 Mon Sep 17 00:00:00 2001 From: Eremey Remzin Date: Tue, 16 Nov 2021 20:09:31 +0300 Subject: [PATCH 05/19] dirty realisation was added --- brush.png | Bin 0 -> 1479 bytes project/client/CMakeLists.txt | 10 +- project/client/MainWindow.cpp | 6 -- project/client/include/Changer.h | 31 ++++++ project/client/include/MainWindow.h | 37 +++++-- project/client/include/PaintScene.h | 62 ++++++++++++ project/client/include/Palette.h | 32 ++++++ project/client/src/Changer.cpp | 46 +++++++++ project/client/src/MainWindow.cpp | 150 ++++++++++++++++++++++++++++ project/client/src/PaintScene.cpp | 87 ++++++++++++++++ project/client/src/Palette.cpp | 38 +++++++ project/client/src/main.cpp | 13 ++- 12 files changed, 489 insertions(+), 23 deletions(-) create mode 100644 brush.png delete mode 100644 project/client/MainWindow.cpp create mode 100644 project/client/include/Changer.h create mode 100644 project/client/include/PaintScene.h create mode 100644 project/client/include/Palette.h create mode 100644 project/client/src/Changer.cpp create mode 100644 project/client/src/MainWindow.cpp create mode 100644 project/client/src/PaintScene.cpp create mode 100644 project/client/src/Palette.cpp diff --git a/brush.png b/brush.png new file mode 100644 index 0000000000000000000000000000000000000000..db8c9ed872e47776c594ddfaf4605b43f3138322 GIT binary patch literal 1479 zcmV;&1vvVNP)D2k#eilVd|i<%ukgSiH{1egLo07jY{PWw;+wgWR& z_6pG1#JJjm3b32`;%7F5CLtOXU>6IEpV=^QLX%=l7FY2#2@#NruSp1qulSmT0MskK zCZP^OiLXiUAhh_J1Oc(c*CaR)TYODI0gV!0lTd?3i?2z@(Jb*b3D_6bs%=v6TZwI_ z4HxjF;tv2_z{`R%nuG_s;B@vaTjHC4Ih%8qjN%zdRl&-&jdcS zC(IH`{8KS|z%dh$ToOWw53pX~N0Wej%zw(_?{k5#5LW6rLE7^BE&k4P@bNqFBJc<> z2uusEpHxo>Ek3}d1?9(p+kq3S%Dg0qp~zkp)xOEZ5I^I7%rJBScpKOVtSb2Z8-h4& zc9kH`rV&$oDQgIO$F#|I!H&>15R#o(yIN4@&q%SH3_M3zP5ue!D>(J6YHkP|O9$|# zXDmf_S>QN>5n@R>WD*{)GnQI57%;w|gj~-Lk%T_N+hO77$&sd`KDDD;M2f8s2ORuX3PBre2sY${!MVbmKPsj4}Rij#2$|F3<$2XixP+r z_C48J+lbXPA_+-~4`%tGs4{0d`iVUpB`H3bmB^wB?E`uoeRTsLS^VYgVF#a^1peL! zPI8R3Wq2KX#hR6b~J?$2xqp zt@t-#PX79cuot*u*3VW?A3m1MgnKuBcQ~WKl{r5bJC^$p*`cmlHbNpT|6b&k{}{6u zP|Xg9viq1@h}+`~X2qXYN*^q+z2E&i@`@bj+4M<-^Ze-QWh4DHfi9R{;A9%I0lbv>GJ9mlTV)pZ_PJ7c(&*v6Rc~5IXb}vIV<{$APbKdtl4k zznD!OrGQn$IaV^o!Yidp6~ly2z&#DELCIN;|Mp|8;p)D|hMxv~)c+~3Nz7id;z=L= zJz*5nlaxAHj+ql&C*}jdowVQI=0;~V@GS5J^M|H!;CbK%n*UM2mM28Z>|$;|;E!2T h6h%=KMNyOj{sTYn$66$WT4w+N002ovPDHLkV1gt%wlDwy literal 0 HcmV?d00001 diff --git a/project/client/CMakeLists.txt b/project/client/CMakeLists.txt index a39ddec..e6983a6 100644 --- a/project/client/CMakeLists.txt +++ b/project/client/CMakeLists.txt @@ -8,8 +8,16 @@ find_package(Qt5 COMPONENTS Widgets REQUIRED) +set(WITH_SANITIZER ON) +if (WITH_SANITIZER) + add_compile_options(-fsanitize=address) + add_link_options(-fsanitize=address) +endif () -add_executable(client src/main.cpp MainWindow.cpp include/MainWindow.h) +set(SOURCES src/main.cpp src/MainWindow.cpp src/PaintScene.cpp src/Palette.cpp src/Changer.cpp) +set(HEADER_FILES include/Palette.h include/MainWindow.h include/PaintScene.h include/Changer.h) + +add_executable(client ${SOURCES} ${HEADER_FILES}) target_link_libraries(client Qt5::Core Qt5::Gui diff --git a/project/client/MainWindow.cpp b/project/client/MainWindow.cpp deleted file mode 100644 index d94dcc0..0000000 --- a/project/client/MainWindow.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include "MainWindow.h" - -MainWindow::MainWindow(QWidget *parent) - : QMainWindow(parent) { - -} \ No newline at end of file diff --git a/project/client/include/Changer.h b/project/client/include/Changer.h new file mode 100644 index 0000000..7ad82eb --- /dev/null +++ b/project/client/include/Changer.h @@ -0,0 +1,31 @@ +// +// Created by eremey on 16.11.2021. +// +#pragma once +#include + +#include +#include + +class Changer : public QToolBar +{ + Q_OBJECT + +public: + explicit Changer(QString name = "", int value = 30, int max_value = 255, QWidget *parent = 0); + ~Changer() = default; + +private: + int _value; + + QSpinBox *spin_box; + QSlider *slider; + +public: + +signals: + void valueChanged(int value); + +private slots: + void valueChangedSlot(int value); +}; \ No newline at end of file diff --git a/project/client/include/MainWindow.h b/project/client/include/MainWindow.h index 54fe40c..2a30f17 100644 --- a/project/client/include/MainWindow.h +++ b/project/client/include/MainWindow.h @@ -1,12 +1,31 @@ #pragma once - +#include +#include +#include #include -#include - -class MainWindow : public QMainWindow { - - Q_OBJECT - +#include + +#include "PaintScene.h" + +class MainWindow : public QMainWindow +{ +Q_OBJECT + public: - MainWindow(QWidget *parent = 0); -}; \ No newline at end of file + explicit MainWindow(QWidget *parent = 0); + ~MainWindow(); + +signals: + void TemporarySignal(const Curve& curve); + +private: + PaintScene *scene; + QGraphicsView* qGraphicsView; + + QToolBar *parameters_panel; + +private: + +private slots: + void slotBrush(); +}; diff --git a/project/client/include/PaintScene.h b/project/client/include/PaintScene.h new file mode 100644 index 0000000..530343e --- /dev/null +++ b/project/client/include/PaintScene.h @@ -0,0 +1,62 @@ +#pragma once + +#include +#include +#include +#include +#include + +struct point { + int32_t i = 0; + int32_t j = 0; +}; + +struct Curve { + int32_t brush_size = 0; + int32_t color_red = 0; + int32_t color_green = 0; + int32_t color_blue = 0; + + std::vector coords; +}; + +class PaintScene : public QGraphicsScene +{ + + Q_OBJECT + +public: + explicit PaintScene(QObject *parent = 0); + ~PaintScene() = default; + + void ChangeBrushStatus(); + bool BrushStatus(); + void SetBrushSize(qreal brush_size = 10); + void SetBrush(qreal size, int red, int green, int blue, int opacity); + +public slots: + void SetBrushSizeSlot(int size); + void SetTransparencySlot(int transparency); + void SetRedSlot(int transparency); + void SetGreenSlot(int transparency); + void SetBlueSlot(int transparency); + + void PaintCurveSlot(const Curve& curve); + +private: + bool is_brush = false; + + QPointF previous_point; + + std::vector line; + + qreal brush_size = 10; + + QColor brush_color; + +private: + void mousePressEvent(QGraphicsSceneMouseEvent * event); + void mouseMoveEvent(QGraphicsSceneMouseEvent *event); + void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); + +}; diff --git a/project/client/include/Palette.h b/project/client/include/Palette.h new file mode 100644 index 0000000..caf3223 --- /dev/null +++ b/project/client/include/Palette.h @@ -0,0 +1,32 @@ +// +// Created by eremey on 16.11.2021. +// +#pragma once +#include +#include +#include + +class Palette : public QLabel +{ + Q_OBJECT + +public: + explicit Palette(QWidget *parent = 0); + ~Palette() = default; + + void SetColor(int red, int green, int blue, int opacity = 255); + + //void SetGreen(int value); +private: + QColor color; + //QPixmap *pixmap; + +public slots: + void SetRed(int value); + void SetGreen(int value); + void SetBlue(int value); + +private slots: + //void slotTimer(); + //void slotBrush(); +}; \ No newline at end of file diff --git a/project/client/src/Changer.cpp b/project/client/src/Changer.cpp new file mode 100644 index 0000000..1db0493 --- /dev/null +++ b/project/client/src/Changer.cpp @@ -0,0 +1,46 @@ +// +// Created by eremey on 16.11.2021. +// +#include "Changer.h" + +#include +#include + +#include + +Changer::Changer(QString name, int value, int max_value, QWidget *parent) : + QToolBar(parent) { + + QLabel *size_label = new QLabel(name, this); + this->addWidget(size_label); + size_label->setFixedWidth(name.length() * 9); + + spin_box = new QSpinBox; + spin_box->setValue(value); + spin_box->setMaximum(max_value); + spin_box->setFixedWidth(50); + this->addWidget(spin_box); + + slider = new QSlider(Qt::Horizontal); + slider->setFixedWidth(100); + slider->setValue(value); + slider->setMaximum(max_value); + this->addWidget(slider); + + this->setFixedWidth(size_label->width() + spin_box->width() + slider->width() + 20); + std::cout << size_label->width() << " " << spin_box->width() << " " << slider->width(); + + connect(slider, QOverload::of(&QSlider::valueChanged), this, + (&Changer::valueChangedSlot)); + connect(spin_box, QOverload::of(&QSpinBox::valueChanged), this, + (&Changer::valueChangedSlot)); + + this->setStyleSheet(QString("QToolBar {spacing: %1}").arg(5)); + + +} +void Changer::valueChangedSlot(int value) { + _value = value; + spin_box->value() == value ? slider->setValue(value) : spin_box->setValue(value); + emit valueChanged(value); +} diff --git a/project/client/src/MainWindow.cpp b/project/client/src/MainWindow.cpp new file mode 100644 index 0000000..93209d7 --- /dev/null +++ b/project/client/src/MainWindow.cpp @@ -0,0 +1,150 @@ +#include "MainWindow.h" +#include "QGraphicsView" +#include "Palette.h" +#include "Changer.h" + +#include +#include +#include +#include +#include +#include +#include + +#include + +MainWindow::MainWindow(QWidget *parent) : + QMainWindow(parent) { + + scene = new PaintScene(this); // Инициализируем графическую сцену + qGraphicsView = new QGraphicsView; + qGraphicsView->setScene(scene); + setCentralWidget(qGraphicsView); + + // MenuBar + QAction *file_open = new QAction("&Open", this); + QAction *file_close = new QAction("&Close", this); + QAction *file_save = new QAction("&Save", this); + QAction *file_save_as = new QAction("&Save as", this); + QMenu *file; + file = menuBar()->addMenu("File"); + file->addAction(file_open); + file->addAction(file_close); + file->addAction(file_save); + file->addAction(file_save_as); + + QAction *edit_undo = new QAction("&Undo", this); + QAction *edit_redo = new QAction("&Redo", this); + QMenu *edit; + edit = menuBar()->addMenu("Edit"); + edit->addAction(edit_undo); + edit->addAction(edit_redo); + + QAction *help_help = new QAction("&Help", this); + QMenu *help; + help = menuBar()->addMenu("Help"); + help->addAction(help_help); + + // ToolBars + parameters_panel = new QToolBar; + addToolBar(parameters_panel); + parameters_panel->setFixedHeight(40); + + QToolBar *right_panel = new QToolBar; + addToolBar(Qt::RightToolBarArea, right_panel); + right_panel->setFixedWidth(400); + + + QPixmap brush_pm("brush.png"); + //QAction *brush = new QAction(QIcon(brush_pm), "&Brush", this); + QToolBar *tools_panel = new QToolBar; + addToolBar(Qt::LeftToolBarArea, tools_panel); + QAction *brush = tools_panel->addAction(QIcon(brush_pm), "Brush"); + tools_panel->addAction(brush); + connect(brush, &QAction::triggered, this, &MainWindow::slotBrush); + connect(this, &MainWindow::TemporarySignal, scene, &PaintScene::PaintCurveSlot); + + + // Status bar + statusBar()->showMessage("Status bar"); + +// timer = new QTimer(); +// //connect(timer, &QTimer::timeout, this, &MainWindow::slotTimer); +// timer->start(100); +} + +MainWindow::~MainWindow() { +// free(qGraphicsView); +} + +//void MainWindow::slotTimer() +//{ +// timer->stop(); +// scene->setSceneRect(0,0, qGraphicsView->width() - 20, qGraphicsView->height() - 20); +//} + +void MainWindow::slotBrush() { + if (scene->BrushStatus()) { + parameters_panel->clear(); + } else { + scene->SetBrush(30, 0, 180, 40, 255); + scene->setSceneRect(0, 0, qGraphicsView->width() - 20, qGraphicsView->height() - 20); + + QWidget *dummy = new QWidget(); + dummy->setFixedWidth(40); + parameters_panel->addWidget(dummy); + + Changer *size_changer = new Changer("Size", 30, 500, this); + connect(size_changer, QOverload::of(&Changer::valueChanged), scene, &PaintScene::SetBrushSize); + parameters_panel->addWidget(size_changer); + parameters_panel->addSeparator(); + + + Palette *palette = new Palette; + palette->SetColor(0, 180, 40, 255); + + Changer *opacity_changer = new Changer("Opacity", 255, 255, this); + connect(opacity_changer, QOverload::of(&Changer::valueChanged), scene, + (&PaintScene::SetTransparencySlot)); + + + Changer *red_changer = new Changer("Red", 0, 255, this); + connect(red_changer, QOverload::of(&Changer::valueChanged), scene, + (&PaintScene::SetRedSlot)); + connect(red_changer, QOverload::of(&Changer::valueChanged), palette, + (&Palette::SetRed)); + parameters_panel->addWidget(red_changer); + + Changer *green_changer = new Changer("Green", 180, 255, this); + connect(green_changer, QOverload::of(&Changer::valueChanged), scene, + (&PaintScene::SetGreenSlot)); + connect(green_changer, QOverload::of(&Changer::valueChanged), palette, + (&Palette::SetGreen)); + parameters_panel->addWidget(green_changer); + + Changer *blue_changer = new Changer("Blue", 40, 255, this); + connect(blue_changer, QOverload::of(&Changer::valueChanged), scene, + (&PaintScene::SetBlueSlot)); + connect(blue_changer, QOverload::of(&Changer::valueChanged), palette, + (&Palette::SetBlue)); + parameters_panel->addWidget(blue_changer); + + +// Palette palette; + parameters_panel->addWidget(palette); + parameters_panel->addSeparator(); + + parameters_panel->addWidget(opacity_changer); + + + parameters_panel->setStyleSheet(QString("QToolBar {spacing: %1}").arg(10)); + } + scene->ChangeBrushStatus(); + emit(TemporarySignal({20,100,100,100,{{0,0},{50,50}}})); +} + +//void MainWindow::resizeEvent(QResizeEvent *event) +//{ +// timer->start(100); +// QWidget::resizeEvent(event); +//} diff --git a/project/client/src/PaintScene.cpp b/project/client/src/PaintScene.cpp new file mode 100644 index 0000000..62614ad --- /dev/null +++ b/project/client/src/PaintScene.cpp @@ -0,0 +1,87 @@ +#include "PaintScene.h" +#include + +void PaintScene::mousePressEvent(QGraphicsSceneMouseEvent *event) { + if (is_brush) { + addEllipse(event->scenePos().x() - brush_size / 2, + event->scenePos().y() - brush_size / 2, + brush_size, + brush_size, + QPen(Qt::NoPen), + QBrush(brush_color)); + previous_point = event->scenePos(); + line.push_back(previous_point); + } + +} + +void PaintScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { + if (is_brush) { + addLine(previous_point.x(), + previous_point.y(), + event->scenePos().x(), + event->scenePos().y(), + QPen(brush_color, brush_size, Qt::SolidLine, Qt::RoundCap)); + previous_point = event->scenePos(); + line.push_back(previous_point); + } +} + + +void PaintScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { + std::cout << line.size() << std::endl; + line.clear(); +} +void PaintScene::ChangeBrushStatus() { + is_brush = !is_brush; +} +PaintScene::PaintScene(QObject *parent) { + brush_color.setRgb(0,255,0); + +} +void PaintScene::SetBrushSize(qreal brush_size) { + this->brush_size = brush_size; +} +bool PaintScene::BrushStatus() { + return is_brush; +} +void PaintScene::SetBrushSizeSlot(int size) { + brush_size = size; +} +void PaintScene::SetTransparencySlot(int value) { + assert(value < 256 && value >= 0); + brush_color.setAlpha(value); +} +void PaintScene::SetRedSlot(int value) { + assert(value < 256 && value >= 0); + brush_color.setRed(value); +} + +void PaintScene::SetGreenSlot(int value) { + assert(value < 256 && value >= 0); + brush_color.setGreen(value); +} + +void PaintScene::SetBlueSlot(int value) { + assert(value < 256 && value >= 0); + brush_color.setBlue(value); +} +void PaintScene::SetBrush(qreal size, int red, int green, int blue, int opacity) { + brush_size = size; + brush_color.setRgb(red, green, blue, opacity); +} +void PaintScene::PaintCurveSlot(const Curve &curve) { + assert(!curve.coords.empty()); + assert(curve.color_red < 256 && curve.color_red >= 0); + assert(curve.color_green < 256 && curve.color_green >= 0); + assert(curve.color_blue < 256 && curve.color_blue >= 0); + + for (int i = 0; i < curve.coords.size() - 2; ++i) { + addLine(curve.coords[i].i, + curve.coords[i].j, + curve.coords[i + 1].i, + curve.coords[i + 1].j, + QPen(QColor(curve.color_red, curve.color_green, curve.color_blue), + curve.brush_size, Qt::SolidLine, Qt::RoundCap)); + } +} diff --git a/project/client/src/Palette.cpp b/project/client/src/Palette.cpp new file mode 100644 index 0000000..5daf887 --- /dev/null +++ b/project/client/src/Palette.cpp @@ -0,0 +1,38 @@ +// +// Created by eremey on 16.11.2021. +// +#include "Palette.h" +#include + +Palette::Palette(QWidget *parent) : + QLabel(parent), color(0,0,0,255) { + SetRed(255); +} +void Palette::SetGreen(int value) { + color.setGreen(value); + QPixmap pixmap(15, 15); + pixmap.fill( color ); + + this->setPixmap(pixmap); +} +void Palette::SetRed(int value) { + color.setRed(value); + QPixmap pixmap(15, 15); + pixmap.fill( color ); + + this->setPixmap(pixmap); +} +void Palette::SetBlue(int value) { + color.setBlue(value); + QPixmap pixmap(15, 15); + pixmap.fill( color ); + + this->setPixmap(pixmap); +} +void Palette::SetColor(int red, int green, int blue, int opacity) { + color.setRgb(red, green, blue, opacity); + QPixmap pixmap(15, 15); + pixmap.fill( color ); + + this->setPixmap(pixmap); +} diff --git a/project/client/src/main.cpp b/project/client/src/main.cpp index fb1ced9..5d259ae 100644 --- a/project/client/src/main.cpp +++ b/project/client/src/main.cpp @@ -2,13 +2,12 @@ #include -int main(int argc, char *argv[]) { - QApplication app(argc, argv); - +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); MainWindow window; - window.resize(800, 800); + window.setMinimumSize(500, 500); window.setWindowTitle("DeepPic"); window.showMaximized(); - - return app.exec(); -} \ No newline at end of file + return a.exec(); +} From 7295b385e0a2b1bb2f0003dd6269b3f3a53b6d82 Mon Sep 17 00:00:00 2001 From: Eremey Remzin Date: Thu, 18 Nov 2021 15:00:25 +0300 Subject: [PATCH 06/19] temporary reader/writer were added --- project/client/include/MainWindow.h | 7 ++- project/client/include/PaintScene.h | 9 ++-- project/client/src/MainWindow.cpp | 76 +++++++++++++++++++++++++---- project/client/src/PaintScene.cpp | 16 +++--- 4 files changed, 88 insertions(+), 20 deletions(-) diff --git a/project/client/include/MainWindow.h b/project/client/include/MainWindow.h index 2a30f17..3d77e98 100644 --- a/project/client/include/MainWindow.h +++ b/project/client/include/MainWindow.h @@ -16,7 +16,7 @@ Q_OBJECT ~MainWindow(); signals: - void TemporarySignal(const Curve& curve); + void TemporarySignal(const Curve& curve); /// private: PaintScene *scene; @@ -24,8 +24,13 @@ Q_OBJECT QToolBar *parameters_panel; + QTimer *timer; + private: + long temporary_read_position = 0; private slots: void slotBrush(); + void slotTimer(); + void TemporaryWriterSlot(const Curve& curve); }; diff --git a/project/client/include/PaintScene.h b/project/client/include/PaintScene.h index 530343e..d19e01f 100644 --- a/project/client/include/PaintScene.h +++ b/project/client/include/PaintScene.h @@ -6,18 +6,18 @@ #include #include -struct point { +struct Point { int32_t i = 0; int32_t j = 0; }; struct Curve { - int32_t brush_size = 0; + double brush_size = 0; int32_t color_red = 0; int32_t color_green = 0; int32_t color_blue = 0; - std::vector coords; + std::vector coords; }; class PaintScene : public QGraphicsScene @@ -34,6 +34,9 @@ class PaintScene : public QGraphicsScene void SetBrushSize(qreal brush_size = 10); void SetBrush(qreal size, int red, int green, int blue, int opacity); +signals: + void PushCurve(const Curve& curve); + public slots: void SetBrushSizeSlot(int size); void SetTransparencySlot(int transparency); diff --git a/project/client/src/MainWindow.cpp b/project/client/src/MainWindow.cpp index 93209d7..010c34a 100644 --- a/project/client/src/MainWindow.cpp +++ b/project/client/src/MainWindow.cpp @@ -11,6 +11,11 @@ #include #include +// Temporary +#include + + + #include MainWindow::MainWindow(QWidget *parent) : @@ -63,25 +68,67 @@ MainWindow::MainWindow(QWidget *parent) : tools_panel->addAction(brush); connect(brush, &QAction::triggered, this, &MainWindow::slotBrush); connect(this, &MainWindow::TemporarySignal, scene, &PaintScene::PaintCurveSlot); + connect(scene, &PaintScene::PushCurve, this, &MainWindow::TemporaryWriterSlot); // Status bar statusBar()->showMessage("Status bar"); -// timer = new QTimer(); -// //connect(timer, &QTimer::timeout, this, &MainWindow::slotTimer); -// timer->start(100); + timer = new QTimer(); + connect(timer, &QTimer::timeout, this, &MainWindow::slotTimer); + timer->start(500); } MainWindow::~MainWindow() { // free(qGraphicsView); } -//void MainWindow::slotTimer() -//{ -// timer->stop(); -// scene->setSceneRect(0,0, qGraphicsView->width() - 20, qGraphicsView->height() - 20); -//} +void MainWindow::slotTimer() { + std::ifstream isf ("House.txt"); + Curve curve; + int i = 0; + while (i < 1) { + isf >> curve.brush_size >> curve.color_red >> curve.color_green >> curve.color_blue; + + size_t curve_size = 0; + isf >> curve_size; + + QPointF point; + qreal point_x = 0; + qreal point_y = 0; + + for (size_t i = 0; i < curve_size; ++i) { + + isf >> point_x >> point_y; + point.setX(point_x); + point.setY(point_y); + + curve.coords.push_back(point); + //std::cout << point.j << " " << point.i << std::endl; + } + temporary_read_position = isf.tellg(); + emit(TemporarySignal(curve)); + i++; + } +// isf >> curve.brush_size >> curve.color_red >> curve.color_green >> curve.color_blue; +// +// size_t curve_size = 0; +// isf >> curve_size; +// +// Point point; +// for (size_t i = 0; i < curve_size; ++i) { +// isf >> point.i >> point.j; +// curve.coords.push_back(point); +// } +// temporary_read_position = isf.tellg(); + + isf.close(); + + + std::cout << "slotTimer\n"; + timer->stop(); + emit(TemporarySignal(curve)); +} void MainWindow::slotBrush() { if (scene->BrushStatus()) { @@ -140,7 +187,18 @@ void MainWindow::slotBrush() { parameters_panel->setStyleSheet(QString("QToolBar {spacing: %1}").arg(10)); } scene->ChangeBrushStatus(); - emit(TemporarySignal({20,100,100,100,{{0,0},{50,50}}})); +} +void MainWindow::TemporaryWriterSlot(const Curve &curve) { + std::ofstream outf("House.txt", std::ios::app); + + outf << curve.brush_size << " " << curve.color_red << " " << curve.color_green << " " << curve.color_blue << " " << + curve.coords.size() << std::endl; + for (auto& point: curve.coords) { + outf << point.x() << " " << point.y() << " "; + } + outf << std::endl; + + outf.close(); } //void MainWindow::resizeEvent(QResizeEvent *event) diff --git a/project/client/src/PaintScene.cpp b/project/client/src/PaintScene.cpp index 62614ad..7cfcfcf 100644 --- a/project/client/src/PaintScene.cpp +++ b/project/client/src/PaintScene.cpp @@ -29,7 +29,8 @@ void PaintScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { void PaintScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { - std::cout << line.size() << std::endl; + PushCurve({brush_size, brush_color.red(), brush_color.green(), brush_color.blue(), + line}); line.clear(); } void PaintScene::ChangeBrushStatus() { @@ -71,16 +72,17 @@ void PaintScene::SetBrush(qreal size, int red, int green, int blue, int opacity) brush_color.setRgb(red, green, blue, opacity); } void PaintScene::PaintCurveSlot(const Curve &curve) { - assert(!curve.coords.empty()); + assert(curve.coords.size() > 1); assert(curve.color_red < 256 && curve.color_red >= 0); assert(curve.color_green < 256 && curve.color_green >= 0); assert(curve.color_blue < 256 && curve.color_blue >= 0); - for (int i = 0; i < curve.coords.size() - 2; ++i) { - addLine(curve.coords[i].i, - curve.coords[i].j, - curve.coords[i + 1].i, - curve.coords[i + 1].j, + for (int i = 1; i < curve.coords.size(); ++i) { +// std::cout << curve.coords[i].x() << "\n"; + addLine(curve.coords[i].x(), + curve.coords[i].y() , + curve.coords[i - 1].x(), + curve.coords[i - 1].y(), QPen(QColor(curve.color_red, curve.color_green, curve.color_blue), curve.brush_size, Qt::SolidLine, Qt::RoundCap)); } From d9f5a722898cdc8e0ee6445de920f86be8c24567 Mon Sep 17 00:00:00 2001 From: Eremey Remzin Date: Thu, 18 Nov 2021 16:04:24 +0300 Subject: [PATCH 07/19] first test was added --- project/client/CMakeLists.txt | 18 ++++++++++++++++-- project/client/src/main_window_test.cpp | 14 ++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 project/client/src/main_window_test.cpp diff --git a/project/client/CMakeLists.txt b/project/client/CMakeLists.txt index e6983a6..bb8d28f 100644 --- a/project/client/CMakeLists.txt +++ b/project/client/CMakeLists.txt @@ -14,10 +14,10 @@ if (WITH_SANITIZER) add_link_options(-fsanitize=address) endif () -set(SOURCES src/main.cpp src/MainWindow.cpp src/PaintScene.cpp src/Palette.cpp src/Changer.cpp) +set(SOURCES src/MainWindow.cpp src/PaintScene.cpp src/Palette.cpp src/Changer.cpp) set(HEADER_FILES include/Palette.h include/MainWindow.h include/PaintScene.h include/Changer.h) -add_executable(client ${SOURCES} ${HEADER_FILES}) +add_executable(client src/main.cpp ${SOURCES} ${HEADER_FILES}) target_link_libraries(client Qt5::Core Qt5::Gui @@ -25,3 +25,17 @@ target_link_libraries(client ) target_include_directories(client PUBLIC include) + +add_executable(test src/main_window_test.cpp ${SOURCES} ${HEADER_FILES}) +find_package(GTest) + +target_link_libraries(test PRIVATE + GTest::Main + Qt5::Core + Qt5::Gui + Qt5::Widgets + ) + +add_test(NAME test COMMAND test) +target_include_directories(test PUBLIC include) + diff --git a/project/client/src/main_window_test.cpp b/project/client/src/main_window_test.cpp new file mode 100644 index 0000000..ca662a5 --- /dev/null +++ b/project/client/src/main_window_test.cpp @@ -0,0 +1,14 @@ +// +// Created by eremey on 18.11.2021. +// +#include +#include "MainWindow.h" + +TEST(GET_TIME, get_time) { + EXPECT_EQ(-1, -1); +} + +int main(int argc, char **argv) { + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} \ No newline at end of file From 45dbd7d4806e9b7f525ee9ae5f5780f59ae31cf2 Mon Sep 17 00:00:00 2001 From: Eremey Remzin Date: Thu, 18 Nov 2021 17:25:05 +0300 Subject: [PATCH 08/19] executeBrush tests were added --- project/client/include/MainWindow.h | 3 + project/client/src/MainWindow.cpp | 22 ++- project/client/src/main_window_test.cpp | 207 +++++++++++++++++++++++- 3 files changed, 229 insertions(+), 3 deletions(-) diff --git a/project/client/include/MainWindow.h b/project/client/include/MainWindow.h index 3d77e98..d2ad4b8 100644 --- a/project/client/include/MainWindow.h +++ b/project/client/include/MainWindow.h @@ -33,4 +33,7 @@ private slots: void slotBrush(); void slotTimer(); void TemporaryWriterSlot(const Curve& curve); + +public: + void executeBrush(const Curve& curve); }; diff --git a/project/client/src/MainWindow.cpp b/project/client/src/MainWindow.cpp index 010c34a..dbfa65b 100644 --- a/project/client/src/MainWindow.cpp +++ b/project/client/src/MainWindow.cpp @@ -14,7 +14,7 @@ // Temporary #include - +#include #include @@ -84,6 +84,7 @@ MainWindow::~MainWindow() { } void MainWindow::slotTimer() { + scene->setSceneRect(0, 0, qGraphicsView->width() - 20, qGraphicsView->height() - 20); std::ifstream isf ("House.txt"); Curve curve; int i = 0; @@ -189,6 +190,7 @@ void MainWindow::slotBrush() { scene->ChangeBrushStatus(); } void MainWindow::TemporaryWriterSlot(const Curve &curve) { + std::ofstream outf("House.txt", std::ios::app); outf << curve.brush_size << " " << curve.color_red << " " << curve.color_green << " " << curve.color_blue << " " << @@ -200,6 +202,24 @@ void MainWindow::TemporaryWriterSlot(const Curve &curve) { outf.close(); } +void MainWindow::executeBrush(const Curve& curve) { + if (curve.brush_size < 0) { + throw std::invalid_argument("Invalid size"); + } + if (curve.color_red < 0 || curve.color_red > 255) { + throw std::invalid_argument("Invalid red color"); + } + if (curve.color_green < 0 || curve.color_green > 255) { + throw std::invalid_argument("Invalid green color"); + } + if (curve.color_blue < 0 || curve.color_blue > 255) { + throw std::invalid_argument("Invalid blue color"); + } + if (curve.coords.size() < 2) { + throw std::invalid_argument("Invalid curve size"); + } + emit(TemporarySignal(curve)); +} //void MainWindow::resizeEvent(QResizeEvent *event) //{ diff --git a/project/client/src/main_window_test.cpp b/project/client/src/main_window_test.cpp index ca662a5..5a5358c 100644 --- a/project/client/src/main_window_test.cpp +++ b/project/client/src/main_window_test.cpp @@ -3,12 +3,215 @@ // #include #include "MainWindow.h" +#include -TEST(GET_TIME, get_time) { - EXPECT_EQ(-1, -1); +#include +#include + +TEST(INVALID_SIZE, test1) { + MainWindow mainWindow; + Curve curve; + curve.brush_size = -1; + + bool isSuccessTest = false; + try { + mainWindow.executeBrush(curve); + } + + catch (std::invalid_argument const& ex) { + if (strcmp(ex.what(), "Invalid size") == 0) { + isSuccessTest = true; + } + } + EXPECT_EQ(isSuccessTest, true); +} + +TEST(INVALID_COLOR, invalidRed1) { + MainWindow mainWindow; + Curve curve; + curve.brush_size = 10; + curve.color_red = -1; + + bool isSuccessTest = false; + try { + mainWindow.executeBrush(curve); + } + + catch (std::invalid_argument const& ex) { + if (strcmp(ex.what(), "Invalid red color") == 0) { + isSuccessTest = true; + } + } + EXPECT_EQ(isSuccessTest, true); +} + +TEST(INVALID_COLOR, invalidRed2) { + MainWindow mainWindow; + Curve curve; + curve.brush_size = 10; + curve.color_red = 300; + + bool isSuccessTest = false; + try { + mainWindow.executeBrush(curve); + } + + catch (std::invalid_argument const& ex) { + if (strcmp(ex.what(), "Invalid red color") == 0) { + isSuccessTest = true; + } + } + EXPECT_EQ(isSuccessTest, true); +} + +TEST(INVALID_COLOR, invalidGreen1) { + MainWindow mainWindow; + Curve curve; + curve.brush_size = 10; + curve.color_red = 20; + curve.color_green = -1; + + bool isSuccessTest = false; + try { + mainWindow.executeBrush(curve); + } + + catch (std::invalid_argument const& ex) { + if (strcmp(ex.what(), "Invalid green color") == 0) { + isSuccessTest = true; + } + } + EXPECT_EQ(isSuccessTest, true); +} + +TEST(INVALID_COLOR, invalidGreen2) { + MainWindow mainWindow; + Curve curve; + curve.brush_size = 10; + curve.color_red = 20; + curve.color_green = 300; + + bool isSuccessTest = false; + try { + mainWindow.executeBrush(curve); + } + + catch (std::invalid_argument const& ex) { + if (strcmp(ex.what(), "Invalid green color") == 0) { + isSuccessTest = true; + } + } + EXPECT_EQ(isSuccessTest, true); +} + +TEST(INVALID_COLOR, invalidBlue1) { + MainWindow mainWindow; + Curve curve; + curve.brush_size = 10; + curve.color_red = 20; + curve.color_green = 40; + curve.color_blue = -1; + + bool isSuccessTest = false; + try { + mainWindow.executeBrush(curve); + } + + catch (std::invalid_argument const& ex) { + if (strcmp(ex.what(), "Invalid blue color") == 0) { + isSuccessTest = true; + } + } + EXPECT_EQ(isSuccessTest, true); +} + +TEST(INVALID_COLOR, invalidBlue2) { + MainWindow mainWindow; + Curve curve; + curve.brush_size = 10; + curve.color_red = 20; + curve.color_green = 40; + curve.color_blue = 300; + + bool isSuccessTest = false; + try { + mainWindow.executeBrush(curve); + } + + catch (std::invalid_argument const& ex) { + if (strcmp(ex.what(), "Invalid blue color") == 0) { + isSuccessTest = true; + } + } + EXPECT_EQ(isSuccessTest, true); +} + +TEST(INVALID_LENGTH, invalidLength1) { + MainWindow mainWindow; + Curve curve; + curve.brush_size = 10; + curve.color_red = 20; + curve.color_green = 40; + curve.color_blue = 30; + + bool isSuccessTest = false; + try { + mainWindow.executeBrush(curve); + } + + catch (std::invalid_argument const& ex) { + if (strcmp(ex.what(), "Invalid curve size") == 0) { + isSuccessTest = true; + } + } + EXPECT_EQ(isSuccessTest, true); +} + +TEST(INVALID_LENGTH, invalidLength2) { + MainWindow mainWindow; + Curve curve; + curve.brush_size = 10; + curve.color_red = 20; + curve.color_green = 40; + curve.color_blue = 30; + curve.coords.emplace_back(QPointF(1,1)); + + bool isSuccessTest = false; + try { + mainWindow.executeBrush(curve); + } + + catch (std::invalid_argument const& ex) { + if (strcmp(ex.what(), "Invalid curve size") == 0) { + isSuccessTest = true; + } + } + EXPECT_EQ(isSuccessTest, true); +} + +TEST(VALID_CURVE, validCurve) { + MainWindow mainWindow; + Curve curve; + curve.brush_size = 10; + curve.color_red = 20; + curve.color_green = 40; + curve.color_blue = 30; + curve.coords.emplace_back(QPointF(1,1)); + curve.coords.emplace_back(QPointF(2,2)); + + bool isSuccessTest = true; + try { + mainWindow.executeBrush(curve); + } + + catch (std::invalid_argument const& ex) { + isSuccessTest = false; + } + EXPECT_EQ(isSuccessTest, true); } int main(int argc, char **argv) { + QApplication a(argc, argv); ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } \ No newline at end of file From caab5f9ca5cbe1fa3383a10eab446585b6a85fb3 Mon Sep 17 00:00:00 2001 From: Eremey Remzin Date: Thu, 18 Nov 2021 18:05:00 +0300 Subject: [PATCH 09/19] ToolsPanel was added --- project/client/CMakeLists.txt | 4 +-- project/client/include/MainWindow.h | 2 ++ project/client/include/ToolsPanel.h | 14 ++++++++ project/client/src/MainWindow.cpp | 54 ++--------------------------- project/client/src/ToolsPanel.cpp | 28 +++++++++++++++ 5 files changed, 49 insertions(+), 53 deletions(-) create mode 100644 project/client/include/ToolsPanel.h create mode 100644 project/client/src/ToolsPanel.cpp diff --git a/project/client/CMakeLists.txt b/project/client/CMakeLists.txt index bb8d28f..eb37250 100644 --- a/project/client/CMakeLists.txt +++ b/project/client/CMakeLists.txt @@ -14,8 +14,8 @@ if (WITH_SANITIZER) add_link_options(-fsanitize=address) endif () -set(SOURCES src/MainWindow.cpp src/PaintScene.cpp src/Palette.cpp src/Changer.cpp) -set(HEADER_FILES include/Palette.h include/MainWindow.h include/PaintScene.h include/Changer.h) +set(SOURCES src/MainWindow.cpp src/PaintScene.cpp src/Palette.cpp src/Changer.cpp src/ToolsPanel.cpp) +set(HEADER_FILES include/Palette.h include/MainWindow.h include/PaintScene.h include/Changer.h include/ToolsPanel.h) add_executable(client src/main.cpp ${SOURCES} ${HEADER_FILES}) target_link_libraries(client diff --git a/project/client/include/MainWindow.h b/project/client/include/MainWindow.h index d2ad4b8..9a6804e 100644 --- a/project/client/include/MainWindow.h +++ b/project/client/include/MainWindow.h @@ -6,6 +6,7 @@ #include #include "PaintScene.h" +#include "ToolsPanel.h" class MainWindow : public QMainWindow { @@ -24,6 +25,7 @@ Q_OBJECT QToolBar *parameters_panel; + ToolsPanel *toolsPanel; QTimer *timer; private: diff --git a/project/client/include/ToolsPanel.h b/project/client/include/ToolsPanel.h new file mode 100644 index 0000000..ebfba71 --- /dev/null +++ b/project/client/include/ToolsPanel.h @@ -0,0 +1,14 @@ +// +// Created by eremey on 18.11.2021. +// +#pragma once +#include + +class ToolsPanel : public QToolBar { + Q_OBJECT + +public: + explicit ToolsPanel(QWidget *parent = 0); + ~ToolsPanel() = default; +}; + diff --git a/project/client/src/MainWindow.cpp b/project/client/src/MainWindow.cpp index dbfa65b..dd04a58 100644 --- a/project/client/src/MainWindow.cpp +++ b/project/client/src/MainWindow.cpp @@ -59,14 +59,10 @@ MainWindow::MainWindow(QWidget *parent) : addToolBar(Qt::RightToolBarArea, right_panel); right_panel->setFixedWidth(400); + toolsPanel = new ToolsPanel; + addToolBar(Qt::LeftToolBarArea, toolsPanel); - QPixmap brush_pm("brush.png"); - //QAction *brush = new QAction(QIcon(brush_pm), "&Brush", this); - QToolBar *tools_panel = new QToolBar; - addToolBar(Qt::LeftToolBarArea, tools_panel); - QAction *brush = tools_panel->addAction(QIcon(brush_pm), "Brush"); - tools_panel->addAction(brush); - connect(brush, &QAction::triggered, this, &MainWindow::slotBrush); + connect(toolsPanel->actions()[0], &QAction::triggered, this, &MainWindow::slotBrush); connect(this, &MainWindow::TemporarySignal, scene, &PaintScene::PaintCurveSlot); connect(scene, &PaintScene::PushCurve, this, &MainWindow::TemporaryWriterSlot); @@ -85,50 +81,6 @@ MainWindow::~MainWindow() { void MainWindow::slotTimer() { scene->setSceneRect(0, 0, qGraphicsView->width() - 20, qGraphicsView->height() - 20); - std::ifstream isf ("House.txt"); - Curve curve; - int i = 0; - while (i < 1) { - isf >> curve.brush_size >> curve.color_red >> curve.color_green >> curve.color_blue; - - size_t curve_size = 0; - isf >> curve_size; - - QPointF point; - qreal point_x = 0; - qreal point_y = 0; - - for (size_t i = 0; i < curve_size; ++i) { - - isf >> point_x >> point_y; - point.setX(point_x); - point.setY(point_y); - - curve.coords.push_back(point); - //std::cout << point.j << " " << point.i << std::endl; - } - temporary_read_position = isf.tellg(); - emit(TemporarySignal(curve)); - i++; - } -// isf >> curve.brush_size >> curve.color_red >> curve.color_green >> curve.color_blue; -// -// size_t curve_size = 0; -// isf >> curve_size; -// -// Point point; -// for (size_t i = 0; i < curve_size; ++i) { -// isf >> point.i >> point.j; -// curve.coords.push_back(point); -// } -// temporary_read_position = isf.tellg(); - - isf.close(); - - - std::cout << "slotTimer\n"; - timer->stop(); - emit(TemporarySignal(curve)); } void MainWindow::slotBrush() { diff --git a/project/client/src/ToolsPanel.cpp b/project/client/src/ToolsPanel.cpp new file mode 100644 index 0000000..8916b58 --- /dev/null +++ b/project/client/src/ToolsPanel.cpp @@ -0,0 +1,28 @@ +// +// Created by eremey on 18.11.2021. +// + +#include +#include +#include + + +#include "ToolsPanel.h" + +ToolsPanel::ToolsPanel(QWidget *parent) : QToolBar(parent) { + QPixmap brush_pm("brush.png"); + + QAction *brush = addAction(QIcon(brush_pm), "B"); + + QPixmap pixmap(brush_pm.size()); + pixmap.fill( Qt::white); + + QAction *lasso = addAction(pixmap, "L"); + QAction *elaser = addAction(pixmap, "E"); + QAction *filler = addAction(pixmap, "F"); + QAction *scaling = addAction(pixmap, "S"); + QAction *loupe = addAction(pixmap, "L"); + QAction *primitives = addAction(pixmap, "P"); + + +} From b9d86dee6784b413c10f836033575e92c812e428 Mon Sep 17 00:00:00 2001 From: Eremey Remzin Date: Thu, 18 Nov 2021 19:05:19 +0300 Subject: [PATCH 10/19] ParametersPanel was added --- project/client/CMakeLists.txt | 6 ++-- project/client/include/MainWindow.h | 4 +++ project/client/include/ParametersPanel.h | 23 +++++++++++++ project/client/src/MainWindow.cpp | 7 +++- project/client/src/ParametersPanel.cpp | 43 ++++++++++++++++++++++++ project/client/src/ToolsPanel.cpp | 2 -- 6 files changed, 80 insertions(+), 5 deletions(-) create mode 100644 project/client/include/ParametersPanel.h create mode 100644 project/client/src/ParametersPanel.cpp diff --git a/project/client/CMakeLists.txt b/project/client/CMakeLists.txt index eb37250..910de87 100644 --- a/project/client/CMakeLists.txt +++ b/project/client/CMakeLists.txt @@ -14,8 +14,10 @@ if (WITH_SANITIZER) add_link_options(-fsanitize=address) endif () -set(SOURCES src/MainWindow.cpp src/PaintScene.cpp src/Palette.cpp src/Changer.cpp src/ToolsPanel.cpp) -set(HEADER_FILES include/Palette.h include/MainWindow.h include/PaintScene.h include/Changer.h include/ToolsPanel.h) +set(SOURCES src/MainWindow.cpp src/PaintScene.cpp src/Palette.cpp src/Changer.cpp + src/ToolsPanel.cpp src/ParametersPanel.cpp) +set(HEADER_FILES include/Palette.h include/MainWindow.h include/PaintScene.h include/Changer.h + include/ToolsPanel.h include/ParametersPanel.h) add_executable(client src/main.cpp ${SOURCES} ${HEADER_FILES}) target_link_libraries(client diff --git a/project/client/include/MainWindow.h b/project/client/include/MainWindow.h index 9a6804e..207ad04 100644 --- a/project/client/include/MainWindow.h +++ b/project/client/include/MainWindow.h @@ -7,6 +7,7 @@ #include "PaintScene.h" #include "ToolsPanel.h" +#include "ParametersPanel.h" class MainWindow : public QMainWindow { @@ -26,6 +27,9 @@ Q_OBJECT QToolBar *parameters_panel; ToolsPanel *toolsPanel; + + ParametersPanel *parametersPanel; + QTimer *timer; private: diff --git a/project/client/include/ParametersPanel.h b/project/client/include/ParametersPanel.h new file mode 100644 index 0000000..354ce3c --- /dev/null +++ b/project/client/include/ParametersPanel.h @@ -0,0 +1,23 @@ +// +// Created by eremey on 18.11.2021. +// +#pragma once +#include +#include "Changer.h" +#include "Palette.h" +#include + +class ParametersPanel : public QToolBar { + Q_OBJECT + +public: + explicit ParametersPanel(QWidget *parent = 0); + ~ParametersPanel() = default; + +public slots: + void setBrush(); + +private: + Changer* changers; +}; + diff --git a/project/client/src/MainWindow.cpp b/project/client/src/MainWindow.cpp index dd04a58..18b004e 100644 --- a/project/client/src/MainWindow.cpp +++ b/project/client/src/MainWindow.cpp @@ -51,6 +51,8 @@ MainWindow::MainWindow(QWidget *parent) : help->addAction(help_help); // ToolBars +// parametersPanel = new ParametersPanel; +// addToolBar(parametersPanel); parameters_panel = new QToolBar; addToolBar(parameters_panel); parameters_panel->setFixedHeight(40); @@ -76,7 +78,6 @@ MainWindow::MainWindow(QWidget *parent) : } MainWindow::~MainWindow() { -// free(qGraphicsView); } void MainWindow::slotTimer() { @@ -84,9 +85,13 @@ void MainWindow::slotTimer() { } void MainWindow::slotBrush() { + if (scene->BrushStatus()) { parameters_panel->clear(); + //parametersPanel->clear(); } else { + //parametersPanel->setBrush(); + scene->SetBrush(30, 0, 180, 40, 255); scene->setSceneRect(0, 0, qGraphicsView->width() - 20, qGraphicsView->height() - 20); diff --git a/project/client/src/ParametersPanel.cpp b/project/client/src/ParametersPanel.cpp new file mode 100644 index 0000000..77c3670 --- /dev/null +++ b/project/client/src/ParametersPanel.cpp @@ -0,0 +1,43 @@ +// +// Created by eremey on 18.11.2021. +// + +#include "ParametersPanel.h" + +ParametersPanel::ParametersPanel(QWidget *parent) : QToolBar(parent) { + setFixedHeight(40); +}; + +void ParametersPanel::setBrush() { + QWidget *dummy = new QWidget(); + dummy->setFixedWidth(40); + addWidget(dummy); + + Changer *size_changer = new Changer("Size", 30, 500, this); + addWidget(size_changer); + addSeparator(); + + + Palette *palette = new Palette; + palette->SetColor(0, 180, 40, 255); + + Changer *red_changer = new Changer("Red", 0, 255, this); + addWidget(red_changer); + + Changer *green_changer = new Changer("Green", 180, 255, this); + addWidget(green_changer); + + Changer *blue_changer = new Changer("Blue", 40, 255, this); + addWidget(blue_changer); + + + // Palette palette; + addWidget(palette); + addSeparator(); + + Changer *opacity_changer = new Changer("Opacity", 255, 255, this); + addWidget(opacity_changer); + + + setStyleSheet(QString("QToolBar {spacing: %1}").arg(10)); +} diff --git a/project/client/src/ToolsPanel.cpp b/project/client/src/ToolsPanel.cpp index 8916b58..d0014d7 100644 --- a/project/client/src/ToolsPanel.cpp +++ b/project/client/src/ToolsPanel.cpp @@ -23,6 +23,4 @@ ToolsPanel::ToolsPanel(QWidget *parent) : QToolBar(parent) { QAction *scaling = addAction(pixmap, "S"); QAction *loupe = addAction(pixmap, "L"); QAction *primitives = addAction(pixmap, "P"); - - } From 2a08463b89b73ee34a7a1d48e233889fad70f8aa Mon Sep 17 00:00:00 2001 From: Eremey Remzin Date: Thu, 18 Nov 2021 19:13:48 +0300 Subject: [PATCH 11/19] Changed a little --- project/client/include/MainWindow.h | 1 - project/client/src/MainWindow.cpp | 44 +++++++++++------------------ 2 files changed, 16 insertions(+), 29 deletions(-) diff --git a/project/client/include/MainWindow.h b/project/client/include/MainWindow.h index 207ad04..ce564ed 100644 --- a/project/client/include/MainWindow.h +++ b/project/client/include/MainWindow.h @@ -38,7 +38,6 @@ Q_OBJECT private slots: void slotBrush(); void slotTimer(); - void TemporaryWriterSlot(const Curve& curve); public: void executeBrush(const Curve& curve); diff --git a/project/client/src/MainWindow.cpp b/project/client/src/MainWindow.cpp index 18b004e..3ec775a 100644 --- a/project/client/src/MainWindow.cpp +++ b/project/client/src/MainWindow.cpp @@ -27,10 +27,10 @@ MainWindow::MainWindow(QWidget *parent) : setCentralWidget(qGraphicsView); // MenuBar - QAction *file_open = new QAction("&Open", this); - QAction *file_close = new QAction("&Close", this); - QAction *file_save = new QAction("&Save", this); - QAction *file_save_as = new QAction("&Save as", this); + auto *file_open = new QAction("&Open", this); + auto *file_close = new QAction("&Close", this); + auto *file_save = new QAction("&Save", this); + auto *file_save_as = new QAction("&Save as", this); QMenu *file; file = menuBar()->addMenu("File"); file->addAction(file_open); @@ -38,14 +38,14 @@ MainWindow::MainWindow(QWidget *parent) : file->addAction(file_save); file->addAction(file_save_as); - QAction *edit_undo = new QAction("&Undo", this); - QAction *edit_redo = new QAction("&Redo", this); + auto *edit_undo = new QAction("&Undo", this); + auto *edit_redo = new QAction("&Redo", this); QMenu *edit; edit = menuBar()->addMenu("Edit"); edit->addAction(edit_undo); edit->addAction(edit_redo); - QAction *help_help = new QAction("&Help", this); + auto *help_help = new QAction("&Help", this); QMenu *help; help = menuBar()->addMenu("Help"); help->addAction(help_help); @@ -57,7 +57,7 @@ MainWindow::MainWindow(QWidget *parent) : addToolBar(parameters_panel); parameters_panel->setFixedHeight(40); - QToolBar *right_panel = new QToolBar; + auto *right_panel = new QToolBar; addToolBar(Qt::RightToolBarArea, right_panel); right_panel->setFixedWidth(400); @@ -66,7 +66,7 @@ MainWindow::MainWindow(QWidget *parent) : connect(toolsPanel->actions()[0], &QAction::triggered, this, &MainWindow::slotBrush); connect(this, &MainWindow::TemporarySignal, scene, &PaintScene::PaintCurveSlot); - connect(scene, &PaintScene::PushCurve, this, &MainWindow::TemporaryWriterSlot); + //connect(scene, &PaintScene::PushCurve, this, &MainWindow::TemporaryWriterSlot); // Status bar @@ -95,39 +95,39 @@ void MainWindow::slotBrush() { scene->SetBrush(30, 0, 180, 40, 255); scene->setSceneRect(0, 0, qGraphicsView->width() - 20, qGraphicsView->height() - 20); - QWidget *dummy = new QWidget(); + auto *dummy = new QWidget(); dummy->setFixedWidth(40); parameters_panel->addWidget(dummy); - Changer *size_changer = new Changer("Size", 30, 500, this); + auto *size_changer = new Changer("Size", 30, 500, this); connect(size_changer, QOverload::of(&Changer::valueChanged), scene, &PaintScene::SetBrushSize); parameters_panel->addWidget(size_changer); parameters_panel->addSeparator(); - Palette *palette = new Palette; + auto *palette = new Palette; palette->SetColor(0, 180, 40, 255); - Changer *opacity_changer = new Changer("Opacity", 255, 255, this); + auto *opacity_changer = new Changer("Opacity", 255, 255, this); connect(opacity_changer, QOverload::of(&Changer::valueChanged), scene, (&PaintScene::SetTransparencySlot)); - Changer *red_changer = new Changer("Red", 0, 255, this); + auto *red_changer = new Changer("Red", 0, 255, this); connect(red_changer, QOverload::of(&Changer::valueChanged), scene, (&PaintScene::SetRedSlot)); connect(red_changer, QOverload::of(&Changer::valueChanged), palette, (&Palette::SetRed)); parameters_panel->addWidget(red_changer); - Changer *green_changer = new Changer("Green", 180, 255, this); + auto *green_changer = new Changer("Green", 180, 255, this); connect(green_changer, QOverload::of(&Changer::valueChanged), scene, (&PaintScene::SetGreenSlot)); connect(green_changer, QOverload::of(&Changer::valueChanged), palette, (&Palette::SetGreen)); parameters_panel->addWidget(green_changer); - Changer *blue_changer = new Changer("Blue", 40, 255, this); + auto *blue_changer = new Changer("Blue", 40, 255, this); connect(blue_changer, QOverload::of(&Changer::valueChanged), scene, (&PaintScene::SetBlueSlot)); connect(blue_changer, QOverload::of(&Changer::valueChanged), palette, @@ -146,19 +146,7 @@ void MainWindow::slotBrush() { } scene->ChangeBrushStatus(); } -void MainWindow::TemporaryWriterSlot(const Curve &curve) { - std::ofstream outf("House.txt", std::ios::app); - - outf << curve.brush_size << " " << curve.color_red << " " << curve.color_green << " " << curve.color_blue << " " << - curve.coords.size() << std::endl; - for (auto& point: curve.coords) { - outf << point.x() << " " << point.y() << " "; - } - outf << std::endl; - - outf.close(); -} void MainWindow::executeBrush(const Curve& curve) { if (curve.brush_size < 0) { throw std::invalid_argument("Invalid size"); From 3a99a42ef85e9aa2f050f11cfe5d7cc9a3a39875 Mon Sep 17 00:00:00 2001 From: Eremey Remzin Date: Sat, 27 Nov 2021 15:23:58 +0300 Subject: [PATCH 12/19] Interaction between widgets was improved --- project/client/CMakeLists.txt | 4 +- project/client/include/Brush.h | 28 +++++++++ project/client/include/MainWindow.h | 2 +- project/client/include/PaintScene.h | 3 +- project/client/include/ParametersPanel.h | 16 ++++- project/client/include/ToolsPanel.h | 21 ++++++- project/client/src/Brush.cpp | 36 +++++++++++ project/client/src/MainWindow.cpp | 80 +++++++----------------- project/client/src/PaintScene.cpp | 7 +-- project/client/src/ParametersPanel.cpp | 42 ++++++++++--- project/client/src/ToolsPanel.cpp | 34 ++++++++-- 11 files changed, 195 insertions(+), 78 deletions(-) create mode 100644 project/client/include/Brush.h create mode 100644 project/client/src/Brush.cpp diff --git a/project/client/CMakeLists.txt b/project/client/CMakeLists.txt index 910de87..337d855 100644 --- a/project/client/CMakeLists.txt +++ b/project/client/CMakeLists.txt @@ -15,9 +15,9 @@ if (WITH_SANITIZER) endif () set(SOURCES src/MainWindow.cpp src/PaintScene.cpp src/Palette.cpp src/Changer.cpp - src/ToolsPanel.cpp src/ParametersPanel.cpp) + src/ToolsPanel.cpp src/ParametersPanel.cpp src/Brush.cpp) set(HEADER_FILES include/Palette.h include/MainWindow.h include/PaintScene.h include/Changer.h - include/ToolsPanel.h include/ParametersPanel.h) + include/ToolsPanel.h include/ParametersPanel.h include/Brush.h) add_executable(client src/main.cpp ${SOURCES} ${HEADER_FILES}) target_link_libraries(client diff --git a/project/client/include/Brush.h b/project/client/include/Brush.h new file mode 100644 index 0000000..37c254b --- /dev/null +++ b/project/client/include/Brush.h @@ -0,0 +1,28 @@ +// +// Created by eremey on 27.11.2021. +// +#pragma once +#include + +class Brush : public QAction { + Q_OBJECT + +public: + explicit Brush(qreal brushSize = 10, const QColor& brushColor = Qt::red, QWidget *parent = nullptr); + ~Brush() = default; + +signals: + void BrushTriggered(qreal brushSize, const QColor& brushColor); + +public slots: + void SetSizeSlot(int value); + void SetRedSlot(int value); + void SetGreenSlot(int value); + void SetBlueSlot(int value); + void SetOpacitySlot(int value); +private slots: + void BrushTriggeredSlot(); +private: + qreal brushSize; + QColor brushColor; +}; \ No newline at end of file diff --git a/project/client/include/MainWindow.h b/project/client/include/MainWindow.h index ce564ed..e244780 100644 --- a/project/client/include/MainWindow.h +++ b/project/client/include/MainWindow.h @@ -36,7 +36,7 @@ Q_OBJECT long temporary_read_position = 0; private slots: - void slotBrush(); + void slotBrush(qreal brushSize = 10, const QColor& brushColor = Qt::red); void slotTimer(); public: diff --git a/project/client/include/PaintScene.h b/project/client/include/PaintScene.h index d19e01f..e3c8719 100644 --- a/project/client/include/PaintScene.h +++ b/project/client/include/PaintScene.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -32,7 +33,7 @@ class PaintScene : public QGraphicsScene void ChangeBrushStatus(); bool BrushStatus(); void SetBrushSize(qreal brush_size = 10); - void SetBrush(qreal size, int red, int green, int blue, int opacity); + void SetBrush(qreal brushSize = 10, const QColor& brushColor = Qt::red); signals: void PushCurve(const Curve& curve); diff --git a/project/client/include/ParametersPanel.h b/project/client/include/ParametersPanel.h index 354ce3c..e5a3520 100644 --- a/project/client/include/ParametersPanel.h +++ b/project/client/include/ParametersPanel.h @@ -14,10 +14,24 @@ class ParametersPanel : public QToolBar { explicit ParametersPanel(QWidget *parent = 0); ~ParametersPanel() = default; +signals: + void BrushSizeChanged(int newSize); + void BrushRedChanged(int newValue); + void BrushGreenChanged(int newValue); + void BrushBlueChanged(int newValue); + void BrushOpacityChanged(int newValue); public slots: - void setBrush(); + void setBrush(qreal brushSize = 10, const QColor& brushColor = Qt::red); private: Changer* changers; + +private slots: + void BrushSizeChangedSlot(int newSize); + void BrushRedChangedSlot(int newValue); + void BrushGreenChangedSlot(int newValue); + void BrushBlueChangedSlot(int newValue); + void BrushOpacityChangedSlot(int newValue); + }; diff --git a/project/client/include/ToolsPanel.h b/project/client/include/ToolsPanel.h index ebfba71..b746c28 100644 --- a/project/client/include/ToolsPanel.h +++ b/project/client/include/ToolsPanel.h @@ -8,7 +8,26 @@ class ToolsPanel : public QToolBar { Q_OBJECT public: - explicit ToolsPanel(QWidget *parent = 0); + explicit ToolsPanel(QWidget *parent = nullptr); ~ToolsPanel() = default; + +public slots: + void SetBrushSizeSlot(int value); + void SetBrushRedSlot(int value); + void SetBrushGreenSlot(int value); + void SetBrushBlueSlot(int value); + void SetBrushOpacitySlot(int value); + +private slots: + void BrushTriggeredSlot(qreal brushSize, const QColor& brushColor); +private: +signals: + void BrushTriggered(qreal brushSize, const QColor& brushColor); + + void SetBrushSize(int value); + void SetBrushRed(int value); + void SetBrushGreen(int value); + void SetBrushBlue(int value); + void SetBrushOpacity(int value); }; diff --git a/project/client/src/Brush.cpp b/project/client/src/Brush.cpp new file mode 100644 index 0000000..f9da2ad --- /dev/null +++ b/project/client/src/Brush.cpp @@ -0,0 +1,36 @@ +// +// Created by eremey on 27.11.2021. +// +#include "Brush.h" +#include +#include + +#include + +Brush::Brush(qreal brushSize, const QColor& brushColor, QWidget *parent) : + brushSize(brushSize), brushColor(brushColor), QAction(parent) { + assert(parent != nullptr); + connect(this, &QAction::triggered, this, &Brush::BrushTriggeredSlot); + + QPixmap brush_pm("brush.png"); + setIcon(QIcon(brush_pm)); +} +void Brush::BrushTriggeredSlot() { + emit(BrushTriggered(brushSize, brushColor)); +} +void Brush::SetSizeSlot(int value) { + brushSize = value; +} + +void Brush::SetRedSlot(int value) { + brushColor.setRed(value); +} +void Brush::SetGreenSlot(int value) { + brushColor.setGreen(value); +} +void Brush::SetBlueSlot(int value) { + brushColor.setBlue(value); +} +void Brush::SetOpacitySlot(int value) { + brushColor.setAlpha(value); +} diff --git a/project/client/src/MainWindow.cpp b/project/client/src/MainWindow.cpp index 3ec775a..82910c8 100644 --- a/project/client/src/MainWindow.cpp +++ b/project/client/src/MainWindow.cpp @@ -16,14 +16,20 @@ #include +//// +#include +#include +//// + #include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { - scene = new PaintScene(this); // Инициализируем графическую сцену + scene = new PaintScene(this); qGraphicsView = new QGraphicsView; qGraphicsView->setScene(scene); + setCentralWidget(qGraphicsView); // MenuBar @@ -51,8 +57,8 @@ MainWindow::MainWindow(QWidget *parent) : help->addAction(help_help); // ToolBars -// parametersPanel = new ParametersPanel; -// addToolBar(parametersPanel); + parametersPanel = new ParametersPanel(this); + addToolBar(parametersPanel); parameters_panel = new QToolBar; addToolBar(parameters_panel); parameters_panel->setFixedHeight(40); @@ -64,7 +70,7 @@ MainWindow::MainWindow(QWidget *parent) : toolsPanel = new ToolsPanel; addToolBar(Qt::LeftToolBarArea, toolsPanel); - connect(toolsPanel->actions()[0], &QAction::triggered, this, &MainWindow::slotBrush); + connect(toolsPanel, &ToolsPanel::BrushTriggered, this, &MainWindow::slotBrush); connect(this, &MainWindow::TemporarySignal, scene, &PaintScene::PaintCurveSlot); //connect(scene, &PaintScene::PushCurve, this, &MainWindow::TemporaryWriterSlot); @@ -84,65 +90,27 @@ void MainWindow::slotTimer() { scene->setSceneRect(0, 0, qGraphicsView->width() - 20, qGraphicsView->height() - 20); } -void MainWindow::slotBrush() { +void MainWindow::slotBrush(qreal brushSize, const QColor& brushColor) { if (scene->BrushStatus()) { - parameters_panel->clear(); - //parametersPanel->clear(); + parametersPanel->clear(); } else { - //parametersPanel->setBrush(); - - scene->SetBrush(30, 0, 180, 40, 255); - scene->setSceneRect(0, 0, qGraphicsView->width() - 20, qGraphicsView->height() - 20); - - auto *dummy = new QWidget(); - dummy->setFixedWidth(40); - parameters_panel->addWidget(dummy); - - auto *size_changer = new Changer("Size", 30, 500, this); - connect(size_changer, QOverload::of(&Changer::valueChanged), scene, &PaintScene::SetBrushSize); - parameters_panel->addWidget(size_changer); - parameters_panel->addSeparator(); - - - auto *palette = new Palette; - palette->SetColor(0, 180, 40, 255); - - auto *opacity_changer = new Changer("Opacity", 255, 255, this); - connect(opacity_changer, QOverload::of(&Changer::valueChanged), scene, - (&PaintScene::SetTransparencySlot)); - - - auto *red_changer = new Changer("Red", 0, 255, this); - connect(red_changer, QOverload::of(&Changer::valueChanged), scene, - (&PaintScene::SetRedSlot)); - connect(red_changer, QOverload::of(&Changer::valueChanged), palette, - (&Palette::SetRed)); - parameters_panel->addWidget(red_changer); - - auto *green_changer = new Changer("Green", 180, 255, this); - connect(green_changer, QOverload::of(&Changer::valueChanged), scene, - (&PaintScene::SetGreenSlot)); - connect(green_changer, QOverload::of(&Changer::valueChanged), palette, - (&Palette::SetGreen)); - parameters_panel->addWidget(green_changer); - - auto *blue_changer = new Changer("Blue", 40, 255, this); - connect(blue_changer, QOverload::of(&Changer::valueChanged), scene, - (&PaintScene::SetBlueSlot)); - connect(blue_changer, QOverload::of(&Changer::valueChanged), palette, - (&Palette::SetBlue)); - parameters_panel->addWidget(blue_changer); - + scene->SetBrush(brushSize, brushColor); + parametersPanel->setBrush(brushSize, brushColor); + connect(parametersPanel, QOverload::of(&ParametersPanel::BrushSizeChanged), scene, &PaintScene::SetBrushSize); + connect(parametersPanel, QOverload::of(&ParametersPanel::BrushSizeChanged), toolsPanel, &ToolsPanel::SetBrushSizeSlot); -// Palette palette; - parameters_panel->addWidget(palette); - parameters_panel->addSeparator(); + connect(parametersPanel, QOverload::of(&ParametersPanel::BrushRedChanged), scene, &PaintScene::SetRedSlot); + connect(parametersPanel, QOverload::of(&ParametersPanel::BrushRedChanged), toolsPanel, &ToolsPanel::SetBrushRedSlot); - parameters_panel->addWidget(opacity_changer); + connect(parametersPanel, QOverload::of(&ParametersPanel::BrushGreenChanged), scene, &PaintScene::SetGreenSlot); + connect(parametersPanel, QOverload::of(&ParametersPanel::BrushGreenChanged), toolsPanel, &ToolsPanel::SetBrushGreenSlot); + connect(parametersPanel, QOverload::of(&ParametersPanel::BrushBlueChanged), scene, &PaintScene::SetBlueSlot); + connect(parametersPanel, QOverload::of(&ParametersPanel::BrushBlueChanged), toolsPanel, &ToolsPanel::SetBrushBlueSlot); - parameters_panel->setStyleSheet(QString("QToolBar {spacing: %1}").arg(10)); + connect(parametersPanel, QOverload::of(&ParametersPanel::BrushOpacityChanged), scene, &PaintScene::SetTransparencySlot); + connect(parametersPanel, QOverload::of(&ParametersPanel::BrushOpacityChanged), toolsPanel, &ToolsPanel::SetBrushOpacitySlot); } scene->ChangeBrushStatus(); } diff --git a/project/client/src/PaintScene.cpp b/project/client/src/PaintScene.cpp index 7cfcfcf..9a67f59 100644 --- a/project/client/src/PaintScene.cpp +++ b/project/client/src/PaintScene.cpp @@ -1,5 +1,4 @@ #include "PaintScene.h" -#include void PaintScene::mousePressEvent(QGraphicsSceneMouseEvent *event) { if (is_brush) { @@ -67,9 +66,9 @@ void PaintScene::SetBlueSlot(int value) { assert(value < 256 && value >= 0); brush_color.setBlue(value); } -void PaintScene::SetBrush(qreal size, int red, int green, int blue, int opacity) { - brush_size = size; - brush_color.setRgb(red, green, blue, opacity); +void PaintScene::SetBrush(qreal brushSize, const QColor& brushColor) { + brush_size = brushSize; + brush_color = brushColor; } void PaintScene::PaintCurveSlot(const Curve &curve) { assert(curve.coords.size() > 1); diff --git a/project/client/src/ParametersPanel.cpp b/project/client/src/ParametersPanel.cpp index 77c3670..e68a67c 100644 --- a/project/client/src/ParametersPanel.cpp +++ b/project/client/src/ParametersPanel.cpp @@ -8,26 +8,36 @@ ParametersPanel::ParametersPanel(QWidget *parent) : QToolBar(parent) { setFixedHeight(40); }; -void ParametersPanel::setBrush() { +void ParametersPanel::setBrush(qreal brushSize, const QColor& brushColor) { QWidget *dummy = new QWidget(); dummy->setFixedWidth(40); addWidget(dummy); - Changer *size_changer = new Changer("Size", 30, 500, this); + Changer *size_changer = new Changer("Size", brushSize, 500, this); addWidget(size_changer); + connect(size_changer, QOverload::of(&Changer::valueChanged), this, &ParametersPanel::BrushSizeChangedSlot); + addSeparator(); Palette *palette = new Palette; - palette->SetColor(0, 180, 40, 255); + palette->SetColor(brushColor.red(), brushColor.green(), brushColor.blue()); - Changer *red_changer = new Changer("Red", 0, 255, this); + Changer *red_changer = new Changer("Red", brushColor.red(), 255, this); + connect(red_changer, QOverload::of(&Changer::valueChanged), this, &ParametersPanel::BrushRedChangedSlot); + connect(red_changer, QOverload::of(&Changer::valueChanged), palette, (&Palette::SetRed)); addWidget(red_changer); - Changer *green_changer = new Changer("Green", 180, 255, this); + Changer *green_changer = new Changer("Green", brushColor.green(), 255, this); + connect(green_changer, QOverload::of(&Changer::valueChanged), this, &ParametersPanel::BrushGreenChangedSlot); + connect(green_changer, QOverload::of(&Changer::valueChanged), palette, (&Palette::SetGreen)); + addWidget(green_changer); - Changer *blue_changer = new Changer("Blue", 40, 255, this); + Changer *blue_changer = new Changer("Blue", brushColor.blue(), 255, this); + connect(blue_changer, QOverload::of(&Changer::valueChanged), this, &ParametersPanel::BrushBlueChangedSlot); + connect(blue_changer, QOverload::of(&Changer::valueChanged), palette, (&Palette::SetBlue)); + addWidget(blue_changer); @@ -35,9 +45,25 @@ void ParametersPanel::setBrush() { addWidget(palette); addSeparator(); - Changer *opacity_changer = new Changer("Opacity", 255, 255, this); + Changer *opacity_changer = new Changer("Opacity", 100, 100, this); + connect(opacity_changer, QOverload::of(&Changer::valueChanged), this, &ParametersPanel::BrushOpacityChangedSlot); addWidget(opacity_changer); - setStyleSheet(QString("QToolBar {spacing: %1}").arg(10)); } + +void ParametersPanel::BrushSizeChangedSlot(int newSize) { + emit(BrushSizeChanged(newSize)); +} +void ParametersPanel::BrushRedChangedSlot(int newValue) { + emit(BrushRedChanged(newValue)); +} +void ParametersPanel::BrushGreenChangedSlot(int newValue) { + emit(BrushGreenChanged(newValue)); +} +void ParametersPanel::BrushBlueChangedSlot(int newValue) { + emit(BrushBlueChanged(newValue)); +} +void ParametersPanel::BrushOpacityChangedSlot(int newValue) { + emit(BrushOpacityChanged((newValue * 255) / 100)); +} diff --git a/project/client/src/ToolsPanel.cpp b/project/client/src/ToolsPanel.cpp index d0014d7..297ac3e 100644 --- a/project/client/src/ToolsPanel.cpp +++ b/project/client/src/ToolsPanel.cpp @@ -4,15 +4,15 @@ #include #include -#include - - #include "ToolsPanel.h" +#include "Brush.h" + ToolsPanel::ToolsPanel(QWidget *parent) : QToolBar(parent) { QPixmap brush_pm("brush.png"); - QAction *brush = addAction(QIcon(brush_pm), "B"); + auto *brush = new Brush(20, Qt::darkGreen, this); + addAction(brush); QPixmap pixmap(brush_pm.size()); pixmap.fill( Qt::white); @@ -23,4 +23,30 @@ ToolsPanel::ToolsPanel(QWidget *parent) : QToolBar(parent) { QAction *scaling = addAction(pixmap, "S"); QAction *loupe = addAction(pixmap, "L"); QAction *primitives = addAction(pixmap, "P"); + + connect(brush, &Brush::BrushTriggered, this, &ToolsPanel::BrushTriggeredSlot); + + connect(this, &ToolsPanel::SetBrushSize, brush, &Brush::SetSizeSlot); + connect(this, &ToolsPanel::SetBrushRed, brush, &Brush::SetRedSlot); + connect(this, &ToolsPanel::SetBrushGreen, brush, &Brush::SetGreenSlot); + connect(this, &ToolsPanel::SetBrushBlue, brush, &Brush::SetBlueSlot); + connect(this, &ToolsPanel::SetBrushOpacity, brush, &Brush::SetOpacitySlot); +} +void ToolsPanel::BrushTriggeredSlot(qreal brushSize, const QColor& brushColor) { + emit(BrushTriggered(brushSize, brushColor)); +} +void ToolsPanel::SetBrushSizeSlot(int value) { + emit(SetBrushSize(value)); +} +void ToolsPanel::SetBrushRedSlot(int value) { + emit(SetBrushRed(value)); +} +void ToolsPanel::SetBrushGreenSlot(int value) { + emit(SetBrushGreen(value)); +} +void ToolsPanel::SetBrushBlueSlot(int value) { + emit(SetBrushBlue(value)); +} +void ToolsPanel::SetBrushOpacitySlot(int value) { + emit(SetBrushOpacity(value)); } From 38477a53a7ea02dcc9a5e2903f8ad9145e2e591f Mon Sep 17 00:00:00 2001 From: Eremey Remzin Date: Thu, 2 Dec 2021 20:28:43 +0300 Subject: [PATCH 13/19] dirty commit --- project/client/CMakeLists.txt | 4 ++-- project/client/include/MainWindow.h | 4 +++- project/client/src/MainWindow.cpp | 12 ++++++++---- project/client/src/PaintScene.cpp | 2 ++ 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/project/client/CMakeLists.txt b/project/client/CMakeLists.txt index 337d855..e27ffc4 100644 --- a/project/client/CMakeLists.txt +++ b/project/client/CMakeLists.txt @@ -15,9 +15,9 @@ if (WITH_SANITIZER) endif () set(SOURCES src/MainWindow.cpp src/PaintScene.cpp src/Palette.cpp src/Changer.cpp - src/ToolsPanel.cpp src/ParametersPanel.cpp src/Brush.cpp) + src/ToolsPanel.cpp src/ParametersPanel.cpp src/Brush.cpp src/Canvas.cpp src/Loader.cpp) set(HEADER_FILES include/Palette.h include/MainWindow.h include/PaintScene.h include/Changer.h - include/ToolsPanel.h include/ParametersPanel.h include/Brush.h) + include/ToolsPanel.h include/ParametersPanel.h include/Brush.h include/Canvas.h include/Loader.h) add_executable(client src/main.cpp ${SOURCES} ${HEADER_FILES}) target_link_libraries(client diff --git a/project/client/include/MainWindow.h b/project/client/include/MainWindow.h index e244780..651ad81 100644 --- a/project/client/include/MainWindow.h +++ b/project/client/include/MainWindow.h @@ -8,6 +8,7 @@ #include "PaintScene.h" #include "ToolsPanel.h" #include "ParametersPanel.h" +#include "Canvas.h" class MainWindow : public QMainWindow { @@ -22,7 +23,7 @@ Q_OBJECT private: PaintScene *scene; - QGraphicsView* qGraphicsView; + Canvas* canvas; QToolBar *parameters_panel; @@ -39,6 +40,7 @@ private slots: void slotBrush(qreal brushSize = 10, const QColor& brushColor = Qt::red); void slotTimer(); + public: void executeBrush(const Curve& curve); }; diff --git a/project/client/src/MainWindow.cpp b/project/client/src/MainWindow.cpp index 82910c8..98bba3b 100644 --- a/project/client/src/MainWindow.cpp +++ b/project/client/src/MainWindow.cpp @@ -27,16 +27,20 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { scene = new PaintScene(this); - qGraphicsView = new QGraphicsView; - qGraphicsView->setScene(scene); + canvas = new Canvas; + canvas->setScene(scene); - setCentralWidget(qGraphicsView); + setCentralWidget(canvas); // MenuBar auto *file_open = new QAction("&Open", this); + connect(file_open, &QAction::triggered, canvas, &Canvas::openImageSlot); auto *file_close = new QAction("&Close", this); + connect(file_close, &QAction::triggered, canvas, &Canvas::closeImageSlot); auto *file_save = new QAction("&Save", this); + connect(file_save, &QAction::triggered, canvas, &Canvas::saveImageSlot); auto *file_save_as = new QAction("&Save as", this); + connect(file_save_as, &QAction::triggered, canvas, &Canvas::saveAsImageSlot); QMenu *file; file = menuBar()->addMenu("File"); file->addAction(file_open); @@ -87,7 +91,7 @@ MainWindow::~MainWindow() { } void MainWindow::slotTimer() { - scene->setSceneRect(0, 0, qGraphicsView->width() - 20, qGraphicsView->height() - 20); + //scene->setSceneRect(0, 0, qGraphicsView->width() - 20, qGraphicsView->height() - 20); } void MainWindow::slotBrush(qreal brushSize, const QColor& brushColor) { diff --git a/project/client/src/PaintScene.cpp b/project/client/src/PaintScene.cpp index 9a67f59..4bf6500 100644 --- a/project/client/src/PaintScene.cpp +++ b/project/client/src/PaintScene.cpp @@ -1,4 +1,6 @@ #include "PaintScene.h" +#include +#include void PaintScene::mousePressEvent(QGraphicsSceneMouseEvent *event) { if (is_brush) { From ac5a21fc5de41ea509cc584c9a34a964d891be68 Mon Sep 17 00:00:00 2001 From: Eremey Remzin Date: Thu, 2 Dec 2021 20:31:16 +0300 Subject: [PATCH 14/19] dirty commit --- project/client/include/Canvas.h | 29 ++++++++++++++++++ project/client/src/Canvas.cpp | 54 +++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 project/client/include/Canvas.h create mode 100644 project/client/src/Canvas.cpp diff --git a/project/client/include/Canvas.h b/project/client/include/Canvas.h new file mode 100644 index 0000000..ffb7284 --- /dev/null +++ b/project/client/include/Canvas.h @@ -0,0 +1,29 @@ +// +// Created by eremey on 01.12.2021. +// +#pragma once +#include +#include +#include +#include + +class Canvas : public QGraphicsView +{ + Q_OBJECT + +public: + explicit Canvas(QWidget *parent = 0); + ~Canvas() = default; + +public slots: + void openImageSlot(); + void saveImageSlot(); + void saveAsImageSlot(); + void closeImageSlot(); + + +private: + QString filePath = nullptr; + + size_t default_width; +}; \ No newline at end of file diff --git a/project/client/src/Canvas.cpp b/project/client/src/Canvas.cpp new file mode 100644 index 0000000..b4a62af --- /dev/null +++ b/project/client/src/Canvas.cpp @@ -0,0 +1,54 @@ +// +// Created by eremey on 01.12.2021. +// + +#include "Canvas.h" +#include +#include //// + +Canvas::Canvas(QWidget *parent) : + QGraphicsView(parent) { +// default_width = scene().wr +} + +void Canvas::openImageSlot() { + QString path = QFileDialog::getOpenFileName(this, + tr("Open Image"), ".", tr("Image Files (*.png *.jpg *.bmp)")); + auto *image = new QImage; + image->load(path); + + if(image->height() < height()) { + setFixedHeight(image->height() + 16); // 16 - slider height + } + if(image->width() < width()) { + setFixedWidth(image->width() + 16); + } + + setSceneRect(0, 0, image->width(), image->height()); + scene()->addPixmap(QPixmap::fromImage(*image)); + delete image; +} + +void Canvas::saveImageSlot() { + if (filePath == nullptr) { + filePath = QFileDialog::getSaveFileName(this, + tr("Save Image"), "."); + } + + QImage image(scene()->width(), scene()->height(), QImage::Format_ARGB32_Premultiplied); + QPainter painter(&image); + scene()->render(&painter); + image.save(filePath, "JPG"); +} +void Canvas::saveAsImageSlot() { + filePath = QFileDialog::getSaveFileName(this, + tr("Save Image"), "."); + + QImage image(scene()->width(), scene()->height(), QImage::Format_ARGB32_Premultiplied); + QPainter painter(&image); + scene()->render(&painter); + image.save(filePath, "JPG"); +} +void Canvas::closeImageSlot() { + +} From 8899f247e053c4d3ee0d9b7fe1ce59133a5c5e70 Mon Sep 17 00:00:00 2001 From: Eremey Remzin Date: Wed, 8 Dec 2021 00:29:03 +0300 Subject: [PATCH 15/19] TODOs were added --- project/client/include/MainWindow.h | 1 + project/client/src/MainWindow.cpp | 4 ++++ project/client/src/PaintScene.cpp | 2 ++ 3 files changed, 7 insertions(+) diff --git a/project/client/include/MainWindow.h b/project/client/include/MainWindow.h index 651ad81..aca071c 100644 --- a/project/client/include/MainWindow.h +++ b/project/client/include/MainWindow.h @@ -42,5 +42,6 @@ private slots: public: + void execute(std::string&& message); void executeBrush(const Curve& curve); }; diff --git a/project/client/src/MainWindow.cpp b/project/client/src/MainWindow.cpp index 98bba3b..4b611ba 100644 --- a/project/client/src/MainWindow.cpp +++ b/project/client/src/MainWindow.cpp @@ -85,6 +85,7 @@ MainWindow::MainWindow(QWidget *parent) : timer = new QTimer(); connect(timer, &QTimer::timeout, this, &MainWindow::slotTimer); timer->start(500); + // TODO: client } MainWindow::~MainWindow() { @@ -137,6 +138,9 @@ void MainWindow::executeBrush(const Curve& curve) { } emit(TemporarySignal(curve)); } +void MainWindow::execute(std::string&& message) { + // TODO: execute +} //void MainWindow::resizeEvent(QResizeEvent *event) //{ diff --git a/project/client/src/PaintScene.cpp b/project/client/src/PaintScene.cpp index 4bf6500..240ca7d 100644 --- a/project/client/src/PaintScene.cpp +++ b/project/client/src/PaintScene.cpp @@ -30,6 +30,7 @@ void PaintScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { void PaintScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { + // TODO: write PushCurve({brush_size, brush_color.red(), brush_color.green(), brush_color.blue(), line}); line.clear(); @@ -73,6 +74,7 @@ void PaintScene::SetBrush(qreal brushSize, const QColor& brushColor) { brush_color = brushColor; } void PaintScene::PaintCurveSlot(const Curve &curve) { + // TODO: read assert(curve.coords.size() > 1); assert(curve.color_red < 256 && curve.color_red >= 0); assert(curve.color_green < 256 && curve.color_green >= 0); From 704ec80883f3916976b1aba36b5477e488e23866 Mon Sep 17 00:00:00 2001 From: Dmitriy Ilyin Date: Thu, 9 Dec 2021 01:34:01 +0300 Subject: [PATCH 16/19] some client-server --- project/client/CMakeLists.txt | 39 +++++--- project/client/include/MainWindow.h | 3 + project/client/include/ServerConnection.h | 73 +++++++++++++++ project/client/main.cpp | 6 ++ project/client/src/MainWindow.cpp | 54 ++++++++--- project/client/src/ServerConnection.cpp | 104 ++++++++++++++++++++++ 6 files changed, 254 insertions(+), 25 deletions(-) create mode 100644 project/client/include/ServerConnection.h create mode 100644 project/client/main.cpp create mode 100644 project/client/src/ServerConnection.cpp diff --git a/project/client/CMakeLists.txt b/project/client/CMakeLists.txt index e27ffc4..9d008a5 100644 --- a/project/client/CMakeLists.txt +++ b/project/client/CMakeLists.txt @@ -8,6 +8,12 @@ find_package(Qt5 COMPONENTS Widgets REQUIRED) +find_package(Boost 1.40.0 REQUIRED system) +find_package(Boost COMPONENTS log log_setup REQUIRED) +link_directories(${Boost_LIBRARY_DIR}) + +find_package(nlohmann_json 3.2.0 REQUIRED) + set(WITH_SANITIZER ON) if (WITH_SANITIZER) add_compile_options(-fsanitize=address) @@ -15,29 +21,36 @@ if (WITH_SANITIZER) endif () set(SOURCES src/MainWindow.cpp src/PaintScene.cpp src/Palette.cpp src/Changer.cpp - src/ToolsPanel.cpp src/ParametersPanel.cpp src/Brush.cpp src/Canvas.cpp src/Loader.cpp) + src/ToolsPanel.cpp src/ParametersPanel.cpp src/Brush.cpp src/Canvas.cpp src/ServerConnection.cpp) set(HEADER_FILES include/Palette.h include/MainWindow.h include/PaintScene.h include/Changer.h - include/ToolsPanel.h include/ParametersPanel.h include/Brush.h include/Canvas.h include/Loader.h) + include/ToolsPanel.h include/ParametersPanel.h include/Brush.h include/Canvas.h include/ServerConnection.h ${Boost_INCLUDE_DIR}) -add_executable(client src/main.cpp ${SOURCES} ${HEADER_FILES}) +#include_directories(include ${Boost_INCLUDE_DIR}) +#aux_source_directory(src SRC) + +add_executable(client src/main.cpp ${SOURCES} ${HEADER_FILES}) target_link_libraries(client Qt5::Core Qt5::Gui Qt5::Widgets + boost_thread + pthread + ${BOOST_LIBRARIES} + nlohmann_json::nlohmann_json ) target_include_directories(client PUBLIC include) -add_executable(test src/main_window_test.cpp ${SOURCES} ${HEADER_FILES}) -find_package(GTest) +#add_executable(test src/main_window_test.cpp ${SOURCES} ${HEADER_FILES}) +#find_package(GTest) -target_link_libraries(test PRIVATE - GTest::Main - Qt5::Core - Qt5::Gui - Qt5::Widgets - ) +#target_link_libraries(test PRIVATE +# GTest::Main +# Qt5::Core +# Qt5::Gui +# Qt5::Widgets +# ) -add_test(NAME test COMMAND test) -target_include_directories(test PUBLIC include) +#add_test(NAME test COMMAND test) +#target_include_directories(test PUBLIC include) diff --git a/project/client/include/MainWindow.h b/project/client/include/MainWindow.h index aca071c..ae088ec 100644 --- a/project/client/include/MainWindow.h +++ b/project/client/include/MainWindow.h @@ -9,6 +9,7 @@ #include "ToolsPanel.h" #include "ParametersPanel.h" #include "Canvas.h" +#include "ServerConnection.h" class MainWindow : public QMainWindow { @@ -33,6 +34,8 @@ Q_OBJECT QTimer *timer; + ServerConnection serverConnection; + private: long temporary_read_position = 0; diff --git a/project/client/include/ServerConnection.h b/project/client/include/ServerConnection.h new file mode 100644 index 0000000..41f783e --- /dev/null +++ b/project/client/include/ServerConnection.h @@ -0,0 +1,73 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +#define BUFFER_LENGTH 1024 +#define END_STR "\r\n" + +#define SERVER_PORT 8080 + + +/* + * Структура с колбеками, которые будут вызываться после успешного чтения и записи соответственно. + */ +struct ServerConnectionCallbacks { + std::function onReadCb; + std::function onWriteCb; +}; + +class ServerConnection { +public: + ServerConnection(std::string &&url, int port, ServerConnectionCallbacks &&callbacks); + + /* + * Выполняет подключение к серверу и начинает бесконечный цикл чтения сообщений с сервера. + * После того как прочитали сообщение, вызовется onReadCb из ServerConnectionCallbacks. + */ + void start(); + + /** + * Пишет серверу какое-либо сообщение. После записи вызовет onWriteCb, переданный в структуре ServerConnectionCallbacks + * @param message само сообщение + */ + void write(std::string &&message); + + void setServerUrl(std::string &&url); + + void setServerPort(int port); + + void reconnectionToServer(); + +private: + void read(); + + void connectionToServer(); + + void readHandler(const boost::system::error_code &err, size_t bytes_transferred); + + std::size_t checkEndOfRead(const boost::system::error_code &err, std::size_t bytes_transferred); + + void writeHandler(const boost::system::error_code &err); + + void run_ioc(); + + std::string serverUrl_; + int serverPort_; + + boost::asio::io_context service_; + boost::asio::ip::tcp::socket socket_; + ServerConnectionCallbacks callbacks_; + + std::mutex writeMutex_; + std::condition_variable writeCv_; + std::atomic canWrite_{true}; + + char readBuf_[BUFFER_LENGTH]; + char sendBuf_[BUFFER_LENGTH]; +}; + diff --git a/project/client/main.cpp b/project/client/main.cpp new file mode 100644 index 0000000..bc8f460 --- /dev/null +++ b/project/client/main.cpp @@ -0,0 +1,6 @@ +#include + +int main() { + std::cout << "Hello, World!" << std::endl; + return 0; +} diff --git a/project/client/src/MainWindow.cpp b/project/client/src/MainWindow.cpp index 4b611ba..a48fa6b 100644 --- a/project/client/src/MainWindow.cpp +++ b/project/client/src/MainWindow.cpp @@ -1,15 +1,15 @@ #include "MainWindow.h" -#include "QGraphicsView" -#include "Palette.h" #include "Changer.h" +#include "Palette.h" +#include "QGraphicsView" #include -#include -#include -#include -#include #include +#include +#include #include +#include +#include // Temporary #include @@ -23,8 +23,15 @@ #include +#include + +using json = nlohmann::json; + MainWindow::MainWindow(QWidget *parent) : - QMainWindow(parent) { + QMainWindow(parent), + serverConnection(std::string("127.0.0.1"), SERVER_PORT, ServerConnectionCallbacks{[this](std::string &&message) { + this->execute(std::move(message)); + }, {}}) { scene = new PaintScene(this); canvas = new Canvas; @@ -85,7 +92,12 @@ MainWindow::MainWindow(QWidget *parent) : timer = new QTimer(); connect(timer, &QTimer::timeout, this, &MainWindow::slotTimer); timer->start(500); - // TODO: client + + std::thread serverConnectionThread([this]() { this->serverConnection.start(); }); + serverConnectionThread.detach(); + + json test_target = {{"target", "sharing_document"}}; + serverConnection.write(test_target.dump() + std::string("\r\n")); } MainWindow::~MainWindow() { @@ -95,7 +107,7 @@ void MainWindow::slotTimer() { //scene->setSceneRect(0, 0, qGraphicsView->width() - 20, qGraphicsView->height() - 20); } -void MainWindow::slotBrush(qreal brushSize, const QColor& brushColor) { +void MainWindow::slotBrush(qreal brushSize, const QColor &brushColor) { if (scene->BrushStatus()) { parametersPanel->clear(); @@ -120,7 +132,7 @@ void MainWindow::slotBrush(qreal brushSize, const QColor& brushColor) { scene->ChangeBrushStatus(); } -void MainWindow::executeBrush(const Curve& curve) { +void MainWindow::executeBrush(const Curve &curve) { if (curve.brush_size < 0) { throw std::invalid_argument("Invalid size"); } @@ -138,8 +150,26 @@ void MainWindow::executeBrush(const Curve& curve) { } emit(TemporarySignal(curve)); } -void MainWindow::execute(std::string&& message) { - // TODO: execute + +void MainWindow::execute(std::string &&message) { + std::cout << "MainWindow::execute(), message = " << message << std::endl; + try { + auto command = json::parse(message); + if (command["target"] == "sharing_document" && command["status"] == "OK") { + serverConnection.setServerPort(command["port"]); + serverConnection.setServerUrl(command["address"]); + serverConnection.reconnectionToServer(); + } + + json command_json = {{"target", "auth"}, + {"auth_token", command["auth_token"]}}; + serverConnection.write(command_json.dump() + std::string("\r\n")); + } catch (...) { + std::cerr << "some exception" << std::endl; + json command_json = {{"target", "auth"}, + {"auth_token", "dummy_aboba"}}; + serverConnection.write(command_json.dump() + std::string("\r\n")); + } } //void MainWindow::resizeEvent(QResizeEvent *event) diff --git a/project/client/src/ServerConnection.cpp b/project/client/src/ServerConnection.cpp new file mode 100644 index 0000000..3d6b036 --- /dev/null +++ b/project/client/src/ServerConnection.cpp @@ -0,0 +1,104 @@ +#include +#include + +#include "ServerConnection.h" + +ServerConnection::ServerConnection(std::string &&url, int port, ServerConnectionCallbacks &&callbacks) : socket_(service_), + serverUrl_(std::move(url)), + serverPort_(port), + callbacks_(std::move(callbacks)) { +} + +void ServerConnection::connectionToServer() { + boost::asio::ip::tcp::endpoint ep(boost::asio::ip::address::from_string(serverUrl_), serverPort_); + socket_.connect(ep); + read(); +} + +void ServerConnection::read() { + boost::asio::async_read(socket_, boost::asio::buffer(readBuf_), + [this](const boost::system::error_code &err, std::size_t bytes_transferred) -> size_t { + return this->checkEndOfRead(err, bytes_transferred); + }, [this](const boost::system::error_code &err, std::size_t bytes_transferred) { + this->readHandler(err, bytes_transferred); + }); + +} + +void ServerConnection::readHandler(const boost::system::error_code &err, size_t bytes_transferred) { + if (err) { + std::cerr << "ServerConnection::readHandler error" << std::endl; + return; + } + + if (callbacks_.onReadCb) { + callbacks_.onReadCb(std::string(readBuf_, readBuf_ + bytes_transferred)); + } + + read(); +} + +void ServerConnection::run_ioc() { + service_.run(); +} + +void ServerConnection::start() { + connectionToServer(); + run_ioc(); +} + +void ServerConnection::write(std::string &&message) { + std::unique_lock ulock(writeMutex_); + writeCv_.wait(ulock, [this]() { return bool(canWrite_); }); + canWrite_ = false; + + for (int i = 0; i < message.length(); ++i) { + sendBuf_[i] = message[i]; + } + + std::cout << "ServerConnection::write()" << std::endl; + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + boost::asio::async_write(socket_, boost::asio::buffer(sendBuf_, message.length()), + [this](const boost::system::error_code &err, size_t bytes_transferred) { this->writeHandler(err); }); +} + +void ServerConnection::writeHandler(const boost::system::error_code &err) { + if (err) { + std::cerr << "ServerConnection::writeHandler error = " << err.message() << std::endl; + + return; + } + + if (callbacks_.onWriteCb) { + callbacks_.onWriteCb(err); + } + + std::cout << "ServerConnection::writeHandler()" << std::endl; + canWrite_ = true; + writeCv_.notify_one(); +} + +std::size_t ServerConnection::checkEndOfRead(const boost::system::error_code &err, std::size_t bytes_transferred) { + if (bytes_transferred > 2 && + readBuf_[bytes_transferred - 1] == '\n' && readBuf_[bytes_transferred - 2] == '\r') + //std::string(readBuf_ + bytes_transferred - 1 - std::strlen(END_STR), readBuf_ + bytes_transferred - 1) == std::string(END_STR)) + { + return 0; + } + return 1; +} + +void ServerConnection::setServerUrl(std::string &&url) { + serverUrl_ = url; +} + +void ServerConnection::setServerPort(int port) { + serverPort_ = port; +} + +void ServerConnection::reconnectionToServer() { + socket_.close(); + boost::asio::ip::tcp::endpoint ep(boost::asio::ip::address::from_string(serverUrl_), serverPort_); + socket_.connect(ep); + read(); +} From cc475dd1b4b3df11292af9f26871e665426943c7 Mon Sep 17 00:00:00 2001 From: Eremey Remzin Date: Thu, 9 Dec 2021 03:37:03 +0300 Subject: [PATCH 17/19] Connector was added --- project/client/CMakeLists.txt | 6 +- project/client/include/Connector.h | 53 ++++++++++ project/client/include/MainWindow.h | 22 +++- project/client/include/PaintScene.h | 3 + project/client/src/Canvas.cpp | 1 - project/client/src/Connector.cpp | 152 ++++++++++++++++++++++++++++ project/client/src/MainWindow.cpp | 81 +++++++++++---- project/client/src/PaintScene.cpp | 27 ++++- 8 files changed, 315 insertions(+), 30 deletions(-) create mode 100644 project/client/include/Connector.h create mode 100644 project/client/src/Connector.cpp diff --git a/project/client/CMakeLists.txt b/project/client/CMakeLists.txt index e27ffc4..042e8c7 100644 --- a/project/client/CMakeLists.txt +++ b/project/client/CMakeLists.txt @@ -15,9 +15,11 @@ if (WITH_SANITIZER) endif () set(SOURCES src/MainWindow.cpp src/PaintScene.cpp src/Palette.cpp src/Changer.cpp - src/ToolsPanel.cpp src/ParametersPanel.cpp src/Brush.cpp src/Canvas.cpp src/Loader.cpp) + src/ToolsPanel.cpp src/ParametersPanel.cpp src/Brush.cpp src/Canvas.cpp src/Loader.cpp + src/Connector.cpp) set(HEADER_FILES include/Palette.h include/MainWindow.h include/PaintScene.h include/Changer.h - include/ToolsPanel.h include/ParametersPanel.h include/Brush.h include/Canvas.h include/Loader.h) + include/ToolsPanel.h include/ParametersPanel.h include/Brush.h include/Canvas.h include/Loader.h + include/Connector.h) add_executable(client src/main.cpp ${SOURCES} ${HEADER_FILES}) target_link_libraries(client diff --git a/project/client/include/Connector.h b/project/client/include/Connector.h new file mode 100644 index 0000000..137535a --- /dev/null +++ b/project/client/include/Connector.h @@ -0,0 +1,53 @@ +// +// Created by eremey on 08.12.2021. +// +#pragma once +#include +#include +#include + +class Connector : public QToolBar { + Q_OBJECT + +public: + explicit Connector(QWidget *parent = nullptr); + ~Connector(); + +signals: + void writeSignal(std::string& message); + +public slots: + void failedShareSlot(); + void successfulShareSlot(std::string& message); + + void failedConnectSlot(); + void successfulConnectSlot(std::string& message); + +private: + QPushButton* connectButton; + QPushButton* shareButton; + QPushButton* canselButton; + //QLabel* message; + + std::string address = ""; + std::string token = ""; + +signals: + void initSignal(); +// void shareSignal(); +// void connectSignal(); +private slots: + void initSlot(); + void shareSlot(); + void connectSlot(); + + void writeConnectionSlot(); + void writeShareSlot(); + + void successfulConnectSlot(); + + + void addressChangedSlot(const QString &text); + void tokenChangedSlot(const QString &text); + +}; diff --git a/project/client/include/MainWindow.h b/project/client/include/MainWindow.h index aca071c..83fce57 100644 --- a/project/client/include/MainWindow.h +++ b/project/client/include/MainWindow.h @@ -9,6 +9,7 @@ #include "ToolsPanel.h" #include "ParametersPanel.h" #include "Canvas.h" +#include "Connector.h" class MainWindow : public QMainWindow { @@ -18,9 +19,6 @@ Q_OBJECT explicit MainWindow(QWidget *parent = 0); ~MainWindow(); -signals: - void TemporarySignal(const Curve& curve); /// - private: PaintScene *scene; Canvas* canvas; @@ -31,8 +29,19 @@ Q_OBJECT ParametersPanel *parametersPanel; + Connector *connector; + QTimer *timer; +signals: + void failedShareSignal(); + void successfulShareSignal(std::string& message); + + void failedConnectSignal(); + void successfulConnectSignal(std::string& message); + + void addCurve(std::string& message); + private: long temporary_read_position = 0; @@ -40,8 +49,13 @@ private slots: void slotBrush(qreal brushSize = 10, const QColor& brushColor = Qt::red); void slotTimer(); +// void + +public slots: + void writeSlot(std::string& message); + public: void execute(std::string&& message); - void executeBrush(const Curve& curve); + //void executeBrush(const Curve& curve); }; diff --git a/project/client/include/PaintScene.h b/project/client/include/PaintScene.h index e3c8719..9069a19 100644 --- a/project/client/include/PaintScene.h +++ b/project/client/include/PaintScene.h @@ -37,6 +37,7 @@ class PaintScene : public QGraphicsScene signals: void PushCurve(const Curve& curve); + void writeCurveSignal(std::string& curve); public slots: void SetBrushSizeSlot(int size); @@ -47,6 +48,8 @@ public slots: void PaintCurveSlot(const Curve& curve); + void readCurveSlot(std::string& message); + private: bool is_brush = false; diff --git a/project/client/src/Canvas.cpp b/project/client/src/Canvas.cpp index b4a62af..659a2b3 100644 --- a/project/client/src/Canvas.cpp +++ b/project/client/src/Canvas.cpp @@ -8,7 +8,6 @@ Canvas::Canvas(QWidget *parent) : QGraphicsView(parent) { -// default_width = scene().wr } void Canvas::openImageSlot() { diff --git a/project/client/src/Connector.cpp b/project/client/src/Connector.cpp new file mode 100644 index 0000000..e268bc0 --- /dev/null +++ b/project/client/src/Connector.cpp @@ -0,0 +1,152 @@ +// +// Created by eremey on 08.12.2021. +// + +#include "Connector.h" + +#include +#include +#include +#include + + +Connector::Connector(QWidget *parent) : QToolBar(parent) { + connect(this, &Connector::initSignal, this, &Connector::initSlot); +// connect(this, &Connector::connectSignal, this, &Connector::connectSlot); +// connect(this, &Connector::shareSignal, this, &Connector::shareSlot); + emit(initSignal()); +} + +void Connector::initSlot() { + clear(); + shareButton = new QPushButton("Share document", this); + //connect(shareButton, &QPushButton::clicked, this, &Connector::shareSlot); + connect(shareButton, &QPushButton::clicked, this, &Connector::writeShareSlot); + connectButton = new QPushButton("Connect to document", this); + connect(connectButton, &QPushButton::clicked, this, &Connector::connectSlot); + +// connect(this, &Connector::connectSignal, this, &Connector::connectSlot); +// connect(this, &Connector::shareSignal, this, &Connector::shareSlot); + + addWidget(shareButton); + addWidget(connectButton); +} +void Connector::shareSlot() { + // TODO share() + canselButton = new QPushButton("Cansel", this); + connect(canselButton, &QPushButton::clicked, this, &Connector::initSlot); + auto message = new QLabel(this); + + clear(); + QString msg = "You're sharing the document.\nAddress: \nToken: "; + message->setText(msg); + addWidget(message); + addWidget(canselButton); +} +void Connector::connectSlot() { + clear(); + auto addressLbl = new QLabel("Address:", this); + addWidget(addressLbl); + auto addressEdt = new QLineEdit(this); + connect(addressEdt, &QLineEdit::textEdited, this, &Connector::addressChangedSlot); + addWidget(addressEdt); + auto tokenLbl = new QLabel("Token:",this); + addWidget(tokenLbl); + auto tokenEdt = new QLineEdit(this); + connect(tokenEdt, &QLineEdit::textEdited, this, &Connector::tokenChangedSlot); + addWidget(tokenEdt); + + canselButton = new QPushButton("Cansel", this); + connect(canselButton, &QPushButton::clicked, this, &Connector::initSlot); + connectButton = new QPushButton("Connect", this); + connect(connectButton, &QPushButton::clicked, this, &Connector::writeConnectionSlot); + + + addSeparator(); + addWidget(connectButton); + addWidget(canselButton); + addSeparator(); +} +Connector::~Connector() { + clear(); +} +void Connector::successfulConnectSlot() { + canselButton = new QPushButton("Cansel", this); + connect(canselButton, &QPushButton::clicked, this, &Connector::initSlot); + auto message = new QLabel(this); + + clear(); + QString msg = QString::fromStdString("You've joined the document.\nAddress: " + address + "\nToken: " + token); + message->setText(msg); + addWidget(message); + addWidget(canselButton); +} +void Connector::addressChangedSlot(const QString &text) { + token = text.toStdString(); +} +void Connector::tokenChangedSlot(const QString &text) { + address = text.toStdString(); +} +void Connector::writeConnectionSlot() { + // TODO: change separator + char separator = '$'; + std::string message = address + separator + token; + emit(writeSignal(message)); +} +void Connector::writeShareSlot() { + // TODO: change share message + std::string message = "$"; + emit(writeSignal(message)); +} +void Connector::failedShareSlot() { + canselButton = new QPushButton("Cansel", this); + connect(canselButton, &QPushButton::clicked, this, &Connector::initSlot); + auto message = new QLabel(this); + + clear(); + message->setText("failed"); + addWidget(message); + addWidget(canselButton); +} +void Connector::failedConnectSlot() { + canselButton = new QPushButton("Cansel", this); + connect(canselButton, &QPushButton::clicked, this, &Connector::initSlot); + auto message = new QLabel(this); + + clear(); + message->setText("failed"); + addWidget(message); + addWidget(canselButton); +} +void Connector::successfulShareSlot(std::string &message) { + // TODO: add message parser + address = ""; + token = ""; + // + + canselButton = new QPushButton("Cansel", this); + connect(canselButton, &QPushButton::clicked, this, &Connector::initSlot); + auto label = new QLabel(this); + + clear(); + QString msg = QString::fromStdString("You're sharing the document.\nAddress: \nToken: " + token); + label->setText(msg); + addWidget(label); + addWidget(canselButton); +} +void Connector::successfulConnectSlot(std::string& message) { + // TODO: add message parser + address = ""; + token = ""; + // + + canselButton = new QPushButton("Cansel", this); + connect(canselButton, &QPushButton::clicked, this, &Connector::initSlot); + auto label = new QLabel(this); + + clear(); + QString msg = QString::fromStdString("You've joined the document.\nAddress: " + address + "\nToken: " + token); + label->setText(msg); + addWidget(label); + addWidget(canselButton); +} diff --git a/project/client/src/MainWindow.cpp b/project/client/src/MainWindow.cpp index 4b611ba..4b6481f 100644 --- a/project/client/src/MainWindow.cpp +++ b/project/client/src/MainWindow.cpp @@ -27,6 +27,9 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { scene = new PaintScene(this); + connect(scene, &PaintScene::writeCurveSignal, this, &MainWindow::writeSlot); + connect(this, &MainWindow::addCurve, scene, &PaintScene::readCurveSlot); + canvas = new Canvas; canvas->setScene(scene); @@ -67,15 +70,23 @@ MainWindow::MainWindow(QWidget *parent) : addToolBar(parameters_panel); parameters_panel->setFixedHeight(40); - auto *right_panel = new QToolBar; - addToolBar(Qt::RightToolBarArea, right_panel); - right_panel->setFixedWidth(400); + connector = new Connector(this); + addToolBar(Qt::RightToolBarArea, connector); + connector->setFixedWidth(400); + + connect(connector, &Connector::writeSignal, this, &MainWindow::writeSlot); + connect(this, &MainWindow::failedShareSignal, connector, &Connector::failedShareSlot); + connect(this, &MainWindow::failedConnectSignal, connector, &Connector::failedConnectSlot); + //connect(this, &MainWindow::successfulConnectSignal, connector, &Connector::successfulConnectSlot); // TODO + connect(this, &MainWindow::successfulShareSignal, connector, &Connector::successfulShareSlot); + + toolsPanel = new ToolsPanel; addToolBar(Qt::LeftToolBarArea, toolsPanel); connect(toolsPanel, &ToolsPanel::BrushTriggered, this, &MainWindow::slotBrush); - connect(this, &MainWindow::TemporarySignal, scene, &PaintScene::PaintCurveSlot); + //connect(this, &MainWindow::addCurve, scene, &PaintScene::PaintCurveSlot); //connect(scene, &PaintScene::PushCurve, this, &MainWindow::TemporaryWriterSlot); @@ -92,7 +103,7 @@ MainWindow::~MainWindow() { } void MainWindow::slotTimer() { - //scene->setSceneRect(0, 0, qGraphicsView->width() - 20, qGraphicsView->height() - 20); + scene->setSceneRect(0, 0, canvas->width() - 20, canvas->height() - 20); } void MainWindow::slotBrush(qreal brushSize, const QColor& brushColor) { @@ -120,26 +131,58 @@ void MainWindow::slotBrush(qreal brushSize, const QColor& brushColor) { scene->ChangeBrushStatus(); } -void MainWindow::executeBrush(const Curve& curve) { - if (curve.brush_size < 0) { - throw std::invalid_argument("Invalid size"); +//void MainWindow::executeBrush(const Curve& curve) { +// if (curve.brush_size < 0) { +// throw std::invalid_argument("Invalid size"); +// } +// if (curve.color_red < 0 || curve.color_red > 255) { +// throw std::invalid_argument("Invalid red color"); +// } +// if (curve.color_green < 0 || curve.color_green > 255) { +// throw std::invalid_argument("Invalid green color"); +// } +// if (curve.color_blue < 0 || curve.color_blue > 255) { +// throw std::invalid_argument("Invalid blue color"); +// } +// if (curve.coords.size() < 2) { +// throw std::invalid_argument("Invalid curve size"); +// } +// emit(addCurve(curve)); +//} +void MainWindow::execute(std::string&& message) { + // TODO: add parser + + // TODO: if sharing failed + if (false) { + emit(failedShareSignal()); } - if (curve.color_red < 0 || curve.color_red > 255) { - throw std::invalid_argument("Invalid red color"); + + // TODO: if sharing succeeded + if (false) { + std::string message = ""; // TODO: add message format + emit(successfulShareSignal(message)); } - if (curve.color_green < 0 || curve.color_green > 255) { - throw std::invalid_argument("Invalid green color"); + + // TODO: if joining failed + if (false) { + emit(failedConnectSignal()); } - if (curve.color_blue < 0 || curve.color_blue > 255) { - throw std::invalid_argument("Invalid blue color"); + + // TODO: if joining succeeded + if (false) { + std::string message = ""; // TODO: add message format + emit(successfulConnectSignal(message)); } - if (curve.coords.size() < 2) { - throw std::invalid_argument("Invalid curve size"); + + // TODO: if need to draw a line + if (false) { + std::string message = "";// TODO: add parser message->curve + emit(addCurve(message)); } - emit(TemporarySignal(curve)); + } -void MainWindow::execute(std::string&& message) { - // TODO: execute +void MainWindow::writeSlot(std::string &message) { + // TODO: client write } //void MainWindow::resizeEvent(QResizeEvent *event) diff --git a/project/client/src/PaintScene.cpp b/project/client/src/PaintScene.cpp index 240ca7d..16735f6 100644 --- a/project/client/src/PaintScene.cpp +++ b/project/client/src/PaintScene.cpp @@ -30,9 +30,11 @@ void PaintScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { void PaintScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { - // TODO: write - PushCurve({brush_size, brush_color.red(), brush_color.green(), brush_color.blue(), - line}); + // TODO: add curve serialization + std::string message = "curve"; + emit(writeCurveSignal(message)); +// PushCurve({brush_size, brush_color.red(), brush_color.green(), brush_color.blue(), +// line}); line.clear(); } void PaintScene::ChangeBrushStatus() { @@ -74,7 +76,6 @@ void PaintScene::SetBrush(qreal brushSize, const QColor& brushColor) { brush_color = brushColor; } void PaintScene::PaintCurveSlot(const Curve &curve) { - // TODO: read assert(curve.coords.size() > 1); assert(curve.color_red < 256 && curve.color_red >= 0); assert(curve.color_green < 256 && curve.color_green >= 0); @@ -90,3 +91,21 @@ void PaintScene::PaintCurveSlot(const Curve &curve) { curve.brush_size, Qt::SolidLine, Qt::RoundCap)); } } +void PaintScene::readCurveSlot(std::string &message) { + + Curve curve; // TODO: add message parser + assert(curve.coords.size() > 1); + assert(curve.color_red < 256 && curve.color_red >= 0); + assert(curve.color_green < 256 && curve.color_green >= 0); + assert(curve.color_blue < 256 && curve.color_blue >= 0); + + for (int i = 1; i < curve.coords.size(); ++i) { + // std::cout << curve.coords[i].x() << "\n"; + addLine(curve.coords[i].x(), + curve.coords[i].y() , + curve.coords[i - 1].x(), + curve.coords[i - 1].y(), + QPen(QColor(curve.color_red, curve.color_green, curve.color_blue), + curve.brush_size, Qt::SolidLine, Qt::RoundCap)); + } +} From 84360b3109197bf2f543a9206617e4f071a841b4 Mon Sep 17 00:00:00 2001 From: Eremey Remzin Date: Thu, 9 Dec 2021 14:42:42 +0300 Subject: [PATCH 18/19] Curve reader/writer were added, connector was changed --- project/client/include/Connector.h | 3 -- project/client/include/PaintScene.h | 13 ++--- project/client/src/Connector.cpp | 49 ++++++----------- project/client/src/MainWindow.cpp | 11 ++-- project/client/src/PaintScene.cpp | 83 +++++++++++++++-------------- 5 files changed, 72 insertions(+), 87 deletions(-) diff --git a/project/client/include/Connector.h b/project/client/include/Connector.h index 137535a..49179bb 100644 --- a/project/client/include/Connector.h +++ b/project/client/include/Connector.h @@ -38,14 +38,11 @@ public slots: // void connectSignal(); private slots: void initSlot(); - void shareSlot(); void connectSlot(); void writeConnectionSlot(); void writeShareSlot(); - void successfulConnectSlot(); - void addressChangedSlot(const QString &text); void tokenChangedSlot(const QString &text); diff --git a/project/client/include/PaintScene.h b/project/client/include/PaintScene.h index 9069a19..226ddac 100644 --- a/project/client/include/PaintScene.h +++ b/project/client/include/PaintScene.h @@ -13,10 +13,11 @@ struct Point { }; struct Curve { - double brush_size = 0; + int32_t brush_size = 0; int32_t color_red = 0; int32_t color_green = 0; int32_t color_blue = 0; + int32_t opacity = 0; std::vector coords; }; @@ -36,7 +37,7 @@ class PaintScene : public QGraphicsScene void SetBrush(qreal brushSize = 10, const QColor& brushColor = Qt::red); signals: - void PushCurve(const Curve& curve); +// void PushCurve(const Curve& curve); void writeCurveSignal(std::string& curve); public slots: @@ -46,18 +47,18 @@ public slots: void SetGreenSlot(int transparency); void SetBlueSlot(int transparency); - void PaintCurveSlot(const Curve& curve); +// void PaintCurveSlot(const Curve& curve); void readCurveSlot(std::string& message); private: bool is_brush = false; - QPointF previous_point; + QPoint previous_point; - std::vector line; + std::vector curve; - qreal brush_size = 10; + int32_t brush_size = 10; QColor brush_color; diff --git a/project/client/src/Connector.cpp b/project/client/src/Connector.cpp index e268bc0..10c7bf3 100644 --- a/project/client/src/Connector.cpp +++ b/project/client/src/Connector.cpp @@ -9,6 +9,7 @@ #include #include +#include "iostream" Connector::Connector(QWidget *parent) : QToolBar(parent) { connect(this, &Connector::initSignal, this, &Connector::initSlot); @@ -31,18 +32,7 @@ void Connector::initSlot() { addWidget(shareButton); addWidget(connectButton); } -void Connector::shareSlot() { - // TODO share() - canselButton = new QPushButton("Cansel", this); - connect(canselButton, &QPushButton::clicked, this, &Connector::initSlot); - auto message = new QLabel(this); - clear(); - QString msg = "You're sharing the document.\nAddress: \nToken: "; - message->setText(msg); - addWidget(message); - addWidget(canselButton); -} void Connector::connectSlot() { clear(); auto addressLbl = new QLabel("Address:", this); @@ -70,17 +60,7 @@ void Connector::connectSlot() { Connector::~Connector() { clear(); } -void Connector::successfulConnectSlot() { - canselButton = new QPushButton("Cansel", this); - connect(canselButton, &QPushButton::clicked, this, &Connector::initSlot); - auto message = new QLabel(this); - clear(); - QString msg = QString::fromStdString("You've joined the document.\nAddress: " + address + "\nToken: " + token); - message->setText(msg); - addWidget(message); - addWidget(canselButton); -} void Connector::addressChangedSlot(const QString &text) { token = text.toStdString(); } @@ -88,13 +68,13 @@ void Connector::tokenChangedSlot(const QString &text) { address = text.toStdString(); } void Connector::writeConnectionSlot() { - // TODO: change separator - char separator = '$'; + // TODO: 2) change connection message + char separator = '|'; std::string message = address + separator + token; emit(writeSignal(message)); } void Connector::writeShareSlot() { - // TODO: change share message + // TODO: 3) change share message std::string message = "$"; emit(writeSignal(message)); } @@ -118,27 +98,30 @@ void Connector::failedConnectSlot() { addWidget(message); addWidget(canselButton); } + +void separate(const std::string &message, std::string& l, std::string& r, char separator) { + l = message.substr(0, message.find(separator)); + r = message.substr(message.find(separator) + 1, message.size() - 1); +} + void Connector::successfulShareSlot(std::string &message) { - // TODO: add message parser - address = ""; - token = ""; - // +// std::string separator = "|"; +// address = message.substr(0, message.find(separator)); +// token = message.substr(message.find(separator) + 1, message.size() - 1); + separate(message, address, token, '|'); canselButton = new QPushButton("Cansel", this); connect(canselButton, &QPushButton::clicked, this, &Connector::initSlot); auto label = new QLabel(this); clear(); - QString msg = QString::fromStdString("You're sharing the document.\nAddress: \nToken: " + token); + QString msg = QString::fromStdString("You're sharing the document.\nAddress: "+ address + "\nToken: " + token); label->setText(msg); addWidget(label); addWidget(canselButton); } void Connector::successfulConnectSlot(std::string& message) { - // TODO: add message parser - address = ""; - token = ""; - // + separate(message, address, token, '|'); canselButton = new QPushButton("Cansel", this); connect(canselButton, &QPushButton::clicked, this, &Connector::initSlot); diff --git a/project/client/src/MainWindow.cpp b/project/client/src/MainWindow.cpp index 4b6481f..28729df 100644 --- a/project/client/src/MainWindow.cpp +++ b/project/client/src/MainWindow.cpp @@ -77,7 +77,7 @@ MainWindow::MainWindow(QWidget *parent) : connect(connector, &Connector::writeSignal, this, &MainWindow::writeSlot); connect(this, &MainWindow::failedShareSignal, connector, &Connector::failedShareSlot); connect(this, &MainWindow::failedConnectSignal, connector, &Connector::failedConnectSlot); - //connect(this, &MainWindow::successfulConnectSignal, connector, &Connector::successfulConnectSlot); // TODO + connect(this, &MainWindow::successfulConnectSignal, connector, &Connector::successfulConnectSlot); connect(this, &MainWindow::successfulShareSignal, connector, &Connector::successfulShareSlot); @@ -96,7 +96,6 @@ MainWindow::MainWindow(QWidget *parent) : timer = new QTimer(); connect(timer, &QTimer::timeout, this, &MainWindow::slotTimer); timer->start(500); - // TODO: client } MainWindow::~MainWindow() { @@ -150,7 +149,7 @@ void MainWindow::slotBrush(qreal brushSize, const QColor& brushColor) { // emit(addCurve(curve)); //} void MainWindow::execute(std::string&& message) { - // TODO: add parser + // TODO: 4) add parser // TODO: if sharing failed if (false) { @@ -159,7 +158,7 @@ void MainWindow::execute(std::string&& message) { // TODO: if sharing succeeded if (false) { - std::string message = ""; // TODO: add message format + std::string message = "address|token"; //// create a message from the command emit(successfulShareSignal(message)); } @@ -170,13 +169,13 @@ void MainWindow::execute(std::string&& message) { // TODO: if joining succeeded if (false) { - std::string message = ""; // TODO: add message format + std::string message = "address|token"; //// create a message from the command emit(successfulConnectSignal(message)); } // TODO: if need to draw a line if (false) { - std::string message = "";// TODO: add parser message->curve + std::string message = ""; //// create a message from the command emit(addCurve(message)); } diff --git a/project/client/src/PaintScene.cpp b/project/client/src/PaintScene.cpp index 16735f6..5dd5406 100644 --- a/project/client/src/PaintScene.cpp +++ b/project/client/src/PaintScene.cpp @@ -1,6 +1,7 @@ #include "PaintScene.h" #include #include +#include void PaintScene::mousePressEvent(QGraphicsSceneMouseEvent *event) { if (is_brush) { @@ -10,8 +11,8 @@ void PaintScene::mousePressEvent(QGraphicsSceneMouseEvent *event) { brush_size, QPen(Qt::NoPen), QBrush(brush_color)); - previous_point = event->scenePos(); - line.push_back(previous_point); + previous_point = event->scenePos().toPoint(); + curve.push_back(previous_point); } } @@ -23,24 +24,34 @@ void PaintScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { event->scenePos().x(), event->scenePos().y(), QPen(brush_color, brush_size, Qt::SolidLine, Qt::RoundCap)); - previous_point = event->scenePos(); - line.push_back(previous_point); + previous_point = event->scenePos().toPoint(); + curve.push_back(previous_point); } } void PaintScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { - // TODO: add curve serialization - std::string message = "curve"; + char separator = ' '; + std::string message = std::to_string(brush_size) + separator + + std::to_string(brush_color.red()) + separator + + std::to_string(brush_color.green()) + separator + + std::to_string(brush_color.blue()) + separator + + std::to_string(brush_color.alpha()) + separator; + + + for (auto& point: curve) { + message += std::to_string(point.x()) + separator + std::to_string(point.y()) + separator; + } + // TODO: 1) message -> command emit(writeCurveSignal(message)); -// PushCurve({brush_size, brush_color.red(), brush_color.green(), brush_color.blue(), -// line}); - line.clear(); + + curve.clear(); } void PaintScene::ChangeBrushStatus() { is_brush = !is_brush; } PaintScene::PaintScene(QObject *parent) { + //connect(this, &PaintScene::writeCurveSignal, this, &PaintScene::readCurveSlot); // brush_color.setRgb(0,255,0); } @@ -75,37 +86,31 @@ void PaintScene::SetBrush(qreal brushSize, const QColor& brushColor) { brush_size = brushSize; brush_color = brushColor; } -void PaintScene::PaintCurveSlot(const Curve &curve) { - assert(curve.coords.size() > 1); - assert(curve.color_red < 256 && curve.color_red >= 0); - assert(curve.color_green < 256 && curve.color_green >= 0); - assert(curve.color_blue < 256 && curve.color_blue >= 0); - - for (int i = 1; i < curve.coords.size(); ++i) { -// std::cout << curve.coords[i].x() << "\n"; - addLine(curve.coords[i].x(), - curve.coords[i].y() , - curve.coords[i - 1].x(), - curve.coords[i - 1].y(), - QPen(QColor(curve.color_red, curve.color_green, curve.color_blue), - curve.brush_size, Qt::SolidLine, Qt::RoundCap)); - } -} + void PaintScene::readCurveSlot(std::string &message) { + std::stringstream curveStr; + curveStr << message; // bad idea + + Curve brushCurve; + curveStr >> brushCurve.brush_size >> brushCurve.color_red >> brushCurve.color_green >> brushCurve.color_blue >> brushCurve.opacity; + int32_t point_x = 0; + int32_t point_y = 0; + + while (curveStr >> point_x >> point_y){ + brushCurve.coords.emplace_back(point_x, point_y); + } + + assert(brushCurve.coords.size() > 1); + assert(brushCurve.color_red < 256 && brushCurve.color_red >= 0); + assert(brushCurve.color_green < 256 && brushCurve.color_green >= 0); + assert(brushCurve.color_blue < 256 && brushCurve.color_blue >= 0); - Curve curve; // TODO: add message parser - assert(curve.coords.size() > 1); - assert(curve.color_red < 256 && curve.color_red >= 0); - assert(curve.color_green < 256 && curve.color_green >= 0); - assert(curve.color_blue < 256 && curve.color_blue >= 0); - - for (int i = 1; i < curve.coords.size(); ++i) { - // std::cout << curve.coords[i].x() << "\n"; - addLine(curve.coords[i].x(), - curve.coords[i].y() , - curve.coords[i - 1].x(), - curve.coords[i - 1].y(), - QPen(QColor(curve.color_red, curve.color_green, curve.color_blue), - curve.brush_size, Qt::SolidLine, Qt::RoundCap)); + for (int i = 1; i < brushCurve.coords.size(); ++i) { + addLine(brushCurve.coords[i].x(), + brushCurve.coords[i].y() , + brushCurve.coords[i - 1].x(), + brushCurve.coords[i - 1].y(), + QPen(QColor(brushCurve.color_red, brushCurve.color_green, brushCurve.color_blue, brushCurve.opacity), + brushCurve.brush_size, Qt::SolidLine, Qt::RoundCap)); } } From e4343dd8ad0e8f973acd94bdaef4e54631ab41b7 Mon Sep 17 00:00:00 2001 From: Dmitriy Ilyin Date: Thu, 9 Dec 2021 16:17:30 +0300 Subject: [PATCH 19/19] fix bug with send message from shared document --- project/client/include/MainWindow.h | 3 +- project/client/include/ServerConnection.h | 2 -- project/client/src/MainWindow.cpp | 36 ++++++++++++----------- project/client/src/ServerConnection.cpp | 17 +++++------ 4 files changed, 29 insertions(+), 29 deletions(-) diff --git a/project/client/include/MainWindow.h b/project/client/include/MainWindow.h index ae088ec..271c699 100644 --- a/project/client/include/MainWindow.h +++ b/project/client/include/MainWindow.h @@ -34,7 +34,8 @@ Q_OBJECT QTimer *timer; - ServerConnection serverConnection; + std::unique_ptr serverConnection_; + std::unique_ptr forDocumentConnection_; private: long temporary_read_position = 0; diff --git a/project/client/include/ServerConnection.h b/project/client/include/ServerConnection.h index 41f783e..60df071 100644 --- a/project/client/include/ServerConnection.h +++ b/project/client/include/ServerConnection.h @@ -41,8 +41,6 @@ class ServerConnection { void setServerPort(int port); - void reconnectionToServer(); - private: void read(); diff --git a/project/client/src/MainWindow.cpp b/project/client/src/MainWindow.cpp index a48fa6b..7e6b6a3 100644 --- a/project/client/src/MainWindow.cpp +++ b/project/client/src/MainWindow.cpp @@ -28,10 +28,7 @@ using json = nlohmann::json; MainWindow::MainWindow(QWidget *parent) : - QMainWindow(parent), - serverConnection(std::string("127.0.0.1"), SERVER_PORT, ServerConnectionCallbacks{[this](std::string &&message) { - this->execute(std::move(message)); - }, {}}) { + QMainWindow(parent) { scene = new PaintScene(this); canvas = new Canvas; @@ -93,11 +90,15 @@ MainWindow::MainWindow(QWidget *parent) : connect(timer, &QTimer::timeout, this, &MainWindow::slotTimer); timer->start(500); - std::thread serverConnectionThread([this]() { this->serverConnection.start(); }); - serverConnectionThread.detach(); + serverConnection_ = std::make_unique(std::string("127.0.0.1"), 8080, ServerConnectionCallbacks{ + [this](std::string &&message) { this->execute(std::move(message)); }, + {} + }); + + serverConnection_->start(); json test_target = {{"target", "sharing_document"}}; - serverConnection.write(test_target.dump() + std::string("\r\n")); + serverConnection_->write(test_target.dump()); } MainWindow::~MainWindow() { @@ -156,19 +157,20 @@ void MainWindow::execute(std::string &&message) { try { auto command = json::parse(message); if (command["target"] == "sharing_document" && command["status"] == "OK") { - serverConnection.setServerPort(command["port"]); - serverConnection.setServerUrl(command["address"]); - serverConnection.reconnectionToServer(); + forDocumentConnection_ = std::make_unique(std::string("127.0.0.1"), 8080, ServerConnectionCallbacks{ + [this](std::string &&message) { this->execute(std::move(message)); }, + {} + }); + forDocumentConnection_->setServerPort(command["port"]); + forDocumentConnection_->setServerUrl(command["address"]); + forDocumentConnection_->start(); + + json command_json = {{"target", "auth"}, + {"auth_token", command["auth_token"]}}; + forDocumentConnection_->write(command_json.dump() + std::string("\r\n")); } - - json command_json = {{"target", "auth"}, - {"auth_token", command["auth_token"]}}; - serverConnection.write(command_json.dump() + std::string("\r\n")); } catch (...) { std::cerr << "some exception" << std::endl; - json command_json = {{"target", "auth"}, - {"auth_token", "dummy_aboba"}}; - serverConnection.write(command_json.dump() + std::string("\r\n")); } } diff --git a/project/client/src/ServerConnection.cpp b/project/client/src/ServerConnection.cpp index 3d6b036..d08300a 100644 --- a/project/client/src/ServerConnection.cpp +++ b/project/client/src/ServerConnection.cpp @@ -39,7 +39,8 @@ void ServerConnection::readHandler(const boost::system::error_code &err, size_t } void ServerConnection::run_ioc() { - service_.run(); + std::thread run_thread([this]() { service_.run(); }); + run_thread.detach(); } void ServerConnection::start() { @@ -55,10 +56,13 @@ void ServerConnection::write(std::string &&message) { for (int i = 0; i < message.length(); ++i) { sendBuf_[i] = message[i]; } + std::string end_str(END_STR); + for (int i = message.length(); i < message.length() + end_str.length(); ++i) { + sendBuf_[i] = end_str[i - message.length()]; + } std::cout << "ServerConnection::write()" << std::endl; - std::this_thread::sleep_for(std::chrono::milliseconds(1000)); - boost::asio::async_write(socket_, boost::asio::buffer(sendBuf_, message.length()), + boost::asio::async_write(socket_, boost::asio::buffer(sendBuf_, message.length() + end_str.length()), [this](const boost::system::error_code &err, size_t bytes_transferred) { this->writeHandler(err); }); } @@ -96,9 +100,4 @@ void ServerConnection::setServerPort(int port) { serverPort_ = port; } -void ServerConnection::reconnectionToServer() { - socket_.close(); - boost::asio::ip::tcp::endpoint ep(boost::asio::ip::address::from_string(serverUrl_), serverPort_); - socket_.connect(ep); - read(); -} +