summaryrefslogtreecommitdiff
path: root/src/controls/Styles/Base/MenuStyle.qml
blob: 13a889c419d1150bde144929c122c24f29cde53a (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
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Quick Controls 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.Window 2.1
import QtQuick.Controls 1.2
import QtQuick.Controls.Private 1.0

/*!
    \qmltype MenuStyle
    \inqmlmodule QtQuick.Controls.Styles
    \since 5.3
    \ingroup controlsstyling
    \brief Provides custom styling for Menu.

    \target styleData properties
    The \b styleData object contains the following read-only properties:
    \table
        \row \li \b {styleData.index} : int \li The index of the menu item in its menu.
        \row \li \b {styleData.type} : enumeration \li The type of menu item. See below for possible values.
        \row \li \b {styleData.selected} : bool \li \c true if the menu item is selected.
        \row \li \b {styleData.pressed} : bool \li \c true if the menu item is pressed. Available since 5.4.
        \row \li \b {styleData.text} : string \li The menu item's text, or title if it's a submenu.
        \row \li \b {styleData.underlineMnemonic} : bool \li Whether the style should underline the menu item's label mnemonic.
        \row \li \b {styleData.shortcut} : string \li The text for the menu item's shortcut.
        \row \li \b {styleData.iconSource} : url \li The source URL to the menu item's icon. Undefined if it has no icon.
        \row \li \b {styleData.enabled} : bool \li \c true if the menu item is enabled.
        \row \li \b {styleData.checkable} : bool \li \c true if the menu  item is checkable.
        \row \li \b {styleData.exclusive} : bool \li \c true if the menu item is checkable, and it's part of an \l ExclusiveGroup.
        \row \li \b {styleData.checked} : bool \li \c true if the menu item is checkable and currently checked.
        \row \li \b {styleData.scrollerDirection} : enumeration \li If the menu item is a scroller, its pointing direction.
                                                                     Valid values are \c Qt.UpArrow, \c Qt.DownArrow, and \c Qt.NoArrow.
    \endtable

    The valid values for \b {styleData.type} are:
    \list
    \li MenuItemType.Item
    \li MenuItemType.Menu
    \li MenuItemType.Separator
    \li MenuItemType.ScrollIndicator
    \endlist

    \note Styling menus may not be supported on platforms using native menus
    through their QPA plugin.
*/

Style {
    id: styleRoot

    padding {
        top: 1
        bottom: 1
        left: 1
        right: 1
    }

    /*! The amount of pixels by which a submenu popup overlaps horizontally its parent menu. */
    property int submenuOverlap: 1

    /*! The number of milliseconds to wait before opening a submenu. */
    property int submenuPopupDelay: 200

    /*!
        \qmlmethod string MenuStyle::formatMnemonic(string text, bool underline = false)
        Returns a rich-text string to render mnemonics for a given menu item.

        The mnemonic character is prefixed by an ampersand in the original string.

        Passing \c true for \c underline will underline the mnemonic character (e.g.,
        \c formatMnemonic("&Open...", true) will return \c "<u>O</u>pen..."). Passing \c false
        for \c underline will return the plain text form (e.g., \c formatMnemonic("&Open...", false)
        will return \c "Open...").

        \sa Label
    */
    function formatMnemonic(text, underline) {
        return underline ? StyleHelpers.stylizeMnemonics(text) : StyleHelpers.removeMnemonics(text)
    }

    /*! The background frame for the menu popup.

        The \l Menu will resize the frame to its contents plus the padding.
    */
    property Component frame: Rectangle {
        color: styleRoot.__backgroundColor
        border { width: 1; color: styleRoot.__borderColor }
    }

    /*! \qmlproperty Object MenuStyle::itemDelegate

        The object containing the menu item subcontrol components. These subcontrols are used
        for normal menu items only, i.e. not for separators or scroll indicators.

        The subcontrols are:

        \list
        \li \b {itemDelegate.background} : Component

        The menu item background component.

        Its appearance generally changes with \l {styleData properties} {styleData.selected}
        and \l {styleData properties} {styleData.enabled}.

        The default implementation shows only when the item is enabled and selected. It remains
        invisible otherwise.

        \li \b {itemDelegate.label} : Component

        Component for the actual text label.

        The text itself is fetched from \l {styleData properties} {styleData.text}, and its appearance should depend
        on \l {styleData properties} {styleData.enabled} and \l {styleData properties} {styleData.selected}.

        If \l {styleData properties} {styleData.underlineMnemonic} is true, the label should underline its mnemonic
        character. \l formatMnemonic provides the default formatting.

        \li \b {itemDelegate.submenuIndicator} : Component

        It indicates that the current menu item is a submenu.

        Only used when \l {styleData properties} {styleData.type} equals \c MenuItemType.Menu.

        \li \b {itemDelegate.shortcut} : Component

        Displays the shortcut attached to the menu item.

        Only used when \l {styleData properties} {styleData.shortcut} is not empty.

        \li \b {itemDelegate.checkmarkIndicator} : Component

        Will be used when \l {styleData properties} {styleData.checkable} is \c true and its appearance
        may depend on \l {styleData properties} {styleData.exclusive}, i.e., whether it will behave like a
        checkbox or a radio button. Use \l {styleData properties} {styleData.checked} for the checked state.
        \endlist

        \note This property cannot be overwritten although all of the subcontrol properties can.
    */
    property alias itemDelegate: internalMenuItem

    MenuItemSubControls {
        id: internalMenuItem

        background: Rectangle {
            visible: styleData.selected && styleData.enabled
            gradient: Gradient {
                id: selectedGradient
                GradientStop { color: Qt.lighter(__selectedBackgroundColor, 1.3); position: -0.2 }
                GradientStop { color: __selectedBackgroundColor; position: 1.4 }
            }

            border.width: 1
            border.color: Qt.darker(__selectedBackgroundColor, 1)
            antialiasing: true
        }

        label: Text {
            text: formatMnemonic(styleData.text, styleData.underlineMnemonic)
            color: __currentTextColor
            font: styleRoot.font
            renderType: Settings.isMobile ? Text.QtRendering : Text.NativeRendering
        }

        submenuIndicator: Text {
            text: __mirrored ? "\u25c2" : "\u25b8" // BLACK LEFT/RIGHT-POINTING SMALL TRIANGLE
            font: styleRoot.font
            color: __currentTextColor
            style: styleData.selected ? Text.Normal : Text.Raised
            styleColor: Qt.lighter(color, 4)
            renderType: Settings.isMobile ? Text.QtRendering : Text.NativeRendering
        }

        shortcut: Text {
            text: styleData.shortcut
            font {
                bold: styleRoot.font.bold
                capitalization: styleRoot.font.capitalization
                family: styleRoot.font.family
                italic: styleRoot.font.italic
                letterSpacing: styleRoot.font.letterSpacing
                pixelSize: styleRoot.font.pixelSize * 0.9
                strikeout: styleRoot.font.strikeout
                underline: styleRoot.font.underline
                weight: styleRoot.font.weight
                wordSpacing: styleRoot.font.wordSpacing
            }
            color: __currentTextColor
            renderType: Settings.isMobile ? Text.QtRendering : Text.NativeRendering
        }

        checkmarkIndicator: Loader {
            sourceComponent: styleData.exclusive ? exclusiveCheckMark : nonExclusiveCheckMark
            Component {
                id: exclusiveCheckMark
                Rectangle {
                    x: 1
                    width: 10
                    height: 10
                    color: "white"
                    border.color: "gray"
                    antialiasing: true
                    radius: height/2

                    Rectangle {
                        anchors.centerIn: parent
                        visible: styleData.checked
                        width: 4
                        height: 4
                        color: "#666"
                        border.color: "#222"
                        antialiasing: true
                        radius: height/2
                    }
                }
            }

            Component {
                id: nonExclusiveCheckMark
                BorderImage {
                    width: 12
                    height: 12
                    source: "images/editbox.png"
                    border.top: 6
                    border.bottom: 6
                    border.left: 6
                    border.right: 6

                    Rectangle {
                        antialiasing: true
                        visible: styleData.checked
                        color: "#666"
                        radius: 1
                        anchors.margins: 4
                        anchors.fill: parent
                        border.color: "#222"
                        Rectangle {
                            anchors.fill: parent
                            anchors.margins: 1
                            color: "transparent"
                            border.color: "#33ffffff"
                        }
                    }
                }
            }
        }
    }

    /*! Component for the separator menu item.

        Will be used when \l {styleData properties} {styleData.type} equals \c MenuItemType.Separator.
    */
    property Component separator: Item {
        implicitHeight: styleRoot.font.pixelSize / 2
        Rectangle {
            width: parent.width - 2
            height: 1
            x: 1
            anchors.verticalCenter: parent.verticalCenter
            color: "darkgray"
        }
    }

    /*! Component for the scroll indicator menu item.

        Will be used when \l {styleData properties} {styleData.type} equals \c MenuItemType.ScrollIndicator.
        Its appearance should follow \l {styleData properties} {styleData.scrollerDirection}.

        This is the item added at the top and bottom of the menu popup when its contents won't fit the screen
        to indicate more content is available in the direction of the arrow.
    */
    property Component scrollIndicator: Image {
        anchors.centerIn: parent
        source: styleData.scrollerDirection === Qt.UpArrow ? "images/arrow-up.png" : "images/arrow-down.png"
    }

    /*!
        \since QtQuick.Controls.Styles 1.3
        The font of the control.
    */
    property font font

    /*! \internal */
    property string __menuItemType: "menuitem"

    /*! \internal
        The menu popup frame background color.

        This is set to be a uniform background. If you want a gradient or a pixmap,
        you should override \l frame.

        \sa frame, borderColor
    */
    property color __backgroundColor: "#dcdcdc"

    /*! \internal
        The menu popup frame border color.

        The border width is set to 1 pixel. Override \l frame if you want a larger border.

        \sa frame, backgroundColor
    */
    property color __borderColor: "darkgray"

    /*! \internal
        The maximum height for a popup before it will show scrollers.
    */
    property int __maxPopupHeight: 600

    /*! \internal
        The menu item background color when selected.

        This property is provided for convenience and only sets the color.
        It does not change the style in any other way.
    */
    property color __selectedBackgroundColor: "#49d"

    /*! \internal
        The menu item label color.

        When set, keyboard shorcuts get the same color as the item's text.

        \sa selectedLabelColor, disabledLabelColor
    */
    property color __labelColor: "#444"

    /*! \internal
        The menu item label color when selected.

        \sa labelColor, selectedLabelColor
    */
    property color __selectedLabelColor: "white"

    /*! \internal
        The menu item label color when disabled.

        \sa labelColor, disabledLabelColor
    */
    property color __disabledLabelColor: "gray"


    /*! \internal */
    readonly property bool __mirrored: Qt.application.layoutDirection === Qt.RightToLeft

    /*! \internal
        The margin between the frame and the menu item label's left side.

        Generally, this should be large enough to fit optional checkmarks on
        the label's left side.
    */
    property int __leftLabelMargin: 18

    /*! \internal
        The margin between the menu item label's right side and the frame. */
    property int __rightLabelMargin: 12

    /*! \internal
        The minimum spacing between the menu item label's text right side and any
        element located on its right (submenu indicator or shortcut).
    */
    property int __minRightLabelSpacing: 28

    /*! \internal */
    property Component __scrollerStyle: null

    /*! \internal
        The menu item contents itself.

        The default implementation uses \l MenuItemStyle.
    */
    property Component menuItemPanel: Item {
        id: panel

        property QtObject __styleData: styleData
        /*! \internal
            The current color of the text label.

            Use this if you're overriding e.g. \l shortcutIndicator to keep the color matched
            with \l label, or to derive new colors from it.
        */
        property color currentTextColor: !styleData.enabled ? __disabledLabelColor :
                                         styleData.selected ? __selectedLabelColor : __labelColor

        implicitWidth: Math.max((parent ? parent.width : 0),
                                Math.round(__leftLabelMargin + labelLoader.width + __rightLabelMargin +
                                           (rightIndicatorLoader.active ? __minRightLabelSpacing + rightIndicatorLoader.width : 0)))
        implicitHeight: Math.round(styleData.type === MenuItemType.Separator ? separatorLoader.implicitHeight :
                                   !!styleData.scrollerDirection ? styleRoot.font.pixelSize * 0.75 : labelLoader.height + 4)

        Loader {
            property alias styleData: panel.__styleData
            property alias __currentTextColor: panel.currentTextColor
            anchors.fill: parent
            sourceComponent: itemDelegate.background
        }

        Loader {
            id: separatorLoader
            property alias styleData: panel.__styleData
            property alias __currentTextColor: panel.currentTextColor
            anchors.fill: parent
            sourceComponent: separator
            active: styleData.type === MenuItemType.Separator
        }

        Loader {
            property alias styleData: panel.__styleData
            property alias __currentTextColor: panel.currentTextColor
            x: __mirrored ? parent.width - width - 4 : 4
            anchors.verticalCenterOffset: -1
            anchors.verticalCenter: parent.verticalCenter
            active: __menuItemType === "menuitem" && styleData.checkable
            sourceComponent: itemDelegate.checkmarkIndicator
        }

        Loader {
            id: labelLoader
            readonly property real offset: __menuItemType === "menuitem" ? __leftLabelMargin : 6
            property alias styleData: panel.__styleData
            property alias __currentTextColor: panel.currentTextColor
            x: __mirrored ? parent.width - width - offset : offset
            y: 1
            active: styleData.type !== MenuItemType.Separator
            sourceComponent: itemDelegate.label
            baselineOffset: item ? item.baselineOffset : 0.0
        }

        Loader {
            id: rightIndicatorLoader
            property alias styleData: panel.__styleData
            property alias __currentTextColor: panel.currentTextColor
            active: styleData.type === MenuItemType.Menu || styleData.shortcut !== ""
            sourceComponent: styleData.type === MenuItemType.Menu ? itemDelegate.submenuIndicator : itemDelegate.shortcut
            LayoutMirroring.enabled: __mirrored
            baselineOffset: item ? item.baselineOffset : 0.0
            anchors {
                right: parent.right
                rightMargin: 6
                baseline: !styleData.isSubmenu ? labelLoader.baseline : undefined
            }
        }
    }
}