-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasic_example.py
More file actions
204 lines (169 loc) · 7.11 KB
/
basic_example.py
File metadata and controls
204 lines (169 loc) · 7.11 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# M""""""""`M dP
# Mmmmmm .M 88
# MMMMP .MMM dP dP 88 .dP .d8888b.
# MMP .MMMMM 88 88 88888" 88' `88
# M' .MMMMMMM 88. .88 88 `8b. 88. .88
# M M `88888P' dP `YP `88888P'
# MMMMMMMMMMM -*- Created by Zuko -*-
#
# * * * * * * * * * * * * * * * * * * * * *
# * - - - F.R.E.E.M.I.N.D - - - *
# * - Copyright © 2026 (Z) Programing - *
# * - - All Rights Reserved - - *
# * * * * * * * * * * * * * * * * * * * * *
import sys
from pathlib import Path
root = Path(__file__).resolve().parent.parent
sys.path.append(str(root))
import datetime
import random
import sys
from typing import List, Dict, Any
from PySide6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QLabel
from datatable import DataTable, DataType
class ExampleWindow(QMainWindow):
def exampleGrouping(self):
columns = [('id', 'ID', DataType.NUMERIC), ('name', 'Name', DataType.STRING), ('total', 'total', DataType.NUMERIC)]
self.data_table.setColumns(columns)
self.mainLayout.addWidget(self.data_table)
# Enable row collapsing
self.data_table.enableRowCollapsing(True, 'subrows')
# Example data with subrows
data = [
{'id': 1, 'name': 'Category A', 'total': 1000, 'subrows': [{'id': 101, 'name': 'Item A1', 'total': 500}, {'id': 102, 'name': 'Item A2', 'total': 500}]},
{'id': 2, 'name': 'Category B', 'total': 2000, 'subrows': [{'id': 201, 'name': 'Item B1', 'total': 1200}, {'id': 202, 'name': 'Item B2', 'total': 800}]},
]
self.data_table.setData(data)
# Connect expansion signals
self.data_table.rowExpanded.connect(lambda row, data: print(f'Row {row} expanded'))
self.data_table.rowCollapsed.connect(lambda row, data: print(f'Row {row} collapsed'))
return
"""Example window demonstrating DataTable usage"""
def __init__(self):
super().__init__()
self.setWindowTitle('DataTable Example')
self.resize(800, 600)
# set style fusion
self.setStyleFusion()
# Central widget and layout
central_widget = QWidget()
self.setCentralWidget(central_widget)
self.mainLayout = QVBoxLayout(central_widget)
# Create DataTable
self.data_table = DataTable()
# Create sample data
data = self._create_sample_data()
# Set up columns (key, header, data_type)
columns = [
('id', 'ID', DataType.NUMERIC),
('name', 'Name', DataType.STRING),
('position', 'Position', DataType.STRING),
('office', 'Office', DataType.STRING),
('age', 'Age', DataType.NUMERIC),
('start_date', 'Start Date', DataType.DATE),
('salary', 'Salary', DataType.NUMERIC),
('active', 'Active', DataType.BOOLEAN),
]
# Connect signals
self.data_table.rowSelected.connect(self.on_row_selected)
self.data_table.sortChanged.connect(self.on_sort_changed)
# Apply data and columns
self.data_table.setColumns(columns)
self.data_table.setData(data)
# Make the salary column sum visible at the bottom
status_label = QLabel('Total Salary: $0')
self.mainLayout.addWidget(status_label)
# Update salary sum when data changes
def update_salary_sum():
salary_sum = self.data_table.getAggregateValue('salary', 'sum')
if salary_sum is not None:
status_label.setText(f'Total Salary: ${salary_sum:,.2f}')
update_salary_sum()
self.data_table._model.dataChanged.connect(update_salary_sum)
def _create_sample_data(self) -> List[Dict[str, Any]]:
"""Create sample data for the table
Returns:
List of row data dictionaries
"""
names = [
'Airi Satou',
'Angelica Ramos',
'Ashton Cox',
'Bradley Greer',
'Brenden Wagner',
'Brielle Williamson',
'Bruno Nash',
'Caesar Vance',
'Cara Stevens',
'Cedric Kelly',
]
positions = [
'Accountant',
'Chief Executive Officer (CEO)',
'Junior Technical Author',
'Software Engineer',
'Software Engineer',
'Integration Specialist',
'Software Engineer',
'Pre-Sales Support',
'Sales Assistant',
'Senior Javascript Developer',
]
offices = ['Tokyo', 'London', 'San Francisco', 'London', 'San Francisco', 'New York', 'London', 'New York', 'New York', 'Edinburgh']
ages = [33, 47, 66, 41, 28, 61, 38, 21, 46, 22]
start_dates = [
datetime.date(2008, 11, 28),
datetime.date(2009, 10, 9),
datetime.date(2009, 1, 12),
datetime.date(2012, 10, 13),
datetime.date(2011, 6, 7),
datetime.date(2012, 12, 2),
datetime.date(2011, 5, 3),
datetime.date(2011, 12, 12),
datetime.date(2011, 12, 6),
datetime.date(2012, 3, 29),
]
salaries = [162700, 1200000, 86000, 132000, 145000, 372000, 163500, 106450, 145600, 433060]
data = []
for i in range(len(names)):
row = {
'id': i + 1,
'name': names[i],
'position': positions[i],
'office': offices[i],
'age': ages[i],
'start_date': start_dates[i],
'salary': salaries[i],
'active': random.choice([True, False]),
}
data.append(row)
return data
def on_row_selected(self, row: int, row_data: Dict[str, Any]) -> None:
"""Handle row selected
Args:
row: Row index
row_data: Row data
"""
print(f'Row {row} selected: {row_data["name"]}')
# Calculate row's salary as percentage of total
salary = row_data['salary']
total_salary = self.data_table.getAggregateValue('salary', 'sum')
percentage = (salary / total_salary) * 100 if total_salary else 0
print(f'Salary: ${salary:,.2f} ({percentage:.1f}% of total)')
def on_sort_changed(self, column_key: str, sort_order) -> None:
"""Handle sort changed
Args:
column_key: Column key
sort_order: Sort order
"""
order_str = 'ascending' if sort_order.value == 0 else 'descending'
print(f'Table sorted by {column_key} in {order_str} order')
def setStyleFusion(self):
"""Set the application style to Fusion and apply a dark theme."""
from PySide6.QtWidgets import QStyleFactory
QApplication.setStyle(QStyleFactory.create('Fusion'))
if __name__ == '__main__':
app = QApplication(sys.argv)
window = ExampleWindow()
window.show()
sys.exit(app.exec())