Subversion Repositories Scribus

Rev

Rev 11645 | Rev 13600 | 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
 
11645 fschmid 18
//#include <QDebug>
11338 avox 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
{
11645 fschmid 100
//      qDebug() << "LineMove::activate" << flag << m_bounds;   
11338 avox 101
}
102
 
103
 
104
 
105
void LineMove::deactivate(bool flag)
106
{
11645 fschmid 107
//      qDebug() << "LineMove::deactivate" << flag;
11338 avox 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);
11576 avox 133
                m_view->resetMousePressed();
134
                m_line->checkChanges();
11338 avox 135
                m_line->update();
136
        }
137
        m->accept();
138
        m_canvas->update();
11645 fschmid 139
//      qDebug() << "LineMove::mouseRelease" << m_line->xPos() << "," << m_line->yPos() << "@" << m_line->rotation() << m_line->width() << "x" << m_line->height();
11338 avox 140
        m_view->stopGesture();
141
}
142
 
143
 
144
void LineMove::doResize()
145
{
146
        if (m_useOriginAsEndpoint)
147
        {
148
                m_line->setXYPos(m_bounds.right(), m_bounds.bottom());
149
                double rot = rotation();
150
                m_line->setRotation(rot < 180? rot + 180 : rot - 180);
151
        }
152
        else
153
        {
154
                m_line->setXYPos(m_bounds.x(), m_bounds.y());
155
                m_line->setRotation(rotation());
156
        }
157
        m_line->setWidth(length());
158
        m_line->setHeight(1.0);
159
        m_line->updateClip();
160
//      qDebug() << "LineMove::doresize" << m_line->xPos() << "," << m_line->yPos() << "@" << m_line->rotation() << m_line->width() << "x" << m_line->height();
161
}
162
 
163
 
164
void LineMove::mouseMoveEvent(QMouseEvent *m)
165
{
166
        adjustBounds(m);
167
        if (m_haveLineItem)
168
        {
169
                doResize();
12656 fschmid 170
                double angle = rotation();
171
                if (angle > 0)
172
                        angle = 360 - angle;
173
                m_canvas->displaySizeHUD(m->globalPos(), length(), fabs(angle), true);
11338 avox 174
        }
175
        m->accept();
176
        m_canvas->repaint();
177
}
178
 
179
 
180
 
181
void LineMove::adjustBounds(QMouseEvent *m)
182
{
183
        FPoint mousePointDoc = m_canvas->globalToCanvas(m->globalPos());
184
        bool constrainRatio = ((m->modifiers() & Qt::ControlModifier) != Qt::NoModifier);
185
 
186
        double newX = mousePointDoc.x();
187
        double newY = mousePointDoc.y();
188
 
189
        if (m_doc->useRaster)
190
        {
191
                newX = qRound(newX / m_doc->guidesSettings.minorGrid) * m_doc->guidesSettings.minorGrid;
192
                newY = qRound(newY / m_doc->guidesSettings.minorGrid) * m_doc->guidesSettings.minorGrid;
193
        }
194
        m_bounds.setBottomRight(QPointF(newX, newY));
195
//      qDebug() << "LineMove::adjustBounds" << m_bounds << rotation() << length() << m_bounds.bottomRight();
196
 
197
        //Constrain rotation angle, when the mouse is being dragged around for a new line
198
        if (constrainRatio)
199
        {
200
                double newRot = rotation();
201
                newRot = constrainAngle(newRot, m_doc->toolSettings.constrain);
202
                setRotation(newRot);
203
        }
204
        m_view->updateCanvas(m_bounds.normalized().adjusted(-10, -10, 20, 20));
205
}
206
 
207
 
208
 
209
void LineMove::mousePressEvent(QMouseEvent *m)
210
{
211
        PageItem_Line* line = m_doc->m_Selection->count() == 1 ? m_doc->m_Selection->itemAt(0)->asLine() : NULL;
212
        if (line)
213
        {
214
                bool hitsOrigin = m_canvas->hitsCanvasPoint(m->globalPos(), line->xyPos());
215
                prepare(line, hitsOrigin);
216
                // now we also know the line's endpoint:
217
                bool hitsEnd = m_canvas->hitsCanvasPoint(m->globalPos(), endPoint());
218
                m_haveLineItem = hitsOrigin || hitsEnd;
219
        }
220
        else
221
        {
222
                FPoint point = m_canvas->globalToCanvas(m->globalPos());
11388 avox 223
                setStartPoint(QPointF(point.x(), point.y()));
224
                setEndPoint(QPointF(point.x(), point.y()));
11338 avox 225
                m_haveLineItem = false;
226
        }
227
        if (m_haveLineItem)
228
                m->accept();
11556 fschmid 229
}