1
0
mirror of https://github.com/d47081/qBittorrent.git synced 2025-01-15 01:00:17 +00:00

Follow project coding style. Issue #2192.

This commit is contained in:
buinsky 2015-01-29 20:35:57 +03:00
parent f5f9e206fb
commit 623797a6c4
2 changed files with 450 additions and 421 deletions

View File

@ -48,332 +48,353 @@
#include "autoexpandabledialog.h" #include "autoexpandabledialog.h"
#include "torrentfilterenum.h" #include "torrentfilterenum.h"
LabelFiltersList::LabelFiltersList(QWidget *parent): QListWidget(parent) { LabelFiltersList::LabelFiltersList(QWidget *parent): QListWidget(parent)
itemHover = 0; {
// Accept drop
setAcceptDrops(true);
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
setStyleSheet("QListWidget { background: transparent; border: 0 }");
#if defined(Q_OS_MAC)
setAttribute(Qt::WA_MacShowFocusRect, false);
#endif
}
void LabelFiltersList::addItem(QListWidgetItem *it) {
Q_ASSERT(count() >= 2);
for (int i=2; i<count(); ++i) {
if (item(i)->text().localeAwareCompare(it->text()) >= 0) {
insertItem(i, it);
return;
}
}
QListWidget::addItem(it);
}
QString LabelFiltersList::labelFromRow(int row) const {
Q_ASSERT(row > 1);
const QString &label = item(row)->text();
QStringList parts = label.split(" ");
Q_ASSERT(parts.size() >= 2);
parts.removeLast(); // Remove trailing number
return parts.join(" ");
}
int LabelFiltersList::rowFromLabel(QString label) const {
Q_ASSERT(!label.isEmpty());
for (int i=2; i<count(); ++i) {
if (label == labelFromRow(i)) return i;
}
return -1;
}
void LabelFiltersList::dragMoveEvent(QDragMoveEvent *event) {
if (itemAt(event->pos()) && row(itemAt(event->pos())) > 0) {
if (itemHover) {
if (itemHover != itemAt(event->pos())) {
setItemHover(false);
itemHover = itemAt(event->pos());
setItemHover(true);
}
} else {
itemHover = itemAt(event->pos());
setItemHover(true);
}
event->acceptProposedAction();
} else {
if (itemHover)
setItemHover(false);
event->ignore();
}
}
void LabelFiltersList::dropEvent(QDropEvent *event) {
qDebug("Drop Event in labels list");
if (itemAt(event->pos())) {
emit torrentDropped(row(itemAt(event->pos())));
}
event->ignore();
setItemHover(false);
// Select current item again
currentItem()->setSelected(true);
}
void LabelFiltersList::dragLeaveEvent(QDragLeaveEvent*) {
if (itemHover)
setItemHover(false);
// Select current item again
currentItem()->setSelected(true);
}
void LabelFiltersList::setItemHover(bool hover) {
Q_ASSERT(itemHover);
if (hover) {
itemHover->setData(Qt::DecorationRole, IconProvider::instance()->getIcon("folder-documents.png"));
itemHover->setSelected(true);
//setCurrentItem(itemHover);
} else {
itemHover->setData(Qt::DecorationRole, IconProvider::instance()->getIcon("inode-directory.png"));
//itemHover->setSelected(false);
itemHover = 0; itemHover = 0;
} // Accept drop
} setAcceptDrops(true);
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
StatusFiltersWidget::StatusFiltersWidget(QWidget *parent) : QListWidget(parent), m_shown(false) { setStyleSheet("QListWidget { background: transparent; border: 0 }");
setUniformItemSizes(true);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
// Height is fixed (sizeHint().height() is used)
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
setStyleSheet("QListWidget { background: transparent; border: 0 }");
#if defined(Q_OS_MAC) #if defined(Q_OS_MAC)
setAttribute(Qt::WA_MacShowFocusRect, false); setAttribute(Qt::WA_MacShowFocusRect, false);
#endif #endif
} }
QSize StatusFiltersWidget::sizeHint() const { void LabelFiltersList::addItem(QListWidgetItem *it)
QSize size = QListWidget::sizeHint(); {
// Height should be exactly the height of the content Q_ASSERT(count() >= 2);
size.setHeight(contentsSize().height() + 2 * frameWidth()+6); for (int i = 2; i<count(); ++i) {
return size; if (item(i)->text().localeAwareCompare(it->text()) >= 0) {
insertItem(i, it);
return;
}
}
QListWidget::addItem(it);
} }
TransferListFiltersWidget::TransferListFiltersWidget(QWidget *parent, TransferListWidget *transferList): QFrame(parent), transferList(transferList), nb_labeled(0), nb_torrents(0) { QString LabelFiltersList::labelFromRow(int row) const
// Construct lists {
vLayout = new QVBoxLayout(); Q_ASSERT(row > 1);
vLayout->setContentsMargins(0, 4, 0, 0); const QString &label = item(row)->text();
QFont font; QStringList parts = label.split(" ");
font.setBold(true); Q_ASSERT(parts.size() >= 2);
font.setCapitalization(QFont::SmallCaps); parts.removeLast(); // Remove trailing number
QLabel *torrentsLabel = new QLabel(tr("Torrents")); return parts.join(" ");
torrentsLabel->setIndent(2);
torrentsLabel->setFont(font);
vLayout->addWidget(torrentsLabel);
statusFilters = new StatusFiltersWidget(this);
vLayout->addWidget(statusFilters);
QLabel *labelsLabel = new QLabel(tr("Labels"));
labelsLabel->setIndent(2);
labelsLabel->setFont(font);
vLayout->addWidget(labelsLabel);
labelFilters = new LabelFiltersList(this);
vLayout->addWidget(labelFilters);
setLayout(vLayout);
labelFilters->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
statusFilters->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
statusFilters->setSpacing(0);
setContentsMargins(0,0,0,0);
vLayout->setSpacing(2);
// Add status filters
QListWidgetItem *all = new QListWidgetItem(statusFilters);
all->setData(Qt::DisplayRole, QVariant(tr("All") + " (0)"));
all->setData(Qt::DecorationRole, QIcon(":/icons/skin/filterall.png"));
QListWidgetItem *downloading = new QListWidgetItem(statusFilters);
downloading->setData(Qt::DisplayRole, QVariant(tr("Downloading") + " (0)"));
downloading->setData(Qt::DecorationRole, QIcon(":/icons/skin/downloading.png"));
QListWidgetItem *completed = new QListWidgetItem(statusFilters);
completed->setData(Qt::DisplayRole, QVariant(tr("Completed") + " (0)"));
completed->setData(Qt::DecorationRole, QIcon(":/icons/skin/uploading.png"));
QListWidgetItem *paused = new QListWidgetItem(statusFilters);
paused->setData(Qt::DisplayRole, QVariant(tr("Paused") + " (0)"));
paused->setData(Qt::DecorationRole, QIcon(":/icons/skin/paused.png"));
QListWidgetItem *resumed = new QListWidgetItem(statusFilters);
resumed->setData(Qt::DisplayRole, QVariant(tr("Resumed") + " (0)"));
resumed->setData(Qt::DecorationRole, QIcon(":/icons/skin/resumed.png"));
QListWidgetItem *active = new QListWidgetItem(statusFilters);
active->setData(Qt::DisplayRole, QVariant(tr("Active") + " (0)"));
active->setData(Qt::DecorationRole, QIcon(":/icons/skin/filteractive.png"));
QListWidgetItem *inactive = new QListWidgetItem(statusFilters);
inactive->setData(Qt::DisplayRole, QVariant(tr("Inactive") + " (0)"));
inactive->setData(Qt::DecorationRole, QIcon(":/icons/skin/filterinactive.png"));
// SIGNAL/SLOT
connect(statusFilters, SIGNAL(currentRowChanged(int)), transferList, SLOT(applyStatusFilter(int)));
connect(transferList->getSourceModel(), SIGNAL(modelRefreshed()), SLOT(updateTorrentNumbers()));
connect(transferList->getSourceModel(), SIGNAL(torrentAdded(TorrentModelItem*)), SLOT(handleNewTorrent(TorrentModelItem*)));
connect(labelFilters, SIGNAL(currentRowChanged(int)), this, SLOT(applyLabelFilter(int)));
connect(labelFilters, SIGNAL(torrentDropped(int)), this, SLOT(torrentDropped(int)));
connect(transferList->getSourceModel(), SIGNAL(torrentAboutToBeRemoved(TorrentModelItem*)), SLOT(torrentAboutToBeDeleted(TorrentModelItem*)));
connect(transferList->getSourceModel(), SIGNAL(torrentChangedLabel(TorrentModelItem*,QString,QString)), SLOT(torrentChangedLabel(TorrentModelItem*, QString, QString)));
// Add Label filters
QListWidgetItem *allLabels = new QListWidgetItem(labelFilters);
allLabels->setData(Qt::DisplayRole, QVariant(tr("All labels") + " (0)"));
allLabels->setData(Qt::DecorationRole, IconProvider::instance()->getIcon("inode-directory"));
QListWidgetItem *noLabel = new QListWidgetItem(labelFilters);
noLabel->setData(Qt::DisplayRole, QVariant(tr("Unlabeled") + " (0)"));
noLabel->setData(Qt::DecorationRole, IconProvider::instance()->getIcon("inode-directory"));
// Load settings
loadSettings();
labelFilters->setCurrentRow(0);
//labelFilters->selectionModel()->select(labelFilters->model()->index(0,0), QItemSelectionModel::Select);
// Label menu
labelFilters->setContextMenuPolicy(Qt::CustomContextMenu);
connect(labelFilters, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showLabelMenu(QPoint)));
} }
TransferListFiltersWidget::~TransferListFiltersWidget() { int LabelFiltersList::rowFromLabel(QString label) const
saveSettings(); {
delete statusFilters; Q_ASSERT(!label.isEmpty());
delete labelFilters; for (int i = 2; i<count(); ++i)
delete vLayout; if (label == labelFromRow(i)) return i;
return -1;
} }
StatusFiltersWidget* TransferListFiltersWidget::getStatusFilters() const { void LabelFiltersList::dragMoveEvent(QDragMoveEvent *event)
return statusFilters; {
if (itemAt(event->pos()) && row(itemAt(event->pos())) > 0) {
if (itemHover) {
if (itemHover != itemAt(event->pos())) {
setItemHover(false);
itemHover = itemAt(event->pos());
setItemHover(true);
}
}
else {
itemHover = itemAt(event->pos());
setItemHover(true);
}
event->acceptProposedAction();
}
else {
if (itemHover)
setItemHover(false);
event->ignore();
}
} }
void TransferListFiltersWidget::saveSettings() const { void LabelFiltersList::dropEvent(QDropEvent *event)
Preferences* const pref = Preferences::instance(); {
pref->setTransSelFilter(statusFilters->currentRow()); qDebug("Drop Event in labels list");
pref->setTorrentLabels(customLabels.keys()); if (itemAt(event->pos()))
emit torrentDropped(row(itemAt(event->pos())));
event->ignore();
setItemHover(false);
// Select current item again
currentItem()->setSelected(true);
} }
void TransferListFiltersWidget::loadSettings() { void LabelFiltersList::dragLeaveEvent(QDragLeaveEvent*)
statusFilters->setCurrentRow(Preferences::instance()->getTransSelFilter()); {
const QStringList label_list = Preferences::instance()->getTorrentLabels(); if (itemHover)
foreach (const QString &label, label_list) { setItemHover(false);
customLabels.insert(label, 0); // Select current item again
qDebug("Creating label QListWidgetItem: %s", qPrintable(label)); currentItem()->setSelected(true);
}
void LabelFiltersList::setItemHover(bool hover)
{
Q_ASSERT(itemHover);
if (hover) {
itemHover->setData(Qt::DecorationRole, IconProvider::instance()->getIcon("folder-documents.png"));
itemHover->setSelected(true);
//setCurrentItem(itemHover);
}
else {
itemHover->setData(Qt::DecorationRole, IconProvider::instance()->getIcon("inode-directory.png"));
//itemHover->setSelected(false);
itemHover = 0;
}
}
StatusFiltersWidget::StatusFiltersWidget(QWidget *parent): QListWidget(parent), m_shown(false)
{
setUniformItemSizes(true);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
// Height is fixed (sizeHint().height() is used)
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
setStyleSheet("QListWidget { background: transparent; border: 0 }");
#if defined(Q_OS_MAC)
setAttribute(Qt::WA_MacShowFocusRect, false);
#endif
}
QSize StatusFiltersWidget::sizeHint() const
{
QSize size = QListWidget::sizeHint();
// Height should be exactly the height of the content
size.setHeight(contentsSize().height() + 2 * frameWidth() + 6);
return size;
}
TransferListFiltersWidget::TransferListFiltersWidget(QWidget *parent, TransferListWidget *transferList): QFrame(parent), transferList(transferList), nb_labeled(0), nb_torrents(0)
{
// Construct lists
vLayout = new QVBoxLayout();
vLayout->setContentsMargins(0, 4, 0, 0);
QFont font;
font.setBold(true);
font.setCapitalization(QFont::SmallCaps);
QLabel *torrentsLabel = new QLabel(tr("Torrents"));
torrentsLabel->setIndent(2);
torrentsLabel->setFont(font);
vLayout->addWidget(torrentsLabel);
statusFilters = new StatusFiltersWidget(this);
vLayout->addWidget(statusFilters);
QLabel *labelsLabel = new QLabel(tr("Labels"));
labelsLabel->setIndent(2);
labelsLabel->setFont(font);
vLayout->addWidget(labelsLabel);
labelFilters = new LabelFiltersList(this);
vLayout->addWidget(labelFilters);
setLayout(vLayout);
labelFilters->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
statusFilters->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
statusFilters->setSpacing(0);
setContentsMargins(0,0,0,0);
vLayout->setSpacing(2);
// Add status filters
QListWidgetItem *all = new QListWidgetItem(statusFilters);
all->setData(Qt::DisplayRole, QVariant(tr("All") + " (0)"));
all->setData(Qt::DecorationRole, QIcon(":/Icons/skin/filterall.png"));
QListWidgetItem *downloading = new QListWidgetItem(statusFilters);
downloading->setData(Qt::DisplayRole, QVariant(tr("Downloading") + " (0)"));
downloading->setData(Qt::DecorationRole, QIcon(":/Icons/skin/downloading.png"));
QListWidgetItem *completed = new QListWidgetItem(statusFilters);
completed->setData(Qt::DisplayRole, QVariant(tr("Completed") + " (0)"));
completed->setData(Qt::DecorationRole, QIcon(":/Icons/skin/uploading.png"));
QListWidgetItem *paused = new QListWidgetItem(statusFilters);
paused->setData(Qt::DisplayRole, QVariant(tr("Paused") + " (0)"));
paused->setData(Qt::DecorationRole, QIcon(":/Icons/skin/paused.png"));
QListWidgetItem *resumed = new QListWidgetItem(statusFilters);
resumed->setData(Qt::DisplayRole, QVariant(tr("Resumed") + " (0)"));
resumed->setData(Qt::DecorationRole, QIcon(":/Icons/skin/resumed.png"));
QListWidgetItem *active = new QListWidgetItem(statusFilters);
active->setData(Qt::DisplayRole, QVariant(tr("Active") + " (0)"));
active->setData(Qt::DecorationRole, QIcon(":/Icons/skin/filteractive.png"));
QListWidgetItem *inactive = new QListWidgetItem(statusFilters);
inactive->setData(Qt::DisplayRole, QVariant(tr("Inactive") + " (0)"));
inactive->setData(Qt::DecorationRole, QIcon(":/Icons/skin/filterinactive.png"));
// SIGNAL/SLOT
connect(statusFilters, SIGNAL(currentRowChanged(int)), transferList, SLOT(applyStatusFilter(int)));
connect(transferList->getSourceModel(), SIGNAL(modelRefreshed()), SLOT(updateTorrentNumbers()));
connect(transferList->getSourceModel(), SIGNAL(torrentAdded(TorrentModelItem*)), SLOT(handleNewTorrent(TorrentModelItem*)));
connect(labelFilters, SIGNAL(currentRowChanged(int)), this, SLOT(applyLabelFilter(int)));
connect(labelFilters, SIGNAL(torrentDropped(int)), this, SLOT(torrentDropped(int)));
connect(transferList->getSourceModel(), SIGNAL(torrentAboutToBeRemoved(TorrentModelItem*)), SLOT(torrentAboutToBeDeleted(TorrentModelItem*)));
connect(transferList->getSourceModel(), SIGNAL(torrentChangedLabel(TorrentModelItem*,QString,QString)), SLOT(torrentChangedLabel(TorrentModelItem*, QString, QString)));
// Add Label filters
QListWidgetItem *allLabels = new QListWidgetItem(labelFilters);
allLabels->setData(Qt::DisplayRole, QVariant(tr("All labels") + " (0)"));
allLabels->setData(Qt::DecorationRole, IconProvider::instance()->getIcon("inode-directory"));
QListWidgetItem *noLabel = new QListWidgetItem(labelFilters);
noLabel->setData(Qt::DisplayRole, QVariant(tr("Unlabeled") + " (0)"));
noLabel->setData(Qt::DecorationRole, IconProvider::instance()->getIcon("inode-directory"));
// Load settings
loadSettings();
labelFilters->setCurrentRow(0);
//labelFilters->selectionModel()->select(labelFilters->model()->index(0,0), QItemSelectionModel::Select);
// Label menu
labelFilters->setContextMenuPolicy(Qt::CustomContextMenu);
connect(labelFilters, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showLabelMenu(QPoint)));
}
TransferListFiltersWidget::~TransferListFiltersWidget()
{
saveSettings();
delete statusFilters;
delete labelFilters;
delete vLayout;
}
StatusFiltersWidget* TransferListFiltersWidget::getStatusFilters() const
{
return statusFilters;
}
void TransferListFiltersWidget::saveSettings() const
{
Preferences* const pref = Preferences::instance();
pref->setTransSelFilter(statusFilters->currentRow());
pref->setTorrentLabels(customLabels.keys());
}
void TransferListFiltersWidget::loadSettings()
{
statusFilters->setCurrentRow(Preferences::instance()->getTransSelFilter());
const QStringList label_list = Preferences::instance()->getTorrentLabels();
foreach (const QString &label, label_list) {
customLabels.insert(label, 0);
qDebug("Creating label QListWidgetItem: %s", qPrintable(label));
QListWidgetItem *newLabel = new QListWidgetItem();
newLabel->setText(label + " (0)");
newLabel->setData(Qt::DecorationRole, IconProvider::instance()->getIcon("inode-directory"));
labelFilters->addItem(newLabel);
}
}
void TransferListFiltersWidget::updateTorrentNumbers()
{
const TorrentStatusReport report = transferList->getSourceModel()->getTorrentStatusReport();
statusFilters->item(TorrentFilter::ALL)->setData(Qt::DisplayRole, QVariant(tr("All") + " (" + QString::number(report.nb_active + report.nb_inactive) + ")"));
statusFilters->item(TorrentFilter::DOWNLOADING)->setData(Qt::DisplayRole, QVariant(tr("Downloading") + " (" + QString::number(report.nb_downloading) + ")"));
statusFilters->item(TorrentFilter::COMPLETED)->setData(Qt::DisplayRole, QVariant(tr("Completed") + " (" + QString::number(report.nb_seeding) + ")"));
statusFilters->item(TorrentFilter::PAUSED)->setData(Qt::DisplayRole, QVariant(tr("Paused") + " (" + QString::number(report.nb_paused) + ")"));
statusFilters->item(TorrentFilter::RESUMED)->setData(Qt::DisplayRole, QVariant(tr("Resumed") + " (" + QString::number(report.nb_active + report.nb_inactive - report.nb_paused) + ")"));
statusFilters->item(TorrentFilter::ACTIVE)->setData(Qt::DisplayRole, QVariant(tr("Active") + " (" + QString::number(report.nb_active) + ")"));
statusFilters->item(TorrentFilter::INACTIVE)->setData(Qt::DisplayRole, QVariant(tr("Inactive") + " (" + QString::number(report.nb_inactive) + ")"));
}
void TransferListFiltersWidget::torrentDropped(int row)
{
Q_ASSERT(row > 0);
if (row == 1)
transferList->setSelectionLabel("");
else
transferList->setSelectionLabel(labelFilters->labelFromRow(row));
}
void TransferListFiltersWidget::addLabel(QString& label)
{
label = fsutils::toValidFileSystemName(label.trimmed());
if (label.isEmpty() || customLabels.contains(label)) return;
QListWidgetItem *newLabel = new QListWidgetItem(); QListWidgetItem *newLabel = new QListWidgetItem();
newLabel->setText(label + " (0)"); newLabel->setText(label + " (0)");
newLabel->setData(Qt::DecorationRole, IconProvider::instance()->getIcon("inode-directory")); newLabel->setData(Qt::DecorationRole, IconProvider::instance()->getIcon("inode-directory"));
labelFilters->addItem(newLabel); labelFilters->addItem(newLabel);
} customLabels.insert(label, 0);
Preferences::instance()->addTorrentLabel(label);
} }
void TransferListFiltersWidget::updateTorrentNumbers() { void TransferListFiltersWidget::showLabelMenu(QPoint)
const TorrentStatusReport report = transferList->getSourceModel()->getTorrentStatusReport(); {
statusFilters->item(TorrentFilter::ALL)->setData(Qt::DisplayRole, QVariant(tr("All")+" ("+QString::number(report.nb_active+report.nb_inactive)+")")); QMenu labelMenu(labelFilters);
statusFilters->item(TorrentFilter::DOWNLOADING)->setData(Qt::DisplayRole, QVariant(tr("Downloading")+" ("+QString::number(report.nb_downloading)+")")); QAction *addAct = labelMenu.addAction(IconProvider::instance()->getIcon("list-add"), tr("Add label..."));
statusFilters->item(TorrentFilter::COMPLETED)->setData(Qt::DisplayRole, QVariant(tr("Completed")+" ("+QString::number(report.nb_seeding)+")")); QAction *removeAct = 0;
statusFilters->item(TorrentFilter::PAUSED)->setData(Qt::DisplayRole, QVariant(tr("Paused")+" ("+QString::number(report.nb_paused)+")")); QAction *removeUnusedAct = 0;
statusFilters->item(TorrentFilter::RESUMED)->setData(Qt::DisplayRole, QVariant(tr("Resumed")+" ("+QString::number(report.nb_active+report.nb_inactive-report.nb_paused)+")")); if (!labelFilters->selectedItems().empty() && labelFilters->row(labelFilters->selectedItems().first()) > 1)
statusFilters->item(TorrentFilter::ACTIVE)->setData(Qt::DisplayRole, QVariant(tr("Active")+" ("+QString::number(report.nb_active)+")")); removeAct = labelMenu.addAction(IconProvider::instance()->getIcon("list-remove"), tr("Remove label"));
statusFilters->item(TorrentFilter::INACTIVE)->setData(Qt::DisplayRole, QVariant(tr("Inactive")+" ("+QString::number(report.nb_inactive)+")")); else
} removeUnusedAct = labelMenu.addAction(IconProvider::instance()->getIcon("list-remove"), tr("Remove unused labels"));
labelMenu.addSeparator();
void TransferListFiltersWidget::torrentDropped(int row) { QAction *startAct = labelMenu.addAction(IconProvider::instance()->getIcon("media-playback-start"), tr("Resume torrents"));
Q_ASSERT(row > 0); QAction *pauseAct = labelMenu.addAction(IconProvider::instance()->getIcon("media-playback-pause"), tr("Pause torrents"));
if (row == 1) { QAction *deleteTorrentsAct = labelMenu.addAction(IconProvider::instance()->getIcon("edit-delete"), tr("Delete torrents"));
transferList->setSelectionLabel(""); QAction *act = 0;
} else { act = labelMenu.exec(QCursor::pos());
transferList->setSelectionLabel(labelFilters->labelFromRow(row)); if (act) {
} if (act == removeAct) {
} removeSelectedLabel();
return;
void TransferListFiltersWidget::addLabel(QString& label) { }
label = fsutils::toValidFileSystemName(label.trimmed()); if (act == removeUnusedAct) {
if (label.isEmpty() || customLabels.contains(label)) return; removeUnusedLabels();
QListWidgetItem *newLabel = new QListWidgetItem(); return;
newLabel->setText(label + " (0)"); }
newLabel->setData(Qt::DecorationRole, IconProvider::instance()->getIcon("inode-directory")); if (act == deleteTorrentsAct) {
labelFilters->addItem(newLabel); transferList->deleteVisibleTorrents();
customLabels.insert(label, 0); return;
Preferences::instance()->addTorrentLabel(label); }
} if (act == startAct) {
transferList->startVisibleTorrents();
void TransferListFiltersWidget::showLabelMenu(QPoint) { return;
QMenu labelMenu(labelFilters); }
QAction *addAct = labelMenu.addAction(IconProvider::instance()->getIcon("list-add"), tr("Add label...")); if (act == pauseAct) {
QAction *removeAct = 0; transferList->pauseVisibleTorrents();
QAction *removeUnusedAct = 0; return;
if (!labelFilters->selectedItems().empty() && labelFilters->row(labelFilters->selectedItems().first()) > 1) }
removeAct = labelMenu.addAction(IconProvider::instance()->getIcon("list-remove"), tr("Remove label")); if (act == addAct) {
else bool ok;
removeUnusedAct = labelMenu.addAction(IconProvider::instance()->getIcon("list-remove"), tr("Remove unused labels")); QString label = "";
labelMenu.addSeparator(); bool invalid;
QAction *startAct = labelMenu.addAction(IconProvider::instance()->getIcon("media-playback-start"), tr("Resume torrents")); do {
QAction *pauseAct = labelMenu.addAction(IconProvider::instance()->getIcon("media-playback-pause"), tr("Pause torrents")); invalid = false;
QAction *deleteTorrentsAct = labelMenu.addAction(IconProvider::instance()->getIcon("edit-delete"), tr("Delete torrents")); label = AutoExpandableDialog::getText(this, tr("New Label"), tr("Label:"), QLineEdit::Normal, label, &ok);
QAction *act = 0; if (ok && !label.isEmpty()) {
act = labelMenu.exec(QCursor::pos()); if (fsutils::isValidFileSystemName(label)) {
if (act) { addLabel(label);
if (act == removeAct) { }
removeSelectedLabel(); else {
return; QMessageBox::warning(this, tr("Invalid label name"), tr("Please don't use any special characters in the label name."));
} invalid = true;
if (act == removeUnusedAct) { }
removeUnusedLabels(); }
return; } while (invalid);
} return;
if (act == deleteTorrentsAct) {
transferList->deleteVisibleTorrents();
return;
}
if (act == startAct) {
transferList->startVisibleTorrents();
return;
}
if (act == pauseAct) {
transferList->pauseVisibleTorrents();
return;
}
if (act == addAct) {
bool ok;
QString label = "";
bool invalid;
do {
invalid = false;
label = AutoExpandableDialog::getText(this, tr("New Label"), tr("Label:"), QLineEdit::Normal, label, &ok);
if (ok && !label.isEmpty()) {
if (fsutils::isValidFileSystemName(label)) {
addLabel(label);
} else {
QMessageBox::warning(this, tr("Invalid label name"), tr("Please don't use any special characters in the label name."));
invalid = true;
}
} }
} while(invalid);
return;
} }
}
} }
void TransferListFiltersWidget::removeSelectedLabel() { void TransferListFiltersWidget::removeSelectedLabel()
const int row = labelFilters->row(labelFilters->selectedItems().first()); {
Q_ASSERT(row > 1); const int row = labelFilters->row(labelFilters->selectedItems().first());
const QString &label = labelFilters->labelFromRow(row); Q_ASSERT(row > 1);
Q_ASSERT(customLabels.contains(label)); const QString &label = labelFilters->labelFromRow(row);
customLabels.remove(label); Q_ASSERT(customLabels.contains(label));
transferList->removeLabelFromRows(label); customLabels.remove(label);
// Select first label transferList->removeLabelFromRows(label);
labelFilters->setCurrentItem(labelFilters->item(0)); // Select first label
labelFilters->selectionModel()->select(labelFilters->model()->index(0,0), QItemSelectionModel::Select); labelFilters->setCurrentItem(labelFilters->item(0));
applyLabelFilter(0); labelFilters->selectionModel()->select(labelFilters->model()->index(0,0), QItemSelectionModel::Select);
// Un display filter applyLabelFilter(0);
delete labelFilters->takeItem(row); // Un display filter
// Save custom labels to remember it was deleted delete labelFilters->takeItem(row);
Preferences::instance()->removeTorrentLabel(label); // Save custom labels to remember it was deleted
Preferences::instance()->removeTorrentLabel(label);
} }
void TransferListFiltersWidget::removeUnusedLabels() { void TransferListFiltersWidget::removeUnusedLabels()
{
QStringList unusedLabels; QStringList unusedLabels;
QHash<QString, int>::const_iterator i; QHash<QString, int>::const_iterator i;
for (i = customLabels.begin(); i != customLabels.end(); ++i) { for (i = customLabels.begin(); i != customLabels.end(); ++i)
if (i.value() == 0) if (i.value() == 0)
unusedLabels << i.key(); unusedLabels << i.key();
}
foreach (const QString &label, unusedLabels) { foreach (const QString &label, unusedLabels) {
customLabels.remove(label); customLabels.remove(label);
delete labelFilters->takeItem(labelFilters->rowFromLabel(label)); delete labelFilters->takeItem(labelFilters->rowFromLabel(label));
@ -381,95 +402,100 @@ void TransferListFiltersWidget::removeUnusedLabels() {
} }
} }
void TransferListFiltersWidget::applyLabelFilter(int row) { void TransferListFiltersWidget::applyLabelFilter(int row)
switch(row) { {
case 0: switch (row) {
transferList->applyLabelFilterAll(); case 0:
break; transferList->applyLabelFilterAll();
case 1: break;
transferList->applyLabelFilter(QString()); case 1:
break; transferList->applyLabelFilter(QString());
default: break;
transferList->applyLabelFilter(labelFilters->labelFromRow(row)); default:
} transferList->applyLabelFilter(labelFilters->labelFromRow(row));
}
void TransferListFiltersWidget::torrentChangedLabel(TorrentModelItem *torrentItem, QString old_label, QString new_label) {
Q_UNUSED(torrentItem);
qDebug("Torrent label changed from %s to %s", qPrintable(old_label), qPrintable(new_label));
if (!old_label.isEmpty()) {
if (customLabels.contains(old_label)) {
const int new_count = customLabels.value(old_label, 0) - 1;
Q_ASSERT(new_count >= 0);
customLabels.insert(old_label, new_count);
const int row = labelFilters->rowFromLabel(old_label);
Q_ASSERT(row >= 2);
labelFilters->item(row)->setText(old_label + " ("+ QString::number(new_count) +")");
} }
--nb_labeled;
}
if (!new_label.isEmpty()) {
if (!customLabels.contains(new_label))
addLabel(new_label);
const int new_count = customLabels.value(new_label, 0) + 1;
Q_ASSERT(new_count >= 1);
customLabels.insert(new_label, new_count);
const int row = labelFilters->rowFromLabel(new_label);
Q_ASSERT(row >= 2);
labelFilters->item(row)->setText(new_label + " ("+ QString::number(new_count) +")");
++nb_labeled;
}
updateStickyLabelCounters();
} }
void TransferListFiltersWidget::handleNewTorrent(TorrentModelItem* torrentItem) { void TransferListFiltersWidget::torrentChangedLabel(TorrentModelItem *torrentItem, QString old_label, QString new_label)
QString label = torrentItem->data(TorrentModelItem::TR_LABEL).toString(); {
qDebug("New torrent was added with label: %s", qPrintable(label)); Q_UNUSED(torrentItem);
if (!label.isEmpty()) { qDebug("Torrent label changed from %s to %s", qPrintable(old_label), qPrintable(new_label));
if (!customLabels.contains(label)) { if (!old_label.isEmpty()) {
addLabel(label); if (customLabels.contains(old_label)) {
// addLabel may have changed the label, update the model accordingly. const int new_count = customLabels.value(old_label, 0) - 1;
torrentItem->setData(TorrentModelItem::TR_LABEL, label); Q_ASSERT(new_count >= 0);
customLabels.insert(old_label, new_count);
const int row = labelFilters->rowFromLabel(old_label);
Q_ASSERT(row >= 2);
labelFilters->item(row)->setText(old_label + " (" + QString::number(new_count) + ")");
}
--nb_labeled;
} }
// Update label counter if (!new_label.isEmpty()) {
Q_ASSERT(customLabels.contains(label)); if (!customLabels.contains(new_label))
const int new_count = customLabels.value(label, 0) + 1; addLabel(new_label);
customLabels.insert(label, new_count); const int new_count = customLabels.value(new_label, 0) + 1;
const int row = labelFilters->rowFromLabel(label); Q_ASSERT(new_count >= 1);
qDebug("torrentAdded, Row: %d", row); customLabels.insert(new_label, new_count);
Q_ASSERT(row >= 2); const int row = labelFilters->rowFromLabel(new_label);
Q_ASSERT(labelFilters->item(row)); Q_ASSERT(row >= 2);
labelFilters->item(row)->setText(label + " ("+ QString::number(new_count) +")"); labelFilters->item(row)->setText(new_label + " (" + QString::number(new_count) + ")");
++nb_labeled; ++nb_labeled;
} }
++nb_torrents; updateStickyLabelCounters();
Q_ASSERT(nb_torrents >= 0);
Q_ASSERT(nb_labeled >= 0);
Q_ASSERT(nb_labeled <= nb_torrents);
updateStickyLabelCounters();
} }
void TransferListFiltersWidget::torrentAboutToBeDeleted(TorrentModelItem* torrentItem) { void TransferListFiltersWidget::handleNewTorrent(TorrentModelItem* torrentItem)
Q_ASSERT(torrentItem); {
QString label = torrentItem->data(TorrentModelItem::TR_LABEL).toString(); QString label = torrentItem->data(TorrentModelItem::TR_LABEL).toString();
if (!label.isEmpty()) { qDebug("New torrent was added with label: %s", qPrintable(label));
// Update label counter if (!label.isEmpty()) {
const int new_count = customLabels.value(label, 0) - 1; if (!customLabels.contains(label)) {
customLabels.insert(label, new_count); addLabel(label);
const int row = labelFilters->rowFromLabel(label); // addLabel may have changed the label, update the model accordingly.
Q_ASSERT(row >= 2); torrentItem->setData(TorrentModelItem::TR_LABEL, label);
labelFilters->item(row)->setText(label + " ("+ QString::number(new_count) +")"); }
--nb_labeled; // Update label counter
} Q_ASSERT(customLabels.contains(label));
--nb_torrents; const int new_count = customLabels.value(label, 0) + 1;
qDebug("nb_torrents: %d, nb_labeled: %d", nb_torrents, nb_labeled); customLabels.insert(label, new_count);
Q_ASSERT(nb_torrents >= 0); const int row = labelFilters->rowFromLabel(label);
Q_ASSERT(nb_labeled >= 0); qDebug("torrentAdded, Row: %d", row);
Q_ASSERT(nb_labeled <= nb_torrents); Q_ASSERT(row >= 2);
updateStickyLabelCounters(); Q_ASSERT(labelFilters->item(row));
labelFilters->item(row)->setText(label + " (" + QString::number(new_count) + ")");
++nb_labeled;
}
++nb_torrents;
Q_ASSERT(nb_torrents >= 0);
Q_ASSERT(nb_labeled >= 0);
Q_ASSERT(nb_labeled <= nb_torrents);
updateStickyLabelCounters();
} }
void TransferListFiltersWidget::updateStickyLabelCounters() { void TransferListFiltersWidget::torrentAboutToBeDeleted(TorrentModelItem* torrentItem)
labelFilters->item(0)->setText(tr("All labels") + " ("+QString::number(nb_torrents)+")"); {
labelFilters->item(1)->setText(tr("Unlabeled") + " ("+QString::number(nb_torrents-nb_labeled)+")"); Q_ASSERT(torrentItem);
QString label = torrentItem->data(TorrentModelItem::TR_LABEL).toString();
if (!label.isEmpty()) {
// Update label counter
const int new_count = customLabels.value(label, 0) - 1;
customLabels.insert(label, new_count);
const int row = labelFilters->rowFromLabel(label);
Q_ASSERT(row >= 2);
labelFilters->item(row)->setText(label + " (" + QString::number(new_count) + ")");
--nb_labeled;
}
--nb_torrents;
qDebug("nb_torrents: %d, nb_labeled: %d", nb_torrents, nb_labeled);
Q_ASSERT(nb_torrents >= 0);
Q_ASSERT(nb_labeled >= 0);
Q_ASSERT(nb_labeled <= nb_torrents);
updateStickyLabelCounters();
}
void TransferListFiltersWidget::updateStickyLabelCounters()
{
labelFilters->item(0)->setText(tr("All labels") + " (" + QString::number(nb_torrents) + ")");
labelFilters->item(1)->setText(tr("Unlabeled") + " (" + QString::number(nb_torrents - nb_labeled) + ")");
} }

View File

@ -43,77 +43,80 @@ QT_END_NAMESPACE
class TransferListWidget; class TransferListWidget;
class TorrentModelItem; class TorrentModelItem;
class LabelFiltersList: public QListWidget { class LabelFiltersList: public QListWidget
Q_OBJECT {
Q_OBJECT
private: private:
QListWidgetItem *itemHover; QListWidgetItem * itemHover;
public: public:
LabelFiltersList(QWidget *parent); LabelFiltersList(QWidget *parent);
// Redefine addItem() to make sure the list stays sorted // Redefine addItem() to make sure the list stays sorted
void addItem(QListWidgetItem *it); void addItem(QListWidgetItem *it);
QString labelFromRow(int row) const; QString labelFromRow(int row) const;
int rowFromLabel(QString label) const; int rowFromLabel(QString label) const;
signals: signals:
void torrentDropped(int label_row); void torrentDropped(int label_row);
protected: protected:
void dragMoveEvent(QDragMoveEvent *event); void dragMoveEvent(QDragMoveEvent *event);
void dropEvent(QDropEvent *event); void dropEvent(QDropEvent *event);
void dragLeaveEvent(QDragLeaveEvent*); void dragLeaveEvent(QDragLeaveEvent*);
void setItemHover(bool hover); void setItemHover(bool hover);
}; };
class StatusFiltersWidget : public QListWidget { class StatusFiltersWidget: public QListWidget
Q_OBJECT {
Q_OBJECT
public: public:
StatusFiltersWidget(QWidget *parent); StatusFiltersWidget(QWidget *parent);
protected: protected:
QSize sizeHint() const; QSize sizeHint() const;
private: private:
bool m_shown; bool m_shown;
}; };
class TransferListFiltersWidget: public QFrame { class TransferListFiltersWidget: public QFrame
Q_OBJECT {
Q_OBJECT
private: private:
QHash<QString, int> customLabels; QHash<QString, int> customLabels;
StatusFiltersWidget* statusFilters; StatusFiltersWidget* statusFilters;
LabelFiltersList* labelFilters; LabelFiltersList* labelFilters;
QVBoxLayout* vLayout; QVBoxLayout* vLayout;
TransferListWidget *transferList; TransferListWidget *transferList;
int nb_labeled; int nb_labeled;
int nb_torrents; int nb_torrents;
public: public:
TransferListFiltersWidget(QWidget *parent, TransferListWidget *transferList); TransferListFiltersWidget(QWidget *parent, TransferListWidget *transferList);
~TransferListFiltersWidget(); ~TransferListFiltersWidget();
StatusFiltersWidget* getStatusFilters() const; StatusFiltersWidget* getStatusFilters() const;
void saveSettings() const; void saveSettings() const;
void loadSettings(); void loadSettings();
protected slots: protected slots:
void updateTorrentNumbers(); void updateTorrentNumbers();
void torrentDropped(int row); void torrentDropped(int row);
void addLabel(QString& label); void addLabel(QString& label);
void showLabelMenu(QPoint); void showLabelMenu(QPoint);
void removeSelectedLabel(); void removeSelectedLabel();
void removeUnusedLabels(); void removeUnusedLabels();
void applyLabelFilter(int row); void applyLabelFilter(int row);
void torrentChangedLabel(TorrentModelItem *torrentItem, QString old_label, QString new_label); void torrentChangedLabel(TorrentModelItem *torrentItem, QString old_label, QString new_label);
void handleNewTorrent(TorrentModelItem* torrentItem); void handleNewTorrent(TorrentModelItem* torrentItem);
void torrentAboutToBeDeleted(TorrentModelItem* torrentItem); void torrentAboutToBeDeleted(TorrentModelItem* torrentItem);
void updateStickyLabelCounters(); void updateStickyLabelCounters();
}; };
#endif // TRANSFERLISTFILTERSWIDGET_H #endif // TRANSFERLISTFILTERSWIDGET_H