KShutdown – graphical shutdown utility
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
4.7KB

  1. /*
  2. mmessagedialog.cpp - A warning message dialog
  3. Copyright (C) 2004 Konrad Twardowski <kdtonline@poczta.onet.pl>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. */
  16. #include "miscutils.h"
  17. #include "mmainwindow.h"
  18. #include "mmessagedialog.h"
  19. #include <tqdatetime.h>
  20. #include <tqhbox.h>
  21. #include <tqlabel.h>
  22. #include <tqlayout.h>
  23. #include <tqlcdnumber.h>
  24. #include <tqtimer.h>
  25. #include <kdebug.h>
  26. #include <kiconloader.h>
  27. #include <tdelocale.h>
  28. #include <tdepopupmenu.h>
  29. #include <kpushbutton.h>
  30. #include <twin.h>
  31. // public
  32. MMessageDialog *MMessageDialog::_instance = 0;
  33. MMessageDialog::MMessageDialog(const int delay, const Action::Type actionToExecute, const TQString &text)
  34. : KDialog(ks_main, "MMessageDialog", true),
  35. _dialogDelay(delay),
  36. _lastTime(-1),
  37. _action(actionToExecute)
  38. {
  39. setCaption(i18n("Message"));
  40. setPaletteBackgroundColor(yellow);
  41. setPaletteForegroundColor(black);
  42. // main layout
  43. TQVBoxLayout *l = new TQVBoxLayout(this, 5);
  44. // message text
  45. TQLabel *t_message = new TQLabel(this);
  46. TQFont f = TQFont(t_message->font());
  47. f.setPointSize(16);
  48. t_message->setFont(f);
  49. t_message->setPaletteBackgroundColor(yellow);
  50. t_message->setPaletteForegroundColor(black);
  51. t_message->setAlignment(AlignCenter);
  52. t_message->setFrameShape(TQFrame::StyledPanel);
  53. t_message->setFrameShadow(TQFrame::Plain);
  54. t_message->setLineWidth(4);
  55. t_message->setMargin(20);
  56. t_message->setSizePolicy(TQSizePolicy(TQSizePolicy::Preferred, TQSizePolicy::Expanding));
  57. t_message->setText(text);
  58. // bottom layout
  59. TQHBox *bottomBox = new TQHBox(this);
  60. bottomBox->setSpacing(5);
  61. // seconds
  62. l_seconds = new TQLCDNumber(bottomBox);
  63. l_seconds->display(_dialogDelay);
  64. l_seconds->setFrameShape(TQFrame::NoFrame);
  65. l_seconds->setSegmentStyle(TQLCDNumber::Flat);
  66. MiscUtils::setHint(l_seconds, i18n("Remaining time."));
  67. // continue button
  68. b_continue = new KPushButton(
  69. ks_actions->getIcon(_action), ks_actions->getName(_action),
  70. bottomBox, "KPushButton::b_continue"
  71. );
  72. // FIXME: 2.0: no accelerator for "b_continue"
  73. b_continue->setPaletteBackgroundColor(yellow);
  74. b_continue->setPaletteForegroundColor(black);
  75. connect(b_continue, SIGNAL(clicked()), SLOT(slotAccept()));
  76. b_continue->setEnabled(false);
  77. TQTimer::singleShot(2000, this, SLOT(slotEnableContinue()));
  78. // cancel button
  79. KPushButton *b_cancel = new KPushButton(KStdGuiItem::cancel(), bottomBox, "KPushButton::b_cancel");
  80. b_cancel->setDefault(true);
  81. b_cancel->setPaletteBackgroundColor(yellow);
  82. b_cancel->setPaletteForegroundColor(black);
  83. connect(b_cancel, SIGNAL(clicked()), SLOT(slotReject()));
  84. l->addWidget(t_message);
  85. l->addWidget(bottomBox);
  86. // init time checker
  87. _checkTimer = new TQTimer(this);
  88. connect(_checkTimer, SIGNAL(timeout()), SLOT(slotCheckTime()));
  89. _checkTimer->start(500);
  90. setFixedSize(sizeHint());
  91. // make sure it is visible
  92. // code from KAlarm
  93. WId id = winId();
  94. KWin::setState(id, NET::StaysOnTop | NET::Sticky);
  95. KWin::setOnAllDesktops(id, true);
  96. }
  97. MMessageDialog::~MMessageDialog()
  98. {
  99. // kdDebug() << "MMessageDialog::~MMessageDialog()" << endl;
  100. }
  101. void MMessageDialog::cancel() {
  102. if (_instance) {
  103. // kdDebug() << "MMessageDialog::cancel()" << endl;
  104. _instance->slotReject();
  105. delete _instance;
  106. _instance = 0;
  107. }
  108. }
  109. bool MMessageDialog::show(const int timeout) {
  110. cancel();
  111. KWin::setOnDesktop(ks_main->winId(), KWin::currentDesktop());
  112. // show warning message dialog
  113. _instance = new MMessageDialog(
  114. timeout,
  115. ks_actions->current(),
  116. ks_actions->getCurrentName()
  117. );
  118. bool result = (_instance->exec() == Accepted);
  119. delete _instance;
  120. _instance = 0;
  121. // kdDebug() << "result = " << result << endl;
  122. return result;
  123. }
  124. // private slots
  125. void MMessageDialog::slotAccept() {
  126. done(Accepted);
  127. }
  128. void MMessageDialog::slotEnableContinue() {
  129. b_continue->setEnabled(true);
  130. }
  131. void MMessageDialog::slotCheckTime()
  132. {
  133. TQTime t = TQTime::currentTime();
  134. if (t.second() == _lastTime)
  135. return;
  136. l_seconds->display(_dialogDelay);
  137. // timeout?
  138. if (_dialogDelay == 0)
  139. {
  140. slotAccept();
  141. return;
  142. }
  143. MiscUtils::notifyUser(_dialogDelay);
  144. _lastTime = t.second();
  145. _dialogDelay--;
  146. }
  147. void MMessageDialog::slotReject() {
  148. done(Rejected);
  149. }
  150. #include "mmessagedialog.moc"