- Label list is now alphabetically sorted

This commit is contained in:
Christophe Dumez 2010-01-09 23:24:50 +00:00
parent 1b1dde3fc8
commit b16c26b24c

View file

@ -59,6 +59,18 @@ public:
setAcceptDrops(true); setAcceptDrops(true);
} }
// Redefine addItem() to make sure the list stays sorted
void 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);
}
signals: signals:
void torrentDropped(int label_row); void torrentDropped(int label_row);
@ -121,8 +133,7 @@ class TransferListFiltersWidget: public QFrame {
Q_OBJECT Q_OBJECT
private: private:
QStringList customLabels; QHash<QString, int> customLabels;
QList<int> labelCounters;
QListWidget* statusFilters; QListWidget* statusFilters;
LabelFiltersList* labelFilters; LabelFiltersList* labelFilters;
QVBoxLayout* vLayout; QVBoxLayout* vLayout;
@ -201,29 +212,45 @@ public:
settings.beginGroup(QString::fromUtf8("TransferListFilters")); settings.beginGroup(QString::fromUtf8("TransferListFilters"));
settings.setValue("selectedFilterIndex", QVariant(statusFilters->currentRow())); settings.setValue("selectedFilterIndex", QVariant(statusFilters->currentRow()));
//settings.setValue("selectedLabelIndex", QVariant(labelFilters->currentRow())); //settings.setValue("selectedLabelIndex", QVariant(labelFilters->currentRow()));
settings.setValue("customLabels", customLabels); settings.setValue("customLabels", QVariant(customLabels.keys()));
} }
void saveCustomLabels() const { void saveCustomLabels() const {
QSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent")); QSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent"));
settings.beginGroup(QString::fromUtf8("TransferListFilters")); settings.beginGroup(QString::fromUtf8("TransferListFilters"));
settings.setValue("customLabels", customLabels); settings.setValue("customLabels", QVariant(customLabels.keys()));
} }
void loadSettings() { void loadSettings() {
QSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent")); QSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent"));
settings.beginGroup(QString::fromUtf8("TransferListFilters")); settings.beginGroup(QString::fromUtf8("TransferListFilters"));
statusFilters->setCurrentRow(settings.value("selectedFilterIndex", 0).toInt()); statusFilters->setCurrentRow(settings.value("selectedFilterIndex", 0).toInt());
customLabels = settings.value("customLabels", QStringList()).toStringList(); QStringList label_list = settings.value("customLabels", QStringList()).toStringList();
for(int i=0; i<customLabels.size(); ++i) { foreach(const QString &label, label_list) {
labelCounters << 0; customLabels.insert(label, 0);
} qDebug("Creating label QListWidgetItem: %s", label.toLocal8Bit().data());
foreach(const QString& label, customLabels) { QListWidgetItem *newLabel = new QListWidgetItem();
QListWidgetItem *newLabel = new QListWidgetItem(labelFilters);
newLabel->setText(label + " (0)"); newLabel->setText(label + " (0)");
newLabel->setData(Qt::DecorationRole, QIcon(":/Icons/oxygen/folder.png")); newLabel->setData(Qt::DecorationRole, QIcon(":/Icons/oxygen/folder.png"));
labelFilters->addItem(newLabel);
} }
}
QString labelFromRow(int row) const {
Q_ASSERT(row > 1);
QString label = labelFilters->item(row)->text();
QStringList parts = label.split(" ");
Q_ASSERT(parts.size() >= 2);
parts.removeLast(); // Remove trailing number
return parts.join(" ");
}
int rowFromLabel(QString label) const {
Q_ASSERT(!label.isEmpty());
for(int i=2; i<labelFilters->count(); ++i) {
if(label == labelFromRow(i)) return i;
}
return -1;
} }
protected slots: protected slots:
@ -240,19 +267,18 @@ protected slots:
if(row == 1) { if(row == 1) {
transferList->setSelectionLabel(""); transferList->setSelectionLabel("");
} else { } else {
QString label = customLabels.at(row-2); transferList->setSelectionLabel(labelFromRow(row));
transferList->setSelectionLabel(label);
} }
} }
void addLabel(QString label) { void addLabel(QString label) {
label = misc::toValidFileSystemName(label.trimmed()); label = misc::toValidFileSystemName(label.trimmed());
if(label.isEmpty() || customLabels.contains(label)) return; if(label.isEmpty() || customLabels.contains(label)) return;
QListWidgetItem *newLabel = new QListWidgetItem(labelFilters); QListWidgetItem *newLabel = new QListWidgetItem();
newLabel->setText(label + " (0)"); newLabel->setText(label + " (0)");
newLabel->setData(Qt::DecorationRole, QIcon(":/Icons/oxygen/folder.png")); newLabel->setData(Qt::DecorationRole, QIcon(":/Icons/oxygen/folder.png"));
customLabels << label; labelFilters->addItem(newLabel);
labelCounters << 0; customLabels.insert(label, 0);
saveCustomLabels(); saveCustomLabels();
} }
@ -284,7 +310,7 @@ protected slots:
invalid = true; invalid = true;
} }
} }
}while(invalid); } while(invalid);
return; return;
} }
} }
@ -293,8 +319,9 @@ protected slots:
void removeSelectedLabel() { void removeSelectedLabel() {
int row = labelFilters->row(labelFilters->selectedItems().first()); int row = labelFilters->row(labelFilters->selectedItems().first());
Q_ASSERT(row > 1); Q_ASSERT(row > 1);
QString label = customLabels.takeAt(row - 2); QString label = labelFromRow(row);
labelCounters.removeAt(row-2); Q_ASSERT(customLabels.contains(label));
customLabels.remove(label);
transferList->removeLabelFromRows(label); transferList->removeLabelFromRows(label);
// Select first label // Select first label
labelFilters->setCurrentItem(labelFilters->item(0)); labelFilters->setCurrentItem(labelFilters->item(0));
@ -315,29 +342,32 @@ protected slots:
transferList->applyLabelFilter("none"); transferList->applyLabelFilter("none");
break; break;
default: default:
transferList->applyLabelFilter(customLabels.at(row-2)); transferList->applyLabelFilter(labelFromRow(row));
} }
} }
void torrentChangedLabel(QString old_label, QString new_label) { void torrentChangedLabel(QString old_label, QString new_label) {
qDebug("Torrent label changed from %s to %s", old_label.toLocal8Bit().data(), new_label.toLocal8Bit().data()); qDebug("Torrent label changed from %s to %s", old_label.toLocal8Bit().data(), new_label.toLocal8Bit().data());
if(!old_label.isEmpty()) { if(!old_label.isEmpty()) {
int i = customLabels.indexOf(old_label); if(customLabels.contains(old_label)) {
if(i >= 0) { int new_count = customLabels.value(old_label, 0) - 1;
int new_count = labelCounters[i]-1;
Q_ASSERT(new_count >= 0); Q_ASSERT(new_count >= 0);
labelCounters.replace(i, new_count); customLabels.insert(old_label, new_count);
labelFilters->item(i+2)->setText(old_label + " ("+ QString::number(new_count) +")"); int row = rowFromLabel(old_label);
Q_ASSERT(row >= 2);
labelFilters->item(row)->setText(old_label + " ("+ QString::number(new_count) +")");
} }
--nb_labeled; --nb_labeled;
} }
if(!new_label.isEmpty()) { if(!new_label.isEmpty()) {
if(!customLabels.contains(new_label)) if(!customLabels.contains(new_label))
addLabel(new_label); addLabel(new_label);
int i = customLabels.indexOf(new_label); int new_count = customLabels.value(new_label, 0) + 1;
int new_count = labelCounters[i]+1; Q_ASSERT(new_count >= 1);
labelCounters.replace(i, new_count); customLabels.insert(new_label, new_count);
labelFilters->item(i+2)->setText(new_label + " ("+ QString::number(new_count) +")"); int row = rowFromLabel(new_label);
Q_ASSERT(row >= 2);
labelFilters->item(row)->setText(new_label + " ("+ QString::number(new_count) +")");
++nb_labeled; ++nb_labeled;
} }
updateStickyLabelCounters(); updateStickyLabelCounters();
@ -353,12 +383,14 @@ protected slots:
addLabel(label); addLabel(label);
} }
// Update label counter // Update label counter
int i = customLabels.indexOf(label); Q_ASSERT(customLabels.contains(label));
Q_ASSERT(i >= 0); int new_count = customLabels.value(label, 0) + 1;
int new_count = labelCounters[i]+1; customLabels.insert(label, new_count);
labelCounters.replace(i, new_count); int row = rowFromLabel(label);
Q_ASSERT(labelFilters->item(i+2)); qDebug("torrentAdded, Row: %d", row);
labelFilters->item(i+2)->setText(label + " ("+ QString::number(new_count) +")"); Q_ASSERT(row >= 2);
Q_ASSERT(labelFilters->item(row));
labelFilters->item(row)->setText(label + " ("+ QString::number(new_count) +")");
++nb_labeled; ++nb_labeled;
} }
++nb_torrents; ++nb_torrents;
@ -374,11 +406,11 @@ protected slots:
QString label = transferList->getSourceModel()->index(index.row(), TR_LABEL).data(Qt::DisplayRole).toString().trimmed(); QString label = transferList->getSourceModel()->index(index.row(), TR_LABEL).data(Qt::DisplayRole).toString().trimmed();
if(!label.isEmpty()) { if(!label.isEmpty()) {
// Update label counter // Update label counter
int i = customLabels.indexOf(label); int new_count = customLabels.value(label, 0) - 1;
Q_ASSERT(i >= 0); customLabels.insert(label, new_count);
int new_count = labelCounters[i]-1; int row = rowFromLabel(label);
labelCounters.replace(i, new_count); Q_ASSERT(row >= 2);
labelFilters->item(i+2)->setText(label + " ("+ QString::number(new_count) +")"); labelFilters->item(row)->setText(label + " ("+ QString::number(new_count) +")");
--nb_labeled; --nb_labeled;
} }
--nb_torrents; --nb_torrents;