powerdialog.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #include "powerdialog.h"
  2. QString PowerDialog::getInfoText() {
  3. return mStringByType[mType].arg(mSecondsLeft);
  4. }
  5. PowerDialog::PowerDialog(Type type,
  6. Panel* parentPanel,
  7. ConfigManager* cfgMan) {
  8. // Icon theme
  9. QIcon::setThemeName(cfgMan->mIconTheme);
  10. mIconByType[Type::PowerOff] = "system-shutdown";
  11. mIconByType[Type::Reboot] = "system-reboot";
  12. mIconByType[Type::LogOut] = "system-log-out";
  13. mActionByType[Type::PowerOff] = "Shut Down";
  14. mActionByType[Type::Reboot] = "Reboot";
  15. mActionByType[Type::LogOut] = "Log Out";
  16. mStringByType[Type::PowerOff] = QString("Are you sure you want to shut down\nyour system?\n\n"
  17. "If you do nothing, the system will shut down\n"
  18. "automatically in %1 sec");
  19. mStringByType[Type::Reboot] = QString("Are you sure you want to reboot\nyour system?\n\n"
  20. "If you do nothing, the system will reboot\n"
  21. "automatically in %1 sec");
  22. mStringByType[Type::LogOut] = QString("Are you sure you want to quit all applications\n"
  23. "and log out?\n\n"
  24. "If you do nothing, you will be logged out\n"
  25. "automatically in %1 sec");
  26. mParentPanel = parentPanel;
  27. mType = type;
  28. mSecondsLeft = cfgMan->mSecondsUntilPowerOff;
  29. mTimer = new QTimer(this);
  30. mTimer->setInterval(1000);
  31. QWidget* dialog = new QWidget();
  32. dialog->setObjectName("powerDialog");
  33. // Window flags
  34. dialog->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
  35. // Font
  36. dialog->setFont(cfgMan->mFont);
  37. // Theme
  38. QFile stylesheetReader("/usr/share/plainDE/styles/" + cfgMan->mStylesheet);
  39. stylesheetReader.open(QIODevice::ReadOnly | QIODevice::Text);
  40. QTextStream styleSheet(&stylesheetReader);
  41. dialog->setStyleSheet(styleSheet.readAll());
  42. // Opacity
  43. dialog->setWindowOpacity(parentPanel->mOpacity);
  44. // UI
  45. QVBoxLayout* mainLayout = new QVBoxLayout(dialog);
  46. QHBoxLayout* infoLayout = new QHBoxLayout();
  47. infoLayout->addSpacerItem(new QSpacerItem(0, 0,
  48. QSizePolicy::MinimumExpanding,
  49. QSizePolicy::Ignored));
  50. QLabel* iconLabel = new QLabel();
  51. iconLabel->setPixmap(QIcon::fromTheme(mIconByType[type]).pixmap(64, 64));
  52. iconLabel->setAlignment(Qt::AlignCenter);
  53. infoLayout->addWidget(iconLabel);
  54. QLabel* infoLabel = new QLabel(mStringByType[type].arg(mSecondsLeft));
  55. //infoLabel->setWordWrap(true);
  56. infoLayout->addWidget(infoLabel);
  57. infoLayout->addSpacerItem(new QSpacerItem(0, 0,
  58. QSizePolicy::MinimumExpanding,
  59. QSizePolicy::Ignored));
  60. mainLayout->addLayout(infoLayout);
  61. QHBoxLayout* buttonsLayout = new QHBoxLayout();
  62. buttonsLayout->addSpacerItem(new QSpacerItem(0, 0,
  63. QSizePolicy::MinimumExpanding,
  64. QSizePolicy::Ignored));
  65. QPushButton* cancelPushButton = new QPushButton("Cancel");
  66. connect(cancelPushButton, &QPushButton::clicked, this, [this, dialog]() {
  67. delete dialog;
  68. delete mTimer;
  69. emit cancelled();
  70. });
  71. buttonsLayout->addWidget(cancelPushButton);
  72. QPushButton* actionPushButton = new QPushButton(mActionByType[type]);
  73. connect(actionPushButton, &QPushButton::clicked, this, [this, dialog]() {
  74. delete dialog;
  75. delete mTimer;
  76. emit actionRequested();
  77. });
  78. if (cfgMan->mSecondsUntilPowerOff > -1) {
  79. connect(mTimer, &QTimer::timeout, this, [this, infoLabel]() {
  80. if (mSecondsLeft > 0) {
  81. --mSecondsLeft;
  82. infoLabel->setText(getInfoText());
  83. }
  84. else {
  85. emit actionRequested();
  86. }
  87. });
  88. }
  89. else {
  90. QString text = getInfoText();
  91. infoLabel->setText(text.split("\n\n")[0]);
  92. }
  93. buttonsLayout->addWidget(actionPushButton);
  94. mainLayout->addLayout(buttonsLayout);
  95. QScreen* screen = dialog->screen();
  96. QRect scrGeometry = screen->geometry();
  97. dialog->setGeometry(scrGeometry.width() / 2 - 210,
  98. scrGeometry.height() / 2 - 75,
  99. 420,
  100. 150);
  101. dialog->setMinimumSize(420, 150);
  102. dialog->setMaximumSize(420, 150);
  103. // Set logout sound
  104. mPlayer = new QMediaPlayer();
  105. QString path = cfgMan->mLogoutSound;
  106. qDebug() << path;
  107. if (!path.isEmpty() && QFile::exists(path)) {
  108. mPlayer->setMedia(QUrl::fromLocalFile(path));
  109. mPlayLogoutSound = true;
  110. }
  111. // Transparency
  112. if (cfgMan->mTransparent) {
  113. setTransparency(dialog,
  114. dialog->geometry().x(),
  115. dialog->geometry().y(),
  116. 420,
  117. 150);
  118. }
  119. dialog->show();
  120. }
  121. void PowerDialog::startTimer() {
  122. mTimer->start();
  123. }
  124. void PowerDialog::setTransparency(QWidget* widget,
  125. int ax,
  126. int ay,
  127. int width,
  128. int height) {
  129. QScreen* screen = widget->screen();
  130. QPixmap pixmap = screen->grabWindow(0, ax, ay, width, height);
  131. QGraphicsBlurEffect* blurEffect = new QGraphicsBlurEffect();
  132. blurEffect->setBlurRadius(15);
  133. blurEffect->setBlurHints(QGraphicsBlurEffect::QualityHint);
  134. QGraphicsScene* scene = new QGraphicsScene();
  135. QGraphicsPixmapItem item;
  136. item.setPixmap(pixmap);
  137. item.setGraphicsEffect(blurEffect);
  138. scene->addItem(&item);
  139. QImage res(QSize(width, height), QImage::Format_ARGB32);
  140. res.fill(Qt::transparent);
  141. QPainter ptr(&res);
  142. scene->render(&ptr, QRectF(), QRectF(0, 0, width, height));
  143. QPalette palette;
  144. palette.setBrush(widget->backgroundRole(),
  145. QBrush(QPixmap::fromImage(res)));
  146. widget->setPalette(palette);
  147. }