askrobots / dbbasic-copypaste
Public
A clipboard manager for macOS: browse your clipboard history, text and images, and restore any item.
__main__.py · 11.6 KB · 350 lines
| 1 | #!/usr/bin/env python3 |
| 2 | """ |
| 3 | dbbasic-copypaste: Clipboard manager with history |
| 4 | """ |
| 5 | |
| 6 | import sys |
| 7 | from datetime import datetime |
| 8 | from PyQt5.QtWidgets import ( |
| 9 | QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, |
| 10 | QListWidget, QListWidgetItem, QTextEdit, QLabel, QPushButton, |
| 11 | QSplitter, QMessageBox |
| 12 | ) |
| 13 | from PyQt5.QtCore import Qt, QTimer, pyqtSignal |
| 14 | from PyQt5.QtGui import QPixmap, QImage, QClipboard |
| 15 | from io import BytesIO |
| 16 | from PIL import Image |
| 17 | |
| 18 | |
| 19 | class ClipboardItem: |
| 20 | """Represents a single clipboard history item""" |
| 21 | def __init__(self, content, content_type, timestamp=None): |
| 22 | self.content = content |
| 23 | self.content_type = content_type # 'text' or 'image' |
| 24 | self.timestamp = timestamp or datetime.now() |
| 25 | |
| 26 | def get_preview(self, max_length=50): |
| 27 | """Get a short preview of the content""" |
| 28 | if self.content_type == 'text': |
| 29 | text = self.content.replace('\n', ' ').strip() |
| 30 | if len(text) > max_length: |
| 31 | return text[:max_length] + '...' |
| 32 | return text |
| 33 | elif self.content_type == 'image': |
| 34 | return '[Image]' |
| 35 | return '[Unknown]' |
| 36 | |
| 37 | def get_display_text(self): |
| 38 | """Get the full display text for the list""" |
| 39 | time_str = self.timestamp.strftime('%H:%M:%S') |
| 40 | preview = self.get_preview() |
| 41 | return f"{time_str} - {preview}" |
| 42 | |
| 43 | |
| 44 | class ClipboardManager(QMainWindow): |
| 45 | """Main clipboard manager window""" |
| 46 | |
| 47 | def __init__(self): |
| 48 | super().__init__() |
| 49 | self.clipboard = QApplication.clipboard() |
| 50 | self.history = [] |
| 51 | self.max_history = 100 |
| 52 | self.monitoring = True |
| 53 | |
| 54 | self.init_ui() |
| 55 | self.start_monitoring() |
| 56 | |
| 57 | # Capture initial clipboard content |
| 58 | self.check_clipboard() |
| 59 | |
| 60 | def init_ui(self): |
| 61 | """Initialize the user interface""" |
| 62 | self.setWindowTitle('dbbasic-copypaste - Clipboard Manager') |
| 63 | self.setGeometry(100, 100, 900, 600) |
| 64 | |
| 65 | # Create central widget and main layout |
| 66 | central_widget = QWidget() |
| 67 | self.setCentralWidget(central_widget) |
| 68 | main_layout = QVBoxLayout(central_widget) |
| 69 | |
| 70 | # Create top bar with controls |
| 71 | top_bar = QHBoxLayout() |
| 72 | |
| 73 | self.status_label = QLabel('Monitoring clipboard...') |
| 74 | top_bar.addWidget(self.status_label) |
| 75 | |
| 76 | top_bar.addStretch() |
| 77 | |
| 78 | self.pause_button = QPushButton('Pause Monitoring') |
| 79 | self.pause_button.clicked.connect(self.toggle_monitoring) |
| 80 | top_bar.addWidget(self.pause_button) |
| 81 | |
| 82 | self.clear_button = QPushButton('Clear History') |
| 83 | self.clear_button.clicked.connect(self.clear_history) |
| 84 | top_bar.addWidget(self.clear_button) |
| 85 | |
| 86 | main_layout.addLayout(top_bar) |
| 87 | |
| 88 | # Create splitter for history list and preview |
| 89 | splitter = QSplitter(Qt.Horizontal) |
| 90 | |
| 91 | # Left side: History list |
| 92 | left_widget = QWidget() |
| 93 | left_layout = QVBoxLayout(left_widget) |
| 94 | left_layout.setContentsMargins(0, 0, 0, 0) |
| 95 | |
| 96 | list_label = QLabel('Clipboard History:') |
| 97 | left_layout.addWidget(list_label) |
| 98 | |
| 99 | self.history_list = QListWidget() |
| 100 | self.history_list.currentItemChanged.connect(self.on_history_selection_changed) |
| 101 | left_layout.addWidget(self.history_list) |
| 102 | |
| 103 | splitter.addWidget(left_widget) |
| 104 | |
| 105 | # Right side: Preview and actions |
| 106 | right_widget = QWidget() |
| 107 | right_layout = QVBoxLayout(right_widget) |
| 108 | right_layout.setContentsMargins(0, 0, 0, 0) |
| 109 | |
| 110 | preview_label = QLabel('Preview:') |
| 111 | right_layout.addWidget(preview_label) |
| 112 | |
| 113 | # Text preview |
| 114 | self.text_preview = QTextEdit() |
| 115 | self.text_preview.setReadOnly(True) |
| 116 | right_layout.addWidget(self.text_preview) |
| 117 | |
| 118 | # Image preview |
| 119 | self.image_preview = QLabel() |
| 120 | self.image_preview.setAlignment(Qt.AlignCenter) |
| 121 | self.image_preview.setMinimumSize(200, 200) |
| 122 | self.image_preview.hide() |
| 123 | right_layout.addWidget(self.image_preview) |
| 124 | |
| 125 | # Action buttons |
| 126 | button_layout = QHBoxLayout() |
| 127 | |
| 128 | self.restore_button = QPushButton('Restore to Clipboard') |
| 129 | self.restore_button.clicked.connect(self.restore_to_clipboard) |
| 130 | self.restore_button.setEnabled(False) |
| 131 | button_layout.addWidget(self.restore_button) |
| 132 | |
| 133 | self.delete_button = QPushButton('Delete Item') |
| 134 | self.delete_button.clicked.connect(self.delete_item) |
| 135 | self.delete_button.setEnabled(False) |
| 136 | button_layout.addWidget(self.delete_button) |
| 137 | |
| 138 | right_layout.addLayout(button_layout) |
| 139 | |
| 140 | splitter.addWidget(right_widget) |
| 141 | |
| 142 | # Set initial splitter sizes |
| 143 | splitter.setSizes([300, 600]) |
| 144 | |
| 145 | main_layout.addWidget(splitter) |
| 146 | |
| 147 | # Status bar |
| 148 | self.statusBar().showMessage('Ready') |
| 149 | |
| 150 | def start_monitoring(self): |
| 151 | """Start monitoring clipboard changes""" |
| 152 | self.timer = QTimer() |
| 153 | self.timer.timeout.connect(self.check_clipboard) |
| 154 | self.timer.start(500) # Check every 500ms |
| 155 | |
| 156 | def toggle_monitoring(self): |
| 157 | """Toggle clipboard monitoring on/off""" |
| 158 | self.monitoring = not self.monitoring |
| 159 | if self.monitoring: |
| 160 | self.pause_button.setText('Pause Monitoring') |
| 161 | self.status_label.setText('Monitoring clipboard...') |
| 162 | self.statusBar().showMessage('Monitoring resumed') |
| 163 | else: |
| 164 | self.pause_button.setText('Resume Monitoring') |
| 165 | self.status_label.setText('Monitoring paused') |
| 166 | self.statusBar().showMessage('Monitoring paused') |
| 167 | |
| 168 | def check_clipboard(self): |
| 169 | """Check for clipboard changes and add to history""" |
| 170 | if not self.monitoring: |
| 171 | return |
| 172 | |
| 173 | mime_data = self.clipboard.mimeData() |
| 174 | |
| 175 | if mime_data.hasImage(): |
| 176 | image = self.clipboard.image() |
| 177 | if not image.isNull(): |
| 178 | # Convert QImage to bytes for storage |
| 179 | byte_array = BytesIO() |
| 180 | # Convert QImage to PIL Image |
| 181 | width = image.width() |
| 182 | height = image.height() |
| 183 | ptr = image.bits() |
| 184 | ptr.setsize(height * width * 4) |
| 185 | pil_image = Image.frombytes('RGBA', (width, height), ptr.asstring()) |
| 186 | pil_image.save(byte_array, format='PNG') |
| 187 | image_bytes = byte_array.getvalue() |
| 188 | |
| 189 | # Check if this image is already the most recent |
| 190 | if self.history and self.history[0].content_type == 'image': |
| 191 | if self.history[0].content == image_bytes: |
| 192 | return |
| 193 | |
| 194 | self.add_to_history(image_bytes, 'image') |
| 195 | |
| 196 | elif mime_data.hasText(): |
| 197 | text = self.clipboard.text() |
| 198 | if text: |
| 199 | # Check if this text is already the most recent |
| 200 | if self.history and self.history[0].content_type == 'text': |
| 201 | if self.history[0].content == text: |
| 202 | return |
| 203 | |
| 204 | self.add_to_history(text, 'text') |
| 205 | |
| 206 | def add_to_history(self, content, content_type): |
| 207 | """Add a new item to the clipboard history""" |
| 208 | item = ClipboardItem(content, content_type) |
| 209 | self.history.insert(0, item) |
| 210 | |
| 211 | # Limit history size |
| 212 | if len(self.history) > self.max_history: |
| 213 | self.history = self.history[:self.max_history] |
| 214 | |
| 215 | self.update_history_list() |
| 216 | self.statusBar().showMessage(f'Added {content_type} to history', 2000) |
| 217 | |
| 218 | def update_history_list(self): |
| 219 | """Update the history list widget""" |
| 220 | self.history_list.clear() |
| 221 | for item in self.history: |
| 222 | list_item = QListWidgetItem(item.get_display_text()) |
| 223 | list_item.setData(Qt.UserRole, item) |
| 224 | self.history_list.addItem(list_item) |
| 225 | |
| 226 | # Automatically select the first (most recent) item |
| 227 | if self.history_list.count() > 0: |
| 228 | self.history_list.setCurrentRow(0) |
| 229 | |
| 230 | def on_history_selection_changed(self, current, previous): |
| 231 | """Handle history selection changes""" |
| 232 | if current is None: |
| 233 | self.text_preview.clear() |
| 234 | self.image_preview.clear() |
| 235 | self.image_preview.hide() |
| 236 | self.text_preview.show() |
| 237 | self.restore_button.setEnabled(False) |
| 238 | self.delete_button.setEnabled(False) |
| 239 | return |
| 240 | |
| 241 | item = current.data(Qt.UserRole) |
| 242 | |
| 243 | if item.content_type == 'text': |
| 244 | self.text_preview.setPlainText(item.content) |
| 245 | self.text_preview.show() |
| 246 | self.image_preview.hide() |
| 247 | elif item.content_type == 'image': |
| 248 | # Load image from bytes |
| 249 | byte_array = BytesIO(item.content) |
| 250 | pil_image = Image.open(byte_array) |
| 251 | |
| 252 | # Convert PIL Image to QPixmap |
| 253 | img_byte_arr = BytesIO() |
| 254 | pil_image.save(img_byte_arr, format='PNG') |
| 255 | img_byte_arr.seek(0) |
| 256 | |
| 257 | pixmap = QPixmap() |
| 258 | pixmap.loadFromData(img_byte_arr.getvalue()) |
| 259 | |
| 260 | # Scale to fit preview area while maintaining aspect ratio |
| 261 | scaled_pixmap = pixmap.scaled( |
| 262 | self.image_preview.width() - 20, |
| 263 | self.image_preview.height() - 20, |
| 264 | Qt.KeepAspectRatio, |
| 265 | Qt.SmoothTransformation |
| 266 | ) |
| 267 | |
| 268 | self.image_preview.setPixmap(scaled_pixmap) |
| 269 | self.text_preview.hide() |
| 270 | self.image_preview.show() |
| 271 | |
| 272 | self.restore_button.setEnabled(True) |
| 273 | self.delete_button.setEnabled(True) |
| 274 | |
| 275 | def restore_to_clipboard(self): |
| 276 | """Restore the selected item to clipboard""" |
| 277 | current = self.history_list.currentItem() |
| 278 | if current is None: |
| 279 | return |
| 280 | |
| 281 | item = current.data(Qt.UserRole) |
| 282 | |
| 283 | # Temporarily pause monitoring to avoid adding the restored item again |
| 284 | was_monitoring = self.monitoring |
| 285 | self.monitoring = False |
| 286 | |
| 287 | if item.content_type == 'text': |
| 288 | self.clipboard.setText(item.content) |
| 289 | self.statusBar().showMessage('Text restored to clipboard', 2000) |
| 290 | elif item.content_type == 'image': |
| 291 | # Convert bytes back to QImage |
| 292 | byte_array = BytesIO(item.content) |
| 293 | pil_image = Image.open(byte_array) |
| 294 | |
| 295 | # Convert PIL Image to QImage |
| 296 | img_byte_arr = BytesIO() |
| 297 | pil_image.save(img_byte_arr, format='PNG') |
| 298 | img_byte_arr.seek(0) |
| 299 | |
| 300 | qimage = QImage() |
| 301 | qimage.loadFromData(img_byte_arr.getvalue()) |
| 302 | |
| 303 | self.clipboard.setImage(qimage) |
| 304 | self.statusBar().showMessage('Image restored to clipboard', 2000) |
| 305 | |
| 306 | # Resume monitoring after a short delay |
| 307 | QTimer.singleShot(1000, lambda: setattr(self, 'monitoring', was_monitoring)) |
| 308 | |
| 309 | def delete_item(self): |
| 310 | """Delete the selected history item""" |
| 311 | current = self.history_list.currentItem() |
| 312 | if current is None: |
| 313 | return |
| 314 | |
| 315 | row = self.history_list.row(current) |
| 316 | self.history.pop(row) |
| 317 | self.history_list.takeItem(row) |
| 318 | self.statusBar().showMessage('Item deleted', 2000) |
| 319 | |
| 320 | def clear_history(self): |
| 321 | """Clear all clipboard history""" |
| 322 | reply = QMessageBox.question( |
| 323 | self, |
| 324 | 'Clear History', |
| 325 | 'Are you sure you want to clear all clipboard history?', |
| 326 | QMessageBox.Yes | QMessageBox.No, |
| 327 | QMessageBox.No |
| 328 | ) |
| 329 | |
| 330 | if reply == QMessageBox.Yes: |
| 331 | self.history.clear() |
| 332 | self.history_list.clear() |
| 333 | self.text_preview.clear() |
| 334 | self.image_preview.clear() |
| 335 | self.statusBar().showMessage('History cleared', 2000) |
| 336 | |
| 337 | |
| 338 | def main(): |
| 339 | """Main entry point""" |
| 340 | app = QApplication(sys.argv) |
| 341 | app.setApplicationName('dbbasic-copypaste') |
| 342 | |
| 343 | window = ClipboardManager() |
| 344 | window.show() |
| 345 | |
| 346 | sys.exit(app.exec_()) |
| 347 | |
| 348 | |
| 349 | if __name__ == '__main__': |
| 350 | main() |