applet.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include "applet.h"
  2. void Applet::setBlurredBackground() {
  3. QScreen* screen = mInternalWidget->screen();
  4. QRect screenGeometry = screen->geometry();
  5. QRect widgetGeometry = mInternalWidget->geometry();
  6. QPixmap pixmap = screen->grabWindow(0,
  7. widgetGeometry.x(),
  8. widgetGeometry.y(),
  9. widgetGeometry.width(),
  10. widgetGeometry.height());
  11. QGraphicsBlurEffect* blurEffect = new QGraphicsBlurEffect();
  12. blurEffect->setBlurRadius(15);
  13. blurEffect->setBlurHints(QGraphicsBlurEffect::QualityHint);
  14. QGraphicsScene* scene = new QGraphicsScene();
  15. QGraphicsPixmapItem item;
  16. item.setPixmap(pixmap);
  17. item.setGraphicsEffect(blurEffect);
  18. scene->addItem(&item);
  19. QImage res(QSize(widgetGeometry.width(), widgetGeometry.height()), QImage::Format_ARGB32);
  20. res.fill(Qt::transparent);
  21. QPainter ptr(&res);
  22. scene->render(&ptr, QRectF(), QRectF(0, 0, widgetGeometry.width(), widgetGeometry.height()));
  23. QPalette palette;
  24. palette.setBrush(mInternalWidget->backgroundRole(),
  25. QBrush(QPixmap::fromImage(res)));
  26. mInternalWidget->setPalette(palette);
  27. }
  28. QPair<int,int> Applet::getButtonCoordinates() {
  29. int buttonCoord1, buttonCoord2;
  30. if (mParentPanel->mPanelLayout == Horizontal) {
  31. buttonCoord1 = mExternalWidget->x() + mParentPanel->mAxisShift;
  32. buttonCoord2 = mExternalWidget->geometry().topRight().x() +
  33. mParentPanel->mAxisShift;
  34. }
  35. else { // Vertical
  36. buttonCoord1 = mExternalWidget->y() + mParentPanel->mAxisShift;
  37. buttonCoord2 = mExternalWidget->geometry().bottomRight().y() +
  38. mParentPanel->mAxisShift;
  39. }
  40. return qMakePair(buttonCoord1, buttonCoord2);
  41. }
  42. void Applet::preliminaryInternalWidgetSetup(int width,
  43. int height,
  44. bool canBeTransparent) {
  45. // Window flags
  46. mInternalWidget->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
  47. // Geometry
  48. QScreen* screen = mParentPanel->mPanelScreen;
  49. QRect screenGeometry = screen->geometry();
  50. int ax = 0, ay = 0;
  51. QPair<int,int> buttonCoords = getButtonCoordinates();
  52. int buttonCoord1 = buttonCoords.first, buttonCoord2 = buttonCoords.second;
  53. switch (mParentPanel->mPanelLocation) {
  54. case Top:
  55. ax = (screenGeometry.width() - buttonCoord1 >= width) ? buttonCoord1 : buttonCoord2 - width;
  56. ay = mParentPanel->mPanelThickness + 5;
  57. break;
  58. case Bottom:
  59. ax = (screenGeometry.width() - buttonCoord1 >= width) ? buttonCoord1 : buttonCoord2 - width;
  60. ay = screenGeometry.height() - mParentPanel->mPanelThickness - height - 5;
  61. break;
  62. case Left:
  63. ax = mParentPanel->mPanelThickness + 5;
  64. ay = (screenGeometry.height() - buttonCoord1 >= height) ? buttonCoord1 : buttonCoord2 - height;
  65. break;
  66. case Right:
  67. ax = screenGeometry.width() - mParentPanel->mPanelThickness - width - 5;
  68. ay = (screenGeometry.height() - buttonCoord1 >= height) ? buttonCoord1 : buttonCoord2 - height;
  69. break;
  70. }
  71. ax += screenGeometry.x();
  72. ay += screenGeometry.y();
  73. mInternalWidget->setFixedSize(width, height);
  74. mInternalWidget->move(ax, ay);
  75. // Font
  76. mInternalWidget->setFont(mCfgMan->mFont);
  77. // Theme
  78. QFile stylesheetReader("/usr/share/plainDE/styles/" + mCfgMan->mStylesheet);
  79. stylesheetReader.open(QIODevice::ReadOnly | QIODevice::Text);
  80. QTextStream styleSheet(&stylesheetReader);
  81. mInternalWidget->setStyleSheet(styleSheet.readAll());
  82. if (mCfgMan->mTransparent && canBeTransparent) {
  83. setBlurredBackground();
  84. }
  85. // Opacity
  86. mInternalWidget->setWindowOpacity(mParentPanel->mOpacity);
  87. }
  88. void Applet::externalWidgetSetup() {
  89. }
  90. void Applet::internalWidgetSetup() {
  91. }
  92. Applet::Applet(QString appletID, ConfigManager* cfgMan, Panel* parentPanel) {
  93. mAppletID = appletID;
  94. mCfgMan = cfgMan;
  95. mParentPanel = parentPanel;
  96. }