Simplify code

This commit is contained in:
Chocobo1 2019-08-07 14:26:36 +08:00
parent c61116882b
commit 061219d0a2
No known key found for this signature in database
GPG key ID: 210D9C873253A68C
3 changed files with 63 additions and 78 deletions

View file

@ -54,16 +54,18 @@ TransferListDelegate::TransferListDelegate(QObject *parent)
void TransferListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
painter->save();
bool isHideState = true;
if (Preferences::instance()->getHideZeroComboValues() == 1) { // paused torrents only
QModelIndex stateIndex = index.sibling(index.row(), TransferListModel::TR_STATUS);
const QModelIndex stateIndex = index.sibling(index.row(), TransferListModel::TR_STATUS);
if (stateIndex.data().value<BitTorrent::TorrentState>() != BitTorrent::TorrentState::PausedDownloading)
isHideState = false;
}
const bool hideValues = Preferences::instance()->getHideZeroValues() & isHideState;
const bool hideValues = Preferences::instance()->getHideZeroValues() && isHideState;
QStyleOptionViewItem opt = QItemDelegate::setOptions(index, option);
QItemDelegate::drawBackground(painter, opt, index);
switch (index.column()) {
case TransferListModel::TR_AMOUNT_DOWNLOADED:
case TransferListModel::TR_AMOUNT_UPLOADED:
@ -81,10 +83,10 @@ void TransferListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &
}
break;
case TransferListModel::TR_ETA: {
opt.displayAlignment = Qt::AlignRight | Qt::AlignVCenter;
QItemDelegate::drawDisplay(painter, opt, option.rect, Utils::Misc::userFriendlyDuration(index.data().toLongLong(), MAX_ETA));
opt.displayAlignment = Qt::AlignRight | Qt::AlignVCenter;
QItemDelegate::drawDisplay(painter, opt, option.rect, Utils::Misc::userFriendlyDuration(index.data().toLongLong(), MAX_ETA));
}
break;
}
case TransferListModel::TR_SEEDS:
case TransferListModel::TR_PEERS: {
qlonglong value = index.data().toLongLong();
@ -148,30 +150,32 @@ void TransferListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &
case TransferListModel::TR_QUEUE_POSITION: {
const int queuePos = index.data().toInt();
opt.displayAlignment = Qt::AlignRight | Qt::AlignVCenter;
if (queuePos > 0) {
if (queuePos > 0)
QItemDelegate::paint(painter, opt, index);
}
else {
else
QItemDelegate::drawDisplay(painter, opt, opt.rect, "*");
}
}
break;
case TransferListModel::TR_PROGRESS: {
const qreal progress = index.data().toDouble() * 100;
QStyleOptionProgressBar newopt;
qreal progress = index.data().toDouble() * 100.;
newopt.rect = opt.rect;
newopt.text = ((progress == 100.0) ? QString("100%") : Utils::String::fromDouble(progress, 1) + '%');
newopt.text = ((progress == 100)
? QString("100%")
: (Utils::String::fromDouble(progress, 1) + '%'));
newopt.progress = static_cast<int>(progress);
newopt.maximum = 100;
newopt.minimum = 0;
newopt.state |= QStyle::State_Enabled;
newopt.textVisible = true;
#if !defined(Q_OS_WIN) && !defined(Q_OS_MACOS)
QApplication::style()->drawControl(QStyle::CE_ProgressBar, &newopt, painter);
#else
// XXX: To avoid having the progress text on the right of the bar
#if defined(Q_OS_WIN) || defined(Q_OS_MACOS)
// XXX: To avoid having the progress text on the right of the bar
QProxyStyle st("fusion");
st.drawControl(QStyle::CE_ProgressBar, &newopt, painter, 0);
st.drawControl(QStyle::CE_ProgressBar, &newopt, painter);
#else
QApplication::style()->drawControl(QStyle::CE_ProgressBar, &newopt, painter);
#endif
}
break;
@ -207,6 +211,7 @@ void TransferListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &
default:
QItemDelegate::paint(painter, option, index);
}
painter->restore();
}
@ -225,7 +230,7 @@ QSize TransferListDelegate::sizeHint(const QStyleOptionViewItem &option, const Q
static int nameColHeight = -1;
if (nameColHeight == -1) {
QModelIndex nameColumn = index.sibling(index.row(), TransferListModel::TR_NAME);
const QModelIndex nameColumn = index.sibling(index.row(), TransferListModel::TR_NAME);
nameColHeight = QItemDelegate::sizeHint(option, nameColumn).height();
}
@ -236,60 +241,41 @@ QSize TransferListDelegate::sizeHint(const QStyleOptionViewItem &option, const Q
QString TransferListDelegate::getStatusString(const BitTorrent::TorrentState state) const
{
QString str;
switch (state) {
case BitTorrent::TorrentState::Downloading:
str = tr("Downloading");
break;
return tr("Downloading");
case BitTorrent::TorrentState::StalledDownloading:
str = tr("Stalled", "Torrent is waiting for download to begin");
break;
return tr("Stalled", "Torrent is waiting for download to begin");
case BitTorrent::TorrentState::DownloadingMetadata:
str = tr("Downloading metadata", "used when loading a magnet link");
break;
return tr("Downloading metadata", "Used when loading a magnet link");
case BitTorrent::TorrentState::ForcedDownloading:
str = tr("[F] Downloading", "used when the torrent is forced started. You probably shouldn't translate the F.");
break;
return tr("[F] Downloading", "Used when the torrent is forced started. You probably shouldn't translate the F.");
case BitTorrent::TorrentState::Allocating:
str = tr("Allocating", "qBittorrent is allocating the files on disk");
break;
return tr("Allocating", "qBittorrent is allocating the files on disk");
case BitTorrent::TorrentState::Uploading:
case BitTorrent::TorrentState::StalledUploading:
str = tr("Seeding", "Torrent is complete and in upload-only mode");
break;
return tr("Seeding", "Torrent is complete and in upload-only mode");
case BitTorrent::TorrentState::ForcedUploading:
str = tr("[F] Seeding", "used when the torrent is forced started. You probably shouldn't translate the F.");
break;
return tr("[F] Seeding", "Used when the torrent is forced started. You probably shouldn't translate the F.");
case BitTorrent::TorrentState::QueuedDownloading:
case BitTorrent::TorrentState::QueuedUploading:
str = tr("Queued", "i.e. torrent is queued");
break;
return tr("Queued", "Torrent is queued");
case BitTorrent::TorrentState::CheckingDownloading:
case BitTorrent::TorrentState::CheckingUploading:
str = tr("Checking", "Torrent local data is being checked");
break;
return tr("Checking", "Torrent local data is being checked");
case BitTorrent::TorrentState::CheckingResumeData:
str = tr("Checking resume data", "used when loading the torrents from disk after qbt is launched. It checks the correctness of the .fastresume file. Normally it is completed in a fraction of a second, unless loading many many torrents.");
break;
return tr("Checking resume data", "Used when loading the torrents from disk after qbt is launched. It checks the correctness of the .fastresume file. Normally it is completed in a fraction of a second, unless loading many many torrents.");
case BitTorrent::TorrentState::PausedDownloading:
str = tr("Paused");
break;
return tr("Paused");
case BitTorrent::TorrentState::PausedUploading:
str = tr("Completed");
break;
return tr("Completed");
case BitTorrent::TorrentState::Moving:
str = tr("Moving", "Torrent local data are being moved/relocated");
break;
return tr("Moving", "Torrent local data are being moved/relocated");
case BitTorrent::TorrentState::MissingFiles:
str = tr("Missing Files");
break;
return tr("Missing Files");
case BitTorrent::TorrentState::Error:
str = tr("Errored", "torrent status, the torrent has an error");
break;
return tr("Errored", "Torrent status, the torrent has an error");
default:
str = "";
return {};
}
return str;
}