landscape.cpp 17.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
    Copyright (C) 2002-2005, Jason Katz-Brown <jasonkb@mit.edu>
    Copyright 2010 Stefan Majewsky <majewsky@gmx.net>

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

    This program 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 General Public License for more details.

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

#include "landscape.h"
#include "ball.h"
#include "game.h"

Laurent Montel's avatar
Laurent Montel committed
24
#include <QDoubleSpinBox>
25
26
27
28
#include <QBoxLayout>
#include <QCheckBox>
#include <QLabel>
#include <QSlider>
29
#include <KComboBox>
30
#include <KConfigGroup>
Laurent Montel's avatar
Laurent Montel committed
31
#include <KLocalizedString>
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

//BEGIN Kolf::LandscapeItem
//END Kolf::LandscapeItem

Kolf::LandscapeItem::LandscapeItem(const QString& type, QGraphicsItem* parent, b2World* world)
	: EllipticalCanvasItem(false, type, parent, world)
	, m_blinkEnabled(false)
	, m_blinkInterval(50)
	, m_blinkFrame(0)
{
	setSimulationType(CanvasItem::NoSimulation);
}

bool Kolf::LandscapeItem::isBlinkEnabled() const
{
	return m_blinkEnabled;
}

void Kolf::LandscapeItem::setBlinkEnabled(bool blinkEnabled)
{
	m_blinkEnabled = blinkEnabled;
	//reset animation
	m_blinkFrame = 0;
	setVisible(true);
}

int Kolf::LandscapeItem::blinkInterval() const
{
	return m_blinkInterval;
}

void Kolf::LandscapeItem::setBlinkInterval(int blinkInterval)
{
	m_blinkInterval = blinkInterval;
	//reset animation
	m_blinkFrame = 0;
	setVisible(true);
}

void Kolf::LandscapeItem::advance(int phase)
{
	EllipticalCanvasItem::advance(phase);
	if (phase == 1 && m_blinkEnabled)
	{
		const int actualInterval = 1.8 * (10 + m_blinkInterval);
		m_blinkFrame = (m_blinkFrame + 1) % (2 * actualInterval);
		setVisible(m_blinkFrame < actualInterval);
	}
}

void Kolf::LandscapeItem::load(KConfigGroup* group)
{
	EllipticalCanvasItem::loadSize(group);
	setBlinkEnabled(group->readEntry("changeEnabled", m_blinkEnabled));
	setBlinkInterval(group->readEntry("changeEvery", m_blinkInterval));
}

void Kolf::LandscapeItem::save(KConfigGroup* group)
{
	EllipticalCanvasItem::saveSize(group);
	group->writeEntry("changeEnabled", m_blinkEnabled);
	group->writeEntry("changeEvery", m_blinkInterval);
}

Config* Kolf::LandscapeItem::config(QWidget* parent)
{
	return new Kolf::LandscapeConfig(this, parent);
}

Kolf::Overlay* Kolf::LandscapeItem::createOverlay()
{
	return new Kolf::LandscapeOverlay(this);
}

//BEGIN Kolf::LandscapeOverlay

Kolf::LandscapeOverlay::LandscapeOverlay(Kolf::LandscapeItem* item)
	: Kolf::Overlay(item, item)
{
111
	//TODO: code duplication to Kolf::RectangleOverlay and Kolf::SlopeOverlay
112
113
114
115
116
	for (int i = 0; i < 4; ++i)
	{
		Kolf::OverlayHandle* handle = new Kolf::OverlayHandle(Kolf::OverlayHandle::CircleShape, this);
		m_handles << handle;
		addHandle(handle);
117
		connect(handle, &Kolf::OverlayHandle::moveRequest, this, &Kolf::LandscapeOverlay::moveHandle);
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
	}
}

void Kolf::LandscapeOverlay::update()
{
	Kolf::Overlay::update();
	const QRectF rect = qitem()->boundingRect();
	m_handles[0]->setPos(rect.topLeft());
	m_handles[1]->setPos(rect.topRight());
	m_handles[2]->setPos(rect.bottomLeft());
	m_handles[3]->setPos(rect.bottomRight());
}

