Skip to content

Commit

Permalink
Progress bar (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonMrt authored Nov 21, 2024
1 parent 69d4701 commit 2a27268
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 27 deletions.
2 changes: 2 additions & 0 deletions gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
69 changes: 69 additions & 0 deletions gui/cool_progressbar.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "cool_progressbar.h"
#include <QDebug>
#include <QPainter>
#include <QStyleOption>


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();
}

29 changes: 29 additions & 0 deletions gui/cool_progressbar.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef COOLPROGRESSBAR_H
#define COOLPROGRESSBAR_H

#include <QObject>
#include <QWidget>
#include <QPropertyAnimation>

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
84 changes: 69 additions & 15 deletions gui/davis_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
#include <QJsonArray>
#include "QDateTime"
#include <QProcess>
#include <QProgressBar>
#include <QTimer>
#include <QtConcurrent/QtConcurrent>
#include "json_utils.h"


Expand All @@ -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;
Expand Down Expand Up @@ -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 {"
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -166,6 +174,7 @@ void DavisGUI::hideElementsDuringResize() {
ui->label_text->setVisible(false);
qpbBuffer->setVisible(false);
qpbOpen->setVisible(false);
barCool->setVisible(false);
update();
}

Expand Down Expand Up @@ -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();
});

Expand Down Expand Up @@ -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;
Expand All @@ -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<void> future = QtConcurrent::run(lambdaFunction);
QFutureWatcher<void>* watcher = new QFutureWatcher<void>(this);
connect(watcher, &QFutureWatcher<void>::finished, this, &DavisGUI::hideProgressBar);
watcher->setFuture(future);
}



void DavisGUI::readPlotText(QStringList& str_lines) {
std::vector<double>lines;
std::vector<std::vector<double>> data;
Expand Down Expand Up @@ -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<void> future = QtConcurrent::run(this, &DavisGUI::visualizeFiles, fileNames);
QFutureWatcher<void>* watcher = new QFutureWatcher<void>(this);
connect(watcher, &QFutureWatcher<void>::finished, this, &DavisGUI::hideProgressBar);
watcher->setFuture(future);
}

bool DavisGUI::isFileContainsSingleChart(const QString& pathToFile,
Expand Down Expand Up @@ -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 {
Expand All @@ -491,12 +537,17 @@ void DavisGUI::dragEnterEvent(QDragEnterEvent* event) {


void DavisGUI::dropEvent(QDropEvent* event) {
emit showProgressBar();
QList<QUrl> 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<void> future = QtConcurrent::run(this, &DavisGUI::visualizeFiles, file_list);
QFutureWatcher<void>* watcher = new QFutureWatcher<void>(this);
connect(watcher, &QFutureWatcher<void>::finished, this, &DavisGUI::hideProgressBar);
watcher->setFuture(future);
}

void DavisGUI::visualizeFiles(const QStringList& file_list) {
Expand Down Expand Up @@ -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();
Expand Down
24 changes: 14 additions & 10 deletions gui/davis_gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -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);
Expand Down Expand Up @@ -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
5 changes: 4 additions & 1 deletion gui/davis_gui.ui
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,11 @@
<pointsize>24</pointsize>
</font>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(255, 255, 0) ;</string>
</property>
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p align=&quot;center&quot;&gt;&lt;span style=&quot; font-size:20pt; color:#969696;&quot;&gt;drag &amp;amp; drop&lt;br/&gt;text file(s) containing&lt;br/&gt;numbers for visualize&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p align=&quot;center&quot;&gt;&lt;span style=&quot; font-size:16pt; color:#969696;&quot;&gt;drag &amp;amp; drop&lt;br/&gt;text file(s) containing&lt;br/&gt;numbers for visualize&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
<widget class="QLabel" name="label_doc">
Expand Down
2 changes: 1 addition & 1 deletion gui/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit 2a27268

Please sign in to comment.