m_roles = roles;
if (count() > 0) {
- const bool supportedExpanding = m_requestRole[IsExpandedRole] && m_requestRole[ExpansionLevelRole];
- const bool willSupportExpanding = roles.contains("isExpanded") && roles.contains("expansionLevel");
+ const bool supportedExpanding = m_requestRole[ExpansionLevelRole];
+ const bool willSupportExpanding = roles.contains("expansionLevel");
if (supportedExpanding && !willSupportExpanding) {
// No expanding is supported anymore. Take care to delete all items that have an expansion level
// that is not 0 (and hence are part of an expanded item).
bool KFileItemModel::setExpanded(int index, bool expanded)
{
- if (isExpanded(index) == expanded || index < 0 || index >= count()) {
+ if (!isExpandable(index) || isExpanded(index) == expanded) {
return false;
}
bool KFileItemModel::isExpandable(int index) const
{
if (index >= 0 && index < count()) {
- return m_itemData.at(index)->item.isDir();
+ return m_itemData.at(index)->values.value("isExpandable").toBool();
}
return false;
}
void KFileItemModel::setExpanded(const QSet<KUrl>& urls)
{
-
const KDirLister* dirLister = m_dirLister.data();
if (!dirLister) {
return;
void KFileItemModel::slotNewItems(const KFileItemList& items)
{
+ Q_ASSERT(!items.isEmpty());
+
if (m_requestRole[ExpansionLevelRole] && m_rootExpansionLevel >= 0) {
- // If the expanding of items is enabled in the model, it might be
- // possible that the call dirLister->openUrl(url, KDirLister::Keep) in
- // KFileItemModel::setExpanded() results in emitting of the same items
- // twice due to the Keep-parameter. This case happens if an item gets
- // expanded, collapsed and expanded again before the items could be loaded
- // for the first expansion.
- foreach (const KFileItem& item, items) {
- const int index = m_items.value(item.url(), -1);
- if (index >= 0) {
- // The items are already part of the model.
- return;
- }
+ // To be able to compare whether the new items may be inserted as children
+ // of a parent item the pending items must be added to the model first.
+ dispatchPendingItemsToInsert();
+
+ KFileItem item = items.first();
+
+ // If the expanding of items is enabled, the call
+ // dirLister->openUrl(url, KDirLister::Keep) in KFileItemModel::setExpanded()
+ // might result in emitting the same items twice due to the Keep-parameter.
+ // This case happens if an item gets expanded, collapsed and expanded again
+ // before the items could be loaded for the first expansion.
+ const int index = m_items.value(item.url(), -1);
+ if (index >= 0) {
+ // The items are already part of the model.
+ return;
+ }
+
+ // KDirLister keeps the children of items that got expanded once even if
+ // they got collapsed again with KFileItemModel::setExpanded(false). So it must be
+ // checked whether the parent for new items is still expanded.
+ KUrl parentUrl = item.url().upUrl();
+ parentUrl.adjustPath(KUrl::RemoveTrailingSlash);
+ const int parentIndex = m_items.value(parentUrl, -1);
+ if (parentIndex >= 0 && !m_itemData[parentIndex]->values.value("isExpanded").toBool()) {
+ // The parent is not expanded.
+ return;
}
}
rolesHash.insert("rating", RatingRole);
rolesHash.insert("isDir", IsDirRole);
rolesHash.insert("isExpanded", IsExpandedRole);
+ rolesHash.insert("isExpandable", IsExpandableRole);
rolesHash.insert("expansionLevel", ExpansionLevelRole);
}
return rolesHash.value(role, NoRole);
data.insert("isExpanded", false);
}
+ if (m_requestRole[IsExpandableRole]) {
+ data.insert("isExpandable", item.isDir() && item.url() == item.targetUrl());
+ }
+
if (m_requestRole[ExpansionLevelRole]) {
if (m_rootExpansionLevel == UninitializedRootExpansionLevel && m_dirLister.data()) {
const KUrl rootUrl = m_dirLister.data()->url();
const QString protocol = rootUrl.protocol();
const bool forceRootExpansionLevel = (protocol == QLatin1String("trash") ||
protocol == QLatin1String("nepomuk") ||
+ protocol == QLatin1String("remote") ||
protocol.contains(QLatin1String("search")));
if (forceRootExpansionLevel) {
m_rootExpansionLevel = ForceRootExpansionLevel;