-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
589 lines (482 loc) · 17.9 KB
/
mainwindow.cpp
File metadata and controls
589 lines (482 loc) · 17.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
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
#include "mainwindow.h"
#include "windowhelper.h"
#include <QPainter>
#include <QMouseEvent>
#include <QColorDialog>
#include <QApplication>
#include <QScreen>
#include <QGraphicsBlurEffect>
#include <functional>
#ifdef HAVE_KWINDOWSYSTEM
#include <KWindowSystem>
#include <KWindowEffects>
#endif
MainWindow::MainWindow(QWidget *parent)
: QWidget(parent)
, currentMode(MODE_CLICK)
, isDrawing(false)
, brushColor(Qt::red)
, brushSize(5)
, eraserSize(50)
, isMovingToolbar(false)
{
createToolbar();
createColorPicker();
WindowHelper::setupOverlayWindow(this);
initializeCanvas();
applyBlurEffect();
setMode(MODE_CLICK);
}
MainWindow::~MainWindow()
{
}
void MainWindow::initializeCanvas()
{
// Get the screen's device pixel ratio
QScreen *screen = QApplication::primaryScreen();
qreal dpr = screen->devicePixelRatio();
QSize screenSize = screen->size();
// Create canvas with physical pixel dimensions
int physicalWidth = screenSize.width() * dpr;
int physicalHeight = screenSize.height() * dpr;
canvas = QPixmap(physicalWidth, physicalHeight);
canvas.setDevicePixelRatio(dpr);
canvas.fill(Qt::transparent);
qDebug() << "Canvas initialized:";
qDebug() << " Logical size:" << screenSize;
qDebug() << " Device pixel ratio:" << dpr;
qDebug() << " Physical size:" << physicalWidth << "x" << physicalHeight;
qDebug() << " Canvas size:" << canvas.size();
}
void MainWindow::temporarilyUnmaskForOperation(std::function<void()> operation)
{
QString platform = WindowHelper::detectPlatform();
if (platform == "wayland" && !currentInputRegion.isEmpty()) {
// Store current mask
QRegion savedRegion = currentInputRegion;
// Temporarily clear mask to allow full window rendering
clearMask();
// Perform the operation
operation();
// Force synchronous repaint while mask is cleared
// This ensures paintEvent() happens NOW before we restore the mask
repaint();
// Now restore mask
setMask(savedRegion);
qDebug() << "Wayland: Temporarily unmasked for operation";
} else {
// On X11 or when no mask is set, just do the operation
operation();
}
}
void MainWindow::createToolbar()
{
// Create toolbar widget
toolbar = new QWidget(this);
toolbar->setFixedSize(220, 450);
toolbar->move(20, 20);
toolbar->setObjectName("toolbarContainer");
// Install event filter to handle toolbar dragging
toolbar->installEventFilter(this);
// Main layout for toolbar
QVBoxLayout *mainLayout = new QVBoxLayout(toolbar);
mainLayout->setContentsMargins(0, 0, 0, 0);
mainLayout->setSpacing(0);
// Create title bar
titleBar = new QWidget(toolbar);
titleBar->setObjectName("titleBar");
titleBar->setFixedHeight(35);
titleBar->setCursor(Qt::OpenHandCursor);
QHBoxLayout *titleLayout = new QHBoxLayout(titleBar);
titleLayout->setContentsMargins(10, 5, 10, 5);
titleBar->setLayout(titleLayout);
mainLayout->addWidget(titleBar);
// Content area
QWidget *contentWidget = new QWidget(toolbar);
contentWidget->setObjectName("contentWidget");
// Apply stylesheet using object names to target specific widgets only
toolbar->setStyleSheet(
"#titleBar {"
" background-color: rgba(80, 80, 80, 128);"
" border-top-left-radius: 10px;"
" border-top-right-radius: 10px;"
"}"
"#contentWidget {"
" background-color: rgba(50, 50, 50, 128);"
" border-bottom-left-radius: 10px;"
" border-bottom-right-radius: 10px;"
"}"
"QLabel {"
" color: white;"
" background: transparent;"
"}"
);
// Create content layout
QVBoxLayout *contentLayout = new QVBoxLayout(contentWidget);
contentLayout->setContentsMargins(10, 10, 10, 10);
contentLayout->setSpacing(10);
// Mode buttons
btnClick = new QPushButton("🖱️ Click", contentWidget);
btnClick->setCheckable(true);
btnClick->setChecked(true);
contentLayout->addWidget(btnClick);
connect(btnClick, &QPushButton::clicked, this, &MainWindow::onClickClicked);
btnBrush = new QPushButton("✏️ Brush", contentWidget);
btnBrush->setCheckable(true);
contentLayout->addWidget(btnBrush);
connect(btnBrush, &QPushButton::clicked, this, &MainWindow::onBrushClicked);
btnEraser = new QPushButton("🧹 Eraser", contentWidget);
btnEraser->setCheckable(true);
contentLayout->addWidget(btnEraser);
connect(btnEraser, &QPushButton::clicked, this, &MainWindow::onEraserClicked);
contentLayout->addSpacing(10);
// Brush/Eraser size
brushSizeLabel = new QLabel("Size:", contentWidget);
contentLayout->addWidget(brushSizeLabel);
brushSizeSlider = new QSlider(Qt::Horizontal, contentWidget);
brushSizeSlider->setMinimum(1);
brushSizeSlider->setMaximum(10);
brushSizeSlider->setValue(5);
contentLayout->addWidget(brushSizeSlider);
connect(brushSizeSlider, &QSlider::valueChanged, this, &MainWindow::onBrushSizeChanged);
contentLayout->addSpacing(10);
// Color picker
btnColorPicker = new QPushButton("🎨 Color", contentWidget);
contentLayout->addWidget(btnColorPicker);
connect(btnColorPicker, &QPushButton::clicked, this, &MainWindow::onColorPickerClicked);
// Clear button
btnClear = new QPushButton("🗑️ Clear", contentWidget);
contentLayout->addWidget(btnClear);
connect(btnClear, &QPushButton::clicked, this, &MainWindow::onClearClicked);
contentLayout->addStretch();
// Quit button at bottom
btnQuit = new QPushButton("❌ Quit", contentWidget);
contentLayout->addWidget(btnQuit);
connect(btnQuit, &QPushButton::clicked, this, &MainWindow::onQuitClicked);
contentWidget->setLayout(contentLayout);
mainLayout->addWidget(contentWidget);
toolbar->setLayout(mainLayout);
}
void MainWindow::createColorPicker()
{
// Create color picker as a child widget (not a dialog)
colorPicker = new QColorDialog(this);
colorPicker->setWindowFlags(Qt::Widget); // Embed as widget, not dialog
colorPicker->setOptions(QColorDialog::DontUseNativeDialog | QColorDialog::NoButtons);
colorPicker->setCurrentColor(brushColor);
// Disable translucent background for color picker - use normal Qt style
colorPicker->setAttribute(Qt::WA_TranslucentBackground, false);
colorPicker->setAutoFillBackground(true);
colorPicker->hide();
// Connect color selection
connect(colorPicker, &QColorDialog::currentColorChanged, this, &MainWindow::onColorSelected);
qDebug() << "Color picker widget created";
}
void MainWindow::applyBlurEffect()
{
#ifdef HAVE_KWINDOWSYSTEM
// Apply blur effect to toolbar region using KWindowSystem
if (KWindowSystem::isPlatformWayland() || KWindowSystem::isPlatformX11()) {
QRegion blurRegion(toolbar->geometry());
KWindowEffects::enableBlurBehind(windowHandle(), true, blurRegion);
qDebug() << "Blur effect applied to toolbar region:" << toolbar->geometry();
}
#else
qDebug() << "KWindowSystem not available - blur effect disabled";
#endif
}
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
if (obj == toolbar) {
if (event->type() == QEvent::MouseButtonPress) {
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
if (mouseEvent->button() == Qt::LeftButton) {
// Only allow dragging from titlebar
if (titleBar->geometry().contains(mouseEvent->pos())) {
isMovingToolbar = true;
toolbarDragStartPos = mouseEvent->pos();
titleBar->setCursor(Qt::ClosedHandCursor);
return true;
}
}
} else if (event->type() == QEvent::MouseMove) {
if (isMovingToolbar) {
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
QPoint delta = mouseEvent->pos() - toolbarDragStartPos;
QString platform = WindowHelper::detectPlatform();
if (platform == "wayland" && ! currentInputRegion.isEmpty()) {
// On Wayland, unmask during move to allow full canvas rendering
clearMask();
toolbar->move(toolbar->pos() + delta);
// Update color picker position if visible
if (colorPicker && colorPicker->isVisible()) {
updateColorPickerPosition();
}
applyBlurEffect();
// Force synchronous repaint while unmasked
repaint();
// Update input region (which will restore the mask)
updateInputRegion();
} else {
// X11 path - just move normally
toolbar->move(toolbar->pos() + delta);
// Update color picker position if visible
if (colorPicker && colorPicker->isVisible()) {
updateColorPickerPosition();
}
applyBlurEffect();
updateInputRegion();
}
return true;
}
} else if (event->type() == QEvent::MouseButtonRelease) {
if (isMovingToolbar) {
isMovingToolbar = false;
titleBar->setCursor(Qt::OpenHandCursor);
// Final updates after move
applyBlurEffect();
updateInputRegion();
return true;
}
}
}
return QWidget::eventFilter(obj, event);
}
void MainWindow::resizeEvent(QResizeEvent *event)
{
QWidget::resizeEvent(event);
applyBlurEffect();
}
void MainWindow::setMode(DrawMode mode)
{
currentMode = mode;
// Update button states
btnClick->setChecked(mode == MODE_CLICK);
btnBrush->setChecked(mode == MODE_BRUSH);
btnEraser->setChecked(mode == MODE_ERASER);
// Update slider to show current tool's size
updateSizeSlider();
// Update input region
updateInputRegion();
qDebug() << "Mode changed to:" << mode;
}
void MainWindow::updateSizeSlider()
{
brushSizeSlider->blockSignals(true);
if (currentMode == MODE_BRUSH) {
// Brush: range 1-10, default 5
brushSizeSlider->setMinimum(1);
brushSizeSlider->setMaximum(10);
brushSizeSlider->setValue(brushSize);
brushSizeLabel->setText(QString("Brush Size: %1").arg(brushSize));
} else if (currentMode == MODE_ERASER) {
// Eraser: range 10-100, default 50
brushSizeSlider->setMinimum(10);
brushSizeSlider->setMaximum(100);
brushSizeSlider->setValue(eraserSize);
brushSizeLabel->setText(QString("Eraser Size: %1").arg(eraserSize));
} else {
// Click mode: disable slider
brushSizeLabel->setText("Size:");
}
brushSizeSlider->blockSignals(false);
}
void MainWindow::updateInputRegion()
{
if (currentMode == MODE_CLICK) {
QRegion toolbarRegion(toolbar->geometry());
// If color picker is visible, add it to input region
if (colorPicker && colorPicker->isVisible()) {
toolbarRegion = toolbarRegion.united(QRegion(colorPicker->geometry()));
}
currentInputRegion = toolbarRegion;
WindowHelper::setInputRegion(this, toolbarRegion);
qDebug() << "Input region: toolbar" << (colorPicker->isVisible() ? "+ color picker" : "only");
} else {
QRegion fullRegion(rect());
currentInputRegion = fullRegion;
WindowHelper::setInputRegion(this, fullRegion);
qDebug() << "Input region: full window";
}
}
void MainWindow::onBrushClicked()
{
setMode(MODE_BRUSH);
}
void MainWindow::onBrushSizeChanged(int value)
{
if (currentMode == MODE_BRUSH) {
brushSize = value;
brushSizeLabel->setText(QString("Brush Size: %1").arg(value));
} else if (currentMode == MODE_ERASER) {
eraserSize = value;
brushSizeLabel->setText(QString("Eraser Size: %1").arg(value));
}
}
void MainWindow::onClearClicked()
{
// Unmask, clear, then remask
temporarilyUnmaskForOperation([&]() {
canvas.fill(Qt::transparent);
});
qDebug() << "Canvas cleared";
}
void MainWindow::onClickClicked()
{
setMode(MODE_CLICK);
}
void MainWindow::onColorPickerClicked()
{
if (colorPicker->isVisible()) {
// Hide color picker
colorPicker->hide();
btnColorPicker->setText("🎨 Color");
// Update input region
updateInputRegion();
qDebug() << "Color picker hidden";
} else {
// Update position before showing
updateColorPickerPosition();
// Show color picker
colorPicker->setCurrentColor(brushColor);
colorPicker->show();
colorPicker->raise(); // Bring to front
btnColorPicker->setText("✓ Done");
// Update input region to include color picker
updateInputRegion();
qDebug() << "Color picker shown";
}
}
void MainWindow::onColorSelected(const QColor &color)
{
if (color.isValid()) {
brushColor = color;
qDebug() << "Brush color changed to:" << color.name();
}
}
void MainWindow::onEraserClicked()
{
setMode(MODE_ERASER);
}
void MainWindow::onQuitClicked()
{
QApplication::quit();
}
void MainWindow::updateColorPickerPosition()
{
if (! colorPicker) {
return;
}
// Get color picker size (may need to show/hide to get accurate size)
QSize pickerSize = colorPicker->sizeHint();
if (pickerSize.isEmpty()) {
pickerSize = QSize(400, 300); // Default fallback size
}
QRect screenRect = rect();
QRect toolbarRect = toolbar->geometry();
int margin = 10;
QPoint newPos;
// Try right side first
if (toolbarRect.right() + margin + pickerSize.width() <= screenRect.right()) {
newPos = QPoint(toolbarRect.right() + margin, toolbarRect.top());
qDebug() << "Color picker positioned on right";
}
// Try left side
else if (toolbarRect.left() - margin - pickerSize.width() >= screenRect.left()) {
newPos = QPoint(toolbarRect.left() - margin - pickerSize.width(), toolbarRect.top());
qDebug() << "Color picker positioned on left";
}
// Try below
else if (toolbarRect.bottom() + margin + pickerSize.height() <= screenRect.bottom()) {
newPos = QPoint(toolbarRect.left(), toolbarRect.bottom() + margin);
qDebug() << "Color picker positioned below";
}
// Try above
else if (toolbarRect.top() - margin - pickerSize.height() >= screenRect.top()) {
newPos = QPoint(toolbarRect.left(), toolbarRect.top() - margin - pickerSize.height());
qDebug() << "Color picker positioned above";
}
// Fallback: center of screen
else {
newPos = QPoint(
(screenRect.width() - pickerSize.width()) / 2,
(screenRect.height() - pickerSize.height()) / 2
);
qDebug() << "Color picker positioned at center (fallback)";
}
colorPicker->move(newPos);
}
void MainWindow::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
// Create a clipping region that excludes the toolbar
// This ensures canvas drawings don't appear under the toolbar,
// allowing the background blur to show through properly
QRegion clipRegion(rect());
clipRegion = clipRegion.subtracted(QRegion(toolbar->geometry()));
painter.setClipRegion(clipRegion);
// Draw the canvas - Qt will automatically handle the DPR scaling
// Canvas will not be painted in the toolbar area
painter.drawPixmap(0, 0, canvas);
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
if (currentMode == MODE_CLICK) {
return;
}
if (toolbar->geometry().contains(event->pos())) {
return;
}
isDrawing = true;
lastPoint = event->position();
qDebug() << "Mouse press at:" << lastPoint << "(sub-pixel precision)";
// Draw a dot at the press position
// This handles the case where user clicks without moving (single dot)
QPainter painter(&canvas);
painter.setRenderHint(QPainter::Antialiasing);
if (currentMode == MODE_BRUSH) {
painter.setPen(QPen(brushColor, brushSize, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
painter.drawPoint(lastPoint);
} else if (currentMode == MODE_ERASER) {
painter.setCompositionMode(QPainter::CompositionMode_Clear);
painter.setPen(QPen(Qt::transparent, eraserSize, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
painter.drawPoint(lastPoint);
}
repaint();
}
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
if (!isDrawing || currentMode == MODE_CLICK) {
return;
}
if (toolbar->geometry().contains(event->pos())) {
return;
}
// Get current position with sub-pixel precision
QPointF currentPoint = event->position();
// Debug output to verify sub-pixel precision
qDebug() << "Mouse move:" << currentPoint
<< "| Delta:" << (currentPoint - lastPoint)
<< "| Distance:" << QLineF(lastPoint, currentPoint).length();
QPainter painter(&canvas);
painter.setRenderHint(QPainter::Antialiasing);
// Draw using floating-point coordinates
// Qt will handle the DPR scaling automatically
if (currentMode == MODE_BRUSH) {
painter.setPen(QPen(brushColor, brushSize, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
painter.drawLine(lastPoint, currentPoint);
} else if (currentMode == MODE_ERASER) {
painter.setCompositionMode(QPainter::CompositionMode_Clear);
painter.setPen(QPen(Qt::transparent, eraserSize, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
painter.drawLine(lastPoint, currentPoint);
}
lastPoint = currentPoint;
repaint();
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
if (isDrawing) {
isDrawing = false;
qDebug() << "Mouse release at:" << event->position();
}
}