1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00
qTox/src/chatlog/content/filetransferwidget.cpp

232 lines
6.4 KiB
C++
Raw Normal View History

2014-11-16 19:58:43 +08:00
/*
Copyright (C) 2014 by Project Tox <https://tox.im>
This file is part of qTox, a Qt-based graphical interface for Tox.
This program is libre software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the COPYING file for more details.
*/
2014-11-12 21:11:25 +08:00
#include "filetransferwidget.h"
#include "ui_filetransferwidget.h"
2014-11-17 03:01:37 +08:00
#include "src/core.h"
2014-11-17 23:05:14 +08:00
#include "src/misc/style.h"
2014-11-17 03:01:37 +08:00
2014-11-12 21:11:25 +08:00
#include <QMouseEvent>
#include <QDebug>
2014-11-17 03:01:37 +08:00
FileTransferWidget::FileTransferWidget(QWidget *parent, ToxFile file)
: QWidget(parent)
, ui(new Ui::FileTransferWidget)
, fileInfo(file)
2014-11-17 23:05:14 +08:00
, lastTick(QTime::currentTime())
2014-11-12 21:11:25 +08:00
{
ui->setupUi(this);
2014-11-17 03:01:37 +08:00
ui->filenameLabel->setText(file.fileName);
ui->progressBar->setValue(0);
2014-11-17 23:05:14 +08:00
ui->fileSizeLabel->setText(getHumanReadableSize(file.filesize));
ui->progressLabel->setText("0%");
ui->etaLabel->setText("--:--");
setStyleSheet(Style::getStylesheet(":/ui/fileTransferInstance/grey.css"));
Style::repolish(this);
2014-11-17 03:01:37 +08:00
connect(Core::getInstance(), &Core::fileTransferInfo, this, &FileTransferWidget::onFileTransferInfo);
2014-11-17 23:05:14 +08:00
connect(Core::getInstance(), &Core::fileTransferAccepted, this, &FileTransferWidget::onFileTransferAccepted);
connect(Core::getInstance(), &Core::fileTransferCancelled, this, &FileTransferWidget::onFileTransferCancelled);
connect(Core::getInstance(), &Core::fileTransferPaused, this, &FileTransferWidget::onFileTransferPaused);
connect(Core::getInstance(), &Core::fileTransferFinished, this, &FileTransferWidget::onFileTransferFinished);
setupButtons();
2014-11-17 03:01:37 +08:00
2014-11-17 23:05:14 +08:00
setFixedHeight(85);
2014-11-12 21:11:25 +08:00
}
FileTransferWidget::~FileTransferWidget()
{
delete ui;
}
2014-11-17 23:05:14 +08:00
void FileTransferWidget::onFileTransferInfo(ToxFile file)
2014-11-12 21:11:25 +08:00
{
2014-11-17 23:05:14 +08:00
if(fileInfo != file)
2014-11-17 03:01:37 +08:00
return;
2014-11-12 21:11:25 +08:00
2014-11-17 23:05:14 +08:00
fileInfo = file;
2014-11-17 03:01:37 +08:00
// update progress
2014-11-17 23:05:14 +08:00
qreal progress = static_cast<qreal>(file.bytesSent) / static_cast<qreal>(file.filesize);
2014-11-17 03:01:37 +08:00
ui->progressBar->setValue(static_cast<int>(progress * 100.0));
2014-11-17 23:05:14 +08:00
// eta, speed
QTime now = QTime::currentTime();
qreal deltaSecs = lastTick.msecsTo(now) / 1000.0;
if(deltaSecs >= 1.0)
{
qint64 deltaBytes = file.bytesSent - lastBytesSent;
qint64 bytesPerSec = static_cast<int>(static_cast<qreal>(deltaBytes) / deltaSecs);
if(bytesPerSec > 0)
{
QTime toGo(0,0,file.filesize / bytesPerSec);
ui->etaLabel->setText(toGo.toString("mm:ss"));
}
else
{
ui->etaLabel->setText("--:--");
}
ui->progressLabel->setText(getHumanReadableSize(bytesPerSec) + "/s");
lastTick = now;
lastBytesSent = file.bytesSent;
}
setupButtons();
repaint();
}
void FileTransferWidget::onFileTransferAccepted(ToxFile file)
{
if(fileInfo != file)
return;
fileInfo = file;
setStyleSheet(Style::getStylesheet(":/ui/fileTransferInstance/yellow.css"));
Style::repolish(this);
setupButtons();
}
void FileTransferWidget::onFileTransferCancelled(ToxFile file)
{
if(fileInfo != file)
return;
fileInfo = file;
setStyleSheet(Style::getStylesheet(":/ui/fileTransferInstance/red.css"));
Style::repolish(this);
setupButtons();
hideWidgets();
disconnect(Core::getInstance());
}
void FileTransferWidget::onFileTransferPaused(ToxFile file)
{
if(fileInfo != file)
return;
fileInfo = file;
setStyleSheet(Style::getStylesheet(":/ui/fileTransferInstance/grey.css"));
Style::repolish(this);
setupButtons();
}
void FileTransferWidget::onFileTransferFinished(ToxFile file)
{
if(fileInfo != file)
return;
fileInfo = file;
setStyleSheet(Style::getStylesheet(":/ui/fileTransferInstance/green.css"));
Style::repolish(this);
setupButtons();
hideWidgets();
disconnect(Core::getInstance());
}
QString FileTransferWidget::getHumanReadableSize(qint64 size)
{
static const char* suffix[] = {"B","kiB","MiB","GiB","TiB"};
int exp = 0;
if (size > 0)
exp = std::min( (int) (log(size) / log(1024)), (int) (sizeof(suffix) / sizeof(suffix[0]) - 1));
return QString().setNum(size / pow(1024, exp),'f',2).append(suffix[exp]);
}
void FileTransferWidget::hideWidgets()
{
ui->buttonWidget->hide();
ui->progressBar->hide();
ui->progressLabel->hide();
ui->etaLabel->hide();
}
void FileTransferWidget::setupButtons()
{
switch(fileInfo.status)
{
case ToxFile::TRANSMITTING:
ui->topButton->setIcon(QIcon(":/ui/fileTransferInstance/no_2x.png"));
ui->topButton->setObjectName("cancel");
ui->bottomButton->setIcon(QIcon(":/ui/fileTransferInstance/pause_2x.png"));
ui->bottomButton->setObjectName("pause");
break;
case ToxFile::PAUSED:
ui->topButton->setIcon(QIcon(":/ui/fileTransferInstance/no_2x.png"));
ui->topButton->setObjectName("cancel");
ui->bottomButton->setIcon(QIcon(":/ui/fileTransferInstance/arrow_white_2x.png"));
ui->bottomButton->setObjectName("resume");
break;
case ToxFile::STOPPED:
case ToxFile::BROKEN: //TODO: ?
break;
}
}
void FileTransferWidget::handleButton(QPushButton *btn)
{
if(fileInfo.direction == ToxFile::SENDING)
{
if(btn->objectName() == "cancel")
Core::getInstance()->cancelFileSend(fileInfo.friendId, fileInfo.fileNum);
if(btn->objectName() == "pause")
Core::getInstance()->pauseResumeFileSend(fileInfo.friendId, fileInfo.fileNum);
if(btn->objectName() == "resume")
Core::getInstance()->pauseResumeFileSend(fileInfo.friendId, fileInfo.fileNum);
}
else
{
if(btn->objectName() == "cancel")
Core::getInstance()->cancelFileRecv(fileInfo.friendId, fileInfo.fileNum);
if(btn->objectName() == "pause")
Core::getInstance()->pauseResumeFileRecv(fileInfo.friendId, fileInfo.fileNum);
if(btn->objectName() == "resume")
Core::getInstance()->pauseResumeFileRecv(fileInfo.friendId, fileInfo.fileNum);
}
}
void FileTransferWidget::on_topButton_clicked()
{
handleButton(ui->topButton);
}
void FileTransferWidget::on_bottomButton_clicked()
{
handleButton(ui->bottomButton);
2014-11-12 21:11:25 +08:00
}