-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
316 lines (265 loc) · 10.9 KB
/
main.py
File metadata and controls
316 lines (265 loc) · 10.9 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
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
#!/usr/bin/env python3
"""
Notes - KDE Desktop Application for Note Management with Speech-to-Text
Supports multiple profiles via --profile <directory>
"""
import sys
import argparse
import locale
from pathlib import Path
from PyQt6.QtWidgets import (
QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout,
QPushButton, QSplitter, QMessageBox, QLabel
)
from PyQt6.QtCore import Qt, QTranslator, QLocale, QLibraryInfo
from models.database import Database
from gui.notebook_list import NotebookList
from gui.note_list import NoteList
from gui.editor_widget import EditorWidget
class NotesApp(QMainWindow):
"""Main window of the Notes application"""
def __init__(self, profile_dir: Path, language: str = 'en'):
super().__init__()
self.profile_dir = profile_dir
self.language = language
self.current_notebook_id = None
self.current_note_id = None
# Initialize database
self.db = Database(profile_dir)
# Configure interface
self.setWindowTitle(self.tr("Notes"))
self.setGeometry(100, 100, 1400, 900)
# Apply dark theme similar to Joplin
self.setStyleSheet("""
QMainWindow {
background-color: #1e1e1e;
}
QWidget {
background-color: #2b2b2b;
color: #e0e0e0;
}
QListWidget {
background-color: #252525;
border: 1px solid #3c3c3c;
color: #e0e0e0;
selection-background-color: #0d6efd;
}
QListWidget::item:hover {
background-color: #353535;
}
QPushButton {
background-color: #3c3c3c;
color: #e0e0e0;
border: 1px solid #4c4c4c;
padding: 6px 12px;
border-radius: 3px;
}
QPushButton:hover {
background-color: #4c4c4c;
}
QPushButton:pressed {
background-color: #2c2c2c;
}
QLineEdit {
background-color: #2b2b2b;
color: #e0e0e0;
border: 1px solid #3c3c3c;
padding: 5px;
border-radius: 3px;
}
QTextEdit {
background-color: #1e1e1e;
color: #e0e0e0;
border: none;
selection-background-color: #264f78;
}
QLabel {
color: #e0e0e0;
}
QToolBar {
background-color: #2b2b2b;
border: none;
spacing: 3px;
}
""")
self.init_ui()
def init_ui(self):
"""Initialize user interface"""
# Central widget
central_widget = QWidget()
self.setCentralWidget(central_widget)
# Main horizontal layout
main_layout = QHBoxLayout(central_widget)
# Main splitter to divide sidebar and editor
main_splitter = QSplitter(Qt.Orientation.Horizontal)
# === LEFT SIDEBAR (Vertical like Joplin) ===
sidebar = QWidget()
sidebar.setMinimumWidth(200)
sidebar.setMaximumWidth(250)
sidebar_layout = QVBoxLayout(sidebar)
sidebar_layout.setContentsMargins(0, 0, 0, 0)
sidebar_layout.setSpacing(0)
# Notebooks header
notebooks_header = QLabel(self.tr("NOTEBOOKS"))
notebooks_header.setStyleSheet("padding: 8px; font-weight: bold; font-size: 11px; color: #888;")
sidebar_layout.addWidget(notebooks_header)
# Notebooks tree view
self.notebook_list = NotebookList(self.db)
self.notebook_list.notebook_selected.connect(self.on_notebook_selected)
self.notebook_list.trash_selected.connect(self.on_trash_selected)
self.notebook_list.notebook_from_trash_selected.connect(self.on_deleted_notebook_selected)
self.notebook_list.tag_selected.connect(self.on_tag_selected)
sidebar_layout.addWidget(self.notebook_list)
nb_buttons_layout = QHBoxLayout()
nb_buttons_layout.setSpacing(5)
nb_buttons_layout.setContentsMargins(5, 5, 5, 5)
new_notebook_btn = QPushButton(self.tr("+"))
new_notebook_btn.setMaximumWidth(35)
new_notebook_btn.setToolTip(self.tr("New Notebook"))
new_notebook_btn.clicked.connect(self.create_notebook)
nb_buttons_layout.addWidget(new_notebook_btn)
nb_buttons_layout.addStretch()
sidebar_layout.addLayout(nb_buttons_layout)
# === MIDDLE COLUMN (Notes list) ===
notes_column = QWidget()
notes_column.setMinimumWidth(250)
notes_column.setMaximumWidth(350)
notes_layout = QVBoxLayout(notes_column)
notes_layout.setContentsMargins(0, 0, 0, 0)
notes_layout.setSpacing(0)
notes_header = QLabel(self.tr("NOTES"))
notes_header.setStyleSheet("padding: 8px; font-weight: bold; font-size: 11px; color: #888;")
notes_layout.addWidget(notes_header)
self.note_list = NoteList(self.db)
self.note_list.note_selected.connect(self.on_note_selected)
self.note_list.note_restored.connect(self.on_item_restored)
notes_layout.addWidget(self.note_list)
new_note_btn = QPushButton(self.tr("+ New Note"))
new_note_btn.clicked.connect(self.create_note)
notes_layout.addWidget(new_note_btn)
# === EDITOR ===
self.editor = EditorWidget(self.db, self.profile_dir, self.language)
self.editor.note_saved.connect(self.on_note_saved)
# Add to main splitter
main_splitter.addWidget(sidebar)
main_splitter.addWidget(notes_column)
main_splitter.addWidget(self.editor)
main_splitter.setSizes([200, 300, 900])
main_layout.addWidget(main_splitter)
# Load initial notebooks
self.notebook_list.load_notebooks()
def create_notebook(self):
"""Create a new notebook"""
from PyQt6.QtWidgets import QInputDialog
name, ok = QInputDialog.getText(self, self.tr("New Notebook"), self.tr("Notebook name:"))
if ok and name:
notebook_id = self.db.create_notebook(name)
self.notebook_list.load_notebooks()
QMessageBox.information(self, self.tr("Success"), self.tr("Notebook '{}' created!").format(name))
def create_note(self):
"""Create a new note in the current notebook"""
if not self.current_notebook_id:
QMessageBox.warning(self, self.tr("Warning"), self.tr("Please select a notebook first!"))
return
from PyQt6.QtWidgets import QInputDialog
title, ok = QInputDialog.getText(self, self.tr("New Note"), self.tr("Note title:"))
if ok and title:
note_id = self.db.create_note(self.current_notebook_id, title)
self.note_list.load_notes(self.current_notebook_id)
QMessageBox.information(self, self.tr("Success"), self.tr("Note '{}' created!").format(title))
def on_notebook_selected(self, notebook_id: int):
"""Handle notebook selection"""
self.current_notebook_id = notebook_id
self.current_note_id = None
self.note_list.load_notes(notebook_id)
def on_note_selected(self, note_id: int):
"""Handle note selection"""
self.current_note_id = note_id
note = self.db.get_note(note_id)
if note:
# Check if we're in trash view or if the note is deleted
is_trash = self.note_list.is_trash_view or note.get('is_deleted', 0) == 1
self.editor.load_note(note, read_only=is_trash)
def on_note_saved(self):
"""Update note list after saving"""
if self.current_notebook_id:
self.note_list.load_notes(self.current_notebook_id)
def on_trash_selected(self):
"""Handle trash selection - show all deleted notes"""
self.current_notebook_id = None
self.current_note_id = None
self.note_list.load_deleted_notes()
def on_deleted_notebook_selected(self, notebook_id: int):
"""Handle selection of deleted notebook from trash"""
self.current_notebook_id = notebook_id
self.current_note_id = None
self.note_list.load_deleted_notes_from_notebook(notebook_id)
def on_item_restored(self):
"""Handle item restored from trash"""
self.notebook_list.load_notebooks()
if self.current_notebook_id:
self.note_list.load_notes(self.current_notebook_id)
def on_tag_selected(self, tag_id: int):
"""Handle tag selection - show notes with this tag"""
self.current_notebook_id = None
self.current_note_id = None
self.note_list.load_notes_by_tag(tag_id)
def parse_arguments():
"""Parse command-line arguments"""
parser = argparse.ArgumentParser(
description="Notes - Note management application with speech-to-text"
)
parser.add_argument(
'--profile',
type=str,
default=None,
help='Profile directory (default: ~/.config/Notes)'
)
parser.add_argument(
'--lang',
type=str,
default=None,
help='Language code (e.g., en, it). Auto-detected if not specified.'
)
return parser.parse_args()
def main():
"""Main function"""
# Parse arguments
args = parse_arguments()
# Determine profile directory
if args.profile:
profile_dir = Path(args.profile).expanduser().resolve()
else:
profile_dir = Path.home() / '.config' / 'Notes'
# Create directory if it doesn't exist
profile_dir.mkdir(parents=True, exist_ok=True)
print(f"[INFO] Profile directory: {profile_dir}")
# Start application
app = QApplication(sys.argv)
app.setApplicationName("Notes")
app.setOrganizationName("Notes")
# Determine language
if args.lang:
lang_code = args.lang
else:
# Auto-detect system language
system_locale = QLocale.system().name() # e.g., 'en_US', 'it_IT'
lang_code = system_locale.split('_')[0] # Extract 'en', 'it'
print(f"[INFO] Language: {lang_code}")
# Load translations
translator = QTranslator()
i18n_dir = Path(__file__).parent / 'i18n'
translation_file = i18n_dir / f"{lang_code}.qm"
if translation_file.exists():
if translator.load(str(translation_file)):
app.installTranslator(translator)
print(f"[OK] Loaded translation: {translation_file}")
else:
print(f"[WARNING] Failed to load translation: {translation_file}")
else:
print(f"[INFO] Translation file not found: {translation_file}, using English default")
window = NotesApp(profile_dir, lang_code)
window.show()
sys.exit(app.exec())
if __name__ == "__main__":
main()