-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbookmarks.py
More file actions
188 lines (151 loc) · 5.93 KB
/
Copy pathbookmarks.py
File metadata and controls
188 lines (151 loc) · 5.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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Bookmarks system"""
import json
import os
from PyQt6.QtWidgets import (
QDockWidget, QWidget, QVBoxLayout, QHBoxLayout,
QListWidget, QLineEdit, QPushButton, QLabel,
QListWidgetItem, QMessageBox
)
from PyQt6.QtCore import Qt, pyqtSignal
class BookmarksWidget(QWidget):
"""Bookmarks panel"""
bookmark_clicked = pyqtSignal(str) # URL
def __init__(self, parent=None):
super().__init__(parent)
self.bookmarks_file = 'bookmarks.json'
self.bookmarks = []
self.setup_ui()
self.load_bookmarks()
def setup_ui(self):
"""Setup UI"""
layout = QVBoxLayout()
layout.setContentsMargins(10, 10, 10, 10)
layout.setSpacing(8)
# Title with icon
title_layout = QHBoxLayout()
title_icon = QLabel("▶")
title_icon.setStyleSheet("font-size: 14px; color: #0078d4;")
title_text = QLabel("Bookmarks")
title_text.setStyleSheet("font-size: 15px; font-weight: 600;")
title_layout.addWidget(title_icon)
title_layout.addWidget(title_text)
title_layout.addStretch()
layout.addLayout(title_layout)
# Add bookmark section
add_layout = QVBoxLayout()
add_layout.setSpacing(6)
self.url_input = QLineEdit()
self.url_input.setPlaceholderText("URL...")
add_layout.addWidget(self.url_input)
self.title_input = QLineEdit()
self.title_input.setPlaceholderText("Title...")
add_layout.addWidget(self.title_input)
btn_layout = QHBoxLayout()
add_btn = QPushButton("+ Add")
add_btn.setToolTip("Add bookmark")
add_btn.clicked.connect(self.add_bookmark)
btn_layout.addWidget(add_btn)
del_btn = QPushButton("− Remove")
del_btn.setToolTip("Remove selected bookmark")
del_btn.clicked.connect(self.remove_bookmark)
btn_layout.addWidget(del_btn)
clear_btn = QPushButton("✕ Clear All")
clear_btn.setToolTip("Remove all bookmarks")
clear_btn.clicked.connect(self.clear_all_bookmarks)
btn_layout.addWidget(clear_btn)
add_layout.addLayout(btn_layout)
layout.addLayout(add_layout)
# Bookmarks list
self.list_widget = QListWidget()
self.list_widget.itemDoubleClicked.connect(self.on_bookmark_double_click)
layout.addWidget(self.list_widget)
self.setLayout(layout)
def add_bookmark(self, url=None, title=None):
"""Add bookmark"""
if url is None:
url = self.url_input.text().strip()
if title is None:
title = self.title_input.text().strip()
if not url:
return
if not title:
title = url
# Check if exists
for bm in self.bookmarks:
if bm['url'] == url:
return
bookmark = {'url': url, 'title': title}
self.bookmarks.append(bookmark)
self.save_bookmarks()
self.refresh_list()
self.url_input.clear()
self.title_input.clear()
def remove_bookmark(self):
"""Remove selected bookmark"""
current = self.list_widget.currentRow()
if current >= 0:
del self.bookmarks[current]
self.save_bookmarks()
self.refresh_list()
def on_bookmark_double_click(self, item):
"""Handle double click"""
index = self.list_widget.row(item)
if 0 <= index < len(self.bookmarks):
url = self.bookmarks[index]['url']
self.bookmark_clicked.emit(url)
def refresh_list(self):
"""Refresh bookmarks list"""
self.list_widget.clear()
for bm in self.bookmarks:
item = QListWidgetItem(f"▸ {bm['title']}")
item.setToolTip(bm['url'])
self.list_widget.addItem(item)
def clear_all_bookmarks(self):
"""Clear all bookmarks"""
from PyQt6.QtWidgets import QMessageBox
reply = QMessageBox.question(
self,
'Clear All Bookmarks',
'Are you sure you want to remove all bookmarks?',
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,
QMessageBox.StandardButton.No
)
if reply == QMessageBox.StandardButton.Yes:
self.bookmarks.clear()
self.save_bookmarks()
self.refresh_list()
def load_bookmarks(self):
"""Load bookmarks from file"""
if os.path.exists(self.bookmarks_file):
try:
with open(self.bookmarks_file, 'r', encoding='utf-8') as f:
self.bookmarks = json.load(f)
self.refresh_list()
except:
self.bookmarks = []
def save_bookmarks(self):
"""Save bookmarks to file"""
with open(self.bookmarks_file, 'w', encoding='utf-8') as f:
json.dump(self.bookmarks, f, ensure_ascii=False, indent=2)
def get_current_url_title(self):
"""Get URL and title from parent browser"""
browser = self.parent()
while browser and not hasattr(browser, 'tabs'):
browser = browser.parent()
if browser and hasattr(browser, 'tabs'):
tab = browser.tabs.currentWidget()
if tab:
return tab.get_url(), tab.get_title()
return "", ""
class BookmarksDock(QDockWidget):
"""Bookmarks dock widget"""
def __init__(self, parent=None):
super().__init__("Bookmarks", parent)
self.setAllowedAreas(Qt.DockWidgetArea.LeftDockWidgetArea |
Qt.DockWidgetArea.RightDockWidgetArea)
self.bookmarks_widget = BookmarksWidget(self)
self.setWidget(self.bookmarks_widget)
# Hide by default
self.hide()