File indexing completed on 2024-04-21 05:43:55

0001 /***************************************************************************
0002  *   Copyright (C) 2005 by David Saxton                                    *
0003  *   david@bluehaze.org                                                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  ***************************************************************************/
0010 
0011 #include "eventinfo.h"
0012 #include "itemdocument.h"
0013 #include "itemview.h"
0014 
0015 #include <QMouseEvent>
0016 
0017 EventInfo::EventInfo()
0018 {
0019     reset();
0020 }
0021 
0022 EventInfo::EventInfo(ItemView *itemView, QEvent *e)
0023 {
0024     Q_UNUSED(itemView);
0025     Q_UNUSED(e);
0026     reset();
0027 }
0028 
0029 void EventInfo::extractPos(ItemView *itemView, const QPoint &contentsMouseClick)
0030 {
0031     pos = itemView->mousePosToCanvasPos(contentsMouseClick);
0032 }
0033 
0034 EventInfo::EventInfo(ItemView *itemView, QMouseEvent *e)
0035 {
0036     reset();
0037 
0038     extractPos(itemView, e->pos());
0039     globalPos = e->globalPos();
0040     isRightClick = e->button() == Qt::RightButton;
0041     isMiddleClick = e->button() == Qt::MidButton;
0042     ctrlPressed = e->modifiers() & Qt::ControlModifier; // QMouseEvent::ControlButton;
0043     shiftPressed = e->modifiers() & Qt::ShiftModifier;  // QMouseEvent::ShiftButton;
0044     altPressed = e->modifiers() & Qt::AltModifier;      // QMouseEvent::AltButton;
0045     if (ItemDocument *id = dynamic_cast<ItemDocument *>(itemView->document()))
0046         qcanvasItemClickedOn = id->itemAtTop(pos);
0047 }
0048 
0049 EventInfo::EventInfo(ItemView *itemView, QWheelEvent *e)
0050 {
0051     reset();
0052 
0053     extractPos(itemView, e->position().toPoint());
0054     globalPos = e->globalPosition().toPoint();
0055     ctrlPressed = e->modifiers() & Qt::ControlModifier; // QMouseEvent::ControlButton;
0056     shiftPressed = e->modifiers() & Qt::ShiftModifier;  // QMouseEvent::ShiftButton;
0057     altPressed = e->modifiers() & Qt::AltModifier;      // QMouseEvent::AltButton;
0058     if (ItemDocument *id = dynamic_cast<ItemDocument *>(itemView->document()))
0059         qcanvasItemClickedOn = id->itemAtTop(pos);
0060     pixelDelta = e->pixelDelta();
0061     angleDelta = e->angleDelta();
0062     phase = e->phase();
0063     inverted = e->inverted();
0064 }
0065 
0066 void EventInfo::reset()
0067 {
0068     isRightClick = false;
0069     isMiddleClick = false;
0070     ctrlPressed = false;
0071     shiftPressed = false;
0072     altPressed = false;
0073     qcanvasItemClickedOn = nullptr;
0074     pixelDelta.setX(0);
0075     pixelDelta.setY(0);
0076     angleDelta.setX(0);
0077     angleDelta.setY(0);
0078     phase = Qt::NoScrollPhase;
0079     inverted = false;
0080 }
0081 
0082 QMouseEvent *EventInfo::mousePressEvent(int dx, int dy) const
0083 {
0084     return new QMouseEvent(QEvent::MouseButtonPress,
0085                            pos + QPoint(dx, dy),
0086                            (isRightClick ? Qt::RightButton : Qt::LeftButton),
0087                            (isRightClick ? Qt::RightButton : Qt::LeftButton),
0088                            (ctrlPressed ? Qt::ControlModifier : Qt::NoModifier) | (shiftPressed ? Qt::ShiftModifier : Qt::NoModifier) | (altPressed ? Qt::AltModifier : Qt::NoModifier));
0089 }
0090 
0091 QMouseEvent *EventInfo::mouseReleaseEvent(int dx, int dy) const
0092 {
0093     return new QMouseEvent(QEvent::MouseButtonRelease,
0094                            pos + QPoint(dx, dy),
0095                            (isRightClick ? Qt::RightButton : Qt::LeftButton),
0096                            Qt::NoButton
0097                            /*(isRightClick ? Qt::RightButton : Qt::LeftButton)
0098                             * - 2017.09.18 - this is the state after the mouse has been released */
0099                            ,
0100                            (ctrlPressed ? Qt::ControlModifier : Qt::NoModifier) | (shiftPressed ? Qt::ShiftModifier : Qt::NoModifier) | (altPressed ? Qt::AltModifier : Qt::NoModifier));
0101 }
0102 
0103 QMouseEvent *EventInfo::mouseDoubleClickEvent(int dx, int dy) const
0104 {
0105     return new QMouseEvent(QEvent::MouseButtonDblClick,
0106                            pos + QPoint(dx, dy),
0107                            (isRightClick ? Qt::RightButton : Qt::LeftButton),
0108                            (isRightClick ? Qt::RightButton : Qt::LeftButton),
0109                            (ctrlPressed ? Qt::ControlModifier : Qt::NoModifier) | (shiftPressed ? Qt::ShiftModifier : Qt::NoModifier) | (altPressed ? Qt::AltModifier : Qt::NoModifier));
0110 }
0111 
0112 QMouseEvent *EventInfo::mouseMoveEvent(int dx, int dy) const
0113 {
0114     return new QMouseEvent(
0115         QEvent::MouseMove, pos + QPoint(dx, dy), Qt::NoButton, Qt::NoButton, (ctrlPressed ? Qt::ControlModifier : Qt::NoModifier) | (shiftPressed ? Qt::ShiftModifier : Qt::NoModifier) | (altPressed ? Qt::AltModifier : Qt::NoModifier));
0116 }
0117 
0118 QWheelEvent *EventInfo::wheelEvent(int dx, int dy) const
0119 {
0120     return new QWheelEvent(
0121         pos + QPoint(dx, dy), globalPos,
0122         pixelDelta, angleDelta,
0123         Qt::NoButton,
0124         (ctrlPressed ? Qt::ControlModifier : Qt::NoModifier) | (shiftPressed ? Qt::ShiftModifier : Qt::NoModifier) | (altPressed ? Qt::AltModifier : Qt::NoModifier),
0125         phase, inverted);
0126 }