Commit 53dc9a1a authored by Georgiy Yankovskiy's avatar Georgiy Yankovskiy

Simple game list

parent 4c0b8d14
import QtQuick import QtQuick
import QtQuick.Controls as C
import QtQuick.Layouts
import "../delegates"
import "../constants/tabs.js" as TabConstants import "../constants/tabs.js" as TabConstants
Rectangle { Rectangle {
// PROPERTIES // PROPERTIES
property string currentTab: TabConstants.systemManagementTab property string currentTab: TabConstants.systemManagementTab
...@@ -13,6 +17,9 @@ Rectangle { ...@@ -13,6 +17,9 @@ Rectangle {
height: 480 height: 480
color: "#ffffff" color: "#ffffff"
// onWidthChanged: { console.log("Window Width changed: " + width) }
// onHeightChanged: { console.log("Window Height changed: " + height)}
// COMPONENTS // COMPONENTS
Grid { Grid {
...@@ -38,26 +45,51 @@ Rectangle { ...@@ -38,26 +45,51 @@ Rectangle {
Rectangle { color: "magenta"; width: 10; height: 10 } Rectangle { color: "magenta"; width: 10; height: 10 }
} }
Grid {
id: gamesGrid
visible: tabs.currentTab == TabConstants.gamesTab
columns: 3 C.ScrollView {
spacing: 2 visible: tabs.currentTab == TabConstants.gamesTab
id: gamesScroller
anchors.fill: parent
anchors.topMargin: 60
clip : true
GridLayout {
id: gamesGrid
readonly property int elementWidth: 256 + gamesGrid.rowSpacing*2
anchors.left: parent.left anchors.left: parent.left
anchors.right: parent.right anchors.right: parent.right
anchors.top: parent.top anchors.top: parent.top
anchors.bottom: parent.bottom anchors.bottom: parent.bottom
anchors.topMargin: 60
anchors.rightMargin: 0
anchors.leftMargin: 0
anchors.bottomMargin: 0
Text { // columns: Math.max(Math.floor(parent.width / elementWidth), 1)
id: text2 // rows: Math.max(Math.ceil(children.length / columns), 1)
text: qsTr("Text")
font.pixelSize: 12 columns: Math.max(Math.floor(gamesScroller.width / elementWidth), 1)
rows: Math.max(Math.ceil(children.length / columns), 1)
anchors.rightMargin: 8
anchors.leftMargin: 8
anchors.bottomMargin: 8
anchors.topMargin: 8
rowSpacing: 8
columnSpacing: rowSpacing
Repeater {
// Layout.fillHeight: true
// Layout.fillWidth: true
model: core_app.games
Game {
title: model.name
exec: model.exec
icon: model.icon
// width: gamesGrid.elementWidth
// height: gamesGrid.elementHeight
// icon: core_app.games.icon
// exec: core_app.games.exec
}
}
} }
} }
...@@ -100,8 +132,10 @@ Rectangle { ...@@ -100,8 +132,10 @@ Rectangle {
text: "Games" text: "Games"
onClicked: function(){ onClicked: function(){
tabs.currentTab = TabConstants.gamesTab; tabs.currentTab = TabConstants.gamesTab;
if(app === undefined) return; //if(core_app === undefined) return;
app.get_games(); //console.log("core_app found");
//app.get_games();
// tabs.changeTab(); // tabs.changeTab();
// console.log(tabs.currentTab); // console.log(tabs.currentTab);
} }
...@@ -111,3 +145,9 @@ Rectangle { ...@@ -111,3 +145,9 @@ Rectangle {
} }
/*##^##
Designer {
D{i:0}D{i:1;invisible:true}
}
##^##*/
import QtQuick
Rectangle {
property string title: "Generic title"
property string icon: ""
property string exec: ""
id: game
width: 256
height: 256
color: "#efefef"
radius: 5
border.width: 1
Image {
id: image
anchors.left: parent.left
anchors.right: parent.right
anchors.top: parent.top
anchors.bottom: parent.bottom
source: game.icon
anchors.rightMargin: 8
anchors.bottomMargin: 47
anchors.leftMargin: 8
anchors.topMargin: 8
fillMode: Image.PreserveAspectFit
}
Text {
id: title
y: 439
height: 33
text: game.title
anchors.left: parent.left
anchors.right: parent.right
anchors.bottom: parent.bottom
font.pixelSize: 22
horizontalAlignment: Text.AlignHCenter
anchors.rightMargin: 8
anchors.leftMargin: 8
anchors.bottomMargin: 8
}
}
# This Python file uses the following encoding: utf-8
import sys import sys
from pathlib import Path from pathlib import Path
...@@ -6,6 +5,7 @@ from PySide6.QtGui import QGuiApplication ...@@ -6,6 +5,7 @@ from PySide6.QtGui import QGuiApplication
from PySide6.QtQml import QQmlApplicationEngine from PySide6.QtQml import QQmlApplicationEngine
from src.models.App import App from src.models.App import App
from src.models.GamesModel import GamesModel
# TODO: add VirtualKeyboard # TODO: add VirtualKeyboard
...@@ -13,10 +13,13 @@ if __name__ == "__main__": ...@@ -13,10 +13,13 @@ if __name__ == "__main__":
app = QGuiApplication(sys.argv) app = QGuiApplication(sys.argv)
qml_file = Path(__file__).resolve().parent / "../qml/qml.qml" qml_file = Path(__file__).resolve().parent / "../qml/qml.qml"
engine = QQmlApplicationEngine() engine = QQmlApplicationEngine()
app_model = App()
context = engine.rootContext()
context.setContextProperty("core_app", app_model)
engine.load(qml_file) engine.load(qml_file)
if not engine.rootObjects(): if not engine.rootObjects():
sys.exit(-1) sys.exit(-1)
appModel = App()
context = engine.rootContext()
context.setContextProperty("app", appModel)
sys.exit(app.exec()) sys.exit(app.exec())
...@@ -2,6 +2,10 @@ from PySide6 import QtCore ...@@ -2,6 +2,10 @@ from PySide6 import QtCore
from os.path import expanduser from os.path import expanduser
import glob import glob
from desktop_parser import DesktopFile from desktop_parser import DesktopFile
import os.path
from pathlib import Path
from PySide6.QtCore import Property, Signal, Slot, QObject
from src.models.GamesModel import Game, GamesModel
class GameShortcut: class GameShortcut:
...@@ -14,6 +18,7 @@ class GameShortcut: ...@@ -14,6 +18,7 @@ class GameShortcut:
class App(QtCore.QObject): class App(QtCore.QObject):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self.games_model = GamesModel()
self.home = expanduser('~') self.home = expanduser('~')
self.config_location = '/.config/PortProton.conf' self.config_location = '/.config/PortProton.conf'
self.portproton_location = '' self.portproton_location = ''
...@@ -24,19 +29,43 @@ class App(QtCore.QObject): ...@@ -24,19 +29,43 @@ class App(QtCore.QObject):
with open(self.home + self.config_location, 'r') as file: with open(self.home + self.config_location, 'r') as file:
self.portproton_location = file.read().strip() self.portproton_location = file.read().strip()
print(f'Current PortProton location: {self.portproton_location}') print(f'Current PortProton location: {self.portproton_location}')
self.games_model.clear()
files = glob.glob(f"{self.portproton_location}/*.desktop") files = glob.glob(f"{self.portproton_location}/*.desktop")
# for val in files: for val in files:
# print(val) desktop_file = DesktopFile.from_file(val)
# desktop_file = DesktopFile.from_file("path/to/file.desktop") data = desktop_file.data
entry = data['Desktop Entry']
name = entry['Name'] or 'generic'
exec = 'Exec' in entry and entry['Exec'] or ''
icon = entry['Icon']
assert (isinstance(name, str)
and isinstance(exec, str)
and isinstance(icon, str))
exec_split = exec.split(' ')
# Ignore extra non-related desktop entries
if (len(exec_split) <= 1 or
('data/scripts/start.sh' not in exec_split[1] or '%F' in exec_split[-1])):
continue
# TODO parse product name
icon = (os.path.isfile(icon) and icon
or os.path.realpath(f"{Path(__file__).resolve().parent}../../../qml/images/game_icon.png"))
self.games_model.add_game(Game(name=name, icon=icon, exec=exec))
except FileNotFoundError: except FileNotFoundError:
print('File not found') print('File not found')
except Exception: except Exception as e:
print('An error occurred') print('An error occurred', e)
pass pass
### SLOTS ### ### SLOTS ###
@QtCore.Slot() @Property(QObject, constant=True)
def get_games(self): def games(self):
pass return self.games_model
import typing
from dataclasses import dataclass, fields
from PySide6.QtCore import QAbstractListModel, QModelIndex, Qt, QByteArray
@dataclass
class Game:
name: str = ''
exec: str = ''
icon: str = ''
class GamesModel(QAbstractListModel):
def __init__(self):
super().__init__()
self._list = []
def data(self, index: QModelIndex, role: int = Qt.DisplayRole) -> typing.Any:
if 0 <= index.row() < self.rowCount():
student = self._list[index.row()]
name = self.roleNames().get(role)
if name:
return getattr(student, name.decode())
def roleNames(self) -> dict[int, QByteArray]:
d = {}
for i, field in enumerate(fields(Game)):
d[Qt.DisplayRole + i] = field.name.encode()
return d
def rowCount(self, index: QModelIndex = QModelIndex()) -> int:
return len(self._list)
def add_game(self, game: Game) -> None:
self.beginInsertRows(QModelIndex(), self.rowCount(), self.rowCount())
self._list.append(game)
self.endInsertRows()
def clear(self) -> None:
self.beginInsertRows(QModelIndex(), self.rowCount(), self.rowCount())
self._list = []
self.endInsertRows()
pass
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment