-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListModel.py
More file actions
executable file
·47 lines (43 loc) · 2.13 KB
/
ListModel.py
File metadata and controls
executable file
·47 lines (43 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""
ListModel.py
Abstract List Model class for displaying loaded templates to select.
"""
from pathlib import Path
from PyQt5.QtCore import QAbstractListModel, Qt
class ListModel(QAbstractListModel):
'''Model for Qt View that displays list of selectable text items'''
def __init__(self, template_folder: str):
'''Initilizes list of template paths given the path location of the
Templates folder'''
super().__init__()
self.template_folder = template_folder
self.update_data()
def data(self, index, role):
'''The Qt View will call this and provide an index number of the data
it is requesting. The role is role the Qt View is asking for.'''
if role == Qt.DisplayRole:
# index has .row() and .column() contain the index numbers
# using row for list.
# Display only the folder name, but stores absolute pathlib path object in list.
pathlib_path = self.items[index.row()]
display_text = pathlib_path.name
return display_text
def rowCount(self, index):
'''Qt View calls this to get the number of rows in the list,
it is called so the Qt View knows the maximum index it can request
from the data store.'''
return len(self.items)
def _get_templates(self, template_folder_path: str):
"""Takes in location of the source folder and returns list
of all the files & dirs in that path, as Pathlib path objects.
This is logical input output function only, independent of class wide varialbes."""
templates = [folder for folder in Path(template_folder_path).iterdir()]
return templates
def update_data(self):
"""sets paths list (stored in self.items), using the tempaltes folder
of this class instance, then emmits layout changed.
This can be called after class has been initilized to refresh the
list of paths, or can be called after self.template_folder has been
changed to chagne the source folder to use."""
self.items = self._get_templates(self.template_folder)
self.layoutChanged.emit()