00001 #include "accountgraphicsproxywidget.h"
00002 #include <QStyleOptionGraphicsItem>
00003 #include <QPainter>
00004 #include <QDebug>
00005 #include <QGraphicsScene>
00006
00007 AccountGraphicsProxyWidget::AccountGraphicsProxyWidget(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsProxyWidget(parent, wFlags), popupShown(false)
00008 {
00009 timeLine = new QTimeLine(250, this);
00010 connect(timeLine, SIGNAL(valueChanged(qreal)), this, SLOT(updateStep(qreal)));
00011 connect(timeLine, SIGNAL(stateChanged(QTimeLine::State)), this, SLOT(stateChanged(QTimeLine::State)));
00012 }
00013
00014 AccountGraphicsProxyWidget::~AccountGraphicsProxyWidget()
00015 {
00016 timeLine->deleteLater();
00017 }
00018
00019 QRectF AccountGraphicsProxyWidget::boundingRect() const
00020 {
00021 return QGraphicsProxyWidget::boundingRect().adjusted(0, 0, 10, 10);
00022 }
00023
00024 void AccountGraphicsProxyWidget::paintWindowFrame(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
00025 {
00026 const QColor color(0, 0, 0, 64);
00027
00028 QRectF r = windowFrameRect();
00029 QRectF right(r.right(), r.top() + 10, 10, r.height() - 10);
00030 QRectF bottom(r.left() + 10, r.bottom(), r.width(), 10);
00031 bool intersectsRight = right.intersects(option->exposedRect);
00032 bool intersectsBottom = bottom.intersects(option->exposedRect);
00033 if(intersectsRight && intersectsBottom)
00034 {
00035 QPainterPath path;
00036 path.addRect(right);
00037 path.addRect(bottom);
00038 painter->setPen(Qt::NoPen);
00039 painter->setBrush(color);
00040 painter->drawPath(path);
00041 }
00042 else if(intersectsBottom)
00043 {
00044 painter->fillRect(bottom, color);
00045 }
00046 else if(intersectsRight)
00047 {
00048 painter->fillRect(right, color);
00049 }
00050
00051 QGraphicsProxyWidget::paintWindowFrame(painter, option, widget);
00052 }
00053
00054 void AccountGraphicsProxyWidget::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
00055 {
00056
00057
00058 QGraphicsProxyWidget::hoverEnterEvent(event);
00059 scene()->setActiveWindow(this);
00060 if(timeLine->currentValue() != 1)
00061 {
00062 zoomIn();
00063 }
00064 }
00065
00066 void AccountGraphicsProxyWidget::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
00067 {
00068
00069 QGraphicsProxyWidget::hoverLeaveEvent(event);
00070 if(!popupShown && (timeLine->direction() != QTimeLine::Backward || timeLine->currentValue() != 0))
00071 {
00072 zoomOut();
00073 }
00074 }
00075
00076 bool AccountGraphicsProxyWidget::sceneEventFilter(QGraphicsItem *watched, QEvent *event)
00077 {
00078 if(watched->isWindow() && (event->type() == QEvent::UngrabMouse || event->type() == QEvent::GrabMouse))
00079 {
00080 popupShown = watched->isVisible();
00081 if(!popupShown && !isUnderMouse())
00082 {
00083 zoomOut();
00084 }
00085 }
00086 return QGraphicsProxyWidget::sceneEventFilter(watched, event);
00087 }
00088
00089 QVariant AccountGraphicsProxyWidget::itemChange(GraphicsItemChange change, const QVariant &value)
00090 {
00091 if(change == ItemChildAddedChange || change == ItemChildRemovedChange)
00092 {
00093 QGraphicsItem *item = qVariantValue<QGraphicsItem *>(value);
00094 if(change == ItemChildAddedChange)
00095 {
00096 item->installSceneEventFilter(this);
00097 }
00098 else
00099 {
00100 item->removeSceneEventFilter(this);
00101 }
00102 }
00103 return QGraphicsProxyWidget::itemChange(change, value);
00104 }
00105
00106 void AccountGraphicsProxyWidget::updateStep(qreal step)
00107 {
00108 QRectF r = boundingRect();
00109 setTransform(QTransform()
00110 .translate(r.width() / 2, r.height() / 2)
00111 .scale(1 + 1.05 * step, 1 + 1.05 * step)
00112 .translate(-r.width() / 2, -r.height() / 2));
00113 }
00114
00115 void AccountGraphicsProxyWidget::stateChanged(QTimeLine::State state)
00116 {
00117 if(state == QTimeLine::Running)
00118 {
00119 if(timeLine->direction() == QTimeLine::Forward)
00120 {
00121 setCacheMode(NoCache);
00122 }
00123 }
00124 else if(state == QTimeLine::NotRunning)
00125 {
00126 if(timeLine->direction() == QTimeLine::Backward)
00127 {
00128 setCacheMode(DeviceCoordinateCache);
00129 }
00130 }
00131 }
00132
00133 void AccountGraphicsProxyWidget::zoomIn()
00134 {
00135 if(timeLine->direction() != QTimeLine::Forward)
00136 {
00137 timeLine->setDirection(QTimeLine::Forward);
00138 }
00139 if(timeLine->state() == QTimeLine::NotRunning)
00140 {
00141 timeLine->start();
00142 }
00143 }
00144
00145 void AccountGraphicsProxyWidget::zoomOut()
00146 {
00147 if(timeLine->direction() != QTimeLine::Backward)
00148 {
00149 timeLine->setDirection(QTimeLine::Backward);
00150 }
00151 if(timeLine->state() == QTimeLine::NotRunning)
00152 {
00153 timeLine->start();
00154 }
00155 }