void Kolf::LandscapeOverlay::moveHandle(const QPointF& handleScenePos)
{
	const QPointF handlePos = mapFromScene(handleScenePos);
	//factor 2: item bounding rect is always centered around (0,0)
	QSizeF newSize(2 * qAbs(handlePos.x()), 2 * qAbs(handlePos.y()));
	dynamic_cast<Kolf::LandscapeItem*>(qitem())->setSize(newSize);
}

//END Kolf::LandscapeOverlay
//BEGIN Kolf::LandscapeConfig

Kolf::LandscapeConfig::LandscapeConfig(Kolf::LandscapeItem* item, QWidget* parent)
	: Config(parent)
{
	QVBoxLayout* vlayout = new QVBoxLayout(this);
	QCheckBox* checkBox = new QCheckBox(i18n("Enable show/hide"), this);
	vlayout->addWidget(checkBox);

	QHBoxLayout* hlayout = new QHBoxLayout;
	vlayout->addLayout(hlayout);
	QLabel* label1 = new QLabel(i18n("Slow"), this);
	hlayout->addWidget(label1);
	QSlider* slider = new QSlider(Qt::Horizontal, this);
	hlayout->addWidget(slider);
	QLabel* label2 = new QLabel(i18n("Fast"), this);
	hlayout->addWidget(label2);

	vlayout->addStretch();

	checkBox->setChecked(true);
161
162
163
164
	connect(checkBox, &QCheckBox::toggled, label1, &QLabel::setEnabled);
	connect(checkBox, &QCheckBox::toggled, label2, &QLabel::setEnabled);
	connect(checkBox, &QCheckBox::toggled, slider, &QSlider::setEnabled);
	connect(checkBox, &QCheckBox::toggled, item, &Kolf::LandscapeItem::setBlinkEnabled);
165
166
167
168
	checkBox->setChecked(item->isBlinkEnabled());
	slider->setRange(1, 100);
	slider->setPageStep(5);
	slider->setValue(100 - item->blinkInterval());
169
170
	connect(slider, &QSlider::valueChanged, this, &Kolf::LandscapeConfig::setBlinkInterval);
	connect(this, &Kolf::LandscapeConfig::blinkIntervalChanged, item, &Kolf::LandscapeItem::setBlinkInterval);
171
172
173
174
175
176
177
178
179
180
181
}

void Kolf::LandscapeConfig::setBlinkInterval(int sliderValue)
{
	emit blinkIntervalChanged(100 - sliderValue);
}

//END Kolf::LandscapeConfig
//BEGIN Kolf::Puddle

Kolf::Puddle::Puddle(QGraphicsItem* parent, b2World* world)
Laurent Montel's avatar
Laurent Montel committed
182
	: Kolf::LandscapeItem(QStringLiteral("puddle"), parent, world)
183
184
185
{
	setData(0, Rtti_DontPlaceOn);
	setSize(QSizeF(45, 30));
Stefan Majewsky's avatar
Stefan Majewsky committed
186
	setZBehavior(CanvasItem::FixedZValue, 3);
187
188
189
190
191
192
193
194
195
}

bool Kolf::Puddle::collision(Ball* ball)
{
	if (!ball->isVisible())
		return false;
	if (!contains(ball->pos() - pos()))
		return true;
	//ball is visible and has reached the puddle
Yuri Chornoivan's avatar
Yuri Chornoivan committed
196
	game->playSound(Sound::Puddle);
197
198
199
200
201
202
203
204
205
206
207
208
209
210
	ball->setAddStroke(ball->addStroke() + 1);
	ball->setPlaceOnGround(true);
	ball->setVisible(false);
	ball->setState(Stopped);
	ball->setVelocity(Vector());
	if (game && game->curBall() == ball)
		game->stoppedBall();
	return false;
}

//END Kolf::Puddle
//BEGIN Kolf::Sand

Kolf::Sand::Sand(QGraphicsItem* parent, b2World* world)
Laurent Montel's avatar
Laurent Montel committed
211
	: Kolf::LandscapeItem(QStringLiteral("sand"), parent, world)
