1 /*
2     This file is part of Android File Transfer For Linux.
3     Copyright (C) 2015-2020  Vladimir Menshakov
4 
5     This library is free software; you can redistribute it and/or modify it
6     under the terms of the GNU Lesser General Public License as published by
7     the Free Software Foundation; either version 2.1 of the License,
8     or (at your option) any later version.
9 
10     This library is distributed in the hope that it will be useful, but
11     WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13     Lesser General Public License for more details.
14 
15     You should have received a copy of the GNU Lesser General Public License
16     along with this library; if not, write to the Free Software Foundation,
17     Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 
20 #include "progressdialog.h"
21 #include "ui_progressdialog.h"
22 #include <QCloseEvent>
23 #include <QPropertyAnimation>
24 #include <QPushButton>
25 #include <QDebug>
26 
ProgressDialog(QWidget * parent,bool showAbort)27 ProgressDialog::ProgressDialog(QWidget *parent, bool showAbort) :
28 	QDialog(parent),
29 	ui(new Ui::ProgressDialog), _progress(0), _duration(0)
30 {
31 	ui->setupUi(this);
32 	ui->progressBar->setMaximum(10000);
33 
34 	_animation = new QPropertyAnimation(this, "progress", this);
35 	_animation->setDuration(30000);
36 	_animation->setStartValue(0);
37 	_animation->setEndValue(1);
38 	_animation->start();
39 
40 	if (showAbort)
41 	{
42 		QPushButton *b = ui->buttonBox->button(QDialogButtonBox::Abort);
43 		Q_ASSERT(b);
44 		connect(b, SIGNAL(clicked(bool)), SLOT(onAbortButtonPressed()));
45 	}
46 	else
47 		ui->buttonBox->clear();
48 }
49 
~ProgressDialog()50 ProgressDialog::~ProgressDialog()
51 {
52 	delete ui;
53 }
54 
reject()55 void ProgressDialog::reject()
56 { }
57 
onAbortButtonPressed()58 void ProgressDialog::onAbortButtonPressed()
59 {
60 	QPushButton *b = ui->buttonBox->button(QDialogButtonBox::Abort);
61 	Q_ASSERT(b);
62 	b->setEnabled(false);
63 	emit abort();
64 }
65 
setProgress(float current)66 void ProgressDialog::setProgress(float current)
67 {
68 	if (current < 0)
69 		current = 0;
70 	if (current > 1)
71 		current = 1;
72 	ui->progressBar->setValue(current * 10000);
73 }
74 
setValue(float current)75 void ProgressDialog::setValue(float current)
76 {
77 	//qDebug() << "setValue " << current;
78 	_duration += _animation->currentTime();
79 	float currentAnimated = _animation->currentValue().toFloat();
80 	if (_duration <= 0)
81 		return;
82 
83 	float currentSpeed = current * 1000 / _duration;
84 	if (currentSpeed <= 0)
85 		return;
86 
87 	int estimate = 1000 / currentSpeed;
88 	int duration = estimate - _duration;
89 	if (duration < 100)
90 		duration = 100;
91 	//qDebug() << current << currentSpeed << estimate;
92 	_animation->stop();
93 	_animation->setStartValue(currentAnimated);
94 	_animation->setDuration(duration);
95 	_animation->start();
96 }
97 
setSpeed(qint64 speed)98 void ProgressDialog::setSpeed(qint64 speed)
99 {
100 	static constexpr double kB = 1000;
101 	static constexpr double MB = 1000 * kB; // k, M, G are metric prefixes
102 	static constexpr double GB = 1000 * MB; // 1024 is for binary prefixes
103 	if (speed < 2 * MB)
104 		ui->speedLabel->setText(tr("Speed: %1 kB/s").arg(speed / kB, 0, 'f', 1));
105 	else if (speed < 2 * GB)
106 		ui->speedLabel->setText(tr("Speed: %1 MB/s").arg(speed / MB, 0, 'f', 1));
107 	else
108 		ui->speedLabel->setText(tr("Speed: %1 GB/s").arg(speed / GB, 0, 'f', 1));
109 }
110 
setFilename(const QString & filename)111 void ProgressDialog::setFilename(const QString &filename)
112 {
113 	ui->fileLabel->setText(filename);
114 }
115 
closeEvent(QCloseEvent * event)116 void ProgressDialog::closeEvent(QCloseEvent *event)
117 {
118 	event->ignore();
119 }
120