Skip to content

Commit 0930660

Browse files
authored
Merge pull request #5 from MeshAndrey/Notification
Notification
2 parents a08c2b5 + 5a73b6b commit 0930660

File tree

4 files changed

+57
-15
lines changed

4 files changed

+57
-15
lines changed

src/MainWindow.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,16 @@ void MainWindow::showMessageBox(QString message)
9191
msg.exec();
9292
}
9393

94+
void MainWindow::showNotification(QString title, QString message)
95+
{
96+
trayIcon->showMessage(title, message, QSystemTrayIcon::Information, 5000);
97+
}
98+
99+
void MainWindow::showErrorNotification(QString title, QString message)
100+
{
101+
trayIcon->showMessage(title, message, QSystemTrayIcon::Critical, 5000);
102+
}
103+
94104
void MainWindow::showHideWindow()
95105
{
96106
setVisible(!isVisible());

src/MainWindow.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@ class MainWindow : public QMainWindow
1616
QSystemTrayIcon* trayIcon;
1717
QMenu* trayIconMenu;
1818

19-
void showEvent(QShowEvent* event);
20-
void hideEvent(QHideEvent* event);
21-
void closeEvent(QCloseEvent* event);
22-
2319
private slots:
2420
void addButtonClicked();
2521
void showHideWindow();
2622

23+
protected:
24+
void showEvent(QShowEvent* event);
25+
void hideEvent(QHideEvent* event);
26+
void closeEvent(QCloseEvent* event);
27+
2728
public:
2829
MainWindow(QWidget *parent = nullptr);
2930
~MainWindow();
@@ -32,5 +33,7 @@ private slots:
3233
QWidget* newWidget);
3334

3435
void showMessageBox(QString message);
36+
void showNotification(QString title, QString message);
37+
void showErrorNotification(QString title, QString message);
3538
};
3639
#endif // MAINWINDOW_H

src/widgets/TimerWidget.cpp

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "TimerWidget.h"
22
#include "AlarmWidget.h"
33
#include "InputWidget.h"
4-
#include "../MainWindow.h"
4+
55
#include "../TimeUtils.h"
66

77
TimerWidget::TimerWidget(QString name, int timerValue,
@@ -105,16 +105,37 @@ void TimerWidget::stopButtonClicked()
105105
updateTimer->stop();
106106
timer->stop();
107107

108-
auto mainWindow = static_cast<MainWindow*>(this->parent()->parent()->parent()->parent());
108+
auto mainWindow = getMainWindow();
109+
if (mainWindow == nullptr)
110+
{
111+
showMessageBox("MainWindow is nullptr");
112+
return;
113+
}
114+
109115
mainWindow->replaceWidget(this, new InputWidget(static_cast<QWidget*>(this->parent())));
110116
}
111117

112118
void TimerWidget::timerTimeout()
113119
{
114-
if (!shellCommand.isEmpty())
115-
executeProcess(shellCommand);
120+
auto mainWindow = getMainWindow();
121+
if (mainWindow == nullptr)
122+
{
123+
showMessageBox("MainWindow is nullptr");
124+
return;
125+
}
116126

117-
auto mainWindow = static_cast<MainWindow*>(this->parent()->parent()->parent()->parent());
127+
if (!shellCommand.isEmpty())
128+
{
129+
if (!executeProcess(shellCommand))
130+
{
131+
const QString message = QString("Process \"%1\" not started")
132+
.arg(shellCommand);
133+
showMessageBox(message);
134+
mainWindow->showErrorNotification(name, message);
135+
}
136+
else
137+
mainWindow->showNotification(name, QString("\"%1\" was executed").arg(shellCommand));
138+
}
118139

119140
if (autoStopAlarm)
120141
{
@@ -130,12 +151,12 @@ void TimerWidget::timerTimeout()
130151
}
131152
}
132153

133-
void TimerWidget::executeProcess(const QString program)
154+
bool TimerWidget::executeProcess(const QString program)
134155
{
135156
if (program == "")
136157
{
137158
showMessageBox("Empty program param");
138-
return;
159+
return false;
139160
}
140161

141162
QStringList splitedProgramCommand = program.split(" ");
@@ -153,11 +174,10 @@ void TimerWidget::executeProcess(const QString program)
153174
process->start(appName, appArgs);
154175

155176
if (process->waitForStarted(3000)) // if ok
156-
return;
177+
return true;
157178

158-
showMessageBox(QString("Process %1 %2 not started")
159-
.arg(program, splitedProgramCommand.join(" ")));
160179
delete process;
180+
return false;
161181
}
162182

163183
void TimerWidget::updateTimerTimeout()
@@ -176,3 +196,8 @@ void TimerWidget::showMessageBox(const QString message)
176196
msg.setText(message);
177197
msg.exec();
178198
}
199+
200+
MainWindow* TimerWidget::getMainWindow()
201+
{
202+
return static_cast<MainWindow*>(this->parent()->parent()->parent()->parent());
203+
}

src/widgets/TimerWidget.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <QWidget>
66
#include <QtWidgets>
77

8+
#include "../MainWindow.h"
9+
810
class TimerWidget : public QWidget
911
{
1012
Q_OBJECT
@@ -24,7 +26,9 @@ class TimerWidget : public QWidget
2426
void initLayout();
2527
void initConnections();
2628
void showMessageBox(const QString message);
27-
void executeProcess(const QString program);
29+
bool executeProcess(const QString program);
30+
31+
MainWindow* getMainWindow();
2832

2933
private slots:
3034
void pauseResumeButtonCLicked();

0 commit comments

Comments
 (0)