From 2a2726870e725f32f15fa52384022f24d5786513 Mon Sep 17 00:00:00 2001 From: AntonMrt <104432560+AntonMrt@users.noreply.github.com> Date: Thu, 21 Nov 2024 15:57:51 +0300 Subject: [PATCH] Progress bar (#135) --- gui/CMakeLists.txt | 2 + gui/cool_progressbar.cpp | 69 +++++++++++++++++++++++++++++++++ gui/cool_progressbar.h | 29 ++++++++++++++ gui/davis_gui.cpp | 84 +++++++++++++++++++++++++++++++++------- gui/davis_gui.h | 24 +++++++----- gui/davis_gui.ui | 5 ++- gui/main.cpp | 2 +- 7 files changed, 188 insertions(+), 27 deletions(-) create mode 100644 gui/cool_progressbar.cpp create mode 100644 gui/cool_progressbar.h diff --git a/gui/CMakeLists.txt b/gui/CMakeLists.txt index 0381f39..66e2a84 100644 --- a/gui/CMakeLists.txt +++ b/gui/CMakeLists.txt @@ -49,6 +49,8 @@ set(PROJECT_SOURCES about_window.ui json_utils.h json_utils.cpp + cool_progressbar.h + cool_progressbar.cpp ) qt5_add_resources(PROJECT_SOURCES res.qrc) diff --git a/gui/cool_progressbar.cpp b/gui/cool_progressbar.cpp new file mode 100644 index 0000000..48cecc7 --- /dev/null +++ b/gui/cool_progressbar.cpp @@ -0,0 +1,69 @@ +#include "cool_progressbar.h" +#include +#include +#include + + +coolProgressBar::coolProgressBar(const QColor& backgroundColor, + const QColor& animatedColor, + int animationTimeMs, + QWidget* parent) { + setVisible(false); + m_backgroundColor = backgroundColor; + m_animatedColor = animatedColor; + m_animationTimeMs = animationTimeMs; + QString styleBack( + " background-color: %1;" + " border-radius: 1px;" + " border: none;" + ); + setStyleSheet(styleBack.arg(m_backgroundColor.name())); + + + QWidget* movingSquare = new QWidget(this); + + m_movingSquare = movingSquare; + QString styleMoving( + " background-color: %1;" + " border-radius: 1px;" + " border: none;" + ); + m_movingSquare->setStyleSheet(styleMoving.arg(m_animatedColor.name())); + + m_animation = new QPropertyAnimation(m_movingSquare, "geometry"); + m_animation->setDuration(m_animationTimeMs); + m_animation->setLoopCount(-1); +} + +void coolProgressBar::startAnimation() { + m_animation->setStartValue(QRect(-m_movingSquare->width(), + m_movingSquare->y(), + m_movingSquare->width(), + m_movingSquare->height())); + m_animation->setEndValue(QRect(width(), + m_movingSquare->y(), + m_movingSquare->width(), + m_movingSquare->height())); + m_animation->setEasingCurve(QEasingCurve::InOutCubic); + m_animation->start(); + setVisible(true); +} + +void coolProgressBar::stopAnimation() { + m_animation->stop(); + setVisible(false); +} + +void coolProgressBar::paintEvent(QPaintEvent* event) { + QStyleOption opt; + opt.init(this); + QPainter p(this); + style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); +} + +void coolProgressBar::showEvent(QShowEvent* event) { + qDebug() << width(); + m_movingSquare->setGeometry(0, 0, width() / 2, height()); + qDebug() << "blue - " << m_movingSquare->geometry(); +} + diff --git a/gui/cool_progressbar.h b/gui/cool_progressbar.h new file mode 100644 index 0000000..88bf4bf --- /dev/null +++ b/gui/cool_progressbar.h @@ -0,0 +1,29 @@ +#ifndef COOLPROGRESSBAR_H +#define COOLPROGRESSBAR_H + +#include +#include +#include + +class coolProgressBar : public QWidget { + Q_OBJECT + public: + explicit coolProgressBar(const QColor& backgroundColor, + const QColor& animatedColor, + int animationTimeMs, + QWidget* parent = nullptr); + public slots: + void startAnimation(); + void stopAnimation(); + protected: + void paintEvent(QPaintEvent* event) override; + void showEvent(QShowEvent* event) override; + private: + QColor m_backgroundColor; + QColor m_animatedColor; + int m_animationTimeMs; + QWidget* m_movingSquare; + QPropertyAnimation* m_animation; +}; + +#endif // COOLPROGRESSBAR_H diff --git a/gui/davis_gui.cpp b/gui/davis_gui.cpp index 1ca1673..7bd5900 100644 --- a/gui/davis_gui.cpp +++ b/gui/davis_gui.cpp @@ -24,6 +24,9 @@ #include #include "QDateTime" #include +#include +#include +#include #include "json_utils.h" @@ -33,9 +36,9 @@ const int ANIMATION_DURATION = 300; DavisGUI::DavisGUI(QWidget* parent) : QMainWindow(parent) , ui(new Ui::DavisGUI) { + ui->setupUi(this); isAboutWindowShowed = false; m_isMinStyleWindow = false; - ui->setupUi(this); this->setAcceptDrops(true); QHBoxLayout* hbl = ui->horizontalLayout_menu; QMenuBar* mb = new QMenuBar; @@ -69,6 +72,7 @@ DavisGUI::DavisGUI(QWidget* parent) hbl->addWidget(mb); hbl->addItem(new QSpacerItem(2, 25, QSizePolicy::Expanding, QSizePolicy::Expanding)); + ui->label_text->setStyleSheet("background-color: rgba(255, 255, 255, 0);"); QString buttonStyle( "QPushButton {" @@ -133,12 +137,16 @@ DavisGUI::DavisGUI(QWidget* parent) hbl->addWidget(qpbExit); this->setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint); + barCool = new coolProgressBar(QColor(52, 52, 52), QColor(42, 130, 218), 2500, this); + barCool->setGeometry(70, 220, 270, 2); + this->layout()->addWidget(barCool); + connect(this, &DavisGUI::showProgressBar, barCool, &coolProgressBar::startAnimation); + connect(this, &DavisGUI::hideProgressBar, barCool, &coolProgressBar::stopAnimation); qpbOpen = new AnimatedButton("Open", QColor(120, 120, 120), QColor(42, 130, 218), this); qpbOpen->setGeometry(70, 180, 90, 30); qpbOpen->setOriginalGeometry(qpbOpen->geometry()); - qpbBuffer = new AnimatedButton("Copy from buffer or Ctrl+V", QColor(120, 120, 120), QColor(42, 130, 218), @@ -166,6 +174,7 @@ void DavisGUI::hideElementsDuringResize() { ui->label_text->setVisible(false); qpbBuffer->setVisible(false); qpbOpen->setVisible(false); + barCool->setVisible(false); update(); } @@ -199,6 +208,7 @@ void DavisGUI::setMaxStyleWindow(int animDuration) { ui->label_arrow->setGeometry(170, 90, 50, 50); ui->label_graph->setGeometry(210, 70, 81, 81); ui->label_text->setGeometry(0, 230, 391, 111); + barCool->setGeometry(97, 155, 187, 2); update(); }); @@ -234,6 +244,7 @@ void DavisGUI::setMinStyleWindow(int animDuration) { ui->label_doc->setGeometry(30, 60, 41, 41); ui->label_arrow->setGeometry(60, 60, 41, 41); ui->label_graph->setGeometry(90, 60, 41, 41); + barCool->setGeometry(35, 105, 90, 2); update(); }); QParallelAnimationGroup* group = new QParallelAnimationGroup; @@ -253,17 +264,21 @@ void DavisGUI::showAboutWindow() { } void DavisGUI::pasteFromClipboard() { - QClipboard* clipboard = QApplication::clipboard(); - QString clipboardText = clipboard->text(); - qDebug() << clipboardText; - QStringList lines = clipboardText.split(QRegExp("[\r\n]+")); - if (checkDateTimeVariant(lines) == false) { - readPlotText(lines); + auto lambdaFunction = [this]() { + emit showProgressBar(); + QClipboard* clipboard = QApplication::clipboard(); + QString clipboardText = clipboard->text(); + QStringList lines = clipboardText.split(QRegExp("[\r\n]+")); + if (checkDateTimeVariant(lines) == false) { + readPlotText(lines); + }; }; + QFuture future = QtConcurrent::run(lambdaFunction); + QFutureWatcher* watcher = new QFutureWatcher(this); + connect(watcher, &QFutureWatcher::finished, this, &DavisGUI::hideProgressBar); + watcher->setFuture(future); } - - void DavisGUI::readPlotText(QStringList& str_lines) { std::vectorlines; std::vector> data; @@ -381,14 +396,16 @@ bool DavisGUI::checkDateTimeVariant(const QStringList& lines) { } void DavisGUI::selectAndShowFiles() { - QApplication::processEvents(); + QStringList fileNames = QFileDialog::getOpenFileNames(this, QObject::tr("Open Files"), "", QObject::tr("All Files (*)")); - - visualizeFiles(fileNames); - + emit showProgressBar(); + QFuture future = QtConcurrent::run(this, &DavisGUI::visualizeFiles, fileNames); + QFutureWatcher* watcher = new QFutureWatcher(this); + connect(watcher, &QFutureWatcher::finished, this, &DavisGUI::hideProgressBar); + watcher->setFuture(future); } bool DavisGUI::isFileContainsSingleChart(const QString& pathToFile, @@ -482,6 +499,35 @@ bool DavisGUI::isFileContainsSingleChart(const QString& pathToFile, void DavisGUI::dragEnterEvent(QDragEnterEvent* event) { + + QSequentialAnimationGroup* group = new QSequentialAnimationGroup(this); + + QRect originalGeometry = geometry(); + int shakeDistance = 6; // Distance to shake + int duration = 70; // Duration of each shake step + + for (int i = 0; i < 6; ++i) { + QPropertyAnimation* animation = new QPropertyAnimation(this, "geometry"); + animation->setDuration(duration); + if (i % 4 == 0) { + animation->setStartValue(originalGeometry.translated(-shakeDistance, 0)); + animation->setEndValue(originalGeometry.translated(shakeDistance, 0)); + } else if (i % 4 == 1) { + animation->setStartValue(originalGeometry.translated(shakeDistance, 0)); + animation->setEndValue(originalGeometry.translated(-shakeDistance, 0)); + } else if (i % 4 == 2) { + animation->setStartValue(originalGeometry.translated(0, -shakeDistance)); + animation->setEndValue(originalGeometry.translated(0, shakeDistance)); + } else if (i % 4 == 3) { + animation->setStartValue(originalGeometry.translated(0, shakeDistance)); + animation->setEndValue(originalGeometry.translated(0, -shakeDistance)); + } + group->addAnimation(animation); + } + + group->start(QAbstractAnimation::DeleteWhenStopped); + + if (event->mimeData()->hasUrls()) { event->acceptProposedAction(); } else { @@ -491,12 +537,17 @@ void DavisGUI::dragEnterEvent(QDragEnterEvent* event) { void DavisGUI::dropEvent(QDropEvent* event) { + emit showProgressBar(); QList file_list_urls = event->mimeData()->urls(); QStringList file_list; for (int i = 0; i < file_list_urls.size(); ++i) { file_list.append(file_list_urls[i].toLocalFile()); } - visualizeFiles(file_list); + + QFuture future = QtConcurrent::run(this, &DavisGUI::visualizeFiles, file_list); + QFutureWatcher* watcher = new QFutureWatcher(this); + connect(watcher, &QFutureWatcher::finished, this, &DavisGUI::hideProgressBar); + watcher->setFuture(future); } void DavisGUI::visualizeFiles(const QStringList& file_list) { @@ -576,11 +627,14 @@ void DavisGUI::paintEvent(QPaintEvent* event) { painter.setRenderHint(QPainter::Antialiasing); QPainterPath path; path.addRoundedRect(rectangle, 5, 5); + QPen dashpen; dashpen.setStyle(Qt::DashLine); dashpen.setColor(QColor(150, 150, 150)); dashpen.setWidth(2); painter.setPen(dashpen); + painter.fillPath(path, QColor(160, 60, 60)); + painter.drawPath(path); painter.fillPath(path, QColor(60, 60, 60)); painter.drawPath(path); painter.end(); diff --git a/gui/davis_gui.h b/gui/davis_gui.h index 16ab5ff..836d7d7 100644 --- a/gui/davis_gui.h +++ b/gui/davis_gui.h @@ -5,6 +5,7 @@ #include "about_window.h" #include "QAction" #include "animated_button.h" +#include "cool_progressbar.h" QT_BEGIN_NAMESPACE namespace Ui { class DavisGUI; } @@ -13,11 +14,23 @@ QT_END_NAMESPACE class DavisGUI : public QMainWindow { Q_OBJECT + signals: + void showProgressBar(); + void hideProgressBar(); + public: DavisGUI(QWidget* parent = nullptr); ~DavisGUI(); void show(); + protected: + void dragEnterEvent(QDragEnterEvent* event) override; + void dropEvent(QDropEvent* event) override; + void paintEvent(QPaintEvent* event) override; + void mousePressEvent(QMouseEvent* event) override; + void mouseMoveEvent(QMouseEvent* event) override; + void keyPressEvent(QKeyEvent* event) override; + private: void setMaxStyleWindow(int animDuration); void setMinStyleWindow(int animDuration); @@ -45,15 +58,6 @@ class DavisGUI : public QMainWindow { bool m_isMinStyleWindow; AnimatedButton* qpbBuffer; AnimatedButton* qpbOpen; - - - // QWidget interface - protected: - void dragEnterEvent(QDragEnterEvent* event) override; - void dropEvent(QDropEvent* event) override; - void paintEvent(QPaintEvent* event) override; - void mousePressEvent(QMouseEvent* event) override; - void mouseMoveEvent(QMouseEvent* event) override; - void keyPressEvent(QKeyEvent* event) override; + coolProgressBar* barCool; }; #endif // DAVISGUI_H diff --git a/gui/davis_gui.ui b/gui/davis_gui.ui index b4521bb..d2fef4e 100644 --- a/gui/davis_gui.ui +++ b/gui/davis_gui.ui @@ -54,8 +54,11 @@ 24 + + background-color: rgb(255, 255, 0) ; + - <html><head/><body><p align="center"><span style=" font-size:20pt; color:#969696;">drag &amp; drop<br/>text file(s) containing<br/>numbers for visualize</span></p></body></html> + <html><head/><body><p align="center"><span style=" font-size:16pt; color:#969696;">drag &amp; drop<br/>text file(s) containing<br/>numbers for visualize</span></p></body></html> diff --git a/gui/main.cpp b/gui/main.cpp index ad1d778..26b8d1b 100644 --- a/gui/main.cpp +++ b/gui/main.cpp @@ -11,7 +11,7 @@ void applyDark() { qApp->setStyle(QStyleFactory::create("Fusion")); QPalette darkPalette; - QColor gray(230, 230, 230); + QColor gray = QColor(230, 230, 230); darkPalette.setColor(QPalette::Window, QColor(53, 53, 53)); darkPalette.setColor(QPalette::WindowText, gray); darkPalette.setColor(QPalette::Disabled, QPalette::WindowText, QColor(127, 127, 127));