Permalink
Browse files

Simplify code in the PowerManager

  • Loading branch information...
1 parent 6f3b47c commit 641803d521cde86eb8e4119af5c52272ed255efa @SokoloffA SokoloffA committed Oct 24, 2012
@@ -40,21 +40,30 @@ class MessageBox: public QMessageBox
public:
explicit MessageBox(QWidget *parent = 0): QMessageBox(parent) {}
- static QMessageBox::StandardButton question(QWidget* parent,
- const QString& title,
- const QString& text,
- StandardButton button0,
- StandardButton button1)
+ static QWidget *parentWidget()
{
- MessageBox msgBox(parent);
+ QWidgetList widgets = QApplication::topLevelWidgets();
+
+ if (widgets.count())
+ return widgets.at(0);
+ else
+ return 0;
+ }
+
+ static bool question(const QString& title, const QString& text)
+ {
+ MessageBox msgBox(parentWidget());
msgBox.setWindowTitle(title);
msgBox.setText(text);
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
- if (msgBox.exec() == QMessageBox::Yes)
- return QMessageBox::Yes;
- else
- return QMessageBox::No;
+ return (msgBox.exec() == QMessageBox::Yes);
+ }
+
+
+ static void warning(const QString& title, const QString& text)
+ {
+ QMessageBox::warning(parentWidget(), tr("Razor Power Manager Error"), tr("Hibernate failed."));
}
@@ -69,8 +78,7 @@ class MessageBox: public QMessageBox
};
PowerManager::PowerManager(QObject * parent)
- : QObject(parent),
- m_parentWidget(0)
+ : QObject(parent)
{
libTranslate("librazorqt");
m_power = new RazorPower(this);
@@ -130,74 +138,57 @@ QList<QAction*> PowerManager::availableActions()
}
-
-
void PowerManager::suspend()
{
- if (MessageBox::question(m_parentWidget, tr("Razor Session Suspend"),
- tr("Do you want to really suspend your computer?<p>Suspends the computer into a low power state. System state is not preserved if the power is lost."),
- QMessageBox::Yes, QMessageBox::No) == QMessageBox::No)
+ if (MessageBox::question(tr("Razor Session Suspend"),
+ tr("Do you want to really suspend your computer?<p>Suspends the computer into a low power state. System state is not preserved if the power is lost.")))
{
- return;
+ m_power->suspend();
}
-
- m_power->suspend();
}
void PowerManager::hibernate()
{
- if (MessageBox::question(m_parentWidget, tr("Razor Session Hibernate"),
- tr("Do you want to really hibernate your computer?<p>Hibernates the computer into a low power state. System state is preserved if the power is lost."),
- QMessageBox::Yes, QMessageBox::No) == QMessageBox::No)
+ if (MessageBox::question(tr("Razor Session Hibernate"),
+ tr("Do you want to really hibernate your computer?<p>Hibernates the computer into a low power state. System state is preserved if the power is lost.")))
{
- return;
+ m_power->hibernate();
}
-
- m_power->hibernate();
}
void PowerManager::reboot()
{
- if (MessageBox::question(m_parentWidget, tr("Razor Session Reboot"),
- tr("Do you want to really restart your computer? All unsaved work will be lost..."),
- QMessageBox::Yes, QMessageBox::No) == QMessageBox::No)
+ if (MessageBox::question(tr("Razor Session Reboot"),
+ tr("Do you want to really restart your computer? All unsaved work will be lost...")))
{
- return;
+ m_power->reboot();
}
-
- m_power->reboot();
}
void PowerManager::shutdown()
{
- if (MessageBox::question(m_parentWidget, tr("Razor Session Shutdown"),
- tr("Do you want to really switch off your computer? All unsaved work will be lost..."),
- QMessageBox::Yes, QMessageBox::No) == QMessageBox::No)
+ if (MessageBox::question(tr("Razor Session Shutdown"),
+ tr("Do you want to really switch off your computer? All unsaved work will be lost...")))
{
- return;
+ m_power->shutdown();
}
-
- m_power->shutdown();
}
void PowerManager::logout()
{
- if (MessageBox::question(m_parentWidget, tr("Razor Session Logout"),
- tr("Do you want to really logout? All unsaved work will be lost..."),
- QMessageBox::Yes, QMessageBox::No) == QMessageBox::No)
+ if (MessageBox::question(tr("Razor Session Logout"),
+ tr("Do you want to really logout? All unsaved work will be lost...")))
{
- return;
- }
-
- m_power->logout();
+ m_power->logout();
+ }
}
void PowerManager::hibernateFailed()
{
- QMessageBox::warning(m_parentWidget, tr("Razor Power Manager Error"), tr("Hibernate failed."));
+ MessageBox::warning(tr("Razor Power Manager Error"), tr("Hibernate failed."));
}
void PowerManager::suspendFailed()
{
- QMessageBox::warning(m_parentWidget, tr("Razor Power Manager Error"), tr("Suspend failed."));
+ MessageBox::warning(tr("Razor Power Manager Error"), tr("Suspend failed."));
}
@@ -43,8 +43,7 @@ class PowerManager : public QObject
PowerManager(QObject * parent);
~PowerManager();
QList<QAction*> availableActions();
- QWidget* parentWidget() const { return m_parentWidget; }
- void setParentWidget(QWidget* parentWidget) { m_parentWidget = parentWidget; }
+
public slots:
// power management
void suspend();
@@ -56,7 +55,6 @@ public slots:
private:
RazorPower * m_power;
- QWidget * m_parentWidget;
private slots:
void hibernateFailed();
@@ -68,8 +68,6 @@ RazorMainMenu::RazorMainMenu(const RazorPanelPluginStartInfo* startInfo, QWidget
connect(panel(), SIGNAL(panelRealigned()), this, SLOT(realign()));
mPowerManager = new PowerManager(this);
- mPowerManager->setParentWidget(panel());
-
mScreenSaver = new ScreenSaver(this);
mShortcut = new QxtGlobalShortcut(this);

0 comments on commit 641803d

Please sign in to comment.