Warning, /multimedia/amarok/src/context/context_qml_package/contents/ui/toolbar/AppletToolbar.qml is written in an unsupported language. File is not indexed.

0001 /****************************************************************************************
0002  * Copyright (c) 2017 Malte Veerman <malte.veerman@gmail.com>                           *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 import QtQuick 2.4
0018 import QtQuick.Controls 1.4
0019 import QtQml.Models 2.1
0020 import QtQuick.Layouts 1.3
0021 import org.kde.kirigami 2.0 as Kirigami
0022 
0023 
0024 Rectangle {
0025     id: root
0026 
0027     readonly property alias configEnabled: configureButton.checked
0028     property var addItem
0029     property var flickable
0030     property var contextRoot
0031 
0032     function resizeApplets() {
0033         var items = [];
0034         var children = toolbarAppletRow.contentItem.visibleChildren;
0035         for (var i=0; i<children.length; i++) {
0036             if (!!children[i].status && children[i].status != Loader.Error)
0037                 items.push(children[i]);
0038         }
0039         if (items.length > 0) {
0040             var space = toolbarAppletRow.width - (items.length - 1) * toolbarAppletRow.spacing;
0041             var threshold = space / items.length;
0042             var smallApplets = [];
0043             var largeApplets = [];
0044             for (var i=0; i<items.length; i++) {
0045                 if (!!items[i] && !!items[i].status && items[i].status != Loader.Error)
0046                     smallApplets.push(items[i]);
0047             }
0048             while (smallApplets.length > 0 && space > 0) {
0049                 var countSmallApplets = smallApplets.length;
0050                 smallApplets.forEach(function (applet, index) {
0051                     if (applet.implicitWidth >= threshold) {
0052                         largeApplets.push(applet);
0053                         applet.width = applet.implicitWidth;
0054                         space -= applet.width;
0055                     }
0056                 });
0057                 smallApplets = smallApplets.filter(function (applet) { return largeApplets.indexOf(applet) == -1 });
0058                 if (countSmallApplets == smallApplets.length) {
0059                     smallApplets.forEach(function (applet, index) {
0060                         applet.width = space / countSmallApplets;
0061                     });
0062                     return;
0063                 }
0064                 threshold = space / smallApplets.length;
0065             }
0066             if (smallApplets.length == 0) return;
0067             for (var i=0; i<smallApplets.length; i++) {
0068                 var applet = smallApplets[i];
0069                 applet.width = applet.implicitWidth;
0070             }
0071         }
0072     }
0073 
0074     height: configureButton.height + Kirigami.Units.smallSpacing
0075     radius: 4
0076     color: palette.mid
0077 
0078     ListView {
0079         id: toolbarAppletRow
0080 
0081         anchors {
0082             top: parent.top
0083             bottom: parent.bottom
0084             left: parent.left
0085             right: configureButton.left
0086             leftMargin: spacing
0087             rightMargin: spacing
0088         }
0089         orientation: ListView.Horizontal
0090         spacing: Kirigami.Units.smallSpacing
0091         interactive: false
0092         clip: true
0093 
0094         model: AppletProxyModel
0095 
0096         delegate: Loader {
0097             active: true
0098             anchors.verticalCenter: parent.verticalCenter
0099 
0100             Component.onCompleted: {
0101                 setSource("AppletToolbarAppletItem.qml", {
0102                     "name": name,
0103                     "appletId": appletId,
0104                     "toolbar": root,
0105                     "flickable": root.flickable,
0106                     "contextRoot": root.contextRoot
0107                 });
0108                 root.resizeApplets();
0109             }
0110             onStatusChanged: {
0111                 if (status == Loader.Error) {
0112                     Context.error("Error loading toolbar item for applet: " + name);
0113                     Context.error(sourceComponent.errorString());
0114                 }
0115             }
0116         }
0117         onWidthChanged: root.resizeApplets()
0118         onCountChanged: root.resizeApplets()
0119     }
0120 
0121     ToolButton {
0122         id: configureButton
0123 
0124         anchors.verticalCenter: parent.verticalCenter
0125         anchors.right: parent.right
0126         anchors.margins: (parent.height - height) / 2
0127         iconName: "configure"
0128         checkable: true
0129         tooltip: i18n( "Configure Applets..." )
0130     }
0131 
0132     SystemPalette {
0133         id: palette
0134     }
0135 }