1 /***************************************************************************
2  *   Copyright (C) 2006-2009 by Rajko Albrecht                             *
3  *   ral@alwins-world.de                                                   *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20 #include "pannerview.h"
21 #include "graphtree_defines.h"
22 
23 #include <QPainter>
24 #include <QMouseEvent>
25 #include <QGraphicsRectItem>
26 #include <QStyleOptionGraphicsItem>
27 
28 class GraphPanMark: public QGraphicsRectItem
29 {
30 public:
31     GraphPanMark(QGraphicsItem *p = nullptr);
32     ~GraphPanMark();
33     int type()const override;
34     void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *) override;
35 };
36 
GraphPanMark(QGraphicsItem * p)37 GraphPanMark::GraphPanMark(QGraphicsItem *p)
38     : QGraphicsRectItem(p)
39 {
40     setZValue(1.9);
41     setPen(QPen(Qt::red));
42     QPen pe = pen();
43     pe.setWidthF(0.0);
44     pe.setStyle(Qt::DashDotLine);
45     setPen(pe);
46 }
47 
~GraphPanMark()48 GraphPanMark::~GraphPanMark()
49 {
50 }
51 
paint(QPainter * p,const QStyleOptionGraphicsItem * option,QWidget * w)52 void GraphPanMark::paint(QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *w)
53 {
54     if (option->levelOfDetail < .5) {
55         QGraphicsRectItem::paint(p, option, w);
56     }
57 }
58 
type() const59 int GraphPanMark::type()const
60 {
61     return GRAPHTREE_PANMARK;
62 }
63 
PannerView(QWidget * parent)64 PannerView::PannerView(QWidget *parent)
65     : QGraphicsView(parent)// KDE4 check , /*Qt::WNoAutoErase |*/ Qt::WA_StaticContents/*WStaticContents*/ )
66 {
67     m_Mark = nullptr;
68     m_Moving = false;
69     viewport()->setFocusPolicy(Qt::NoFocus);
70     setFocusPolicy(Qt::NoFocus);
71 }
72 
~PannerView()73 PannerView::~PannerView()
74 {
75     if (scene() && m_Mark) {
76         scene()->removeItem(m_Mark);
77         delete m_Mark;
78     }
79 }
80 
setScene(QGraphicsScene * sc)81 void PannerView::setScene(QGraphicsScene *sc)
82 {
83     if (!sc) {
84         if (scene()) {
85             scene()->removeItem(m_Mark);
86         }
87     } else {
88         if (!m_Mark) {
89             m_Mark = new GraphPanMark;
90         }
91         sc->addItem(m_Mark);
92     }
93     QGraphicsView::setScene(sc);
94 }
95 
setZoomRect(const QRectF & theValue)96 void PannerView::setZoomRect(const QRectF &theValue)
97 {
98     m_ZoomRect = theValue;
99     if (m_Mark) {
100         m_Mark->setRect(m_ZoomRect);
101     }
102 }
103 
104 /*!
105     \fn PannerView::contentsMouseMoveEvent(QMouseEvent* e)
106  */
mouseMoveEvent(QMouseEvent * e)107 void PannerView::mouseMoveEvent(QMouseEvent *e)
108 {
109     if (m_Moving) {
110         QPointF sPos = mapToScene(e->pos());
111         emit zoomRectMoved(sPos.x() - m_ZoomRect.center().x(),
112                            sPos.y() - m_ZoomRect.center().y());
113 
114         m_LastPos = e->pos();
115     }
116 }
117 
118 /*!
119     \fn PannerView::contentsMousePressEvent(QMouseEvent* e)
120  */
mousePressEvent(QMouseEvent * e)121 void PannerView::mousePressEvent(QMouseEvent *e)
122 {
123     if (m_ZoomRect.isValid()) {
124         QPointF sPos = mapToScene(e->pos());
125         if (!m_ZoomRect.contains(sPos)) {
126             emit zoomRectMoved(sPos.x() - m_ZoomRect.center().x(),
127                                sPos.y() - m_ZoomRect.center().y());
128         }
129         m_Moving = true;
130         m_LastPos = e->pos();
131     }
132 }
133 
134 /*!
135     \fn PannerView::contentsMouseReleaseEvent(QMouseEvent*)
136  */
mouseReleaseEvent(QMouseEvent *)137 void PannerView::mouseReleaseEvent(QMouseEvent *)
138 {
139     m_Moving = false;
140     emit zoomRectMoveFinished();
141 }
142 
143 /*!
144     \fn PannerView::updateCurrentRect()
145  */
updateCurrentRect()146 void PannerView::updateCurrentRect()
147 {
148     if (m_ZoomRect.isValid()) {
149     }
150 }
151