Warning, /pim/kube/framework/qml/ConversationListView.qml is written in an unsupported language. File is not indexed.

0001 /*
0002  *  Copyright (C) 2016 Michael Bohlender, <michael.bohlender@kdemail.net>
0003  *  Copyright (C) 2017 Christian Mollekopf, <mollekopf@kolabsystems.com>
0004  *
0005  *  This program is free software; you can redistribute it and/or modify
0006  *  it under the terms of the GNU General Public License as published by
0007  *  the Free Software Foundation; either version 2 of the License, or
0008  *  (at your option) any later version.
0009  *
0010  *  This program is distributed in the hope that it will be useful,
0011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0013  *  GNU General Public License for more details.
0014  *
0015  *  You should have received a copy of the GNU General Public License along
0016  *  with this program; if not, write to the Free Software Foundation, Inc.,
0017  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
0018  */
0019 
0020 import QtQuick 2.7
0021 import QtQuick.Controls 2
0022 import QtQuick.Layouts 1.1
0023 import org.kube.framework 1.0 as Kube
0024 
0025 import QtQml 2.2 as QtQml
0026 
0027 FocusScope {
0028     id: root
0029     property alias model: repeater.model
0030     property alias delegate: repeater.delegate
0031     property alias count: repeater.count
0032     property alias contentHeight: flickable.contentHeight
0033     property int currentIndex: -1
0034 
0035     //We want to avoid interfering with scrolling as soon as the user starts to scroll. This is important if i.e. an html mail loads slowly.
0036     //However, we have to maintain position as the initial items expand, so we have to react to contentHeight changes. scrollToEnd ensures both.
0037     property bool scrollToEnd: true
0038 
0039     property var currentItem: null
0040 
0041     function scrollDown() {
0042         scrollHelper.scrollDown()
0043     }
0044 
0045     function scrollUp() {
0046         scrollHelper.scrollUp()
0047     }
0048 
0049     function setCurrentItem() {
0050         if (currentItem) {
0051             currentItem.isCurrentItem = false
0052         }
0053         if (currentIndex >= 0 && activeFocus) {
0054             var item = repeater.itemAt(currentIndex)
0055             if (item) {
0056                 item.isCurrentItem = true
0057                 currentItem = item
0058             }
0059         } else {
0060             currentItem = null
0061         }
0062     }
0063 
0064     onCurrentIndexChanged: {
0065         setCurrentItem()
0066     }
0067 
0068     onActiveFocusChanged: {
0069         setCurrentItem()
0070     }
0071 
0072     function incrementCurrentIndex() {
0073         flickable.incrementCurrentIndex()
0074     }
0075 
0076     function decrementCurrentIndex() {
0077         flickable.decrementCurrentIndex()
0078     }
0079 
0080     Flickable {
0081         id: flickable
0082         anchors.fill: parent
0083 
0084         //Optimize for view quality
0085         pixelAligned: true
0086 
0087         contentWidth: width
0088         contentHeight: col.height
0089 
0090         function scrollToIndex(index) {
0091             var item = repeater.itemAt(index)
0092             if (item) {
0093                 scrollToPos(item.y)
0094             }
0095         }
0096 
0097         function scrollToPos(pos) {
0098             var scrollToEndPos = (flickable.contentHeight - flickable.height)
0099             //Avoid scrolling past the end
0100             if (pos < scrollToEndPos) {
0101                 flickable.contentY = pos
0102             } else {
0103                 flickable.contentY = scrollToEndPos
0104             }
0105         }
0106 
0107         onMovementStarted: {
0108             root.scrollToEnd = false
0109         }
0110 
0111         onContentHeightChanged: {
0112             if (repeater.count && root.scrollToEnd) {
0113                 //Scroll to the last item
0114                 root.currentIndex = repeater.count - 1
0115                 flickable.scrollToIndex(root.currentIndex)
0116             }
0117         }
0118 
0119         Column {
0120             id: col
0121             width: parent.width
0122             spacing: 2
0123             Repeater {
0124                 id: repeater
0125                 onItemAdded: {
0126                     root.scrollToEnd = true
0127                     flickable.scrollToIndex(root.currentIndex)
0128                 }
0129             }
0130         }
0131 
0132         function incrementCurrentIndex() {
0133             if (currentIndex < repeater.count - 1) {
0134                 currentIndex = currentIndex + 1
0135             }
0136             scrollToIndex(currentIndex)
0137         }
0138 
0139         function decrementCurrentIndex() {
0140             if (currentIndex > 0) {
0141                 currentIndex = currentIndex - 1
0142             }
0143             scrollToIndex(currentIndex)
0144         }
0145 
0146         Keys.onPressed: {
0147             if (event.matches(StandardKey.MoveToNextLine)) {
0148                 scrollHelper.scrollDown()
0149             } else if (event.matches(StandardKey.MoveToPreviousLine)) {
0150                 scrollHelper.scrollUp()
0151             }
0152         }
0153 
0154         Kube.ScrollHelper {
0155             id: scrollHelper
0156             flickable: flickable
0157             anchors.fill: parent
0158         }
0159 
0160         //Intercept all scroll events,
0161         //necessary due to the webengineview
0162         Kube.MouseProxy {
0163             anchors.fill: parent
0164             target: scrollHelper
0165             forwardWheelEvents: true
0166         }
0167 
0168         ScrollBar.vertical: Kube.ScrollBar {}
0169 
0170     }
0171 }