-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinuxink.py
More file actions
813 lines (691 loc) · 28.5 KB
/
Copy pathlinuxink.py
File metadata and controls
813 lines (691 loc) · 28.5 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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
#!/usr/bin/env python3
"""
LinuxInk - A lightweight, hotkey-driven screen annotation overlay for Linux.
A professional presentation annotation tool with freehand drawing, shapes,
highlighting, and instant screenshots. Built with PyQt6.
Author: LinuxInk Team
License: MIT
"""
import sys
import json
from datetime import datetime
from pathlib import Path
from enum import Enum
from dataclasses import dataclass
from typing import List, Tuple, Optional
from PyQt6.QtCore import Qt, QPoint, QRect, QTimer, QSize
from PyQt6.QtGui import (
QPainter, QPen, QColor, QFont, QBrush, QPixmap,
QIcon, QPainterPath
)
from PyQt6.QtWidgets import (
QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout,
QPushButton, QLabel, QFrame, QButtonGroup, QMessageBox, QFileDialog
)
from PyQt6.QtCore import pyqtSignal
from PyQt6.QtGui import QShortcut, QKeySequence
# ============================================================================
# DATA STRUCTURES
# ============================================================================
class ToolType(Enum):
"""Available drawing tools."""
PEN = "pen"
LINE = "line"
RECTANGLE = "rectangle"
CIRCLE = "circle"
ARROW = "arrow"
HIGHLIGHTER = "highlighter"
TEXT = "text"
@dataclass
class Point:
"""A 2D point."""
x: int
y: int
def to_qpoint(self) -> QPoint:
"""Convert to PyQt6 QPoint."""
return QPoint(self.x, self.y)
@classmethod
def from_qpoint(cls, qp: QPoint) -> "Point":
"""Create from PyQt6 QPoint."""
return cls(qp.x(), qp.y())
@dataclass
class Annotation:
"""Base annotation object. Subclassed for each tool type."""
tool_type: ToolType
color: QColor
width: int
def draw(self, painter: QPainter):
"""Draw this annotation. Override in subclasses."""
pass
def contains_point(self, p: Point) -> bool:
"""Check if this annotation contains a point. Override in subclasses."""
return False
@dataclass
class PenAnnotation(Annotation):
"""Freehand pen stroke."""
points: List[Point] = None
def __post_init__(self):
if self.points is None:
self.points = []
def draw(self, painter: QPainter):
if len(self.points) < 2:
return
pen = QPen(self.color, self.width, Qt.PenStyle.SolidLine)
pen.setCapStyle(Qt.PenCapStyle.RoundCap)
pen.setJoinStyle(Qt.PenJoinStyle.RoundJoin)
painter.setPen(pen)
path = QPainterPath()
path.moveTo(self.points[0].to_qpoint())
for p in self.points[1:]:
path.lineTo(p.to_qpoint())
painter.drawPath(path)
def contains_point(self, p: Point) -> bool:
"""Check if point is near the path."""
for point in self.points:
dist = ((point.x - p.x) ** 2 + (point.y - p.y) ** 2) ** 0.5
if dist <= self.width + 5:
return True
return False
@dataclass
class LineAnnotation(Annotation):
"""Straight line."""
start: Point = None
end: Point = None
def draw(self, painter: QPainter):
if self.start is None or self.end is None:
return
pen = QPen(self.color, self.width, Qt.PenStyle.SolidLine)
pen.setCapStyle(Qt.PenCapStyle.RoundCap)
painter.setPen(pen)
painter.drawLine(self.start.to_qpoint(), self.end.to_qpoint())
def contains_point(self, p: Point) -> bool:
"""Check if point is near the line."""
if self.start is None or self.end is None:
return False
# Distance from point to line segment
dx = self.end.x - self.start.x
dy = self.end.y - self.start.y
if dx == 0 and dy == 0:
dist = ((p.x - self.start.x) ** 2 + (p.y - self.start.y) ** 2) ** 0.5
return dist <= self.width + 5
t = max(0, min(1, ((p.x - self.start.x) * dx + (p.y - self.start.y) * dy) / (dx * dx + dy * dy)))
closest_x = self.start.x + t * dx
closest_y = self.start.y + t * dy
dist = ((p.x - closest_x) ** 2 + (p.y - closest_y) ** 2) ** 0.5
return dist <= self.width + 5
@dataclass
class RectangleAnnotation(Annotation):
"""Rectangle outline."""
start: Point = None
end: Point = None
def draw(self, painter: QPainter):
if self.start is None or self.end is None:
return
pen = QPen(self.color, self.width, Qt.PenStyle.SolidLine)
painter.setPen(pen)
rect = QRect(self.start.to_qpoint(), self.end.to_qpoint())
painter.drawRect(rect)
def contains_point(self, p: Point) -> bool:
"""Check if point is on the rectangle outline."""
if self.start is None or self.end is None:
return False
x1, x2 = min(self.start.x, self.end.x), max(self.start.x, self.end.x)
y1, y2 = min(self.start.y, self.end.y), max(self.start.y, self.end.y)
margin = self.width + 5
on_h_edge = (abs(p.y - y1) <= margin or abs(p.y - y2) <= margin) and x1 <= p.x <= x2
on_v_edge = (abs(p.x - x1) <= margin or abs(p.x - x2) <= margin) and y1 <= p.y <= y2
return on_h_edge or on_v_edge
@dataclass
class CircleAnnotation(Annotation):
"""Circle/ellipse outline."""
start: Point = None
end: Point = None
def draw(self, painter: QPainter):
if self.start is None or self.end is None:
return
pen = QPen(self.color, self.width, Qt.PenStyle.SolidLine)
painter.setPen(pen)
rect = QRect(self.start.to_qpoint(), self.end.to_qpoint())
painter.drawEllipse(rect)
def contains_point(self, p: Point) -> bool:
"""Check if point is on the circle outline."""
if self.start is None or self.end is None:
return False
cx = (self.start.x + self.end.x) / 2
cy = (self.start.y + self.end.y) / 2
rx = abs(self.end.x - self.start.x) / 2
ry = abs(self.end.y - self.start.y) / 2
if rx == 0 or ry == 0:
return False
dist_from_ellipse = abs((((p.x - cx) / rx) ** 2 + ((p.y - cy) / ry) ** 2) ** 0.5 - 1)
return dist_from_ellipse <= (self.width + 5) / max(rx, ry)
@dataclass
class ArrowAnnotation(Annotation):
"""Arrow with line and arrowhead."""
start: Point = None
end: Point = None
def draw(self, painter: QPainter):
if self.start is None or self.end is None:
return
pen = QPen(self.color, self.width, Qt.PenStyle.SolidLine)
pen.setCapStyle(Qt.PenCapStyle.RoundCap)
painter.setPen(pen)
painter.drawLine(self.start.to_qpoint(), self.end.to_qpoint())
# Draw arrowhead
import math
dx = self.end.x - self.start.x
dy = self.end.y - self.start.y
angle = math.atan2(dy, dx)
arrow_size = max(10, self.width * 3)
p1_x = self.end.x - arrow_size * math.cos(angle - math.pi / 6)
p1_y = self.end.y - arrow_size * math.sin(angle - math.pi / 6)
p2_x = self.end.x - arrow_size * math.cos(angle + math.pi / 6)
p2_y = self.end.y - arrow_size * math.sin(angle + math.pi / 6)
painter.drawLine(self.end.to_qpoint(), QPoint(int(p1_x), int(p1_y)))
painter.drawLine(self.end.to_qpoint(), QPoint(int(p2_x), int(p2_y)))
def contains_point(self, p: Point) -> bool:
"""Check if point is near the arrow."""
if self.start is None or self.end is None:
return False
dx = self.end.x - self.start.x
dy = self.end.y - self.start.y
if dx == 0 and dy == 0:
dist = ((p.x - self.start.x) ** 2 + (p.y - self.start.y) ** 2) ** 0.5
return dist <= self.width + 5
t = max(0, min(1, ((p.x - self.start.x) * dx + (p.y - self.start.y) * dy) / (dx * dx + dy * dy)))
closest_x = self.start.x + t * dx
closest_y = self.start.y + t * dy
dist = ((p.x - closest_x) ** 2 + (p.y - closest_y) ** 2) ** 0.5
return dist <= self.width + 5
@dataclass
class HighlighterAnnotation(Annotation):
"""Semi-transparent highlighter."""
points: List[Point] = None
def __post_init__(self):
if self.points is None:
self.points = []
def draw(self, painter: QPainter):
if len(self.points) < 2:
return
# Make color semi-transparent
color = QColor(self.color)
color.setAlpha(100)
pen = QPen(color, self.width * 2, Qt.PenStyle.SolidLine)
pen.setCapStyle(Qt.PenCapStyle.RoundCap)
pen.setJoinStyle(Qt.PenJoinStyle.RoundJoin)
painter.setPen(pen)
path = QPainterPath()
path.moveTo(self.points[0].to_qpoint())
for p in self.points[1:]:
path.lineTo(p.to_qpoint())
painter.drawPath(path)
def contains_point(self, p: Point) -> bool:
"""Check if point is near the path."""
for point in self.points:
dist = ((point.x - p.x) ** 2 + (point.y - p.y) ** 2) ** 0.5
if dist <= self.width * 2 + 5:
return True
return False
@dataclass
class TextAnnotation(Annotation):
"""Text label."""
position: Point = None
text: str = ""
def draw(self, painter: QPainter):
if self.position is None or not self.text:
return
font = QFont("Arial", max(8, self.width * 2))
painter.setFont(font)
painter.setPen(self.color)
painter.drawText(self.position.to_qpoint(), self.text)
def contains_point(self, p: Point) -> bool:
"""Check if point is near the text."""
if self.position is None or not self.text:
return False
font = QFont("Arial", max(8, self.width * 2))
metrics = painter.fontMetrics() if hasattr(self, 'painter') else None
# Simple bounding box check
dist = ((p.x - self.position.x) ** 2 + (p.y - self.position.y) ** 2) ** 0.5
return dist <= 50
# ============================================================================
# FLOATING TOOLBAR
# ============================================================================
class FloatingToolbar(QFrame):
"""Collapsible floating toolbar docked to the right edge."""
tool_changed = pyqtSignal(ToolType)
color_changed = pyqtSignal(QColor)
width_changed = pyqtSignal(int)
clear_all = pyqtSignal()
undo = pyqtSignal()
redo = pyqtSignal()
spotlight_toggle = pyqtSignal(bool)
def __init__(self):
super().__init__()
self.setWindowFlags(
Qt.WindowType.FramelessWindowHint |
Qt.WindowType.WindowStaysOnTopHint |
Qt.WindowType.Tool
)
self.setStyleSheet("""
QFrame { background-color: #2b3e50; border-radius: 8px; }
QPushButton {
background-color: #34495e;
color: white;
border: 1px solid #7f8c8d;
border-radius: 4px;
padding: 6px;
font-size: 11px;
}
QPushButton:hover { background-color: #3d556e; }
QPushButton:pressed { background-color: #2a3f55; }
QPushButton:checked {
background-color: #e74c3c;
border: 1px solid #c0392b;
}
QLabel { color: #ecf0f1; font-size: 10px; }
""")
self.collapsed = False
self.current_tool = ToolType.PEN
self.current_color = QColor("red")
self.current_width = 2
self._setup_ui()
self._move_to_right_edge()
def _setup_ui(self):
"""Build toolbar UI."""
layout = QVBoxLayout()
layout.setContentsMargins(8, 8, 8, 8)
layout.setSpacing(6)
# Toggle button
toggle_btn = QPushButton("◄")
toggle_btn.setMaximumWidth(30)
toggle_btn.clicked.connect(self._toggle_collapse)
layout.addWidget(toggle_btn)
self.content_layout = QVBoxLayout()
self.content_layout.setSpacing(6)
# Tools section
layout.addWidget(QLabel("TOOLS"))
self.tool_buttons = {}
for tool in ToolType:
btn = QPushButton(tool.value.upper())
btn.setCheckable(True)
btn.clicked.connect(lambda checked, t=tool: self._select_tool(t))
self.tool_buttons[tool] = btn
self.content_layout.addWidget(btn)
self.tool_buttons[ToolType.PEN].setChecked(True)
# Colors section
layout.addWidget(QLabel("COLORS"))
self.color_buttons = {}
colors = [
("Red", QColor("red")),
("Green", QColor("green")),
("Blue", QColor("blue")),
("Yellow", QColor("yellow")),
("White", QColor("white")),
("Orange", QColor("orange")),
("Cyan", QColor("cyan")),
]
for label, color in colors:
btn = QPushButton(label)
btn.setCheckable(True)
btn.setStyleSheet(f"""
QPushButton {{ background-color: {color.name()}; }}
QPushButton:checked {{ border: 2px solid white; }}
""")
btn.clicked.connect(lambda checked, c=color: self._select_color(c))
self.color_buttons[color.name()] = btn
self.content_layout.addWidget(btn)
self.color_buttons["red"].setChecked(True)
# Width section
layout.addWidget(QLabel("WIDTH"))
self.width_buttons = {}
widths = [("Thin", 2), ("Medium", 5), ("Thick", 10)]
for label, w in widths:
btn = QPushButton(label)
btn.setCheckable(True)
btn.clicked.connect(lambda checked, width=w: self._select_width(width))
self.width_buttons[w] = btn
self.content_layout.addWidget(btn)
self.width_buttons[2].setChecked(True)
# Actions section
layout.addWidget(QLabel("ACTIONS"))
undo_btn = QPushButton("Undo (Ctrl+Z)")
undo_btn.clicked.connect(self.undo.emit)
self.content_layout.addWidget(undo_btn)
redo_btn = QPushButton("Redo (Ctrl+Y)")
redo_btn.clicked.connect(self.redo.emit)
self.content_layout.addWidget(redo_btn)
clear_btn = QPushButton("Clear All (F7)")
clear_btn.clicked.connect(self.clear_all.emit)
self.content_layout.addWidget(clear_btn)
spotlight_btn = QPushButton("Spotlight (F10)")
spotlight_btn.clicked.connect(lambda: self.spotlight_toggle.emit(True))
self.content_layout.addWidget(spotlight_btn)
layout.addLayout(self.content_layout)
layout.addStretch()
self.setLayout(layout)
def _toggle_collapse(self):
"""Toggle toolbar collapse."""
self.collapsed = not self.collapsed
for widget in self.content_layout.parentWidget().findChildren(QWidget):
if widget != self and widget.parent() is not self.content_layout.parentWidget():
widget.setVisible(not self.collapsed)
def _select_tool(self, tool: ToolType):
"""Select a drawing tool."""
for t, btn in self.tool_buttons.items():
btn.setChecked(t == tool)
self.current_tool = tool
self.tool_changed.emit(tool)
def _select_color(self, color: QColor):
"""Select a color."""
for name, btn in self.color_buttons.items():
btn.setChecked(name == color.name())
self.current_color = color
self.color_changed.emit(color)
def _select_width(self, width: int):
"""Select stroke width."""
for w, btn in self.width_buttons.items():
btn.setChecked(w == width)
self.current_width = width
self.width_changed.emit(width)
def _move_to_right_edge(self):
"""Position toolbar at right edge of screen."""
screen = QApplication.primaryScreen()
geom = screen.geometry()
self.move(geom.width() - self.width() - 20, 50)
# ============================================================================
# MAIN ANNOTATION OVERLAY
# ============================================================================
class AnnotationOverlay(QMainWindow):
"""Main fullscreen transparent overlay for annotations."""
def __init__(self):
super().__init__()
# Window properties
self.setWindowFlags(
Qt.WindowType.FramelessWindowHint |
Qt.WindowType.WindowStaysOnTopHint |
Qt.WindowType.Tool
)
self.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground, True)
# State
self.annotations: List[Annotation] = []
self.undo_stack: List[List[Annotation]] = []
self.redo_stack: List[List[Annotation]] = []
self.current_tool = ToolType.PEN
self.current_color = QColor("red")
self.current_width = 2
self.is_drawing = False
self.drawing_start = None
self.current_annotation = None
self.is_passthrough = False
self.selected_annotation = None
self.is_spotlight_active = False
self.spotlight_radius = 100
# Text input state
self.text_input_mode = False
self.text_input_position = None
# Toolbar
self.toolbar = FloatingToolbar()
self.toolbar.tool_changed.connect(self._on_tool_changed)
self.toolbar.color_changed.connect(self._on_color_changed)
self.toolbar.width_changed.connect(self._on_width_changed)
self.toolbar.clear_all.connect(self._clear_all)
self.toolbar.undo.connect(self._undo)
self.toolbar.redo.connect(self._redo)
self.toolbar.spotlight_toggle.connect(self._toggle_spotlight)
# Setup fullscreen
self._setup_fullscreen()
# Hotkeys
self._setup_hotkeys()
# Mouse tracking
self.setMouseTracking(True)
self.show()
self.toolbar.show()
def _setup_fullscreen(self):
"""Setup fullscreen overlay."""
screen = QApplication.primaryScreen()
geom = screen.geometry()
self.setGeometry(geom)
def _setup_hotkeys(self):
"""Register global hotkeys."""
QShortcut(QKeySequence(Qt.Key.Key_F6), self, self._toggle_passthrough)
QShortcut(QKeySequence(Qt.Key.Key_F7), self, self._clear_all)
QShortcut(QKeySequence(Qt.Key.Key_F8), self, self._undo)
QShortcut(QKeySequence(Qt.Modifier.CTRL + Qt.Key.Key_Z), self, self._undo)
QShortcut(QKeySequence(Qt.Key.Key_F9), self, self._save_screenshot)
QShortcut(QKeySequence(Qt.Modifier.CTRL + Qt.Key.Key_Y), self, self._redo)
QShortcut(QKeySequence(Qt.Key.Key_F10), self, self._toggle_spotlight)
QShortcut(QKeySequence(Qt.Key.Key_Escape), self, self.close)
QShortcut(QKeySequence(Qt.Key.Key_Delete), self, self._delete_selected)
def _toggle_passthrough(self):
"""Toggle between active and passthrough mode."""
self.is_passthrough = not self.is_passthrough
if self.is_passthrough:
self.setWindowFlag(Qt.WindowType.WindowTransparentForInput, True)
else:
self.setWindowFlag(Qt.WindowType.WindowTransparentForInput, False)
self.show()
self._show_toast(f"Mode: {'Passthrough' if self.is_passthrough else 'Active'}")
def _toggle_spotlight(self):
"""Toggle spotlight mode."""
self.is_spotlight_active = not self.is_spotlight_active
self.update()
self._show_toast(f"Spotlight: {'ON' if self.is_spotlight_active else 'OFF'}")
def _on_tool_changed(self, tool: ToolType):
"""Handle tool change from toolbar."""
self.current_tool = tool
def _on_color_changed(self, color: QColor):
"""Handle color change from toolbar."""
self.current_color = color
def _on_width_changed(self, width: int):
"""Handle width change from toolbar."""
self.current_width = width
def _clear_all(self):
"""Clear all annotations."""
if self.annotations:
self.undo_stack.append([ann for ann in self.annotations])
self.redo_stack.clear()
self.annotations.clear()
self.selected_annotation = None
self.update()
def _undo(self):
"""Undo last annotation."""
if self.annotations or self.undo_stack:
self.redo_stack.append([ann for ann in self.annotations])
if self.undo_stack:
self.annotations = [ann for ann in self.undo_stack.pop()]
else:
self.annotations.clear()
self.selected_annotation = None
self.update()
def _redo(self):
"""Redo last undone annotation."""
if self.redo_stack:
self.undo_stack.append([ann for ann in self.annotations])
self.annotations = [ann for ann in self.redo_stack.pop()]
self.selected_annotation = None
self.update()
def _delete_selected(self):
"""Delete selected annotation."""
if self.selected_annotation and self.selected_annotation in self.annotations:
self.undo_stack.append([ann for ann in self.annotations])
self.redo_stack.clear()
self.annotations.remove(self.selected_annotation)
self.selected_annotation = None
self.update()
def _save_screenshot(self):
"""Save fullscreen screenshot with annotations."""
screen = QApplication.primaryScreen()
screenshot = screen.grabWindow(0)
# Draw annotations onto screenshot
painter = QPainter(screenshot)
for ann in self.annotations:
ann.draw(painter)
painter.end()
# Save to Desktop
desktop = Path.home() / "Desktop"
desktop.mkdir(exist_ok=True)
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
filepath = desktop / f"linuxink_{timestamp}.png"
screenshot.save(str(filepath))
self._show_toast(f"Saved to Desktop")
def _show_toast(self, message: str):
"""Show temporary notification toast."""
# Simple implementation: print to terminal and briefly highlight
print(f"[LinuxInk] {message}")
# TODO: Implement visual toast in corner
def mousePressEvent(self, event):
"""Handle mouse press."""
if self.is_passthrough:
super().mousePressEvent(event)
return
pos = Point.from_qpoint(event.pos())
if event.button() == Qt.MouseButton.LeftButton:
# Check if clicking on existing annotation (select mode)
for ann in reversed(self.annotations):
if ann.contains_point(pos):
self.selected_annotation = ann
self.update()
return
# Start new annotation
self.is_drawing = True
self.drawing_start = pos
self.undo_stack.append([ann for ann in self.annotations])
self.redo_stack.clear()
self.selected_annotation = None
if self.current_tool == ToolType.PEN:
self.current_annotation = PenAnnotation(
tool_type=ToolType.PEN,
color=self.current_color,
width=self.current_width,
points=[pos]
)
elif self.current_tool == ToolType.HIGHLIGHTER:
self.current_annotation = HighlighterAnnotation(
tool_type=ToolType.HIGHLIGHTER,
color=self.current_color,
width=self.current_width,
points=[pos]
)
elif self.current_tool == ToolType.LINE:
self.current_annotation = LineAnnotation(
tool_type=ToolType.LINE,
color=self.current_color,
width=self.current_width,
start=pos,
end=pos
)
elif self.current_tool == ToolType.RECTANGLE:
self.current_annotation = RectangleAnnotation(
tool_type=ToolType.RECTANGLE,
color=self.current_color,
width=self.current_width,
start=pos,
end=pos
)
elif self.current_tool == ToolType.CIRCLE:
self.current_annotation = CircleAnnotation(
tool_type=ToolType.CIRCLE,
color=self.current_color,
width=self.current_width,
start=pos,
end=pos
)
elif self.current_tool == ToolType.ARROW:
self.current_annotation = ArrowAnnotation(
tool_type=ToolType.ARROW,
color=self.current_color,
width=self.current_width,
start=pos,
end=pos
)
elif self.current_tool == ToolType.TEXT:
self.current_annotation = TextAnnotation(
tool_type=ToolType.TEXT,
color=self.current_color,
width=self.current_width,
position=pos,
text=""
)
self.text_input_mode = True
def mouseMoveEvent(self, event):
"""Handle mouse move."""
if self.is_passthrough:
super().mouseMoveEvent(event)
return
if self.is_drawing and self.current_annotation:
pos = Point.from_qpoint(event.pos())
if isinstance(self.current_annotation, (PenAnnotation, HighlighterAnnotation)):
self.current_annotation.points.append(pos)
elif isinstance(self.current_annotation, (LineAnnotation, ArrowAnnotation)):
self.current_annotation.end = pos
elif isinstance(self.current_annotation, (RectangleAnnotation, CircleAnnotation)):
self.current_annotation.end = pos
self.update()
def mouseReleaseEvent(self, event):
"""Handle mouse release."""
if self.is_passthrough:
super().mouseReleaseEvent(event)
return
if event.button() == Qt.MouseButton.LeftButton and self.is_drawing:
if self.current_annotation:
self.annotations.append(self.current_annotation)
self.is_drawing = False
self.current_annotation = None
self.update()
def keyPressEvent(self, event):
"""Handle key press for text input."""
if self.text_input_mode and self.current_annotation:
if event.key() == Qt.Key.Key_Return or event.key() == Qt.Key.Key_Enter:
self.annotations.append(self.current_annotation)
self.text_input_mode = False
self.current_annotation = None
self.update()
elif event.key() == Qt.Key.Key_Backspace:
self.current_annotation.text = self.current_annotation.text[:-1]
self.update()
elif not event.text().isprintable() or event.text() == ' ':
pass
else:
self.current_annotation.text += event.text()
self.update()
else:
super().keyPressEvent(event)
def paintEvent(self, event):
"""Paint annotations and overlay."""
painter = QPainter(self)
painter.setRenderHint(QPainter.RenderHint.Antialiasing, True)
# Draw spotlight if active
if self.is_spotlight_active:
self._draw_spotlight(painter)
# Draw all annotations
for ann in self.annotations:
ann.draw(painter)
# Draw current annotation being drawn
if self.current_annotation:
self.current_annotation.draw(painter)
# Draw selection highlight
if self.selected_annotation:
painter.setPen(QPen(QColor(255, 255, 255, 128), 2, Qt.PenStyle.DashLine))
if isinstance(self.selected_annotation, PenAnnotation):
for p in self.selected_annotation.points:
painter.drawEllipse(p.to_qpoint(), 3, 3)
painter.end()
def _draw_spotlight(self, painter: QPainter):
"""Draw spotlight effect."""
# Dark overlay
painter.fillRect(self.rect(), QColor(0, 0, 0, 200))
# Clear circle around cursor
pos = self.mapFromGlobal(self.cursor().pos())
painter.setCompositionMode(QPainter.CompositionMode.CompositionMode_Clear)
painter.drawEllipse(pos, self.spotlight_radius, self.spotlight_radius)
painter.setCompositionMode(QPainter.CompositionMode.CompositionMode_SourceOver)
# ============================================================================
# APPLICATION ENTRY POINT
# ============================================================================
def main():
"""Launch LinuxInk."""
app = QApplication(sys.argv)
overlay = AnnotationOverlay()
sys.exit(app.exec())
if __name__ == "__main__":
main()