Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
151 lines (130 loc) · 4.93 KB
/
Copy pathmain.py
File metadata and controls
151 lines (130 loc) · 4.93 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
import sys
import os
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QSplitter, QPushButton, QFileDialog
from PyQt5.QtGui import QIcon, QSurfaceFormat
from gcode_parser import parse_gcode_file
from viewer3d import GcodeViewer3D
from ui_panels import FilamentConfigPanel
# https://stackoverflow.com/questions/31836104/pyinstaller-and-onefile-how-to-include-an-image-in-the-exe-file
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("G-Code Color Viewer")
icon_path = resource_path("app_icon.ico")
if os.path.exists(icon_path):
self.setWindowIcon(QIcon(icon_path))
self.resize(1024, 768)
main_widget = QWidget()
main_layout = QHBoxLayout()
main_layout.setContentsMargins(0,0,0,0)
splitter = QSplitter()
# Left: 3D Viewer
self.viewer = GcodeViewer3D()
splitter.addWidget(self.viewer)
# Right: Control Panel
control_panel = QWidget()
control_layout = QVBoxLayout()
btn_load = QPushButton("Load G-Code")
btn_load.clicked.connect(self.load_gcode)
control_layout.addWidget(btn_load)
self.config_panel = FilamentConfigPanel()
self.config_panel.config_changed.connect(self.update_viewer)
control_layout.addWidget(self.config_panel)
control_layout.addStretch()
control_panel.setLayout(control_layout)
splitter.addWidget(control_panel)
# Set splitter proportions (70% viewer, 30% controls)
splitter.setSizes([700, 300])
main_layout.addWidget(splitter)
main_widget.setLayout(main_layout)
self.setCentralWidget(main_widget)
self.current_paths = []
# Auto load test G-code if it exists
test_file = "test_gcode.gcode"
if os.path.exists(test_file):
self.load_gcode(test_file)
def load_gcode(self, filepath=None):
if not filepath:
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
filepath, _ = QFileDialog.getOpenFileName(self, "Open G-Code", "", "G-Code Files (*.gcode *.nc);;All Files (*)", options=options)
if filepath:
print(f"Loading {filepath}...")
# For large files, parsing in the main thread might freeze UI,
# but for a simplest prototype, sync is fine.
self.current_paths = parse_gcode_file(filepath)
self.update_viewer(self.config_panel.get_config())
print("Loaded.")
def update_viewer(self, config):
if self.current_paths:
self.viewer.set_gcode_data(self.current_paths, config)
if __name__ == "__main__":
from PyQt5.QtGui import QSurfaceFormat
fmt = QSurfaceFormat()
fmt.setSamples(8)
QSurfaceFormat.setDefaultFormat(fmt)
app = QApplication(sys.argv)
# Apply dark orange-accented theme globally to QApplication
app.setStyleSheet("""
QMainWindow, QWidget, QDialog, QColorDialog, QFileDialog {
background-color: #2b2b2b;
color: #ffffff;
}
QGroupBox {
color: #FC6D09;
font-weight: bold;
border: 1px solid #555;
margin-top: 15px;
}
QGroupBox::title {
subcontrol-origin: margin;
subcontrol-position: top left;
padding: 0 5px;
}
QPushButton {
background-color: #444;
color: white;
border: 1px solid #666;
padding: 5px;
border-radius: 3px;
}
QPushButton:hover {
background-color: #555;
}
QComboBox, QSlider::groove:horizontal {
background-color: #444;
border: 1px solid #666;
}
QSlider::handle:horizontal {
background-color: #FC6D09;
width: 14px;
margin: -4px 0;
border-radius: 7px;
}
QHeaderView::section {
background-color: #333;
color: #ffffff;
border: 1px solid #555;
padding: 4px;
}
QTreeView, QListView {
background-color: #2b2b2b;
color: #ffffff;
alternate-background-color: #333;
}
QTreeView::item:selected, QListView::item:selected {
background-color: #FC6D09;
color: #ffffff;
}
""")
window = MainWindow()
window.show()
sys.exit(app.exec_())