#include <QPainter>
#include <QStyleOptionGraphicsItem>
#include <QCursor>
+#include <QGraphicsView>
OnMonitorRectItem::OnMonitorRectItem(const QRectF &rect, double dar, QGraphicsItem* parent) :
- QGraphicsRectItem(rect, parent),
- m_dar(dar),
- m_modified(false)
+ QGraphicsRectItem(rect, parent)
+ , m_dar(dar)
+ , m_mode(NoAction)
+ , m_modified(false)
+ , m_view(NULL)
{
setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
setAcceptHoverEvents(true);
}
-rectActions OnMonitorRectItem::getMode(QPointF pos)
+rectActions OnMonitorRectItem::getMode(const QPointF &pos)
{
// Item mapped coordinates
QPolygonF pol(rect().normalized());
right.lineTo(pol.at(2));
QPainterPath mouseArea;
- mouseArea.addRect(pos.x() - 4, pos.y() - 4, 8, 8);
+ qreal xsize = 12;
+ qreal ysize = 12;
+ if (getView()) {
+ xsize /= m_view->matrix().m11();
+ ysize /= m_view->matrix().m22();
+ }
+ mouseArea.addRect(pos.x() - xsize / 2, pos.y() - ysize / 2, xsize, ysize);
// Check for collisions between the mouse and the borders
if (mouseArea.contains(pol.at(0)))
// Keep aspect ratio
if (event->modifiers() == Qt::ControlModifier) {
- // compare to rect during mouse press:
+ // compare to rect during mouse press:
// if we subtract rect() we'll get a whole lot of flickering
// because of diffWidth > diffHeight changing all the time
int diffWidth = qAbs(r.width() - m_oldRect.width());
void OnMonitorRectItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
- Q_UNUSED(widget);
+ Q_UNUSED(widget)
+ Q_UNUSED(option)
painter->setPen(pen());
- painter->drawRect(option->rect);
-
+ //painter->setClipRect(option->rect);
+ const QRectF r = rect();
+ painter->drawRect(r);
+ QRectF handle = painter->worldTransform().inverted().mapRect(QRectF(0, 0, 6, 6));
if (isEnabled()) {
- double handleSize = 6 / painter->matrix().m11();
- double halfHandleSize = handleSize / 2;
- painter->fillRect(-halfHandleSize, -halfHandleSize, handleSize, handleSize, QColor(Qt::yellow));
- painter->fillRect(option->rect.width() - halfHandleSize, -halfHandleSize, handleSize, handleSize, QColor(Qt::yellow));
- painter->fillRect(option->rect.width() - halfHandleSize, option->rect.height() - halfHandleSize, handleSize, handleSize, QColor(Qt::yellow));
- painter->fillRect(-halfHandleSize, option->rect.height() - halfHandleSize, handleSize, handleSize, QColor(Qt::yellow));
+ handle.moveTopLeft(r.topLeft());
+ painter->fillRect(handle, QColor(Qt::yellow));
+ handle.moveTopRight(r.topRight());
+ painter->fillRect(handle, QColor(Qt::yellow));
+ handle.moveBottomLeft(r.bottomLeft());
+ painter->fillRect(handle, QColor(Qt::yellow));
+ handle.moveBottomRight(r.bottomRight());
+ painter->fillRect(handle, QColor(Qt::yellow));
+ }
+
+ // Draw cross at center
+ QPointF center = r.center();
+ painter->drawLine(center + QPointF(-handle.width(), 0), center + QPointF(handle.width(), 0));
+ painter->drawLine(center + QPointF(0, handle.height()), center + QPointF(0, -handle.height()));
+}
+
+bool OnMonitorRectItem::getView()
+{
+ if (m_view)
+ return true;
+
+ if (scene() && !scene()->views().isEmpty()) {
+ m_view = scene()->views().first();
+ return true;
+ } else {
+ return false;
}
}