Merge lp:~zeller-benjamin/ubuntu-ui-toolkit/mathutils_to_cpp into lp:ubuntu-ui-toolkit/staging

Proposed by Benjamin Zeller
Status: Merged
Approved by: Zsombor Egri
Approved revision: 1729
Merged at revision: 1724
Proposed branch: lp:~zeller-benjamin/ubuntu-ui-toolkit/mathutils_to_cpp
Merge into: lp:ubuntu-ui-toolkit/staging
Diff against target: 1138 lines (+390/-432)
20 files modified
components.api (+5/-2)
src/Ubuntu/Components/1.2/DraggingArea.qml (+2/-2)
src/Ubuntu/Components/1.2/Slider.qml (+0/-1)
src/Ubuntu/Components/1.2/TextArea.qml (+1/-2)
src/Ubuntu/Components/1.2/mathUtils.js (+0/-61)
src/Ubuntu/Components/1.2/scrollbarUtils.js (+0/-129)
src/Ubuntu/Components/1.3/Slider.qml (+3/-3)
src/Ubuntu/Components/1.3/mathUtils.js (+0/-61)
src/Ubuntu/Components/ComponentModule.pro (+0/-3)
src/Ubuntu/Components/Themes/Ambiance/1.2/ScrollbarStyle.qml (+88/-11)
src/Ubuntu/Components/Themes/Ambiance/1.3/ScrollbarStyle.qml (+88/-11)
src/Ubuntu/Components/Themes/Ambiance/1.3/scrollbarUtils.js (+0/-139)
src/Ubuntu/Components/Themes/Ambiance/qmldir (+0/-1)
src/Ubuntu/Components/plugin/plugin.cpp (+2/-0)
src/Ubuntu/Components/plugin/plugin.pri (+2/-0)
src/Ubuntu/Components/plugin/ucmathutils.cpp (+81/-0)
src/Ubuntu/Components/plugin/ucmathutils.h (+40/-0)
src/Ubuntu/Components/qmldir (+0/-5)
tests/unit/tst_components/tst_math_utils.qml (+77/-0)
ubuntu-sdk.pro (+1/-1)
To merge this branch: bzr merge lp:~zeller-benjamin/ubuntu-ui-toolkit/mathutils_to_cpp
Reviewer Review Type Date Requested Status
PS Jenkins bot continuous-integration Approve
Zsombor Egri Approve
Review via email: mp+277704@code.launchpad.net

Commit message

Move MathUtils to Cpp

Description of the change

Move MathUtils to Cpp

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Cris Dywan (kalikiana) wrote :

Is it faster than the JS?

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
Zsombor Egri (zsombi) wrote :

Welcome to the wonderful world of the UI Toolkit!!! Nice cleanup!!!

review: Approve
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Zsombor Egri (zsombi) wrote :

One small request: print a warning for clamp() when min > max and test that.

review: Needs Fixing
1727. By Benjamin Zeller

- Add warning when min > max for MathUtils::clamp
- Add test to check if the warning is printed

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Needs Fixing (continuous-integration)
1728. By Benjamin Zeller

Add some more tests

1729. By Benjamin Zeller

merge staging

Revision history for this message
Zsombor Egri (zsombi) wrote :

Code looks good, tests are fine as well, let's hope J loves it as well.

