summaryrefslogtreecommitdiff
path: root/src/extras/PieMenu.qml
blob: cfaaecbe7fc58e5d88d5d4a5d4c5db3b33d93106 (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
275
276
277
278
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
337
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
377
378
379
380
381
382
383
384
385
386
387
388
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
490
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Quick Extras module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

import QtQuick 2.2
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4
import QtQuick.Controls.Private 1.0
import QtQuick.Extras 1.4
import QtQuick.Extras.Private 1.0
import QtQuick.Extras.Private.CppUtils 1.0 as CppUtils

/*!
    \qmltype PieMenu
    \inqmlmodule QtQuick.Extras
    \since 5.5
    \ingroup extras
    \ingroup extras-interactive
    \brief A popup menu that displays several menu items along an arc.

    \image piemenu.png A PieMenu

    The PieMenu provides a radial context menu as an alternative to a
    traditional menu. All of the items in a PieMenu are an equal distance
    from the center of the control.

    \section2 Populating the Menu

    To create a menu, define at least one MenuItem as a child of it:
    \code
    PieMenu {
        id: pieMenu

        MenuItem {
            text: "Action 1"
            onTriggered: print("Action 1")
        }
        MenuItem {
            text: "Action 2"
            onTriggered: print("Action 2")
        }
        MenuItem {
            text: "Action 3"
            onTriggered: print("Action 3")
        }
    }
    \endcode

    By default, only the currently selected item's text is displayed above the
    menu. To provide text that is always visible when there is no current item,
    set the \l title property.

    \section2 Displaying the Menu

    The typical use case for a menu is to open at the point of the mouse
    cursor after a right click occurs. To do that, define a MouseArea that
    covers the region upon which clicks should open the menu. When the
    MouseArea is right-clicked, call the popup() function:
    \code
    MouseArea {
        anchors.fill: parent
        acceptedButtons: Qt.RightButton

        onClicked: pieMenu.popup(mouseX, mouseY)
    }
    \endcode

    If the menu is opened in a position where some of its menu items would be
    outside of \l boundingItem, it is automatically moved to a position where
    they will not be hidden. By default, the boundingItem is set to the parent
    of the menu. It can also be set to \c null to prevent this behavior.

    PieMenu can be displayed at any position on the screen. With a traditional
    context menu, the menu would be positioned with its top left corner at the
    position of the right click, but since PieMenu is radial, we position it
    centered over the position of the right click.

    To create a PieMenu that opens after a long press and selects items upon
    releasing, you can combine ActivationMode.ActivateOnRelease with a
    MouseArea using a Timer:
    \code
    MouseArea {
        id: touchArea
        anchors.fill: parent

        Timer {
            id: pressAndHoldTimer
            interval: 300
            onTriggered: pieMenu.popup(touchArea.mouseX, touchArea.mouseY);
        }

        onPressed: pressAndHoldTimer.start()
        onReleased: pressAndHoldTimer.stop();
    }

    PieMenu {
        id: pieMenu

        triggerMode: TriggerMode.TriggerOnRelease

        MenuItem {
            text: "Action 1"
            onTriggered: print("Action 1")
        }
        MenuItem {
            text: "Action 2"
            onTriggered: print("Action 2")
        }
        MenuItem {
            text: "Action 3"
            onTriggered: print("Action 3")
        }
    }
    \endcode

    You can hide individual menu items by setting their visible property to
    \c false. Hiding items does not affect the
    \l {PieMenuStyle::}{startAngle} or
    \l {PieMenuStyle::}{endAngle}; the
    remaining items will grow to consume the available space.

    You can create a custom appearance for a PieMenu by assigning a \l {PieMenuStyle}
*/

Control {
    id: pieMenu
    visible: false

    style: Settings.styleComponent(Settings.style, "PieMenuStyle.qml", pieMenu)

    /*!
        This property reflects the angle (in radians) created by the imaginary
        line from the center of the menu to the position of the cursor.

        Its value is undefined when the menu is not visible.
    */
    readonly property real selectionAngle: {
        var centerX = width / 2;
        var centerY = height / 2;
        var targetX = __protectedScope.selectionPos.x;
        var targetY = __protectedScope.selectionPos.y;

        var xDistance = centerX - targetX;
        var yDistance = centerY - targetY;

        var angleToTarget = Math.atan2(xDistance, yDistance) * -1;
        angleToTarget;
    }

    /*!
        \qmlproperty enumeration PieMenu::activationMode

        This property determines the method for selecting items in the menu.

        \list
        \li An activationMode of \a ActivationMode.ActivateOnPress means that menu
        items will only be selected when a mouse press event occurs over them.

        \li An activationMode of \a ActivationMode.ActivateOnRelease means that menu
        items will only be selected when a mouse release event occurs over them.
        This means that the user must keep the mouse button down after opening
        the menu and release the mouse over the item they wish to select.

        \li An activationMode of \a ActivationMode.ActivateOnClick means that menu
        items will only be selected when the user clicks once over them.
        \endlist

        \warning Changing the activationMode while the menu is visible will
        result in undefined behavior.

        \deprecated Use triggerMode instead.
    */
    property alias activationMode: pieMenu.triggerMode

    /*!
        \qmlproperty enumeration PieMenu::triggerMode

        This property determines the method for selecting items in the menu.

        \list
        \li A triggerMode of \a TriggerMode.TriggerOnPress means that menu
        items will only be selected when a mouse press event occurs over them.

        \li A triggerMode of \a TriggerMode.TriggerOnRelease means that menu
        items will only be selected when a mouse release event occurs over them.
        This means that the user must keep the mouse button down after opening
        the menu and release the mouse over the item they wish to select.

        \li A triggerMode of \a TriggerMode.TriggerOnClick means that menu
        items will only be selected when the user clicks once over them.
        \endlist

        \warning Changing the triggerMode while the menu is visible will
        result in undefined behavior.
    */
    property int triggerMode: TriggerMode.TriggerOnClick

    /*!
        \qmlproperty list<MenuItem> menuItems

        The list of menu items displayed by this menu.

        You can assign menu items by declaring them as children of PieMenu:
        \code
        PieMenu {
            MenuItem {
                text: "Action 1"
                onTriggered: function() { print("Action 1"); }
            }
            MenuItem {
                text: "Action 2"
                onTriggered: function() { print("Action 2"); }
            }
            MenuItem {
                text: "Action 3"
                onTriggered: function() { print("Action 3"); }
            }
        }
        \endcode
    */
    default property alias menuItems: defaultPropertyHack.menuItems

    QtObject {
        // Can't specify a list as a default property (QTBUG-10822)
        id: defaultPropertyHack
        property list<MenuItem> menuItems
    }

    /*!
        \qmlproperty int PieMenu::currentIndex

        The index of the the menu item that is currently under the mouse,
        or \c -1 if there is no such item.
    */
    readonly property alias currentIndex: protectedScope.currentIndex

    /*!
        \qmlproperty int PieMenu::currentItem

        The menu item that is currently under the mouse, or \c null if there is
        no such item.
    */
    readonly property alias currentItem: protectedScope.currentItem

    /*!
        This property defines the text that is shown above the menu when
        there is no current menu item (currentIndex is \c -1).

        The default value is \c "" (an empty string).
    */
    property string title: ""

    /*!
        The item which the menu must stay within.

        A typical use case for PieMenu involves:

        \list
            \li A MouseArea that determines the clickable area within which the
                menu can be opened.
            \li The bounds that the menu must not go outside of.
        \endlist

        Although they sound similar, they have different purposes. Consider the
        example below:

        \image piemenu-boundingItem-example.png Canvas boundingItem example

        The user can only open the menu within the inner rectangle. In this
        case, they've opened the menu on the edge of the MouseArea, but there
        would not be enough room to display the entire menu centered at the
        cursor position, so it was moved to the left.

        If for some reason we didn't want this restriction, we can set
        boundingItem to \c null:

        \image piemenu-boundingItem-null-example.png Canvas null boundingItem example

        By default, the menu's \l {Item::}{parent} is the boundingItem.
    */
    property Item boundingItem: parent

    /*!
        \qmlmethod void popup(real x, real y)

        Opens the menu at coordinates \a x, \a y.
     */
    function popup(x, y) {
        if (x !== undefined)
            pieMenu.x = x - pieMenu.width / 2;
        if (y !== undefined)
            pieMenu.y = y - pieMenu.height / 2;

        pieMenu.visible = true;
    }

    /*!
        \qmlmethod void addItem(string text)

        Adds a \a text item to the end of the menu items.

        Equivalent to passing calling \c insertItem(menuItems.length, text).

        Returns the newly added item.
    */
    function addItem(text) {
        return insertItem(menuItems.length, text);
    }

    /*!
        \qmlmethod void insertItem(int before, string text)

        Inserts a MenuItem with \a text before the index at \a before.

        To insert an item at the end, pass \c menuItems.length.

        Returns the newly inserted item, or \c null if \a before is invalid.
    */
    function insertItem(before, text) {
        if (before < 0 || before > menuItems.length) {
            return null;
        }

        var newItems = __protectedScope.copyItemsToJsArray();
        var newItem = Qt.createQmlObject("import QtQuick.Controls 1.1; MenuItem {}", pieMenu, "");
        newItem.text = text;
        newItems.splice(before, 0, newItem);

        menuItems = newItems;
        return newItem;
    }

    /*!
        \qmlmethod void removeItem(item)

        Removes \a item from the menu.
    */
    function removeItem(item) {
        for (var i = 0; i < menuItems.length; ++i) {
            if (menuItems[i] === item) {
                var newItems = __protectedScope.copyItemsToJsArray();

                newItems.splice(i, 1);
                menuItems = newItems;
                break;
            }
        }
    }

    MouseArea {
        id: mouseArea
        anchors.fill: parent
        hoverEnabled: !Settings.hasTouchScreen && triggerMode !== TriggerMode.TriggerOnRelease
        acceptedButtons: Qt.LeftButton | Qt.RightButton
        onContainsMouseChanged: if (!containsMouse) __protectedScope.currentIndex = -1
        objectName: "PieMenu internal MouseArea"

        // The mouse thief also updates the selectionPos, so we can't bind to
        // this mouseArea's mouseX/mouseY.
        onPositionChanged: {
            __protectedScope.selectionPos = Qt.point(mouseX, mouseY)
        }
    }

    /*! \internal */
    property alias __mouseThief: mouseThief

    CppUtils.MouseThief {
        id: mouseThief

        onPressed: {
            __protectedScope.selectionPos = Qt.point(mouseX, mouseY);
            if (__protectedScope.handleEvent(ActivationMode.ActivateOnPress)) {
                mouseThief.acceptCurrentEvent();
                // We handled the press event, so we can reset this now.
                mouseThief.receivedPressEvent = false;
            }
        }
        onReleased: {
            __protectedScope.selectionPos = Qt.point(mouseX, mouseY);
            if (__protectedScope.handleEvent(ActivationMode.ActivateOnRelease)) {
                mouseThief.acceptCurrentEvent();
                // We handled the press event, so we can reset this now.
                mouseThief.receivedPressEvent = false;
            }
            __protectedScope.pressedIndex = -1;
        }
        onClicked: {
            __protectedScope.selectionPos = Qt.point(mouseX, mouseY);
            if (__protectedScope.handleEvent(ActivationMode.ActivateOnClick)) {
                mouseThief.acceptCurrentEvent();
            }

            // Clicked is the last stage in a click event (press, release, click),
            // so we can safely set this to false now.
            mouseThief.receivedPressEvent = false;
        }
        onTouchUpdate: __protectedScope.selectionPos = Qt.point(mouseX, mouseY)
    }

    onVisibleChanged: {
        // parent check is for when it's created without a parent,
        // which we do in the tests, for example.
        if (parent) {
            if (visible) {
                if (boundingItem)
                    __protectedScope.moveWithinBounds();

                // We need to grab the mouse so that we can detect released()
                // (which is only emitted after pressed(), which our MouseArea can't
                // emit as it didn't have focus until we were made visible).
                mouseThief.grabMouse(mouseArea);
            } else {
                mouseThief.ungrabMouse();
                __protectedScope.selectionPos = Qt.point(width / 2, height / 2);
            }
        }
    }
    onSelectionAngleChanged: __protectedScope.checkForCurrentItem()

    /*! \internal */
    property QtObject __protectedScope: QtObject {
        id: protectedScope

        property int currentIndex: -1
        property MenuItem currentItem: currentIndex != -1 ? visibleItems[currentIndex] : null
        property point selectionPos: Qt.point(width / 2, height / 2)
        property int pressedIndex: -1
        readonly property var localRect: mapFromItem(mouseArea, mouseArea.mouseX, mouseArea.mouseY)
        readonly property var visibleItems: {
            var items = [];
            for (var i = 0; i < menuItems.length; ++i) {
                if (menuItems[i].visible) {
                    items.push(menuItems[i]);
                }
            }
            return items;
        }

        onSelectionPosChanged: __protectedScope.checkForCurrentItem()

        // Can't bind directly, because the menu sets this to (0, 0) on closing.
        onLocalRectChanged: {
            if (visible)
                selectionPos = Qt.point(localRect.x, localRect.y);
        }

        function copyItemsToJsArray() {
            var newItems = [];
            for (var j = 0; j < menuItems.length; ++j) {
                newItems.push(menuItems[j]);
            }
            return newItems;
        }

        /*!
            Returns \c true if the mouse is over the section at \a itemIndex.
        */
        function isMouseOver(itemIndex) {
            if (__style == null)
                return false;

            // Our mouse angle's origin is north naturally, but the section angles need to be
            // altered to have their origin north, so we need to remove the alteration here in order to compare properly.
            // For example, section 0 will start at -1.57, whereas we want it to start at 0.
            var sectionStart = __protectedScope.sectionStartAngle(itemIndex) + Math.PI / 2;
            var sectionEnd = __protectedScope.sectionEndAngle(itemIndex) + Math.PI / 2;

            var selAngle = selectionAngle;
            var isWithinOurAngle = false;

            if (sectionStart > CppUtils.MathUtils.pi2) {
                sectionStart %= CppUtils.MathUtils.pi2;
            } else if (sectionStart < -CppUtils.MathUtils.pi2) {
                sectionStart %= -CppUtils.MathUtils.pi2;
            }

            if (sectionEnd > CppUtils.MathUtils.pi2) {
                sectionEnd %= CppUtils.MathUtils.pi2;
            } else if (sectionEnd < -CppUtils.MathUtils.pi2) {
                sectionEnd %= -CppUtils.MathUtils.pi2;
            }

            // If the section crosses the -180 => 180 wrap-around point (from atan2),
            // temporarily rotate the section so it doesn't.
            if (sectionStart > Math.PI) {
                var difference = sectionStart - Math.PI;
                selAngle -= difference;
                sectionStart -= difference;
                sectionEnd -= difference;
            } else if (sectionStart < -Math.PI) {
                difference = Math.abs(sectionStart - (-Math.PI));
                selAngle += difference;
                sectionStart += difference;
                sectionEnd += difference;
            }

            if (sectionEnd > Math.PI) {
                difference = sectionEnd - Math.PI;
                selAngle -= difference;
                sectionStart -= difference;
                sectionEnd -= difference;
            } else if (sectionEnd < -Math.PI) {
                difference = Math.abs(sectionEnd - (-Math.PI));
                selAngle += difference;
                sectionStart += difference;
                sectionEnd += difference;
            }

            // If we moved the mouse past -180 or 180, we need to move it back within,
            // without changing its actual direction.
            if (selAngle > Math.PI) {
                selAngle = selAngle - CppUtils.MathUtils.pi2;
            } else if (selAngle < -Math.PI) {
                selAngle += CppUtils.MathUtils.pi2;
            }

            if (sectionStart > sectionEnd) {
                isWithinOurAngle = selAngle >= sectionEnd && selAngle < sectionStart;
            } else {
                isWithinOurAngle = selAngle >= sectionStart && selAngle < sectionEnd;
            }

            var x1 = width / 2;
            var y1 = height / 2;
            var x2 = __protectedScope.selectionPos.x;
            var y2 = __protectedScope.selectionPos.y;
            var distanceFromCenter = Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2);
            var cancelRadiusSquared = __style.cancelRadius * __style.cancelRadius;
            var styleRadiusSquared = __style.radius * __style.radius;
            var isWithinOurRadius = distanceFromCenter >= cancelRadiusSquared
                && distanceFromCenter < styleRadiusSquared;
            return isWithinOurAngle && isWithinOurRadius;
        }

        readonly property real arcRange: endAngleRadians - startAngleRadians

        /*!
            The size of one section in radians.
        */
        readonly property real sectionSize: arcRange / visibleItems.length
        readonly property real startAngleRadians: CppUtils.MathUtils.degToRadOffset(__style.startAngle)
        readonly property real endAngleRadians: CppUtils.MathUtils.degToRadOffset(__style.endAngle)

        readonly property real circumferenceOfFullRange: 2 * Math.PI * __style.radius
        readonly property real percentageOfFullRange: (arcRange / (Math.PI * 2))
        readonly property real circumferenceOfSection: (sectionSize / arcRange) * (percentageOfFullRange * circumferenceOfFullRange)

        function sectionStartAngle(section) {
            var start = startAngleRadians + section * sectionSize;
            return start;
        }

        function sectionCenterAngle(section) {
            return (sectionStartAngle(section) + sectionEndAngle(section)) / 2;
        }

        function sectionEndAngle(section) {
            var end = startAngleRadians + section * sectionSize + sectionSize;
            return end;
        }

        function handleEvent(eventType) {
            if (!visible)
                return false;

            checkForCurrentItem();

            if (eventType === TriggerMode.TriggerOnPress)
                pressedIndex = currentIndex;

            if (eventType === TriggerMode.TriggerOnPress && triggerMode === TriggerMode.TriggerOnClick) {
                // We *MUST* accept press events if we plan on also accepting the release
                // (aka click, since we create that ourselves) event. If we don't, the
                // external mouse area gets the press event but not the release event,
                // and won't open until a release event is received, which means until the
                // user taps twice on the external mouse area.
                // Usually, we accept the current event in the onX MouseThief event handlers above,
                // but there we set receivedPressEvent to false if this function says it handled
                // the event, which we don't want, since TriggerOnClick is expecting to have
                // received a press event. So, we ensure that receivedPressEvent stays true
                // by saying we didn't handle the event, even though we actually do.
                mouseThief.acceptCurrentEvent();
                return false;
            }

            if (triggerMode === eventType) {
                if (eventType === TriggerMode.TriggerOnClick && !mouseThief.receivedPressEvent) {
                    // When the trigger mode is TriggerOnClick, we can't
                    // act on a click event if we didn't receive the press.
                    return false;
                }

                // Setting visible to false resets the selectionPos to the center
                // of the menu, which in turn causes the currentItem check to be re-evaluated,
                // which sees that there's no current item because the selectionPos is centered.
                // To avoid all of that, we store these variables before setting visible to false.
                var currentItemBeforeClosing = currentItem;
                var selectionPosBeforeClosing = selectionPos;
                var currentIndexBeforeClosing = currentIndex;

                // If the cursor was over an item; trigger it. If it wasn't,
                // close our menu regardless. We do this first so that it's
                // possible to keep the menu open by setting visible to true in onTriggered.
                visible = false;

                if (currentItemBeforeClosing) {
                    currentItemBeforeClosing.trigger();
                }

                if (visible && !Settings.hasTouchScreen && !Settings.isMobile) {
                    // The user kept the menu open in onTriggered, so restore the hover stuff.
                    selectionPos = selectionPosBeforeClosing;
                    currentIndex = currentIndexBeforeClosing;
                }

                // If the trigger mode and event are Release, we should ensure
                // that we received a press event beforehand. If we didn't, we shouldn't steal
                // the event in MouseThief's event filter.
                return mouseThief.receivedPressEvent;
            }
            return false;
        }

        function checkForCurrentItem() {
            // Use a temporary varibable because setting currentIndex to -1 here
            // will trigger onCurrentIndexChanged.
            if (!!visibleItems) {
                var hoveredIndex = -1;
                for (var i = 0; i < visibleItems.length; ++i) {
                    if (isMouseOver(i)) {
                        hoveredIndex = i;
                        break;
                    }
                }
                currentIndex = hoveredIndex;
            }
        }

        function simplifyAngle(angle) {
            var simplified = angle % 360;
            if (simplified < 0)
                simplified += 360;
            return simplified;
        }

        function isWithinBottomEdge() {
            var start = simplifyAngle(pieMenu.__style.startAngle);
            var end = simplifyAngle(pieMenu.__style.endAngle);
            return start >= 270 && end <= 90 && ((start < 360 && end <= 360) || (start >= 0 && end > 0));
        }

        function isWithinTopEdge() {
            var start = simplifyAngle(pieMenu.__style.startAngle);
            var end = simplifyAngle(pieMenu.__style.endAngle);
            return start >= 90 && start < 270 && end > 90 && end <= 270;
        }

        function isWithinLeftEdge() {
            var start = simplifyAngle(pieMenu.__style.startAngle);
            var end = simplifyAngle(pieMenu.__style.endAngle);
            return (start === 360 || start >= 0) && start < 180 && end > 0 && end <= 180;
        }

        function isWithinRightEdge() {
            var start = simplifyAngle(pieMenu.__style.startAngle);
            var end = simplifyAngle(pieMenu.__style.endAngle);
            return start >= 180 && start < 360 && end > 180 && (end === 360 || end === 0);
        }

        /*!
            Moves the menu if it would open with parts outside of \a rootParent.
        */
        function moveWithinBounds() {
            // Find the bounding rect of the bounding item in the parent's referential.
            var topLeft = boundingItem.mapToItem(pieMenu.parent, 0, 0);
            var topRight = boundingItem.mapToItem(pieMenu.parent, boundingItem.width, 0);
            var bottomLeft = boundingItem.mapToItem(pieMenu.parent, 0, boundingItem.height);
            var bottomRight = boundingItem.mapToItem(pieMenu.parent, boundingItem.width, boundingItem.height);

            // If the boundingItem is rotated, normalize the bounding rect.
            topLeft.x = Math.min(topLeft.x, topRight.x, bottomLeft.x, bottomRight.x);
            topLeft.y = Math.min(topLeft.y, topRight.y, bottomLeft.y, bottomRight.y);
            bottomRight.x = Math.max(topLeft.x, topRight.x, bottomLeft.x, bottomRight.x);
            bottomRight.y = Math.max(topLeft.y, topRight.y, bottomLeft.y, bottomRight.y);

            if (pieMenu.x < topLeft.x && !isWithinLeftEdge()) {
                // The width and height of the menu is always that of a full circle,
                // so the menu is not always outside an edge when it's outside the edge -
                // it depends on the start and end angles.
                pieMenu.x = topLeft.x;
            } else if (pieMenu.x + pieMenu.width > bottomRight.x && !isWithinRightEdge()) {
                pieMenu.x = bottomRight.x - pieMenu.width;
            }

            if (pieMenu.y < topLeft.y && !isWithinTopEdge()) {
                pieMenu.y = topLeft.y;
            } else if (pieMenu.y + pieMenu.height > bottomRight.y && !isWithinBottomEdge()) {
                pieMenu.y = bottomRight.y - pieMenu.height;
            }
        }
    }
}