screenutils.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include "screenutils.h"
  2. #include <QWidget>
  3. #include <QRect>
  4. #include <QLabel>
  5. #include <QTimer>
  6. #include <QGridLayout>
  7. #include <QStyleOption>
  8. #include <QMouseEvent>
  9. #include <QPainter>
  10. DesktopBox::DesktopBox(int screen, int w, int h)
  11. : QWidget() {
  12. this->screen = screen;
  13. setStyleSheet(base_style);
  14. setFixedSize(w, h);
  15. }
  16. void DesktopBox::mousePressEvent(QMouseEvent *event) {
  17. if (event->button() == Qt::LeftButton) {
  18. emit clicked();
  19. emit clicked(screen);
  20. }
  21. }
  22. void DesktopBox::mouseDoubleClickEvent(QMouseEvent *event) {
  23. if (event->button() == Qt::LeftButton) {
  24. emit doubleClicked();
  25. }
  26. }
  27. void DesktopBox::paintEvent(QPaintEvent *) {
  28. QStyleOption opt;
  29. opt.init(this);
  30. QPainter p(this);
  31. style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
  32. }
  33. void DesktopBox::highlight() {
  34. highlight(true);
  35. }
  36. void DesktopBox::highlight(bool h) {
  37. if (h)
  38. setStyleSheet(QString(base_style) + "background-color: rbg(255, 0, 0);");
  39. else
  40. setStyleSheet(base_style);
  41. }
  42. void DesktopBox::setIdentifier(QRect dimensions) {
  43. ident = new ScreenIdentifier(screen, dimensions);
  44. }
  45. void DesktopBox::identify() {
  46. if (ident!=NULL) {
  47. ident->show();
  48. }
  49. }
  50. ScreenIdentifier::ScreenIdentifier(int number, QRect dimensions, int timeout, QWidget *parent)
  51. : QWidget(parent) {
  52. //housework
  53. setWindowFlags(Qt::SplashScreen | Qt::WindowStaysOnTopHint);
  54. setCursor(Qt::BlankCursor);
  55. setStyleSheet("background-color: rgb(0, 0, 0);");
  56. // setAttribute(Qt::WA_TranslucentBackground);
  57. //set attributes
  58. this->timeout = timeout;
  59. setBaseSize(dimensions.width(), dimensions.height());
  60. setGeometry(dimensions.x(), dimensions.y(), dimensions.width(), dimensions.height());
  61. //the visuals
  62. QLabel *label = new QLabel(QString::number(number));
  63. label->setFont(QFont("Arial", 340, QFont::Bold));
  64. label->setStyleSheet("color: rgb(255, 255, 255);");
  65. QGridLayout *layout = new QGridLayout(this);
  66. layout->setAlignment(Qt::AlignCenter);
  67. layout->addWidget(label, 0, 0);
  68. //do the timeout
  69. timer = new QTimer();
  70. timer->setSingleShot(true);
  71. connect(timer, SIGNAL(timeout()), this, SLOT(hide()));
  72. }
  73. void ScreenIdentifier::show() {
  74. timer->start(timeout);
  75. QWidget::show();
  76. }