1 /*******************************************************************************
2 **
3 ** Photivo
4 **
5 ** Copyright (C) 2009-2011 Michael Munzert <mail@mm-log.com>
6 ** Copyright (C) 2011 Bernd Schoeler <brjohn@brother-john.net>
7 **
8 ** This file is part of Photivo.
9 **
10 ** Photivo is free software: you can redistribute it and/or modify
11 ** it under the terms of the GNU General Public License version 3
12 ** as published by the Free Software Foundation.
13 **
14 ** Photivo is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ** GNU General Public License for more details.
18 **
19 ** You should have received a copy of the GNU General Public License
20 ** along with Photivo.  If not, see <http://www.gnu.org/licenses/>.
21 **
22 *******************************************************************************/
23 
24 #include "ptConstants.h"
25 #include "ptLineInteraction.h"
26 #include "ptDefines.h"
27 
28 #include <cassert>
29 #include <cmath>
30 
31 //==============================================================================
32 
ptLineInteraction(QGraphicsView * View)33 ptLineInteraction::ptLineInteraction(QGraphicsView* View)
34 : ptAbstractInteraction(View),
35   m_NowDragging(0)
36 {
37   m_Line = new QLineF();
38   m_LineItem = NULL;
39 }
40 
41 //==============================================================================
42 
~ptLineInteraction()43 ptLineInteraction::~ptLineInteraction() {
44   delete m_Line;
45 }
46 
47 //==============================================================================
48 
angle()49 double ptLineInteraction::angle() {
50   if (m_Line->x1() == m_Line->x2()) {
51     return 90.0;
52   }
53   double m = -(double)(m_Line->y1() - m_Line->y2()) / (m_Line->x1() - m_Line->x2());
54   return atan(m) * 180.0 / ptPI;
55 }
56 
57 //==============================================================================
58 
Finalize(const ptStatus status)59 void ptLineInteraction::Finalize(const ptStatus status) {
60   if (m_LineItem != NULL) {
61     FView->scene()->removeItem(m_LineItem);
62     DelAndNull(m_LineItem);
63   }
64 
65   m_NowDragging = 0;
66   emit finished(status);
67 }
68 
69 //==============================================================================
70 
keyAction(QKeyEvent * event)71 void ptLineInteraction::keyAction(QKeyEvent* event) {
72   switch (event->type()) {
73     case QEvent::KeyPress:
74       if (event->key() == Qt::Key_Escape) {
75         event->accept();
76         Finalize(stFailure);
77       }
78       break;
79 
80     default:
81       // nothing to do
82       break;
83   }
84 }
85 
86 //==============================================================================
87 
mouseAction(QMouseEvent * event)88 void ptLineInteraction::mouseAction(QMouseEvent* event) {
89   switch (event->type()) {
90     // left button press
91     case QEvent::MouseButtonPress: {
92       if (event->button() == Qt::LeftButton) {
93         event->accept();
94         assert(m_LineItem == NULL);
95 
96         // map viewport coords to scene coords
97         QPointF pos(FView->mapToScene(event->pos()));
98         m_Line->setPoints(pos, pos);
99 
100         m_LineItem = FView->scene()->addLine(*m_Line, QPen(QColor(255, 0, 0)));
101         FView->repaint();
102 
103         m_NowDragging = 1;
104       }
105       break;
106     }
107 
108 
109     // left button release
110     case QEvent::MouseButtonRelease: {
111       if (event->button() == Qt::LeftButton) {
112         event->accept();
113         Finalize(stSuccess);
114       }
115       break;
116     }
117 
118 
119     // mouse move
120     case QEvent::MouseMove: {
121       if (m_NowDragging) {
122         event->accept();
123         m_Line->setP2(FView->mapToScene(event->pos()));
124         m_LineItem->setLine(*m_Line);
125         FView->repaint();
126       }
127       break;
128     }
129 
130 
131     default: {
132       assert(!"Wrong event type");
133       break;
134     }
135   } //switch
136 }
137 
138 //==============================================================================
139