1 /*-
2 * Copyright (c) 2016 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 "icons.h"
31 #include "delay.h"
32 #include "qt-helper/qt-helper.h"
33
34 #define PB_STYLE "padding: 5px 10px 5px 10px; text-align: left;"
35
Delay(int seconds,const QString & actionName,const QString & text,const QIcon & icon,QWidget * parent)36 Delay::Delay(int seconds, const QString &actionName, const QString &text,
37 const QIcon &icon, QWidget *parent) : QDialog(parent)
38 {
39 secondsLeft = seconds; this->text = text;
40
41 label = new QLabel("");
42 timer = new QTimer(this);
43 QIcon cnclIcon = qh_loadStockIcon(QStyle::SP_DialogCancelButton, 0);
44
45 QHBoxLayout *hbox = new QHBoxLayout;
46 QHBoxLayout *btHbox = new QHBoxLayout;
47 QVBoxLayout *layout = new QVBoxLayout(this);
48 QPushButton *cancel = new QPushButton(cnclIcon, tr("Cancel"));
49 QPushButton *action = new QPushButton(icon, actionName);
50
51 action->setStyleSheet(PB_STYLE);
52 cancel->setStyleSheet(PB_STYLE);
53
54 setLabelText(text, seconds);
55 setWindowIcon(icon);
56
57 hbox->setSpacing(20);
58 hbox->addWidget(label, 1, Qt::AlignHCenter);
59
60 btHbox->setSpacing(2);
61 btHbox->addWidget(action, 1, Qt::AlignRight);
62 btHbox->addWidget(cancel, 0, Qt::AlignRight);
63
64 layout->setSpacing(10);
65 layout->addLayout(hbox);
66 layout->addLayout(btHbox);
67 layout->setContentsMargins(15, 15, 15, 15);
68
69 connect(timer, SIGNAL(timeout()), this, SLOT(update()));
70 connect(action, SIGNAL(clicked()), this, SLOT(accept()));
71 connect(cancel, SIGNAL(clicked()), this, SLOT(reject()));
72
73 setModal(true);
74 setWindowFlags(windowFlags() | Qt::Dialog | Qt::FramelessWindowHint |
75 Qt::WindowStaysOnTopHint | Qt::BypassWindowManagerHint);
76 show();
77 setGeometry(QStyle::alignedRect(Qt::LeftToRight, Qt::AlignCenter,
78 parent->size(), qApp->primaryScreen()->geometry()));
79 timer->start(1000);
80 }
81
setLabelText(const QString & text,int seconds)82 void Delay::setLabelText(const QString &text, int seconds)
83 {
84 QString s;
85
86 if (seconds > 0) {
87 s = text + " %1 %2";
88 s = s.arg(seconds);
89 s = s.arg(seconds == 1 ? tr("second") : tr("seconds"));
90 label->setText(s);
91 }
92 }
93
keyPressEvent(QKeyEvent * e)94 void Delay::keyPressEvent(QKeyEvent *e) {
95 if (e->key() == Qt::Key_Escape)
96 reject();
97 QDialog::keyPressEvent(e);
98 }
99
closeEvent(QCloseEvent * event)100 void Delay::closeEvent(QCloseEvent *event)
101 {
102 hide();
103 event->ignore();
104 }
105
update()106 void Delay::update()
107 {
108 if (secondsLeft-- > 0)
109 setLabelText(text, secondsLeft);
110 else if (secondsLeft <= 0)
111 accept();
112 }
113
114