212
213
{
	setSize(QSizeF(45, 40));
Stefan Majewsky's avatar
Stefan Majewsky committed
214
	setZBehavior(CanvasItem::FixedZValue, 2);
215
216
217
218
219
220
221
222
223
224
}

bool Kolf::Sand::collision(Ball* ball)
{
	if (contains(ball->pos() - pos()))
		ball->setFrictionMultiplier(7);
	return true;
}

//END Kolf::Sand
225
226
227
228
229
230
231
232
//BEGIN Kolf::Slope

struct SlopeData
{
	QStringList gradientKeys, translatedGradientKeys;
	QStringList spriteKeys, reversedSpriteKeys;
	SlopeData()
	{
Laurent Montel's avatar
Laurent Montel committed
233
234
235
236
237
		gradientKeys << QStringLiteral("Vertical")
		             << QStringLiteral("Horizontal")
		             << QStringLiteral("Diagonal")
		             << QStringLiteral("Opposite Diagonal")
		             << QStringLiteral("Elliptic");
238
239
240
241
242
		translatedGradientKeys << i18n("Vertical")
		             << i18n("Horizontal")
		             << i18n("Diagonal")
		             << i18n("Opposite Diagonal")
		             << i18n("Elliptic");
Laurent Montel's avatar
Laurent Montel committed
243
244
245
246
247
248
249
250
251
252
		spriteKeys   << QStringLiteral("slope_n")
			         << QStringLiteral("slope_w")
			         << QStringLiteral("slope_nw")
			         << QStringLiteral("slope_ne")
			         << QStringLiteral("slope_bump");
		reversedSpriteKeys << QStringLiteral("slope_s")
			         << QStringLiteral("slope_e")
			         << QStringLiteral("slope_se")
			         << QStringLiteral("slope_sw")
			         << QStringLiteral("slope_dip");
253
254
	}
};
Laurent Montel's avatar
Laurent Montel committed
255
Q_GLOBAL_STATIC(SlopeData, g_slopeData)
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276

Kolf::Slope::Slope(QGraphicsItem* parent, b2World* world)
	: Tagaro::SpriteObjectItem(Kolf::renderer(), QString(), parent)
	, CanvasItem(world)
	, m_grade(4)
	, m_reversed(false)
	, m_stuckOnGround(false)
	, m_type(Kolf::VerticalSlope)
	, m_gradeItem(new QGraphicsSimpleTextItem(this))
{
	m_gradeItem->setBrush(Qt::white);
	m_gradeItem->setVisible(false);
	m_gradeItem->setZValue(1);
	for (int i = 0; i < 4; ++i)
	{
		ArrowItem* arrow = new ArrowItem(this);
		arrow->setLength(0);
		arrow->setVisible(false);
		m_arrows << arrow;
	}
	setSize(QSizeF(40, 40));
Stefan Majewsky's avatar
Stefan Majewsky committed
277
278
	m_stuckOnGround = true; //so that the following call does not return early
	setStuckOnGround(false); //initializes Z behavior
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
	updateAppearance();
}

double Kolf::Slope::grade() const
{
	return m_grade;
}

void Kolf::Slope::setGrade(double grade)
{
	if (m_grade != grade && grade > 0)
	{
		m_grade = grade;
		updateAppearance();
		propagateUpdate();
	}
}

bool Kolf::Slope::isReversed() const
{
	return m_reversed;
}

void Kolf::Slope::setReversed(bool reversed)
{
	if (m_reversed != reversed)
	{
		m_reversed = reversed;
		updateAppearance();
		propagateUpdate();
	}
}

Kolf::SlopeType Kolf::Slope::slopeType() const
{
	return m_type;
}

void Kolf::Slope::setSlopeType(int type)
{
	if (m_type != type && type >= 0)
	{
		m_type = (Kolf::SlopeType) type;
		updateAppearance();
		propagateUpdate();
	}
}

bool Kolf::Slope::isStuckOnGround() const
{
	return m_stuckOnGround;
}

