#include "SelectItemProxyModel.h"
SelectItemProxyModel
::SelectItemProxyModel(QObject *parent
) : IdentityFileSystemProxyModel
(parent
),
m_dataFetchingInProgress(false),
{
}
{
//Returns data from the source model.
if (role == Qt::CheckStateRole) return m_checkTable.contains(index)? Qt::Checked : Qt::Unchecked;
//Removes all decoration are removed from the source model.
if (role == Qt::DecorationRole) return m_noData;
//Returns data from the source model.
return m_model->data(mapToSource(index), role);
}
Qt
::ItemFlags SelectItemProxyModel
::flags(const QModelIndex &index
) const{
if (m_dataFetchingInProgress == true)
{
// Prevents the data to be modified by the user when it is being updated by loadAllDataUnder()
return Qt::NoItemFlags;
}
else
{
//Sets all elements as user checkable.
return m_model->flags(mapToSource(index)) | Qt::ItemIsUserCheckable;
}
}
{
//Sets check state (checked or unchecked) on the specified proxy's item and all items under.
if (role == Qt::CheckStateRole)
{
//Sets check state (checked or unchecked) of the specified proxy's item.
if(value == Qt::Checked) m_checkTable.insert(index); else m_checkTable.remove(index);
emit dataChanged(index, index);
//Sets check state (checked or unchecked) of all the items under.
loadAllDataUnder(index);
return true;
}
// regular QFileSystemModel case.
return m_model->setData(mapToSource(index), value, role);
}
void SelectItemProxyModel
::loadAllDataUnder(const QModelIndex &index
) {
m_loadAllDataUnderIndex = index;
//If the specifed index does not have children if a "data loading process" is already running, do not
//start a new one.
if ((hasChildren(index))||(m_dataFetchingInProgress == false))
{
m_checkValueToUpdate = data(index, Qt::CheckStateRole);
m_dataFetchingInProgress = true;
emit allDataFetchingStart(index);
if (canFetchMore(index))
{
fetchMore(index);
}
else
{
// If the data of the current index has alreday been loaded, send a directoryLoaded signal so the item can
// still be processed in loadAllDataUnder_itteration().
emit directoryLoaded(filePath(m_loadAllDataUnderIndex));
}
}
}
// Processes the data of the last elmt of m_indexesToBeFetched and feeds m_indexesToBeFetched with the next items to be fetched until
// m_indexesToBeFetched is empty.
void SelectItemProxyModel
::loadAllDataUnder_itteration(const QString ¤tPath
) {
//Check if the loaded data is relevant for loadAllDataUnder_itteration()
if(m_dataFetchingInProgress == false)return;
QModelIndex currentIndex
= m_indexesToBeFetched
[m_indexesToBeFetched.
size()-1];
if(filePath(currentIndex) != currentPath) return;
//Remove currentIndex from the m_indexesToBeFetched queue.
m_indexesToBeFetched.pop_back();
int elmtCount = rowCount(currentIndex);
QModelIndex indexLast
= index
(elmtCount
-1,
0,currentIndex
);
//Examine the children of the current index.
for (int i(0); i < elmtCount; i++)
{
QModelIndex currentChildIndex
= index
(i,
0,currentIndex
);
//Check or uncheck elmts depending on m_checkValueToUpdate value. (value of the top parent set by the user)
if (m_checkValueToUpdate == Qt::Checked) m_checkTable.insert(currentChildIndex); else m_checkTable.remove(currentChildIndex);
//Queue the elmts that have chidren (directories) in m_indexesToBeFetched for next rounds.
if (hasChildren(currentChildIndex))m_indexesToBeFetched.push_back(currentChildIndex);
}
emit dataChanged(indexFirst, indexLast);
if (m_indexesToBeFetched.empty())
{
//All the nodes and branches branches have been explored. All the data has been loaded into the view.
m_dataFetchingInProgress = false;
emit allDataFetchingStop(m_loadAllDataUnderIndex);
}
else
{
// Fetch the data of the last element in m_indexesToBeFetched.
currentIndex = m_indexesToBeFetched[m_indexesToBeFetched.size()-1];
if(canFetchMore(currentIndex))
{
fetchMore(currentIndex);
}
else
{
// If the data of the current index has alreday been loaded, send a directoryLoaded signal so the next item in the process
// can be processed else fetch data of the current index.
emit directoryLoaded(filePath(currentIndex));
}
}
}
//Slot called when the model has finished loading data under an index.
void SelectItemProxyModel
::dataReady(const QString ¤tPath
) {
loadAllDataUnder_itteration(currentPath);
}