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

@ -301,7 +301,7 @@ void PeerListWidget::copySelectedPeers()
int row = m_proxyModel->mapToSource(index).row(); int row = m_proxyModel->mapToSource(index).row();
QString ip = m_listModel->data(m_listModel->index(row, PeerListDelegate::IP_HIDDEN)).toString(); QString ip = m_listModel->data(m_listModel->index(row, PeerListDelegate::IP_HIDDEN)).toString();
QString myport = m_listModel->data(m_listModel->index(row, PeerListDelegate::PORT)).toString(); QString myport = m_listModel->data(m_listModel->index(row, PeerListDelegate::PORT)).toString();
if (ip.indexOf('.') == -1) // IPv6 if (!ip.contains('.')) // IPv6
selectedPeers << '[' + ip + "]:" + myport; selectedPeers << '[' + ip + "]:" + myport;
else // IPv4 else // IPv4
selectedPeers << ip + ':' + myport; selectedPeers << ip + ':' + myport;

View file

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

View file

@ -114,13 +114,11 @@ bool TransferListSortModel::lessThan(const QModelIndex &left, const QModelIndex
case TransferListModel::TR_ADD_DATE: case TransferListModel::TR_ADD_DATE:
case TransferListModel::TR_SEED_DATE: case TransferListModel::TR_SEED_DATE:
case TransferListModel::TR_SEEN_COMPLETE_DATE: { case TransferListModel::TR_SEEN_COMPLETE_DATE:
return dateLessThan(sortColumn(), left, right, true); return dateLessThan(sortColumn(), left, right, true);
}
case TransferListModel::TR_QUEUE_POSITION: { case TransferListModel::TR_QUEUE_POSITION:
return lowerPositionThan(left, right); return lowerPositionThan(left, right);
}
case TransferListModel::TR_SEEDS: case TransferListModel::TR_SEEDS:
case TransferListModel::TR_PEERS: { case TransferListModel::TR_PEERS: {
@ -140,20 +138,22 @@ bool TransferListSortModel::lessThan(const QModelIndex &left, const QModelIndex
} }
case TransferListModel::TR_ETA: { case TransferListModel::TR_ETA: {
const TransferListModel *model = qobject_cast<TransferListModel *>(sourceModel());
// Sorting rules prioritized. // Sorting rules prioritized.
// 1. Active torrents at the top // 1. Active torrents at the top
// 2. Seeding torrents at the bottom // 2. Seeding torrents at the bottom
// 3. Torrents with invalid ETAs at the bottom // 3. Torrents with invalid ETAs at the bottom
const bool isActiveL = TorrentFilter::ActiveTorrent.match(model->torrentHandle(model->index(left.row()))); const TransferListModel *model = qobject_cast<TransferListModel *>(sourceModel());
const bool isActiveR = TorrentFilter::ActiveTorrent.match(model->torrentHandle(model->index(right.row())));
// From QSortFilterProxyModel::lessThan() documentation:
// "Note: The indices passed in correspond to the source model"
const bool isActiveL = TorrentFilter::ActiveTorrent.match(model->torrentHandle(left));
const bool isActiveR = TorrentFilter::ActiveTorrent.match(model->torrentHandle(right));
if (isActiveL != isActiveR) if (isActiveL != isActiveR)
return isActiveL; return isActiveL;
const int queuePosL = model->data(model->index(left.row(), TransferListModel::TR_QUEUE_POSITION)).toInt(); const int queuePosL = left.sibling(left.row(), TransferListModel::TR_QUEUE_POSITION).data().toInt();
const int queuePosR = model->data(model->index(right.row(), TransferListModel::TR_QUEUE_POSITION)).toInt(); const int queuePosR = right.sibling(right.row(), TransferListModel::TR_QUEUE_POSITION).data().toInt();
const bool isSeedingL = (queuePosL < 0); const bool isSeedingL = (queuePosL < 0);
const bool isSeedingR = (queuePosR < 0); const bool isSeedingR = (queuePosR < 0);
if (isSeedingL != isSeedingR) { if (isSeedingL != isSeedingR) {
@ -201,22 +201,20 @@ bool TransferListSortModel::lessThan(const QModelIndex &left, const QModelIndex
return (vL < vR); return (vL < vR);
} }
default: { default:
if (left.data() != right.data()) if (left.data() != right.data())
return QSortFilterProxyModel::lessThan(left, right); return QSortFilterProxyModel::lessThan(left, right);
return lowerPositionThan(left, right); return lowerPositionThan(left, right);
}
} }
} }
bool TransferListSortModel::lowerPositionThan(const QModelIndex &left, const QModelIndex &right) const bool TransferListSortModel::lowerPositionThan(const QModelIndex &left, const QModelIndex &right) const
{ {
const TransferListModel *model = qobject_cast<TransferListModel *>(sourceModel());
// Sort according to TR_QUEUE_POSITION // Sort according to TR_QUEUE_POSITION
const int queueL = model->data(model->index(left.row(), TransferListModel::TR_QUEUE_POSITION)).toInt(); const int queueL = left.sibling(left.row(), TransferListModel::TR_QUEUE_POSITION).data().toInt();
const int queueR = model->data(model->index(right.row(), TransferListModel::TR_QUEUE_POSITION)).toInt(); const int queueR = right.sibling(right.row(), TransferListModel::TR_QUEUE_POSITION).data().toInt();
if ((queueL > 0) || (queueR > 0)) { if ((queueL > 0) || (queueR > 0)) {
if ((queueL > 0) && (queueR > 0)) if ((queueL > 0) && (queueR > 0))
return queueL < queueR; return queueL < queueR;
@ -233,9 +231,9 @@ bool TransferListSortModel::lowerPositionThan(const QModelIndex &left, const QMo
// (detailed discussion in #2526 and #2158). // (detailed discussion in #2526 and #2158).
bool TransferListSortModel::dateLessThan(const int dateColumn, const QModelIndex &left, const QModelIndex &right, bool sortInvalidInBottom) const bool TransferListSortModel::dateLessThan(const int dateColumn, const QModelIndex &left, const QModelIndex &right, bool sortInvalidInBottom) const
{ {
const TransferListModel *model = qobject_cast<TransferListModel *>(sourceModel()); const QDateTime dateL = left.sibling(left.row(), dateColumn).data().toDateTime();
const QDateTime dateL = model->data(model->index(left.row(), dateColumn)).toDateTime(); const QDateTime dateR = right.sibling(right.row(), dateColumn).data().toDateTime();
const QDateTime dateR = model->data(model->index(right.row(), dateColumn)).toDateTime();
if (dateL.isValid() && dateR.isValid()) { if (dateL.isValid() && dateR.isValid()) {
if (dateL != dateR) if (dateL != dateR)
return dateL < dateR; return dateL < dateR;
@ -248,23 +246,24 @@ bool TransferListSortModel::dateLessThan(const int dateColumn, const QModelIndex
} }
// Finally, sort by hash // Finally, sort by hash
const QString hashL(model->torrentHandle(model->index(left.row()))->hash()); const TransferListModel *model = qobject_cast<TransferListModel *>(sourceModel());
const QString hashR(model->torrentHandle(model->index(right.row()))->hash()); const QString hashL = model->torrentHandle(left)->hash();
const QString hashR = model->torrentHandle(right)->hash();
return hashL < hashR; return hashL < hashR;
} }
bool TransferListSortModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const bool TransferListSortModel::filterAcceptsRow(const int sourceRow, const QModelIndex &sourceParent) const
{ {
return matchFilter(sourceRow, sourceParent) return matchFilter(sourceRow, sourceParent)
&& QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent); && QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
} }
bool TransferListSortModel::matchFilter(int sourceRow, const QModelIndex &sourceParent) const bool TransferListSortModel::matchFilter(const int sourceRow, const QModelIndex &sourceParent) const
{ {
auto *model = qobject_cast<TransferListModel *>(sourceModel()); const auto *model = qobject_cast<TransferListModel *>(sourceModel());
if (!model) return false; if (!model) return false;
BitTorrent::TorrentHandle *const torrent = model->torrentHandle(model->index(sourceRow, 0, sourceParent)); const BitTorrent::TorrentHandle *torrent = model->torrentHandle(model->index(sourceRow, 0, sourceParent));
if (!torrent) return false; if (!torrent) return false;
return m_filter.match(torrent); return m_filter.match(torrent);