Repository navigation
Expand file tree
/
Copy pathftwindow_paint.cpp
More file actions
6050 lines (5496 loc) · 295 KB
/
Copy pathftwindow_paint.cpp
File metadata and controls
6050 lines (5496 loc) · 295 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
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "ftwindow_common.h"
// ---------------------------------------------------------------------------
// Painting
// ---------------------------------------------------------------------------
void FtWindow::drawParamLabel(QPainter &p, const QFontMetrics &fm,
int x, int y, const QString &text, const QString &tip,
int fieldH)
{
// Align label baseline with the text baseline inside the adjacent input
// field of height `fieldH` (line edit / combo box, typically 22 px).
// Input fields vertically centre their text, so their baseline sits at
// fieldH/2 + ascent/2 - descent/2 from the widget top — not at
// fm.ascent() as with a top-anchored drawText.
int baselineY = y + (fieldH + fm.ascent() - fm.descent()) / 2;
p.drawText(x, baselineY, text);
if (!tip.isEmpty()) {
QRect r(x, baselineY - fm.ascent(), fm.horizontalAdvance(text), fm.height());
m_paramLabelTips.emplace_back(r, tip);
}
}
// The radius governs the inverse as much as the forward transform — running the
// circle transform backwards means drawing circles of that radius back in — so
// the slider stays put whichever direction is selected.
bool FtWindow::houghShowsRadiusSlider() const
{
return m_houghActive && m_houghElementCombo
&& m_houghElementCombo->currentData().toInt() == HoughCirclesOne;
}
bool FtWindow::p1ToolWindowOpen() const
{
return m_p1EraserActive || m_p1BrushActive || m_p1TaperActive || m_p1SymmetrizeActive
|| m_threshActive
|| m_binActive || m_padActive || m_copyActive || m_averageActive || m_cropActive || m_peakPickActive || m_extractActive
|| m_tileAvgActive
|| m_gaborActive || m_hessianActive || m_houghActive || m_amyloidActive || m_measureActive
|| m_shiftActive || m_rotateActive || m_shearActive || m_alignActive
|| m_p1FlipHActive || m_p1FlipVActive || m_p1InvertActive;
}
bool FtWindow::p2ToolWindowOpen() const
{
return m_bandpassActive || m_directionalActive || m_lineFilterActive || m_brushActive
|| m_eraserActive || m_latticeActive || m_ftCropActive || m_crossSectionActive
|| m_ctfActive || m_ctfFitActive || m_phaseRampActive || m_p2SymmetrizeActive
|| m_ftRotateActive;
}
bool FtWindow::p1FunctionOpen() const { return p1ToolWindowOpen() || m_mathActive; }
bool FtWindow::p2FunctionOpen() const { return p2ToolWindowOpen() || m_ftMathActive; }
bool FtWindow::toolHelpInfo(bool panel2, QString &title, QString &anchor) const
{
struct Entry { bool active; const char *title; const char *anchor; };
// Listed in the same order as the drawing chains in paintEvent, so the
// header always names the tool whose parameters are on screen.
const Entry p1[] = {
{ m_p1EraserActive, "Eraser", "p1-eraser" },
{ m_p1BrushActive, "Paint brush", "p1-paint-brush" },
{ m_p1TaperActive, "Taper edges (Hanning)", "p1-taper-edges" },
{ m_threshActive, "Threshold histogram", "p1-threshold" },
{ m_p1SymmetrizeActive, "Symmetrize image", "p1-symmetrize" },
{ m_binActive, "Bin image", "p1-bin" },
{ m_padActive, "Pad image", "p1-pad" },
{ m_copyActive, "Copy image buffer", "p1-copy" },
{ m_averageActive, "Average images", "p1-average" },
{ m_cropActive, "Crop image", "p1-crop" },
{ m_peakPickActive, "Peak search", "p1-peak-search" },
{ m_extractActive, "Extract particles", "p1-extract-particles" },
{ m_tileAvgActive, "Average image tiles", "p1-tile-average" },
{ m_gaborActive, "Gabor filter", "p1-gabor" },
{ m_hessianActive, "Hessian filter", "p1-hessian" },
{ m_houghActive, "Hough transform", "p1-hough" },
{ m_measureActive, "Measure", "p1-measure" },
{ m_shiftActive, "Shift image", "p1-shift" },
{ m_rotateActive, "Rotate image", "p1-rotate" },
{ m_shearActive, "Shear image", "p1-shear" },
{ m_amyloidActive, "Amyloid filament", "p1-amyloid" },
{ m_alignActive, "Align to reference", "p1-align" },
{ m_p1FlipHActive, "Flip horizontally", "p1-flip" },
{ m_p1FlipVActive, "Flip vertically", "p1-flip" },
{ m_p1InvertActive, "Invert contrast", "p1-invert-contrast" },
};
const Entry p2[] = {
{ m_bandpassActive, "Bandpass filter", "p2-bandpass" },
{ m_directionalActive, "Directional filter", "p2-directional" },
{ m_brushActive, "Paint brush", "p2-paint-brush" },
{ m_eraserActive, "Eraser", "p2-eraser" },
{ m_lineFilterActive, "Line filter", "p2-line-filter" },
{ m_latticeActive, "Lattice filter", "p2-lattice-filter" },
{ m_crossSectionActive, "Cross-section profile", "p2-cross-section" },
{ m_p2SymmetrizeActive, "Symmetrize Fourier space", "p2-symmetrize" },
{ m_ftCropActive, "Fourier crop / Fourier pad","p2-fourier-crop-pad" },
{ m_ctfActive, "CTF SIM (simulate a CTF)", "p2-ctf-sim" },
{ m_ctfFitActive, "CTF FIT (fit a CTF for an image)", "p2-ctf-fit" },
{ m_phaseRampActive, "Phase ramp", "p2-phase-ramp" },
{ m_ftRotateActive, "Rotate Fourier space", "p2-rotate" },
};
const Entry *entries = panel2 ? p2 : p1;
const size_t n = panel2 ? std::size(p2) : std::size(p1);
for (size_t i = 0; i < n; i++) {
if (entries[i].active) {
title = QString::fromLatin1(entries[i].title);
anchor = QString::fromLatin1(entries[i].anchor);
return true;
}
}
return false;
}
// One raised 3D square button carrying a single character, matching the toggle
// buttons next to the histograms. Shared by the "?" help button and the "E"
// exercise button so the two are guaranteed identical in size and styling.
void FtWindow::drawSquareLabelButton(QPainter &p, const QRect &br,
const QString &label, const QString &tip)
{
p.save();
p.fillRect(br, QColor(0xdc, 0xdc, 0xdc));
const QColor lite(0xff, 0xff, 0xff), dark(0x60, 0x60, 0x60);
p.fillRect(QRect(br.left(), br.top(), br.width(), 2), lite);
p.fillRect(QRect(br.left(), br.top(), 2, br.height()), lite);
p.fillRect(QRect(br.right() - 1, br.top(), 2, br.height()), dark);
p.fillRect(QRect(br.left(), br.bottom() - 1, br.width(), 2), dark);
QFont qf; qf.setPixelSize(std::clamp(br.width() * 3 / 4, 9, 18)); qf.setBold(true);
p.setFont(qf);
p.setPen(Qt::black);
p.drawText(br, Qt::AlignCenter, label);
p.restore();
m_paramLabelTips.emplace_back(br, tip);
}
QRect FtWindow::drawHelpButton(QPainter &p, const QRect &frame, int marginX,
int marginY, int rowH, int maxSize)
{
int bs = std::clamp(rowH - 6, 14, maxSize);
QRect br(frame.right() - marginX - bs,
frame.top() + marginY + (rowH - bs) / 2, bs, bs);
drawSquareLabelButton(p, br, QStringLiteral("?"), QStringLiteral("Help"));
return br;
}
// The "E" button sits immediately left of the help button, same size and shape,
// and is drawn only for tools that an exercise actually covers. Returns a null
// rect otherwise, which is also what suppresses its click target.
QRect FtWindow::drawExerciseButton(QPainter &p, const QRect &helpRect,
const QString &anchor)
{
if (helpRect.isNull() || anchor.isEmpty()) return QRect();
QRect br = helpRect.translated(-(helpRect.width() + 4), 0);
drawSquareLabelButton(p, br, QStringLiteral("E"),
QStringLiteral("Show the exercise for this function"));
return br;
}
// The exercise in manual_exercises.html that teaches the tool currently open in
// this panel, or an empty string when no exercise covers it. Only these tools
// get an "E" button.
QString FtWindow::toolExerciseAnchor(bool panel2) const
{
if (panel2) {
if (m_brushActive || m_eraserActive) return "exercise-0";
if (m_bandpassActive || m_directionalActive
|| m_lineFilterActive) return "exercise-1";
if (m_latticeActive) return "exercise-4";
if (m_phaseRampActive) return "exercise-7";
if (m_ftMathActive) return "exercise-8";
if (m_ctfFitActive) return "exercise-12";
if (m_ctfActive) return "exercise-13";
return QString();
}
if (m_p1BrushActive || m_p1EraserActive) return "exercise-0";
if (m_mathActive) return "exercise-6";
if (m_peakPickActive || m_extractActive) return "exercise-9";
if (m_amyloidActive) return "exercise-10";
if (m_alignActive) return "exercise-11";
if (m_houghActive) return "exercise-14";
return QString();
}
QRect FtWindow::drawToolHelpHeader(QPainter &p, const QRect &toolRect, int margin,
int lh, int fontPx, const QString &title)
{
QFont tf; tf.setPixelSize(fontPx); tf.setBold(true);
QFontMetrics tfm(tf);
p.save();
p.setFont(tf);
p.setPen(QColor(20, 20, 20));
int baselineY = toolRect.top() + margin + (lh + tfm.ascent() - tfm.descent()) / 2;
p.drawText(toolRect.left() + margin, baselineY, title);
p.restore();
return drawHelpButton(p, toolRect, margin, margin, lh);
}
void FtWindow::paintEvent(QPaintEvent *)
{
QPainter p(this);
p.setRenderHint(QPainter::Antialiasing, false);
// The maximized view replaces the whole layout, so it paints on its own
// and none of the normal panel bookkeeping below applies.
m_p1MaxRect = QRect();
m_p2MaxRectA = QRect();
m_p2MaxRectB = QRect();
if (m_maxPanel != 0) {
paintMaximized(p);
return;
}
// Reset painted parameter-label hover rectangles; they are repopulated
// below as the tool option panels are drawn.
m_paramLabelTips.clear();
m_p1ToolRect = QRect(); // cleared each frame, set when a p1 tool dialog draws
m_p2ToolRect = QRect(); // cleared each frame, set when a p2 tool dialog draws
m_p1HelpRect = QRect();
m_p2HelpRect = QRect();
m_p1MathHelpRect = QRect();
m_p2MathHelpRect = QRect();
m_p1ExerciseRect = QRect();
m_p2ExerciseRect = QRect();
m_imageHistLockRect = QRect();
m_markImageCenterRect = QRect();
m_pixelSizeInfoRect = QRect();
m_ftHistLockRect = QRect();
m_maskBtnRect = QRect();
// Function-button tooltips are stashed here during tool-button rendering
// and drawn at the very end of this paintEvent, so they appear on top of
// the image / Fourier display rather than being clipped by them.
QRect pendingTipRect;
QString pendingTipText;
// Label size for the custom-painted toggles beside the histograms. They
// shrink with the window like the real buttons do (chromeScale()), and both
// the rects computed further down and the painter below must use the same
// number or the text would no longer fit the box it is drawn in.
const int toggleFontPx = std::max(9, (int)std::lround(11 * chromeScale()));
// Helper to paint a 3D-look toggle button (raised when up, sunken when
// down). Shared by all panel-1 / panel-2 toggle buttons.
auto paintToggleButton = [&](const QRect &br, const QString &text, bool down) {
QFont bf; bf.setPixelSize(toggleFontPx);
p.save();
p.fillRect(br, down ? QColor(0xb8, 0xb8, 0xb8) : QColor(0xdc, 0xdc, 0xdc));
QColor lite(0xff, 0xff, 0xff), dark(0x60, 0x60, 0x60);
QColor topC = down ? dark : lite;
QColor leftC = down ? dark : lite;
QColor rightC = down ? lite : dark;
QColor botC = down ? lite : dark;
p.fillRect(QRect(br.left(), br.top(), br.width(), 2), topC);
p.fillRect(QRect(br.left(), br.top(), 2, br.height()), leftC);
p.fillRect(QRect(br.right() - 1, br.top(), 2, br.height()), rightC);
p.fillRect(QRect(br.left(), br.bottom() - 1, br.width(), 2), botC);
p.setFont(bf);
p.setPen(Qt::black);
QRect textRect = br.adjusted(2, 2, -2, -2);
if (down) textRect.translate(1, 1);
p.drawText(textRect, Qt::AlignCenter, text);
p.restore();
};
int cx = width() / 2;
int hy = height() - height() / 5;
// scaled label font sizes
int labelFontMain = std::clamp(std::min(cx, hy) / 20, 12, 48);
int labelFontHist = std::clamp(std::min(cx / 5, (height() - hy)) / 6, 8, 24);
// background
p.fillRect(rect(), QColor(75, 75, 75));
m_numDispItems = 0; // reset display item tracking
// helper: look up the raw pixel value under the mouse for a given display area
auto sampleValue = [&](const QRect &inner, const ZoomState &zoom,
int imgW, int imgH,
const std::vector<double> &vals,
double &outVal,
int *outX = nullptr, int *outY = nullptr) -> bool {
if (!inner.contains(m_mousePos) || vals.empty()) return false;
QRectF src = zoom.visibleRect(imgW, imgH);
double relX = (m_mousePos.x() - inner.x()) / (double)inner.width();
double relY = (m_mousePos.y() - inner.y()) / (double)inner.height();
int ix = (int)(src.x() + relX * src.width());
int iy = (int)(src.y() + relY * src.height());
if (ix < 0 || ix >= imgW || iy < 0 || iy >= imgH) return false;
outVal = vals[iy * imgW + ix];
if (outX) *outX = ix;
if (outY) *outY = iy;
return true;
};
// ---- panel titles ----------------------------------------------------------
{
QFont tf;
tf.setBold(true);
tf.setPixelSize(28);
p.setFont(tf);
p.setPen(Qt::white);
QFontMetrics tfm(tf);
QString t1 = "Real Space";
p.drawText((cx - 1 - tfm.horizontalAdvance(t1)) / 2, 8 + tfm.ascent(), t1);
QString t2 = "Fourier Space";
p.drawText(cx + 2 + (width() - cx - 2 - tfm.horizontalAdvance(t2)) / 2, 8 + tfm.ascent(), t2);
}
// ---- Tool button columns ----------------------------------
{
// Group/slot geometry is computed in layoutToolSlots() (shared with the
// mouse handler). Each visible tool id draws into the slot it currently
// occupies: a group face when collapsed, or a popup cell when its group
// is open. Invisible ids (non-face members of a collapsed group) skip.
layoutToolSlots();
int btnSide = m_toolBtnSide;
// Floating popup panel background, under the member cells drawn below.
// layoutToolSlots() sets the rect for whichever group is showing its
// members — clicked open or previewed on hover — and clears it otherwise,
// so its emptiness is the test.
for (const QRect &popup : { m_p1PopupRect, m_p2PopupRect }) {
if (popup.isNull()) continue;
p.setPen(QPen(QColor(200, 200, 200), 1));
p.setBrush(QColor(30, 30, 30));
p.drawRect(popup);
}
// Panel 1: left edge
for (int i = 0; i < P1_TOOL_BUTTONS; i++) {
if (!m_p1SlotVisible[i]) continue;
QRect r = m_p1BtnRects[i];
p.setPen(QPen(Qt::white, 1));
if ((i == 0 && m_p1EraserActive) || (i == 1 && m_p1BrushActive) ||
(i == 2 && m_measureActive) ||
(i == 5 && m_shiftActive) || (i == 6 && m_rotateActive) ||
(i == 22 && m_shearActive) ||
(i == 8 && m_p1TaperActive) || (i == 9 && m_p1SymmetrizeActive) ||
(i == 25 && m_threshActive) ||
(i == 10 && m_binActive) || (i == 11 && m_cropActive) ||
(i == 19 && m_padActive) || (i == 20 && m_copyActive) ||
(i == 12 && m_gaborActive) || (i == 13 && m_hessianActive) ||
(i == 23 && m_houghActive) ||
(i == 14 && m_amyloidActive) || (i == 15 && m_mathActive) ||
(i == 16 && m_peakPickActive) || (i == 17 && m_extractActive) ||
(i == 24 && m_tileAvgActive) ||
(i == 18 && m_alignActive) || (i == 21 && m_averageActive))
p.setBrush(QColor(60, 60, 60));
else
p.setBrush(QColor(0, 0, 0));
p.drawRect(r);
// Eraser icon (button 0): pencil eraser tilted at an angle
if (i == 0) {
p.setRenderHint(QPainter::Antialiasing, true);
QRect ir = r.adjusted(3, 3, -3, -3);
double ix = ir.x(), iy2 = ir.y(), iw = ir.width(), ih = ir.height();
// Body (wooden pencil shaft) — tilted parallelogram
QPainterPath body;
body.moveTo(ix + iw * 0.30, iy2 + ih * 0.08);
body.lineTo(ix + iw * 0.85, iy2 + ih * 0.08);
body.lineTo(ix + iw * 0.70, iy2 + ih * 0.50);
body.lineTo(ix + iw * 0.15, iy2 + ih * 0.50);
body.closeSubpath();
p.setPen(QPen(QColor(160, 130, 90), std::max(1, (int)(iw * 0.04))));
p.setBrush(QColor(210, 185, 140));
p.drawPath(body);
// Highlight stripe on body
QPainterPath stripe;
stripe.moveTo(ix + iw * 0.33, iy2 + ih * 0.18);
stripe.lineTo(ix + iw * 0.82, iy2 + ih * 0.18);
stripe.lineTo(ix + iw * 0.78, iy2 + ih * 0.30);
stripe.lineTo(ix + iw * 0.29, iy2 + ih * 0.30);
stripe.closeSubpath();
p.setPen(Qt::NoPen);
p.setBrush(QColor(225, 200, 160));
p.drawPath(stripe);
// Metal ferrule band
QPainterPath ferrule;
ferrule.moveTo(ix + iw * 0.15, iy2 + ih * 0.50);
ferrule.lineTo(ix + iw * 0.70, iy2 + ih * 0.50);
ferrule.lineTo(ix + iw * 0.65, iy2 + ih * 0.62);
ferrule.lineTo(ix + iw * 0.10, iy2 + ih * 0.62);
ferrule.closeSubpath();
p.setPen(QPen(QColor(140, 140, 140), std::max(1, (int)(iw * 0.03))));
p.setBrush(QColor(190, 195, 200));
p.drawPath(ferrule);
// Pink eraser tip
QPainterPath eraser;
eraser.moveTo(ix + iw * 0.10, iy2 + ih * 0.62);
eraser.lineTo(ix + iw * 0.65, iy2 + ih * 0.62);
eraser.lineTo(ix + iw * 0.55, iy2 + ih * 0.92);
eraser.lineTo(ix + iw * 0.00, iy2 + ih * 0.92);
eraser.closeSubpath();
p.setPen(QPen(QColor(180, 70, 80), std::max(1, (int)(iw * 0.04))));
p.setBrush(QColor(235, 120, 130));
p.drawPath(eraser);
// Eraser highlight
QPainterPath eHighlight;
eHighlight.moveTo(ix + iw * 0.12, iy2 + ih * 0.66);
eHighlight.lineTo(ix + iw * 0.50, iy2 + ih * 0.66);
eHighlight.lineTo(ix + iw * 0.47, iy2 + ih * 0.76);
eHighlight.lineTo(ix + iw * 0.09, iy2 + ih * 0.76);
eHighlight.closeSubpath();
p.setPen(Qt::NoPen);
p.setBrush(QColor(245, 160, 165));
p.drawPath(eHighlight);
p.setRenderHint(QPainter::Antialiasing, false);
if (r.contains(m_mousePos)) {
QFont ttf; ttf.setPixelSize(11); p.setFont(ttf);
QFontMetrics ttfm(ttf);
QString tip = "Eraser";
int ttw = ttfm.horizontalAdvance(tip) + 8;
int tth = ttfm.height() + 4;
int ttx = r.right() + 4;
int tty = r.center().y() - tth / 2;
pendingTipRect = QRect(ttx, tty, ttw, tth);
pendingTipText = tip;
}
}
// Paint brush icon (button 1): reuse panel 2 brush icon
if (i == 1) {
p.setRenderHint(QPainter::Antialiasing, true);
QRect ir = r.adjusted(3, 3, -3, -3);
int bx2 = ir.x(), by3 = ir.y(), bw = ir.width(), bh = ir.height();
p.setPen(QPen(QColor(160, 120, 60), std::max(1, bw / 6)));
p.drawLine(bx2 + bw * 0.5, by3 + bh * 0.05, bx2 + bw * 0.5, by3 + bh * 0.45);
p.setPen(Qt::NoPen);
p.setBrush(QColor(180, 180, 180));
p.drawRect(bx2 + bw * 0.25, by3 + bh * 0.40, bw * 0.5, bh * 0.15);
p.setBrush(QColor(200, 160, 80));
QPainterPath br;
br.moveTo(bx2 + bw * 0.2, by3 + bh * 0.55);
br.lineTo(bx2 + bw * 0.8, by3 + bh * 0.55);
br.lineTo(bx2 + bw * 0.65, by3 + bh * 0.95);
br.lineTo(bx2 + bw * 0.35, by3 + bh * 0.95);
br.closeSubpath();
p.drawPath(br);
p.setRenderHint(QPainter::Antialiasing, false);
if (r.contains(m_mousePos)) {
QFont ttf; ttf.setPixelSize(11); p.setFont(ttf);
QFontMetrics ttfm(ttf);
QString tip = "Paint brush";
int ttw = ttfm.horizontalAdvance(tip) + 8;
int tth = ttfm.height() + 4;
int ttx = r.right() + 4;
int tty = r.center().y() - tth / 2;
pendingTipRect = QRect(ttx, tty, ttw, tth);
pendingTipText = tip;
}
}
// Measure tool icon (button 2): simple mm ruler
if (i == 2) {
p.setRenderHint(QPainter::Antialiasing, true);
QRect ir = r.adjusted(3, 3, -3, -3);
double ix = ir.x(), iy = ir.y(), iw = ir.width(), ih = ir.height();
// Ruler body (light tan rectangle)
QRectF body(ix + iw * 0.05, iy + ih * 0.30,
iw * 0.90, ih * 0.35);
p.setPen(QPen(QColor(120, 90, 0), std::max(1, (int)(iw * 0.04))));
p.setBrush(QColor(245, 220, 140));
p.drawRect(body);
// Tick marks along the top edge
p.setPen(QPen(QColor(40, 40, 40), std::max(1, (int)(iw * 0.03))));
int nTicks = 11;
for (int k = 0; k < nTicks; k++) {
double tx = body.left() + k * body.width() / (nTicks - 1);
double tickLen = (k % 5 == 0) ? body.height() * 0.55
: body.height() * 0.30;
p.drawLine(QPointF(tx, body.top()),
QPointF(tx, body.top() + tickLen));
}
p.setRenderHint(QPainter::Antialiasing, false);
if (r.contains(m_mousePos)) {
QFont ttf; ttf.setPixelSize(11); p.setFont(ttf);
QFontMetrics ttfm(ttf);
QString tip = "Measure";
int ttw = ttfm.horizontalAdvance(tip) + 8;
int tth = ttfm.height() + 4;
int ttx = r.right() + 4;
int tty = r.center().y() - tth / 2;
pendingTipRect = QRect(ttx, tty, ttw, tth);
pendingTipText = tip;
}
}
// Flip horizontal icon (button 3): double arrow left-right
if (i == 3) {
p.setRenderHint(QPainter::Antialiasing, true);
double cx2 = r.x() + r.width() / 2.0;
double cy2 = r.y() + r.height() / 2.0;
double hw = r.width() * 0.35;
double hh = r.height() * 0.2;
p.setPen(Qt::NoPen);
p.setBrush(Qt::white);
QPainterPath la;
la.moveTo(cx2 - hw, cy2);
la.lineTo(cx2 - hw * 0.3, cy2 - hh);
la.lineTo(cx2 - hw * 0.3, cy2 + hh);
la.closeSubpath();
p.drawPath(la);
QPainterPath ra;
ra.moveTo(cx2 + hw, cy2);
ra.lineTo(cx2 + hw * 0.3, cy2 - hh);
ra.lineTo(cx2 + hw * 0.3, cy2 + hh);
ra.closeSubpath();
p.drawPath(ra);
p.setPen(QPen(Qt::white, std::max(1, btnSide / 12)));
p.drawLine(cx2 - hw * 0.3, cy2, cx2 + hw * 0.3, cy2);
p.setRenderHint(QPainter::Antialiasing, false);
if (r.contains(m_mousePos)) {
QFont ttf; ttf.setPixelSize(11); p.setFont(ttf);
QFontMetrics ttfm(ttf);
QString tip = "Flip horizontally";
int ttw = ttfm.horizontalAdvance(tip) + 8;
int tth = ttfm.height() + 4;
int ttx = r.right() + 4;
int tty = r.center().y() - tth / 2;
pendingTipRect = QRect(ttx, tty, ttw, tth);
pendingTipText = tip;
}
}
// Flip vertical icon (button 3): double arrow up-down
if (i == 4) {
p.setRenderHint(QPainter::Antialiasing, true);
double cx2 = r.x() + r.width() / 2.0;
double cy2 = r.y() + r.height() / 2.0;
double hw = r.width() * 0.2;
double hh = r.height() * 0.35;
p.setPen(Qt::NoPen);
p.setBrush(Qt::white);
QPainterPath ua;
ua.moveTo(cx2, cy2 - hh);
ua.lineTo(cx2 - hw, cy2 - hh * 0.3);
ua.lineTo(cx2 + hw, cy2 - hh * 0.3);
ua.closeSubpath();
p.drawPath(ua);
QPainterPath da;
da.moveTo(cx2, cy2 + hh);
da.lineTo(cx2 - hw, cy2 + hh * 0.3);
da.lineTo(cx2 + hw, cy2 + hh * 0.3);
da.closeSubpath();
p.drawPath(da);
p.setPen(QPen(Qt::white, std::max(1, btnSide / 12)));
p.drawLine(cx2, cy2 - hh * 0.3, cx2, cy2 + hh * 0.3);
p.setRenderHint(QPainter::Antialiasing, false);
if (r.contains(m_mousePos)) {
QFont ttf; ttf.setPixelSize(11); p.setFont(ttf);
QFontMetrics ttfm(ttf);
QString tip = "Flip vertically";
int ttw = ttfm.horizontalAdvance(tip) + 8;
int tth = ttfm.height() + 4;
int ttx = r.right() + 4;
int tty = r.center().y() - tth / 2;
pendingTipRect = QRect(ttx, tty, ttw, tth);
pendingTipText = tip;
}
}
// Shift image icon (button 5): the classic four-way move glyph — a
// plus of shafts with an arrowhead on each end, reading clearly as
// "drag to shift in any direction".
if (i == 5) {
p.setRenderHint(QPainter::Antialiasing, true);
const double cx2 = r.x() + r.width() / 2.0;
const double cy2 = r.y() + r.height() / 2.0;
const QColor col = m_shiftActive ? QColor(180, 180, 255) : Qt::white;
const double reach = r.width() * 0.40; // arrow tip from centre
const double head = r.width() * 0.18; // arrowhead length
const double half = r.height() * 0.15; // arrowhead half-width
const double shaftW = std::max(1.5, r.width() / 9.0);
// Shafts: a plus reaching out to where each head begins.
p.setPen(QPen(col, shaftW, Qt::SolidLine, Qt::RoundCap));
p.drawLine(QPointF(cx2 - (reach - head), cy2),
QPointF(cx2 + (reach - head), cy2));
p.drawLine(QPointF(cx2, cy2 - (reach - head)),
QPointF(cx2, cy2 + (reach - head)));
// Four arrowheads at the tips.
p.setPen(Qt::NoPen);
p.setBrush(col);
QPainterPath ar; // right
ar.moveTo(cx2 + reach, cy2);
ar.lineTo(cx2 + reach - head, cy2 - half);
ar.lineTo(cx2 + reach - head, cy2 + half);
ar.closeSubpath();
QPainterPath al; // left
al.moveTo(cx2 - reach, cy2);
al.lineTo(cx2 - reach + head, cy2 - half);
al.lineTo(cx2 - reach + head, cy2 + half);
al.closeSubpath();
QPainterPath au; // up
au.moveTo(cx2, cy2 - reach);
au.lineTo(cx2 - half, cy2 - reach + head);
au.lineTo(cx2 + half, cy2 - reach + head);
au.closeSubpath();
QPainterPath ad; // down
ad.moveTo(cx2, cy2 + reach);
ad.lineTo(cx2 - half, cy2 + reach - head);
ad.lineTo(cx2 + half, cy2 + reach - head);
ad.closeSubpath();
p.drawPath(ar); p.drawPath(al); p.drawPath(au); p.drawPath(ad);
p.setRenderHint(QPainter::Antialiasing, false);
if (r.contains(m_mousePos)) {
QFont ttf; ttf.setPixelSize(11); p.setFont(ttf);
QFontMetrics ttfm(ttf);
QString tip = "Shift image";
int ttw = ttfm.horizontalAdvance(tip) + 8;
int tth = ttfm.height() + 4;
int ttx = r.right() + 4;
int tty = r.center().y() - tth / 2;
pendingTipRect = QRect(ttx, tty, ttw, tth);
pendingTipText = tip;
}
}
// Rotate image icon (button 5): curved arrow
if (i == 6) {
p.setRenderHint(QPainter::Antialiasing, true);
double cx2 = r.x() + r.width() / 2.0;
double cy2 = r.y() + r.height() / 2.0;
double rad = std::min(r.width(), r.height()) * 0.32;
p.setPen(QPen(m_rotateActive ? QColor(180, 180, 255) : Qt::white,
std::max(1, (int)(rad * 0.25))));
p.setBrush(Qt::NoBrush);
p.drawArc(QRectF(cx2 - rad, cy2 - rad, rad * 2, rad * 2),
30 * 16, 280 * 16);
// Arrowhead at end of arc (~310 degrees = -50 degrees)
double aAngle = 50.0 * M_PI / 180.0;
double ax = cx2 + rad * std::cos(aAngle);
double ay = cy2 + rad * std::sin(aAngle);
double arrowLen = rad * 1.0;
double arrowHalfW = rad * 0.3;
QPointF tangent(std::sin(aAngle), -std::cos(aAngle));
QPointF normal(std::cos(aAngle), std::sin(aAngle));
QPointF base(ax, ay);
QPointF tip = base + tangent * arrowLen;
QPointF left = base + normal * arrowHalfW;
QPointF right = base - normal * arrowHalfW;
p.setPen(Qt::NoPen);
p.setBrush(m_rotateActive ? QColor(180, 180, 255) : Qt::white);
QPainterPath ah;
ah.moveTo(tip);
ah.lineTo(left);
ah.lineTo(right);
ah.closeSubpath();
p.drawPath(ah);
p.setRenderHint(QPainter::Antialiasing, false);
if (r.contains(m_mousePos)) {
QFont ttf; ttf.setPixelSize(11); p.setFont(ttf);
QFontMetrics ttfm(ttf);
QString tip = "Rotate image";
int ttw = ttfm.horizontalAdvance(tip) + 8;
int tth = ttfm.height() + 4;
int ttx = r.right() + 4;
int tty = r.center().y() - tth / 2;
pendingTipRect = QRect(ttx, tty, ttw, tth);
pendingTipText = tip;
}
}
// Histogram threshold icon (button 25): a white bar histogram with a
// Gaussian peak, its top third of the value axis washed over in grey
// — the part a threshold takes out.
if (i == 25) {
p.setRenderHint(QPainter::Antialiasing, false);
const QRectF ir = QRectF(r).adjusted(3, 3, -3, -3);
const int nBars = 9;
const double bw = ir.width() / nBars;
const double sigma = nBars / 5.0;
const double mid = (nBars - 1) / 2.0;
p.setPen(Qt::NoPen);
p.setBrush(m_threshActive ? QColor(180, 180, 255) : Qt::white);
for (int k = 0; k < nBars; k++) {
const double t = (k - mid) / sigma;
const double bh = std::max(1.0, ir.height() * std::exp(-0.5 * t * t));
p.drawRect(QRectF(ir.x() + k * bw + bw * 0.15, ir.bottom() - bh,
std::max(1.0, bw * 0.7), bh));
}
p.setBrush(QColor(150, 150, 150, 180));
p.drawRect(QRectF(ir.x() + ir.width() * 2.0 / 3.0, ir.y(),
ir.width() / 3.0, ir.height()));
if (r.contains(m_mousePos)) {
QFont ttf; ttf.setPixelSize(11); p.setFont(ttf);
QFontMetrics ttfm(ttf);
QString tip = "Threshold the image with the current histogram selection";
int ttw = ttfm.horizontalAdvance(tip) + 8;
int tth = ttfm.height() + 4;
int ttx = r.right() + 4;
int tty = r.center().y() - tth / 2;
pendingTipRect = QRect(ttx, tty, ttw, tth);
pendingTipText = tip;
}
}
// Shear icon (button 22): a parallelogram — a square whose top edge
// has slid sideways, which is exactly the shape the tool leaves
// behind. Nothing else in the column is a leaning quadrilateral, so
// it reads at button size.
if (i == 22) {
p.setRenderHint(QPainter::Antialiasing, true);
const QColor col = m_shearActive ? QColor(180, 180, 255) : Qt::white;
const double cx2 = r.x() + r.width() / 2.0;
const double cy2 = r.y() + r.height() / 2.0;
const double side = std::min(r.width(), r.height());
const double half = side * 0.25; // half the (unsheared) square
const double lean = side * 0.15; // how far the top edge slides
QPainterPath pg;
pg.moveTo(cx2 - half + lean, cy2 - half);
pg.lineTo(cx2 + half + lean, cy2 - half);
pg.lineTo(cx2 + half - lean, cy2 + half);
pg.lineTo(cx2 - half - lean, cy2 + half);
pg.closeSubpath();
p.setPen(Qt::NoPen);
p.setBrush(col);
p.drawPath(pg);
p.setRenderHint(QPainter::Antialiasing, false);
if (r.contains(m_mousePos)) {
QFont ttf; ttf.setPixelSize(11); p.setFont(ttf);
QFontMetrics ttfm(ttf);
QString tip = "Shear image";
int ttw = ttfm.horizontalAdvance(tip) + 8;
int tth = ttfm.height() + 4;
int ttx = r.right() + 4;
int tty = r.center().y() - tth / 2;
pendingTipRect = QRect(ttx, tty, ttw, tth);
pendingTipText = tip;
}
}
// Invert contrast icon (button 6): +/- sign
if (i == 7) {
p.setRenderHint(QPainter::Antialiasing, true);
QRect ir = r.adjusted(3, 3, -3, -3);
int ix = ir.x(), iy2 = ir.y(), iw = ir.width(), ih = ir.height();
int voff = ih / 8; // shift down to visually center
int lw = std::max(1, iw / 12);
p.setPen(QPen(Qt::white, lw));
// Plus sign (top-left)
int plusCx = ix + iw / 4;
int plusCy = iy2 + ih / 4 + voff;
int arm = iw / 7;
p.drawLine(plusCx - arm, plusCy, plusCx + arm, plusCy);
p.drawLine(plusCx, plusCy - arm, plusCx, plusCy + arm);
// Minus sign (bottom-right)
int minusCx = ix + iw * 3 / 4;
int minusCy = iy2 + ih * 3 / 4 + voff;
p.drawLine(minusCx - arm, minusCy, minusCx + arm, minusCy);
// Slash between
p.drawLine(ix + iw * 0.60, iy2 + ih * 0.25 + voff, ix + iw * 0.40, iy2 + ih * 0.75 + voff);
p.setRenderHint(QPainter::Antialiasing, false);
if (r.contains(m_mousePos)) {
QFont ttf; ttf.setPixelSize(11); p.setFont(ttf);
QFontMetrics ttfm(ttf);
QString tip = "Invert contrast";
int ttw = ttfm.horizontalAdvance(tip) + 8;
int tth = ttfm.height() + 4;
int ttx = r.right() + 4;
int tty = r.center().y() - tth / 2;
pendingTipRect = QRect(ttx, tty, ttw, tth);
pendingTipText = tip;
}
}
// Taper edges icon (button 7): white square ring with black center
if (i == 8) {
p.setRenderHint(QPainter::Antialiasing, true);
QColor col = m_p1TaperActive ? QColor(180, 180, 255) : Qt::white;
int side = static_cast<int>(btnSide * 0.85);
int left = r.x() + (r.width() - side) / 2;
int top = r.y() + (r.height() - side) / 2;
QRect outer(left, top, side, side);
int ring = std::max(1, side / 10);
QRect inner = outer.adjusted(ring, ring, -ring, -ring);
p.setPen(Qt::NoPen);
p.setBrush(col);
p.drawRect(outer);
p.setBrush(Qt::black);
p.drawRect(inner);
p.setRenderHint(QPainter::Antialiasing, false);
if (r.contains(m_mousePos)) {
QFont ttf; ttf.setPixelSize(11); p.setFont(ttf);
QFontMetrics ttfm(ttf);
QString tip = "Taper edges";
int ttw = ttfm.horizontalAdvance(tip) + 8;
int tth = ttfm.height() + 4;
int ttx = r.right() + 4;
int tty = r.center().y() - tth / 2;
pendingTipRect = QRect(ttx, tty, ttw, tth);
pendingTipText = tip;
}
}
// Symmetrize icon (button 9): white five-fold star on black field
if (i == 9) {
p.setRenderHint(QPainter::Antialiasing, true);
double cx2 = r.x() + r.width() / 2.0;
double cy2 = r.y() + r.height() / 2.0;
double outerR = std::min(r.width(), r.height()) * 0.42;
double innerR = outerR * 0.40;
QPainterPath star;
for (int k = 0; k < 10; k++) {
double rad = (k % 2 == 0) ? outerR : innerR;
// Start with the top point pointing up (angle = -90°)
double ang = -M_PI / 2.0 + k * (M_PI / 5.0);
double px = cx2 + rad * std::cos(ang);
double py = cy2 + rad * std::sin(ang);
if (k == 0) star.moveTo(px, py);
else star.lineTo(px, py);
}
star.closeSubpath();
p.setPen(Qt::NoPen);
p.setBrush(m_p1SymmetrizeActive ? QColor(180, 180, 255) : Qt::white);
p.drawPath(star);
p.setRenderHint(QPainter::Antialiasing, false);
if (r.contains(m_mousePos)) {
QFont ttf; ttf.setPixelSize(11); p.setFont(ttf);
QFontMetrics ttfm(ttf);
QString tip = "Symmetrize Image";
int ttw = ttfm.horizontalAdvance(tip) + 8;
int tth = ttfm.height() + 4;
int ttx = r.right() + 4;
int tty = r.center().y() - tth / 2;
pendingTipRect = QRect(ttx, tty, ttw, tth);
pendingTipText = tip;
}
}
// Bin image icon (button 10): 2x2 grid representing pixel binning
if (i == 10) {
if (m_binActive) {
p.setPen(QPen(Qt::white, 1));
p.setBrush(QColor(60, 60, 60));
p.drawRect(r);
}
QColor col = m_binActive ? QColor(180, 180, 255) : Qt::white;
int m = std::max(2, btnSide / 5); // margin
QRect inner = r.adjusted(m, m, -m, -m);
int midX = inner.x() + inner.width() / 2;
int midY = inner.y() + inner.height() / 2;
// Draw 4 filled squares (2x2 grid) with gaps
int g = std::max(1, btnSide / 10); // gap between cells
p.setPen(Qt::NoPen);
p.setBrush(col);
p.drawRect(QRect(inner.x(), inner.y(), midX - inner.x() - g, midY - inner.y() - g));
p.setBrush(col.darker(140));
p.drawRect(QRect(midX + g, inner.y(), inner.right() - midX - g + 1, midY - inner.y() - g));
p.setBrush(col.darker(170));
p.drawRect(QRect(inner.x(), midY + g, midX - inner.x() - g, inner.bottom() - midY - g + 1));
p.setBrush(col.darker(120));
p.drawRect(QRect(midX + g, midY + g, inner.right() - midX - g + 1, inner.bottom() - midY - g + 1));
if (r.contains(m_mousePos)) {
QFont ttf; ttf.setPixelSize(11); p.setFont(ttf);
QFontMetrics ttfm(ttf);
QString tip = "Bin image";
int ttw = ttfm.horizontalAdvance(tip) + 8;
int tth = ttfm.height() + 4;
int ttx = r.right() + 4;
int tty = r.center().y() - tth / 2;
pendingTipRect = QRect(ttx, tty, ttw, tth);
pendingTipText = tip;
}
}
// Crop image icon (button 11): thin white square outline over the
// lower-right third of an otherwise black field
if (i == 11) {
if (m_cropActive) {
p.setPen(QPen(Qt::white, 1));
p.setBrush(QColor(60, 60, 60));
p.drawRect(r);
}
QColor col = m_cropActive ? QColor(180, 180, 255) : Qt::white;
int m = std::max(2, btnSide / 6);
int sq = std::max(2, (r.width() - 2 * m) * 3 / 5);
QRect cropSquare(r.right() - m - sq, r.bottom() - m - sq, sq, sq);
p.setPen(QPen(col, 1)); // thin white line
p.setBrush(Qt::NoBrush);
p.drawRect(cropSquare);
if (r.contains(m_mousePos)) {
QFont ttf; ttf.setPixelSize(11); p.setFont(ttf);
QFontMetrics ttfm(ttf);
QString tip = "Crop image";
int ttw = ttfm.horizontalAdvance(tip) + 8;
int tth = ttfm.height() + 4;
int ttx = r.right() + 4;
int tty = r.center().y() - tth / 2;
pendingTipRect = QRect(ttx, tty, ttw, tth);
pendingTipText = tip;
}
}
// Math calculations icon (button 15): Sigma/Sum sign
if (i == 15) {
if (m_mathActive) {
p.setPen(QPen(Qt::white, 1));
p.setBrush(QColor(60, 60, 60));
p.drawRect(r);
}
p.setRenderHint(QPainter::Antialiasing, true);
int inset = std::max(3, r.width() / 4);
QRect ir = r.adjusted(inset, inset, -inset, -inset);
int sx = ir.x(), sy = ir.y(), sw = ir.width(), sh = ir.height();
QColor col = m_mathActive ? QColor(180, 180, 255) : Qt::white;
// Draw Sigma (Σ) symbol
QPainterPath sigma;
sigma.moveTo(sx + sw * 0.85, sy + sh * 0.10); // top-right
sigma.lineTo(sx + sw * 0.15, sy + sh * 0.10); // top-left
sigma.lineTo(sx + sw * 0.50, sy + sh * 0.50); // center point (chevron)
sigma.lineTo(sx + sw * 0.15, sy + sh * 0.90); // bottom-left
sigma.lineTo(sx + sw * 0.85, sy + sh * 0.90); // bottom-right
// Top and bottom horizontal serifs
sigma.moveTo(sx + sw * 0.85, sy + sh * 0.10);
sigma.lineTo(sx + sw * 0.85, sy + sh * 0.18);
sigma.moveTo(sx + sw * 0.85, sy + sh * 0.90);
sigma.lineTo(sx + sw * 0.85, sy + sh * 0.82);
p.setPen(QPen(col, std::max(1, sw / 6)));
p.setBrush(Qt::NoBrush);
p.drawPath(sigma);
p.setRenderHint(QPainter::Antialiasing, false);
if (r.contains(m_mousePos)) {
QFont ttf; ttf.setPixelSize(11); p.setFont(ttf);
QFontMetrics ttfm(ttf);
QString tip = "Math calculations";
int ttw = ttfm.horizontalAdvance(tip) + 8;
int tth = ttfm.height() + 4;
int ttx = r.right() + 4;
int tty = r.center().y() - tth / 2;
pendingTipRect = QRect(ttx, tty, ttw, tth);
pendingTipText = tip;
}
}
// Particle picking icon (button 16): four green plus signs in 2x2 grid
if (i == 16) {
if (m_peakPickActive) {
p.setPen(QPen(Qt::white, 1));
p.setBrush(QColor(60, 60, 60));
p.drawRect(r);
}