32 public void onDraw(@NotNilptr QPainter canvas) {
34 __nilptr_safe(_cachedBitmap);
36 if (_present != 0 && switchbegin == 0) {
37 switchbegin = (width() - height()) * _present;
42 canvas.setRenderHint(RenderHint.Antialiasing,
true);
44 bool bReCreateCached =
false;
45 if (_cachedBitmap == nilptr ) {
46 bReCreateCached =
true;
48 }
else if (_cachedBitmap.width() != width() || _cachedBitmap.height() != height()) {
49 bReCreateCached =
true;
53 if (bReCreateCached) {
54 _cachedBitmap =
new QImage(width(), height(), QImage.Format_ARGB32);
58 _bForceRedraw =
false;
59 QPainter _canvas =
new QPainter(_cachedBitmap);
60 _canvas.setCompositionMode(CompositionMode.CompositionMode_Source);
61 _canvas.fillRect(0, 0, width(), height(), 0, QBrush.Style.SolidPattern);
62 _canvas.setRenderHint(RenderHint.Antialiasing,
true);
65 double padding = dip2px(3.f);
68 double r = (height() / 2.f) - padding ;
70 QPainter.Paint paint =
new QPainter.Paint();
73 QRect rect =
new QRect();
74 rect.left = rect.top = (padding / 2);
75 rect.right = width() - (padding / 2);
76 rect.bottom = height() - (padding / 2);
77 double rxy = rect.height() / 2.f;
79 paint.setStyle(QPainter.Paint.FILL_AND_STROKE);
80 paint.setColor( (this._bOn && (_present == 1)) ? switchOnColor : switchOffColor);
81 paint.setStrokeWidth(1.f);
83 _canvas.drawRoundedRect(rect, (
int)rxy, (int)rxy,paint);
85 QRect disablerect =
new QRect(rect);
86 disablerect.left = switchbegin;
87 disablerect.right = rect.right - (switchbegin);
88 disablerect.top = rect.top + (switchbegin);
89 disablerect.bottom = rect.bottom - (switchbegin);
91 if (disablerect.right > disablerect.left && disablerect.bottom > disablerect.top) {
92 paint.setStyle(QPainter.Paint.FILL);
94 double rate =
Math.
min(1.f,(1.f -_present));
95 int alpha = (int) (0xff * rate);
98 }
else if (alpha < 0) {
102 paint.setColor(0x00ffffff | alpha);
103 _canvas.drawRoundedRect(disablerect, (
int)rxy, (int)rxy,paint);
107 if (_present != 0.f) {
108 paint.setStyle(QPainter.Paint.FILL);
109 QRect avarect =
new QRect(rect);
110 avarect.right = switchbegin + (r * 2);
112 double rate = (1.f -_present);
113 int rgbValue = switchOnColor & 0xffffff;
114 int dist = (0xffffff - rgbValue);
116 int _rv = (int) (((dist>>16) & 0xff) * rate);
117 int _gv = (int) (((dist>>8) & 0xff) * rate);
118 int _bv = (int) ((dist & 0xff) * rate);
120 int newclr = (_rv<<16) | (_gv << 8) | _bv;
121 int colordis = (int) (switchOnColor + newclr);
122 paint.setColor(colordis);
123 _canvas.drawRoundedRect(avarect,(
int)rxy, (int)rxy,paint);
126 paint.setStyle(QPainter.Paint.FILL);
127 paint.setColor(0xffffffff);
129 _canvas.drawCircle((
int)(switchbegin + (height() / 2.f)), (int)(height() / 2.f), (
int)r, paint);
134 paint.setStrokeWidth(0.f);
135 paint.setStyle(QPainter.Paint.STROKE);
136 paint.setColor(0xffdedede);
138 _canvas.drawCircle((
int)(switchbegin + (height() / 2.f)), (int)(height() / 2.f), (
int)r, paint);
140 if (this.isEnabled() ==
false) {
143 canvas.drawImage(_cachedBitmap, 0, 0);
145 canvas.drawImage(_cachedBitmap, 0, 0);