Commit 43cd2a61 authored by Daniel Teske's avatar Daniel Teske Committed by Tobias Hunger

FlatModel: Fix Node::setPath() and FolderNode::setDisplayName

Both can lead to a change in ordering. This adds the necessary
QAbstractItemModel signals that need to be emitted and the necessary
updates to the internal data structures.

Change-Id: I23824d839ddd4a615eb5bc3bdfe68ab42ed89a9e
Reviewed-by: default avatarTobias Hunger <tobias.hunger@digia.com>
parent 0d6e20c6
......@@ -198,6 +198,11 @@ FlatModel::FlatModel(SessionNode *rootNode, QObject *parent)
connect(watcher, SIGNAL(filesRemoved()),
this, SLOT(filesRemoved()));
connect(watcher, SIGNAL(nodeSortKeyAboutToChange(Node*)),
this, SLOT(nodeSortKeyAboutToChange(Node*)));
connect(watcher, SIGNAL(nodeSortKeyChanged()),
this, SLOT(nodeSortKeyChanged()));
connect(watcher, SIGNAL(nodeUpdated(ProjectExplorer::Node*)),
this, SLOT(nodeUpdated(ProjectExplorer::Node*)));
}
......@@ -816,6 +821,28 @@ void FlatModel::removeFromCache(QList<FolderNode *> list)
}
}
void FlatModel::changedSortKey(FolderNode *folderNode, Node *node)
{
QList<Node *> nodes = m_childNodes.value(folderNode);
int oldIndex = nodes.indexOf(node);
nodes.removeAt(oldIndex);
QList<Node *>::iterator newPosIt = qLowerBound(nodes.begin(), nodes.end(), node, sortNodes);
int newIndex = newPosIt - nodes.begin();
if (newIndex == oldIndex)
return;
nodes.insert(newPosIt, node);
QModelIndex parentIndex = indexForNode(folderNode);
if (newIndex > oldIndex)
++newIndex; // see QAbstractItemModel::beginMoveRows
beginMoveRows(parentIndex, oldIndex, oldIndex, parentIndex, newIndex);
m_childNodes[folderNode] = nodes;
endMoveRows();
}
void FlatModel::foldersRemoved()
{
// Do nothing
......@@ -856,6 +883,17 @@ void FlatModel::filesRemoved()
// Do nothing
}
void FlatModel::nodeSortKeyAboutToChange(Node *node)
{
m_nodeForSortKeyChange = node;
}
void FlatModel::nodeSortKeyChanged()
{
FolderNode *folderNode = visibleFolderNode(m_nodeForSortKeyChange->parentFolderNode());
changedSortKey(folderNode, m_nodeForSortKeyChange);
}
void FlatModel::nodeUpdated(Node *node)
{
QModelIndex idx = indexForNode(node);
......
......@@ -93,12 +93,16 @@ private slots:
void filesAboutToBeRemoved(FolderNode *folder, const QList<FileNode*> &staleFiles);
void filesRemoved();
void nodeSortKeyAboutToChange(Node *node);
void nodeSortKeyChanged();
void nodeUpdated(ProjectExplorer::Node *node);
private:
void added(FolderNode* folderNode, const QList<Node*> &newNodeList);
void removed(FolderNode* parentNode, const QList<Node*> &newNodeList);
void removeFromCache(QList<FolderNode *> list);
void changedSortKey(FolderNode *folderNode, Node *node);
void fetchMore(FolderNode *foldernode) const;
void recursiveAddFolderNodes(FolderNode *startNode, QList<Node *> *list, const QSet<Node *> &blackList = QSet<Node*>()) const;
......@@ -117,6 +121,7 @@ private:
ProjectNode *m_startupProject;
FolderNode *m_parentFolderForChange;
Node *m_nodeForSortKeyChange;
friend class FlatModelManager;
};
......
......@@ -73,6 +73,22 @@ Node::Node(NodeType nodeType,
}
void Node::emitNodeSortKeyAboutToChange()
{
if (ProjectNode *project = projectNode()) {
foreach (NodesWatcher *watcher, project->watchers())
emit watcher->nodeSortKeyAboutToChange(this);
}
}
void Node::emitNodeSortKeyChanged()
{
if (ProjectNode *project = projectNode()) {
foreach (NodesWatcher *watcher, project->watchers())
emit watcher->nodeSortKeyChanged();
}
}
/*!
* \brief The path of the file representing this node.
*
......@@ -80,7 +96,13 @@ Node::Node(NodeType nodeType,
*/
void Node::setPath(const QString &path)
{
if (m_path == path)
return;
emitNodeSortKeyAboutToChange();
m_path = path;
emitNodeSortKeyChanged();
emitNodeUpdated();
}
NodeType Node::nodeType() const
......@@ -149,8 +171,9 @@ void Node::setProjectNode(ProjectNode *project)
void Node::emitNodeUpdated()
{
foreach (NodesWatcher *watcher, projectNode()->watchers())
emit watcher->nodeUpdated(this);
if (ProjectNode *node = projectNode())
foreach (NodesWatcher *watcher, node->watchers())
emit watcher->nodeUpdated(this);
}
void Node::setParentFolderNode(FolderNode *parentFolder)
......@@ -248,7 +271,12 @@ void FolderNode::accept(NodesVisitor *visitor)
void FolderNode::setDisplayName(const QString &name)
{
if (m_displayName == name)
return;
emitNodeSortKeyAboutToChange();
m_displayName = name;
emitNodeSortKeyChanged();
emitNodeUpdated();
}
void FolderNode::setIcon(const QIcon &icon)
......
......@@ -91,7 +91,7 @@ public:
virtual QString tooltip() const;
virtual bool isEnabled() const;
void setPath(const QString &path); // this does not call emitNodeUpdated!
void setPath(const QString &path);
void emitNodeUpdated();
protected:
......@@ -101,6 +101,9 @@ protected:
void setProjectNode(ProjectNode *project);
void setParentFolderNode(FolderNode *parentFolder);
void emitNodeSortKeyAboutToChange();
void emitNodeSortKeyChanged();
private:
NodeType m_nodeType;
ProjectNode *m_projectNode;
......@@ -327,6 +330,8 @@ signals:
void filesAboutToBeRemoved(FolderNode *folder,
const QList<FileNode*> &staleFiles);
void filesRemoved();
void nodeSortKeyAboutToChange(Node *node);
void nodeSortKeyChanged();
private:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment