Skip to content

Commit a08c2b5

Browse files
authored
Merge pull request #4 from MeshAndrey/ShellExecOnAlarm
Shell exec on alarm
2 parents 0701540 + 0fad96b commit a08c2b5

File tree

6 files changed

+99
-9
lines changed

6 files changed

+99
-9
lines changed

src/MainWindow.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ void MainWindow::showHideWindow()
9595
{
9696
setVisible(!isVisible());
9797
}
98+
9899
void MainWindow::showEvent(QShowEvent* event)
99100
{
100101
QMainWindow::showEvent(event);

src/widgets/AlarmWidget.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ void AlarmWidget::repeatButtonClicked()
7777
mainWindow->replaceWidget(this,
7878
new TimerWidget(this->name,
7979
this->timerValue,
80+
"",
81+
false,
8082
static_cast<QWidget*>(this->parent())));
8183
}
8284

src/widgets/InputWidget.cpp

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@ void InputWidget::initWidgets()
1515
hoursEdit = new QLineEdit;
1616
minsEdit = new QLineEdit;
1717
secsEdit = new QLineEdit;
18+
shellCommandEdit = new QLineEdit;
1819

1920
clearButton = new QPushButton("Clear");
2021
okButton = new QPushButton("Ok");
2122
deleteButton = new QPushButton("Delete");
23+
24+
execShellCommandCheckBox = new QCheckBox("Exec shell command on alarm");
25+
autoStopAlarmCheckBox = new QCheckBox("Auto stop alarm");
2226
}
2327

2428
void InputWidget::initLayout()
@@ -65,11 +69,17 @@ void InputWidget::initLayout()
6569
centralLayout->addLayout(nameLayout);
6670
centralLayout->addLayout(timeInputsLayout);
6771
centralLayout->addLayout(gridLayout);
72+
centralLayout->addWidget(execShellCommandCheckBox);
73+
centralLayout->addWidget(shellCommandEdit);
74+
centralLayout->addWidget(autoStopAlarmCheckBox);
6875
centralLayout->addLayout(bottomHorLayout);
6976
centralLayout->addWidget(deleteButton);
7077

78+
shellCommandEdit->setVisible(false);
79+
autoStopAlarmCheckBox->setVisible(false);
80+
7181
setMinimumSize(400, 250);
72-
setMaximumSize(400, 300);
82+
setMaximumSize(400, 400);
7383
setLayout(centralLayout);
7484
}
7585

@@ -88,6 +98,9 @@ void InputWidget::initConnections()
8898
this, &InputWidget::okButtonClicked);
8999
connect(deleteButton, &QPushButton::clicked,
90100
this, &InputWidget::deleteButtonClicked);
101+
102+
connect(execShellCommandCheckBox, &QCheckBox::stateChanged,
103+
this, &InputWidget::execShellCheckBoxStateChanged);
91104
}
92105

93106
void InputWidget::clearButtonClicked()
@@ -127,6 +140,8 @@ void InputWidget::okButtonClicked()
127140
mainWindow->replaceWidget(this,
128141
new TimerWidget(nameEdit->text(),
129142
timerValue,
143+
shellCommandEdit->text(),
144+
autoStopAlarmCheckBox->isChecked(),
130145
static_cast<QWidget*>(this->parent())));
131146
}
132147

@@ -209,3 +224,20 @@ void InputWidget::textEdited(const QString &text)
209224
}
210225
}
211226

227+
void InputWidget::execShellCheckBoxStateChanged(int state)
228+
{
229+
bool visibility = false;
230+
231+
if (state == Qt::Checked)
232+
{
233+
visibility = true;
234+
}
235+
else if (state == Qt::Unchecked)
236+
{
237+
visibility = false;
238+
}
239+
240+
shellCommandEdit->setVisible(visibility);
241+
autoStopAlarmCheckBox->setVisible(visibility);
242+
}
243+

src/widgets/InputWidget.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ class InputWidget : public QWidget
1212
QLineEdit *nameEdit = nullptr,
1313
*hoursEdit = nullptr,
1414
*minsEdit = nullptr,
15-
*secsEdit = nullptr;
15+
*secsEdit = nullptr,
16+
*shellCommandEdit = nullptr;
1617
QPushButton *clearButton = nullptr,
1718
*okButton = nullptr,
1819
*deleteButton = nullptr;
20+
QCheckBox *execShellCommandCheckBox = nullptr,
21+
*autoStopAlarmCheckBox = nullptr;
1922

