1/**************************************************************************************** 2 * Copyright (c) 2017 Malte Veerman <malte.veerman@gmail.com> * 3 * * 4 * This program is free software; you can redistribute it and/or modify it under * 5 * the terms of the GNU General Public License as published by the Free Software * 6 * Foundation; either version 2 of the License, or (at your option) any later * 7 * version. * 8 * * 9 * This program is distributed in the hope that it will be useful, but WITHOUT ANY * 10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * 11 * PARTICULAR PURPOSE. See the GNU General Public License for more details. * 12 * * 13 * You should have received a copy of the GNU General Public License along with * 14 * this program. If not, see <http://www.gnu.org/licenses/>. * 15 ****************************************************************************************/ 16 17import QtQuick 2.4 18import QtQuick.Controls 1.4 19import QtQml.Models 2.1 20import QtQuick.Layouts 1.3 21import org.kde.kirigami 2.0 as Kirigami 22 23 24Rectangle { 25 id: root 26 27 readonly property alias configEnabled: configureButton.checked 28 property var addItem 29 property var flickable 30 property var contextRoot 31 32 function resizeApplets() { 33 var items = []; 34 var children = toolbarAppletRow.contentItem.visibleChildren; 35 for (var i=0; i<children.length; i++) { 36 if (!!children[i].status && children[i].status != Loader.Error) 37 items.push(children[i]); 38 } 39 if (items.length > 0) { 40 var space = toolbarAppletRow.width - (items.length - 1) * toolbarAppletRow.spacing; 41 var threshold = space / items.length; 42 var smallApplets = []; 43 var largeApplets = []; 44 for (var i=0; i<items.length; i++) { 45 if (!!items[i] && !!items[i].status && items[i].status != Loader.Error) 46 smallApplets.push(items[i]); 47 } 48 while (smallApplets.length > 0 && space > 0) { 49 var countSmallApplets = smallApplets.length; 50 smallApplets.forEach(function (applet, index) { 51 if (applet.implicitWidth >= threshold) { 52 largeApplets.push(applet); 53 applet.width = applet.implicitWidth; 54 space -= applet.width; 55 } 56 }); 57 smallApplets = smallApplets.filter(function (applet) { return largeApplets.indexOf(applet) == -1 }); 58 if (countSmallApplets == smallApplets.length) { 59 smallApplets.forEach(function (applet, index) { 60 applet.width = space / countSmallApplets; 61 }); 62 return; 63 } 64 threshold = space / smallApplets.length; 65 } 66 if (smallApplets.length == 0) return; 67 for (var i=0; i<smallApplets.length; i++) { 68 var applet = smallApplets[i]; 69 applet.width = applet.implicitWidth; 70 } 71 } 72 } 73 74 height: configureButton.height + Kirigami.Units.smallSpacing 75 radius: 4 76 color: palette.mid 77 78 ListView { 79 id: toolbarAppletRow 80 81 anchors { 82 top: parent.top 83 bottom: parent.bottom 84 left: parent.left 85 right: configureButton.left 86 leftMargin: spacing 87 rightMargin: spacing 88 } 89 orientation: ListView.Horizontal 90 spacing: Kirigami.Units.smallSpacing 91 interactive: false 92 clip: true 93 94 model: AppletProxyModel 95 96 delegate: Loader { 97 active: true 98 anchors.verticalCenter: parent.verticalCenter 99 100 Component.onCompleted: { 101 setSource("AppletToolbarAppletItem.qml", { 102 "name": name, 103 "appletId": appletId, 104 "toolbar": root, 105 "flickable": root.flickable, 106 "contextRoot": root.contextRoot 107 }); 108 root.resizeApplets(); 109 } 110 onStatusChanged: { 111 if (status == Loader.Error) { 112 Context.error("Error loading toolbar item for applet: " + name); 113 Context.error(sourceComponent.errorString()); 114 } 115 } 116 } 117 onWidthChanged: root.resizeApplets() 118 onCountChanged: root.resizeApplets() 119 } 120 121 ToolButton { 122 id: configureButton 123 124 anchors.verticalCenter: parent.verticalCenter 125 anchors.right: parent.right 126 anchors.margins: (parent.height - height) / 2 127 iconName: "configure" 128 checkable: true 129 tooltip: i18n( "Configure Applets..." ) 130 } 131 132 SystemPalette { 133 id: palette 134 } 135} 136