• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

marble

  • sources
  • kde-4.12
  • kdeedu
  • marble
  • src
  • lib
  • marble
kineticmodel.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the Ofi Labs X2 project.
3 
4  Copyright (C) 2010 Ariya Hidayat <ariya.hidayat@gmail.com>
5 
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions are met:
8 
9  * Redistributions of source code must retain the above copyright
10  notice, this list of conditions and the following disclaimer.
11  * Redistributions in binary form must reproduce the above copyright
12  notice, this list of conditions and the following disclaimer in the
13  documentation and/or other materials provided with the distribution.
14  * Neither the name of the <organization> nor the
15  names of its contributors may be used to endorse or promote products
16  derived from this software without specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
22  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29 
30 #include "kineticmodel.h"
31 
32 #include <QTimer>
33 #include <QDateTime>
34 
35 static const int KineticModelDefaultUpdateInterval = 15; // ms
36 
37 class KineticModelPrivate
38 {
39 public:
40  QTimer ticker;
41 
42  int duration;
43  QPointF position;
44  QPointF velocity;
45  QPointF deacceleration;
46 
47  QTime timestamp;
48  QPointF lastPosition;
49 
50  KineticModelPrivate();
51 };
52 
53 KineticModelPrivate::KineticModelPrivate()
54  : duration(1403)
55  , position(0, 0)
56  , velocity(0, 0)
57  , deacceleration(0, 0)
58  , lastPosition(0, 0)
59 {
60 
61 }
62 
63 KineticModel::KineticModel(QObject *parent)
64  : QObject(parent)
65  , d_ptr(new KineticModelPrivate)
66 {
67  connect(&d_ptr->ticker, SIGNAL(timeout()), SLOT(update()));
68  d_ptr->ticker.setInterval(KineticModelDefaultUpdateInterval);
69 }
70 
71 KineticModel::~KineticModel()
72 {
73 
74 }
75 
76 int KineticModel::duration() const
77 {
78  return d_ptr->duration;
79 }
80 
81 void KineticModel::setDuration(int ms)
82 {
83  d_ptr->duration = ms;
84 }
85 
86 QPointF KineticModel::position() const
87 {
88  return d_ptr->position;
89 }
90 
91 void KineticModel::setPosition(QPointF position)
92 {
93  setPosition( position.x(), position.y() );
94 }
95 
96 void KineticModel::setPosition(qreal posX, qreal posY)
97 {
98  d_ptr->position.setX( posX );
99  d_ptr->position.setY( posY );
100 
101  int elapsed = d_ptr->timestamp.elapsed();
102 
103  // too fast gives less accuracy
104  if (elapsed < d_ptr->ticker.interval() / 2) {
105  return;
106  }
107 
108  qreal delta = static_cast<qreal>( elapsed ) / 1000.0;
109 
110  QPointF lastSpeed = d_ptr->velocity;
111  QPointF currentSpeed = ( d_ptr->position - d_ptr->lastPosition ) / delta;
112  d_ptr->velocity = 0.2 * lastSpeed + 0.8 * currentSpeed;
113  d_ptr->lastPosition = d_ptr->position;
114 
115  d_ptr->timestamp.start();
116 }
117 
118 void KineticModel::jumpToPosition(QPointF position)
119 {
120  jumpToPosition( position.x(), position.y() );
121 }
122 
123 void KineticModel::jumpToPosition(qreal posX, qreal posY)
124 {
125  d_ptr->position.setX( posX );
126  d_ptr->position.setY( posY );
127 }
128 
129 int KineticModel::updateInterval() const
130 {
131  return d_ptr->ticker.interval();
132 }
133 
134 void KineticModel::setUpdateInterval(int ms)
135 {
136  d_ptr->ticker.setInterval(ms);
137 }
138 
139 void KineticModel::stop()
140 {
141  Q_D(KineticModel);
142 
143  d->ticker.stop();
144  d->timestamp.start();
145  d->velocity = QPointF(0, 0);
146 }
147 
148 void KineticModel::start()
149 {
150  Q_D(KineticModel);
151 
152  // prevent kinetic spinning if last mouse move is too long ago
153  const int elapsed = d->timestamp.elapsed();
154  if ( elapsed > 2 * d->ticker.interval() ) {
155  d->ticker.stop();
156  emit finished();
157  return;
158  }
159 
160  d->deacceleration = d->velocity * 1000 / ( 1 + d_ptr->duration );
161  if (d->deacceleration.x() < 0) {
162  d->deacceleration.setX( -d->deacceleration.x() );
163  }
164  if (d->deacceleration.y() < 0) {
165  d->deacceleration.setY( -d->deacceleration.y() );
166  }
167 
168  if (!d->ticker.isActive())
169  d->ticker.start();
170 }
171 
172 void KineticModel::update()
173 {
174  Q_D(KineticModel);
175 
176  int elapsed = qMin( d->timestamp.elapsed(), 100 ); // limit to 100msec to reduce catapult effect (bug 294608)
177  qreal delta = static_cast<qreal>(elapsed) / 1000.0;
178 
179  d->position += d->velocity * delta;
180  QPointF vstep = d->deacceleration * delta;
181 
182  if (d->velocity.x() < vstep.x() && d->velocity.x() >= -vstep.x()) {
183  d->velocity.setX( 0 );
184  } else {
185  if (d->velocity.x() > 0)
186  d->velocity.setX( d->velocity.x() - vstep.x() );
187  else
188  d->velocity.setX( d->velocity.x() + vstep.x() );
189  }
190 
191  if (d->velocity.y() < vstep.y() && d->velocity.y() >= -vstep.y()) {
192  d->velocity.setY( 0 );
193  } else {
194  if (d->velocity.y() > 0)
195  d->velocity.setY( d->velocity.y() - vstep.y() );
196  else
197  d->velocity.setY( d->velocity.y() + vstep.y() );
198  }
199 
200  emit positionChanged( d->position.x(), d->position.y() );
201 
202  if (d->velocity.isNull()) {
203  emit finished();
204  d->ticker.stop();
205  }
206 
207  d->timestamp.start();
208 }
209 
210 #include "kineticmodel.moc"
211 
KineticModel::positionChanged
void positionChanged(qreal lon, qreal lat)
KineticModel::KineticModel
KineticModel(QObject *parent=0)
Definition: kineticmodel.cpp:63
KineticModel::setUpdateInterval
void setUpdateInterval(int ms)
Definition: kineticmodel.cpp:134
KineticModel::updateInterval
int updateInterval() const
KineticModel::setDuration
void setDuration(int ms)
Definition: kineticmodel.cpp:81
KineticModel
Definition: kineticmodel.h:39
QObject
KineticModelDefaultUpdateInterval
static const int KineticModelDefaultUpdateInterval
Definition: kineticmodel.cpp:35
KineticModel::setPosition
void setPosition(QPointF position)
Definition: kineticmodel.cpp:91
KineticModel::jumpToPosition
void jumpToPosition(QPointF position)
Definition: kineticmodel.cpp:118
KineticModel::stop
void stop()
Definition: kineticmodel.cpp:139
KineticModel::duration
int duration() const
KineticModel::position
QPointF position() const
kineticmodel.h
KineticModel::~KineticModel
~KineticModel()
Definition: kineticmodel.cpp:71
KineticModel::start
void start()
Definition: kineticmodel.cpp:148
KineticModel::finished
void finished()
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:38:50 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

marble

Skip menu "marble"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal