summaryrefslogtreecommitdiffstats
path: root/karbon/tools/vshapetool.cc
blob: 4f46c317c09d7ee2213a7b3b0cdc5aa0185b071e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/* This file is part of the KDE project
   Copyright (C) 2001, 2002, 2003 The Karbon Developers

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include <tqcursor.h>
#include <tqevent.h>
#include <tqlabel.h>

#include "karbon_part.h"
#include "karbon_view.h"
#include "vcanvas.h"
#include "vcomposite.h"
#include "vglobal.h"
#include "vpainter.h"
#include "vpainterfactory.h"
#include "vtqshapecmd.h"
#include "vtqshapetool.h"
#include "vselection.h"
#include "vcursor.h"

VShapeTool::VShapeTool( KarbonView *view, const char *name, bool polar )
	: VTool( view, name )
{
	m_cursor = new TQCursor( VCursor::createCursor( VCursor::CrossHair ) );

	m_isPolar = polar;
	m_isSquare   = false;
	m_isCentered = false;
}

VShapeTool::~VShapeTool()
{
	delete m_cursor;
}

TQString
VShapeTool::contextHelp()
{
	TQString s = i18n( "<qt><b>Shape tool</b><br>" );
	s += i18n( "<i>Click and drag</i> to place your own tqshape.<br>" );
	s += i18n( "<i>Click</i> to place a tqshape using the tool properties values.</qt>" );
	return s;
} 

void
VShapeTool::activate()
{
	VTool::activate();
	view()->setCursor( *m_cursor );
	view()->part()->document().selection()->showHandle( true );
}

TQString
VShapeTool::statusText()
{
	return uiname();
}

void
VShapeTool::draw()
{
	VPainter* painter = view()->painterFactory()->editpainter();
	painter->setRasterOp( TQt::NotROP );

	VPath* composite = tqshape();
	composite->setState( VPath::edit );
	composite->draw( painter, &composite->boundingBox() );
	delete( composite );
}

void
VShapeTool::mouseButtonPress()
{
	recalc();

	// Draw new object:
	draw();
}

void
VShapeTool::mouseButtonRelease()
{
	draw();

	recalc();

	if( showDialog() )
	{
		VPath* composite = tqshape( true );

		if( composite )
		{
			VShapeCmd* cmd = new VShapeCmd(
				&view()->part()->document(),
				uiname(), composite, icon() );

			view()->part()->addCommand( cmd, true );
		}
	}

	m_isSquare = false;
	m_isCentered = false;
}

void
VShapeTool::mouseDrag()
{
	// Erase old object:
	draw();

	recalc();

	// Draw new object:
	draw();
}

void
VShapeTool::mouseDragRelease()
{
	//recalc();

	VShapeCmd* cmd = new VShapeCmd(
		&view()->part()->document(),
		uiname(), tqshape(), icon() );

	view()->part()->addCommand( cmd, true );

	m_isSquare = false;
	m_isCentered = false;
}

void
VShapeTool::mouseDragShiftPressed()
{
	// Erase old object:
	draw();

	m_isSquare = true;
	recalc();

	// Draw new object:
	draw();
}

void
VShapeTool::mouseDragCtrlPressed()
{
	// Erase old object:
	draw();

	m_isCentered = true;
	recalc();

	// Draw new object:
	draw();
}

void
VShapeTool::mouseDragShiftReleased()
{
	// Erase old object:
	draw();

	m_isSquare = false;
	recalc();

	// Draw new object:
	draw();
}

void
VShapeTool::mouseDragCtrlReleased()
{
	// Erase old object:
	draw();

	m_isCentered = false;
	recalc();

	// Draw new object:
	draw();
}

void
VShapeTool::cancel()
{
	// Erase old object:
	if ( isDragging() )
	{
		draw();
		m_isSquare = false;
		m_isCentered = false;
	}
}

void
VShapeTool::recalc()
{
	m_isSquare = shiftPressed();
	m_isCentered = ctrlPressed();

	KoPoint _first = view()->canvasWidget()->snapToGrid( first() );
	KoPoint _last = view()->canvasWidget()->snapToGrid( last() );

	// Calculate radius and angle:
	if( m_isPolar )
	{
		// Radius:
		m_d1 = sqrt(
			( _last.x() - _first.x() ) * ( _last.x() - _first.x() ) +
			( _last.y() - _first.y() ) * ( _last.y() - _first.y() ) );

		// Angle:
		m_d2 = atan2( _last.y() - _first.y(), _last.x() - _first.x() );

		// Define pi/2 as "0.0":
		m_d2 -= VGlobal::pi_2;

		m_p = _first;
	}
	else
	// Calculate width and height:
	{
		m_d1 = _last.x() - _first.x();
		m_d2 = _last.y() - _first.y();

		const int m_sign1 = VGlobal::sign( m_d1 );
// TODO: revert when we introduce y-mirroring:
		const int m_sign2 = VGlobal::sign( -m_d2 );

		// Make unsigned:
		if( m_d1 < 0.0 )
			m_d1 = -m_d1;

		if( m_d2 < 0.0 )
			m_d2 = -m_d2;

		if ( m_isSquare )
		{
			if ( m_d1 > m_d2 )
				m_d2 = m_d1;
			else
				m_d1 = m_d2;
		}

		m_p.setX(
			_first.x() - ( m_sign1 == -1 ? m_d1 : 0.0 ) );
// TODO: revert when we introduce y-mirroring:
		m_p.setY(
			_first.y() + ( m_sign2 == -1 ? m_d2 : 0.0 ) );

		if ( m_isCentered )
		{
			m_p.setX( m_p.x() - m_sign1 * tqRound( m_d1 * 0.5 ) );
			m_p.setY( m_p.y() + m_sign2 * tqRound( m_d2 * 0.5 ) );
		}
	}
}