Files
iDescriptor/src/ui/DeviceGallery.qml
T
2026-05-10 18:19:52 +00:00

234 lines
6.8 KiB
QML

import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import iDescriptor 1.0
Item {
id: root
readonly property Query query : Query {}
property bool loading: true
required property var udid
property int albumId
property bool isMainPage : root.albumId == -1 ? false : root.albumId == -2 ? false : !root.albumId
Component.onCompleted: {
console.log("Calling init query")
query.init(root.udid);
}
ListModel {
id: albumModel
}
function selectItemsInRect(rect, append) {
for (let i = 0; i < gallery.count; i++) {
const item = gallery.itemAtIndex(i)
if (!item) continue
const itemRect = {
x: item.x,
y: item.y - gallery.contentY,
w: item.width,
h: item.height
}
const intersects =
itemRect.x < rect.x + rect.width &&
itemRect.x + itemRect.w > rect.x &&
itemRect.y < rect.y + rect.height &&
itemRect.y + itemRect.h > rect.y
if (intersects) {
albumModel.setProperty(i, "selected", true)
} else if (!append) {
albumModel.setProperty(i, "selected", false)
}
}
}
Connections {
target: query
function onStateChanged() {
console.log("state changed")
query.read_albums()
}
function onAlbumsChanged() {
console.log(JSON.stringify(query.albums))
albumModel.clear()
const keys = Object.keys(query.albums)
for (let i = 0; i < keys.length; i++) {
const key = keys[i]
const jsonStr = query.albums[key]
const obj = JSON.parse(jsonStr)
albumModel.append({
albumId : obj.album_id ? obj.album_id : -1,
fileName: key,
filePath: obj.file_path,
dateTime: new Date(),
fileType: 0,
selected: false,
thumbVersion: 0
})
}
}
}
Connections {
target : imageLoader
function onThumbnailReady(path, rowHint) {
const item = albumModel.get(rowHint)
if (item && item.filePath == path) {
albumModel.setProperty(rowHint, "thumbVersion", item.thumbVersion + 1)
}
}
}
BusyIndicator {
running: !query.albums
anchors.centerIn: parent
}
ColumnLayout {
anchors.fill : parent
Button {
text: isMainPage ? "BACK" : "BACK TO MAIN"
enabled : root.albumId != 0
onClicked : {
root.albumId = 0
}
}
GridView {
id: gallery
Layout.fillWidth: true
Layout.fillHeight: true
visible: albumId ? false : query.albums
interactive: true
// FIXME: only available in Qt 6.9
acceptedButtons : Qt.NoButton
cellWidth: 250
cellHeight: 250
model: albumModel
delegate: ItemDelegate {
// required property int index
// required property string filePath
// required property int albumId
width: 250
height: 250
highlighted: selected
MouseArea {
anchors.fill: parent
onDoubleClicked: {
console.log("delegate double-click", index, albumId)
root.albumId = albumId
}
}
Rectangle {
anchors.fill: parent
color: selected ? "#4FC3F7" : "transparent"
opacity : 0.3
z : 1
}
Image {
cache: false
anchors.fill: parent
//FIXME:use encodeuricomp
source: "image://thumb/" + filePath + "?udid=" + root.udid + "&index=" + index + "&v=" + thumbVersion
fillMode: Image.PreserveAspectFit
}
Text {
anchors.bottom: parent.bottom
anchors.left: parent.left
anchors.right: parent.right
text: fileName + albumId
font.pixelSize: 10
color: "white"
elide: Text.ElideMiddle
}
}
//rubber band
Item {
anchors.fill: parent
Rectangle {
id: selectionRect
color: "transparent"
border.color: "blue"
border.width: 1
visible: false
opacity: 0.3
Rectangle { anchors.fill: parent; color: "blue"; opacity: 0.2 }
}
MouseArea {
id: mouseArea
anchors.fill: parent
property point startPos
propagateComposedEvents: true
onPressed: (mouse) => {
// mouse.accepted = false
startPos = Qt.point(mouse.x, mouse.y)
selectionRect.x = startPos.x
selectionRect.y = startPos.y
selectionRect.width = 0
selectionRect.height = 0
selectionRect.visible = true
}
onPositionChanged: {
selectionRect.x = Math.min(mouse.x, startPos.x)
selectionRect.y = Math.min(mouse.y, startPos.y)
selectionRect.width = Math.abs(mouse.x - startPos.x)
selectionRect.height = Math.abs(mouse.y - startPos.y)
}
onReleased: {
selectionRect.visible = false
const append = mouse.modifiers & Qt.ControlModifier
selectItemsInRect({
x: selectionRect.x,
y: selectionRect.y,
width: selectionRect.width,
height: selectionRect.height
}, append)
}
}
}
}
AlbumContents {
visible : !isMainPage
query : root.query
udid : root.udid
albumId: root.albumId
Layout.fillWidth: true
Layout.fillHeight: true
}
}
}