transmission/qt/file-tree.cc

933 lines
21 KiB
C++
Raw Normal View History

2009-04-09 18:55:47 +00:00
/*
* This file Copyright (C) Mnemosyne LLC
2009-04-09 18:55:47 +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
2009-04-09 18:55:47 +00:00
*
* $Id$
2009-04-09 18:55:47 +00:00
*/
#include <cassert>
#include <QApplication>
2009-10-06 00:27:26 +00:00
#include <QHeaderView>
2009-04-09 18:55:47 +00:00
#include <QPainter>
#include <QResizeEvent>
#include <QSortFilterProxyModel>
2009-04-09 18:55:47 +00:00
#include <QStringList>
#include <libtransmission/transmission.h> // priorities
#include "file-tree.h"
#include "formatter.h"
2009-04-09 18:55:47 +00:00
#include "hig.h"
#include "torrent.h" // FileList
#include "utils.h" // mime icons
enum
{
COL_NAME,
FIRST_VISIBLE_COLUMN = COL_NAME,
COL_PROGRESS,
COL_WANTED,
COL_PRIORITY,
LAST_VISIBLE_COLUMN = COL_PRIORITY,
COL_FILE_INDEX,
NUM_COLUMNS
2009-04-09 18:55:47 +00:00
};
/****
*****
****/
const QHash<QString,int>&
FileTreeItem :: getMyChildRows ()
{
const size_t n = childCount();
// ensure that all the rows are hashed
while (myFirstUnhashedRow < n)
{
2013-01-28 18:20:20 +00:00
myChildRows.insert (myChildren[myFirstUnhashedRow]->name(),
myFirstUnhashedRow);
++myFirstUnhashedRow;
}
return myChildRows;
}
FileTreeItem :: ~FileTreeItem ()
2009-04-09 18:55:47 +00:00
{
assert(myChildren.isEmpty());
2009-04-09 18:55:47 +00:00
if (myParent != 0)
{
const int pos = row();
assert ((pos>=0) && "couldn't find child in parent's lookup");
myParent->myChildren.removeAt(pos);
myParent->myChildRows.remove(name());
myParent->myFirstUnhashedRow = pos;
}
2009-04-09 18:55:47 +00:00
}
void
FileTreeItem :: appendChild (FileTreeItem * child)
2009-04-09 18:55:47 +00:00
{
const size_t n = childCount();
child->myParent = this;
myChildren.append (child);
myFirstUnhashedRow = n;
2009-04-09 18:55:47 +00:00
}
FileTreeItem *
FileTreeItem :: child (const QString& filename)
2009-04-09 18:55:47 +00:00
{
FileTreeItem * item(0);
const int row = getMyChildRows().value (filename, -1);
if (row != -1)
{
item = child (row);
assert (filename == item->name());
}
2009-04-09 18:55:47 +00:00
return item;
2009-04-09 18:55:47 +00:00
}
int
FileTreeItem :: row () const
2009-04-09 18:55:47 +00:00
{
int i(-1);
2009-04-09 18:55:47 +00:00
if(myParent)
{
i = myParent->getMyChildRows().value (name(), -1);
assert (this == myParent->myChildren[i]);
}
2009-04-09 18:55:47 +00:00
return i;
2009-04-09 18:55:47 +00:00
}
QVariant
FileTreeItem :: data (int column, int role) const
2009-04-09 18:55:47 +00:00
{
QVariant value;
2009-04-09 18:55:47 +00:00
if (column == COL_FILE_INDEX)
{
2013-01-28 18:20:20 +00:00
return myFileIndex;
}
else if (role == Qt::EditRole)
{
if (column == 0)
value.setValue (name());
}
else if (role == Qt::DisplayRole)
{
switch(column)
{
case COL_NAME:
value.setValue (fileSizeName());
break;
case COL_PROGRESS:
value.setValue (progress());
break;
case COL_WANTED:
value.setValue (isSubtreeWanted());
break;
case COL_PRIORITY:
value.setValue (priorityString());
break;
}
2009-04-09 18:55:47 +00:00
}
return value;
2009-04-09 18:55:47 +00:00
}
void
FileTreeItem :: getSubtreeSize (uint64_t& have, uint64_t& total) const
2009-04-09 18:55:47 +00:00
{
have += myHaveSize;
total += myTotalSize;
2009-04-09 18:55:47 +00:00
foreach(const FileTreeItem * i, myChildren)
i->getSubtreeSize(have, total);
2009-04-09 18:55:47 +00:00
}
double
FileTreeItem :: progress () const
2009-04-09 18:55:47 +00:00
{
double d(0);
uint64_t have(0), total(0);
getSubtreeSize(have, total);
if (total)
d = have / (double)total;
return d;
2009-04-09 18:55:47 +00:00
}
QString
FileTreeItem :: fileSizeName () const
{
uint64_t have(0), total(0);
QString str;
getSubtreeSize(have, total);
str = QString(name() + " (%1)").arg(Formatter::sizeToString(total));
return str;
}
#include <iostream>
2009-04-09 18:55:47 +00:00
bool
FileTreeItem :: update (const QString& name,
bool wanted,
int priority,
uint64_t haveSize,
bool updateFields)
2009-04-09 18:55:47 +00:00
{
bool changed = false;
2009-04-09 18:55:47 +00:00
if (myName != name)
2009-04-09 18:55:47 +00:00
{
if (myParent)
myParent->myFirstUnhashedRow = row();
myName = name;
changed = true;
2009-04-09 18:55:47 +00:00
}
if (fileIndex() != -1)
2009-04-09 18:55:47 +00:00
{
if (myHaveSize != haveSize)
{
myHaveSize = haveSize;
changed = true;
}
if (updateFields)
{
if (myIsWanted != wanted)
{
myIsWanted = wanted;
changed = true;
}
if (myPriority != priority)
{
myPriority = priority;
changed = true;
}
}
2009-04-09 18:55:47 +00:00
}
return changed;
2009-04-09 18:55:47 +00:00
}
QString
FileTreeItem :: priorityString () const
2009-04-09 18:55:47 +00:00
{
2013-01-28 18:20:20 +00:00
const int i = priority();
switch (i)
{
case LOW: return tr("Low");
case HIGH: return tr("High");
case NORMAL: return tr("Normal");
default: return tr("Mixed");
}
2009-04-09 18:55:47 +00:00
}
int
FileTreeItem :: priority () const
2009-04-09 18:55:47 +00:00
{
int i(0);
if (myChildren.isEmpty())
{
switch (myPriority)
{
case TR_PRI_LOW:
i |= LOW;
break;
case TR_PRI_HIGH:
i |= HIGH;
break;
2009-04-09 18:55:47 +00:00
default:
i |= NORMAL;
break;
}
2009-04-09 18:55:47 +00:00
}
foreach (const FileTreeItem * child, myChildren)
i |= child->priority();
2009-04-09 18:55:47 +00:00
return i;
2009-04-09 18:55:47 +00:00
}
void
FileTreeItem :: setSubtreePriority (int i, QSet<int>& ids)
2009-04-09 18:55:47 +00:00
{
if (myPriority != i)
{
myPriority = i;
2013-01-28 18:20:20 +00:00
if (myFileIndex >= 0)
ids.insert (myFileIndex);
2009-04-09 18:55:47 +00:00
}
foreach (FileTreeItem * child, myChildren)
child->setSubtreePriority (i, ids);
2009-04-09 18:55:47 +00:00
}
void
FileTreeItem :: twiddlePriority (QSet<int>& ids, int& p)
2009-04-09 18:55:47 +00:00
{
const int old(priority());
2009-04-09 18:55:47 +00:00
if (old & LOW)
p = TR_PRI_NORMAL;
else if (old & NORMAL)
p = TR_PRI_HIGH;
else
p = TR_PRI_LOW;
2009-04-09 18:55:47 +00:00
setSubtreePriority (p, ids);
2009-04-09 18:55:47 +00:00
}
int
FileTreeItem :: isSubtreeWanted () const
2009-04-09 18:55:47 +00:00
{
if(myChildren.isEmpty())
return myIsWanted ? Qt::Checked : Qt::Unchecked;
int wanted(-1);
foreach (const FileTreeItem * child, myChildren)
{
const int childWanted = child->isSubtreeWanted();
if (wanted == -1)
wanted = childWanted;
2009-04-09 18:55:47 +00:00
if (wanted != childWanted)
wanted = Qt::PartiallyChecked;
if (wanted == Qt::PartiallyChecked)
return wanted;
2009-04-09 18:55:47 +00:00
}
return wanted;
2009-04-09 18:55:47 +00:00
}
void
FileTreeItem :: setSubtreeWanted (bool b, QSet<int>& ids)
2009-04-09 18:55:47 +00:00
{
if (myIsWanted != b)
{
myIsWanted = b;
2013-01-28 18:20:20 +00:00
if (myFileIndex >= 0)
ids.insert(myFileIndex);
2009-04-09 18:55:47 +00:00
}
foreach (FileTreeItem * child, myChildren)
child->setSubtreeWanted (b, ids);
2009-04-09 18:55:47 +00:00
}
void
FileTreeItem :: twiddleWanted (QSet<int>& ids, bool& wanted)
2009-04-09 18:55:47 +00:00
{
wanted = isSubtreeWanted() != Qt::Checked;
setSubtreeWanted (wanted, ids);
2009-04-09 18:55:47 +00:00
}
/***
****
****
***/
FileTreeModel :: FileTreeModel (QObject *parent, bool isEditable):
QAbstractItemModel(parent),
myRootItem (new FileTreeItem),
myIsEditable (isEditable)
2009-04-09 18:55:47 +00:00
{
}
FileTreeModel :: ~FileTreeModel()
2009-04-09 18:55:47 +00:00
{
clear();
2009-04-09 18:55:47 +00:00
delete myRootItem;
2009-04-09 18:55:47 +00:00
}
2013-01-28 18:20:20 +00:00
FileTreeItem *
FileTreeModel :: itemFromIndex (const QModelIndex& index) const
{
return static_cast<FileTreeItem*>(index.internalPointer());
}
2009-04-09 18:55:47 +00:00
QVariant
FileTreeModel :: data (const QModelIndex &index, int role) const
2009-04-09 18:55:47 +00:00
{
QVariant value;
2009-04-09 18:55:47 +00:00
if (index.isValid())
2013-01-28 18:20:20 +00:00
value = itemFromIndex(index)->data (index.column(), role);
2009-04-09 18:55:47 +00:00
return value;
2009-04-09 18:55:47 +00:00
}
Qt::ItemFlags
FileTreeModel :: flags (const QModelIndex& index) const
2009-04-09 18:55:47 +00:00
{
int i(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
2009-04-09 18:55:47 +00:00
if(myIsEditable && (index.column() == COL_NAME))
i |= Qt::ItemIsEditable;
if(index.column() == COL_WANTED)
i |= Qt::ItemIsUserCheckable | Qt::ItemIsTristate;
2009-04-09 18:55:47 +00:00
return (Qt::ItemFlags)i;
2009-04-09 18:55:47 +00:00
}
bool
FileTreeModel :: setData (const QModelIndex& index, const QVariant& newname, int role)
{
if (role == Qt::EditRole)
{
QString oldpath;
QModelIndex walk = index;
2013-01-28 18:20:20 +00:00
FileTreeItem * item = itemFromIndex (index);
while (item && !item->name().isEmpty())
{
if (oldpath.isEmpty())
oldpath = item->name();
else
oldpath = item->name() + "/" + oldpath;
item = item->parent ();
}
emit pathEdited (oldpath, newname.toString());
}
return false; // don't update the view until the session confirms the change
}
2009-04-09 18:55:47 +00:00
QVariant
FileTreeModel :: headerData (int column, Qt::Orientation orientation, int role) const
2009-04-09 18:55:47 +00:00
{
QVariant data;
if (orientation==Qt::Horizontal && role==Qt::DisplayRole)
{
switch (column)
{
case COL_NAME:
data.setValue (tr("File"));
break;
case COL_PROGRESS:
data.setValue (tr("Progress"));
break;
case COL_WANTED:
data.setValue (tr("Download"));
break;
case COL_PRIORITY:
data.setValue (tr("Priority"));
break;
2009-04-09 18:55:47 +00:00
default:
break;
2009-04-09 18:55:47 +00:00
}
}
return data;
2009-04-09 18:55:47 +00:00
}
QModelIndex
FileTreeModel :: index (int row, int column, const QModelIndex& parent) const
2009-04-09 18:55:47 +00:00
{
QModelIndex i;
2009-04-09 18:55:47 +00:00
2013-01-30 00:23:32 +00:00
if (hasIndex (row, column, parent))
2009-04-09 18:55:47 +00:00
{
FileTreeItem * parentItem;
2009-04-09 18:55:47 +00:00
2013-01-30 00:23:32 +00:00
if (!parent.isValid ())
parentItem = myRootItem;
else
2013-01-28 18:20:20 +00:00
parentItem = itemFromIndex (parent);
2009-04-09 18:55:47 +00:00
2013-01-30 00:23:32 +00:00
FileTreeItem * childItem = parentItem->child (row);
2009-04-09 18:55:47 +00:00
if (childItem)
2013-01-30 00:23:32 +00:00
i = createIndex (row, column, childItem);
2009-04-09 18:55:47 +00:00
}
2013-01-30 00:23:32 +00:00
return i;
2009-04-09 18:55:47 +00:00
}
QModelIndex
FileTreeModel :: parent (const QModelIndex& child) const
2009-04-09 18:55:47 +00:00
{
return parent (child, 0); // QAbstractItemModel::parent() wants col 0
2009-04-09 18:55:47 +00:00
}
QModelIndex
FileTreeModel :: parent (const QModelIndex& child, int column) const
2009-04-09 18:55:47 +00:00
{
2013-01-28 18:20:20 +00:00
QModelIndex parent;
2009-04-09 18:55:47 +00:00
2013-01-28 18:20:20 +00:00
if (child.isValid())
parent = indexOf (itemFromIndex(child)->parent(), column);
2009-04-09 18:55:47 +00:00
2013-01-28 18:20:20 +00:00
return parent;
2009-04-09 18:55:47 +00:00
}
int
FileTreeModel :: rowCount (const QModelIndex& parent) const
2009-04-09 18:55:47 +00:00
{
FileTreeItem * parentItem;
2009-04-09 18:55:47 +00:00
2013-01-28 18:20:20 +00:00
if (parent.isValid())
parentItem = itemFromIndex (parent);
else
2013-01-28 18:20:20 +00:00
parentItem = myRootItem;
2009-04-09 18:55:47 +00:00
return parentItem->childCount();
2009-04-09 18:55:47 +00:00
}
int
FileTreeModel :: columnCount (const QModelIndex &parent) const
2009-04-09 18:55:47 +00:00
{
Q_UNUSED(parent);
2009-04-09 18:55:47 +00:00
return NUM_COLUMNS;
2009-04-09 18:55:47 +00:00
}
QModelIndex
FileTreeModel :: indexOf (FileTreeItem * item, int column) const
2009-04-09 18:55:47 +00:00
{
if (!item || item==myRootItem)
return QModelIndex();
2009-04-09 18:55:47 +00:00
return createIndex(item->row(), column, item);
2009-04-09 18:55:47 +00:00
}
void
FileTreeModel :: clearSubtree (const QModelIndex& top)
2009-04-09 18:55:47 +00:00
{
size_t i = rowCount (top);
while (i > 0)
clearSubtree(index(--i, 0, top));
2009-04-09 18:55:47 +00:00
2013-01-28 18:20:20 +00:00
delete static_cast<FileTreeItem*>(itemFromIndex(top));
2009-04-09 18:55:47 +00:00
}
void
FileTreeModel :: clear ()
2009-04-09 18:55:47 +00:00
{
clearSubtree (QModelIndex());
2009-04-09 18:55:47 +00:00
reset ();
2009-04-09 18:55:47 +00:00
}
FileTreeItem *
FileTreeModel :: findItemForFileIndex (int fileIndex) const
{
FileTreeItem * ret = 0;
QModelIndexList indices = match (index (0,COL_FILE_INDEX),
Qt::DisplayRole,
fileIndex,
1,
Qt::MatchFlags (Qt::MatchExactly | Qt::MatchRecursive));
if (!indices.isEmpty ())
{
QModelIndex& index = indices.front ();
if (index.isValid())
2013-01-28 18:20:20 +00:00
ret = itemFromIndex (index);
}
return ret;
}
void
FileTreeModel :: itemChanged (FileTreeItem * item)
{
dataChanged (indexOf(item, FIRST_VISIBLE_COLUMN), indexOf(item, LAST_VISIBLE_COLUMN));
}
2009-04-09 18:55:47 +00:00
void
FileTreeModel :: addFile (int fileIndex,
2009-04-09 18:55:47 +00:00
const QString & filename,
bool wanted,
int priority,
uint64_t totalSize,
2009-04-09 18:55:47 +00:00
uint64_t have,
QList<QModelIndex> & rowsAdded,
bool updateFields)
2009-04-09 18:55:47 +00:00
{
bool added = false;
FileTreeItem * item;
QStringList tokens = filename.split (QChar::fromAscii('/'));
item = findItemForFileIndex (fileIndex);
2009-04-09 18:55:47 +00:00
if (item) // this file is already in the tree, we've added this
2009-04-09 18:55:47 +00:00
{
while (!tokens.isEmpty())
2009-04-09 18:55:47 +00:00
{
const QString token = tokens.takeLast();
if (item->update (token, wanted, priority, have, updateFields))
itemChanged (item);
item = item->parent();
2009-04-09 18:55:47 +00:00
}
assert (item == myRootItem);
2009-04-09 18:55:47 +00:00
}
else // we haven't build the FileTreeItems for these tokens yet
{
item = myRootItem;
while (!tokens.isEmpty())
{
const QString token = tokens.takeFirst();
FileTreeItem * child(item->child(token));
if (!child)
{
added = true;
QModelIndex parentIndex (indexOf(item, 0));
const int n (item->childCount());
beginInsertRows (parentIndex, n, n);
if (tokens.isEmpty())
child = new FileTreeItem (token, fileIndex, totalSize);
else
child = new FileTreeItem (token);
item->appendChild (child);
endInsertRows ();
rowsAdded.append (indexOf(child, 0));
}
item = child;
}
if (item != myRootItem)
{
assert (item->fileIndex() == fileIndex);
assert (item->totalSize() == totalSize);
if (item->update (item->name(), wanted, priority, have, added || updateFields))
itemChanged (item);
}
}
2009-04-09 18:55:47 +00:00
}
void
FileTreeModel :: parentsChanged (const QModelIndex& index, int column)
2009-04-09 18:55:47 +00:00
{
QModelIndex walk = index;
2009-04-09 18:55:47 +00:00
for (;;)
{
walk = parent(walk, column);
if(!walk.isValid())
break;
dataChanged(walk, walk);
2009-04-09 18:55:47 +00:00
}
}
void
FileTreeModel :: subtreeChanged (const QModelIndex& index, int column)
2009-04-09 18:55:47 +00:00
{
const int childCount = rowCount (index);
if (!childCount)
return;
2009-04-09 18:55:47 +00:00
// tell everyone that this tier changed
dataChanged (index.child(0,column), index.child(childCount-1,column));
2009-04-09 18:55:47 +00:00
// walk the subtiers
for (int i=0; i<childCount; ++i)
subtreeChanged (index.child(i,column), column);
2009-04-09 18:55:47 +00:00
}
void
FileTreeModel :: clicked (const QModelIndex& index)
2009-04-09 18:55:47 +00:00
{
const int column (index.column());
2009-04-09 18:55:47 +00:00
if (!index.isValid())
return;
2009-04-09 18:55:47 +00:00
if (column == COL_WANTED)
2009-04-09 18:55:47 +00:00
{
bool want;
QSet<int> file_ids;
FileTreeItem * item;
2009-04-09 18:55:47 +00:00
2013-01-28 18:20:20 +00:00
item = itemFromIndex (index);
item->twiddleWanted (file_ids, want);
emit wantedChanged (file_ids, want);
dataChanged (index, index);
parentsChanged (index, column);
subtreeChanged (index, column);
2009-04-09 18:55:47 +00:00
}
else if (column == COL_PRIORITY)
2009-04-09 18:55:47 +00:00
{
int priority;
QSet<int> file_ids;
FileTreeItem * item;
2013-01-28 18:20:20 +00:00
item = itemFromIndex (index);
item->twiddlePriority (file_ids, priority);
emit priorityChanged (file_ids, priority);
2009-04-09 18:55:47 +00:00
dataChanged(index, index);
parentsChanged(index, column);
subtreeChanged(index, column);
2009-04-09 18:55:47 +00:00
}
}
/****
*****
****/
QSize
FileTreeDelegate :: sizeHint(const QStyleOptionViewItem& item, const QModelIndex& index) const
{
QSize size;
switch(index.column())
{
case COL_NAME:
{
const QFontMetrics fm(item.font);
const QString text = index.data().toString();
const int iconSize = QApplication::style()->pixelMetric(QStyle::PM_SmallIconSize);
size.rwidth() = HIG::PAD_SMALL + iconSize;
size.rheight() = std::max(iconSize, fm.height());
break;
}
case COL_PROGRESS:
case COL_WANTED:
size = QSize(20, 1);
break;
default:
{
const QFontMetrics fm(item.font);
const QString text = index.data().toString();
size = fm.size(0, text);
break;
}
}
size.rheight() += 8; // make the spacing a little nicer
return size;
}
2009-04-09 18:55:47 +00:00
void
FileTreeDelegate :: paint (QPainter * painter,
2009-04-09 18:55:47 +00:00
const QStyleOptionViewItem & option,
const QModelIndex & index) const
2009-04-09 18:55:47 +00:00
{
const int column(index.column());
2009-04-09 18:55:47 +00:00
if ((column != COL_PROGRESS) && (column != COL_WANTED) && (column != COL_NAME))
2009-04-09 18:55:47 +00:00
{
QItemDelegate::paint(painter, option, index);
return;
2009-04-09 18:55:47 +00:00
}
QStyle * style (QApplication :: style());
if (option.state & QStyle::State_Selected)
painter->fillRect (option.rect, option.palette.highlight());
painter->save();
if (option.state & QStyle::State_Selected)
painter->setBrush (option.palette.highlightedText());
2009-04-09 18:55:47 +00:00
if (column == COL_NAME)
2009-04-09 18:55:47 +00:00
{
// draw the file icon
static const int iconSize (style->pixelMetric(QStyle :: PM_SmallIconSize));
const QRect iconArea (option.rect.x(),
option.rect.y() + (option.rect.height()-iconSize)/2,
iconSize, iconSize);
QIcon icon;
if (index.model()->hasChildren(index))
{
icon = style->standardIcon(QStyle::StandardPixmap(QStyle::SP_DirOpenIcon));
}
else
{
QString name = index.data().toString();
icon = Utils :: guessMimeIcon (name.left(name.lastIndexOf(" (")));
}
icon.paint (painter, iconArea, Qt::AlignCenter, QIcon::Normal, QIcon::On);
// draw the name
QStyleOptionViewItem tmp (option);
tmp.rect.setWidth (option.rect.width() - iconArea.width() - HIG::PAD_SMALL);
tmp.rect.moveRight (option.rect.right());
QItemDelegate::paint (painter, tmp, index);
}
else if(column == COL_PROGRESS)
{
QStyleOptionProgressBar p;
p.state = option.state | QStyle::State_Small;
p.direction = QApplication::layoutDirection();
p.rect = option.rect;
p.rect.setSize (QSize(option.rect.width()-2, option.rect.height()-8));
p.rect.moveCenter (option.rect.center());
p.fontMetrics = QApplication::fontMetrics();
p.minimum = 0;
p.maximum = 100;
p.textAlignment = Qt::AlignCenter;
p.textVisible = true;
p.progress = (int)(100.0*index.data().toDouble());
p.text = QString().sprintf("%d%%", p.progress);
style->drawControl(QStyle::CE_ProgressBar, &p, painter);
}
else if(column == COL_WANTED)
{
QStyleOptionButton o;
o.state = option.state;
o.direction = QApplication::layoutDirection();
o.rect.setSize (QSize(20, option.rect.height()));
o.rect.moveCenter (option.rect.center());
o.fontMetrics = QApplication::fontMetrics();
switch (index.data().toInt())
{
case Qt::Unchecked: o.state |= QStyle::State_Off; break;
case Qt::Checked: o.state |= QStyle::State_On; break;
default: o.state |= QStyle::State_NoChange;break;
2009-04-09 18:55:47 +00:00
}
style->drawControl (QStyle::CE_CheckBox, &o, painter);
2009-04-09 18:55:47 +00:00
}
painter->restore();
2009-04-09 18:55:47 +00:00
}
/****
*****
*****
*****
****/
FileTreeView :: FileTreeView (QWidget * parent, bool isEditable):
QTreeView (parent),
myModel (this, isEditable),
myProxy (new QSortFilterProxyModel()),
myDelegate (this)
2009-04-09 18:55:47 +00:00
{
setSortingEnabled (true);
setAlternatingRowColors (true);
setSelectionBehavior (QAbstractItemView::SelectRows);
setSelectionMode (QAbstractItemView::ExtendedSelection);
myProxy->setSourceModel (&myModel);
setModel (myProxy);
setItemDelegate (&myDelegate);
setHorizontalScrollBarPolicy (Qt::ScrollBarAlwaysOff);
sortByColumn (COL_NAME, Qt::AscendingOrder);
installEventFilter (this);
2009-04-09 18:55:47 +00:00
for (int i=0; i<FIRST_VISIBLE_COLUMN; ++i)
hideColumn (i);
for (int i=LAST_VISIBLE_COLUMN+1; i<NUM_COLUMNS; ++i)
hideColumn (i);
for (int i=FIRST_VISIBLE_COLUMN; i<=LAST_VISIBLE_COLUMN; ++i)
header()->setResizeMode(i, QHeaderView::Interactive);
2009-04-09 18:55:47 +00:00
connect (this, SIGNAL(clicked(const QModelIndex&)),
this, SLOT(onClicked(const QModelIndex&)));
2009-04-09 18:55:47 +00:00
connect (&myModel, SIGNAL(priorityChanged(const QSet<int>&, int)),
this, SIGNAL(priorityChanged(const QSet<int>&, int)));
2009-04-09 18:55:47 +00:00
connect (&myModel, SIGNAL(wantedChanged(const QSet<int>&, bool)),
this, SIGNAL(wantedChanged(const QSet<int>&, bool)));
connect (&myModel, SIGNAL(pathEdited(const QString&, const QString&)),
this, SIGNAL(pathEdited(const QString&, const QString&)));
2009-04-09 18:55:47 +00:00
}
FileTreeView :: ~FileTreeView ()
{
myProxy->deleteLater();
}
void
FileTreeView :: onClicked (const QModelIndex& proxyIndex)
{
const QModelIndex modelIndex = myProxy->mapToSource (proxyIndex);
myModel.clicked (modelIndex);
}
bool
FileTreeView :: eventFilter (QObject * o, QEvent * event)
{
// this is kind of a hack to get the last three columns be the
// right size, and to have the filename column use whatever
// space is left over...
if ((o == this) && (event->type() == QEvent::Resize))
{
QResizeEvent * r = dynamic_cast<QResizeEvent*>(event);
int left = r->size().width();
const QFontMetrics fontMetrics(font());
for (int column=FIRST_VISIBLE_COLUMN; column<=LAST_VISIBLE_COLUMN; ++column)
{
if (column == COL_NAME)
continue;
if (isColumnHidden (column))
continue;
const QString header = myModel.headerData (column, Qt::Horizontal).toString() + " ";
const int width = fontMetrics.size (0, header).width();
setColumnWidth (column, width);
2009-04-09 18:55:47 +00:00
left -= width;
}
left -= 20; // not sure why this is necessary. it works in different themes + font sizes though...
setColumnWidth(COL_NAME, std::max(left,0));
}
return false;
2009-04-09 18:55:47 +00:00
}
void
FileTreeView :: update (const FileList& files, bool updateFields)
2009-04-09 18:55:47 +00:00
{
foreach (const TrFile file, files)
{
QList<QModelIndex> added;
myModel.addFile (file.index, file.filename, file.wanted, file.priority, file.size, file.have, added, updateFields);
foreach (QModelIndex i, added)
expand (myProxy->mapFromSource(i));
2009-04-09 18:55:47 +00:00
}
}
void
FileTreeView :: clear ()
2009-04-09 18:55:47 +00:00
{
myModel.clear();
2009-04-09 18:55:47 +00:00
}