Subversion Repositories Scribus

Rev

Rev 11338 | Rev 11556 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
11338 avox 1
/*
2
 For general Scribus (>=1.3.2) copyright and licensing information please refer
3
 to the COPYING file provided with the program. Following this notice may exist
4
 a copyright and/or license notice that predates the release of Scribus 1.3.2
5
 for which a new license (GPL+exception) is in place.
6
 */
7
/***************************************************************************
8
*                                                                         *
9
*   This program is free software; you can redistribute it and/or modify  *
10
*   it under the terms of the GNU General Public License as published by  *
11
*   the Free Software Foundation; either version 2 of the License, or     *
12
*   (at your option) any later version.                                   *
13
*                                                                         *
14
***************************************************************************/
15
 
16
#include "canvasgesture_linemove.h"
17
 
18
#include <QDebug>
19
#include <QMouseEvent>
20
#include <QPainter>
21
#include <QPen>
22
 
23
#include "canvas.h"
24
#include "pageitem_line.h"
25
#include "scribusdoc.h"
26
#include "scribusview.h"
27
#include "selection.h"
28
#include "util_math.h"
29
 
30
 
31
void LineMove::clear()
32
{
33
        m_haveLineItem = false;
34
}
35
 
36
 
37
void LineMove::prepare(QPointF start, QPointF end)
38
{
39
        m_haveLineItem = false;
40
        setStartPoint(start);
41
        setStartPoint(end);
42
}
43
 
44
 
45
void LineMove::prepare(PageItem_Line* line, bool useOriginAsEndpoint)
46
{
47
        m_haveLineItem = (line != NULL);
48
        if (!m_haveLineItem)
49
                return;
50
        m_useOriginAsEndpoint = useOriginAsEndpoint;
51
        m_line = line;
52
        setStartPoint(QPointF(m_line->xPos(), m_line->yPos()));
53
        setEndPoint(QPointF(m_line->xPos() + m_line->width(), m_line->yPos()));
54
        setRotation(m_line->rotation());
55
        if (m_useOriginAsEndpoint)
56
        {
57
                QPointF tmp = startPoint();
58
                setStartPoint(endPoint());
59
                setEndPoint(tmp);
60
        }
61
}
62
 
63
 
64
double LineMove::rotation() const
65
{
66
        double rot = xy2Deg(m_bounds.width(), m_bounds.height());
67
        if (rot < 0.0)
68
                return 360 + rot;
69
        else
70
                return rot;
71
}
72
 
73
 
74
void LineMove::setRotation(double rot)
75
{
76
        m_bounds.setSize(length() * QSizeF(cosd(rot), sind(rot)));
77
}
78
 
79
 
80
double LineMove::length() const
81
{
82
        return qMax(0.01, distance(m_bounds.width(), m_bounds.height()));
83
}
84
 
85
 
86
void LineMove::setStartPoint(QPointF p)
87
{
88
        m_bounds.setTopLeft(p);
89
}
90
 
91
 
92
void LineMove::setEndPoint(QPointF p)
93
{
94
        m_bounds.setBottomRight(p);
95
}
96
 
97
 
98
void LineMove::activate(bool flag)
99
{
100
        qDebug() << "LineMove::activate" << flag << m_bounds;  
101
}
102
 
103
 
104
 
105
void LineMove::deactivate(bool flag)
106
{
107
        qDebug() << "LineMove::deactivate" << flag;
108
        m_haveLineItem = false;
109
}
110
 
111
 
112
 
113
void LineMove::drawControls(QPainter* p)
114
{
115
        p->save();
116
        p->scale(m_canvas->scale(), m_canvas->scale());
117
        p->translate(-m_doc->minCanvasCoordinate.x(), -m_doc->minCanvasCoordinate.y());
118
        p->setBrush(Qt::NoBrush);
119
        p->setPen(QPen(Qt::black, 1.0 / m_canvas->scale(), Qt::DotLine, Qt::FlatCap, Qt::MiterJoin));
120
        p->drawLine(m_bounds.topLeft(), m_bounds.bottomRight());
121
        p->restore();
122
}
123
 
