(qt) more futzing with file-tree

This commit is contained in:
Jordan Lee 2013-01-28 18:20:20 +00:00
parent ea6228d81b
commit a17d4d5e3f
2 changed files with 47 additions and 49 deletions

View File

@ -52,8 +52,9 @@ FileTreeItem :: getMyChildRows ()
// ensure that all the rows are hashed
while (myFirstUnhashedRow < n)
{
myChildRows.insert (myChildren[myFirstUnhashedRow]->name(), myFirstUnhashedRow);
myFirstUnhashedRow++;
myChildRows.insert (myChildren[myFirstUnhashedRow]->name(),
myFirstUnhashedRow);
++myFirstUnhashedRow;
}
return myChildRows;
@ -119,7 +120,7 @@ FileTreeItem :: data (int column, int role) const
if (column == COL_FILE_INDEX)
{
return myIndex;
return myFileIndex;
}
else if (role == Qt::EditRole)
{
@ -234,11 +235,15 @@ FileTreeItem :: update (const QString& name,
QString
FileTreeItem :: priorityString () const
{
const int i(priority());
if(i == LOW) return tr("Low");
if(i == HIGH) return tr("High");
if(i == NORMAL) return tr("Normal");
return tr("Mixed");
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");
}
}
int
@ -277,8 +282,8 @@ FileTreeItem :: setSubtreePriority (int i, QSet<int>& ids)
{
myPriority = i;
if (myIndex >= 0)
ids.insert (myIndex);
if (myFileIndex >= 0)
ids.insert (myFileIndex);
}
foreach (FileTreeItem * child, myChildren)
@ -331,8 +336,8 @@ FileTreeItem :: setSubtreeWanted (bool b, QSet<int>& ids)
{
myIsWanted = b;
if (myIndex >= 0)
ids.insert(myIndex);
if (myFileIndex >= 0)
ids.insert(myFileIndex);
}
foreach (FileTreeItem * child, myChildren)
@ -365,16 +370,19 @@ FileTreeModel :: ~FileTreeModel()
delete myRootItem;
}
FileTreeItem *
FileTreeModel :: itemFromIndex (const QModelIndex& index) const
{
return static_cast<FileTreeItem*>(index.internalPointer());
}
QVariant
FileTreeModel :: data (const QModelIndex &index, int role) const
{
QVariant value;
if (index.isValid())
{
FileTreeItem * i = static_cast<FileTreeItem*>(index.internalPointer());
value = i->data (index.column(), role);
}
value = itemFromIndex(index)->data (index.column(), role);
return value;
}
@ -400,7 +408,7 @@ FileTreeModel :: setData (const QModelIndex& index, const QVariant& newname, int
{
QString oldpath;
QModelIndex walk = index;
FileTreeItem * item = static_cast<FileTreeItem*>(index.internalPointer());
FileTreeItem * item = itemFromIndex (index);
while (item && !item->name().isEmpty())
{
@ -466,7 +474,7 @@ FileTreeModel :: index (int row, int column, const QModelIndex& parent) const
if(!parent.isValid())
parentItem = myRootItem;
else
parentItem = static_cast<FileTreeItem*>(parent.internalPointer());
parentItem = itemFromIndex (parent);
FileTreeItem * childItem = parentItem->child(row);
@ -486,12 +494,12 @@ FileTreeModel :: parent (const QModelIndex& child) const
QModelIndex
FileTreeModel :: parent (const QModelIndex& child, int column) const
{
if (!child.isValid())
return QModelIndex();
QModelIndex parent;
FileTreeItem * childItem = static_cast<FileTreeItem*>(child.internalPointer());
if (child.isValid())
parent = indexOf (itemFromIndex(child)->parent(), column);
return indexOf (childItem->parent(), column);
return parent;
}
int
@ -499,10 +507,10 @@ FileTreeModel :: rowCount (const QModelIndex& parent) const
{
FileTreeItem * parentItem;
if (!parent.isValid())
parentItem = myRootItem;
if (parent.isValid())
parentItem = itemFromIndex (parent);
else
parentItem = static_cast<FileTreeItem*>(parent.internalPointer());
parentItem = myRootItem;
return parentItem->childCount();
}
@ -532,7 +540,7 @@ FileTreeModel :: clearSubtree (const QModelIndex& top)
while (i > 0)
clearSubtree(index(--i, 0, top));
delete static_cast<FileTreeItem*>(top.internalPointer());
delete static_cast<FileTreeItem*>(itemFromIndex(top));
}
void
@ -558,7 +566,7 @@ FileTreeModel :: findItemForFileIndex (int fileIndex) const
{
QModelIndex& index = indices.front ();
if (index.isValid())
ret = static_cast<FileTreeItem*>(index.internalPointer());
ret = itemFromIndex (index);
}
return ret;
@ -678,7 +686,7 @@ FileTreeModel :: clicked (const QModelIndex& index)
QSet<int> file_ids;
FileTreeItem * item;
item = static_cast<FileTreeItem*>(index.internalPointer());
item = itemFromIndex (index);
item->twiddleWanted (file_ids, want);
emit wantedChanged (file_ids, want);
@ -692,7 +700,7 @@ FileTreeModel :: clicked (const QModelIndex& index)
QSet<int> file_ids;
FileTreeItem * item;
item = static_cast<FileTreeItem*>(index.internalPointer());
item = itemFromIndex (index);
item->twiddlePriority (file_ids, priority);
emit priorityChanged (file_ids, priority);
@ -904,12 +912,6 @@ FileTreeView :: eventFilter (QObject * o, QEvent * event)
return false;
}
void
FileTreeView :: update (const FileList& files)
{
update (files, true);
}
void
FileTreeView :: update (const FileList& files, bool updateFields)
{

View File

@ -10,8 +10,8 @@
* $Id$
*/
#ifndef QTR_TREE_FILE_MODEL
#define QTR_TREE_FILE_MODEL
#ifndef QTR_FILE_TREE
#define QTR_FILE_TREE
#include <QAbstractItemModel>
#include <QObject>
@ -44,7 +44,7 @@ class FileTreeItem: public QObject
virtual ~FileTreeItem();
FileTreeItem (const QString& name="", int fileIndex=-1, uint64_t size=0):
myIndex (fileIndex),
myFileIndex (fileIndex),
myParent (0),
myName (name),
myPriority (0),
@ -66,10 +66,8 @@ class FileTreeItem: public QObject
bool update (const QString& name, bool want, int priority, uint64_t have, bool updateFields);
void twiddleWanted (QSet<int>& fileIds, bool&);
void twiddlePriority (QSet<int>& fileIds, int&);
int fileIndex () const { return myIndex; }
int fileIndex () const { return myFileIndex; }
uint64_t totalSize () const { return myTotalSize; }
private:
void setSubtreePriority (int priority, QSet<int>& fileIds);
@ -81,7 +79,7 @@ class FileTreeItem: public QObject
int priority () const;
int isSubtreeWanted () const;
const int myIndex;
const int myFileIndex;
FileTreeItem * myParent;
QList<FileTreeItem*> myChildren;
QHash<QString,int> myChildRows;
@ -133,8 +131,7 @@ class FileTreeModel: public QAbstractItemModel
void parentsChanged (const QModelIndex &, int column);
void subtreeChanged (const QModelIndex &, int column);
FileTreeItem * findItemForFileIndex (int fileIndex) const;
FileTreeItem * itemFromIndex (const QModelIndex&) const;
private:
FileTreeItem * myRootItem;
@ -165,12 +162,11 @@ class FileTreeView: public QTreeView
FileTreeView (QWidget * parent=0, bool editable=true);
virtual ~FileTreeView ();
void clear ();
void update (const FileList& files);
void update (const FileList& files, bool torrentChanged);
void update (const FileList& files, bool updateProperties=true);
signals:
void priorityChanged (const QSet<int>& fileIndices, int);
void wantedChanged (const QSet<int>& fileIndices, bool);
void priorityChanged (const QSet<int>& fileIndices, int priority);
void wantedChanged (const QSet<int>& fileIndices, bool wanted);
void pathEdited (const QString& oldpath, const QString& newname);
protected:
@ -182,7 +178,7 @@ class FileTreeView: public QTreeView
FileTreeDelegate myDelegate;
public slots:
void onClicked (const QModelIndex & index);
void onClicked (const QModelIndex& index);
};
#endif