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 #include "ADM_inttype.h"
16 #include <QtCore/QEvent>
17 #include <QtGui/QCloseEvent>
18 #include "Q_encoding.h"
19 
20 
21 #include "prefs.h"
22 #include "DIA_encoding.h"
23 #include "DIA_coreToolkit.h"
24 #include "ADM_vidMisc.h"
25 #include "ADM_misc.h"
26 #include "ADM_toolkitQt.h"
27 #include "GUI_ui.h"
28 #include "ADM_coreUtils.h"
29 #include "ADM_prettyPrint.h"
30 #include "../ADM_gui/ADM_systemTrayProgress.h" // this is Qt only
31 
32 //*******************************************
33 #define WIDGET(x) (ui->x)
34 #define WRITEM(x,y) ui->x->setText(QString::fromUtf8(y))
35 #define WRITE(x) WRITEM(x,stringMe)
36 /*************************************/
37 
38 extern bool ADM_slaveReportProgress(uint32_t percent);
39 
40 
41 
useTrayButtonPressed(void)42 void DIA_encodingQt4::useTrayButtonPressed(void)
43 {
44     hide();
45     UI_iconify();
46     if(!tray)
47         tray=DIA_createTray(this);
48 }
49 
pauseButtonPressed(void)50 void DIA_encodingQt4::pauseButtonPressed(void)
51 {
52 	ADM_info("StopReq\n");
53 	stopRequest=true;
54 }
55 
priorityChanged(int priorityLevel)56 void DIA_encodingQt4::priorityChanged(int priorityLevel)
57 {
58 #ifndef _WIN32
59 	if (getuid() != 0)
60 	{
61 		ui->comboBoxPriority->disconnect(SIGNAL(currentIndexChanged(int)));
62 		ui->comboBoxPriority->setCurrentIndex(2);
63 		connect(ui->checkBoxShutdown, SIGNAL(currentIndexChanged(int)), this, SLOT(priorityChanged(int)));
64 
65 		GUI_Error_HIG(QT_TRANSLATE_NOOP("qencoding","Privileges Required"), QT_TRANSLATE_NOOP("qencoding","Root privileges are required to perform this operation."));
66 
67 		return;
68 	}
69 #endif
70 
71 	#ifndef __HAIKU__
72 	setpriority(PRIO_PROCESS, 0, ADM_getNiceValue(priorityLevel));
73 	#endif
74 }
75 
shutdownChanged(int state)76 void DIA_encodingQt4::shutdownChanged(int state)
77 {
78 #ifndef _WIN32
79 	if (getuid() != 0)
80 	{
81 		ui->checkBoxShutdown->disconnect(SIGNAL(stateChanged(int)));
82 		ui->checkBoxShutdown->setCheckState(Qt::Unchecked);
83 		connect(ui->checkBoxShutdown, SIGNAL(stateChanged(int)), this, SLOT(shutdownChanged(int)));
84 
85 		GUI_Error_HIG(QT_TRANSLATE_NOOP("qencoding","Privileges Required"), QT_TRANSLATE_NOOP("qencoding","Root privileges are required to perform this operation."));
86 	}
87 #else
88 	if(state)
89 	{
90 		ui->checkBoxKeepOpen->disconnect(SIGNAL(stateChanged(int)));
91 		ui->checkBoxKeepOpen->setCheckState(Qt::Unchecked);
92 		stayOpen=false;
93 		connect(ui->checkBoxKeepOpen, SIGNAL(stateChanged(int)), this, SLOT(keepOpenChanged(int)));
94 	}
95 #endif
96 }
97 
keepOpenChanged(int state)98 void DIA_encodingQt4::keepOpenChanged(int state)
99 {
100     stayOpen=!!state;
101 #ifdef _WIN32
102     if(state)
103     {
104         ui->checkBoxShutdown->disconnect(SIGNAL(stateChanged(int)));
105         ui->checkBoxShutdown->setCheckState(Qt::Unchecked);
106         connect(ui->checkBoxShutdown, SIGNAL(stateChanged(int)), this, SLOT(shutdownChanged(int)));
107     }
108 #endif
109 }
110 
111 static char stringMe[80];
112 
DIA_encodingQt4(uint64_t duration)113 DIA_encodingQt4::DIA_encodingQt4(uint64_t duration) : DIA_encodingBase(duration)
114 {
115         stopRequest=false;
116         stayOpen=false;
117         UI_getTaskBarProgress()->enable();
118         ui=new Ui_encodingDialog;
119 	ui->setupUi(this);
120 
121 #ifndef _WIN32
122 	//check for root privileges
123 	if (getuid() == 0)
124 	{
125 		// set priority to normal, regardless of preferences
126 		ui->comboBoxPriority->setCurrentIndex(2);
127 
128 	}else
129         {
130             ui->comboBoxPriority->setVisible(false);
131             ui->labelPrio->setVisible(false);
132         }
133         ui->checkBoxShutdown->setVisible(false);
134 #endif
135 	connect(ui->checkBoxShutdown, SIGNAL(stateChanged(int)), this, SLOT(shutdownChanged(int)));
136 	connect(ui->checkBoxKeepOpen, SIGNAL(stateChanged(int)), this, SLOT(keepOpenChanged(int)));
137 	connect(ui->pushButton1, SIGNAL(pressed()), this, SLOT(useTrayButtonPressed()));
138 	connect(ui->pushButton2, SIGNAL(pressed()), this, SLOT(pauseButtonPressed()));
139 	connect(ui->comboBoxPriority, SIGNAL(currentIndexChanged(int)), this, SLOT(priorityChanged(int)));
140 
141 	// set priority
142 	uint32_t priority;
143 
144 	prefs->get(PRIORITY_ENCODING,&priority);
145 
146 #ifndef _WIN32
147 	// check for root privileges
148 	if (getuid() == 0)
149 	{
150 		ui->comboBoxPriority->setCurrentIndex(priority);
151 	}
152 #else
153 	ui->comboBoxPriority->setCurrentIndex(priority);
154 #endif
155 
156         setModal(true);
157         qtRegisterDialog(this);
158         show();
159         tray=NULL;
160 }
161 /**
162     \fn setFps(uint32_t fps)
163     \brief Memorize fps, it will be used later for bitrate computation
164 */
165 
setFps(uint32_t fps)166 void DIA_encodingQt4::setFps(uint32_t fps)
167 {
168       snprintf(stringMe,79,"%" PRIu32" fps",fps);
169       WRITE(labelFps);
170 }
171 
172 /**
173     \fn dtpor
174 */
~DIA_encodingQt4()175 DIA_encodingQt4::~DIA_encodingQt4( )
176 {
177     ADM_info("Destroying encoding qt4\n");
178     UI_getTaskBarProgress()->disable();
179     bool shutdownRequested = (ui->checkBoxShutdown->checkState() == Qt::Checked);
180     if(tray)
181     {
182         UI_deiconify();
183         delete tray;
184         tray=NULL;
185     }
186     if(ui)
187     {
188         qtUnregisterDialog(this);
189         delete ui;
190         ui=NULL;
191     }
192     if(shutdownRequested)
193     {
194         prefs->save(); // won't get another chance
195         ADM_info("Requesting shutdown...\n");
196         if(!ADM_shutdown())
197             ADM_warning("Shutdown request failed\n");
198     }
199 }
200 /**
201     \fn setPhasis(const char *n)
202     \brief Display parameters as phasis
203 */
204 
setPhasis(const char * n)205 void DIA_encodingQt4::setPhasis(const char *n)
206 {
207     ADM_assert(ui);
208     if(!strcmp(n,"Pass 1"))
209     {
210         ui->tabWidget->setTabEnabled(1, false); // disable the "Advanced" tab
211         this->setWindowTitle(QString::fromUtf8(QT_TRANSLATE_NOOP("qencoding","First Pass")));
212         WRITEM(labelPhasis,QT_TRANSLATE_NOOP("qencoding","Pass 1"));
213     }else
214     {
215         this->setWindowTitle(QString::fromUtf8(QT_TRANSLATE_NOOP("qencoding","Encoding...")));
216         ui->tabWidget->setTabEnabled(1, true);
217         WRITEM(labelPhasis,n);
218     }
219 }
220 /**
221     \fn    setFrameCount
222     \brief display the # of processed frames
223 */
224 
setFrameCount(uint32_t nb)225 void DIA_encodingQt4::setFrameCount(uint32_t nb)
226 {
227           ADM_assert(ui);
228           snprintf(stringMe,79,"%" PRIu32,nb);
229           WRITE(labelFrame);
230 
231 }
232 /**
233     \fn    setPercent
234     \brief display percent of saved file
235 */
236 
setPercent(uint32_t p)237 void DIA_encodingQt4::setPercent(uint32_t p)
238 {
239           ADM_assert(ui);
240           printf("Percent:%u\n",p);
241           WIDGET(progressBar)->setValue(p);
242           ADM_slaveReportProgress(p);
243           if(tray)
244                 tray->setPercent(p);
245           UI_getTaskBarProgress()->setProgress(p);
246           UI_purge();
247 }
248 /**
249     \fn setAudioCodec(const char *n)
250     \brief Display parameters as audio codec
251 */
252 
setAudioCodec(const char * n)253 void DIA_encodingQt4::setAudioCodec(const char *n)
254 {
255           ADM_assert(ui);
256           WRITEM(labelAudCodec,n);
257 }
258 /**
259     \fn setCodec(const char *n)
260     \brief Display parameters as video codec
261 */
262 
setVideoCodec(const char * n)263 void DIA_encodingQt4::setVideoCodec(const char *n)
264 {
265           ADM_assert(ui);
266           WRITEM(labelVidCodec,n);
267 }
268 /**
269     \fn setBitrate(uint32_t br,uint32_t globalbr)
270     \brief Display parameters as instantaneous bitrate and average bitrate
271 */
272 
setBitrate(uint32_t br,uint32_t globalbr)273 void DIA_encodingQt4::setBitrate(uint32_t br,uint32_t globalbr)
274 {
275           ADM_assert(ui);
276           snprintf(stringMe,79,"%" PRIu32" kB/s",br);
277           WRITE(labelVidBitrate);
278 
279 }
280 /**
281     \fn setContainer(const char *container)
282     \brief Display parameter as container field
283 */
284 
setContainer(const char * container)285 void DIA_encodingQt4::setContainer(const char *container)
286 {
287         ADM_assert(ui);
288         WRITEM(labelContainer,container);
289 }
290 
291 /**
292     \fn setQuantIn(int size)
293     \brief display parameter as quantizer
294 */
295 
setQuantIn(int size)296 void DIA_encodingQt4::setQuantIn(int size)
297 {
298           ADM_assert(ui);
299           sprintf(stringMe,"%" PRIu32,size);
300           WRITE(labelQz);
301 
302 }
303 /**
304     \fn setSize(int size)
305     \brief display parameter as total size
306 */
307 
setTotalSize(uint64_t size)308 void DIA_encodingQt4::setTotalSize(uint64_t size)
309 {
310           ADM_assert(ui);
311           uint64_t mb=size>>20;
312           sprintf(stringMe,"%" PRIu32" MB",(int)mb);
313           WRITE(labelTotalSize);
314 
315 }
316 
317 /**
318     \fn setVideoSize(int size)
319     \brief display parameter as total size
320 */
321 
setVideoSize(uint64_t size)322 void DIA_encodingQt4::setVideoSize(uint64_t size)
323 {
324           ADM_assert(ui);
325           uint64_t mb=size>>20;
326           sprintf(stringMe,"%" PRIu32" MB",(int)mb);
327           WRITE(labelVideoSize);
328 
329 }
330 /**
331     \fn setAudioSizeIn(int size)
332     \brief display parameter as audio size
333 */
334 
setAudioSize(uint64_t size)335 void DIA_encodingQt4::setAudioSize(uint64_t size)
336 {
337           ADM_assert(ui);
338           uint64_t mb=size>>20;
339           sprintf(stringMe,"%" PRIu32" MB",(int)mb);
340           WRITE(labelAudioSize);
341 
342 }
343 
344 /**
345     \fn setAudioSizeIn(int size)
346     \brief display elapsed time since saving start
347 */
setElapsedTimeMs(uint32_t nb)348 void DIA_encodingQt4::setElapsedTimeMs(uint32_t nb)
349 {
350           ADM_assert(ui);
351           uint64_t mb=nb;
352           mb*=1000;
353           strcpy(stringMe,ADM_us2plain(mb));
354           WRITE(labelElapsed);
355 }
356 /**
357     \fn setAverageQz(int size)
358     \brief display average quantizer used
359 */
360 
setAverageQz(uint32_t nb)361 void DIA_encodingQt4::setAverageQz(uint32_t nb)
362 {
363           ADM_assert(ui);
364           snprintf(stringMe,79,"%" PRIu32,nb);
365           WRITE(labelQz);
366 }
367 /**
368     \fn setAverageBitrateKbits(int size)
369     \brief display average bitrate in kb/s
370 */
371 
setAverageBitrateKbits(uint32_t kb)372 void DIA_encodingQt4::setAverageBitrateKbits(uint32_t kb)
373 {
374           ADM_assert(ui);
375           snprintf(stringMe,79,"%" PRIu32" kbits/s",kb);
376           WRITE(labelVidBitrate);
377 }
378 
379 /**
380     \fn setRemainingTimeMS
381     \brief display remaining time (ETA)
382 */
setRemainingTimeMS(uint32_t milliseconds)383 void DIA_encodingQt4::setRemainingTimeMS(uint32_t milliseconds)
384 {
385           ADM_assert(ui);
386           std::string s;
387           ADM_durationToString(milliseconds,s);
388           ui->labelETA->setText(QString::fromUtf8(s.c_str()));
389 }
390 
391 /**
392     \fn isAlive( void )
393     \brief return 0 if the window was killed or cancel button pressed, 1 otherwise
394 */
isAlive(void)395 bool DIA_encodingQt4::isAlive( void )
396 {
397     if(!stopRequest)
398         return true;
399 
400     if(GUI_Alternate((char*)QT_TRANSLATE_NOOP("qencoding","The encoding is paused. Do you want to resume or abort?"),
401                      (char*)QT_TRANSLATE_NOOP("qencoding","Resume"),(char*)QT_TRANSLATE_NOOP("qencoding","Abort")))
402     {
403         stopRequest=false;
404         return true;
405     }
406     ui->checkBoxShutdown->setCheckState(Qt::Unchecked);
407     return false;
408 }
409 
410 /**
411     \fn keepOpen
412     \brief Allow user to read the muxing stats calmly
413 */
keepOpen(void)414 void DIA_encodingQt4::keepOpen(void)
415 {
416     if(stayOpen)
417     {
418         stopRequest=true;
419         UI_getTaskBarProgress()->disable();
420         if(tray)
421         {
422             UI_deiconify();
423             delete tray;
424             tray=NULL;
425         }
426         ui->checkBoxShutdown->setCheckState(Qt::Unchecked);
427         ui->checkBoxShutdown->setEnabled(false);
428         ui->pushButton1->setEnabled(false);
429         ui->pushButton2->setEnabled(false);
430         ui->comboBoxPriority->setEnabled(false);
431         show();
432         while(stayOpen)
433         {
434             ADM_usleep(100*1000);
435             QCoreApplication::processEvents();
436         }
437     }
438 }
439 
440 /**
441  * \fn closeEvent
442  * @param event
443  */
closeEvent(QCloseEvent * event)444 void DIA_encodingQt4::closeEvent(QCloseEvent *event)
445 {
446     if(stopRequest && stayOpen)
447     {
448         stayOpen=false;
449         return;
450     }
451     stopRequest=true;
452     event->ignore(); // keep window
453 }
454 
455 /**
456  * \fn reject
457  * \brief Prevent dialog from being closed by pressing the Esc key
458  */
reject(void)459 void DIA_encodingQt4::reject(void)
460 {
461     if(stopRequest && stayOpen) // close dialog when done
462     {
463         stayOpen=false;
464         return;
465     }
466     stopRequest=true;
467 }
468 
469 /**
470         \fn createEncoding
471 */
472 namespace ADM_Qt4CoreUIToolkit
473 {
createEncoding(uint64_t duration)474 DIA_encodingBase *createEncoding(uint64_t duration)
475 {
476         return new DIA_encodingQt4(duration);
477 }
478 }
479 //********************************************
480 //EOF
481