Skip to content

Commit

Permalink
Convert timer and start/stop button to automatic memory.
Browse files Browse the repository at this point in the history
  • Loading branch information
przemek83 committed Dec 26, 2024
1 parent 4661047 commit b07f62f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 49 deletions.
91 changes: 42 additions & 49 deletions examples/Examples.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
#include "Examples.h"

#include <QApplication>
#include <QGroupBox>
#include <QLabel>
#include <QPushButton>
#include <QStyleFactory>
#include <QTimer>
#include <QVBoxLayout>
#include <QWindow>

Examples::Examples()
: info_(QStringLiteral("Status")),
Expand All @@ -17,7 +10,9 @@ Examples::Examples()
filterDates_{QStringLiteral("Dates Filter"), startDate_, endDate_, true},
filterStrings_{QStringLiteral("Names Filter"), exampleStringsList_},
progressBarInfinite_{QStringLiteral("Title")},
progressBarCounter_{QStringLiteral("Title"), MAX_PROGRESS_BAR_VALUE}
progressBarCounter_{QStringLiteral("Title"), MAX_PROGRESS_BAR_VALUE},
startStopButtonInfinite_{QStringLiteral("start")},
startStopButtonCounter_{QStringLiteral("start")}
{
setWindowTitle("Wble library examples");
QHBoxLayout* widgetLayout{new QHBoxLayout(this)};
Expand Down Expand Up @@ -107,62 +102,60 @@ QGroupBox* Examples::wrapProgressBar(const QString& name,

QGroupBox* Examples::createProgressBarInfinite()
{
auto* startStopButton{new QPushButton(QStringLiteral("start"))};
QObject::connect(
startStopButton, &QPushButton::clicked, &progressBarInfinite_,
[&bar = progressBarInfinite_, startStop = startStopButton]()
&startStopButtonInfinite_, &QPushButton::clicked, &progressBarInfinite_,
[&bar = progressBarInfinite_, &startStop = startStopButtonInfinite_]()
{
const bool running{bar.isRunning()};
if (running)
bar.stop();
else
bar.start();
startStop->setText(
startStop.setText(
(running ? QStringLiteral("start") : QStringLiteral("stop")));
});
return wrapProgressBar(QStringLiteral("Infinite progress bar"),
&progressBarInfinite_, startStopButton);
&progressBarInfinite_, &startStopButtonInfinite_);
}

QGroupBox* Examples::createProgressBarCounter()
{
auto* startStopButton{new QPushButton(QStringLiteral("start"))};
auto* progressTimer{new QTimer(&progressBarCounter_)};
progressTimer->setInterval(TIME_INTERVAL);
QObject::connect(progressTimer, &QTimer::timeout, &progressBarCounter_,
[&progress = progress_, &bar = progressBarCounter_,
startStop = startStopButton, timer = progressTimer]()
{
bar.updateProgress(progress);
++progress;
if (progress > MAX_PROGRESS_BAR_VALUE)
{
timer->stop();
progress = 0;
startStop->click();
}
});
QObject::connect(startStopButton, &QPushButton::clicked,
&progressBarCounter_,
[timer = progressTimer, startStop = startStopButton,
&bar = progressBarCounter_]()
{
const bool running = bar.isRunning();
if (running)
{
bar.stop();
timer->stop();
}
else
{
bar.start();
timer->start();
}
startStop->setText((running ? QStringLiteral("start")
: QStringLiteral("stop")));
});
progressCounterTimer_.setInterval(TIME_INTERVAL);
QObject::connect(
&progressCounterTimer_, &QTimer::timeout, &progressBarCounter_,
[&progress = progress_, &bar = progressBarCounter_,
&startStop = startStopButtonCounter_, &timer = progressCounterTimer_]()
{
bar.updateProgress(progress);
++progress;
if (progress > MAX_PROGRESS_BAR_VALUE)
{
timer.stop();
progress = 0;
startStop.click();
}
});
QObject::connect(
&startStopButtonCounter_, &QPushButton::clicked, &progressBarCounter_,
[&timer = progressCounterTimer_, &startStop = startStopButtonCounter_,
&bar = progressBarCounter_]()
{
const bool running = bar.isRunning();
if (running)
{
bar.stop();
timer.stop();
}
else
{
bar.start();
timer.start();
}
startStop.setText(
(running ? QStringLiteral("start") : QStringLiteral("stop")));
});
return wrapProgressBar(QStringLiteral("Counter progress bar"),
&progressBarCounter_, startStopButton);
&progressBarCounter_, &startStopButtonCounter_);
}

QVBoxLayout* Examples::createRightWidgetColumn()
Expand Down
5 changes: 5 additions & 0 deletions examples/Examples.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once

#include <QLabel>
#include <QPushButton>
#include <QTimer>
#include <QWidget>

#include <wble/DoubleSlider.h>
Expand Down Expand Up @@ -61,6 +63,9 @@ class Examples : public QWidget
FilterStrings filterStrings_;
ProgressBarInfinite progressBarInfinite_;
ProgressBarCounter progressBarCounter_;
QPushButton startStopButtonInfinite_;
QPushButton startStopButtonCounter_;
QTimer progressCounterTimer_;

private slots:
void doubleSliderMinChanged(double min);
Expand Down

0 comments on commit b07f62f

Please sign in to comment.