/
qnanopainter.cpp
1618 lines (1263 loc) · 48.1 KB
/
qnanopainter.cpp
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
/**********************************************************
** Copyright (c) 2015 QUIt Coding <info@quitcoding.com>
**
** This software is provided 'as-is', without any express or implied
** warranty. In no event will the authors be held liable for any damages
** arising from the use of this software.
**
** Permission is granted to anyone to use this software for any purpose,
** including commercial applications, and to alter it and redistribute it
** freely, subject to the following restrictions:
**
** 1. The origin of this software must not be misrepresented; you must not
** claim that you wrote the original software. If you use this software
** in a product, an acknowledgment in the product documentation would be
** appreciated but is not required.
** 2. Altered source versions must be plainly marked as such, and must not be
** misrepresented as being the original software.
** 3. This notice may not be removed or altered from any source distribution.
**
**********************************************************/
#include "qnanopainter.h"
#include "nanovg/nanovg.h"
#include "private/qnanobackendfactory.h"
#include <QScreen>
#include <QGuiApplication>
#include <QOpenGLContext>
#include <QOpenGLFunctions>
Q_GLOBAL_STATIC(QNanoPainter, instance)
/*!
\class QNanoPainter
\brief The QNanoPainter class provides an API for painting into QNanoQuickItem.
\inmodule QNanoPainter
TODO: Write more documentation here.
*/
/*!
\enum QNanoPainter::PathWinding
PathWinding is used to specify the direction of path drawing.
This direction is used to determine if a subpath is solid or hole in the path.
\value WINDING_CCW (default) Clockwise winding for solid shapes.
\value WINDING_CW Counter-clockwise winding for holes.
\sa setPathWinding()
*/
/*!
\enum QNanoPainter::LineCap
LineCap is used to define how the end of the line (cap) is drawn.
\value CAP_BUTT (default) Square line ending that does not cover the end point of the line.
\value CAP_ROUND Round line ending.
\value CAP_SQUARE Square line ending that covers the end point and extends beyond it by half the line width.
\sa setLineCap()
*/
/*!
\enum QNanoPainter::LineJoin
LineJoin is used to define how the joins between two connected lines are drawn.
\value JOIN_ROUND Circular arc between the two lines is filled.
\value JOIN_BEVEL The triangular notch between the two lines is filled.
\value JOIN_MITER (default) The outer edges of the lines are extended to meet at an angle, and this area is filled.
\sa setLineJoin(), setMiterLimit()
*/
/*!
\enum QNanoPainter::TextAlign
TextAlign is used to define how the text is aligned horizontally.
\value ALIGN_LEFT (default) Align the left side of the text horizontally to the specified position.
\value ALIGN_CENTER Align the center of the text horizontally to the specified position.
\value ALIGN_RIGHT Align the right side of the text horizontally to the specified position.
\sa setTextAlign(), fillText()
*/
/*!
\enum QNanoPainter::TextBaseline
TextBaseline is used to define how the text is aligned (baselined) vertically.
\value BASELINE_TOP Align the top of the text vertically to the specified position.
\value BASELINE_MIDDLE Align the middle of the text vertically to the specified position.
\value BASELINE_BOTTOM Align the bottom of the text vertically to the specified position.
\value BASELINE_ALPHABETIC (default) Align the baseline of the text vertically to the specified position.
\sa setTextBaseline(), fillText()
*/
/*!
\enum QNanoPainter::PixelAlign
PixelAlign is used to define if painting or text should be aligned to pixels.
\value PIXEL_ALIGN_NONE (default) Do not do any alignment.
\value PIXEL_ALIGN_HALF Align to half pixels. This will make painting appear sharp when line width is odd.
\value PIXEL_ALIGN_FULL Align to full pixels. This will make painting appear sharp when line width is even.
\sa setPixelAlign(), setPixelAlignText()
*/
/*!
Constructs a painter.
*/
QNanoPainter::QNanoPainter()
: m_nvgContext(nullptr)
, m_textAlign(QNanoPainter::ALIGN_LEFT)
, m_textBaseline(QNanoPainter::BASELINE_ALPHABETIC)
, m_devicePixelRatio(1.0f)
, m_fontSet(false)
{
// Request actual OpenGL context version and type
QOpenGLContext *context = QOpenGLContext::currentContext();
Q_ASSERT_X(context, "QNanoPainter::QNanoPainter", "No QOpenGL Context available!");
m_surfaceFormat = context->format();
int major = m_surfaceFormat.majorVersion();
int minor = m_surfaceFormat.minorVersion();
bool isGLES = (QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGLES);
// Create QNanoBackend most suitable for the context
m_backend.reset(QNanoBackendFactory::createBackend(major, minor, isGLES));
m_openglContextName = QString("%1 %2.%3").arg(isGLES ? "OpenGL ES" : "OpenGL").arg(major).arg(minor);
qDebug() << "Using backend:" << m_backend->backendName();
// Initialize NanoVG for correct GL version
// NOTE: Add also NVG_DEBUG when want to check possible OpenGL errors.
m_nvgContext = m_backend->nvgCreate(NVG_ANTIALIAS);
Q_ASSERT_X(m_nvgContext, "QNanoPainter::QNanoPainter", "Could not init nanovg!");
setPixelAlign(QNanoPainter::PIXEL_ALIGN_NONE);
setPixelAlignText(QNanoPainter::PIXEL_ALIGN_NONE);
}
/*!
Destroys the painter.
*/
QNanoPainter::~QNanoPainter()
{
if (m_backend && m_nvgContext && QOpenGLContext::currentContext()) {
// Do NanoVG side cleanups only if OpenGL context still exists
m_backend->nvgDelete(m_nvgContext);
}
qDeleteAll(m_dataCache);
}
/*!
\fn void QNanoPainter::beginFrame(float width, float height)
Initializes QNanoPainter frame painting. All QNanoPainting calls should
be in between beginFrame() and endFrame(). This is called automatically
before entering into paint() -method, so users don't usually need to
call it. But when utilizing FBOs, call this after binding the FBO to
start painting with QNanoPainter into it. Also sets glViewport to
match the frame position & size.
QNanoPainter frame position will be 0, 0 and size \a width, \a height.
\sa endFrame()
*/
void QNanoPainter::beginFrame(float width, float height)
{
beginFrameAt(0, 0, width, height);
}
/*!
\fn void QNanoPainter::beginFrame(float x, float y, float width, float height)
Initializes QNanoPainter frame painting. All QNanoPainting calls should
be in between beginFrame() and endFrame(). This is called automatically
before entering into paint() -method, so users don't usually need to
call it. But when utilizing FBOs, call this after binding the FBO to
start painting with QNanoPainter into it. Also sets glViewport to
match the frame position & size.
QNanoPainter frame position will be x, y and size \a width, \a height.
\sa endFrame()
*/
void QNanoPainter::beginFrameAt(float x, float y, float width, float height)
{
nvgBeginFrameAt(nvgCtx(), x, y, width, height, m_devicePixelRatio);
QOpenGLFunctions glF(QOpenGLContext::currentContext());
glF.glViewport(int(x), int(y), int(width), int(height));
}
/*!
\fn void QNanoPainter::endFrame()
Finalizes QNanoPainter frame painting. This is called automatically
after exiting paint() -method, so users don't usually need to call it.
But when utilizing FBOs, call this before binding the FBO to end default
frame painting as well as after you have painted into FBO.
\sa beginFrame()
*/
void QNanoPainter::endFrame()
{
nvgEndFrame(nvgCtx());
}
/*!
\fn void QNanoPainter::cancelFrame()
Cancel all painting to current frame. This can be called if you wish to
not draw the frame.
\sa beginFrame()
*/
void QNanoPainter::cancelFrame()
{
nvgCancelFrame(nvgCtx());
}
// *** State Handling ***
// QNanoPainter state controls how the painting paths will be rendered.
// The state contains transform, fill and stroke styles, text and font styles,
// and scissor clipping.
/*!
\fn void QNanoPainter::save()
Pushes and saves the current render state into a state stack.
A matching restore() must be used to restore the state.
\sa restore()
*/
void QNanoPainter::save()
{
nvgSave(nvgCtx());
}
/*!
\fn void QNanoPainter::restore()
Pops and restores current render state.
So previously saved state will be restored.
\sa save()
*/
void QNanoPainter::restore()
{
nvgRestore(nvgCtx());
}
/*!
\fn void QNanoPainter::reset()
Resets the current painter state to default values.
Does not affect the render state stack.
\sa save(), restore()
*/
void QNanoPainter::reset()
{
m_textAlign = QNanoPainter::ALIGN_LEFT;
m_textBaseline = QNanoPainter::BASELINE_ALPHABETIC;
m_fontSet = false;
m_pixelAlign = QNanoPainter::PIXEL_ALIGN_NONE;
m_pixelAlignText = QNanoPainter::PIXEL_ALIGN_NONE;
nvgReset(nvgCtx());
}
// *** Render styles ***
// Fill and stroke render style can be either a solid color or a brush which is a gradient or a pattern.
// Solid color is QNanoColor, brush can be QNanoLinearGradient etc. or QNanoImagePattern.
//
// Current render style can be saved and restored using save() and restore().
/*!
\fn void QNanoPainter::setStrokeStyle(const QNanoColor &color)
Sets the stroke style to a solid \a color.
*/
void QNanoPainter::setStrokeStyle(const QNanoColor &color)
{
nvgStrokeColor(nvgCtx(), color.nvgColor());
}
/*!
\fn void QNanoPainter::setStrokeStyle(const QNanoBrush &brush)
\overload
Sets the stroke style to \a brush. Brush can be some gradient or QNanoImagePattern.
*/
void QNanoPainter::setStrokeStyle(const QNanoBrush &brush)
{
// If brush is QNanoImagePattern set its painter
QNanoImagePattern *ip = dynamic_cast<QNanoImagePattern*>(const_cast<QNanoBrush*>(&brush));
if (ip && ip->m_image) {
ip->m_image->setParentPainter(this);
}
QNanoBrush* b = const_cast<QNanoBrush*>(&brush);
if (b) {
b->setParentPainter(this);
}
nvgStrokePaint(nvgCtx(), brush.nvgPaint(nvgCtx()));
}
/*!
\fn void QNanoPainter::setFillStyle(const QNanoColor &color)
Sets the fill style to a solid \a color.
*/
void QNanoPainter::setFillStyle(const QNanoColor &color)
{
nvgFillColor(nvgCtx(), color.nvgColor());
}
/*!
\fn void QNanoPainter::setFillStyle(const QNanoBrush &brush)
\overload
Sets the fill style to \a brush. Brush can be some gradient or QNanoImagePattern.
*/
void QNanoPainter::setFillStyle(const QNanoBrush &brush)
{
// If brush is QNanoImagePattern set its painter
QNanoImagePattern *ip = dynamic_cast<QNanoImagePattern*>(const_cast<QNanoBrush*>(&brush));
if (ip && ip->m_image) {
ip->m_image->setParentPainter(this);
}
QNanoBrush* b = const_cast<QNanoBrush*>(&brush);
if (b) {
b->setParentPainter(this);
}
nvgFillPaint(nvgCtx(), brush.nvgPaint(nvgCtx()));
}
/*!
\fn void QNanoPainter::setMiterLimit(float limit)
Sets the miter limit to \a limit. Miter limit controls when a sharp corner
is beveled. When corner degree between lines in a path is less than this limit,
LineJoin will be applied between the lines.
The default limit is 10.0.
\sa setLineJoin()
*/
void QNanoPainter::setMiterLimit(float limit)
{
nvgMiterLimit(nvgCtx(), limit);
}
/*!
\fn void QNanoPainter::setLineWidth(float width)
Sets the line width of stoke to \a width in pixels.
The default line width is 1.0.
\sa stroke()
*/
void QNanoPainter::setLineWidth(float width)
{
nvgStrokeWidth(nvgCtx(), width);
}
/*!
\fn void QNanoPainter::setLineCap(LineCap cap)
Sets the end of the line of stoke to \a cap.
The default line cap is CAP_BUTT.
*/
void QNanoPainter::setLineCap(LineCap cap)
{
nvgLineCap(nvgCtx(), cap);
}
/*!
\fn void QNanoPainter::setLineJoin(LineJoin join)
Sets the line join of stoke to \a join. This join is used when drawing
sharper corners than miter limit.
The default line join is JOIN_MITER.
\sa setMiterLimit()
*/
void QNanoPainter::setLineJoin(LineJoin join)
{
nvgLineJoin(nvgCtx(), join);
}
/*!
\fn void QNanoPainter::setGlobalAlpha(float alpha)
Sets the global alpha (transparency) value to \a alpha. This alpha value is
applied to all rendered shapes. Already transparent paths will get
proportionally more transparent as well.
Alpha should be between 0.0 (fully transparent) and 1.0 (fully opaque).
By default alpha is 1.0.
*/
void QNanoPainter::setGlobalAlpha(float alpha)
{
nvgGlobalAlpha(nvgCtx(), alpha);
}
/*!
\fn void QNanoPainter::setGlobalCompositeOperation(CompositeOperation operation)
Sets the global composite operation mode to \a operation. This mode is
applied to all painting operations. Composite modes match to ones available in
HTML canvas. The default mode is COMPOSITE_SOURCE_OVER.
Note: Composite (blend) mode can be set with either HTML or OpenGL style method,
they override each other.
\sa setGlobalCompositeBlendFunc(), setGlobalCompositeBlendFuncSeparate()
*/
void QNanoPainter::setGlobalCompositeOperation(CompositeOperation operation)
{
nvgGlobalCompositeOperation(nvgCtx(), operation);
}
/*!
\fn void QNanoPainter::setGlobalCompositeBlendFunc(BlendFactor sourceFactor, BlendFactor destinationFactor)
Sets the global blend modes to \a sourceFactor and \a destinationFactor. This mode is
applied to all painting operations. Blend modes match to ones available in
OpenGL. The default modes are source BLEND_ONE and destination BLEND_ZERO.
Note: Composite (blend) mode can be set with either HTML or OpenGL style method,
they override each other.
\sa setGlobalCompositeBlendFuncSeparate(), setGlobalCompositeOperation()
*/
void QNanoPainter::setGlobalCompositeBlendFunc(BlendFactor sourceFactor, BlendFactor destinationFactor)
{
nvgGlobalCompositeBlendFunc(nvgCtx(), sourceFactor, destinationFactor);
}
/*!
\fn void QNanoPainter::setGlobalCompositeBlendFuncSeparate(BlendFactor sourceRGB, BlendFactor destinationRGB, BlendFactor sourceAlpha, BlendFactor destinationAlpha)
Sets the global blend modes separately for RGB and alpha to \a sourceRGB,
\a destinationRGB, \a sourceAlpha and \a destinationAlpha. This mode is
applied to all painting operations. Blend modes match to ones available in
OpenGL. The default modes are sourceRGB and sourceAlpha BLEND_ONE,
destinationRGB and destinationAlpha BLEND_ZERO.
Note: Composite (blend) mode can be set with either HTML or OpenGL style method,
they override each other.
\sa setGlobalCompositeBlendFunc(), setGlobalCompositeOperation()
*/
void QNanoPainter::setGlobalCompositeBlendFuncSeparate(BlendFactor sourceRGB, BlendFactor destinationRGB, BlendFactor sourceAlpha, BlendFactor destinationAlpha)
{
nvgGlobalCompositeBlendFuncSeparate(nvgCtx(), sourceRGB, destinationRGB, sourceAlpha, destinationAlpha);
}
// *** Transforms ***
// The paths, gradients, patterns and scissor region are transformed by an transformation
// matrix at the time when they are passed to the API.
// The current transformation matrix is a affine matrix:
// [sx kx tx]
// [ky sy ty]
// [ 0 0 1]
// Where: sx,sy define scaling, kx,ky skewing, and tx,ty translation.
// The last row is assumed to be 0,0,1 and is not stored.
//
// Current coordinate system (transformation) can be saved and restored using save() and restore().
/*!
\fn void QNanoPainter::resetTransform()
Resets current transform to a identity matrix.
*/
void QNanoPainter::resetTransform()
{
nvgResetTransform(nvgCtx());
}
/*!
\fn void QNanoPainter::transform(const QTransform &transform)
Premultiplies current coordinate system by specified \a transform.
*/
void QNanoPainter::transform(const QTransform &transform)
{
nvgTransform(nvgCtx()
, static_cast<float>(transform.m11())
, static_cast<float>(transform.m12())
, static_cast<float>(transform.m21())
, static_cast<float>(transform.m22())
, static_cast<float>(transform.m31())
, static_cast<float>(transform.m32()));
}
/*!
\fn void QNanoPainter::translate(float x, float y)
Translates current coordinate system by \a x and \a y.
*/
void QNanoPainter::translate(float x, float y)
{
nvgTranslate(nvgCtx(), x, y);
}
/*!
\fn void QNanoPainter::translate(const QPointF &point)
Translates current coordinate system by \a point.
*/
void QNanoPainter::translate(const QPointF &point)
{
translate(static_cast<float>(point.x()),
static_cast<float>(point.y()));
}
/*!
\fn void QNanoPainter::rotate(float angle)
Rotates current coordinate system by \a angle. Angle is specified in radians.
*/
void QNanoPainter::rotate(float angle)
{
nvgRotate(nvgCtx(), angle);
}
/*!
\fn void QNanoPainter::skewX(float angle)
Skews the current coordinate system along X axis by \a angle. Angle is specifid in radians.
*/
void QNanoPainter::skewX(float angle)
{
nvgSkewX(nvgCtx(), angle);
}
/*!
\fn void QNanoPainter::skewY(float angle)
Skews the current coordinate system along Y axis by \a angle. Angle is specifid in radians.
*/
void QNanoPainter::skewY(float angle)
{
nvgSkewY(nvgCtx(), angle);
}
/*!
\fn void QNanoPainter::scale(float scale)
Scales the current coordinat system by \a scale. Both x and y coordinates
are scaled evenly.
*/
void QNanoPainter::scale(float scale)
{
nvgScale(nvgCtx(), scale, scale);
}
/*!
\fn void QNanoPainter::scale(float scaleX, float scaleY)
Scales the current coordinat system by \a scaleX and \a scaleY.
*/
void QNanoPainter::scale(float scaleX, float scaleY)
{
nvgScale(nvgCtx(), scaleX, scaleY);
}
/*!
\fn const QTransform QNanoPainter::currentTransform() const
Returns the current transform.
*/
const QTransform QNanoPainter::currentTransform() const
{
float *xform = new float[6];
nvgCurrentTransform(nvgCtx(), xform);
QTransform t(static_cast<double>(xform[0]),
static_cast<double>(xform[1]),
static_cast<double>(xform[2]),
static_cast<double>(xform[3]),
static_cast<double>(xform[4]),
static_cast<double>(xform[5]));
delete [] xform;
return t;
}
// *** Scissoring ***
// Scissoring allows you to clip the rendering into a rectangle. This is useful for varius
// user interface cases like rendering a text edit or a timeline.
/*!
\fn void QNanoPainter::setClipRect(float x, float y, float width, float height)
Sets the current scissor rectangle to (\a x, \a y, \a width, \a height).
The scissor rectangle is transformed by the current transform.
Note: Clipping has some performance cost and it should only be used
when needed.
*/
void QNanoPainter::setClipRect(float x, float y, float width, float height)
{
_checkAlignPixelsAdjust(&x, &y);
_checkAlignPixels(&width, &height);
nvgScissor(nvgCtx(), x, y, width, height);
// Note: By-design the flag is set whenever clipping is used but
// it is never unset. That is because the flag changes the shader
// and we don't want multiple of those when some paintings are
// clipped and some not.
if (!m_clippingEnabled) {
m_clippingEnabled = true;
m_backend->setFlag(nvgCtx(), NVG_SCISSORING, true);
}
}
/*!
\fn void QNanoPainter::setClipRect(const QRectF &rect)
Sets the current scissor rectangle to \a rect.
The scissor rectangle is transformed by the current transform.
\sa resetClipping()
*/
void QNanoPainter::setClipRect(const QRectF &rect)
{
setClipRect(static_cast<float>(rect.x()),
static_cast<float>(rect.y()),
static_cast<float>(rect.width()),
static_cast<float>(rect.height()));
}
/*!
\fn void QNanoPainter::resetClipping()
Resets and disables clipping.
\sa setClipRect()
*/
void QNanoPainter::resetClipping()
{
nvgResetScissor(nvgCtx());
}
// *** Paths ***
// All shape drawing in QNanoPainter belong to paths. Drawing a path starts with beginPath()
// which clears the currently defined path. Then draw paths using shapes such as lines, rects,
// circles etc. And finally stroke and/or fill the path using current styles.
//
// QNanoPainter uses even-odd fill rule to draw shapes. To draw holes into a shape you can
// set pathWinding correspondingly.
//
// Current transtormation affects how paths are drawn.
/*!
\fn void QNanoPainter::beginPath()
Begins drawing a new path while clearing the current path.
*/
void QNanoPainter::beginPath()
{
nvgBeginPath(nvgCtx());
}
/*!
\fn void QNanoPainter::closePath()
Closes the current sub-path with a line segment.
This is equivalent to lineTo([starting point]) as the last path element.
*/
void QNanoPainter::closePath()
{
nvgClosePath(nvgCtx());
}
/*!
\fn void QNanoPainter::moveTo(float x, float y)
Starts new sub-path with ( \a x, \a y) as first point.
*/
void QNanoPainter::moveTo(float x, float y)
{
_checkAlignPixelsAdjust(&x, &y);
nvgMoveTo(nvgCtx(), x, y);
}
/*!
\fn void QNanoPainter::moveTo(const QPointF &point)
\overload
Starts new sub-path with \a point as first point.
*/
void QNanoPainter::moveTo(const QPointF &point)
{
moveTo(static_cast<float>(point.x()),
static_cast<float>(point.y()));
}
/*!
\fn void QNanoPainter::lineTo(float x, float y)
Adds line segment from the last point in the path to the ( \a x, \a y) point.
*/
void QNanoPainter::lineTo(float x, float y)
{
_checkAlignPixelsAdjust(&x, &y);
nvgLineTo(nvgCtx(), x, y);
}
/*!
\fn void QNanoPainter::lineTo(const QPointF &point)
\overload
Adds line segment from the last point in the path to the \a point.
*/
void QNanoPainter::lineTo(const QPointF &point)
{
lineTo(static_cast<float>(point.x()),
static_cast<float>(point.y()));
}
/*!
\fn void QNanoPainter::bezierTo(float c1x, float c1y, float c2x, float c2y, float x, float y)
Adds cubic bezier segment from last point in the path via two
control points (\a c1x, \a c1y and \a c2x, \a c2y) to the specified point (\a x, \a y).
*/
void QNanoPainter::bezierTo(float c1x, float c1y, float c2x, float c2y, float x, float y)
{
_checkAlignPixelsAdjust(&x, &y);
_checkAlignPixelsAdjust(&c1x, &c1y, &c2x, &c2y);
nvgBezierTo(nvgCtx(), c1x, c1y, c2x, c2y, x, y);
}
/*!
\fn void QNanoPainter::bezierTo(const QPointF &controlPoint1, const QPointF &controlPoint2, const QPointF &endPoint)
\overload
Adds cubic bezier segment from last point in the path via two
control points (\a controlPoint1 and \a controlPoint2) to the specified point \a endPoint.
*/
void QNanoPainter::bezierTo(const QPointF &controlPoint1, const QPointF &controlPoint2, const QPointF &endPoint)
{
bezierTo(static_cast<float>(controlPoint1.x()),
static_cast<float>(controlPoint1.y()),
static_cast<float>(controlPoint2.x()),
static_cast<float>(controlPoint2.y()),
static_cast<float>(endPoint.x()),
static_cast<float>(endPoint.y()));
}
/*!
\fn void QNanoPainter::quadTo(float cx, float cy, float x, float y)
Adds quadratic bezier segment from last point in the path via
a control point (\a cx, \a cy) to the specified point (\a x, \a y).
*/
void QNanoPainter::quadTo(float cx, float cy, float x, float y)
{
_checkAlignPixelsAdjust(&cx, &cy, &x, &y);
nvgQuadTo(nvgCtx(), cx, cy, x, y);
}
/*!
\fn void QNanoPainter::quadTo(const QPointF &controlPoint, const QPointF &endPoint)
\overload
Adds quadratic bezier segment from last point in the path via
a \a controlPoint to the specified \a endPoint.
*/
void QNanoPainter::quadTo(const QPointF &controlPoint, const QPointF &endPoint)
{
quadTo(static_cast<float>(controlPoint.x()),
static_cast<float>(controlPoint.y()),
static_cast<float>(endPoint.x()),
static_cast<float>(endPoint.y()));
}
/*!
\fn void QNanoPainter::arcTo(float c1x, float c1y, float c2x, float c2y, float radius)
Adds an arc segment at the corner defined by the last path point,
and two specified points (\a c1x, \a c1y and \a c2x, \a c2y) with \a radius.
*/
void QNanoPainter::arcTo(float c1x, float c1y, float c2x, float c2y, float radius)
{
_checkAlignPixelsAdjust(&c1x, &c1y, &c2x, &c2y);
nvgArcTo(nvgCtx(), c1x, c1y, c2x, c2y, radius);
}
/*!
\fn void QNanoPainter::arcTo(const QPointF &controlPoint1, const QPointF &controlPoint2, float radius)
\overload
Adds an arc segment at the corner defined by the last path point,
and two specified points (\a controlPoint1 and \a controlPoint2) with \a radius.
*/
void QNanoPainter::arcTo(const QPointF &controlPoint1, const QPointF &controlPoint2, float radius)
{
arcTo(static_cast<float>(controlPoint1.x()),
static_cast<float>(controlPoint1.y()),
static_cast<float>(controlPoint2.x()),
static_cast<float>(controlPoint2.y()),
radius);
}
/*!
\fn void QNanoPainter::arc(float centerX, float centerY, float radius, float a0, float a1, PathWinding direction)
Creates new circle arc shaped sub-path. The arc center is at \a centerX, \a centerY,
with \a radius, and the arc is drawn from angle \a a0 to \a a1,
and swept in \a direction (WINDING_CW or WINDING_CCW).
Angles are specified in radians.
*/
void QNanoPainter::arc(float centerX, float centerY, float radius, float a0, float a1, PathWinding direction)
{
_checkAlignPixelsAdjust(¢erX, ¢erY);
nvgArc(nvgCtx(), centerX, centerY, radius, a0, a1, direction);
}
/*!
\fn void QNanoPainter::arc(const QPointF ¢erPoint, float radius, float a0, float a1, PathWinding direction)
\overload
Creates new circle arc shaped sub-path. The arc center is at \a centerPoint,
with \a radius, and the arc is drawn from angle \a a0 to \a a1,
and swept in \a direction (WINDING_CW or WINDING_CCW).
Angles are specified in radians.
*/
void QNanoPainter::arc(const QPointF ¢erPoint, float radius, float a0, float a1, PathWinding direction)
{
arc(static_cast<float>(centerPoint.x()),
static_cast<float>(centerPoint.y()),
radius, a0, a1, direction);
}
/*!
\fn void QNanoPainter::rect(float x, float y, float width, float height)
Creates new rectangle shaped sub-path in position \a x, \a y with
size \a width, \a height.
*/
void QNanoPainter::rect(float x, float y, float width, float height)
{
_checkAlignPixelsAdjust(&x, &y);
_checkAlignPixels(&width, &height);
nvgRect(nvgCtx(), x, y, width, height);
}
/*!
\fn void QNanoPainter::rect(const QRectF &rect)
\overload
Creates new rectangle shaped sub-path at \a rect.
This is an overloaded method using QRectF.
*/
void QNanoPainter::rect(const QRectF &rect)
{
this->rect(static_cast<float>(rect.x()),
static_cast<float>(rect.y()),
static_cast<float>(rect.width()),
static_cast<float>(rect.height()));
}
/*!
\fn void QNanoPainter::roundedRect(float x, float y, float width, float height, float radius)
Creates new rounded rectangle shaped sub-path in position \a x, \a y with
size \a width, \a height. Corners rounding will be \a radius.
*/
void QNanoPainter::roundedRect(float x, float y, float width, float height, float radius)
{
_checkAlignPixelsAdjust(&x, &y);
_checkAlignPixels(&width, &height);
nvgRoundedRect(nvgCtx(), x, y, width, height, radius);
}
/*!
\fn void QNanoPainter::roundedRect(const QRectF &rect, float radius)
\overload
Creates new rounded rectangle shaped sub-path at \a rect with \a radius corners.
This is an overloaded method using QRectF.
*/
void QNanoPainter::roundedRect(const QRectF &rect, float radius)
{
roundedRect(static_cast<float>(rect.x()),
static_cast<float>(rect.y()),
static_cast<float>(rect.width()),
static_cast<float>(rect.height()),
radius);
}
/*!
\fn void QNanoPainter::roundedRect(float x, float y, float width, float height, float radiusTopLeft, float radiusTopRight, float radiusBottomRight, float radiusBottomLeft)
Creates new rounded rectangle shaped sub-path in position \a x, \a y with
size \a width, \a height. Corners rounding can be varying per-corner, with
\a radiusTopLeft, \a radiusTopRight, \a radiusBottomRight, \a radiusBottomLeft.
*/
void QNanoPainter::roundedRect(float x, float y, float width, float height, float radiusTopLeft, float radiusTopRight, float radiusBottomRight, float radiusBottomLeft)
{
_checkAlignPixelsAdjust(&x, &y);
_checkAlignPixels(&width, &height);
nvgRoundedRectVarying(nvgCtx(), x, y, width, height, radiusTopLeft, radiusTopRight, radiusBottomRight, radiusBottomLeft);
}
/*!
\fn void QNanoPainter::roundedRect(const QRectF &rect, float radiusTopLeft, float radiusTopRight, float radiusBottomRight, float radiusBottomLeft)
\overload
Creates new rounded rectangle shaped sub-path at \a rect. Corners rounding can be
varying per-corner, with \a radiusTopLeft, \a radiusTopRight, \a radiusBottomRight,
\a radiusBottomLeft.
*/
void QNanoPainter::roundedRect(const QRectF &rect, float radiusTopLeft, float radiusTopRight, float radiusBottomRight, float radiusBottomLeft)
{
roundedRect(static_cast<float>(rect.x()),
static_cast<float>(rect.y()),
static_cast<float>(rect.width()),
static_cast<float>(rect.height()),
radiusTopLeft, radiusTopRight, radiusBottomRight, radiusBottomLeft);
}