124
 
125
 
126
void LineMove::mouseReleaseEvent(QMouseEvent *m)
127
{
128
        adjustBounds(m);
129
        if (m_haveLineItem)
130
        {
131
                doResize();
132
                m_doc->setRedrawBounding(m_line);
133
                m_line->update();
134
        }
135
        m->accept();
136
        m_canvas->update();
137
        qDebug() << "LineMove::mouseRelease" << m_line->xPos() << "," << m_line->yPos() << "@" << m_line->rotation() << m_line->width() << "x" << m_line->height();
138
        m_view->stopGesture();
139
}
140
 
141
 
142
void LineMove::doResize()
143
{
144
        if (m_useOriginAsEndpoint)
145
        {
146
                m_line->setXYPos(m_bounds.right(), m_bounds.bottom());
147
                double rot = rotation();
148
                m_line->setRotation(rot < 180? rot + 180 : rot - 180);
149
        }
150
        else
151
        {
152
                m_line->setXYPos(m_bounds.x(), m_bounds.y());
153
                m_line->setRotation(rotation());
154
        }
155
        m_line->setWidth(length());
156
        m_line->setHeight(1.0);
157
        m_line->updateClip();
158
//      qDebug() << "LineMove::doresize" << m_line->xPos() << "," << m_line->yPos() << "@" << m_line->rotation() << m_line->width() << "x" << m_line->height();
159
}
160
 
161
 
162
void LineMove::mouseMoveEvent(QMouseEvent *m)
163
{
164
        adjustBounds(m);
165
        if (m_haveLineItem)
166
        {
167
                doResize();
168
        }
169
        m->accept();
170
        m_canvas->repaint();
171
}
172
 
173
 
174
 
175
void LineMove::adjustBounds(QMouseEvent *m)
176
{
177
        FPoint mousePointDoc = m_canvas->globalToCanvas(m->globalPos());
178
        bool constrainRatio = ((m->modifiers() & Qt::ControlModifier) != Qt::NoModifier);
179
 
180
        double newX = mousePointDoc.x();
181
        double newY = mousePointDoc.y();
182
 
183
        if (m_doc->useRaster)
184
        {
185
                newX = qRound(newX / m_doc->guidesSettings.minorGrid) * m_doc->guidesSettings.minorGrid;
186
                newY = qRound(newY / m_doc->guidesSettings.minorGrid) * m_doc->guidesSettings.minorGrid;
187
        }
188
        m_bounds.setBottomRight(QPointF(newX, newY));
189
//      qDebug() << "LineMove::adjustBounds" << m_bounds << rotation() << length() << m_bounds.bottomRight();
190
 
191
        //Constrain rotation angle, when the mouse is being dragged around for a new line
192
        if (constrainRatio)
193
        {
194
                double newRot = rotation();
195
                newRot = constrainAngle(newRot, m_doc->toolSettings.constrain);
196
                setRotation(newRot);
197
        }
198
        m_view->updateCanvas(m_bounds.normalized().adjusted(-10, -10, 20, 20));
199
}
200
 
201
 
202
 
203
void LineMove::mousePressEvent(QMouseEvent *m)
204
{
205
        PageItem_Line* line = m_doc->m_Selection->count() == 1 ? m_doc->m_Selection->itemAt(0)->asLine() : NULL;
206
        if (line)
207
        {
208
                bool hitsOrigin = m_canvas->hitsCanvasPoint(m->globalPos(), line->xyPos());
209
                prepare(line, hitsOrigin);
210
                // now we also know the line's endpoint:
211
                bool hitsEnd = m_canvas->hitsCanvasPoint(m->globalPos(), endPoint());
212
                m_haveLineItem = hitsOrigin || hitsEnd;
213
        }
214
        else
215
        {
216
                FPoint point = m_canvas->globalToCanvas(m->globalPos());
11388 avox 217
                setStartPoint(QPointF(point.x(), point.y()));
218
                setEndPoint(QPointF(point.x(), point.y()));
11338 avox 219
                m_haveLineItem = false;
220
        }
221
        if (m_haveLineItem)
222
                m->accept();
223
}