TrafficLightWidget.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /**
  2. * Copyright (c) 2012 committers of YAKINDU and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. * Contributors:
  8. * m.muehlbrandt - initial API and implementation
  9. *
  10. */
  11. #include "TrafficLightWidget.h"
  12. TrafficLightWidget::TrafficLightWidget(QWidget *parent) :
  13. QWidget(parent) {
  14. red = false;
  15. yellow = false;
  16. green = false;
  17. }
  18. void TrafficLightWidget::setSignals(bool red, bool yellow, bool green) {
  19. this->red = red;
  20. this->yellow = yellow;
  21. this->green = green;
  22. }
  23. void TrafficLightWidget::paintEvent(QPaintEvent *event) {
  24. QWidget::paintEvent(event);
  25. QPainter painter(this);
  26. painter.setBrush(Qt::darkGray);
  27. painter.drawRect(this->rect());
  28. QRect rect = this->rect().translated(0, 0);
  29. //red signal
  30. painter.setBrush(red ? Qt::red : Qt::black);
  31. painter.drawEllipse(QPoint(rect.width() / 2, rect.height() / 5),
  32. rect.width() / 3, rect.width() / 3);
  33. //yellow signal
  34. painter.setBrush(yellow ? Qt::yellow : Qt::black);
  35. painter.drawEllipse(QPoint(rect.width() / 2, rect.height() / 2),
  36. rect.width() / 3, rect.width() / 3);
  37. //green signal
  38. painter.setBrush(green ? Qt::green : Qt::black);
  39. painter.drawEllipse(QPoint(rect.width() / 2, rect.height() * 4 / 5),
  40. rect.width() / 3, rect.width() / 3);
  41. }
  42. TrafficLightWidget::~TrafficLightWidget() {
  43. }