Skip to content

Instantly share code, notes, and snippets.

@pamaury
Created December 16, 2013 15:54
Show Gist options
  • Save pamaury/7989309 to your computer and use it in GitHub Desktop.
Save pamaury/7989309 to your computer and use it in GitHub Desktop.
#include "myview.h"
#include <QDebug>
MyFileSystemView::MyFileSystemView()
{
}
int MyFileSystemView::columnCount(const QModelIndex& parent) const
{
return 1;
}
QVariant MyFileSystemView::data(const QModelIndex& index, int role) const
{
if(role == Qt::CheckStateRole)
return getState(index);
else
return QFileSystemModel::data(index, role);
}
Qt::ItemFlags MyFileSystemView::flags(const QModelIndex& index) const
{
return QFileSystemModel::flags(index) | Qt::ItemIsUserCheckable;
}
bool MyFileSystemView::setData(const QModelIndex& index, const QVariant& value, int role)
{
if(role == Qt::CheckStateRole)
{
setState(index, static_cast<Qt::CheckState>(value.toInt()));
propagateCheckState(index.parent(), Qt::PartiallyChecked, false);
propagateCheckState(index, getState(index), true);
emit dataChanged(index, index);
return true;
}
else
return QFileSystemModel::setData(index, value, role);
}
void MyFileSystemView::propagateCheckState(const QModelIndex& index, Qt::CheckState state, bool down)
{
if(!index.isValid())
return;
setState(index, state);
emit dataChanged(index, index);
if(down)
{
for(int i = 0; i < rowCount(index); i++)
propagateCheckState(index.child(i, 0), state, down);
}
else
propagateCheckState(index.parent(), state, down);
}
#ifdef USE_PERSISTENT
Qt::CheckState MyFileSystemView::getState(const QModelIndex& index) const
{
if(m_checkstate.find(index) == m_checkstate.end())
m_checkstate[index] = Qt::Checked;
return m_checkstate[index];
}
void MyFileSystemView::setState(const QModelIndex& index, Qt::CheckState state)
{
m_checkstate[index] = state;
}
#else
Qt::CheckState MyFileSystemView::getState(const QModelIndex& index) const
{
if(m_checkstate.find(index.internalPointer()) == m_checkstate.end())
m_checkstate[index.internalPointer()] = Qt::Checked;
return m_checkstate[index.internalPointer()];
}
void MyFileSystemView::setState(const QModelIndex& index, Qt::CheckState state)
{
m_checkstate[index.internalPointer()] = state;
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment