void DownloadView::buttonPushed(QModelIndex index) { const DownloadItemPointer downloadItemPointer = index.data(DownloadItemRole).value<DownloadItemPointer>(); DownloadItem *downloadItem = downloadItemPointer.data(); switch (downloadItem->status()) { case Downloading: case Starting: downloadItem->stop(); break; case Idle: case Failed: downloadItem->tryAgain(); break; case Finished: downloadItem->openFolder(); } }
void PlaylistItemDelegate::paintDownloadInfo( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const { // get the video metadata const DownloadItemPointer downloadItemPointer = index.data(DownloadItemRole).value<DownloadItemPointer>(); const DownloadItem *downloadItem = downloadItemPointer.data(); painter->save(); const QRect line(0, 0, option.rect.width() / 2, option.rect.height()); painter->translate(option.rect.topLeft()); painter->translate(line.width(), 0); QString message; DownloadItemStatus status = downloadItem->status(); if (status == Downloading) { QString downloaded = DownloadItem::formattedFilesize(downloadItem->bytesReceived()); QString total = DownloadItem::formattedFilesize(downloadItem->bytesTotal()); QString speed = DownloadItem::formattedSpeed(downloadItem->currentSpeed()); QString eta = DownloadItem::formattedTime(downloadItem->remainingTime()); message = tr("%1 of %2 (%3) — %4").arg( downloaded, total, speed, eta ); } else if (status == Starting) { message = tr("Preparing"); } else if (status == Failed) { message = tr("Failed") + " — " + downloadItem->errorMessage(); } else if (status == Finished) { message = tr("Completed"); } else if (status == Idle) { message = tr("Stopped"); } // progressBar->setPalette(option.palette); if (status == Finished) { progressBar->setValue(100); progressBar->setEnabled(true); } else if (status == Downloading) { progressBar->setValue(downloadItem->currentPercent()); progressBar->setEnabled(true); } else { progressBar->setValue(0); progressBar->setEnabled(false); } int progressBarWidth = line.width() - PADDING*4 - 16; progressBar->setMaximumWidth(progressBarWidth); progressBar->setMinimumWidth(progressBarWidth); painter->save(); painter->translate(PADDING, PADDING); progressBar->render(painter); painter->restore(); bool downloadButtonHovered = false; bool downloadButtonPressed = false; const bool isHovered = index.data(HoveredItemRole).toBool(); if (isHovered) { downloadButtonHovered = index.data(DownloadButtonHoveredRole).toBool(); downloadButtonPressed = index.data(DownloadButtonPressedRole).toBool(); } QIcon::Mode iconMode; if (downloadButtonPressed) iconMode = QIcon::Selected; else if (downloadButtonHovered) iconMode = QIcon::Active; else iconMode = QIcon::Normal; if (status != Finished && status != Failed && status != Idle) { if (downloadButtonHovered) message = tr("Stop downloading"); painter->save(); QIcon closeIcon = IconUtils::icon("window-close"); painter->drawPixmap(downloadButtonRect(line), closeIcon.pixmap(16, 16, iconMode)); painter->restore(); } else if (status == Finished) { if (downloadButtonHovered) #ifdef APP_MAC message = tr("Show in %1").arg("Finder"); #else message = tr("Open parent folder"); #endif painter->save(); QIcon searchIcon = IconUtils::icon("system-search"); painter->drawPixmap(downloadButtonRect(line), searchIcon.pixmap(16, 16, iconMode)); painter->restore(); } else if (status == Failed || status == Idle) { if (downloadButtonHovered) message = tr("Restart downloading"); painter->save(); QIcon searchIcon = IconUtils::icon("view-refresh"); painter->drawPixmap(downloadButtonRect(line), searchIcon.pixmap(16, 16, iconMode)); painter->restore(); } QRect textBox = line.adjusted(PADDING, PADDING*2 + progressBar->sizeHint().height(), -2 * PADDING, -PADDING); textBox = painter->boundingRect( textBox, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, message); painter->drawText(textBox, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, message); painter->restore(); }