2009-04-09 18:55:47 +00:00
|
|
|
/*
|
2014-01-19 01:09:44 +00:00
|
|
|
* This file Copyright (C) 2009-2014 Mnemosyne LLC
|
2009-04-09 18:55:47 +00:00
|
|
|
*
|
2014-01-19 01:09:44 +00:00
|
|
|
* It may be used under the GNU Public License v2 or v3 licenses,
|
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2009-04-09 18:55:47 +00:00
|
|
|
*
|
2009-05-31 19:33:48 +00:00
|
|
|
* $Id$
|
2009-04-09 18:55:47 +00:00
|
|
|
*/
|
|
|
|
|
2013-02-01 22:40:08 +00:00
|
|
|
#include <algorithm>
|
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>
|
2010-07-28 18:53:25 +00:00
|
|
|
#include <QSortFilterProxyModel>
|
2009-04-09 18:55:47 +00:00
|
|
|
#include <QStringList>
|
|
|
|
|
|
|
|
#include <libtransmission/transmission.h> // priorities
|
|
|
|
|
|
|
|
#include "file-tree.h"
|
2010-07-03 01:10:36 +00:00
|
|
|
#include "formatter.h"
|
2009-04-09 18:55:47 +00:00
|
|
|
#include "hig.h"
|
|
|
|
#include "torrent.h" // FileList
|
|
|
|
#include "utils.h" // mime icons
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
2013-01-25 18:37:11 +00:00
|
|
|
COL_NAME,
|
2013-01-28 07:01:58 +00:00
|
|
|
FIRST_VISIBLE_COLUMN = COL_NAME,
|
2013-02-09 20:51:17 +00:00
|
|
|
COL_SIZE,
|
2013-01-25 18:37:11 +00:00
|
|
|
COL_PROGRESS,
|
|
|
|
COL_WANTED,
|
|
|
|
COL_PRIORITY,
|
2013-01-28 07:01:58 +00:00
|
|
|
LAST_VISIBLE_COLUMN = COL_PRIORITY,
|
|
|
|
|
|
|
|
COL_FILE_INDEX,
|
2013-01-25 18:37:11 +00:00
|
|
|
NUM_COLUMNS
|
2009-04-09 18:55:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
2013-01-28 07:01:58 +00:00
|
|
|
const QHash<QString,int>&
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeItem::getMyChildRows ()
|
2012-07-12 23:37:04 +00:00
|
|
|
{
|
2013-01-25 18:37:11 +00:00
|
|
|
const size_t n = childCount();
|
2012-07-13 00:27:04 +00:00
|
|
|
|
2013-01-25 18:37:11 +00:00
|
|
|
// ensure that all the rows are hashed
|
|
|
|
while (myFirstUnhashedRow < n)
|
2012-07-13 00:27:04 +00:00
|
|
|
{
|
2013-01-28 18:20:20 +00:00
|
|
|
myChildRows.insert (myChildren[myFirstUnhashedRow]->name(),
|
|
|
|
myFirstUnhashedRow);
|
|
|
|
++myFirstUnhashedRow;
|
2012-07-12 23:37:04 +00:00
|
|
|
}
|
|
|
|
|
2013-01-25 18:37:11 +00:00
|
|
|
return myChildRows;
|
2012-07-12 23:37:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeItem::~FileTreeItem ()
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-01-25 18:37:11 +00:00
|
|
|
assert(myChildren.isEmpty());
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2013-01-25 18:37:11 +00:00
|
|
|
if (myParent != 0)
|
2012-07-13 00:27:04 +00:00
|
|
|
{
|
2013-01-25 18:37:11 +00:00
|
|
|
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-09-02 15:34:18 +00:00
|
|
|
}
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeItem::appendChild (FileTreeItem * child)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-01-25 18:37:11 +00:00
|
|
|
const size_t n = childCount();
|
|
|
|
child->myParent = this;
|
|
|
|
myChildren.append (child);
|
|
|
|
myFirstUnhashedRow = n;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FileTreeItem *
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeItem::child (const QString& filename)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2012-07-12 23:37:04 +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
|
|
|
|
2012-07-12 23:37:04 +00:00
|
|
|
return item;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeItem::row () const
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-01-25 18:37:11 +00:00
|
|
|
int i(-1);
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2013-01-25 18:37:11 +00:00
|
|
|
if(myParent)
|
2012-07-13 00:27:04 +00:00
|
|
|
{
|
2013-01-25 18:37:11 +00:00
|
|
|
i = myParent->getMyChildRows().value (name(), -1);
|
|
|
|
assert (this == myParent->myChildren[i]);
|
2012-07-13 00:27:04 +00:00
|
|
|
}
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2013-01-25 18:37:11 +00:00
|
|
|
return i;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeItem::data (int column, int role) const
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-01-23 20:44:13 +00:00
|
|
|
QVariant value;
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2013-01-28 07:01:58 +00:00
|
|
|
if (column == COL_FILE_INDEX)
|
|
|
|
{
|
2013-02-09 20:51:17 +00:00
|
|
|
value.setValue (myFileIndex);
|
2013-01-28 07:01:58 +00:00
|
|
|
}
|
|
|
|
else if (role == Qt::EditRole)
|
2013-01-23 20:44:13 +00:00
|
|
|
{
|
|
|
|
if (column == 0)
|
|
|
|
value.setValue (name());
|
|
|
|
}
|
2013-02-09 20:51:17 +00:00
|
|
|
else if ((role == Qt::TextAlignmentRole) && column == COL_SIZE)
|
|
|
|
{
|
|
|
|
value = Qt::AlignRight + Qt::AlignVCenter;
|
|
|
|
}
|
2013-01-23 20:44:13 +00:00
|
|
|
else if (role == Qt::DisplayRole)
|
|
|
|
{
|
2013-01-25 18:37:11 +00:00
|
|
|
switch(column)
|
2013-01-23 20:44:13 +00:00
|
|
|
{
|
|
|
|
case COL_NAME:
|
2013-02-09 20:51:17 +00:00
|
|
|
value.setValue (name());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case COL_SIZE:
|
|
|
|
value.setValue (sizeString() + " ");
|
2013-01-23 20:44:13 +00:00
|
|
|
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
|
|
|
}
|
2014-07-03 23:12:50 +00:00
|
|
|
else if (role == Qt::DecorationRole && column == COL_NAME)
|
|
|
|
{
|
|
|
|
if (childCount () > 0)
|
|
|
|
value = QApplication::style ()->standardIcon (QStyle::SP_DirOpenIcon);
|
|
|
|
else
|
|
|
|
value = Utils::guessMimeIcon (name ());
|
|
|
|
}
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2013-01-23 20:44:13 +00:00
|
|
|
return value;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeItem::getSubtreeWantedSize (uint64_t& have, uint64_t& total) const
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-02-09 19:16:16 +00:00
|
|
|
if (myIsWanted)
|
|
|
|
{
|
|
|
|
have += myHaveSize;
|
|
|
|
total += myTotalSize;
|
|
|
|
}
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2013-01-25 18:37:11 +00:00
|
|
|
foreach(const FileTreeItem * i, myChildren)
|
2013-02-09 19:16:16 +00:00
|
|
|
i->getSubtreeWantedSize(have, total);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeItem::progress () const
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-01-25 18:37:11 +00:00
|
|
|
double d(0);
|
2013-08-29 00:37:37 +00:00
|
|
|
uint64_t have(0), total(0);
|
2013-01-25 18:37:11 +00:00
|
|
|
|
2013-02-09 19:16:16 +00:00
|
|
|
getSubtreeWantedSize (have, total);
|
2013-01-25 18:37:11 +00:00
|
|
|
if (total)
|
|
|
|
d = have / (double)total;
|
|
|
|
|
|
|
|
return d;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2010-04-03 13:56:23 +00:00
|
|
|
QString
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeItem::sizeString () const
|
2010-04-03 13:56:23 +00:00
|
|
|
{
|
2013-02-09 20:51:17 +00:00
|
|
|
QString str;
|
2013-02-09 19:16:16 +00:00
|
|
|
|
|
|
|
if (myChildren.isEmpty())
|
2013-02-09 20:51:17 +00:00
|
|
|
{
|
|
|
|
str = Formatter::sizeToString (myTotalSize);
|
|
|
|
}
|
2013-02-09 19:16:16 +00:00
|
|
|
else
|
2013-02-09 20:51:17 +00:00
|
|
|
{
|
2013-08-29 00:37:37 +00:00
|
|
|
uint64_t have = 0;
|
|
|
|
uint64_t total = 0;
|
2013-02-09 20:51:17 +00:00
|
|
|
getSubtreeWantedSize (have, total);
|
|
|
|
str = Formatter::sizeToString (total);
|
|
|
|
}
|
2013-02-09 19:16:16 +00:00
|
|
|
|
2013-02-09 20:51:17 +00:00
|
|
|
return str;
|
2010-04-03 13:56:23 +00:00
|
|
|
}
|
|
|
|
|
2013-02-01 22:40:08 +00:00
|
|
|
std::pair<int,int>
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeItem::update (const QString& name,
|
|
|
|
bool wanted,
|
|
|
|
int priority,
|
|
|
|
uint64_t haveSize,
|
|
|
|
bool updateFields)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-02-01 22:40:08 +00:00
|
|
|
int changed_count = 0;
|
2013-02-09 20:51:17 +00:00
|
|
|
int changed_columns[4];
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2013-01-28 07:01:58 +00:00
|
|
|
if (myName != name)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-01-28 07:01:58 +00:00
|
|
|
if (myParent)
|
|
|
|
myParent->myFirstUnhashedRow = row();
|
2013-01-25 18:37:11 +00:00
|
|
|
|
2013-01-28 07:01:58 +00:00
|
|
|
myName = name;
|
2013-02-02 03:51:10 +00:00
|
|
|
changed_columns[changed_count++] = COL_NAME;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
2013-01-25 18:37:11 +00:00
|
|
|
|
2014-11-30 18:12:28 +00:00
|
|
|
if (fileIndex () != -1)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2014-11-30 18:12:28 +00:00
|
|
|
if (myHaveSize != haveSize)
|
|
|
|
{
|
|
|
|
myHaveSize = haveSize;
|
|
|
|
changed_columns[changed_count++] = COL_PROGRESS;
|
|
|
|
}
|
2013-01-25 18:37:11 +00:00
|
|
|
|
2013-01-28 07:01:58 +00:00
|
|
|
if (updateFields)
|
|
|
|
{
|
|
|
|
if (myIsWanted != wanted)
|
|
|
|
{
|
|
|
|
myIsWanted = wanted;
|
2013-02-02 03:51:10 +00:00
|
|
|
changed_columns[changed_count++] = COL_WANTED;
|
2013-01-28 07:01:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (myPriority != priority)
|
|
|
|
{
|
|
|
|
myPriority = priority;
|
2013-02-02 03:51:10 +00:00
|
|
|
changed_columns[changed_count++] = COL_PRIORITY;
|
2013-01-28 07:01:58 +00:00
|
|
|
}
|
|
|
|
}
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2013-02-01 22:40:08 +00:00
|
|
|
std::pair<int,int> changed (-1, -1);
|
|
|
|
if (changed_count > 0)
|
|
|
|
{
|
2013-02-02 03:51:10 +00:00
|
|
|
std::sort (changed_columns, changed_columns+changed_count);
|
|
|
|
changed.first = changed_columns[0];
|
|
|
|
changed.second = changed_columns[changed_count-1];
|
2013-02-01 22:40:08 +00:00
|
|
|
}
|
2013-01-25 18:37:11 +00:00
|
|
|
return changed;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString
|
2014-12-12 23:05:10 +00:00
|
|
|
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
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeItem::priority () const
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-01-25 18:37:11 +00:00
|
|
|
int i(0);
|
|
|
|
|
2013-01-25 18:54:06 +00:00
|
|
|
if (myChildren.isEmpty())
|
2013-01-25 18:37:11 +00:00
|
|
|
{
|
2013-01-25 18:54:06 +00:00
|
|
|
switch (myPriority)
|
|
|
|
{
|
|
|
|
case TR_PRI_LOW:
|
|
|
|
i |= LOW;
|
|
|
|
break;
|
2013-01-25 18:37:11 +00:00
|
|
|
|
2013-01-25 18:54:06 +00:00
|
|
|
case TR_PRI_HIGH:
|
|
|
|
i |= HIGH;
|
|
|
|
break;
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2013-01-25 18:54:06 +00:00
|
|
|
default:
|
|
|
|
i |= NORMAL;
|
|
|
|
break;
|
|
|
|
}
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2013-01-25 18:54:06 +00:00
|
|
|
foreach (const FileTreeItem * child, myChildren)
|
2013-01-25 18:37:11 +00:00
|
|
|
i |= child->priority();
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2013-01-25 18:37:11 +00:00
|
|
|
return i;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeItem::setSubtreePriority (int i, QSet<int>& ids)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-01-25 18:37:11 +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
|
|
|
}
|
|
|
|
|
2013-01-25 18:37:11 +00:00
|
|
|
foreach (FileTreeItem * child, myChildren)
|
|
|
|
child->setSubtreePriority (i, ids);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeItem::twiddlePriority (QSet<int>& ids, int& p)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-01-25 18:37:11 +00:00
|
|
|
const int old(priority());
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2013-01-25 18:37:11 +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
|
|
|
|
2013-01-25 18:37:11 +00:00
|
|
|
setSubtreePriority (p, ids);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeItem::isSubtreeWanted () const
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-01-25 18:37:11 +00:00
|
|
|
if(myChildren.isEmpty())
|
|
|
|
return myIsWanted ? Qt::Checked : Qt::Unchecked;
|
|
|
|
|
|
|
|
int wanted(-1);
|
|
|
|
foreach (const FileTreeItem * child, myChildren)
|
|
|
|
{
|
|
|
|
const int childWanted = child->isSubtreeWanted();
|
|
|
|
|
2013-01-25 18:54:06 +00:00
|
|
|
if (wanted == -1)
|
2013-01-25 18:37:11 +00:00
|
|
|
wanted = childWanted;
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2013-01-25 18:54:06 +00:00
|
|
|
if (wanted != childWanted)
|
2013-01-25 18:37:11 +00:00
|
|
|
wanted = Qt::PartiallyChecked;
|
|
|
|
|
2013-01-25 18:54:06 +00:00
|
|
|
if (wanted == Qt::PartiallyChecked)
|
2013-01-25 18:37:11 +00:00
|
|
|
return wanted;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2013-01-25 18:37:11 +00:00
|
|
|
return wanted;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeItem::setSubtreeWanted (bool b, QSet<int>& ids)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-01-25 23:34:20 +00:00
|
|
|
if (myIsWanted != b)
|
2013-01-25 18:37:11 +00:00
|
|
|
{
|
|
|
|
myIsWanted = b;
|
|
|
|
|
2013-01-28 18:20:20 +00:00
|
|
|
if (myFileIndex >= 0)
|
|
|
|
ids.insert(myFileIndex);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2013-01-25 18:37:11 +00:00
|
|
|
foreach (FileTreeItem * child, myChildren)
|
|
|
|
child->setSubtreeWanted (b, ids);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeItem::twiddleWanted (QSet<int>& ids, bool& wanted)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-01-25 18:37:11 +00:00
|
|
|
wanted = isSubtreeWanted() != Qt::Checked;
|
|
|
|
setSubtreeWanted (wanted, ids);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2013-09-08 19:03:25 +00:00
|
|
|
QString
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeItem::path () const
|
2013-09-08 19:03:25 +00:00
|
|
|
{
|
|
|
|
QString itemPath;
|
|
|
|
const FileTreeItem * item = this;
|
|
|
|
|
|
|
|
while (item != NULL && !item->name().isEmpty())
|
|
|
|
{
|
|
|
|
if (itemPath.isEmpty())
|
|
|
|
itemPath = item->name();
|
|
|
|
else
|
|
|
|
itemPath = item->name() + "/" + itemPath;
|
|
|
|
item = item->parent ();
|
|
|
|
}
|
|
|
|
|
|
|
|
return itemPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeItem::isComplete () const
|
2013-09-08 19:03:25 +00:00
|
|
|
{
|
|
|
|
return myHaveSize == totalSize ();
|
|
|
|
}
|
|
|
|
|
2009-04-09 18:55:47 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeModel::FileTreeModel (QObject * parent, bool isEditable):
|
2013-01-28 07:01:58 +00:00
|
|
|
QAbstractItemModel(parent),
|
|
|
|
myRootItem (new FileTreeItem),
|
2014-11-30 18:12:28 +00:00
|
|
|
myIndexCache (),
|
2013-01-28 07:01:58 +00:00
|
|
|
myIsEditable (isEditable)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
|
|
|
}
|
2010-01-05 23:47:50 +00:00
|
|
|
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeModel::~FileTreeModel()
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-01-25 18:37:11 +00:00
|
|
|
clear();
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2013-01-28 07:01:58 +00:00
|
|
|
delete myRootItem;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2013-01-28 18:20:20 +00:00
|
|
|
FileTreeItem *
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeModel::itemFromIndex (const QModelIndex& index) const
|
2013-01-28 18:20:20 +00:00
|
|
|
{
|
|
|
|
return static_cast<FileTreeItem*>(index.internalPointer());
|
|
|
|
}
|
|
|
|
|
2009-04-09 18:55:47 +00:00
|
|
|
QVariant
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeModel::data (const QModelIndex &index, int role) const
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-01-25 18:37:11 +00:00
|
|
|
QVariant value;
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2013-01-25 18:37:11 +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
|
|
|
|
2013-01-25 18:37:11 +00:00
|
|
|
return value;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Qt::ItemFlags
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeModel::flags (const QModelIndex& index) const
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-01-25 18:37:11 +00:00
|
|
|
int i(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2013-01-28 07:01:58 +00:00
|
|
|
if(myIsEditable && (index.column() == COL_NAME))
|
2013-01-25 18:37:11 +00:00
|
|
|
i |= Qt::ItemIsEditable;
|
2013-01-20 01:31:58 +00:00
|
|
|
|
2013-01-25 18:37:11 +00:00
|
|
|
if(index.column() == COL_WANTED)
|
|
|
|
i |= Qt::ItemIsUserCheckable | Qt::ItemIsTristate;
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2013-01-25 18:37:11 +00:00
|
|
|
return (Qt::ItemFlags)i;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2013-01-20 01:31:58 +00:00
|
|
|
bool
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeModel::setData (const QModelIndex& index, const QVariant& newname, int role)
|
2013-01-20 01:31:58 +00:00
|
|
|
{
|
|
|
|
if (role == Qt::EditRole)
|
|
|
|
{
|
2013-01-28 18:20:20 +00:00
|
|
|
FileTreeItem * item = itemFromIndex (index);
|
2013-01-20 01:31:58 +00:00
|
|
|
|
2013-09-08 19:03:25 +00:00
|
|
|
emit pathEdited (item->path (), newname.toString ());
|
2013-01-20 01:31:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false; // don't update the view until the session confirms the change
|
|
|
|
}
|
|
|
|
|
2009-04-09 18:55:47 +00:00
|
|
|
QVariant
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeModel::headerData (int column, Qt::Orientation orientation, int role) const
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-01-25 18:37:11 +00:00
|
|
|
QVariant data;
|
|
|
|
|
2013-01-25 18:54:06 +00:00
|
|
|
if (orientation==Qt::Horizontal && role==Qt::DisplayRole)
|
2013-01-25 18:37:11 +00:00
|
|
|
{
|
|
|
|
switch (column)
|
|
|
|
{
|
|
|
|
case COL_NAME:
|
|
|
|
data.setValue (tr("File"));
|
|
|
|
break;
|
|
|
|
|
2013-02-09 20:51:17 +00:00
|
|
|
case COL_SIZE:
|
|
|
|
data.setValue (tr("Size"));
|
|
|
|
break;
|
|
|
|
|
2013-01-25 18:37:11 +00:00
|
|
|
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
|
|
|
|
2013-01-25 18:37:11 +00:00
|
|
|
default:
|
|
|
|
break;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
}
|
2010-01-05 23:47:50 +00:00
|
|
|
|
2013-01-25 18:37:11 +00:00
|
|
|
return data;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeModel::index (int row, int column, const QModelIndex& parent) const
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-01-25 18:37:11 +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
|
|
|
{
|
2013-01-25 18:37:11 +00:00
|
|
|
FileTreeItem * parentItem;
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2013-01-30 00:23:32 +00:00
|
|
|
if (!parent.isValid ())
|
2013-01-28 07:01:58 +00:00
|
|
|
parentItem = myRootItem;
|
2013-01-25 18:37:11 +00:00
|
|
|
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
|
|
|
|
2013-01-25 18:37:11 +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
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeModel::parent (const QModelIndex& child) const
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-01-25 18:37:11 +00:00
|
|
|
return parent (child, 0); // QAbstractItemModel::parent() wants col 0
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex
|
2014-12-12 23:05:10 +00:00
|
|
|
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
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeModel::rowCount (const QModelIndex& parent) const
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-01-25 18:37:11 +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);
|
2013-01-25 18:37:11 +00:00
|
|
|
else
|
2013-01-28 18:20:20 +00:00
|
|
|
parentItem = myRootItem;
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2013-01-25 18:37:11 +00:00
|
|
|
return parentItem->childCount();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeModel::columnCount (const QModelIndex& parent) const
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-01-25 18:37:11 +00:00
|
|
|
Q_UNUSED(parent);
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2013-01-28 07:01:58 +00:00
|
|
|
return NUM_COLUMNS;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeModel::indexOf (FileTreeItem * item, int column) const
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-01-28 07:01:58 +00:00
|
|
|
if (!item || item==myRootItem)
|
2013-01-25 18:37:11 +00:00
|
|
|
return QModelIndex();
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2013-01-25 18:37:11 +00:00
|
|
|
return createIndex(item->row(), column, item);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeModel::clearSubtree (const QModelIndex& top)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-01-25 18:37:11 +00:00
|
|
|
size_t i = rowCount (top);
|
2012-07-13 00:27:04 +00:00
|
|
|
|
2013-01-25 18:37:11 +00:00
|
|
|
while (i > 0)
|
|
|
|
clearSubtree(index(--i, 0, top));
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2014-11-30 18:12:28 +00:00
|
|
|
FileTreeItem * const item = itemFromIndex (top);
|
|
|
|
if (item == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (item->fileIndex () != -1)
|
|
|
|
myIndexCache.remove (item->fileIndex ());
|
|
|
|
|
|
|
|
delete item;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeModel::clear ()
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-07-27 21:58:14 +00:00
|
|
|
beginResetModel ();
|
2013-01-25 18:37:11 +00:00
|
|
|
clearSubtree (QModelIndex());
|
2013-07-27 21:58:14 +00:00
|
|
|
endResetModel ();
|
2014-11-30 18:12:28 +00:00
|
|
|
|
|
|
|
assert (myIndexCache.isEmpty ());
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2013-01-28 07:01:58 +00:00
|
|
|
FileTreeItem *
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeModel::findItemForFileIndex (int fileIndex) const
|
2013-01-28 07:01:58 +00:00
|
|
|
{
|
2014-11-30 18:12:28 +00:00
|
|
|
return myIndexCache.value (fileIndex, 0);
|
2013-01-28 07:01:58 +00:00
|
|
|
}
|
|
|
|
|
2009-04-09 18:55:47 +00:00
|
|
|
void
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeModel::addFile (int fileIndex,
|
|
|
|
const QString & filename,
|
|
|
|
bool wanted,
|
|
|
|
int priority,
|
|
|
|
uint64_t totalSize,
|
|
|
|
uint64_t have,
|
|
|
|
QList<QModelIndex> & rowsAdded,
|
|
|
|
bool updateFields)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-01-28 07:01:58 +00:00
|
|
|
FileTreeItem * item;
|
2013-07-27 21:58:14 +00:00
|
|
|
QStringList tokens = filename.split (QChar::fromLatin1('/'));
|
2013-01-28 07:01:58 +00:00
|
|
|
|
|
|
|
item = findItemForFileIndex (fileIndex);
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2013-01-28 07:01:58 +00:00
|
|
|
if (item) // this file is already in the tree, we've added this
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2014-11-30 18:12:28 +00:00
|
|
|
QModelIndex indexWithChangedParents;
|
2013-01-28 07:01:58 +00:00
|
|
|
while (!tokens.isEmpty())
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-01-28 07:01:58 +00:00
|
|
|
const QString token = tokens.takeLast();
|
2013-02-01 22:40:08 +00:00
|
|
|
const std::pair<int,int> changed = item->update (token, wanted, priority, have, updateFields);
|
|
|
|
if (changed.first >= 0)
|
2014-11-30 18:12:28 +00:00
|
|
|
{
|
|
|
|
dataChanged (indexOf (item, changed.first), indexOf (item, changed.second));
|
|
|
|
if (!indexWithChangedParents.isValid () &&
|
|
|
|
changed.first <= COL_PRIORITY && changed.second >= COL_SIZE)
|
|
|
|
indexWithChangedParents = indexOf (item, 0);
|
|
|
|
}
|
2013-01-28 07:01:58 +00:00
|
|
|
item = item->parent();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
2013-01-28 07:01:58 +00:00
|
|
|
assert (item == myRootItem);
|
2014-11-30 18:12:28 +00:00
|
|
|
if (indexWithChangedParents.isValid ())
|
|
|
|
parentsChanged (indexWithChangedParents, COL_SIZE, COL_PRIORITY);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
2013-01-28 07:01:58 +00:00
|
|
|
else // we haven't build the FileTreeItems for these tokens yet
|
2013-01-25 23:34:20 +00:00
|
|
|
{
|
2014-12-12 23:52:17 +00:00
|
|
|
bool added = false;
|
|
|
|
|
2013-01-28 07:01:58 +00:00
|
|
|
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)
|
2013-01-25 23:34:20 +00:00
|
|
|
{
|
2013-01-28 07:01:58 +00:00
|
|
|
assert (item->fileIndex() == fileIndex);
|
|
|
|
assert (item->totalSize() == totalSize);
|
|
|
|
|
2014-11-30 18:12:28 +00:00
|
|
|
myIndexCache[fileIndex] = item;
|
|
|
|
|
2013-02-01 22:40:08 +00:00
|
|
|
const std::pair<int,int> changed = item->update (item->name(), wanted, priority, have, added || updateFields);
|
|
|
|
if (changed.first >= 0)
|
|
|
|
dataChanged (indexOf (item, changed.first), indexOf (item, changed.second));
|
2013-01-25 23:34:20 +00:00
|
|
|
}
|
|
|
|
}
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeModel::parentsChanged (const QModelIndex& index, int firstColumn, int lastColumn)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2014-11-30 18:12:28 +00:00
|
|
|
assert (firstColumn <= lastColumn);
|
|
|
|
|
2013-01-25 18:37:11 +00:00
|
|
|
QModelIndex walk = index;
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2013-01-25 18:37:11 +00:00
|
|
|
for (;;)
|
|
|
|
{
|
2014-11-30 18:12:28 +00:00
|
|
|
walk = parent (walk, firstColumn);
|
|
|
|
if (!walk.isValid ())
|
2013-01-25 18:37:11 +00:00
|
|
|
break;
|
|
|
|
|
2014-11-30 18:12:28 +00:00
|
|
|
dataChanged (walk, walk.sibling (walk.row (), lastColumn));
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeModel::subtreeChanged (const QModelIndex& index, int firstColumn, int lastColumn)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2014-11-30 18:12:28 +00:00
|
|
|
assert (firstColumn <= lastColumn);
|
|
|
|
|
2013-01-25 18:37:11 +00:00
|
|
|
const int childCount = rowCount (index);
|
|
|
|
if (!childCount)
|
|
|
|
return;
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2013-01-25 18:37:11 +00:00
|
|
|
// tell everyone that this tier changed
|
2014-11-30 18:12:28 +00:00
|
|
|
dataChanged (index.child (0, firstColumn), index.child (childCount - 1, lastColumn));
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2013-01-25 18:37:11 +00:00
|
|
|
// walk the subtiers
|
|
|
|
for (int i=0; i<childCount; ++i)
|
2014-11-30 18:12:28 +00:00
|
|
|
subtreeChanged (index.child (i, 0), firstColumn, lastColumn);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeModel::clicked (const QModelIndex& index)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-01-25 18:37:11 +00:00
|
|
|
const int column (index.column());
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2013-01-25 18:37:11 +00:00
|
|
|
if (!index.isValid())
|
|
|
|
return;
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2013-01-25 18:37:11 +00:00
|
|
|
if (column == COL_WANTED)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-01-25 18:37:11 +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);
|
2013-01-25 18:37:11 +00:00
|
|
|
item->twiddleWanted (file_ids, want);
|
|
|
|
emit wantedChanged (file_ids, want);
|
|
|
|
|
|
|
|
dataChanged (index, index);
|
2014-11-30 18:12:28 +00:00
|
|
|
parentsChanged (index, COL_SIZE, COL_WANTED);
|
|
|
|
subtreeChanged (index, COL_WANTED, COL_WANTED);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
2013-01-25 18:37:11 +00:00
|
|
|
else if (column == COL_PRIORITY)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-01-25 18:37:11 +00:00
|
|
|
int priority;
|
|
|
|
QSet<int> file_ids;
|
|
|
|
FileTreeItem * item;
|
|
|
|
|
2013-01-28 18:20:20 +00:00
|
|
|
item = itemFromIndex (index);
|
2013-01-25 18:37:11 +00:00
|
|
|
item->twiddlePriority (file_ids, priority);
|
|
|
|
emit priorityChanged (file_ids, priority);
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2014-11-30 18:12:28 +00:00
|
|
|
dataChanged (index, index);
|
|
|
|
parentsChanged (index, column, column);
|
|
|
|
subtreeChanged (index, column, column);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-08 19:03:25 +00:00
|
|
|
void
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeModel::doubleClicked (const QModelIndex& index)
|
2013-09-08 19:03:25 +00:00
|
|
|
{
|
|
|
|
if (!index.isValid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
const int column (index.column());
|
|
|
|
if (column == COL_WANTED || column == COL_PRIORITY)
|
|
|
|
return;
|
|
|
|
|
|
|
|
FileTreeItem * item = itemFromIndex (index);
|
|
|
|
|
|
|
|
if (item->childCount () == 0 && item->isComplete ())
|
|
|
|
emit openRequested (item->path ());
|
|
|
|
}
|
|
|
|
|
2009-04-09 18:55:47 +00:00
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
2010-07-28 18:53:25 +00:00
|
|
|
QSize
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeDelegate::sizeHint(const QStyleOptionViewItem& item, const QModelIndex& index) const
|
2010-07-28 18:53:25 +00:00
|
|
|
{
|
2013-01-25 18:37:11 +00:00
|
|
|
QSize size;
|
2010-07-28 18:53:25 +00:00
|
|
|
|
2013-01-25 18:37:11 +00:00
|
|
|
switch(index.column())
|
2010-07-28 18:53:25 +00:00
|
|
|
{
|
2013-01-25 18:37:11 +00:00
|
|
|
case COL_PROGRESS:
|
|
|
|
case COL_WANTED:
|
|
|
|
size = QSize(20, 1);
|
|
|
|
break;
|
2010-07-28 18:53:25 +00:00
|
|
|
|
2013-01-25 18:37:11 +00:00
|
|
|
default:
|
2014-07-03 23:12:50 +00:00
|
|
|
size = QItemDelegate::sizeHint (item, index);
|
2010-07-28 18:53:25 +00:00
|
|
|
}
|
|
|
|
|
2013-01-25 18:37:11 +00:00
|
|
|
size.rheight() += 8; // make the spacing a little nicer
|
|
|
|
return size;
|
2010-07-28 18:53:25 +00:00
|
|
|
}
|
|
|
|
|
2009-04-09 18:55:47 +00:00
|
|
|
void
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeDelegate::paint (QPainter * painter,
|
|
|
|
const QStyleOptionViewItem & option,
|
|
|
|
const QModelIndex & index) const
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-01-25 18:37:11 +00:00
|
|
|
const int column(index.column());
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2014-07-03 23:12:50 +00:00
|
|
|
if ((column != COL_PROGRESS) && (column != COL_WANTED))
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-01-25 18:37:11 +00:00
|
|
|
QItemDelegate::paint(painter, option, index);
|
|
|
|
return;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2014-12-12 23:05:10 +00:00
|
|
|
QStyle * style (QApplication::style ());
|
2014-07-03 23:12:50 +00:00
|
|
|
|
2013-01-25 18:37:11 +00:00
|
|
|
painter->save();
|
2014-07-03 23:12:50 +00:00
|
|
|
QItemDelegate::drawBackground (painter, option, index);
|
2013-01-25 18:37:11 +00:00
|
|
|
|
2014-07-03 23:12:50 +00:00
|
|
|
if(column == COL_PROGRESS)
|
2013-01-25 18:37:11 +00:00
|
|
|
{
|
|
|
|
QStyleOptionProgressBar p;
|
|
|
|
p.state = option.state | QStyle::State_Small;
|
|
|
|
p.direction = QApplication::layoutDirection();
|
|
|
|
p.rect = option.rect;
|
2013-01-25 18:54:06 +00:00
|
|
|
p.rect.setSize (QSize(option.rect.width()-2, option.rect.height()-8));
|
|
|
|
p.rect.moveCenter (option.rect.center());
|
2013-01-25 18:37:11 +00:00
|
|
|
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();
|
2013-01-25 18:54:06 +00:00
|
|
|
o.rect.setSize (QSize(20, option.rect.height()));
|
|
|
|
o.rect.moveCenter (option.rect.center());
|
2013-01-25 18:37:11 +00:00
|
|
|
o.fontMetrics = QApplication::fontMetrics();
|
2013-01-25 18:54:06 +00:00
|
|
|
switch (index.data().toInt())
|
2013-01-25 18:37:11 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
2013-01-25 18:54:06 +00:00
|
|
|
style->drawControl (QStyle::CE_CheckBox, &o, painter);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2014-07-03 23:12:50 +00:00
|
|
|
QItemDelegate::drawFocus (painter, option, option.rect);
|
2013-01-25 18:37:11 +00:00
|
|
|
painter->restore();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/****
|
|
|
|
*****
|
|
|
|
*****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeView::FileTreeView (QWidget * parent, bool isEditable):
|
2013-01-25 18:37:11 +00:00
|
|
|
QTreeView (parent),
|
2013-01-28 07:01:58 +00:00
|
|
|
myModel (this, isEditable),
|
2013-01-25 18:37:11 +00:00
|
|
|
myProxy (new QSortFilterProxyModel()),
|
|
|
|
myDelegate (this)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-01-25 18:37:11 +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
|
|
|
|
2013-02-09 20:51:17 +00:00
|
|
|
for (int i=0; i<NUM_COLUMNS; ++i)
|
|
|
|
{
|
|
|
|
setColumnHidden (i, (i<FIRST_VISIBLE_COLUMN) || (LAST_VISIBLE_COLUMN<i));
|
2013-08-17 16:22:56 +00:00
|
|
|
|
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
|
|
|
|
header()->setResizeMode(i, QHeaderView::Interactive);
|
|
|
|
#else
|
2013-07-27 21:58:14 +00:00
|
|
|
header()->setSectionResizeMode(i, QHeaderView::Interactive);
|
2013-08-17 16:22:56 +00:00
|
|
|
#endif
|
2013-02-09 20:51:17 +00:00
|
|
|
}
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2013-01-25 18:37:11 +00:00
|
|
|
connect (this, SIGNAL(clicked(const QModelIndex&)),
|
|
|
|
this, SLOT(onClicked(const QModelIndex&)));
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2013-09-08 19:03:25 +00:00
|
|
|
connect (this, SIGNAL(doubleClicked(const QModelIndex&)),
|
|
|
|
this, SLOT(onDoubleClicked(const QModelIndex&)));
|
|
|
|
|
2013-01-25 18:37:11 +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
|
|
|
|
2013-01-25 18:37:11 +00:00
|
|
|
connect (&myModel, SIGNAL(wantedChanged(const QSet<int>&, bool)),
|
|
|
|
this, SIGNAL(wantedChanged(const QSet<int>&, bool)));
|
2013-01-20 01:31:58 +00:00
|
|
|
|
2013-01-25 18:37:11 +00:00
|
|
|
connect (&myModel, SIGNAL(pathEdited(const QString&, const QString&)),
|
|
|
|
this, SIGNAL(pathEdited(const QString&, const QString&)));
|
2013-09-08 19:03:25 +00:00
|
|
|
|
|
|
|
connect (&myModel, SIGNAL (openRequested (const QString&)),
|
|
|
|
this, SLOT (onOpenRequested (const QString&)),
|
|
|
|
Qt::QueuedConnection);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeView::~FileTreeView ()
|
2010-07-28 20:17:16 +00:00
|
|
|
{
|
2013-01-25 18:37:11 +00:00
|
|
|
myProxy->deleteLater();
|
2010-07-28 20:17:16 +00:00
|
|
|
}
|
|
|
|
|
2010-07-28 18:53:25 +00:00
|
|
|
void
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeView::onClicked (const QModelIndex& proxyIndex)
|
2010-07-28 18:53:25 +00:00
|
|
|
{
|
2013-01-25 23:34:20 +00:00
|
|
|
const QModelIndex modelIndex = myProxy->mapToSource (proxyIndex);
|
|
|
|
myModel.clicked (modelIndex);
|
2010-07-28 18:53:25 +00:00
|
|
|
}
|
|
|
|
|
2013-09-08 19:03:25 +00:00
|
|
|
void
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeView::onDoubleClicked (const QModelIndex& proxyIndex)
|
2013-09-08 19:03:25 +00:00
|
|
|
{
|
|
|
|
const QModelIndex modelIndex = myProxy->mapToSource (proxyIndex);
|
|
|
|
myModel.doubleClicked (modelIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeView::onOpenRequested (const QString& path)
|
2013-09-08 19:03:25 +00:00
|
|
|
{
|
|
|
|
if (state () == EditingState)
|
|
|
|
return;
|
|
|
|
|
|
|
|
emit openRequested (path);
|
|
|
|
}
|
|
|
|
|
2010-01-05 23:47:50 +00:00
|
|
|
bool
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeView::eventFilter (QObject * o, QEvent * event)
|
2013-01-25 18:37:11 +00:00
|
|
|
{
|
|
|
|
// 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...
|
2013-01-28 07:01:58 +00:00
|
|
|
if ((o == this) && (event->type() == QEvent::Resize))
|
2013-01-25 18:37:11 +00:00
|
|
|
{
|
|
|
|
QResizeEvent * r = dynamic_cast<QResizeEvent*>(event);
|
|
|
|
int left = r->size().width();
|
|
|
|
const QFontMetrics fontMetrics(font());
|
2013-01-28 07:01:58 +00:00
|
|
|
for (int column=FIRST_VISIBLE_COLUMN; column<=LAST_VISIBLE_COLUMN; ++column)
|
2013-01-25 18:37:11 +00:00
|
|
|
{
|
|
|
|
if (column == COL_NAME)
|
|
|
|
continue;
|
2013-01-25 18:54:06 +00:00
|
|
|
if (isColumnHidden (column))
|
2013-01-25 18:37:11 +00:00
|
|
|
continue;
|
2013-01-25 18:54:06 +00:00
|
|
|
|
2013-02-09 20:51:17 +00:00
|
|
|
QString header;
|
|
|
|
if (column == COL_SIZE)
|
|
|
|
header = "999.9 KiB";
|
|
|
|
else
|
|
|
|
header = myModel.headerData (column, Qt::Horizontal).toString();
|
|
|
|
header += " ";
|
2013-01-25 18:37:11 +00:00
|
|
|
const int width = fontMetrics.size (0, header).width();
|
|
|
|
setColumnWidth (column, width);
|
2009-04-09 18:55:47 +00:00
|
|
|
left -= width;
|
|
|
|
}
|
2013-01-25 18:37:11 +00:00
|
|
|
left -= 20; // not sure why this is necessary. it works in different themes + font sizes though...
|
|
|
|
setColumnWidth(COL_NAME, std::max(left,0));
|
2010-11-13 22:07:19 +00:00
|
|
|
}
|
|
|
|
|
2013-09-08 19:07:31 +00:00
|
|
|
// handle using the keyboard to toggle the
|
|
|
|
// wanted/unwanted state or the file priority
|
|
|
|
else if (event->type () == QEvent::KeyPress && state () != EditingState)
|
|
|
|
{
|
|
|
|
switch (static_cast<QKeyEvent*> (event)->key ())
|
|
|
|
{
|
|
|
|
case Qt::Key_Space:
|
|
|
|
foreach (const QModelIndex& i, selectionModel ()->selectedRows (COL_WANTED))
|
|
|
|
clicked (i);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Qt::Key_Enter:
|
|
|
|
case Qt::Key_Return:
|
|
|
|
foreach (const QModelIndex& i, selectionModel ()->selectedRows (COL_PRIORITY))
|
|
|
|
clicked (i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-25 18:37:11 +00:00
|
|
|
return false;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2010-05-05 09:41:14 +00:00
|
|
|
void
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeView::update (const FileList& files, bool updateFields)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-01-25 18:37:11 +00:00
|
|
|
foreach (const TrFile file, files)
|
|
|
|
{
|
|
|
|
QList<QModelIndex> added;
|
2013-01-25 23:34:20 +00:00
|
|
|
myModel.addFile (file.index, file.filename, file.wanted, file.priority, file.size, file.have, added, updateFields);
|
2013-01-25 18:37:11 +00:00
|
|
|
foreach (QModelIndex i, added)
|
2013-01-25 18:54:06 +00:00
|
|
|
expand (myProxy->mapFromSource(i));
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-12-12 23:05:10 +00:00
|
|
|
FileTreeView::clear ()
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-01-25 18:37:11 +00:00
|
|
|
myModel.clear();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|