void Kolf::Slope::setStuckOnGround(bool stuckOnGround)
{
	if (m_stuckOnGround != stuckOnGround)
	{
		m_stuckOnGround = stuckOnGround;
Stefan Majewsky's avatar
Stefan Majewsky committed
337
		setZBehavior(m_stuckOnGround ? CanvasItem::FixedZValue : CanvasItem::IsRaisedByStrut, 1);
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
		propagateUpdate();
	}
}

QPainterPath Kolf::Slope::shape() const
{
	const QRectF rect = boundingRect();
	QPainterPath path;
	if (m_type == Kolf::CrossDiagonalSlope) {
		QPolygonF polygon(3);
		polygon[0] = rect.topLeft();
		polygon[1] = rect.bottomRight();
		polygon[2] = m_reversed ? rect.topRight() : rect.bottomLeft();
		path.addPolygon(polygon);
	} else if (m_type == Kolf::DiagonalSlope) {
		QPolygonF polygon(3);
		polygon[0] = rect.topRight();
		polygon[1] = rect.bottomLeft();
		polygon[2] = m_reversed ? rect.topLeft() : rect.bottomRight();
		path.addPolygon(polygon);
	} else if (m_type == Kolf::EllipticSlope) {
		path.addEllipse(rect);
	} else {
		path.addRect(rect);
	}
	return path;
}

void Kolf::Slope::setSize(const QSizeF& size)
{
	if (m_type == Kolf::EllipticSlope)
	{
		const double extent = qMin(size.width(), size.height());
		Tagaro::SpriteObjectItem::setSize(extent, extent);
	}
	else
		Tagaro::SpriteObjectItem::setSize(size);
	updateInfo();
	propagateUpdate();
Stefan Majewsky's avatar
Stefan Majewsky committed
377
	updateZ(this);
378
379
380
381
382
383
384
385
386
387
}

QPointF Kolf::Slope::getPosition() const
{
	return Tagaro::SpriteObjectItem::pos();
}

void Kolf::Slope::moveBy(double dx, double dy)
{
	Tagaro::SpriteObjectItem::moveBy(dx, dy);
Stefan Majewsky's avatar
Stefan Majewsky committed
388
	CanvasItem::moveBy(dx, dy);
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
}

void Kolf::Slope::load(KConfigGroup* group)
{
	setGrade(group->readEntry("grade", m_grade));
	setReversed(group->readEntry("reversed", m_reversed));
	setStuckOnGround(group->readEntry("stuckOnGround", m_stuckOnGround));
	//gradient is a bit more complicated
	const QString type = group->readEntry("gradient", g_slopeData->gradientKeys.value(m_type));
	setSlopeType(g_slopeData->gradientKeys.indexOf(type));
	//read size
	QSizeF size = Tagaro::SpriteObjectItem::size();
	size.setWidth(group->readEntry("width", size.width()));
	size.setHeight(group->readEntry("height", size.height()));
	setSize(size);
}

void Kolf::Slope::save(KConfigGroup* group)
{
	group->writeEntry("grade", m_grade);
	group->writeEntry("reversed", m_reversed);
	group->writeEntry("stuckOnGround", m_stuckOnGround);
	group->writeEntry("gradient", g_slopeData->gradientKeys.value(m_type));
	const QSizeF size = Tagaro::SpriteObjectItem::size();
	group->writeEntry("width", size.width());
	group->writeEntry("height", size.height());
}

void Kolf::Slope::updateAppearance()
{
	updateInfo();
	//set pixmap
	setSpriteKey((m_reversed ? g_slopeData->reversedSpriteKeys : g_slopeData->spriteKeys).value(m_type));
}

