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.

155 lines
3.9KB

  1. /*
  2. progressbar.cpp - Progress Bar
  3. Copyright (C) 2006 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 <tqapplication.h>
  17. #include <tqdesktopwidget.h>
  18. #include <tqpainter.h>
  19. #include <tdeapplication.h>
  20. #include <kdebug.h>
  21. #include <tdelocale.h>
  22. #include <tdepopupmenu.h>
  23. #include "configuration.h"
  24. #include "progressbar.h"
  25. // static
  26. ProgressBar *ProgressBar::_instance = 0;
  27. // public
  28. ProgressBar::~ProgressBar() { }
  29. void ProgressBar::setPosition(const Position value) {
  30. // kdDebug() << "ProgressBar::setPosition: " << value << endl;
  31. kshutdownrc->progressBarPosition = value;
  32. _position = value;
  33. TQDesktopWidget *desktop = TQApplication::desktop();
  34. resize(desktop->width() - 4, height());
  35. switch (_position) {
  36. case TOP:
  37. move(2, 0);
  38. break;
  39. case BOTTOM:
  40. move(2, desktop->height() - height());
  41. break;
  42. default:
  43. move(2, 0);
  44. break;
  45. }
  46. }
  47. void ProgressBar::setHeight(const int value) {
  48. int newHeight = (value < 2) ? 2 : value;
  49. resize(width(), newHeight);
  50. }
  51. void ProgressBar::setProgress(const int value) {
  52. // kdDebug() << "ProgressBar::setProgress: " << value << endl;
  53. int complete = _total - value;
  54. if (_complete == complete)
  55. return;
  56. _complete = complete;
  57. repaint();
  58. }
  59. void ProgressBar::setValues(const int complete, const int total) {
  60. // kdDebug() << "ProgressBar::setValues: " << complete << ", " << total << endl;
  61. if ((_complete == complete) && (_total == total))
  62. return;
  63. _complete = complete;
  64. _total = total;
  65. repaint();
  66. }
  67. // protected
  68. void ProgressBar::mousePressEvent(TQMouseEvent *e) {
  69. // kdDebug() << "ProgressBar::mousePressEvent" << endl;
  70. if (e->button() == RightButton) {
  71. // TODO: 2.0: color configuration
  72. // TODO: 2.0: size configuration
  73. // show popup menu
  74. TDEPopupMenu *popup = new TDEPopupMenu(this);
  75. popup->insertTitle(kapp->miniIcon(), "KShutDown");
  76. popup->insertItem(i18n("Hide"), this, SLOT(hide()));
  77. popup->insertTitle("Position");
  78. int topPositionItem = popup->insertItem(i18n("Top"), this, SLOT(slotSetTopPosition()));
  79. popup->setItemChecked(topPositionItem, _position == TOP);
  80. int bottomPositionItem = popup->insertItem(i18n("Bottom"), this, SLOT(slotSetBottomPosition()));
  81. popup->setItemChecked(bottomPositionItem, _position == BOTTOM);
  82. popup->popup(e->globalPos());
  83. e->accept();
  84. }
  85. TQWidget::mousePressEvent(e);
  86. }
  87. void ProgressBar::paintEvent(TQPaintEvent *) {
  88. // kdDebug() << "ProgressBar::paintEvent" << endl;
  89. TQPainter g(this);
  90. int w = width();
  91. int h = height();
  92. g.fillRect(0, 0, w, h, TQBrush(backgroundColor()));
  93. if ((_complete <= 0) || (_total <= 0))
  94. return;
  95. w = (int)((float)w * ((float)_complete * 100.0f / (float)_total) / 100.0f);
  96. g.fillRect(0, 0, w, h, TQBrush(foregroundColor()));
  97. }
  98. // private
  99. ProgressBar::ProgressBar()
  100. : TQWidget(
  101. 0,
  102. "ProgressBar",
  103. WStyle_NoBorder | WStyle_StaysOnTop | WType_TopLevel | WX11BypassWM
  104. ),
  105. _complete(0),
  106. _total(0) {
  107. // kdDebug() << "ProgressBar::ProgressBar()" << endl;
  108. setBackgroundColor(black);
  109. setPaletteForegroundColor(yellow);
  110. setHeight(3);
  111. setPosition((Position)kshutdownrc->progressBarPosition);
  112. }
  113. // private slots
  114. void ProgressBar::slotSetBottomPosition() {
  115. setPosition(BOTTOM);
  116. }
  117. void ProgressBar::slotSetTopPosition() {
  118. setPosition(TOP);
  119. }
  120. #include "progressbar.moc"