1 /* pinentryconfirm.cpp - A QMessageBox with a timeout
2  *
3  * Copyright (C) 2011 Ben Kibbey <bjk@luxsci.net>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, see <https://www.gnu.org/licenses/>.
17  * SPDX-License-Identifier: GPL-2.0+
18  */
19 
20 #include "pinentryconfirm.h"
21 #include "pinentrydialog.h"
22 #include <QAbstractButton>
23 #include <QGridLayout>
24 #include <QSpacerItem>
25 #include <QFontMetrics>
26 
27 PinentryConfirm::PinentryConfirm(Icon icon, int timeout, const QString &title,
28                                  const QString &desc, StandardButtons buttons, QWidget *parent) :
29     QMessageBox(icon, title, desc, buttons, parent)
30 {
31     _timed_out = false;
32     if (timeout > 0) {
33         _timer = new QTimer(this);
34         connect(_timer, SIGNAL(timeout()), this, SLOT(slotTimeout()));
35         _timer->start(timeout * 1000);
36     }
37 #ifndef QT_NO_ACCESSIBILITY
38     setAccessibleDescription(desc);
39     setAccessibleName(title);
40 #endif
41     raiseWindow(this);
42 }
43 
44 bool PinentryConfirm::timedOut() const
45 {
46     return _timed_out;
47 }
48 
49 void PinentryConfirm::showEvent(QShowEvent *event)
50 {
51     static bool resized;
52     if (!resized) {
53         QGridLayout* lay = dynamic_cast<QGridLayout*> (layout());
54         if (lay) {
55             QSize textSize = fontMetrics().size(Qt::TextExpandTabs, text(), fontMetrics().maxWidth());
56             QSpacerItem* horizontalSpacer = new QSpacerItem(textSize.width() + iconPixmap().width(),
57                                                             0, QSizePolicy::Minimum, QSizePolicy::Expanding);
58             lay->addItem(horizontalSpacer, lay->rowCount(), 1, 1, lay->columnCount() - 1);
59         }
60         resized = true;
61     }
62 
63     QDialog::showEvent(event);
64     raiseWindow(this);
65 }
66 
67 void PinentryConfirm::slotTimeout()
68 {
69     QAbstractButton *b = button(QMessageBox::Cancel);
70     _timed_out = true;
71 
72     if (b) {
73         b->animateClick(0);
74     }
75 }
76 
77 #include "pinentryconfirm.moc"
78