From 10e5e05c5911372fa38bcffed93b7c76ba05f3db Mon Sep 17 00:00:00 2001 From: =?utf8?q?Oliver=20Gro=C3=9F?= Date: Tue, 2 Sep 2008 20:19:35 +0200 Subject: [PATCH] added QTimerMessageBox --- qbat.pro | 6 ++-- qtimermessagebox.cpp | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++ qtimermessagebox.h | 46 +++++++++++++++++++++++++++ 3 files changed, 138 insertions(+), 2 deletions(-) create mode 100644 qtimermessagebox.cpp create mode 100644 qtimermessagebox.h diff --git a/qbat.pro b/qbat.pro index 3ecf0fe..d132aff 100644 --- a/qbat.pro +++ b/qbat.pro @@ -18,7 +18,8 @@ SOURCES += main.cpp \ batteryicon.cpp \ powermanager.cpp \ common.cpp \ - settings.cpp + settings.cpp \ + qtimermessagebox.cpp DESTDIR = . target.path = /usr/bin @@ -35,7 +36,8 @@ HEADERS += constants.h \ batteryicon.h \ common.h \ powermanager.h \ - settings.h + settings.h \ + qtimermessagebox.h TARGET = qbat FORMS += settingsdialog.ui diff --git a/qtimermessagebox.cpp b/qtimermessagebox.cpp new file mode 100644 index 0000000..bb8c2db --- /dev/null +++ b/qtimermessagebox.cpp @@ -0,0 +1,88 @@ +// +// C++ Implementation: qtimermessagebox +// +// Description: +// +// +// Author: Oliver Groß , (C) 2008 +// +// Copyright: See COPYING file that comes with this distribution +// +// +#include "qtimermessagebox.h" +#include +#include + +QTimerMessageBox::QTimerMessageBox(QWidget * parent) : + QMessageBox(parent), m_TimerID(-1), m_Timeout(0) +{ +} + +QTimerMessageBox::QTimerMessageBox(Icon icon, const QString & title, const QString & text, StandardButtons buttons, QWidget * parent, Qt::WindowFlags f) : + QMessageBox(icon, title, text, buttons, parent, f), m_TimerID(-1), m_Timeout(0) +{ +} + +QTimerMessageBox::~QTimerMessageBox() { + killTimer(m_TimerID); +} + +void QTimerMessageBox::timerEvent(QTimerEvent * event) { + if (event->timerId() == m_TimerID) { + if (m_Timeout > 0) { + markDefaultButton(); + m_Timeout--; + } + else { + killTimer(m_TimerID); + emit aboutToTriggerTimeout(); + defaultButton()->click(); + } + } +} + +inline void QTimerMessageBox::markDefaultButton() { + defaultButton()->setText(m_DefaultButtonText + " (" + QString::number(m_Timeout) + ')'); +} + +inline void QTimerMessageBox::setTimeout(int timeout) { + m_Timeout = timeout; +} + +int QTimerMessageBox::exec(int timeout) { + if (defaultButton()) { + setTimeout(timeout); + m_DefaultButtonText = defaultButton()->text(); + + m_TimerID = startTimer(1000); + + int result = QMessageBox::exec(); + + killTimer(m_TimerID); + return result; + } + else + return QMessageBox::exec(); +} + +inline QMessageBox::StandardButton QTimerMessageBox::messagebox(QWidget * parent, Icon icon, const QString & title, const QString & text, int timeout, StandardButtons buttons, StandardButton defaultButton) { + QTimerMessageBox messageBox(icon, title, text, buttons, parent); + messageBox.setDefaultButton(defaultButton); + return (QMessageBox::StandardButton)messageBox.exec(timeout); +} + +QMessageBox::StandardButton QTimerMessageBox::critical(QWidget * parent, const QString & title, const QString & text, int timeout, StandardButtons buttons, StandardButton defaultButton) { + return messagebox(parent, QMessageBox::Critical, title, text, timeout, buttons, defaultButton); +} + +QMessageBox::StandardButton QTimerMessageBox::information(QWidget * parent, const QString & title, const QString & text, int timeout, StandardButtons buttons, StandardButton defaultButton) { + return messagebox(parent, QMessageBox::Information, title, text, timeout, buttons, defaultButton); +} + +QMessageBox::StandardButton QTimerMessageBox::question(QWidget * parent, const QString & title, const QString & text, int timeout, StandardButtons buttons, StandardButton defaultButton) { + return messagebox(parent, QMessageBox::Question, title, text, timeout, buttons, defaultButton); +} + +QMessageBox::StandardButton QTimerMessageBox::warning(QWidget * parent, const QString & title, const QString & text, int timeout, StandardButtons buttons, StandardButton defaultButton) { + return messagebox(parent, QMessageBox::Warning, title, text, timeout, buttons, defaultButton); +} diff --git a/qtimermessagebox.h b/qtimermessagebox.h new file mode 100644 index 0000000..e5aacc8 --- /dev/null +++ b/qtimermessagebox.h @@ -0,0 +1,46 @@ +// +// C++ Interface: qtimermessagebox +// +// Author: Oliver Groß , (C) 2008 +// +// Copyright: See COPYING file that comes with this distribution +// +#ifndef QTIMERMESSAGEBOX_H +#define QTIMERMESSAGEBOX_H + +#include + +/** + @author Oliver Groß +*/ +class QTimerMessageBox : public QMessageBox { + Q_OBJECT +private: + int m_TimerID; + int m_Timeout; + + QString m_DefaultButtonText; + + void timerEvent(QTimerEvent * event); + inline void markDefaultButton(); + + inline static QTimerMessageBox::StandardButton messagebox(QWidget * parent, Icon icon, const QString & title, const QString & text, int timeout, StandardButtons buttons, StandardButton defaultButton); +public: + QTimerMessageBox(QWidget * parent = 0); + QTimerMessageBox(Icon icon, const QString & title, const QString & text, StandardButtons buttons = NoButton, QWidget * parent = 0, Qt::WindowFlags f = Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint); + ~QTimerMessageBox(); + + int timeout() const { return m_Timeout; } + inline void setTimeout(int timeout); + + static QMessageBox::StandardButton critical(QWidget * parent, const QString & title, const QString & text, int timeout = 10, StandardButtons buttons = Ok, StandardButton defaultButton = Ok); + static QMessageBox::StandardButton information(QWidget * parent, const QString & title, const QString & text, int timeout = 10, StandardButtons buttons = Ok, StandardButton defaultButton = Ok); + static QMessageBox::StandardButton question(QWidget * parent, const QString & title, const QString & text, int timeout = 10, StandardButtons buttons = Ok, StandardButton defaultButton = Ok); + static QMessageBox::StandardButton warning(QWidget * parent, const QString & title, const QString & text, int timeout = 10, StandardButtons buttons = Ok, StandardButton defaultButton = Ok); +public slots: + int exec(int timeout); +signals: + void aboutToTriggerTimeout(); +}; + +#endif -- 2.11.4.GIT