4 @SuppressWarnings public class ProgressView : QWidget {
10 int _titleColor = 0xceffffff;
11 double _fontSize = 18;
13 double _textWidth = 0;
14 double _textHeight = 0;
19 public static const int WAIT_INFINITE = -1;
20 double _progressPersent = 0.f;
21 bool _bShowProgress =
false;
22 bool _bShowResult =
false;
23 bool _bResult =
false;
24 long _dismissMillionSecond = WAIT_INFINITE;
25 long _beginMillionSecond = 0;
26 OnDismissListener __dismissListener = nilptr;
27 QWidget _parent_widget = nilptr;
28 static const double HEIGHT_RATE = 1.15f;
32 public interface OnDismissListener {
37 __dismissListener = _dismissListener;
41 return (
int) (dpValue * _scale + 0.5f);
44 public void onDrawBackground(@NotNilptr QPainter canvas,
double padding, @NotNilptr QPainter.Paint paint) {
45 QRect rect =
new QRect();
46 canvas.setRenderHint(RenderHint.Antialiasing,
true);
47 rect.left = rect.top = padding;
48 rect.right = width() - padding;
49 rect.bottom = height() - padding;
52 paint.setColor(0xaf000000);
53 paint.setStyle(QPainter.Paint.FILL);
55 double rxy =
Math.
min(width(), height()) / 12.f;
57 canvas.drawRoundedRect(rect, (
int)rxy, (
int)rxy, paint);
61 public void DrawComplete(@NotNilptr QPainter canvas,
double padding,@NotNilptr QPainter.Paint paint,
double subCenterX,
62 double subCenterY,
double minCenterY,
double subwidth,
double subheight) {
63 double width = subwidth * 5.f;
64 double height = width / 6;
67 QRect progsub =
new QRect();
68 progsub.left = subCenterX - (width / 2.f);
69 progsub.top = subCenterY - (height / 2 );
70 progsub.bottom = (progsub.top + height);
71 progsub.right = progsub.left + width;
73 paint.setColor(0xffffffff);
75 paint.setStyle(QPainter.Paint.FILL);
78 double roundXY = subheight / 2;
80 canvas.rotate(90.f,subCenterX,subCenterY);
81 canvas.rotate(45.f,subCenterX + (width / 2.f),subCenterY );
83 canvas.drawRoundedRect(progsub, (
int)roundXY,(int)roundXY, paint);
87 canvas.translate(1,width / 2);
88 canvas.rotate(45,subCenterX,subCenterY);
89 progsub.right = progsub.left + (progsub.width() / 2.f) + (height / 2.f);
91 canvas.drawRoundedRect(progsub, (
int)roundXY,(
int)roundXY, paint);
95 public void DrawFatal(@NotNilptr QPainter canvas,
double padding,@NotNilptr QPainter.Paint paint,
double subCenterX,
96 double subCenterY,
double minCenterY,
double subwidth,
double subheight) {
97 double width = subwidth * 5.f;
98 double height = width / 5;
100 QRect progsub =
new QRect();
101 progsub.left = subCenterX - (width / 2.f);
102 progsub.top = subCenterY - (height / 2 );
103 progsub.bottom = (progsub.top + height);
104 progsub.right = progsub.left + width;
106 paint.setColor(0xffffffff);
107 paint.setStyle(QPainter.Paint.FILL);
110 double roundXY = subheight / 2;
111 canvas.rotate(45.f,subCenterX,subCenterY);
112 for (
int i = 0 ; i < 2; i ++) {
113 canvas.rotate(90,subCenterX,subCenterY);
114 canvas.drawRoundedRect(progsub,(
int) roundXY,(int)roundXY, paint);
119 public void DrawWaiting(@NotNilptr QPainter canvas,
double padding,@NotNilptr QPainter.Paint paint,
double subCenterX,
120 double subCenterY,
double minCenterY,
double subwidth,
double subheight) {
121 QRect progsub =
new QRect();
123 if (_bShowProgress) {
124 subCenterY =
Math.
max((
double)minCenterY,height() / 3.f);
127 progsub.left = subCenterX - (subwidth * 1.75f);
128 progsub.top = subCenterY - (subheight / 2);
129 progsub.bottom = progsub.top + subheight;
130 progsub.right = progsub.left + subwidth;
132 paint.setColor(0xbfffffff);
133 paint.setStyle(QPainter.Paint.FILL);
136 double roundXY = subheight / 2;
138 for (
int i = 0 ; i < 12; i ++) {
139 canvas.rotate((360 / 12),subCenterX,subCenterY);
140 int dis = _angle - (i * (360 / 12));
144 paint.setAlpha(255 * (360 - dis) / 360);
145 canvas.drawRoundedRect(progsub, (
int)roundXY, (
int)roundXY, paint);
150 public void calcTitlePosition(@NotNilptr QPainter canvas,
double padding,@NotNilptr QPainter.Paint paint) {
152 paint.setTextSize(dip2px(_fontSize));
154 paint.setColor(_titleColor);
155 QFont qf = canvas.getFont();
158 QRect rect = qf.measure(_titleText);
159 _textHeight= rect.height();
160 _textWidth = rect.width();
162 paint.setStyle(QPainter.Paint.FILL);
163 paint.setAlpha(0xff);
168 @NotNilptr QPainter.Paint paint,
double progressX,
double progressY,
169 double progressCX,
double progressCY) {
171 QRect progressRect =
new QRect();
172 progressRect.left = progressX;
173 progressRect.top = progressY;
174 progressRect.right = progressX + progressCX;
175 progressRect.bottom = progressY + progressCY;
177 double prxy = progressRect.height() / 2.f;
178 paint.setStyle(QPainter.Paint.STROKE);
179 paint.setStrokeWidth(dip2px(1));
180 canvas.drawRoundedRect(progressRect, (
int)prxy, (
int)prxy, paint);
182 paint.setStyle(QPainter.Paint.FILL);
183 progressRect.right = 2 + progressRect.left + (_progressPersent * progressCX );
184 canvas.drawRoundedRect(progressRect, (
int)prxy, (int)prxy, paint);
189 public void onDraw(@NotNilptr QPainter canvas) {
191 canvas.setPen(0, PenStyle.NoPen, 0);
193 double padding = dip2px(5);
194 QPainter.Paint paint =
new QPainter.Paint();
197 onDrawBackground(canvas,padding,paint);
199 double subwidth = dip2px(12);
200 double subheight = dip2px(3);
201 double minCenterY = padding + (subheight * 2.f);
202 double subCenterX = width() / 2.f;
203 double subCenterY =
Math.
max(minCenterY,height() * 2.f / 5.f);
206 DrawComplete(canvas,padding,paint,subCenterX,subCenterY,minCenterY,subwidth,subheight);
208 DrawFatal(canvas,padding,paint,subCenterX,subCenterY,minCenterY,subwidth,subheight);
211 DrawWaiting(canvas,padding,paint,subCenterX,subCenterY,minCenterY,subwidth,subheight);
214 calcTitlePosition(canvas,padding,paint);
215 double textX = subCenterX - (_textWidth / 2.f);
216 double textY = (subCenterY + (subwidth * 1.75f)) + (((height() - padding) - (subCenterY + (subwidth * 1.75f))) / 2);
219 if (_bShowProgress) {
220 textY = subCenterY + (subwidth * 1.75f) + (_textHeight * 1.2f) ;
222 canvas.drawText(_titleText, (
int)textX, (
int)(textY + (_textHeight * 0.8f)), paint);
224 paint.setColor(0xbfffffff);
226 if (_bShowProgress) {
227 double progressCX = (width() - padding) * 2 / 3;
228 double progressCY = progressCX / 12.f;
229 if (progressCY == 0) {
230 progressCY = dip2px(1);
232 double progressX = subCenterX - (width() - padding) / 3;
233 double progressY = ((textY) + ((height() - padding) - (textY)) / 2.f) - (progressCY / 2.f);
234 drawProgress(canvas,padding,paint,progressX,progressY,progressCX,progressCY);
239 synchronized(_timeObj) {
240 if (_animator != nilptr) {
243 _dismissMillionSecond = millionSecond;
244 _animator =
new Timer();
248 long waitTime = millionSecond;
252 _angle = (_angle) % 360;
254 synchronized(_timeObj) {
255 if (_animator != nilptr){
256 ProgressView.this.postUpdate();
257 if (waitTime != WAIT_INFINITE) {
261 runOnUi(
new Runnable() {
267 }
else if (_dismissMillionSecond != waitTime) {
268 waitTime = _dismissMillionSecond;
269 beginTime = _beginMillionSecond;
283 public void showModal(
long millionSecond,
bool bModal) {
284 setPalette(ColorRole.Background, 0x7fffffff);
286 setWindowFlags(WindowType.FramelessWindowHint | WindowType.WindowStaysOnTopHint | WindowType.Tool);
287 setAttribute(Constant.WA_TranslucentBackground,
true);
290 double height = width * HEIGHT_RATE ;
291 resize((
int)width, (
int)height);
292 if (_parent_widget != nilptr){
293 move(_parent_widget.x() + (_parent_widget.width() - width) / 2, _parent_widget.y() + (_parent_widget.height() - height) / 2);
297 beginAnimation(millionSecond);
304 public static ProgressView
showWait(
long millionSecond) {
305 ProgressView _progView =
new ProgressView();
306 if (_progView.create()) {
307 _progView.showModal(millionSecond,
false);
313 public static ProgressView
showWait(@NotNilptr QWidget p,
long millionSecond) {
314 ProgressView _progView =
new ProgressView(p);
315 if (_progView.create(p)) {
316 _progView.showModal(millionSecond,
true);
327 _titleText = szTitle;
331 _titleColor = nColor;
339 _titleText = szTitle;
340 _titleColor = nColor;
345 _bShowResult = bShowResult;
350 _bShowProgress = bEnable;
352 _progressPersent =
Math.
max(0.f, persent);
353 _progressPersent =
Math.
min(1.f, _progressPersent);
358 if (persent >= 0 && persent <= 1) {
359 _progressPersent = persent;
365 _dismissMillionSecond = millionSecond;
373 __dismissListener = _l;
378 double opacity = 1.f;
383 runOnUi(
new Runnable() {
386 if (closed ==
false){
402 public void onPaint(
int l,
int t,
int r,
int b,
long hpaint)
override {
403 onDraw(
new QPainter(hpaint));
407 if (__dismissListener != nilptr) {
408 __dismissListener.onDismissListener(ProgressView.this);
static final long currentTimeMillis()
static final int min(int, int)
static final int max(int, int)
void dismissAfter(int millionSecond)
void EnableProgress(bool bEnable,double persent)
void setTitleColor(int nColor)
void calcTitlePosition(@NotNilptr QPainter canvas,double padding,@NotNilptr QPainter.Paint paint)
void onDraw(@NotNilptr QPainter canvas)
void setTitle(String szTitle,int nColor,int sp)
void setProgress(double persent)
void onPaint(int l, int t, int r, int b, long hpaint) override
void beginAnimation(long millionSecond)
void DrawWaiting(@NotNilptr QPainter canvas,double padding,@NotNilptr QPainter.Paint paint,double subCenterX, double subCenterY,double minCenterY,double subwidth,double subheight)
void setTitleFontSize(int sp)
static ProgressView showWait(long millionSecond)
void setTitleText(String szTitle)
void showResult(bool bShowResult,bool bSuccess)
int dip2px(double dpValue)
void dismiss(OnDismissListener _l)
ProgressView(QWidget parent)
static ProgressView showWait(@NotNilptr QWidget p, long millionSecond)
void setOnDismissListener(OnDismissListener _dismissListener)
void DrawFatal(@NotNilptr QPainter canvas,double padding,@NotNilptr QPainter.Paint paint,double subCenterX, double subCenterY,double minCenterY,double subwidth,double subheight)
void onDrawBackground(@NotNilptr QPainter canvas, double padding, @NotNilptr QPainter.Paint paint)
void drawProgress(@NotNilptr QPainter canvas,double padding, @NotNilptr QPainter.Paint paint,double progressX,double progressY, double progressCX,double progressCY)
void DrawComplete(@NotNilptr QPainter canvas,double padding,@NotNilptr QPainter.Paint paint,double subCenterX, double subCenterY,double minCenterY,double subwidth,double subheight)
void showModal(long millionSecond, bool bModal)
bool schedule(TimerTask, int)
void onDismissListener(ProgressView v)