review: Approve
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'components.api'
2--- components.api 2015-11-18 10:35:33 +0000
3+++ components.api 2015-11-20 07:47:28 +0000
4@@ -569,7 +569,11 @@
5 Ubuntu.Components.MainView 1.3: MainViewBase
6 property bool automaticOrientation
7 default property list<QtObject> contentsItem
8-Ubuntu.Components.MathUtils 0.1 1.0 1.3
9+Ubuntu.Components.MathUtils 1.0 0.1: QtObject singleton
10+ function double clamp(double x, double min, double max)
11+ function double lerp(double delta, double from, double to)
12+ function double projectValue(double x, double xmin, double xmax, double ymin, double ymax)
13+ function double clampAndProject(double x, double xmin, double xmax, double ymin, double ymax)
14 Ubuntu.Components.MimeData 1.0 0.1: QtObject
15 property color color
16 property var data
17@@ -928,7 +932,6 @@
18 Ubuntu.Components.Scrollbar 1.3: StyledItem
19 property int align
20 property Flickable flickableItem
21-Ubuntu.Components.ScrollbarUtils 0.1 1.0
22 Ubuntu.Components.Sections 1.3: StyledItem
23 property list<Action> actions
24 property var model
25
26=== modified file 'src/Ubuntu/Components/1.2/DraggingArea.qml'
27--- src/Ubuntu/Components/1.2/DraggingArea.qml 2015-04-30 08:32:44 +0000
28+++ src/Ubuntu/Components/1.2/DraggingArea.qml 2015-11-20 07:47:28 +0000
29@@ -15,7 +15,7 @@
30 */
31
32 import QtQuick 2.4
33-import "mathUtils.js" as MathLocal
34+import Ubuntu.Components 1.2
35
36 MouseArea {
37 id: draggingArea
38@@ -25,7 +25,7 @@
39 property real dragVelocity: 0
40 property real dragValue: (orientation == Qt.Vertical ? (mouseY - __pressedPosition.y)
41 : (mouseX - __pressedPosition.x))
42- property real lateralPosition: orientation == Qt.Horizontal ? MathLocal.clamp(mouseY, 0, height) : MathLocal.clamp(mouseX, 0, width)
43+ property real lateralPosition: orientation == Qt.Horizontal ? MathUtils.clamp(mouseY, 0, height) : MathUtils.clamp(mouseX, 0, width)
44 property point __pressedPosition: Qt.point(0, 0)
45 property var __dragEvents: []
46 property bool clickValidated: true
47
48=== modified file 'src/Ubuntu/Components/1.2/Slider.qml'
49--- src/Ubuntu/Components/1.2/Slider.qml 2015-04-30 08:32:44 +0000
50+++ src/Ubuntu/Components/1.2/Slider.qml 2015-11-20 07:47:28 +0000
51@@ -17,7 +17,6 @@
52 // FIXME(loicm) Add support for keyboard shortcuts (basically left/right).
53
54 import QtQuick 2.4
55-import "mathUtils.js" as MathUtils
56 import Ubuntu.Components 1.2
57
58 /*!
59
60=== modified file 'src/Ubuntu/Components/1.2/TextArea.qml'
61--- src/Ubuntu/Components/1.2/TextArea.qml 2015-04-30 08:32:44 +0000
62+++ src/Ubuntu/Components/1.2/TextArea.qml 2015-11-20 07:47:28 +0000
63@@ -17,7 +17,6 @@
64 import QtQuick 2.4
65 import Ubuntu.Components 1.2 as Ubuntu
66 import Ubuntu.Components.Popups 1.0
67-import "mathUtils.js" as MathUtils
68
69 /*!
70 \qmltype TextArea
71@@ -767,7 +766,7 @@
72 var max = (control.maximumLineCount <= 0) ?
73 control.lineCount :
74 Math.min(control.maximumLineCount, control.lineCount);
75- control.height = linesHeight(MathUtils.clamp(control.lineCount, 1, max));
76+ control.height = linesHeight(Ubuntu.MathUtils.clamp(control.lineCount, 1, max));
77 }
78 }
79 }
80
81=== removed file 'src/Ubuntu/Components/1.2/mathUtils.js'
82--- src/Ubuntu/Components/1.2/mathUtils.js 2015-07-17 14:42:26 +0000
83+++ src/Ubuntu/Components/1.2/mathUtils.js 1970-01-01 00:00:00 +0000
84@@ -1,61 +0,0 @@
85-/*
86- * Copyright 2012 Canonical Ltd.
87- *
88- * This program is free software; you can redistribute it and/or modify
89- * it under the terms of the GNU Lesser General Public License as published by
90- * the Free Software Foundation; version 3.
91- *
92- * This program is distributed in the hope that it will be useful,
93- * but WITHOUT ANY WARRANTY; without even the implied warranty of
94- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
95- * GNU Lesser General Public License for more details.
96- *
97- * You should have received a copy of the GNU Lesser General Public License
98- * along with this program. If not, see <http://www.gnu.org/licenses/>.
99- */
100-
101-/*!
102- \qmltype mathUtils
103- \inqmlmodule Ubuntu.Components 1.2
104- \ingroup ubuntu
105- \brief Various mathematical utility functions.
106- */
107-
108-.pragma library
109-
110-/*!
111- \qmlmethod clamp(x, min, max)
112- Ensure the value x is between min and max
113- */
114-function clamp(x, min, max) {
115- if (min <= max) {
116- return Math.max(min, Math.min(x, max));
117- } else {
118- // swap min/max if min > max
119- return clamp(x, max, min);
120- }
121-}
122-
123-/*!
124- \qmlmethod lerp(delta, from, to)
125- Get the linear interpolation
126- */
127-function lerp(delta, from, to) {
128- return ((1.0 - delta) * from) + (delta * to);
129-}
130-
131-/*!
132- \qmlmethod getFlickableChild(item)
133- Linearly project a value x from [xmin, xmax] into [ymin, ymax]
134- */
135-function projectValue(x, xmin, xmax, ymin, ymax) {
136- return ((x - xmin) * ymax - (x - xmax) * ymin) / (xmax - xmin)
137-}
138-
139-/*!
140- \qmlmethod clampAndProject(x, xmin, xmax, ymin, ymax)
141- Linearly project a value x, but in addition to projectValue it's clamped to xmin/xmax first
142- */
143-function clampAndProject(x, xmin, xmax, ymin, ymax) {
144- return projectValue(clamp(x, xmin, xmax), xmin, xmax, ymin, ymax)
145-}
146
147=== removed file 'src/Ubuntu/Components/1.2/scrollbarUtils.js'
148--- src/Ubuntu/Components/1.2/scrollbarUtils.js 2015-04-30 08:32:44 +0000
149+++ src/Ubuntu/Components/1.2/scrollbarUtils.js 1970-01-01 00:00:00 +0000
150@@ -1,129 +0,0 @@
151-/*
152- * Copyright 2012 Canonical Ltd.
153- *
154- * This program is free software; you can redistribute it and/or modify
155- * it under the terms of the GNU Lesser General Public License as published by
156- * the Free Software Foundation; version 3.
157- *
158- * This program is distributed in the hope that it will be useful,
159- * but WITHOUT ANY WARRANTY; without even the implied warranty of
160- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
161- * GNU Lesser General Public License for more details.
162- *
163- * You should have received a copy of the GNU Lesser General Public License
164- * along with this program. If not, see <http://www.gnu.org/licenses/>.
165- */
166-
167-.pragma library
168-
169-Qt.include("mathUtils.js")
170-
171-/*!
172- \internal
173- Object storing property names used in calculations.
174- */
175-var _obj = {
176- scrollbar: null,
177- vertical: false,
178- propOrigin: "",
179- propContent: "",
180- propPosRatio: "",
181- propSizeRatio: "",
182- propCoordinate: "",
183- propSize: "",
184- refresh: function () {
185- _obj.vertical = (_obj.scrollbar.align === Qt.AlignLeading) || (_obj.scrollbar.align === Qt.AlignTrailing)
186- _obj.propOrigin = (_obj.vertical) ? "originY" : "originX";
187- _obj.propContent = (_obj.vertical) ? "contentY" : "contentX";
188- _obj.propPosRatio = (_obj.vertical) ? "yPosition" : "xPosition";
189- _obj.propSizeRatio = (_obj.vertical) ? "heightRatio" : "widthRatio";
190- _obj.propCoordinate = (_obj.vertical) ? "y" : "x";
191- _obj.propSize = (_obj.vertical) ? "height" : "width";
192- }
193-}
194-
195-/*!
196- \internal
197- Checks whether the _obj is valid or not. Must be called in every function
198- as those can be invoked prior to the host (style) component completion.
199- */
200-function __check(sb) {
201- if (sb !== null && (_obj.scrollbar !== sb)) {
202- _obj.scrollbar = sb;
203- sb.flickableItemChanged.connect(_obj.refresh);
204- sb.alignChanged.connect(_obj.refresh);
205- _obj.refresh();
206- }
207-
208- return _obj.scrollbar;
209-}
210-
211-/*!
212- Returns whether the scrollbar is vertical or horizontal.
213- */
214-function isVertical(scrollbar) {
215- if (!__check(scrollbar)) return 0;
216- return _obj.vertical;
217-}
218-
219-/*!
220- Calculates the slider position based on the visible area's ratios.
221- */
222-function sliderPos(scrollbar, min, max) {
223- if (!__check(scrollbar)) return 0;
224- return clamp(scrollbar.flickableItem.visibleArea[_obj.propPosRatio] * scrollbar.flickableItem[_obj.propSize], min, max);
225-}
226-
227-/*!
228- Calculates the slider size for ListViews based on the visible area's position
229- and size ratios, clamping it between min and max.
230-
231- The function can be used in Scrollbar styles to calculate the size of the slider.
232- */
233-function sliderSize(scrollbar, min, max) {
234- if (!__check(scrollbar)) return 0;
235- var sizeRatio = scrollbar.flickableItem.visibleArea[_obj.propSizeRatio];
236- var posRatio = scrollbar.flickableItem.visibleArea[_obj.propPosRatio];
237- var sizeUnderflow = (sizeRatio * max) < min ? min - (sizeRatio * max) : 0
238- var startPos = posRatio * (max - sizeUnderflow)
239- var endPos = (posRatio + sizeRatio) * (max - sizeUnderflow) + sizeUnderflow
240- var overshootStart = startPos < 0 ? -startPos : 0
241- var overshootEnd = endPos > max ? endPos - max : 0
242-
243- // overshoot adjusted start and end
244- var adjustedStartPos = startPos + overshootStart
245- var adjustedEndPos = endPos - overshootStart - overshootEnd
246-
247- // final position and size of thumb
248- var position = adjustedStartPos + min > max ? max - min : adjustedStartPos
249- var result = (adjustedEndPos - position) < min ? min : (adjustedEndPos - position)
250-
251- return result;
252-}
253-
254-/*!
255- The function calculates and clamps the position to be scrolled to the minimum
256- and maximum values.
257-
258- The scroll and drag functions require a slider that does not have any minimum
259- size set (meaning the minimum is set to 0.0). Implementations should consider
260- using an invisible cursor to drag the slider and the ListView position.
261- */
262-function scrollAndClamp(scrollbar, amount, min, max) {
263- if (!__check(scrollbar)) return 0;
264- return scrollbar.flickableItem[_obj.propOrigin] +
265- clamp(scrollbar.flickableItem[_obj.propContent] - scrollbar.flickableItem[_obj.propOrigin] + amount,
266- min, max);
267-}
268-
269-/*!
270- The function calculates the new position of the dragged slider. The amount is
271- relative to the contentSize, which is either the flickable's contentHeight or
272- contentWidth or other calculated value, depending on its orientation. The pageSize
273- specifies the visibleArea, and it is usually the heigtht/width of the scrolling area.
274- */
275-function dragAndClamp(scrollbar, cursor, contentSize, pageSize) {
276- if (!__check(scrollbar)) return 0;
277- scrollbar.flickableItem[_obj.propContent] =
278- scrollbar.flickableItem[_obj.propOrigin] + cursor[_obj.propCoordinate] * contentSize / pageSize;
279-}
280
281=== modified file 'src/Ubuntu/Components/1.3/Slider.qml'
282--- src/Ubuntu/Components/1.3/Slider.qml 2015-09-28 14:36:54 +0000
283+++ src/Ubuntu/Components/1.3/Slider.qml 2015-11-20 07:47:28 +0000
284@@ -159,7 +159,7 @@
285 return Toolkit.MathUtils.clampAndProject(value, slider.minimumValue,
286 slider.maximumValue, 1.0, 0.0);
287 } else {
288- return MathUtils.clampAndProject(value, slider.minimumValue,
289+ return Toolkit.MathUtils.clampAndProject(value, slider.minimumValue,
290 slider.maximumValue, 0.0, 1.0);
291 }
292
293@@ -167,10 +167,10 @@
294
295 function valueFromNormalizedValue(normalizedValue) {
296 if (Qt.application.layoutDirection == Qt.RightToLeft) {
297- return MathUtils.lerp(MathUtils.clamp(normalizedValue, 0.0, 1.0),
298+ return Toolkit.MathUtils.lerp(Toolkit.MathUtils.clamp(normalizedValue, 0.0, 1.0),
299 slider.maximumValue, slider.minimumValue);
300 } else {
301- return MathUtils.lerp(MathUtils.clamp(normalizedValue, 0.0, 1.0),
302+ return Toolkit.MathUtils.lerp(Toolkit.MathUtils.clamp(normalizedValue, 0.0, 1.0),
303 slider.minimumValue, slider.maximumValue);
304 }
305 }
306
307=== removed file 'src/Ubuntu/Components/1.3/mathUtils.js'
308--- src/Ubuntu/Components/1.3/mathUtils.js 2015-07-24 18:25:07 +0000
309+++ src/Ubuntu/Components/1.3/mathUtils.js 1970-01-01 00:00:00 +0000
310@@ -1,61 +0,0 @@
311-/*
312- * Copyright 2012-2015 Canonical Ltd.
313- *
314- * This program is free software; you can redistribute it and/or modify
315- * it under the terms of the GNU Lesser General Public License as published by
316- * the Free Software Foundation; version 3.
317- *
318- * This program is distributed in the hope that it will be useful,
319- * but WITHOUT ANY WARRANTY; without even the implied warranty of
320- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
321- * GNU Lesser General Public License for more details.
322- *
323- * You should have received a copy of the GNU Lesser General Public License
324- * along with this program. If not, see <http://www.gnu.org/licenses/>.
325- */
326-
327-/*!
328- \qmltype mathUtils
329- \inqmlmodule Ubuntu.Components 1.3
330- \ingroup ubuntu
331- \brief Various mathematical utility functions.
332- */
333-
334-.pragma library
335-
336-/*!
337- \qmlmethod mathUtils::clamp(x, min, max)
338- Ensure the value x is between min and max
339- */
340-function clamp(x, min, max) {
341- if (min <= max) {
342- return Math.max(min, Math.min(x, max));
343- } else {
344- // swap min/max if min > max
345- return clamp(x, max, min);
346- }
347-}
348-
349-/*!
350- \qmlmethod mathUtils::lerp(delta, from, to)
351- Get the linear interpolation
352- */
353-function lerp(delta, from, to) {
354- return ((1.0 - delta) * from) + (delta * to);
355-}
356-
357-/*!
358- \qmlmethod mathUtils::projectValue(x, xmin, xmax, ymin, ymax)
359- Linearly project a value x from [xmin, xmax] into [ymin, ymax]
360- */
361-function projectValue(x, xmin, xmax, ymin, ymax) {
362- return ((x - xmin) * ymax - (x - xmax) * ymin) / (xmax - xmin)
363-}
364-
365-/*!
366- \qmlmethod mathUtils::clampAndProject(x, xmin, xmax, ymin, ymax)
367- Linearly project a value x, but in addition to projectValue it's clamped to xmin/xmax first
368- */
369-function clampAndProject(x, xmin, xmax, ymin, ymax) {
370- return projectValue(clamp(x, xmin, xmax), xmin, xmax, ymin, ymax)
371-}
372
373=== modified file 'src/Ubuntu/Components/ComponentModule.pro'
374--- src/Ubuntu/Components/ComponentModule.pro 2015-11-04 07:47:46 +0000
375+++ src/Ubuntu/Components/ComponentModule.pro 2015-11-20 07:47:28 +0000
376@@ -43,7 +43,6 @@
377 1.2/MainView12.qml \
378 1.2/MainViewBase.qml \
379 1.2/MainView.qml \
380- 1.2/mathUtils.js \
381 1.2/Object.qml \
382 1.2/OptionSelectorDelegate.qml \
383 1.2/OptionSelector.qml \
384@@ -60,7 +59,6 @@
385 1.2/Panel.qml \
386 1.2/PullToRefresh.qml \
387 1.2/Scrollbar.qml \
388- 1.2/scrollbarUtils.js \
389 1.2/Slider.qml \
390 1.2/sliderUtils.js \
391 1.2/stack.js \
392@@ -96,7 +94,6 @@
393 1.3/InputHandler.qml \
394 1.3/MainViewBase.qml \
395 1.3/MainView.qml \
396- 1.3/mathUtils.js \
397 1.3/OptionSelectorDelegate.qml \
398 1.3/OptionSelector.qml \
399 1.3/OrientationHelper.qml \
400
401=== modified file 'src/Ubuntu/Components/Themes/Ambiance/1.2/ScrollbarStyle.qml'
402--- src/Ubuntu/Components/Themes/Ambiance/1.2/ScrollbarStyle.qml 2015-04-24 14:52:19 +0000
403+++ src/Ubuntu/Components/Themes/Ambiance/1.2/ScrollbarStyle.qml 2015-11-20 07:47:28 +0000
404@@ -85,7 +85,7 @@
405 property Flickable flickableItem: styledItem.flickableItem
406 property bool isScrollable: styledItem.__private.scrollable && pageSize > 0.0
407 && contentSize > 0.0 && contentSize > pageSize
408- property bool isVertical: ScrollbarUtils.isVertical(styledItem)
409+ property bool isVertical: (styledItem.align === Qt.AlignLeading) || (styledItem.align === Qt.AlignTrailing)
410 property bool frontAligned: (styledItem.align === Qt.AlignLeading)
411 property bool rearAligned: (styledItem.align === Qt.AlignTrailing)
412 property bool topAligned: (styledItem.align === Qt.AlignTop)
413@@ -94,6 +94,83 @@
414 property real pageSize: (isVertical) ? styledItem.height : styledItem.width
415 property real contentSize: (isVertical) ? styledItem.flickableItem.contentHeight : styledItem.flickableItem.contentWidth
416
417+ /*!
418+ \internal
419+ Object storing property names used in calculations.
420+ */
421+ QtObject {
422+ id: scrollbarUtils
423+
424+ property string propOrigin: (isVertical) ? "originY" : "originX"
425+ property string propContent: (isVertical) ? "contentY" : "contentX"
426+ property string propPosRatio: (isVertical) ? "yPosition" : "xPosition"
427+ property string propSizeRatio: (isVertical) ? "heightRatio" : "widthRatio"
428+ property string propCoordinate: (isVertical) ? "y" : "x"
429+ property string propSize: (isVertical) ? "height" : "width"
430+
431+ /*!
432+ \internal
433+ Calculates the slider position based on the visible area's ratios.
434+ */
435+ function sliderPos(min, max) {
436+ return MathUtils.clamp(styledItem.flickableItem.visibleArea[propPosRatio] * styledItem.flickableItem[propSize], min, max);
437+ }
438+
439+ /*!
440+ \internal
441+ Calculates the slider size for ListViews based on the visible area's position
442+ and size ratios, clamping it between min and max.
443+
444+ The function can be used in Scrollbar styles to calculate the size of the slider.
445+ */
446+ function sliderSize(min, max) {
447+ var sizeRatio = styledItem.flickableItem.visibleArea[propSizeRatio];
448+ var posRatio = styledItem.flickableItem.visibleArea[propPosRatio];
449+ var sizeUnderflow = (sizeRatio * max) < min ? min - (sizeRatio * max) : 0
450+ var startPos = posRatio * (max - sizeUnderflow)
451+ var endPos = (posRatio + sizeRatio) * (max - sizeUnderflow) + sizeUnderflow
452+ var overshootStart = startPos < 0 ? -startPos : 0
453+ var overshootEnd = endPos > max ? endPos - max : 0
454+
455+ // overshoot adjusted start and end
456+ var adjustedStartPos = startPos + overshootStart
457+ var adjustedEndPos = endPos - overshootStart - overshootEnd
458+
459+ // final position and size of thumb
460+ var position = adjustedStartPos + min > max ? max - min : adjustedStartPos
461+ var result = (adjustedEndPos - position) < min ? min : (adjustedEndPos - position)
462+
463+ return result;
464+ }
465+
466+ /*!
467+ \internal
468+ The function calculates and clamps the position to be scrolled to the minimum
469+ and maximum values.
470+
471+ The scroll and drag functions require a slider that does not have any minimum
472+ size set (meaning the minimum is set to 0.0). Implementations should consider
473+ using an invisible cursor to drag the slider and the ListView position.
474+ */
475+ function scrollAndClamp(amount, min, max) {
476+ return styledItem.flickableItem[propOrigin] +
477+ MathUtils.clamp(styledItem.flickableItem[propContent] - styledItem.flickableItem[propOrigin] + amount,
478+ min, max);
479+ }
480+
481+ /*!
482+ \internal
483+ The function calculates the new position of the dragged slider. The amount is
484+ relative to the contentSize, which is either the flickable's contentHeight or
485+ contentWidth or other calculated value, depending on its orientation. The pageSize
486+ specifies the visibleArea, and it is usually the heigtht/width of the scrolling area.
487+ */
488+ function dragAndClamp(cursor, contentSize, pageSize) {
489+ styledItem.flickableItem[propContent] =
490+ styledItem.flickableItem[propOrigin] + cursor[propCoordinate] * contentSize / pageSize;
491+ }
492+ }
493+
494 /*****************************************
495 Visuals
496 *****************************************/
497@@ -231,13 +308,13 @@
498 // total size of the flickable.
499 Item {
500 id: scrollCursor
501- x: (isVertical) ? 0 : ScrollbarUtils.sliderPos(styledItem, 0.0, styledItem.width - scrollCursor.width)
502- y: (!isVertical) ? 0 : ScrollbarUtils.sliderPos(styledItem, 0.0, styledItem.height - scrollCursor.height)
503- width: (isVertical) ? scrollbarArea.thickness : ScrollbarUtils.sliderSize(styledItem, 0.0, flickableItem.width)
504- height: (!isVertical) ? scrollbarArea.thickness : ScrollbarUtils.sliderSize(styledItem, 0.0, flickableItem.height)
505+ x: (isVertical) ? 0 : scrollbarUtils.sliderPos(styledItem, 0.0, styledItem.width - scrollCursor.width)
506+ y: (!isVertical) ? 0 : scrollbarUtils.sliderPos(styledItem, 0.0, styledItem.height - scrollCursor.height)
507+ width: (isVertical) ? scrollbarArea.thickness : scrollbarUtils.sliderSize(styledItem, 0.0, flickableItem.width)
508+ height: (!isVertical) ? scrollbarArea.thickness : scrollbarUtils.sliderSize(styledItem, 0.0, flickableItem.height)
509
510 function drag() {
511- ScrollbarUtils.dragAndClamp(styledItem, scrollCursor, contentSize, pageSize);
512+ scrollbarUtils.dragAndClamp(styledItem, scrollCursor, contentSize, pageSize);
513 }
514 }
515
516@@ -253,10 +330,10 @@
517 bottom: (!isVertical) ? scrollbarArea.bottom : undefined
518 }
519
520- x: (isVertical) ? 0 : ScrollbarUtils.sliderPos(styledItem, 0.0, styledItem.width - slider.width)
521- y: (!isVertical) ? 0 : ScrollbarUtils.sliderPos(styledItem, 0.0, styledItem.height - slider.height)
522- width: (isVertical) ? scrollbarArea.thickness : ScrollbarUtils.sliderSize(styledItem, minimumSliderSize, flickableItem.width)
523- height: (!isVertical) ? scrollbarArea.thickness : ScrollbarUtils.sliderSize(styledItem, minimumSliderSize, flickableItem.height)
524+ x: (isVertical) ? 0 : scrollbarUtils.sliderPos(styledItem, 0.0, styledItem.width - slider.width)
525+ y: (!isVertical) ? 0 : scrollbarUtils.sliderPos(styledItem, 0.0, styledItem.height - slider.height)
526+ width: (isVertical) ? scrollbarArea.thickness : scrollbarUtils.sliderSize(styledItem, minimumSliderSize, flickableItem.width)
527+ height: (!isVertical) ? scrollbarArea.thickness : scrollbarUtils.sliderSize(styledItem, minimumSliderSize, flickableItem.height)
528 radius: visuals.sliderRadius
529
530 Behavior on width {
531@@ -275,7 +352,7 @@
532 }
533
534 function scroll(amount) {
535- scrollAnimation.to = ScrollbarUtils.scrollAndClamp(styledItem, amount, 0.0, contentSize - pageSize);
536+ scrollAnimation.to = scrollbarUtils.scrollAndClamp(styledItem, amount, 0.0, contentSize - pageSize);
537 scrollAnimation.restart();
538 }
539 }
540
541=== modified file 'src/Ubuntu/Components/Themes/Ambiance/1.3/ScrollbarStyle.qml'
542--- src/Ubuntu/Components/Themes/Ambiance/1.3/ScrollbarStyle.qml 2015-06-02 15:24:54 +0000
543+++ src/Ubuntu/Components/Themes/Ambiance/1.3/ScrollbarStyle.qml 2015-11-20 07:47:28 +0000
544@@ -85,7 +85,7 @@
545 property Flickable flickableItem: styledItem.flickableItem
546 property bool isScrollable: styledItem.__private.scrollable && pageSize > 0.0
547 && contentSize > 0.0 && contentSize > pageSize
548- property bool isVertical: ScrollbarUtils.isVertical(styledItem)
549+ property bool isVertical: (styledItem.align === Qt.AlignLeading) || (styledItem.align === Qt.AlignTrailing)
550 property bool frontAligned: (styledItem.align === Qt.AlignLeading)
551 property bool rearAligned: (styledItem.align === Qt.AlignTrailing)
552 property bool topAligned: (styledItem.align === Qt.AlignTop)
553@@ -94,6 +94,83 @@
554 property real pageSize: (isVertical) ? styledItem.height : styledItem.width
555 property real contentSize: (isVertical) ? styledItem.flickableItem.contentHeight : styledItem.flickableItem.contentWidth
556
557+ /*!
558+ \internal
559+ Object storing property names used in calculations.
560+ */
561+ QtObject {
562+ id: scrollbarUtils
563+
564+ property string propOrigin: (isVertical) ? "originY" : "originX"
565+ property string propContent: (isVertical) ? "contentY" : "contentX"
566+ property string propPosRatio: (isVertical) ? "yPosition" : "xPosition"
567+ property string propSizeRatio: (isVertical) ? "heightRatio" : "widthRatio"
568+ property string propCoordinate: (isVertical) ? "y" : "x"
569+ property string propSize: (isVertical) ? "height" : "width"
570+
571+ /*!
572+ \internal
573+ Calculates the slider position based on the visible area's ratios.
574+ */
575+ function sliderPos(min, max) {
576+ return MathUtils.clamp(styledItem.flickableItem.visibleArea[propPosRatio] * styledItem.flickableItem[propSize], min, max);
577+ }
578+
579+ /*!
580+ \internal
581+ Calculates the slider size for ListViews based on the visible area's position
582+ and size ratios, clamping it between min and max.
583+
584+ The function can be used in Scrollbar styles to calculate the size of the slider.
585+ */
586+ function sliderSize(min, max) {
587+ var sizeRatio = styledItem.flickableItem.visibleArea[propSizeRatio];
588+ var posRatio = styledItem.flickableItem.visibleArea[propPosRatio];
589+ var sizeUnderflow = (sizeRatio * max) < min ? min - (sizeRatio * max) : 0
590+ var startPos = posRatio * (max - sizeUnderflow)
591+ var endPos = (posRatio + sizeRatio) * (max - sizeUnderflow) + sizeUnderflow
592+ var overshootStart = startPos < 0 ? -startPos : 0
593+ var overshootEnd = endPos > max ? endPos - max : 0
594+
595+ // overshoot adjusted start and end
596+ var adjustedStartPos = startPos + overshootStart
597+ var adjustedEndPos = endPos - overshootStart - overshootEnd
598+
599+ // final position and size of thumb
600+ var position = adjustedStartPos + min > max ? max - min : adjustedStartPos
601+ var result = (adjustedEndPos - position) < min ? min : (adjustedEndPos - position)
602+
603+ return result;
604+ }
605+
606+ /*!
607+ \internal
608+ The function calculates and clamps the position to be scrolled to the minimum
609+ and maximum values.
610+
611+ The scroll and drag functions require a slider that does not have any minimum
612+ size set (meaning the minimum is set to 0.0). Implementations should consider
613+ using an invisible cursor to drag the slider and the ListView position.
614+ */
615+ function scrollAndClamp(amount, min, max) {
616+ return styledItem.flickableItem[propOrigin] +
617+ MathUtils.clamp(styledItem.flickableItem[propContent] - styledItem.flickableItem[propOrigin] + amount,
618+ min, max);
619+ }
620+
621+ /*!
622+ \internal
623+ The function calculates the new position of the dragged slider. The amount is
624+ relative to the contentSize, which is either the flickable's contentHeight or
625+ contentWidth or other calculated value, depending on its orientation. The pageSize
626+ specifies the visibleArea, and it is usually the heigtht/width of the scrolling area.
627+ */
628+ function dragAndClamp(cursor, contentSize, pageSize) {
629+ styledItem.flickableItem[propContent] =
630+ styledItem.flickableItem[propOrigin] + cursor[propCoordinate] * contentSize / pageSize;
631+ }
632+ }
633+
634 /*****************************************
635 Visuals
636 *****************************************/
637@@ -231,13 +308,13 @@
638 // total size of the flickable.
639 Item {
640 id: scrollCursor
641- x: (isVertical) ? 0 : ScrollbarUtils.sliderPos(styledItem, 0.0, styledItem.width - scrollCursor.width)
642- y: (!isVertical) ? 0 : ScrollbarUtils.sliderPos(styledItem, 0.0, styledItem.height - scrollCursor.height)
643- width: (isVertical) ? scrollbarArea.thickness : ScrollbarUtils.sliderSize(styledItem, 0.0, flickableItem.width)
644- height: (!isVertical) ? scrollbarArea.thickness : ScrollbarUtils.sliderSize(styledItem, 0.0, flickableItem.height)
645+ x: (isVertical) ? 0 : scrollbarUtils.sliderPos(styledItem, 0.0, styledItem.width - scrollCursor.width)
646+ y: (!isVertical) ? 0 : scrollbarUtils.sliderPos(styledItem, 0.0, styledItem.height - scrollCursor.height)
647+ width: (isVertical) ? scrollbarArea.thickness : scrollbarUtils.sliderSize(styledItem, 0.0, flickableItem.width)
648+ height: (!isVertical) ? scrollbarArea.thickness : scrollbarUtils.sliderSize(styledItem, 0.0, flickableItem.height)
649
650 function drag() {
651- ScrollbarUtils.dragAndClamp(styledItem, scrollCursor, contentSize, pageSize);
652+ scrollbarUtils.dragAndClamp(styledItem, scrollCursor, contentSize, pageSize);
653 }
654 }
655
656@@ -253,10 +330,10 @@
657 bottom: (!isVertical) ? scrollbarArea.bottom : undefined
658 }
659
660- x: (isVertical) ? 0 : ScrollbarUtils.sliderPos(styledItem, 0.0, styledItem.width - slider.width)
661- y: (!isVertical) ? 0 : ScrollbarUtils.sliderPos(styledItem, 0.0, styledItem.height - slider.height)
662- width: (isVertical) ? scrollbarArea.thickness : ScrollbarUtils.sliderSize(styledItem, minimumSliderSize, flickableItem.width)
663- height: (!isVertical) ? scrollbarArea.thickness : ScrollbarUtils.sliderSize(styledItem, minimumSliderSize, flickableItem.height)
664+ x: (isVertical) ? 0 : scrollbarUtils.sliderPos(styledItem, 0.0, styledItem.width - slider.width)
665+ y: (!isVertical) ? 0 : scrollbarUtils.sliderPos(styledItem, 0.0, styledItem.height - slider.height)
666+ width: (isVertical) ? scrollbarArea.thickness : scrollbarUtils.sliderSize(styledItem, minimumSliderSize, flickableItem.width)
667+ height: (!isVertical) ? scrollbarArea.thickness : scrollbarUtils.sliderSize(styledItem, minimumSliderSize, flickableItem.height)
668 radius: visuals.sliderRadius
669
670 Behavior on width {
671@@ -275,7 +352,7 @@
672 }
673
674 function scroll(amount) {
675- scrollAnimation.to = ScrollbarUtils.scrollAndClamp(styledItem, amount, 0.0, contentSize - pageSize);
676+ scrollAnimation.to = scrollbarUtils.scrollAndClamp(styledItem, amount, 0.0, contentSize - pageSize);
677 scrollAnimation.restart();
678 }
679 }
680
681=== removed file 'src/Ubuntu/Components/Themes/Ambiance/1.3/scrollbarUtils.js'
682--- src/Ubuntu/Components/Themes/Ambiance/1.3/scrollbarUtils.js 2015-07-23 22:12:36 +0000
683+++ src/Ubuntu/Components/Themes/Ambiance/1.3/scrollbarUtils.js 1970-01-01 00:00:00 +0000
684@@ -1,139 +0,0 @@
685-/*
686- * Copyright 2012-2015 Canonical Ltd.
687- *
688- * This program is free software; you can redistribute it and/or modify
689- * it under the terms of the GNU Lesser General Public License as published by
690- * the Free Software Foundation; version 3.
691- *
692- * This program is distributed in the hope that it will be useful,
693- * but WITHOUT ANY WARRANTY; without even the implied warranty of
694- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
695- * GNU Lesser General Public License for more details.
696- *
697- * You should have received a copy of the GNU Lesser General Public License
698- * along with this program. If not, see <http://www.gnu.org/licenses/>.
699- */
700-
701-/*!
702- \qmltype scrollbarUtils
703- \inqmlmodule Ubuntu.Components 1.3
704- \ingroup ubuntu
705- \brief Various scrolling utility functions.
706- */
707-
708-.pragma library
709-
710-Qt.include("mathUtils.js")
711-
712-/*
713- Object storing property names used in calculations.
714- */
715-var _obj = {
716- scrollbar: null,
717- vertical: false,
718- propOrigin: "",
719- propContent: "",
720- propPosRatio: "",
721- propSizeRatio: "",
722- propCoordinate: "",
723- propSize: "",
724- refresh: function () {
725- _obj.vertical = (_obj.scrollbar.align === Qt.AlignLeading) || (_obj.scrollbar.align === Qt.AlignTrailing)
726- _obj.propOrigin = (_obj.vertical) ? "originY" : "originX";
727- _obj.propContent = (_obj.vertical) ? "contentY" : "contentX";
728- _obj.propPosRatio = (_obj.vertical) ? "yPosition" : "xPosition";
729- _obj.propSizeRatio = (_obj.vertical) ? "heightRatio" : "widthRatio";
730- _obj.propCoordinate = (_obj.vertical) ? "y" : "x";
731- _obj.propSize = (_obj.vertical) ? "height" : "width";
732- }
733-}
734-
735-/*
736- Checks whether the _obj is valid or not. Must be called in every function
737- as those can be invoked prior to the host (style) component completion.
738- */
739-function __check(sb) {
740- if (sb !== null && (_obj.scrollbar !== sb)) {
741- _obj.scrollbar = sb;
742- sb.flickableItemChanged.connect(_obj.refresh);
743- sb.alignChanged.connect(_obj.refresh);
744- _obj.refresh();
745- }
746-
747- return _obj.scrollbar;
748-}
749-
750-/*!
751- \qmlmethod scrollbarUtils::isVertical(scrollbar)
752- Returns whether the scrollbar is vertical or horizontal.
753- */
754-function isVertical(scrollbar) {
755- if (!__check(scrollbar)) return 0;
756- return _obj.vertical;
757-}
758-
759-/*!
760- \qmlmethod scrollbarUtils::sliderPos(scrollbar, min, max)
761- Calculates the slider position based on the visible area's ratios.
762- */
763-function sliderPos(scrollbar, min, max) {
764- if (!__check(scrollbar)) return 0;
765- return clamp(scrollbar.flickableItem.visibleArea[_obj.propPosRatio] * scrollbar.flickableItem[_obj.propSize], min, max);
766-}
767-
768-/*!
769- \qmlmethod scrollbarUtils::sliderSize(scrollbar, min, max)
770- Calculates the slider size for ListViews based on the visible area's position
771- and size ratios, clamping it between min and max.
772-
773- The function can be used in Scrollbar styles to calculate the size of the slider.
774- */
775-function sliderSize(scrollbar, min, max) {
776- if (!__check(scrollbar)) return 0;
777- var sizeRatio = scrollbar.flickableItem.visibleArea[_obj.propSizeRatio];
778- var posRatio = scrollbar.flickableItem.visibleArea[_obj.propPosRatio];
779- var sizeUnderflow = (sizeRatio * max) < min ? min - (sizeRatio * max) : 0
780- var startPos = posRatio * (max - sizeUnderflow)
781- var endPos = (posRatio + sizeRatio) * (max - sizeUnderflow) + sizeUnderflow
782- var overshootStart = startPos < 0 ? -startPos : 0
783- var overshootEnd = endPos > max ? endPos - max : 0
784-
785- // overshoot adjusted start and end
786- var adjustedStartPos = startPos + overshootStart
787- var adjustedEndPos = endPos - overshootStart - overshootEnd
788-
789- // final position and size of thumb
790- var position = adjustedStartPos + min > max ? max - min : adjustedStartPos
791- var result = (adjustedEndPos - position) < min ? min : (adjustedEndPos - position)
792-
793- return result;
794-}
795-
796-/*!
797- \qmlmethod scrollbarUtils::scrollAndClamp(scrollbar, amount, min, max)
798- The function calculates and clamps the position to be scrolled to the minimum
799- and maximum values.
800-
801- The scroll and drag functions require a slider that does not have any minimum
802- size set (meaning the minimum is set to 0.0). Implementations should consider
803- using an invisible cursor to drag the slider and the ListView position.
804- */
805-function scrollAndClamp(scrollbar, amount, min, max) {
806- if (!__check(scrollbar)) return 0;
807- return scrollbar.flickableItem[_obj.propOrigin] +
808- clamp(scrollbar.flickableItem[_obj.propContent] - scrollbar.flickableItem[_obj.propOrigin] + amount,
809- min, max);
810-}
811-
812-/*!
813- \qmlmethod scrollbarUtils::dragAndClamp(scrollbar, cursor, contentSize, pageSize)
814- The function calculates the new position of the dragged slider. The amount is
815- relative to the contentSize, which is either the flickable's contentHeight or
816- contentWidth or other calculated value, depending on its orientation. The pageSize
817- specifies the visibleArea, and it is usually the heigtht/width of the scrolling area.
818- */
819-function dragAndClamp(scrollbar, cursor, contentSize, pageSize) {
820- if (!__check(scrollbar)) return 0;
821- scrollbar.flickableItem[_obj.propContent] =
822- scrollbar.flickableItem[_obj.propOrigin] + cursor[_obj.propCoordinate] * contentSize / pageSize;
823-}
824
825=== modified file 'src/Ubuntu/Components/Themes/Ambiance/qmldir'
826--- src/Ubuntu/Components/Themes/Ambiance/qmldir 2015-10-02 22:48:13 +0000
827+++ src/Ubuntu/Components/Themes/Ambiance/qmldir 2015-11-20 07:47:28 +0000
828@@ -82,6 +82,5 @@
829
830 ListItemStyle 1.3 ./1.3/ListItemStyle.qml
831 internal SliderUtils 1.3/sliderUtils.js
832-internal ScrollbarUtils 1.3/scrollbarUtils.js
833 internal ColorUtils 1.3/colorUtils.js
834 PageHeaderStyle 1.3 ./1.3/PageHeaderStyle.qml
835
836=== modified file 'src/Ubuntu/Components/plugin/plugin.cpp'
837--- src/Ubuntu/Components/plugin/plugin.cpp 2015-11-11 11:38:42 +0000
838+++ src/Ubuntu/Components/plugin/plugin.cpp 2015-11-20 07:47:28 +0000
839@@ -72,6 +72,7 @@
840 #include "uclistitemlayout.h"
841 #include "ucbottomedgehint.h"
842 #include "gestures/ucswipearea.h"
843+#include "ucmathutils.h"
844
845 #include <sys/types.h>
846 #include <unistd.h>
847@@ -196,6 +197,7 @@
848 qmlRegisterType<UCInverseMouse>(uri, major, minor, "InverseMouse");
849 qmlRegisterType<UCActionItem>(uri, major, minor, "ActionItem");
850 qmlRegisterSingletonType<UCHaptics>(uri, major, minor, "Haptics", registerHaptics);
851+ qmlRegisterSingletonType<UCMathUtils>(uri, major, minor, "MathUtils", UCMathUtils::qmlRegisterTypeCallback);
852 }
853
854 void UbuntuComponentsPlugin::registerTypes(const char *uri)
855
856=== modified file 'src/Ubuntu/Components/plugin/plugin.pri'
857--- src/Ubuntu/Components/plugin/plugin.pri 2015-11-17 13:45:25 +0000
858+++ src/Ubuntu/Components/plugin/plugin.pri 2015-11-20 07:47:28 +0000
859@@ -98,6 +98,7 @@
860 $$PWD/gestures/ucswipearea_p.h \
861 $$PWD/gestures/damper.h \
862 $$PWD/gestures/ubuntugesturesqmlglobal.h \
863+ $$PWD/ucmathutils.h
864
865 SOURCES += $$PWD/plugin.cpp \
866 $$PWD/uctheme.cpp \
867@@ -163,6 +164,7 @@
868 $$PWD/ucimportversionchecker_p.cpp \
869 $$PWD/ucbottomedgehint.cpp \
870 $$PWD/gestures/ucswipearea.cpp \
871+ $$PWD/ucmathutils.cpp
872
873 # adapters
874 SOURCES += $$PWD/adapters/alarmsadapter_organizer.cpp
875
876=== added file 'src/Ubuntu/Components/plugin/ucmathutils.cpp'
877--- src/Ubuntu/Components/plugin/ucmathutils.cpp 1970-01-01 00:00:00 +0000
878+++ src/Ubuntu/Components/plugin/ucmathutils.cpp 2015-11-20 07:47:28 +0000
879@@ -0,0 +1,81 @@
880+/*
881+ * Copyright 2015 Canonical Ltd.
882+ *
883+ * This program is free software; you can redistribute it and/or modify
884+ * it under the terms of the GNU Lesser General Public License as published by
885+ * the Free Software Foundation; version 3.
886+ *
887+ * This program is distributed in the hope that it will be useful,
888+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
889+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
890+ * GNU Lesser General Public License for more details.
891+ *
892+ * You should have received a copy of the GNU Lesser General Public License
893+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
894+ *
895+ */
896+
897+#include "ucmathutils.h"
898+#include <QDebug>
899+
900+/*!
901+ \qmltype mathUtils
902+ \inqmlmodule Ubuntu.Components
903+ \ingroup ubuntu
904+ \brief Various mathematical utility functions.
905+ */
906+
907+UCMathUtils::UCMathUtils(QObject *parent) : QObject(parent)
908+{
909+
910+}
911+
912+/*!
913+ \qmlmethod mathUtils::clamp(x, min, max)
914+ Ensure the value x is between min and max
915+ */
916+double UCMathUtils::clamp(double x, double min, double max)
917+{
918+ if (!(min > max)) {
919+ return qBound(min, x, max);
920+ } else {
921+ // swap min/max if min > max
922+ qWarning()<<"MathUtils.clamp, min value should not be bigger than the max value";
923+ return qBound(max, x, min);
924+ }
925+}
926+
927+/*!
928+ \qmlmethod mathUtils::lerp(delta, from, to)
929+ Get the linear interpolation
930+ */
931+double UCMathUtils::lerp(double delta, double from, double to)
932+{
933+ return ((1.0 - delta) * from) + (delta * to);
934+}
935+
936+/*!
937+ \qmlmethod mathUtils::projectValue(x, xmin, xmax, ymin, ymax)
938+ Linearly project a value x from [xmin, xmax] into [ymin, ymax]
939+ */
940+double UCMathUtils::projectValue(double x, double xmin, double xmax, double ymin, double ymax)
941+{
942+ return ((x - xmin) * ymax - (x - xmax) * ymin) / (xmax - xmin);
943+}
944+
945+/*!
946+ \qmlmethod mathUtils::clampAndProject(x, xmin, xmax, ymin, ymax)
947+ Linearly project a value x, but in addition to projectValue it's clamped to xmin/xmax first
948+ */
949+double UCMathUtils::clampAndProject(double x, double xmin, double xmax, double ymin, double ymax)
950+{
951+ return projectValue(clamp(x, xmin, xmax), xmin, xmax, ymin, ymax);
952+}
953+
954+QObject *UCMathUtils::qmlRegisterTypeCallback(QQmlEngine *engine, QJSEngine *scriptEngine)
955+{
956+ Q_UNUSED(engine);
957+ Q_UNUSED(scriptEngine);
958+ return new UCMathUtils;
959+}
960+
961
962=== added file 'src/Ubuntu/Components/plugin/ucmathutils.h'
963--- src/Ubuntu/Components/plugin/ucmathutils.h 1970-01-01 00:00:00 +0000
964+++ src/Ubuntu/Components/plugin/ucmathutils.h 2015-11-20 07:47:28 +0000
965@@ -0,0 +1,40 @@
966+/*
967+ * Copyright 2015 Canonical Ltd.
968+ *
969+ * This program is free software; you can redistribute it and/or modify
970+ * it under the terms of the GNU Lesser General Public License as published by
971+ * the Free Software Foundation; version 3.
972+ *
973+ * This program is distributed in the hope that it will be useful,
974+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
975+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
976+ * GNU Lesser General Public License for more details.
977+ *
978+ * You should have received a copy of the GNU Lesser General Public License
979+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
980+ *
981+ */
982+
983+#ifndef UCMATHUTILS_H
984+#define UCMATHUTILS_H
985+
986+#include <QObject>
987+
988+QT_FORWARD_DECLARE_CLASS(QQmlEngine)
989+QT_FORWARD_DECLARE_CLASS(QJSEngine)
990+
991+class UCMathUtils : public QObject
992+{
993+ Q_OBJECT
994+public:
995+ explicit UCMathUtils(QObject *parent = 0);
996+
997+ Q_INVOKABLE double clamp(double x, double min, double max);
998+ Q_INVOKABLE double lerp(double delta, double from, double to);
999+ Q_INVOKABLE double projectValue(double x, double xmin, double xmax, double ymin, double ymax);
1000+ Q_INVOKABLE double clampAndProject(double x, double xmin, double xmax, double ymin, double ymax);
1001+
1002+ static QObject *qmlRegisterTypeCallback(QQmlEngine *engine, QJSEngine *scriptEngine);
1003+};
1004+
1005+#endif // UCMATHUTILS_H
1006
1007=== modified file 'src/Ubuntu/Components/qmldir'
1008--- src/Ubuntu/Components/qmldir 2015-11-04 07:47:46 +0000
1009+++ src/Ubuntu/Components/qmldir 2015-11-20 07:47:28 +0000
1010@@ -36,9 +36,7 @@
1011 UbuntuNumberAnimation 0.1 1.2/UbuntuNumberAnimation.qml
1012 UbuntuListView 0.1 1.2/UbuntuListView.qml
1013 singleton UbuntuColors 0.1 1.0/UbuntuColors.qml
1014-MathUtils 0.1 1.2/mathUtils.js
1015 SliderUtils 0.1 1.2/sliderUtils.js
1016-ScrollbarUtils 0.1 1.2/scrollbarUtils.js
1017 ColorUtils 0.1 1.2/colorUtils.js
1018 DateUtils 0.1 1.2/dateUtils.js
1019
1020@@ -73,9 +71,7 @@
1021 OrientationHelper 1.0 1.2/OrientationHelper.qml
1022 UbuntuNumberAnimation 1.0 1.2/UbuntuNumberAnimation.qml
1023 singleton UbuntuColors 1.0 1.0/UbuntuColors.qml
1024-MathUtils 1.0 1.2/mathUtils.js
1025 SliderUtils 1.0 1.2/sliderUtils.js
1026-ScrollbarUtils 1.0 1.2/scrollbarUtils.js
1027 ColorUtils 1.0 1.2/colorUtils.js
1028 DateUtils 1.0 1.2/dateUtils.js
1029 UbuntuListView 1.0 1.2/UbuntuListView.qml
1030@@ -136,7 +132,6 @@
1031 PullToRefresh 1.3 1.3/PullToRefresh.qml
1032 UbuntuListView 1.3 1.3/UbuntuListView11.qml
1033 Captions 1.3 1.3/Captions.qml
1034-MathUtils 1.3 1.3/mathUtils.js
1035 internal ColorUtils 1.3/colorUtils.js
1036 DateUtils 1.3 1.3/dateUtils.js
1037 ProgressionSlot 1.3 1.3/ProgressionSlot.qml
1038
1039=== modified file 'tests/unit/tst_components/tst_math_utils.qml'
1040--- tests/unit/tst_components/tst_math_utils.qml 2015-03-03 13:20:06 +0000
1041+++ tests/unit/tst_components/tst_math_utils.qml 2015-11-20 07:47:28 +0000
1042@@ -84,6 +84,83 @@
1043 compare(clamped, maxValue, "clamped value not within range")
1044 }
1045
1046+ function test_clamp_positive_lower_switched() {
1047+ var minValue = 42
1048+ var maxValue = 9
1049+ var clampValue = -7
1050+
1051+ ignoreWarning("MathUtils.clamp, min value should not be bigger than the max value")
1052+
1053+ var clamped = MathUtils.clamp(clampValue, minValue, maxValue)
1054+ compare(clamped, maxValue, "clamped value not within range")
1055+ }
1056+
1057+ function test_clamp_positive_greater_switched() {
1058+ var minValue = 42
1059+ var maxValue = 9
1060+ var clampValue = 111
1061+
1062+ ignoreWarning("MathUtils.clamp, min value should not be bigger than the max value")
1063+
1064+ var clamped = MathUtils.clamp(clampValue, minValue, maxValue)
1065+ compare(clamped, minValue, "clamped value not within range")
1066+ }
1067+
1068+ function test_clamp_positive_within_switched() {
1069+ var minValue = 53
1070+ var maxValue = 9
1071+ var clampValue = 42
1072+
1073+ ignoreWarning("MathUtils.clamp, min value should not be bigger than the max value")
1074+
1075+ var clamped = MathUtils.clamp(clampValue, minValue, maxValue)
1076+ compare(clamped, clampValue, "clamped value changed even though it shouldn't have")
1077+ }
1078+
1079+ function test_clamp_positive_on_border_switched() {
1080+ var minValue = 42
1081+ var maxValue = 9
1082+ var clampValue = 9
1083+
1084+ ignoreWarning("MathUtils.clamp, min value should not be bigger than the max value")
1085+
1086+ var clamped = MathUtils.clamp(clampValue, minValue, maxValue)
1087+ compare(clamped, clampValue, "clamped value changed even though it shouldn't have")
1088+ }
1089+
1090+ function test_clamp_negative_lower_switched() {
1091+ var minValue = -9
1092+ var maxValue = -42
1093+ var clampValue = -50
1094+
1095+ ignoreWarning("MathUtils.clamp, min value should not be bigger than the max value")
1096+
1097+ var clamped = MathUtils.clamp(clampValue, minValue, maxValue)
1098+ compare(clamped, maxValue, "clamped value not within range")
1099+ }
1100+
1101+ function test_clamp_negative_greater_switched() {
1102+ var minValue = -9
1103+ var maxValue = -42
1104+ var clampValue = 50
1105+
1106+ ignoreWarning("MathUtils.clamp, min value should not be bigger than the max value")
1107+
1108+ var clamped = MathUtils.clamp(clampValue, minValue, maxValue)
1109+ compare(clamped, minValue, "clamped value not within range")
1110+ }
1111+
1112+ function test_clamp_postive_and_negative_greater_switched() {
1113+ var minValue = 9
1114+ var maxValue = -42
1115+ var clampValue = 50
1116+
1117+ ignoreWarning("MathUtils.clamp, min value should not be bigger than the max value")
1118+
1119+ var clamped = MathUtils.clamp(clampValue, minValue, maxValue)
1120+ compare(clamped, minValue, "clamped value not within range")
1121+ }
1122+
1123 function test_lerp() {
1124 var lerped = MathUtils.lerp(0.25, 90, 0)
1125 compare(lerped, 67.5)
1126
1127=== modified file 'ubuntu-sdk.pro'
1128--- ubuntu-sdk.pro 2015-11-05 11:00:10 +0000
1129+++ ubuntu-sdk.pro 2015-11-20 07:47:28 +0000
1130@@ -27,7 +27,7 @@
1131 QMAKE_EXTRA_TARGETS += license
1132
1133 check.target = check
1134-check.commands = $$PWD/tests/checkresults.sh $$PWD/tests/test_tst_*.xml
1135+check.commands = $$PWD/tests/checkresults.sh $$OUT_PWD/tests/test_tst_*.xml
1136
1137 #helper files
1138 OTHER_FILES += \

Subscribers

People subscribed via source and target branches