2010-07-30 22:31:31 +00:00
|
|
|
/*
|
2010-08-01 20:36:13 +00:00
|
|
|
* This file Copyright (C) Mnemosyne LLC
|
2010-07-30 22:31:31 +00:00
|
|
|
*
|
2010-08-01 20:36:13 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
2010-07-30 22:31:31 +00:00
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QString>
|
|
|
|
#include <QtGui>
|
|
|
|
|
|
|
|
#include "app.h"
|
|
|
|
#include "favicon.h"
|
|
|
|
#include "filters.h"
|
|
|
|
#include "filterbar.h"
|
2010-08-01 19:56:42 +00:00
|
|
|
#include "hig.h"
|
2010-07-30 22:31:31 +00:00
|
|
|
#include "prefs.h"
|
|
|
|
#include "torrent-filter.h"
|
|
|
|
#include "torrent-model.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
/****
|
|
|
|
*****
|
|
|
|
***** DELEGATE
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
TorrentCountRole = Qt::UserRole + 1,
|
|
|
|
ActivityRole,
|
|
|
|
TrackerRole
|
|
|
|
};
|
|
|
|
|
2010-07-31 06:55:26 +00:00
|
|
|
namespace
|
|
|
|
{
|
2013-01-26 01:19:54 +00:00
|
|
|
int getHSpacing (QWidget * w)
|
|
|
|
{
|
|
|
|
return qMax (int (HIG::PAD_SMALL), w->style ()->pixelMetric (QStyle::PM_LayoutHorizontalSpacing, 0, w));
|
|
|
|
}
|
2010-07-31 06:55:26 +00:00
|
|
|
}
|
|
|
|
|
2013-01-26 01:19:54 +00:00
|
|
|
FilterBarComboBoxDelegate :: FilterBarComboBoxDelegate (QObject * parent, QComboBox * combo):
|
|
|
|
QItemDelegate (parent),
|
|
|
|
myCombo (combo)
|
2010-07-30 22:31:31 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-01-26 01:19:54 +00:00
|
|
|
FilterBarComboBoxDelegate :: isSeparator (const QModelIndex &index)
|
2010-07-30 22:31:31 +00:00
|
|
|
{
|
2013-01-26 01:19:54 +00:00
|
|
|
return index.data (Qt::AccessibleDescriptionRole).toString () == QLatin1String ("separator");
|
2010-07-30 22:31:31 +00:00
|
|
|
}
|
|
|
|
void
|
2013-01-26 01:19:54 +00:00
|
|
|
FilterBarComboBoxDelegate :: setSeparator (QAbstractItemModel * model, const QModelIndex& index)
|
2010-07-30 22:31:31 +00:00
|
|
|
{
|
2013-01-26 01:19:54 +00:00
|
|
|
model->setData (index, QString::fromLatin1 ("separator"), Qt::AccessibleDescriptionRole);
|
2010-07-30 22:31:31 +00:00
|
|
|
|
2013-01-26 01:19:54 +00:00
|
|
|
if (QStandardItemModel *m = qobject_cast<QStandardItemModel*> (model))
|
|
|
|
if (QStandardItem *item = m->itemFromIndex (index))
|
|
|
|
item->setFlags (item->flags () & ~ (Qt::ItemIsSelectable|Qt::ItemIsEnabled));
|
2010-07-30 22:31:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-26 01:19:54 +00:00
|
|
|
FilterBarComboBoxDelegate :: paint (QPainter * painter,
|
2010-07-30 22:31:31 +00:00
|
|
|
const QStyleOptionViewItem & option,
|
2013-01-26 01:19:54 +00:00
|
|
|
const QModelIndex & index) const
|
2010-07-30 22:31:31 +00:00
|
|
|
{
|
2013-01-26 01:19:54 +00:00
|
|
|
if (isSeparator (index))
|
2010-07-30 22:31:31 +00:00
|
|
|
{
|
2013-01-26 01:19:54 +00:00
|
|
|
QRect rect = option.rect;
|
|
|
|
if (const QStyleOptionViewItemV3 *v3 = qstyleoption_cast<const QStyleOptionViewItemV3*> (&option))
|
|
|
|
if (const QAbstractItemView *view = qobject_cast<const QAbstractItemView*> (v3->widget))
|
|
|
|
rect.setWidth (view->viewport ()->width ());
|
|
|
|
QStyleOption opt;
|
|
|
|
opt.rect = rect;
|
|
|
|
myCombo->style ()->drawPrimitive (QStyle::PE_IndicatorToolBarSeparator, &opt, painter, myCombo);
|
2010-07-30 22:31:31 +00:00
|
|
|
}
|
2013-01-26 01:19:54 +00:00
|
|
|
else
|
2010-07-30 22:31:31 +00:00
|
|
|
{
|
2013-01-26 01:19:54 +00:00
|
|
|
QStyleOptionViewItem disabledOption = option;
|
|
|
|
disabledOption.state &= ~ (QStyle::State_Enabled | QStyle::State_Selected);
|
|
|
|
QRect boundingBox = option.rect;
|
|
|
|
|
|
|
|
const int hmargin = getHSpacing (myCombo);
|
|
|
|
boundingBox.setLeft (boundingBox.left () + hmargin);
|
|
|
|
boundingBox.setRight (boundingBox.right () - hmargin);
|
|
|
|
|
|
|
|
QRect decorationRect = rect (option, index, Qt::DecorationRole);
|
|
|
|
decorationRect.moveLeft (decorationRect.left ());
|
|
|
|
decorationRect.setSize (myCombo->iconSize ());
|
|
|
|
decorationRect = QStyle::alignedRect (Qt::LeftToRight,
|
|
|
|
Qt::AlignLeft|Qt::AlignVCenter,
|
|
|
|
decorationRect.size (), boundingBox);
|
|
|
|
boundingBox.setLeft (decorationRect.right () + hmargin);
|
|
|
|
|
|
|
|
QRect countRect = rect (option, index, TorrentCountRole);
|
|
|
|
countRect = QStyle::alignedRect (Qt::LeftToRight,
|
|
|
|
Qt::AlignRight|Qt::AlignVCenter,
|
|
|
|
countRect.size (), boundingBox);
|
|
|
|
boundingBox.setRight (countRect.left () - hmargin);
|
|
|
|
const QRect displayRect = boundingBox;
|
|
|
|
|
|
|
|
drawBackground (painter, option, index);
|
|
|
|
QStyleOptionViewItem option2 = option;
|
|
|
|
option2.decorationSize = myCombo->iconSize ();
|
|
|
|
drawDecoration (painter, option, decorationRect, decoration (option2,index.data (Qt::DecorationRole)));
|
|
|
|
drawDisplay (painter, option, displayRect, index.data (Qt::DisplayRole).toString ());
|
|
|
|
drawDisplay (painter, disabledOption, countRect, index.data (TorrentCountRole).toString ());
|
|
|
|
drawFocus (painter, option, displayRect|countRect);
|
2010-07-30 22:31:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize
|
2013-01-26 01:19:54 +00:00
|
|
|
FilterBarComboBoxDelegate :: sizeHint (const QStyleOptionViewItem & option,
|
|
|
|
const QModelIndex & index) const
|
2010-07-30 22:31:31 +00:00
|
|
|
{
|
2013-01-26 01:19:54 +00:00
|
|
|
if (isSeparator (index))
|
2010-07-30 22:31:31 +00:00
|
|
|
{
|
2013-01-26 01:19:54 +00:00
|
|
|
const int pm = myCombo->style ()->pixelMetric (QStyle::PM_DefaultFrameWidth, 0, myCombo);
|
|
|
|
return QSize (pm, pm + 10);
|
2010-07-30 22:31:31 +00:00
|
|
|
}
|
2013-01-26 01:19:54 +00:00
|
|
|
else
|
2010-07-30 22:31:31 +00:00
|
|
|
{
|
2013-01-26 01:19:54 +00:00
|
|
|
QStyle * s = myCombo->style ();
|
|
|
|
const int hmargin = getHSpacing (myCombo);
|
|
|
|
|
|
|
|
QSize size = QItemDelegate::sizeHint (option, index);
|
|
|
|
size.setHeight (qMax (size.height (), myCombo->iconSize ().height () + 6));
|
|
|
|
size.rwidth () += s->pixelMetric (QStyle::PM_FocusFrameHMargin, 0, myCombo);
|
|
|
|
size.rwidth () += rect (option,index,TorrentCountRole).width ();
|
|
|
|
size.rwidth () += hmargin * 4;
|
|
|
|
return size;
|
2010-07-30 22:31:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2013-01-26 01:19:54 +00:00
|
|
|
FilterBarComboBox :: FilterBarComboBox (QWidget * parent):
|
|
|
|
QComboBox (parent)
|
2010-07-30 22:31:31 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-26 01:19:54 +00:00
|
|
|
FilterBarComboBox :: paintEvent (QPaintEvent * e)
|
2010-07-30 22:31:31 +00:00
|
|
|
{
|
2013-01-26 01:19:54 +00:00
|
|
|
Q_UNUSED (e);
|
2010-07-30 22:31:31 +00:00
|
|
|
|
2013-01-26 01:19:54 +00:00
|
|
|
QStylePainter painter (this);
|
|
|
|
painter.setPen (palette ().color (QPalette::Text));
|
2010-07-30 22:31:31 +00:00
|
|
|
|
2013-01-26 01:19:54 +00:00
|
|
|
// draw the combobox frame, focusrect and selected etc.
|
|
|
|
QStyleOptionComboBox opt;
|
|
|
|
initStyleOption (&opt);
|
|
|
|
painter.drawComplexControl (QStyle::CC_ComboBox, opt);
|
2010-07-30 22:31:31 +00:00
|
|
|
|
2013-01-26 01:19:54 +00:00
|
|
|
// draw the icon and text
|
|
|
|
const QModelIndex modelIndex = model ()->index (currentIndex (), 0, rootModelIndex ());
|
|
|
|
if (modelIndex.isValid ())
|
2010-07-30 22:31:31 +00:00
|
|
|
{
|
2013-01-26 01:19:54 +00:00
|
|
|
QStyle * s = style ();
|
|
|
|
QRect rect = s->subControlRect (QStyle::CC_ComboBox, &opt, QStyle::SC_ComboBoxEditField, this);
|
|
|
|
const int hmargin = getHSpacing (this);
|
|
|
|
rect.setRight (rect.right () - hmargin);
|
|
|
|
|
|
|
|
// draw the icon
|
|
|
|
QPixmap pixmap;
|
|
|
|
QVariant variant = modelIndex.data (Qt::DecorationRole);
|
|
|
|
switch (variant.type ())
|
|
|
|
{
|
|
|
|
case QVariant::Pixmap: pixmap = qvariant_cast<QPixmap> (variant); break;
|
|
|
|
case QVariant::Icon: pixmap = qvariant_cast<QIcon> (variant).pixmap (iconSize ()); break;
|
|
|
|
default: break;
|
2010-07-30 22:31:31 +00:00
|
|
|
}
|
2013-01-26 01:19:54 +00:00
|
|
|
if (!pixmap.isNull ())
|
|
|
|
{
|
|
|
|
s->drawItemPixmap (&painter, rect, Qt::AlignLeft|Qt::AlignVCenter, pixmap);
|
|
|
|
rect.setLeft (rect.left () + pixmap.width () + hmargin);
|
2010-07-30 22:31:31 +00:00
|
|
|
}
|
|
|
|
|
2013-01-26 01:19:54 +00:00
|
|
|
// draw the count
|
|
|
|
QString text = modelIndex.data (TorrentCountRole).toString ();
|
|
|
|
if (!text.isEmpty ())
|
2010-11-14 05:03:38 +00:00
|
|
|
{
|
2013-01-26 01:19:54 +00:00
|
|
|
const QPen pen = painter.pen ();
|
|
|
|
painter.setPen (opt.palette.color (QPalette::Disabled, QPalette::Text));
|
|
|
|
QRect r = s->itemTextRect (painter.fontMetrics (), rect, Qt::AlignRight|Qt::AlignVCenter, false, text);
|
|
|
|
painter.drawText (r, 0, text);
|
|
|
|
rect.setRight (r.left () - hmargin);
|
|
|
|
painter.setPen (pen);
|
2010-07-30 22:31:31 +00:00
|
|
|
}
|
|
|
|
|
2013-01-26 01:19:54 +00:00
|
|
|
// draw the text
|
|
|
|
text = modelIndex.data (Qt::DisplayRole).toString ();
|
|
|
|
text = painter.fontMetrics ().elidedText (text, Qt::ElideRight, rect.width ());
|
|
|
|
s->drawItemText (&painter, rect, Qt::AlignLeft|Qt::AlignVCenter, opt.palette, true, text);
|
2010-07-30 22:31:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****
|
|
|
|
*****
|
|
|
|
***** ACTIVITY
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
|
|
|
QComboBox*
|
2013-01-26 01:19:54 +00:00
|
|
|
FilterBar :: createActivityCombo ()
|
2010-07-30 22:31:31 +00:00
|
|
|
{
|
2013-01-26 01:19:54 +00:00
|
|
|
QComboBox * c = new FilterBarComboBox (this);
|
|
|
|
FilterBarComboBoxDelegate * delegate = new FilterBarComboBoxDelegate (0, c);
|
|
|
|
c->setItemDelegate (delegate);
|
2010-07-30 22:31:31 +00:00
|
|
|
|
2013-01-26 01:19:54 +00:00
|
|
|
QPixmap blankPixmap (c->iconSize ());
|
|
|
|
blankPixmap.fill (Qt::transparent);
|
|
|
|
QIcon blankIcon (blankPixmap);
|
2010-07-30 22:31:31 +00:00
|
|
|
|
2013-01-26 01:19:54 +00:00
|
|
|
QStandardItemModel * model = new QStandardItemModel;
|
2010-07-30 22:31:31 +00:00
|
|
|
|
2013-01-26 01:19:54 +00:00
|
|
|
QStandardItem * row = new QStandardItem (tr ("All"));
|
|
|
|
row->setData (FilterMode::SHOW_ALL, ActivityRole);
|
|
|
|
model->appendRow (row);
|
2010-07-30 22:31:31 +00:00
|
|
|
|
2013-01-26 01:19:54 +00:00
|
|
|
model->appendRow (new QStandardItem); // separator
|
|
|
|
delegate->setSeparator (model, model->index (1, 0));
|
2010-07-30 22:31:31 +00:00
|
|
|
|
2013-01-26 01:19:54 +00:00
|
|
|
row = new QStandardItem (QIcon::fromTheme ("system-run", blankIcon), tr ("Active"));
|
|
|
|
row->setData (FilterMode::SHOW_ACTIVE, ActivityRole);
|
|
|
|
model->appendRow (row);
|
2010-07-30 22:31:31 +00:00
|
|
|
|
2013-01-26 01:19:54 +00:00
|
|
|
row = new QStandardItem (QIcon::fromTheme ("go-down", blankIcon), tr ("Downloading"));
|
|
|
|
row->setData (FilterMode::SHOW_DOWNLOADING, ActivityRole);
|
|
|
|
model->appendRow (row);
|
2010-07-30 22:31:31 +00:00
|
|
|
|
2013-01-26 01:19:54 +00:00
|
|
|
row = new QStandardItem (QIcon::fromTheme ("go-up", blankIcon), tr ("Seeding"));
|
|
|
|
row->setData (FilterMode::SHOW_SEEDING, ActivityRole);
|
|
|
|
model->appendRow (row);
|
2010-07-30 22:31:31 +00:00
|
|
|
|
2013-01-26 01:19:54 +00:00
|
|
|
row = new QStandardItem (QIcon::fromTheme ("media-playback-pause", blankIcon), tr ("Paused"));
|
|
|
|
row->setData (FilterMode::SHOW_PAUSED, ActivityRole);
|
|
|
|
model->appendRow (row);
|
2010-07-30 22:31:31 +00:00
|
|
|
|
2013-01-26 01:19:54 +00:00
|
|
|
row = new QStandardItem (blankIcon, tr ("Finished"));
|
|
|
|
row->setData (FilterMode::SHOW_FINISHED, ActivityRole);
|
|
|
|
model->appendRow (row);
|
2010-08-01 17:43:35 +00:00
|
|
|
|
2013-01-26 01:19:54 +00:00
|
|
|
row = new QStandardItem (QIcon::fromTheme ("view-refresh", blankIcon), tr ("Verifying"));
|
|
|
|
row->setData (FilterMode::SHOW_VERIFYING, ActivityRole);
|
|
|
|
model->appendRow (row);
|
2010-07-30 22:31:31 +00:00
|
|
|
|
2013-01-26 01:19:54 +00:00
|
|
|
row = new QStandardItem (QIcon::fromTheme ("dialog-error", blankIcon), tr ("Error"));
|
|
|
|
row->setData (FilterMode::SHOW_ERROR, ActivityRole);
|
|
|
|
model->appendRow (row);
|
2010-07-30 22:31:31 +00:00
|
|
|
|
2013-01-26 01:19:54 +00:00
|
|
|
c->setModel (model);
|
|
|
|
return c;
|
2010-07-30 22:31:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/****
|
|
|
|
*****
|
2010-12-12 16:43:19 +00:00
|
|
|
*****
|
2010-07-30 22:31:31 +00:00
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2013-01-26 01:19:54 +00:00
|
|
|
QString readableHostName (const QString host)
|
|
|
|
{
|
|
|
|
// get the readable name...
|
|
|
|
QString name = host;
|
|
|
|
const int pos = name.lastIndexOf ('.');
|
|
|
|
if (pos >= 0)
|
|
|
|
name.truncate (pos);
|
|
|
|
if (!name.isEmpty ())
|
|
|
|
name[0] = name[0].toUpper ();
|
|
|
|
return name;
|
|
|
|
}
|
2010-07-30 22:31:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-26 01:19:54 +00:00
|
|
|
FilterBar :: refreshTrackers ()
|
2010-07-30 22:31:31 +00:00
|
|
|
{
|
2013-01-26 01:19:54 +00:00
|
|
|
Favicons& favicons = dynamic_cast<MyApp*> (QApplication::instance ())->favicons;
|
|
|
|
const int firstTrackerRow = 2; // skip over the "All" and separator...
|
|
|
|
|
|
|
|
// pull info from the tracker model...
|
|
|
|
QSet<QString> oldHosts;
|
|
|
|
for (int row=firstTrackerRow; ; ++row)
|
|
|
|
{
|
|
|
|
QModelIndex index = myTrackerModel->index (row, 0);
|
|
|
|
if (!index.isValid ())
|
|
|
|
break;
|
|
|
|
oldHosts << index.data (TrackerRole).toString ();
|
2010-07-30 22:31:31 +00:00
|
|
|
}
|
|
|
|
|
2013-01-26 01:19:54 +00:00
|
|
|
// pull the new stats from the torrent model...
|
|
|
|
QSet<QString> newHosts;
|
|
|
|
QMap<QString,int> torrentsPerHost;
|
|
|
|
for (int row=0; ; ++row)
|
2010-07-30 22:31:31 +00:00
|
|
|
{
|
2013-01-26 01:19:54 +00:00
|
|
|
QModelIndex index = myTorrents.index (row, 0);
|
|
|
|
if (!index.isValid ())
|
|
|
|
break;
|
|
|
|
const Torrent * tor = index.data (TorrentModel::TorrentRole).value<const Torrent*> ();
|
|
|
|
QSet<QString> torrentNames;
|
|
|
|
foreach (QString host, tor->hosts ())
|
|
|
|
{
|
2012-12-29 23:40:59 +00:00
|
|
|
newHosts.insert (host);
|
2013-01-26 01:19:54 +00:00
|
|
|
torrentNames.insert (readableHostName (host));
|
2010-07-30 22:31:31 +00:00
|
|
|
}
|
2013-01-26 01:19:54 +00:00
|
|
|
foreach (QString name, torrentNames)
|
|
|
|
++torrentsPerHost[name];
|
2010-07-30 22:31:31 +00:00
|
|
|
}
|
|
|
|
|
2013-01-26 01:19:54 +00:00
|
|
|
// update the "All" row
|
|
|
|
myTrackerModel->setData (myTrackerModel->index (0,0), getCountString (myTorrents.rowCount ()), TorrentCountRole);
|
2010-07-30 22:31:31 +00:00
|
|
|
|
2013-01-26 01:19:54 +00:00
|
|
|
// rows to update
|
|
|
|
foreach (QString host, oldHosts & newHosts)
|
2010-07-30 22:31:31 +00:00
|
|
|
{
|
2013-01-26 01:19:54 +00:00
|
|
|
const QString name = readableHostName (host);
|
|
|
|
QStandardItem * row = myTrackerModel->findItems (name).front ();
|
|
|
|
row->setData (getCountString (torrentsPerHost[name]), TorrentCountRole);
|
|
|
|
row->setData (favicons.findFromHost (host), Qt::DecorationRole);
|
2010-07-30 22:31:31 +00:00
|
|
|
}
|
|
|
|
|
2013-01-26 01:19:54 +00:00
|
|
|
// rows to remove
|
|
|
|
foreach (QString host, oldHosts - newHosts)
|
|
|
|
{
|
|
|
|
const QString name = readableHostName (host);
|
|
|
|
QStandardItem * item = myTrackerModel->findItems (name).front ();
|
|
|
|
if (!item->data (TrackerRole).toString ().isEmpty ()) // don't remove "All"
|
|
|
|
myTrackerModel->removeRows (item->row (), 1);
|
2010-07-30 22:31:31 +00:00
|
|
|
}
|
|
|
|
|
2013-01-26 01:19:54 +00:00
|
|
|
// rows to add
|
|
|
|
bool anyAdded = false;
|
|
|
|
foreach (QString host, newHosts - oldHosts)
|
2010-07-30 22:31:31 +00:00
|
|
|
{
|
2013-01-26 01:19:54 +00:00
|
|
|
const QString name = readableHostName (host);
|
2010-07-30 22:31:31 +00:00
|
|
|
|
2013-01-26 01:19:54 +00:00
|
|
|
if (!myTrackerModel->findItems (name).isEmpty ())
|
|
|
|
continue;
|
2011-07-13 17:43:25 +00:00
|
|
|
|
2013-01-26 01:19:54 +00:00
|
|
|
// find the sorted position to add this row
|
|
|
|
int i = firstTrackerRow;
|
|
|
|
for (int n=myTrackerModel->rowCount (); i<n; ++i)
|
|
|
|
{
|
|
|
|
const QString rowName = myTrackerModel->index (i,0).data (Qt::DisplayRole).toString ();
|
|
|
|
if (rowName >= name)
|
|
|
|
break;
|
2011-07-13 17:43:25 +00:00
|
|
|
}
|
2010-07-30 22:31:31 +00:00
|
|
|
|
2013-01-26 01:19:54 +00:00
|
|
|
// add the row
|
|
|
|
QStandardItem * row = new QStandardItem (favicons.findFromHost (host), name);
|
|
|
|
row->setData (getCountString (torrentsPerHost[host]), TorrentCountRole);
|
|
|
|
row->setData (favicons.findFromHost (host), Qt::DecorationRole);
|
|
|
|
row->setData (host, TrackerRole);
|
|
|
|
myTrackerModel->insertRow (i, row);
|
|
|
|
anyAdded = true;
|
2010-07-30 22:31:31 +00:00
|
|
|
}
|
|
|
|
|
2013-01-26 01:19:54 +00:00
|
|
|
if (anyAdded) // the one added might match our filter...
|
|
|
|
refreshPref (Prefs::FILTER_TRACKERS);
|
2010-07-30 22:31:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QComboBox*
|
2013-01-26 01:19:54 +00:00
|
|
|
FilterBar :: createTrackerCombo (QStandardItemModel * model)
|
2010-07-30 22:31:31 +00:00
|
|
|
{
|
2013-01-26 01:19:54 +00:00
|
|
|
QComboBox * c = new FilterBarComboBox (this);
|
|
|
|
FilterBarComboBoxDelegate * delegate = new FilterBarComboBoxDelegate (0, c);
|
|
|
|
c->setItemDelegate (delegate);
|
2010-07-30 22:31:31 +00:00
|
|
|
|
2013-01-26 01:19:54 +00:00
|
|
|
QStandardItem * row = new QStandardItem (tr ("All"));
|
|
|
|
row->setData ("", TrackerRole);
|
|
|
|
row->setData (getCountString (myTorrents.rowCount ()), TorrentCountRole);
|
|
|
|
model->appendRow (row);
|
2010-07-30 22:31:31 +00:00
|
|
|
|
2013-01-26 01:19:54 +00:00
|
|
|
model->appendRow (new QStandardItem); // separator
|
|
|
|
delegate->setSeparator (model, model->index (1, 0));
|
2010-07-30 22:31:31 +00:00
|
|
|
|
2013-01-26 01:19:54 +00:00
|
|
|
c->setModel (model);
|
|
|
|
return c;
|
2010-07-30 22:31:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/****
|
|
|
|
*****
|
2010-12-12 16:43:19 +00:00
|
|
|
*****
|
2010-07-30 22:31:31 +00:00
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
2013-01-26 01:19:54 +00:00
|
|
|
FilterBar :: FilterBar (Prefs& prefs, TorrentModel& torrents, TorrentFilter& filter, QWidget * parent):
|
|
|
|
QWidget (parent),
|
|
|
|
myPrefs (prefs),
|
|
|
|
myTorrents (torrents),
|
|
|
|
myFilter (filter),
|
|
|
|
myRecountTimer (new QTimer (this)),
|
|
|
|
myIsBootstrapping (true)
|
2010-07-30 22:31:31 +00:00
|
|
|
{
|
2013-01-26 01:19:54 +00:00
|
|
|
QHBoxLayout * h = new QHBoxLayout (this);
|
|
|
|
const int hmargin = qMax (int (HIG::PAD), style ()->pixelMetric (QStyle::PM_LayoutHorizontalSpacing));
|
|
|
|
|
|
|
|
h->setSpacing (0);
|
|
|
|
h->setContentsMargins (2, 2, 2, 2);
|
|
|
|
h->addWidget (new QLabel (tr ("Show:"), this));
|
|
|
|
h->addSpacing (hmargin);
|
|
|
|
|
|
|
|
myActivityCombo = createActivityCombo ();
|
|
|
|
h->addWidget (myActivityCombo, 1);
|
|
|
|
h->addSpacing (hmargin);
|
|
|
|
|
|
|
|
myTrackerModel = new QStandardItemModel;
|
|
|
|
myTrackerCombo = createTrackerCombo (myTrackerModel);
|
|
|
|
h->addWidget (myTrackerCombo, 1);
|
|
|
|
h->addSpacing (hmargin*2);
|
|
|
|
|
|
|
|
myLineEdit = new QLineEdit (this);
|
|
|
|
h->addWidget (myLineEdit);
|
|
|
|
connect (myLineEdit, SIGNAL (textChanged (QString)), this, SLOT (onTextChanged (QString)));
|
|
|
|
|
|
|
|
QPushButton * p = new QPushButton;
|
|
|
|
QIcon icon = QIcon::fromTheme ("edit-clear", style ()->standardIcon (QStyle::SP_DialogCloseButton));
|
|
|
|
int iconSize = style ()->pixelMetric (QStyle::PM_SmallIconSize);
|
|
|
|
p->setIconSize (QSize (iconSize, iconSize));
|
|
|
|
p->setIcon (icon);
|
|
|
|
p->setFlat (true);
|
|
|
|
h->addWidget (p);
|
|
|
|
connect (p, SIGNAL (clicked (bool)), myLineEdit, SLOT (clear ()));
|
|
|
|
|
|
|
|
// listen for changes from the other players
|
|
|
|
connect (&myPrefs, SIGNAL (changed (int)), this, SLOT (refreshPref (int)));
|
|
|
|
connect (myActivityCombo, SIGNAL (currentIndexChanged (int)), this, SLOT (onActivityIndexChanged (int)));
|
|
|
|
connect (myTrackerCombo, SIGNAL (currentIndexChanged (int)), this, SLOT (onTrackerIndexChanged (int)));
|
|
|
|
connect (&myTorrents, SIGNAL (modelReset ()), this, SLOT (onTorrentModelReset ()));
|
|
|
|
connect (&myTorrents, SIGNAL (rowsInserted (const QModelIndex&,int,int)), this, SLOT (onTorrentModelRowsInserted (const QModelIndex&,int,int)));
|
|
|
|
connect (&myTorrents, SIGNAL (rowsRemoved (const QModelIndex&,int,int)), this, SLOT (onTorrentModelRowsRemoved (const QModelIndex&,int,int)));
|
|
|
|
connect (&myTorrents, SIGNAL (dataChanged (const QModelIndex&,const QModelIndex&)), this, SLOT (onTorrentModelDataChanged (const QModelIndex&,const QModelIndex&)));
|
|
|
|
connect (myRecountTimer, SIGNAL (timeout ()), this, SLOT (recount ()));
|
|
|
|
|
|
|
|
recountSoon ();
|
|
|
|
refreshTrackers ();
|
|
|
|
myIsBootstrapping = false;
|
|
|
|
|
|
|
|
// initialize our state
|
|
|
|
QList<int> initKeys;
|
|
|
|
initKeys << Prefs :: FILTER_MODE
|
|
|
|
<< Prefs :: FILTER_TRACKERS;
|
|
|
|
foreach (int key, initKeys)
|
|
|
|
refreshPref (key);
|
2010-07-30 22:31:31 +00:00
|
|
|
}
|
|
|
|
|
2013-01-26 01:19:54 +00:00
|
|
|
FilterBar :: ~FilterBar ()
|
2010-07-30 22:31:31 +00:00
|
|
|
{
|
2013-01-26 01:19:54 +00:00
|
|
|
delete myRecountTimer;
|
2010-07-30 22:31:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
void
|
2013-01-26 01:19:54 +00:00
|
|
|
FilterBar :: refreshPref (int key)
|
2010-07-30 22:31:31 +00:00
|
|
|
{
|
2013-01-26 01:19:54 +00:00
|
|
|
switch (key)
|
2010-07-30 22:31:31 +00:00
|
|
|
{
|
2013-01-26 01:19:54 +00:00
|
|
|
case Prefs :: FILTER_MODE:
|
|
|
|
{
|
|
|
|
const FilterMode m = myPrefs.get<FilterMode> (key);
|
|
|
|
QAbstractItemModel * model = myActivityCombo->model ();
|
|
|
|
QModelIndexList indices = model->match (model->index (0,0), ActivityRole, m.mode ());
|
|
|
|
myActivityCombo->setCurrentIndex (indices.isEmpty () ? 0 : indices.first ().row ());
|
|
|
|
break;
|
2010-07-30 22:31:31 +00:00
|
|
|
}
|
|
|
|
|
2013-01-26 01:19:54 +00:00
|
|
|
case Prefs :: FILTER_TRACKERS:
|
|
|
|
{
|
|
|
|
const QString tracker = myPrefs.getString (key);
|
|
|
|
const QString name = readableHostName (tracker);
|
|
|
|
QList<QStandardItem*> rows = myTrackerModel->findItems (name);
|
|
|
|
if (!rows.isEmpty ())
|
|
|
|
{
|
|
|
|
myTrackerCombo->setCurrentIndex (rows.front ()->row ());
|
2010-07-31 00:03:59 +00:00
|
|
|
}
|
2013-01-26 01:19:54 +00:00
|
|
|
else // hm, we don't seem to have this tracker anymore...
|
|
|
|
{
|
|
|
|
const bool isBootstrapping = myTrackerModel->rowCount () <= 2;
|
|
|
|
if (!isBootstrapping)
|
|
|
|
myPrefs.set (key, "");
|
|
|
|
}
|
|
|
|
break;
|
2010-07-30 22:31:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-26 01:19:54 +00:00
|
|
|
FilterBar :: onTextChanged (const QString& str)
|
2010-07-30 22:31:31 +00:00
|
|
|
{
|
2013-01-26 01:19:54 +00:00
|
|
|
if (!myIsBootstrapping)
|
|
|
|
myPrefs.set (Prefs::FILTER_TEXT, str.trimmed ());
|
2010-07-30 22:31:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-26 01:19:54 +00:00
|
|
|
FilterBar :: onTrackerIndexChanged (int i)
|
2010-07-30 22:31:31 +00:00
|
|
|
{
|
2013-01-26 01:19:54 +00:00
|
|
|
if (!myIsBootstrapping)
|
2010-07-31 00:03:59 +00:00
|
|
|
{
|
2013-01-26 01:19:54 +00:00
|
|
|
QString str;
|
|
|
|
const bool isTracker = !myTrackerCombo->itemData (i,TrackerRole).toString ().isEmpty ();
|
|
|
|
if (!isTracker) // show all
|
|
|
|
{
|
|
|
|
str = "";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
str = myTrackerCombo->itemData (i,TrackerRole).toString ();
|
|
|
|
const int pos = str.lastIndexOf ('.');
|
|
|
|
if (pos >= 0)
|
|
|
|
str.truncate (pos+1);
|
2011-07-13 17:43:25 +00:00
|
|
|
}
|
2013-01-26 01:19:54 +00:00
|
|
|
myPrefs.set (Prefs::FILTER_TRACKERS, str);
|
2010-07-31 00:03:59 +00:00
|
|
|
}
|
2010-07-30 22:31:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-26 01:19:54 +00:00
|
|
|
FilterBar :: onActivityIndexChanged (int i)
|
2010-07-30 22:31:31 +00:00
|
|
|
{
|
2013-01-26 01:19:54 +00:00
|
|
|
if (!myIsBootstrapping)
|
2010-07-30 22:31:31 +00:00
|
|
|
{
|
2013-01-26 01:19:54 +00:00
|
|
|
const FilterMode mode = myActivityCombo->itemData (i, ActivityRole).toInt ();
|
|
|
|
myPrefs.set (Prefs::FILTER_MODE, mode);
|
2010-07-30 22:31:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2013-01-26 01:19:54 +00:00
|
|
|
void FilterBar :: onTorrentModelReset () { recountSoon (); }
|
|
|
|
void FilterBar :: onTorrentModelRowsInserted (const QModelIndex&, int, int) { recountSoon (); }
|
|
|
|
void FilterBar :: onTorrentModelRowsRemoved (const QModelIndex&, int, int) { recountSoon (); }
|
|
|
|
void FilterBar :: onTorrentModelDataChanged (const QModelIndex&, const QModelIndex&) { recountSoon (); }
|
2010-07-30 22:31:31 +00:00
|
|
|
|
|
|
|
void
|
2013-01-26 01:19:54 +00:00
|
|
|
FilterBar :: recountSoon ()
|
2010-07-30 22:31:31 +00:00
|
|
|
{
|
2013-01-26 01:19:54 +00:00
|
|
|
if (!myRecountTimer->isActive ())
|
2010-07-30 22:31:31 +00:00
|
|
|
{
|
2013-01-26 01:19:54 +00:00
|
|
|
myRecountTimer->setSingleShot (true);
|
|
|
|
myRecountTimer->start (800);
|
2010-07-30 22:31:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
void
|
2012-12-29 01:20:22 +00:00
|
|
|
FilterBar :: recount ()
|
2010-07-30 22:31:31 +00:00
|
|
|
{
|
2013-01-26 01:19:54 +00:00
|
|
|
QAbstractItemModel * model = myActivityCombo->model ();
|
2012-12-29 01:20:22 +00:00
|
|
|
|
2012-12-30 01:28:28 +00:00
|
|
|
int torrentsPerMode[FilterMode::NUM_MODES] = { };
|
|
|
|
myFilter.countTorrentsPerMode (torrentsPerMode);
|
|
|
|
|
2013-01-26 01:19:54 +00:00
|
|
|
for (int row=0, n=model->rowCount (); row<n; ++row)
|
2010-07-30 22:31:31 +00:00
|
|
|
{
|
2012-12-29 01:20:22 +00:00
|
|
|
QModelIndex index = model->index (row, 0);
|
2013-01-26 01:19:54 +00:00
|
|
|
const int mode = index.data (ActivityRole).toInt ();
|
|
|
|
model->setData (index, getCountString (torrentsPerMode[mode]), TorrentCountRole);
|
2010-07-30 22:31:31 +00:00
|
|
|
}
|
|
|
|
|
2012-12-29 01:20:22 +00:00
|
|
|
refreshTrackers ();
|
2010-07-30 22:31:31 +00:00
|
|
|
}
|
2010-11-14 05:03:38 +00:00
|
|
|
|
|
|
|
QString
|
2013-01-26 01:19:54 +00:00
|
|
|
FilterBar :: getCountString (int n) const
|
2010-11-14 05:03:38 +00:00
|
|
|
{
|
2013-01-26 01:19:54 +00:00
|
|
|
return QString ("%L1").arg (n);
|
2010-11-14 05:03:38 +00:00
|
|
|
}
|