/experiments/graphicstherapist/displaycell.cpp

https://code.google.com/p/dwarftherapist/ · C++ · 97 lines · 72 code · 13 blank · 12 comment · 8 complexity · 4274dd537497a4b66a2bbac2cef08009 MD5 · raw file

  1. #include "displaycell.h"
  2. #include <QtGui>
  3. DisplayCell::DisplayCell(QGraphicsItem *parent)
  4. : BaseGraphicsObject(parent)
  5. , m_tooltip(new ToolTip("My Tooltip"))
  6. , m_show_tooltip(false)
  7. , m_labor_on(false)
  8. , m_bg(QBrush(Qt::gray))
  9. , m_bg_selected(QBrush(Qt::yellow))
  10. {
  11. setAcceptHoverEvents(true);
  12. m_tooltip->setZValue(1);
  13. m_tooltip->hide();
  14. this->setFlag(QGraphicsItem::ItemIsFocusable, true);
  15. connect(this, SIGNAL(hover_start()), SLOT(on_hover_start()));
  16. connect(this, SIGNAL(hover_stop()), SLOT(on_hover_stop()));
  17. connect(this, SIGNAL(single_click()), SLOT(toggle()));
  18. }
  19. DisplayCell::~DisplayCell() {
  20. if (m_tooltip)
  21. m_tooltip->deleteLater();
  22. }
  23. QRectF DisplayCell::boundingRect() const {
  24. return QRectF(-box_w / 2.0f, -box_h / 2.0f, box_w, box_h);
  25. }
  26. void DisplayCell::paint(QPainter *p, const QStyleOptionGraphicsItem *opt,
  27. QWidget *widget) {
  28. p->save();
  29. if (scene()->focusItem() == this) {
  30. p->setPen(QPen(Qt::white, 0.5));
  31. } else {
  32. p->setPen(QPen(Qt::black, 0.5));
  33. }
  34. if (isEnabled() && m_labor_on) {
  35. p->setBrush(m_bg_selected);
  36. } else {
  37. p->setBrush(m_bg);
  38. }
  39. p->drawRect(boundingRect().adjusted(0.5f, 0.5f, -0.5f, -0.5f));
  40. p->restore();
  41. }
  42. void DisplayCell::on_hover_start() {
  43. scene()->setFocusItem(this, Qt::MouseFocusReason);
  44. //qDebug() << "Hover";
  45. m_show_tooltip = true;
  46. QTimer::singleShot(100, this, SLOT(maybe_show_tooltip()));
  47. }
  48. void DisplayCell::on_hover_stop() {
  49. //qDebug() << "Leave";
  50. m_show_tooltip = false;
  51. m_tooltip->hide();
  52. }
  53. void DisplayCell::on_added_to_scene(QGraphicsScene *scene) {
  54. scene->addItem(m_tooltip);
  55. }
  56. void DisplayCell::maybe_show_tooltip() {
  57. if (m_show_tooltip) {
  58. m_tooltip->setPos(mapToScene(boundingRect().bottomRight()) +
  59. QPoint(4, 4));
  60. m_tooltip->show();
  61. /*
  62. QPropertyAnimation *ani = new QPropertyAnimation(m_tooltip, "scale");
  63. connect(ani, SIGNAL(finished()), ani, SLOT(deleteLater()));
  64. ani->setDuration(200);
  65. ani->setStartValue(0.4f);
  66. ani->setEndValue(1.0f);
  67. ani->setEasingCurve(QEasingCurve::InExpo);
  68. */
  69. QPropertyAnimation *ani2 = new QPropertyAnimation(m_tooltip, "opacity");
  70. connect(ani2, SIGNAL(finished()), ani2, SLOT(deleteLater()));
  71. ani2->setDuration(500);
  72. ani2->setStartValue(0);
  73. ani2->setEndValue(1.0f);
  74. //ani->start();
  75. ani2->start();
  76. }
  77. m_show_tooltip = false;
  78. }
  79. void DisplayCell::toggle() {
  80. qDebug() << this << "toggled!";
  81. m_labor_on = !m_labor_on;
  82. maybe_update();
  83. //update();
  84. }