void Kolf::Slope::updateInfo()
{
	m_gradeItem->setText(QString::number(m_grade));
	const QPointF textOffset = m_gradeItem->boundingRect().center();
	//update arrows
	const QSizeF size = Tagaro::SpriteObjectItem::size();
	const double width = size.width(), height = size.height();
	const double length = sqrt(width * width + height * height) / 4;
	if (m_type == Kolf::EllipticSlope)
	{
		double angle = 0;
		for (int i = 0; i < 4; ++i, angle += M_PI / 2)
		{
			ArrowItem* arrow = m_arrows[i];
			arrow->setLength(length);
			arrow->setAngle(angle);
			arrow->setReversed(m_reversed);
			arrow->setPos(QPointF(width / 2, height / 2));
		}
		m_gradeItem->setPos(QPointF(width / 2, height / 2) - textOffset);
	}
	else
	{
		double angle = 0;
		double x = .5 * width, y = .5 * height;
		switch ((int) m_type)
		{
			case Kolf::HorizontalSlope:
				angle = 0;
				break;
			case Kolf::VerticalSlope:
				angle = M_PI / 2;
				break;
			case Kolf::DiagonalSlope:
				angle = atan(width / height);
				x = m_reversed ? .25 * width : .75 * width;
				y = m_reversed ? .25 * height : .75 * height;
				break;
			case Kolf::CrossDiagonalSlope:
				angle = M_PI - atan(width / height);
				x = m_reversed ? .75 * width : .25 * width;
				y = m_reversed ? .25 * height : .75 * height;
				break;
		}
		//only one arrow needed - hide all others
		for (int i = 1; i < 4; ++i)
			m_arrows[i]->setLength(0);
		ArrowItem* arrow = m_arrows[0];
		arrow->setLength(length);
		arrow->setAngle(m_reversed ? angle : angle + M_PI);
		arrow->setPos(QPointF(x, y));
		m_gradeItem->setPos(QPointF(x, y) - textOffset);
	}
}

bool Kolf::Slope::collision(Ball* ball)
{
	Vector v = ball->velocity();
	double addto = 0.013 * m_grade;

	const bool diag = m_type == Kolf::DiagonalSlope || m_type == Kolf::CrossDiagonalSlope;
	const bool circle = m_type == Kolf::EllipticSlope;

	double slopeAngle = 0;
	const double width = size().width(), height = size().height();

Stefan Majewsky's avatar
Stefan Majewsky committed
490
	if (diag)
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
		slopeAngle = atan(width / height);
	else if (circle)
	{
		const QPointF start = pos() + QPointF(width, height) / 2.0;
		const QPointF end = ball->pos();

		Vector betweenVector = start - end;
		const double factor = betweenVector.magnitude() / (width / 2.0);
		slopeAngle = betweenVector.direction();

		// this little bit by Daniel
		addto *= factor * M_PI / 2;
		addto = sin(addto);
	}

	if (!m_reversed)
		addto = -addto;
	switch ((int) m_type)
	{
		case Kolf::HorizontalSlope:
			v.rx() += addto;
			break;
		case Kolf::VerticalSlope:
			v.ry() += addto;
			break;
		case Kolf::DiagonalSlope:
		case Kolf::EllipticSlope:
			v.rx() += cos(slopeAngle) * addto;
			v.ry() += sin(slopeAngle) * addto;
			break;
		case Kolf::CrossDiagonalSlope:
			v.rx() -= cos(slopeAngle) * addto;
			v.ry() += sin(slopeAngle) * addto;
			break;
	}
	ball->setVelocity(v);
	ball->setState(v.isNull() ? Stopped : Rolling);
	// do NOT do terrain collidingItems
	return false;
}

bool Kolf::Slope::terrainCollisions() const
{
	return true;
}

QList<QGraphicsItem*> Kolf::Slope::infoItems() const
{
	QList<QGraphicsItem*> result;
	foreach (ArrowItem* arrow, m_arrows)
		result << arrow;
	result << m_gradeItem;
	return result;
}

Config* Kolf::Slope::config(QWidget* parent)
{
	return new Kolf::SlopeConfig(this, parent);
}

Kolf::Overlay* Kolf::Slope::createOverlay()
{
	return new Kolf::SlopeOverlay(this);
}

//END Kolf::Slope
//BEGIN Kolf::SlopeConfig

