1 /***************************************************************************
2 copyright : (C) 2001 by mean
3 email : fixounet@free.fr
4 ***************************************************************************/
5
6 /***************************************************************************
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 ***************************************************************************/
14 #include <math.h>
15
16 #include "Q_working.h"
17 #include "ADM_default.h"
18 #include "ADM_vidMisc.h"
19 #include "DIA_working.h"
20 #include "ADM_toolkitQt.h"
21 #include "DIA_coreToolkit.h"
22
workWindow(QWidget * parent)23 workWindow::workWindow(QWidget *parent) : QDialog(parent)
24 {
25 ui=new Ui_workingDialog();
26 ui->setupUi(this);
27 active=true;
28 setWindowModality(Qt::ApplicationModal);
29 #if QT_VERSION < QT_VERSION_CHECK(5,0,0)
30 connect(ui->buttonBox,SIGNAL(rejected()),this,SLOT(reject()));
31 #else
32 connect(ui->buttonBox,&QDialogButtonBox::rejected,this,&QDialog::reject);
33 #endif
34 }
35
reject(void)36 void workWindow::reject(void)
37 {
38 ADM_info("Stop Request\n");
39 active=false;
40 }
41 //*******************************************
42
43 namespace ADM_Qt4CoreUIToolkit
44 {
45 /**
46 * \class DIA_workingQt4
47 */
48 class DIA_workingQt4 : public DIA_workingBase
49 {
50
51 protected:
52 virtual void postCtor( void ) ;
53 public:
54 DIA_workingQt4( const char *title=NULL );
55 virtual ~DIA_workingQt4();
56
57 virtual uint8_t update(uint32_t percent); // If returns 1 -> Means aborted
58 virtual uint8_t update(uint32_t current,uint32_t total); // If returns 1 -> Means aborted
59 virtual uint8_t isAlive (void );
60 void closeDialog(void);
61
62 };
63 /**
64 *
65 * @param title
66 */
DIA_workingQt4(const char * title)67 DIA_workingQt4::DIA_workingQt4(const char *title) : DIA_workingBase(title)
68 {
69 workWindow *wind = new workWindow(qtLastRegisteredDialog());
70 qtRegisterDialog(wind);
71 _priv=(void *)wind;
72 wind->setWindowTitle(QString::fromUtf8(title));
73 postCtor();
74 }
75 /**
76 *
77 */
postCtor(void)78 void DIA_workingQt4 :: postCtor( void )
79 {
80 workWindow *wind=(workWindow *)_priv;
81 ADM_assert(wind);
82 wind->show();
83 lastper=0;
84 _nextUpdate=0;
85 }
86 /**
87 *
88 * @param percent
89 * @return
90 */
update(uint32_t percent)91 uint8_t DIA_workingQt4::update(uint32_t percent)
92 {
93 #define GUI_UPDATE_RATE 1000
94
95 UI_purge();
96 if(!_priv)
97 return 1;
98 workWindow *wind=(workWindow *)_priv;
99 ADM_assert(wind);
100 if(!wind->active)
101 {
102 return true;
103 }
104 if(!percent)
105 return 0;
106 if(percent==lastper)
107 {
108 return 0;
109 }
110
111 elapsed=_clock.getElapsedMS();
112 if(elapsed<_nextUpdate)
113 {
114 return 0;
115 }
116
117 _nextUpdate=elapsed+1000;
118 lastper=percent;
119
120 uint32_t hh,mm,ss,mms;
121 char string[32]; // keep margin
122
123 ms2time(elapsed,&hh,&mm,&ss,&mms);
124 sprintf(string,"%02d:%02d:%02d",hh,mm,ss);
125
126
127
128 if(percent>=1)
129 {
130 double totalTime=(100*elapsed)/percent;
131 double remaining=totalTime-elapsed;
132 if(remaining<0)
133 remaining=0;
134 uint32_t remainingMs=(uint32_t)remaining;
135 wind->ui->labelTimeLeft->setText(ms2timedisplay(remainingMs));
136 }
137
138
139 wind->ui->labelElapsed->setText(string);
140 wind->ui->progressBar->setValue(percent);
141
142 return 0;
143 }
144 /**
145 *
146 * @param cur
147 * @param total
148 * @return
149 */
update(uint32_t cur,uint32_t total)150 uint8_t DIA_workingQt4::update(uint32_t cur, uint32_t total)
151 {
152 double d,n;
153 uint32_t percent;
154 UI_purge();
155 if(!_priv) return 1;
156 if(!total) return 0;
157
158 d=total;
159 n=cur;
160 n=n*100.;
161
162 n=n/d;
163
164 percent=(uint32_t )floor(n);
165 if(percent>100) percent=100;
166 return update(percent);
167 }
168 /**
169 *
170 * @return
171 */
isAlive(void)172 uint8_t DIA_workingQt4::isAlive (void )
173 {
174 if(!_priv) return 0;
175 workWindow *wind=(workWindow *)_priv;
176 ADM_assert(wind);
177 return wind->active;
178 }
179 /**
180 *
181 */
~DIA_workingQt4()182 DIA_workingQt4::~DIA_workingQt4()
183 {
184 closeDialog();
185 }
186 /**
187 *
188 */
closeDialog(void)189 void DIA_workingQt4::closeDialog(void)
190 {
191 workWindow *wind = (workWindow*)_priv;
192 ADM_assert(wind);
193
194 if (wind)
195 {
196 qtUnregisterDialog(wind);
197 delete wind;
198 }
199
200 wind = NULL;
201 _priv = NULL;
202 }
203
204 /**
205 \fn createWorking
206 */
createWorking(const char * title)207 DIA_workingBase *createWorking(const char *title)
208 {
209 return new DIA_workingQt4(title);
210 }
211
212
213 }
214 //********************************************
215 //EOF
216