1 /*-
2 * Copyright (c) 2017 Marcel Kaiser. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25 #include <QTimer>
26 #include <QStyle>
27 #include <QScreen>
28 #include <QDesktopWidget>
29
30 #include "countdown.h"
31 #include "qt-helper/qt-helper.h"
32
Countdown(bool suspend,int seconds,QWidget * parent)33 Countdown::Countdown(bool suspend, int seconds, QWidget *parent)
34 : QDialog(parent)
35 {
36 this->suspend = suspend;
37 shutdownTime = time(NULL) + seconds;
38
39 QIcon cnclIcon = qh_loadStockIcon(QStyle::SP_DialogCancelButton, 0);
40 QIcon pic = qh_loadStockIcon(QStyle::SP_MessageBoxCritical, 0);
41 QIcon tIcon = pic;
42
43 label = new QLabel("");
44 timer = new QTimer(this);
45 QLabel *icon = new QLabel;
46 QHBoxLayout *hbox = new QHBoxLayout;
47 QVBoxLayout *layout = new QVBoxLayout(this);
48 QPushButton *cancel = new QPushButton(cnclIcon, tr("&Cancel"));
49
50 setLabelText(seconds);
51 icon->setPixmap(pic.pixmap(64));
52
53 setWindowIcon(tIcon);
54 setWindowTitle(tr("Critical battery capacity reached"));
55
56 hbox->setSpacing(20);
57 hbox->addWidget(icon);
58 hbox->addWidget(label, 1, Qt::AlignHCenter);
59
60 layout->addLayout(hbox);
61 layout->addWidget(cancel, 0, Qt::AlignRight);
62
63 connect(timer, SIGNAL(timeout()), this, SLOT(update()));
64 connect(cancel, SIGNAL(clicked()), this, SLOT(reject()));
65 show();
66 setGeometry(QStyle::alignedRect(Qt::LeftToRight, Qt::AlignCenter,
67 size(), qApp->primaryScreen()->geometry()));
68 timer->start(1000);
69 }
70
setLabelText(int seconds)71 void Countdown::setLabelText(int seconds)
72 {
73 QString s;
74
75 if (seconds <= 0)
76 return;
77 if (suspend)
78 s = tr("Suspending system in %1 %2");
79 else
80 s = tr("Shutting down system in %1 %2");
81 s = s.arg(seconds);
82 s = s.arg(seconds == 1 ? tr("Second") : tr("Seconds"));
83 label->setText(s);
84 }
85
keyPressEvent(QKeyEvent * e)86 void Countdown::keyPressEvent(QKeyEvent *e) {
87 if (e->key() == Qt::Key_Escape)
88 return;
89 QDialog::keyPressEvent(e);
90 }
91
closeEvent(QCloseEvent * event)92 void Countdown::closeEvent(QCloseEvent *event)
93 {
94 event->ignore();
95 }
96
update()97 void Countdown::update()
98 {
99 int secondsLeft = (int)(shutdownTime - time(NULL));
100
101 if (secondsLeft > 0) {
102 timer->start(1000);
103 setLabelText(secondsLeft);
104 } else if (secondsLeft <= 0)
105 accept();
106 }
107
108