Kolf::SlopeConfig::SlopeConfig(Kolf::Slope* slope, QWidget* parent)
	: Config(parent)
{
	QGridLayout* layout = new QGridLayout(this);

	KComboBox* typeBox = new KComboBox(this);
	typeBox->addItems(g_slopeData->translatedGradientKeys);
	typeBox->setCurrentIndex(slope->slopeType());
567
	connect(typeBox, QOverload<int>::of(&KComboBox::currentIndexChanged), slope, &Kolf::Slope::setSlopeType);
568
569
570
571
	layout->addWidget(typeBox, 0, 0, 1, 2);

	QCheckBox* reversed = new QCheckBox(i18n("Reverse direction"), this);
	reversed->setChecked(slope->isReversed());
572
	connect(reversed, &QCheckBox::toggled, slope, &Kolf::Slope::setReversed);
573
574
575
576
577
	layout->addWidget(reversed, 1, 0);

	QCheckBox* stuck = new QCheckBox(i18n("Unmovable"), this);
	stuck->setChecked(slope->isStuckOnGround());
	stuck->setWhatsThis(i18n("Whether or not this slope can be moved by other objects, like floaters."));
578
	connect(stuck, &QCheckBox::toggled, slope, &Kolf::Slope::setStuckOnGround);
579
580
581
582
	layout->addWidget(stuck, 1, 1);

	layout->addWidget(new QLabel(i18n("Grade:"), this), 2, 0);

Laurent Montel's avatar
Laurent Montel committed
583
584
585
	QDoubleSpinBox* grade = new QDoubleSpinBox(this);
	grade->setRange(0, 8);
	grade->setSingleStep(1);
586
	grade->setValue(slope->grade());
587
	connect(grade, QOverload<double>::of(&QDoubleSpinBox::valueChanged), slope, &Kolf::Slope::setGrade);
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
	layout->addWidget(grade, 2, 1);

	layout->setRowStretch(4, 10);
}

//END Kolf::SlopeConfig
//BEGIN Kolf::SlopeOverlay

Kolf::SlopeOverlay::SlopeOverlay(Kolf::Slope* slope)
	: Kolf::Overlay(slope, slope, true) //true = add shape to outlines
{
	//TODO: code duplication to Kolf::LandscapeOverlay and Kolf::RectangleOverlay
	for (int i = 0; i < 4; ++i)
	{
		Kolf::OverlayHandle* handle = new Kolf::OverlayHandle(Kolf::OverlayHandle::CircleShape, this);
		m_handles << handle;
		addHandle(handle);
605
		connect(handle, &Kolf::OverlayHandle::moveRequest, this, &Kolf::SlopeOverlay::moveHandle);
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
	}
}

void Kolf::SlopeOverlay::update()
{
	Kolf::Overlay::update();
	const QRectF rect = qitem()->boundingRect();
	m_handles[0]->setPos(rect.topLeft());
	m_handles[1]->setPos(rect.topRight());
	m_handles[2]->setPos(rect.bottomLeft());
	m_handles[3]->setPos(rect.bottomRight());
}

void Kolf::SlopeOverlay::moveHandle(const QPointF& handleScenePos)
{
	Kolf::OverlayHandle* handle = qobject_cast<Kolf::OverlayHandle*>(sender());
	const int handleIndex = m_handles.indexOf(handle);
	Kolf::Slope* item = dynamic_cast<Kolf::Slope*>(qitem());
	const QPointF handlePos = mapFromScene(handleScenePos);
	//modify bounding rect using new handlePos
	QRectF rect(QPointF(), item->size());
	if (handleIndex % 2 == 0)
		rect.setLeft(qMin(handlePos.x(), rect.right()));
	else
		rect.setRight(qMax(handlePos.x(), rect.left()));
	if (handleIndex < 2)
		rect.setTop(qMin(handlePos.y(), rect.bottom()));
	else
		rect.setBottom(qMax(handlePos.y(), rect.top()));
	item->moveBy(rect.x(), rect.y());
	item->setSize(rect.size());
}

//END Kolf::SlopeOverlay
640

Laurent Montel's avatar
Laurent Montel committed
641