2023
void initWidgets();
2124
void initLayout();
@@ -33,6 +36,7 @@ private slots:
3336
void okButtonClicked();
3437
void deleteButtonClicked();
3538
void textEdited(const QString &text);
39+
void execShellCheckBoxStateChanged(int state);
3640
};
3741

3842
#endif // INPUTWIDGET_H

src/widgets/TimerWidget.cpp

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44
#include "../MainWindow.h"
55
#include "../TimeUtils.h"
66

7-
TimerWidget::TimerWidget(QString name, int timerValue, QWidget *parent) : QWidget(parent)
7+
TimerWidget::TimerWidget(QString name, int timerValue,
8+
QString shellCommand, bool autoStopAlarm,
9+
QWidget *parent) : QWidget(parent)
810
{
911
this->name = name;
1012
this->timerValue = timerValue;
13+
this->shellCommand = shellCommand;
14+
this->autoStopAlarm = autoStopAlarm;
1115

1216
initWidgets();
1317
initConnections();
@@ -107,11 +111,53 @@ void TimerWidget::stopButtonClicked()
107111

108112
void TimerWidget::timerTimeout()
109113
{
114+
if (!shellCommand.isEmpty())
115+
executeProcess(shellCommand);
116+
110117
auto mainWindow = static_cast<MainWindow*>(this->parent()->parent()->parent()->parent());
111-
mainWindow->replaceWidget(this,
112-
new AlarmWidget(this->name,
113-
this->timerValue,
114-
static_cast<QWidget*>(this->parent())));
118+
119+
if (autoStopAlarm)
120+
{
121+
mainWindow->replaceWidget(this,
122+
new InputWidget(static_cast<QWidget*>(this->parent())));
123+
}
124+
else
125+
{
126+
mainWindow->replaceWidget(this,
127+
new AlarmWidget(this->name,
128+
this->timerValue,
129+
static_cast<QWidget*>(this->parent())));
130+
}
131+
}
132+
133+
void TimerWidget::executeProcess(const QString program)
134+
{
135+
if (program == "")
136+
{
137+
showMessageBox("Empty program param");
138+
return;
139+
}
140+
141+
QStringList splitedProgramCommand = program.split(" ");
142+
QString appName = splitedProgramCommand[0];
143+
splitedProgramCommand.removeAt(0);
144+
QStringList appArgs = splitedProgramCommand;
145+
146+
QProcess* process = new QProcess;
147+
148+
if (appArgs.length() == 0)
149+
process->start(appName, QStringList());
150+
else if (appArgs.length() == 1 && appArgs[0].isEmpty())
151+
process->start(appName, QStringList());
152+
else
153+
process->start(appName, appArgs);
154+
155+
if (process->waitForStarted(3000)) // if ok
156+
return;
157+
158+
showMessageBox(QString("Process %1 %2 not started")
159+
.arg(program, splitedProgramCommand.join(" ")));
160+
delete process;
115161
}
116162

117163
void TimerWidget::updateTimerTimeout()

src/widgets/TimerWidget.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ class TimerWidget : public QWidget
1010
Q_OBJECT
1111
private:
1212
int timerValue = 0;
13-
QString name;
13+
bool autoStopAlarm = false;
14+
QString name,
15+
shellCommand;
1416
QLabel *remainingTimeLabel = nullptr;
1517
QPushButton *pauseResumeButton = nullptr,
1618
*stopButton = nullptr;
@@ -22,6 +24,7 @@ class TimerWidget : public QWidget
2224
void initLayout();
2325
void initConnections();
2426
void showMessageBox(const QString message);
27+
void executeProcess(const QString program);
2528

2629
private slots:
2730
void pauseResumeButtonCLicked();
@@ -30,7 +33,9 @@ private slots:
3033
void updateTimerTimeout();
3134

3235
public:
33-
explicit TimerWidget(QString name, int timerValue, QWidget *parent = nullptr);
36+
explicit TimerWidget(QString name, int timerValue,
37+
QString shellCommand, bool autoStopAlarm,
38+
QWidget *parent = nullptr);
3439
};
3540

3641
#endif // TIMERWIDGET_H

0 commit comments

Comments
 (0)