diff options
| author | Peter Penz <[email protected]> | 2012-03-13 16:26:16 +0100 |
|---|---|---|
| committer | Peter Penz <[email protected]> | 2012-03-13 16:29:45 +0100 |
| commit | f041099904d809b5370285185fbbb2303c40b953 (patch) | |
| tree | 5070608d5c19033a119bec98b9466d889bd8bd7c /src/kitemviews | |
| parent | 6c60bf0ad54258976d9dcbd6586b26c65d8d4b78 (diff) | |
Remove RolesInfoAccessor
Now KFileItemModel provides a way to access the available roles
including their translations. Note that the 3 roles "comments",
"rating" and "tags" have not been implemented yet in KFileItemModel
and turning them on does not work currently.
Diffstat (limited to 'src/kitemviews')
| -rw-r--r-- | src/kitemviews/kfileitemmodel.cpp | 164 | ||||
| -rw-r--r-- | src/kitemviews/kfileitemmodel.h | 43 |
2 files changed, 133 insertions, 74 deletions
diff --git a/src/kitemviews/kfileitemmodel.cpp b/src/kitemviews/kfileitemmodel.cpp index 23cd7cd75..428587e73 100644 --- a/src/kitemviews/kfileitemmodel.cpp +++ b/src/kitemviews/kfileitemmodel.cpp @@ -257,34 +257,21 @@ int KFileItemModel::indexForKeyboardSearch(const QString& text, int startFromInd bool KFileItemModel::supportsDropping(int index) const { const KFileItem item = fileItem(index); - return item.isNull() ? false : item.isDir() || item.isDesktopFile(); + return !item.isNull() && (item.isDir() || item.isDesktopFile()); } QString KFileItemModel::roleDescription(const QByteArray& role) const { - QString descr; - - switch (roleIndex(role)) { - case NameRole: descr = i18nc("@item:intable", "Name"); break; - case SizeRole: descr = i18nc("@item:intable", "Size"); break; - case DateRole: descr = i18nc("@item:intable", "Date"); break; - case PermissionsRole: descr = i18nc("@item:intable", "Permissions"); break; - case OwnerRole: descr = i18nc("@item:intable", "Owner"); break; - case GroupRole: descr = i18nc("@item:intable", "Group"); break; - case TypeRole: descr = i18nc("@item:intable", "Type"); break; - case DestinationRole: descr = i18nc("@item:intable", "Destination"); break; - case PathRole: descr = i18nc("@item:intable", "Path"); break; - case CommentRole: descr = i18nc("@item:intable", "Comment"); break; - case TagsRole: descr = i18nc("@item:intable", "Tags"); break; - case RatingRole: descr = i18nc("@item:intable", "Rating"); break; - case NoRole: break; - case IsDirRole: break; - case IsExpandedRole: break; - case ExpandedParentsCountRole: break; - default: Q_ASSERT(false); break; + static QHash<QByteArray, QString> description; + if (description.isEmpty()) { + int count = 0; + const RoleInfoMap* map = rolesInfoMap(count); + for (int i = 0; i < count; ++i) { + description.insert(map[i].role, map[i].roleTranslation); + } } - return descr; + return description.value(role); } QList<QPair<int, QVariant> > KFileItemModel::groups() const @@ -294,7 +281,7 @@ QList<QPair<int, QVariant> > KFileItemModel::groups() const QElapsedTimer timer; timer.start(); #endif - switch (roleIndex(sortRole())) { + switch (typeForRole(sortRole())) { case NameRole: m_groups = nameRoleGroups(); break; case SizeRole: m_groups = sizeRoleGroups(); break; case DateRole: m_groups = dateRoleGroups(); break; @@ -393,7 +380,7 @@ void KFileItemModel::setRoles(const QSet<QByteArray>& roles) QSetIterator<QByteArray> it(roles); while (it.hasNext()) { const QByteArray& role = it.next(); - m_requestRole[roleIndex(role)] = true; + m_requestRole[typeForRole(role)] = true; } if (count() > 0) { @@ -571,6 +558,26 @@ QString KFileItemModel::nameFilter() const return m_filter.pattern(); } +QList<KFileItemModel::RoleInfo> KFileItemModel::rolesInformation() +{ + static QList<RoleInfo> rolesInfo; + if (rolesInfo.isEmpty()) { + int count = 0; + const RoleInfoMap* map = rolesInfoMap(count); + for (int i = 0; i < count; ++i) { + if (map[i].roleType != NoRole) { + RoleInfo info; + info.role = map[i].role; + info.translation = map[i].roleTranslation; + info.group = map[i].groupTranslation; + rolesInfo.append(info); + } + } + } + + return rolesInfo; +} + void KFileItemModel::onGroupedSortingChanged(bool current) { Q_UNUSED(current); @@ -580,7 +587,7 @@ void KFileItemModel::onGroupedSortingChanged(bool current) void KFileItemModel::onSortRoleChanged(const QByteArray& current, const QByteArray& previous) { Q_UNUSED(previous); - m_sortRole = roleIndex(current); + m_sortRole = typeForRole(current); #ifdef KFILEITEMMODEL_DEBUG if (!m_requestRole[m_sortRole]) { @@ -1110,52 +1117,54 @@ void KFileItemModel::resetRoles() } } -KFileItemModel::Role KFileItemModel::roleIndex(const QByteArray& role) const +KFileItemModel::RoleType KFileItemModel::typeForRole(const QByteArray& role) const { - static QHash<QByteArray, Role> rolesHash; - if (rolesHash.isEmpty()) { - rolesHash.insert("name", NameRole); - rolesHash.insert("size", SizeRole); - rolesHash.insert("date", DateRole); - rolesHash.insert("permissions", PermissionsRole); - rolesHash.insert("owner", OwnerRole); - rolesHash.insert("group", GroupRole); - rolesHash.insert("type", TypeRole); - rolesHash.insert("destination", DestinationRole); - rolesHash.insert("path", PathRole); - rolesHash.insert("comment", CommentRole); - rolesHash.insert("tags", TagsRole); - rolesHash.insert("rating", RatingRole); - rolesHash.insert("isDir", IsDirRole); - rolesHash.insert("isExpanded", IsExpandedRole); - rolesHash.insert("isExpandable", IsExpandableRole); - rolesHash.insert("expandedParentsCount", ExpandedParentsCountRole); + static QHash<QByteArray, RoleType> roles; + if (roles.isEmpty()) { + // Insert user visible roles that can be accessed with + // KFileItemModel::roleInformation() + int count = 0; + const RoleInfoMap* map = rolesInfoMap(count); + for (int i = 0; i < count; ++i) { + roles.insert(map[i].role, map[i].roleType); + } + + // Insert internal roles (take care to synchronize the implementation + // with KFileItemModel::roleForType() in case if a change is done). + roles.insert("isDir", IsDirRole); + roles.insert("isExpanded", IsExpandedRole); + roles.insert("isExpandable", IsExpandableRole); + roles.insert("expandedParentsCount", ExpandedParentsCountRole); + + Q_ASSERT(roles.count() == RolesCount); } - return rolesHash.value(role, NoRole); + + return roles.value(role, NoRole); } -QByteArray KFileItemModel::roleByteArray(Role role) const +QByteArray KFileItemModel::roleForType(RoleType roleType) const { - static const char* const roles[RolesCount] = { - 0, // NoRole - "name", - "size", - "date", - "permissions", - "owner", - "group", - "type", - "destination", - "path", - "comment", - "tags", - "rating", - "isDir", - "isExpanded", - "isExpandable", - "expandedParentsCount" + static QHash<RoleType, QByteArray> roles; + if (roles.isEmpty()) { + // Insert user visible roles that can be accessed with + // KFileItemModel::roleInformation() + int count = 0; + const RoleInfoMap* map = rolesInfoMap(count); + for (int i = 0; i < count; ++i) { + roles.insert(map[i].roleType, map[i].role); + } + + // Insert internal roles (take care to synchronize the implementation + // with KFileItemModel::typeForRole() in case if a change is done). + roles.insert(IsDirRole, "isDir"); + roles.insert(IsExpandedRole, "isExpanded"); + roles.insert(IsExpandableRole, "isExpandable"); + roles.insert(ExpandedParentsCountRole, "expandedParentsCount"); + + Q_ASSERT(roles.count() == RolesCount); }; - return roles[role]; + + return roles.value(roleType); } QHash<QByteArray, QVariant> KFileItemModel::retrieveData(const KFileItem& item) const @@ -1364,7 +1373,7 @@ int KFileItemModel::sortRoleCompare(const ItemData* a, const ItemData* b) const case PathRole: case CommentRole: case TagsRole: { - const QByteArray role = roleByteArray(m_sortRole); + const QByteArray role = roleForType(m_sortRole); result = QString::compare(a->values.value(role).toString(), b->values.value(role).toString()); break; @@ -1960,4 +1969,27 @@ KFileItemList KFileItemModel::childItems(const KFileItem& item) const return items; } +const KFileItemModel::RoleInfoMap* KFileItemModel::rolesInfoMap(int& count) +{ + static const RoleInfoMap rolesInfoMap[] = { + // role roleType role translation group translation + { 0, NoRole, 0, 0, 0, 0 }, + { "name", NameRole, I18N_NOOP2_NOSTRIP("@label", "Name"), 0, 0 }, + { "size", SizeRole, I18N_NOOP2_NOSTRIP("@label", "Size"), 0, 0 }, + { "date", DateRole, I18N_NOOP2_NOSTRIP("@label", "Date"), 0, 0 }, + { "permissions", PermissionsRole, I18N_NOOP2_NOSTRIP("@label", "Permissions"), 0, 0 }, + { "owner", OwnerRole, I18N_NOOP2_NOSTRIP("@label", "Owner"), 0, 0 }, + { "group", GroupRole, I18N_NOOP2_NOSTRIP("@label", "Group"), 0, 0 }, + { "type", TypeRole, I18N_NOOP2_NOSTRIP("@label", "Type"), 0, 0 }, + { "destination", DestinationRole, I18N_NOOP2_NOSTRIP("@label", "Link Destination"), 0, 0 }, + { "path", PathRole, I18N_NOOP2_NOSTRIP("@label", "Path"), 0, 0 }, + { "comment", CommentRole, I18N_NOOP2_NOSTRIP("@label", "Comment"), 0, 0 }, + { "tags", TagsRole, I18N_NOOP2_NOSTRIP("@label", "Tags"), 0, 0 }, + { "rating", RatingRole, I18N_NOOP2_NOSTRIP("@label", "Rating"), 0, 0 } + }; + + count = sizeof(rolesInfoMap) / sizeof(RoleInfoMap); + return rolesInfoMap; +} + #include "kfileitemmodel.moc" diff --git a/src/kitemviews/kfileitemmodel.h b/src/kitemviews/kfileitemmodel.h index bb77003c3..5d7a7fc8b 100644 --- a/src/kitemviews/kfileitemmodel.h +++ b/src/kitemviews/kfileitemmodel.h @@ -90,7 +90,7 @@ public: virtual bool supportsDropping(int index) const; /** @reimp */ - virtual QString roleDescription(const QByteArray& role) const; + virtual QString roleDescription(const QByteArray& typeForRole) const; /** @reimp */ virtual QList<QPair<int, QVariant> > groups() const; @@ -158,6 +158,14 @@ public: void setNameFilter(const QString& nameFilter); QString nameFilter() const; + struct RoleInfo + { QByteArray role; + QString translation; + QString group; + }; + + static QList<RoleInfo> rolesInformation(); + signals: /** * Is emitted after the loading of a directory has been completed or new @@ -192,7 +200,7 @@ private slots: void dispatchPendingItemsToInsert(); private: - enum Role { + enum RoleType { NoRole, NameRole, SizeRole, @@ -239,16 +247,16 @@ private: void resetRoles(); /** - * @return Role-index for the given role byte-array. + * @return Role-type for the given role. * Runtime complexity is O(1). */ - Role roleIndex(const QByteArray& role) const; + RoleType typeForRole(const QByteArray& role) const; /** - * @return Role-byte-array for the given role-index. + * @return Role-byte-array for the given role-type. * Runtime complexity is O(1). */ - QByteArray roleByteArray(Role role) const; + QByteArray roleForType(RoleType roleType) const; QHash<QByteArray, QVariant> retrieveData(const KFileItem& item) const; @@ -314,7 +322,7 @@ private: QList<QPair<int, QVariant> > dateRoleGroups() const; QList<QPair<int, QVariant> > permissionRoleGroups() const; QList<QPair<int, QVariant> > ratingRoleGroups() const; - QList<QPair<int, QVariant> > genericStringRoleGroups(const QByteArray& role) const; + QList<QPair<int, QVariant> > genericStringRoleGroups(const QByteArray& typeForRole) const; /** * Helper method for all xxxRoleGroups() methods to check whether the @@ -331,13 +339,32 @@ private: */ KFileItemList childItems(const KFileItem& item) const; + /** + * Maps the QByteArray-roles to RoleTypes and provides translation- and + * group-contexts. + */ + struct RoleInfoMap + { + const char* const role; + const RoleType roleType; + const char* const roleTranslationContext; + const char* const roleTranslation; + const char* const groupTranslationContext; + const char* const groupTranslation; + }; + + /** + * @return Map of user visible roles that are accessible by KFileItemModel::rolesInformation(). + */ + static const RoleInfoMap* rolesInfoMap(int& count); + private: QWeakPointer<KDirLister> m_dirLister; bool m_naturalSorting; bool m_sortFoldersFirst; - Role m_sortRole; + RoleType m_sortRole; QSet<QByteArray> m_roles; Qt::CaseSensitivity m_caseSensitivity; |
