-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathworkbench.py
352 lines (312 loc) · 12.6 KB
/
workbench.py
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import os
import json
from PyQt5.QtWidgets import (
QMainWindow, QWidget, QStackedWidget, QHBoxLayout, QVBoxLayout,
QToolButton, QPushButton, QLabel, QMessageBox, QMenu, QFileDialog, QInputDialog
)
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtCore import Qt, QSize, pyqtSignal
from project_window_core import ProjectWindow
from options import OptionsWindow # Import the global options window
# Define the file used for storing project data.
PROJECTS_FILE = "projects.json"
def load_projects():
"""Load project data from a JSON file. If the file does not exist, return default projects."""
if os.path.exists(PROJECTS_FILE):
try:
with open(PROJECTS_FILE, "r", encoding="utf-8") as f:
return json.load(f)
except Exception as e:
QMessageBox.warning(None, "Load Projects",
f"Error loading projects: {e}")
# Default dummy project data.
return [
{"name": "My First Project", "cover": None},
{"name": "Sci-Fi Epic", "cover": None},
{"name": "Mystery Novel", "cover": None},
]
def save_projects(projects):
"""Save the project data to a JSON file."""
try:
with open(PROJECTS_FILE, "w", encoding="utf-8") as f:
json.dump(projects, f, indent=4)
except Exception as e:
QMessageBox.warning(None, "Save Projects",
f"Error saving projects: {e}")
# Load projects at module level.
PROJECTS = load_projects()
class ProjectPostIt(QToolButton):
"""
A custom QToolButton that displays the project cover.
Left-click opens the project.
Right-click shows a context menu with options.
"""
def __init__(self, project, parent=None):
super().__init__(parent)
self.project = project
self.setup_ui()
self.setToolTip(
"Left-click: Open project\nRight-click: Options (Delete, Export, Stats, Edit Cover)")
def setup_ui(self):
default_size = QSize(300, 450)
if self.project["cover"]:
pixmap = QPixmap(self.project["cover"])
pixmap = pixmap.scaled(
default_size, Qt.KeepAspectRatio, Qt.SmoothTransformation)
else:
pixmap = QPixmap(default_size)
pixmap.fill(Qt.lightGray)
icon = QIcon(pixmap)
self.setIcon(icon)
self.setIconSize(default_size)
self.setFixedSize(default_size)
self.setText("")
self.setStyleSheet("QToolButton { margin: 0px; padding: 0px; }")
def contextMenuEvent(self, event):
menu = QMenu(self)
delete_action = menu.addAction("Delete Project")
export_action = menu.addAction("Export Project")
stats_action = menu.addAction("Project Statistics")
cover_action = menu.addAction("Add Book Cover")
action = menu.exec_(event.globalPos())
if action == delete_action:
confirm = QMessageBox.question(
self, "Delete Project",
f"Are you sure you want to delete '{self.project['name']}'?",
QMessageBox.Yes | QMessageBox.No, QMessageBox.No
)
if confirm == QMessageBox.Yes:
try:
PROJECTS.remove(self.project)
save_projects(PROJECTS)
QMessageBox.information(
self, "Delete Project", f"Project '{self.project['name']}' deleted.")
workbench = self.window()
if hasattr(workbench, "load_covers"):
workbench.load_covers()
except Exception as e:
QMessageBox.warning(
self, "Delete Project", f"Error deleting project: {e}")
elif action == export_action:
QMessageBox.information(
self, "Export Project",
f"Export '{self.project['name']}' functionality will be implemented later."
)
elif action == stats_action:
QMessageBox.information(
self, "Project Statistics",
f"Statistics for '{self.project['name']}' will be available in a future release."
)
elif action == cover_action:
self.add_book_cover()
def add_book_cover(self):
file_path, _ = QFileDialog.getOpenFileName(
self, "Select Book Cover", "", "Image Files (*.png *.jpg *.jpeg *.bmp)"
)
if file_path:
self.project["cover"] = file_path
pixmap = QPixmap(file_path)
if pixmap.isNull():
QMessageBox.warning(self, "Invalid Image",
"The selected file is not a valid image.")
return
default_size = QSize(300, 450)
pixmap = pixmap.scaled(
default_size, Qt.KeepAspectRatio, Qt.SmoothTransformation)
icon = QIcon(pixmap)
self.setIcon(icon)
self.setIconSize(default_size)
save_projects(PROJECTS)
class ProjectCoverWidget(QWidget):
"""
A composite widget that displays the project title above the cover.
"""
openProject = pyqtSignal(str)
def __init__(self, project, parent=None):
super().__init__(parent)
self.project = project
self.init_ui()
def init_ui(self):
layout = QVBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(5)
self.titleLabel = QLabel(self.project["name"])
self.titleLabel.setObjectName("projectTitleLabel")
self.titleLabel.setAlignment(Qt.AlignCenter)
layout.addWidget(self.titleLabel)
self.coverButton = ProjectPostIt(self.project)
self.coverButton.clicked.connect(
lambda: self.openProject.emit(self.project["name"]))
layout.addWidget(self.coverButton)
self.setLayout(layout)
self.setFixedSize(300, 480)
class WorkbenchWindow(QMainWindow):
"""
The main workbench window that shows your project covers in a carousel-like view.
"""
def __init__(self):
super().__init__()
# Removed global stylesheet propagation prevention.
self.setWindowTitle("Writingway - Workbench")
self.resize(640, 720)
self.init_ui()
self.apply_fixed_stylesheet()
def init_ui(self):
central_widget = QWidget()
self.setCentralWidget(central_widget)
layout = QVBoxLayout(central_widget)
layout.setSpacing(15)
header_container = QWidget()
header_layout = QHBoxLayout(header_container)
header_layout.setContentsMargins(0, 0, 0, 0)
header_label = QLabel("")
header_label.setObjectName("headerLabel")
header_label.setAlignment(Qt.AlignCenter)
header_layout.addWidget(header_label)
header_layout.addStretch()
settings_button = QPushButton("")
cog_icon_path = os.path.join("assets", "icons", "settings.svg")
settings_button.setIcon(QIcon(cog_icon_path))
settings_button.setToolTip(
"Click here to configure global options (paths, fonts, themes, etc.)")
settings_button.clicked.connect(self.open_settings)
header_layout.addWidget(settings_button)
layout.addWidget(header_container)
carousel_container = QWidget()
carousel_layout = QHBoxLayout(carousel_container)
carousel_layout.setContentsMargins(0, 0, 0, 0)
carousel_layout.setSpacing(10)
self.left_button = QPushButton("")
left_arrow_icon = QIcon(os.path.join(
"assets", "icons", "chevron-left.svg"))
self.left_button.setIcon(left_arrow_icon)
self.left_button.setIconSize(QSize(32, 32))
self.left_button.setFixedSize(60, 60)
self.left_button.setToolTip("Previous Project")
self.left_button.clicked.connect(self.show_previous)
carousel_layout.addWidget(self.left_button)
self.coverStack = QStackedWidget()
self.coverStack.setFixedSize(300, 480)
carousel_layout.addWidget(self.coverStack, stretch=1)
self.right_button = QPushButton("")
right_arrow_icon = QIcon(os.path.join(
"assets", "icons", "chevron-right.svg"))
self.right_button.setIcon(right_arrow_icon)
self.right_button.setIconSize(QSize(32, 32))
self.right_button.setFixedSize(60, 60)
self.right_button.setToolTip("Next Project")
self.right_button.clicked.connect(self.show_next)
carousel_layout.addWidget(self.right_button)
layout.addWidget(carousel_container)
new_project_button = QPushButton("+ New Project")
new_project_button.setObjectName("newProjectButton")
new_project_button.setFixedSize(200, 50)
new_project_button.setToolTip("Create a brand-new project")
new_project_button.clicked.connect(self.new_project)
layout.addWidget(new_project_button, alignment=Qt.AlignCenter)
self.coverStack.currentChanged.connect(self.updateCoverStackSize)
self.load_covers()
def apply_fixed_stylesheet(self):
fixed_styles = """
QMainWindow {
background-color: #f5f5f5;
}
QLabel#headerLabel {
font-size: 22px;
font-weight: 600;
color: #333;
}
QLabel#projectTitleLabel {
font-size: 14px;
font-weight: 500;
color: #333;
}
QPushButton {
background-color: #ffffff;
border: 1px solid #ccc;
border-radius: 6px;
font-size: 14px;
padding: 6px 12px;
}
QPushButton:hover {
background-color: #e6e6e6;
}
QPushButton:pressed {
background-color: #dcdcdc;
}
QPushButton#newProjectButton {
font-size: 16px;
font-weight: bold;
}
QToolButton {
border: 2px solid #ccc;
border-radius: 5px;
background-color: #fff;
}
QToolButton:hover {
background-color: #f0f0f0;
}
"""
self.setStyleSheet(fixed_styles)
def open_settings(self):
options = OptionsWindow(self)
options.exec_()
def updateCoverStackSize(self, index):
fixed_width = 300
fixed_height = 480
self.coverStack.setFixedSize(fixed_width, fixed_height)
def load_covers(self):
while self.coverStack.count():
widget = self.coverStack.widget(0)
self.coverStack.removeWidget(widget)
widget.deleteLater()
if PROJECTS:
for project in PROJECTS:
cover_widget = ProjectCoverWidget(project)
cover_widget.openProject.connect(self.open_project)
self.coverStack.addWidget(cover_widget)
else:
new_project_btn = QToolButton()
new_project_btn.setText("+ New Project")
new_project_btn.setFixedSize(300, 480)
new_project_btn.clicked.connect(self.new_project)
self.coverStack.addWidget(new_project_btn)
self.updateCoverStackSize(self.coverStack.currentIndex())
def show_previous(self):
count = self.coverStack.count()
if count == 0:
return
index = self.coverStack.currentIndex()
new_index = (index - 1) % count
self.coverStack.setCurrentIndex(new_index)
def show_next(self):
count = self.coverStack.count()
if count == 0:
return
index = self.coverStack.currentIndex()
new_index = (index + 1) % count
self.coverStack.setCurrentIndex(new_index)
def open_project(self, project_name):
self.project_window = ProjectWindow(project_name)
self.project_window.show()
def new_project(self):
name, ok = QInputDialog.getText(
self, "New Project", "Enter new project name:")
if ok and name.strip():
new_project = {"name": name.strip(), "cover": None}
PROJECTS.append(new_project)
save_projects(PROJECTS)
self.load_covers()
self.coverStack.setCurrentIndex(len(PROJECTS) - 1)
QMessageBox.information(
self, "New Project", f"Project '{name}' created.")
else:
QMessageBox.information(
self, "New Project", "Project creation cancelled.")
if __name__ == "__main__":
from PyQt5.QtWidgets import QApplication
import sys
app = QApplication(sys.argv)
window = WorkbenchWindow()
window.show()
sys.exit(app.exec_())