xlang v4.0 Release
程序设计语言基础库文档
载入中...
搜索中...
未找到
ProgressView.xcsm
浏览该文件的文档.
1//xlang Source, Name:ProgressView.xcsm
2//Date: Sun Oct 01:24:51 2018
3package Qt{
4 @SuppressWarnings public class ProgressView : QWidget {
5
6 double _scale = 1.f;
7
8 String _titleText = "loading";
9
10 int _titleColor = 0xceffffff;
11 double _fontSize = 18;
12
13 double _textWidth = 0;
14 double _textHeight = 0;
15 Timer _animator = nilptr;
16 Object _timeObj = new Object();
17 int _angle = 0;
18
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;
29
30 public ProgressView(){}
31 public ProgressView(QWidget parent){_parent_widget = parent;}
32 public interface OnDismissListener {
33 void onDismissListener(ProgressView v);
34 };
35
36 public void setOnDismissListener(OnDismissListener _dismissListener) {
37 __dismissListener = _dismissListener;
38 }
39
40 public int dip2px(double dpValue) {
41 return (int) (dpValue * _scale + 0.5f);
42 }
43
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;
50
51 //paint.setAntiAlias(true);
52 paint.setColor(0xaf000000);
53 paint.setStyle(QPainter.Paint.FILL);
54 //paint.setShadowLayer(dip2px(5), 4, 4, Color.BLACK);
55 double rxy = Math.min(width(), height()) / 12.f;
56
57 canvas.drawRoundedRect(rect, (int)rxy, (int)rxy, paint);
58 }
59
60
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;
65 subCenterX -= height;
66 subCenterY -= height;
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;
72
73 paint.setColor(0xffffffff);
74
75 paint.setStyle(QPainter.Paint.FILL);
76 canvas.save();
77
78 double roundXY = subheight / 2;
79
80 canvas.rotate(90.f,subCenterX,subCenterY);
81 canvas.rotate(45.f,subCenterX + (width / 2.f),subCenterY );
82
83 canvas.drawRoundedRect(progsub, (int)roundXY,(int)roundXY, paint);
84
85 canvas.restore();
86 canvas.save();
87 canvas.translate(1,width / 2);
88 canvas.rotate(45,subCenterX,subCenterY);
89 progsub.right = progsub.left + (progsub.width() / 2.f) + (height / 2.f);
90
91 canvas.drawRoundedRect(progsub, (int)roundXY,(int)roundXY, paint);
92 canvas.restore();
93 }
94
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;
99
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;
105
106 paint.setColor(0xffffffff);
107 paint.setStyle(QPainter.Paint.FILL);
108 canvas.save();
109
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);
115 }
116 canvas.restore();
117 }
118
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();
122
123 if (_bShowProgress) {
124 subCenterY = Math.max((double)minCenterY,height() / 3.f);
125 }
126
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;
131
132 paint.setColor(0xbfffffff);
133 paint.setStyle(QPainter.Paint.FILL);
134 canvas.save();
135
136 double roundXY = subheight / 2;
137
138 for (int i = 0 ; i < 12; i ++) {
139 canvas.rotate((360 / 12),subCenterX,subCenterY);
140 int dis = _angle - (i * (360 / 12));
141 if (dis < 0) {
142 dis += 360;
143 }
144 paint.setAlpha(255 * (360 - dis) / 360);
145 canvas.drawRoundedRect(progsub, (int)roundXY, (int)roundXY, paint);
146 }
147 canvas.restore();
148 }
149
150 public void calcTitlePosition(@NotNilptr QPainter canvas,double padding,@NotNilptr QPainter.Paint paint) {
151
152 paint.setTextSize(dip2px(_fontSize));
153 //paint.setFakeBoldText(true);
154 paint.setColor(_titleColor);
155 QFont qf = canvas.getFont();
156 if (qf != nilptr){
157 //QXReqf.measure(_titleText);
158 QRect rect = qf.measure(_titleText);//BitmapUtils.getStringRect(_titleText,paint);
159 _textHeight= rect.height();
160 _textWidth = rect.width();
161
162 paint.setStyle(QPainter.Paint.FILL);
163 paint.setAlpha(0xff);
164 }
165 }
166
167 public void drawProgress(@NotNilptr QPainter canvas,double padding,
168 @NotNilptr QPainter.Paint paint,double progressX,double progressY,
169 double progressCX,double progressCY) {
170
171 QRect progressRect = new QRect();
172 progressRect.left = progressX;
173 progressRect.top = progressY;
174 progressRect.right = progressX + progressCX;
175 progressRect.bottom = progressY + progressCY;
176
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);
181
182 paint.setStyle(QPainter.Paint.FILL);
183 progressRect.right = 2 + progressRect.left + (_progressPersent * progressCX );
184 canvas.drawRoundedRect(progressRect, (int)prxy, (int)prxy, paint);
185 }
186
187
188
189 public void onDraw(@NotNilptr QPainter canvas) {
190
191 canvas.setPen(0, PenStyle.NoPen, 0);
192
193 double padding = dip2px(5);
194 QPainter.Paint paint = new QPainter.Paint();
195
196 //paint.setShadowLayer(0,0,0, Color.BLACK);
197 onDrawBackground(canvas,padding,paint);
198
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);
204 if (_bShowResult) {
205 if (_bResult) {
206 DrawComplete(canvas,padding,paint,subCenterX,subCenterY,minCenterY,subwidth,subheight);
207 } else {
208 DrawFatal(canvas,padding,paint,subCenterX,subCenterY,minCenterY,subwidth,subheight);
209 }
210 } else {
211 DrawWaiting(canvas,padding,paint,subCenterX,subCenterY,minCenterY,subwidth,subheight);
212 }
213
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);
217
218
219 if (_bShowProgress) {
220 textY = subCenterY + (subwidth * 1.75f) + (_textHeight * 1.2f) ;
221 }
222 canvas.drawText(_titleText, (int)textX, (int)(textY + (_textHeight * 0.8f)), paint);
223
224 paint.setColor(0xbfffffff);
225
226 if (_bShowProgress) {
227 double progressCX = (width() - padding) * 2 / 3;
228 double progressCY = progressCX / 12.f;
229 if (progressCY == 0) {
230 progressCY = dip2px(1);
231 }
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);
235 }
236 }
237
238 public void beginAnimation(long millionSecond) {
239 synchronized(_timeObj) {
240 if (_animator != nilptr) {
241 return ;
242 }
243 _dismissMillionSecond = millionSecond;
244 _animator = new Timer();
245 try{
246 _animator.schedule(new TimerTask() {
247 long beginTime = _system_.currentTimeMillis();
248 long waitTime = millionSecond;
249
250 void run() override{
251 _angle += 20;
252 _angle = (_angle) % 360;
253
254 synchronized(_timeObj) {
255 if (_animator != nilptr){
256 ProgressView.this.postUpdate();
257 if (waitTime != WAIT_INFINITE) {
258 if (_system_.currentTimeMillis() - beginTime > waitTime) {
259 _animator.cancel();
260 _animator = nilptr;
261 runOnUi(new Runnable() {
262 void run() override{
263 _dismiss();
264 }
265 }/*, __file__ + ":" + __line__*/);
266 }
267 } else if (_dismissMillionSecond != waitTime) {
268 waitTime = _dismissMillionSecond;
269 beginTime = _beginMillionSecond;
270 }
271 }
272 }
273 }
274 }, 50,-1);
275 }catch(Exception e){
276
277 }
278
279 }
280 }
281
282
283 public void showModal(long millionSecond, bool bModal) {
284 setPalette(ColorRole.Background, 0x7fffffff);
285 setOpacity(1);
286 setWindowFlags(WindowType.FramelessWindowHint | WindowType.WindowStaysOnTopHint | WindowType.Tool);
287 setAttribute(Constant.WA_TranslucentBackground, true);
288
289 double width = 150;// dm.widthPixels / WIDTH_RATE;//宽度
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);
294 }else{
295 centerScreen();
296 }
297 beginAnimation(millionSecond);
298 if (bModal) {
299 setModal(true);
300 }
301 show();
302 }
303
304 public static ProgressView showWait(long millionSecond) {
305 ProgressView _progView = new ProgressView();
306 if (_progView.create()) {
307 _progView.showModal(millionSecond, false);
308 return _progView;
309 }
310 return nilptr;
311 }
312
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);
317 return _progView;
318 }
319 return nilptr;
320 }
321
323 return _titleText;
324 }
325
326 public void setTitleText(String szTitle) {
327 _titleText = szTitle;
328 }
329
330 public void setTitleColor(int nColor) {
331 _titleColor = nColor;
332 }
333
334 public void setTitleFontSize(int sp) {
335 _fontSize = sp;
336 }
337
338 public void setTitle(String szTitle,int nColor,int sp) {
339 _titleText = szTitle;
340 _titleColor = nColor;
341 _fontSize = sp;
342 }
343
344 public void showResult(bool bShowResult,bool bSuccess) {
345 _bShowResult = bShowResult;
346 _bResult = bSuccess;
347 }
348
349 public void EnableProgress(bool bEnable,double persent) {
350 _bShowProgress = bEnable;
351
352 _progressPersent = Math.max(0.f, persent);
353 _progressPersent = Math.min(1.f, _progressPersent);
354
355 }
356
357 public void setProgress(double persent) {
358 if (persent >= 0 && persent <= 1) {
359 _progressPersent = persent;
360 }
361 }
362
363 public void dismissAfter(int millionSecond) {
364 _beginMillionSecond = _system_.currentTimeMillis();
365 _dismissMillionSecond = millionSecond;
366 }
367
368 public void dismiss() {
369 dismissAfter(0);
370 }
371
372 public void dismiss(OnDismissListener _l) {
373 __dismissListener = _l;
374 dismissAfter(0);
375 }
376
377 private void _dismiss() {
378 double opacity = 1.f;
379 try{
380 new Timer().schedule(new TimerTask() {
381 bool closed = false;
382 void run()override {
383 runOnUi(new Runnable() {
384 void run() override{
385 opacity -= 0.1;
386 if (closed == false){
387 setOpacity(opacity);
388 if (opacity < 0.1) {
389 closed = true;
390 close();
391 }
392 }
393 }
394 }/*, __file__ + ":" + __line__*/);
395 }
396 }, 20, 10);
397 }catch(Exception e){
398
399 }
400 }
401
402 public void onPaint(int l, int t, int r, int b, long hpaint) override {
403 onDraw(new QPainter(hpaint));
404 }
405
406 public bool onClose()override {
407 if (__dismissListener != nilptr) {
408 __dismissListener.onDismissListener(ProgressView.this);
409 }
410 return true;
411 }
412 };
413};
系统和IO相关
static final long currentTimeMillis()
异常类
Math类
static final int min(int, int)
static final int max(int, int)
void dismissAfter(int millionSecond)
void EnableProgress(bool bEnable,double persent)
String getTitleText()
bool onClose() override
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)
字符串类
定时器类
void cancel()
bool schedule(TimerTask, int)
定时器任务类
void onDismissListener(ProgressView v)