Skip to content

Instantly share code, notes, and snippets.

@kovrov
Created January 4, 2012 16:05
Show Gist options
  • Save kovrov/1560700 to your computer and use it in GitHub Desktop.
Save kovrov/1560700 to your computer and use it in GitHub Desktop.
File selector in QML and PySide (http://blogs.kde.org/node/4437)
import QtQuick 1.0
import Self 1.0
Rectangle {
id: page
width: 400; height: 240;
anchors.fill: parent
DirModel {
id: dirModel
}
VisualDataModel {
id: listModel
model: dirModel
Item {
id: itemDelegate
width: listView.width; height: 25
Rectangle {
id: content
anchors.fill: parent
color: "transparent"
Text { text: fileName }
}
states: State {
name: "active"; when: itemDelegate.activeFocus
PropertyChanges { target: content; color: "#FFDDDD" }
}
MouseArea {
anchors.fill: parent
onClicked: {
listView.currentIndex = index
itemDelegate.forceActiveFocus()
if (model.hasModelChildren) {
animModel.rootIndex = listModel.modelIndex(index)
animation.running = true
}
}
}
Keys.onRightPressed: {
if (model.hasModelChildren) {
animModel.rootIndex = listModel.modelIndex(index)
animation.running = true
}
}
Keys.onLeftPressed: {
// if statement does not work as intended
if (listModel.parentModelIndex() != listModel.rootIndex) {
listView.x = -listView.width
listModel.rootIndex = listModel.parentModelIndex()
leftAnimation.running = true
}
}
Keys.onUpPressed: {
if (index > 0) {
listView.currentIndex = index - 1
} else if (listView.keyNavigationWraps) {
listView.currentIndex = listView.count - 1
}
animModel.rootIndex = listModel.modelIndex(listView.currentIndex)
}
Keys.onDownPressed: {
if (listModel.count > index + 1) {
listView.currentIndex = index + 1
} else if (listView.keyNavigationWraps) {
listView.currentIndex = 0
}
animModel.rootIndex = listModel.modelIndex(listView.currentIndex)
}
}
}
VisualDataModel {
id: animModel
model: dirModel
Rectangle {
width: listView.width; height: 25
Text { text: fileName }
}
rootIndex: listModel.modelIndex(0)
}
SequentialAnimation {
id: animation
NumberAnimation {
target: listView
property: "x"
to: -listView.width
duration: 100
}
ScriptAction {
script: {
listView.x = 0
listModel.rootIndex = animModel.rootIndex
animModel.rootIndex = listModel.modelIndex(0)
}
}
}
SequentialAnimation {
id: leftAnimation
NumberAnimation {
target: listView
property: "x"
to: 0
duration: 100
}
ScriptAction {
script: {
animModel.rootIndex = listModel.modelIndex(0)
}
}
}
ListView {
id: listView
x: 0; y: 0
width: page.width * 0.8
height: page.height
model: listModel
focus: true
keyNavigationWraps: true
}
ListView {
id: animBox
x: listView.width; y: listView.y
width: listView.width
height: listView.height
model: animModel
}
}
#!/usr/bin/env python
import sys
from PySide import QtCore, QtGui, QtDeclarative
class DirModel(QtGui.QDirModel):
def __init__(self, parent=None):
super(DirModel, self).__init__(parent)
app = QtGui.QApplication(sys.argv)
QtDeclarative.qmlRegisterType(DirModel, "Self", 1,0, "DirModel")
view = QtDeclarative.QDeclarativeView()
view.setSource(QtCore.QUrl.fromLocalFile("list.qml"))
view.show()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment