From: Renato Araujo Oliveira Filho Date: Thu, 7 Dec 2017 14:36:00 +0000 (-0300) Subject: Implemented support for hide/show groups X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/commitdiff_plain/5f1df43b87898b380228a3548553de3290ddb0d7?ds=inline Implemented support for hide/show groups Summary: Added an option on PlacesPanel context menu to show or hide the entire group of places. Depends on D8855 Test Plan: Open Donlphin and use PlacesPanel context menu to hide and show groups Reviewers: franckarrecot, mlaurent, mwolff, elvisangelaccio Reviewed By: franckarrecot, mlaurent, mwolff, elvisangelaccio Subscribers: rkflx, mwolff, elvisangelaccio, ngraham, #dolphin Differential Revision: https://phabricator.kde.org/D9242 --- diff --git a/src/kitemviews/kitemlistcontroller.cpp b/src/kitemviews/kitemlistcontroller.cpp index 61f520641..130936488 100644 --- a/src/kitemviews/kitemlistcontroller.cpp +++ b/src/kitemviews/kitemlistcontroller.cpp @@ -179,6 +179,20 @@ KItemListController::MouseDoubleClickAction KItemListController::mouseDoubleClic return m_mouseDoubleClickAction; } +int KItemListController::indexCloseToMousePressedPosition() const +{ + QHashIterator it(m_view->m_visibleGroups); + while (it.hasNext()) { + it.next(); + KItemListGroupHeader *groupHeader = it.value(); + const QPointF mappedToGroup = groupHeader->mapFromItem(nullptr, m_pressedMousePos); + if (groupHeader->contains(mappedToGroup)) { + return it.key()->index(); + } + } + return -1; +} + void KItemListController::setAutoActivationDelay(int delay) { m_autoActivationTimer->setInterval(delay); diff --git a/src/kitemviews/kitemlistcontroller.h b/src/kitemviews/kitemlistcontroller.h index 7c48fb18c..3de5edece 100644 --- a/src/kitemviews/kitemlistcontroller.h +++ b/src/kitemviews/kitemlistcontroller.h @@ -108,6 +108,8 @@ public: void setMouseDoubleClickAction(MouseDoubleClickAction action); MouseDoubleClickAction mouseDoubleClickAction() const; + int indexCloseToMousePressedPosition() const; + /** * Sets the delay in milliseconds when dragging an object above an item * until the item gets activated automatically. A value of -1 indicates diff --git a/src/panels/places/placesitem.cpp b/src/panels/places/placesitem.cpp index c47385656..d7e87cf7a 100644 --- a/src/panels/places/placesitem.cpp +++ b/src/panels/places/placesitem.cpp @@ -102,6 +102,16 @@ bool PlacesItem::isHidden() const return dataValue("isHidden").toBool(); } +bool PlacesItem::isGroupHidden() const +{ + return dataValue("isGroupHidden").toBool(); +} + +void PlacesItem::setGroupHidden(bool hidden) +{ + setDataValue("isGroupHidden", hidden); +} + void PlacesItem::setSystemItem(bool isSystemItem) { setDataValue("isSystemItem", isSystemItem); diff --git a/src/panels/places/placesitem.h b/src/panels/places/placesitem.h index d32c616db..733265d46 100644 --- a/src/panels/places/placesitem.h +++ b/src/panels/places/placesitem.h @@ -52,6 +52,9 @@ public: void setHidden(bool hidden); bool isHidden() const; + void setGroupHidden(bool hidden); + bool isGroupHidden() const; + void setSystemItem(bool isSystemItem); bool isSystemItem() const; diff --git a/src/panels/places/placesitemlistwidget.cpp b/src/panels/places/placesitemlistwidget.cpp index 18e561864..b0b3fb2aa 100644 --- a/src/panels/places/placesitemlistwidget.cpp +++ b/src/panels/places/placesitemlistwidget.cpp @@ -30,7 +30,8 @@ PlacesItemListWidget::~PlacesItemListWidget() bool PlacesItemListWidget::isHidden() const { - return data().value("isHidden").toBool(); + return data().value("isHidden").toBool() || + data().value("isGroupHidden").toBool(); } QPalette::ColorRole PlacesItemListWidget::normalTextColorRole() const diff --git a/src/panels/places/placesitemmodel.cpp b/src/panels/places/placesitemmodel.cpp index 074b7ab45..577596c74 100644 --- a/src/panels/places/placesitemmodel.cpp +++ b/src/panels/places/placesitemmodel.cpp @@ -73,6 +73,7 @@ PlacesItemModel::PlacesItemModel(QObject* parent) : connect(m_sourceModel.data(), &KFilePlacesModel::dataChanged, this, &PlacesItemModel::onSourceModelDataChanged); connect(m_sourceModel.data(), &KFilePlacesModel::rowsAboutToBeMoved, this, &PlacesItemModel::onSourceModelRowsAboutToBeMoved); connect(m_sourceModel.data(), &KFilePlacesModel::rowsMoved, this, &PlacesItemModel::onSourceModelRowsMoved); + connect(m_sourceModel.data(), &KFilePlacesModel::groupHiddenChanged, this, &PlacesItemModel::onSourceModelGroupHiddenChanged); } PlacesItemModel::~PlacesItemModel() @@ -195,11 +196,13 @@ void PlacesItemModel::onItemChanged(int index, const QSet& changedRo qWarning() << "invalid item changed signal"; return; } - if (changedRoles.contains("isHidden")) { - m_sourceModel->setPlaceHidden(sourceIndex, changedItem->isHidden()); + if (m_sourceModel->isHidden(sourceIndex) != changedItem->isHidden()) { + m_sourceModel->setPlaceHidden(sourceIndex, changedItem->isHidden()); + } else { + m_sourceModel->refresh(); + } } - KStandardItemModel::onItemChanged(index, changedRoles); } @@ -470,6 +473,7 @@ void PlacesItemModel::updateItem(PlacesItem *item, const QModelIndex &index) { item->setGroup(index.data(KFilePlacesModel::GroupRole).toString()); item->setIcon(index.data(KFilePlacesModel::IconNameRole).toString()); + item->setGroupHidden(index.data(KFilePlacesModel::GroupHiddenRole).toBool()); } void PlacesItemModel::slotStorageTearDownDone(Solid::ErrorType error, const QVariant& errorData) @@ -590,6 +594,16 @@ void PlacesItemModel::onSourceModelDataChanged(const QModelIndex &topLeft, const } } +void PlacesItemModel::onSourceModelGroupHiddenChanged(KFilePlacesModel::GroupType group, bool hidden) +{ + for(const QModelIndex &sourceIndex : m_sourceModel->groupIndexes(group)) { + PlacesItem *item = placesItem(mapFromSource(sourceIndex)); + if (item) { + item->setGroupHidden(hidden); + } + } +} + void PlacesItemModel::loadBookmarks() { for(int r = 0, rMax = m_sourceModel->rowCount(); r < rMax; r++) { @@ -730,6 +744,21 @@ bool PlacesItemModel::isDir(int index) const return true; } +KFilePlacesModel::GroupType PlacesItemModel::groupType(int row) const +{ + return m_sourceModel->groupType(mapToSource(row)); +} + +bool PlacesItemModel::isGroupHidden(KFilePlacesModel::GroupType type) const +{ + return m_sourceModel->isGroupHidden(type); +} + +void PlacesItemModel::setGroupHidden(KFilePlacesModel::GroupType type, bool hidden) +{ + return m_sourceModel->setGroupHidden(type, hidden); +} + QModelIndex PlacesItemModel::mapToSource(int row) const { return m_indexMap.value(row); diff --git a/src/panels/places/placesitemmodel.h b/src/panels/places/placesitemmodel.h index b701c8ea9..c8830ee6f 100644 --- a/src/panels/places/placesitemmodel.h +++ b/src/panels/places/placesitemmodel.h @@ -21,6 +21,7 @@ #define PLACESITEMMODEL_H #include +#include #include #include @@ -31,7 +32,6 @@ class KBookmark; class KBookmarkManager; -class KFilePlacesModel; class PlacesItem; class QAction; @@ -137,6 +137,12 @@ public: void refresh(); bool isDir(int index) const override; + + + KFilePlacesModel::GroupType groupType(int row) const; + bool isGroupHidden(KFilePlacesModel::GroupType type) const; + void setGroupHidden(KFilePlacesModel::GroupType type, bool hidden); + signals: void errorMessage(const QString& message); void storageSetupDone(int index, bool success); @@ -158,6 +164,7 @@ private slots: void onSourceModelRowsAboutToBeMoved(const QModelIndex &parent, int start, int end, const QModelIndex &destination, int row); void onSourceModelRowsMoved(const QModelIndex &parent, int start, int end, const QModelIndex &destination, int row); void onSourceModelDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector &roles); + void onSourceModelGroupHiddenChanged(KFilePlacesModel::GroupType group, bool hidden); private: /** diff --git a/src/panels/places/placespanel.cpp b/src/panels/places/placespanel.cpp index d8eab7dd9..f7ca7d010 100644 --- a/src/panels/places/placespanel.cpp +++ b/src/panels/places/placespanel.cpp @@ -223,6 +223,8 @@ void PlacesPanel::slotItemContextMenuRequested(int index, const QPointF& pos) hideAction->setCheckable(true); hideAction->setChecked(item->isHidden()); + buildGroupContextMenu(&menu, index); + QAction* action = menu.exec(pos.toPoint()); if (action) { if (action == emptyTrashAction) { @@ -273,6 +275,8 @@ void PlacesPanel::slotViewContextMenuRequested(const QPointF& pos) showAllAction->setChecked(m_model->hiddenItemsShown()); } + buildGroupContextMenu(&menu, m_controller->indexCloseToMousePressedPosition()); + QMenu* iconSizeSubMenu = new QMenu(i18nc("@item:inmenu", "Icon Size"), &menu); struct IconSizeInfo @@ -326,6 +330,24 @@ void PlacesPanel::slotViewContextMenuRequested(const QPointF& pos) selectClosestItem(); } +QAction *PlacesPanel::buildGroupContextMenu(QMenu *menu, int index) +{ + if (index == -1) { + return nullptr; + } + + KFilePlacesModel::GroupType groupType = m_model->groupType(index); + QAction *hideGroupAction = menu->addAction(i18nc("@item:inmenu", "Hide Section '%1'", m_model->item(index)->group())); + hideGroupAction->setCheckable(true); + hideGroupAction->setChecked(m_model->isGroupHidden(groupType)); + + connect(hideGroupAction, &QAction::triggered, this, [this, groupType, hideGroupAction]{ + m_model->setGroupHidden(groupType, hideGroupAction->isChecked()); + }); + + return hideGroupAction; +} + void PlacesPanel::slotItemDropEvent(int index, QGraphicsSceneDragDropEvent* event) { if (index < 0) { diff --git a/src/panels/places/placespanel.h b/src/panels/places/placespanel.h index f5c90b1f9..444785888 100644 --- a/src/panels/places/placespanel.h +++ b/src/panels/places/placespanel.h @@ -31,6 +31,7 @@ class PlacesItemModel; class PlacesView; class QGraphicsSceneDragDropEvent; class KJob; +class QMenu; /** * @brief Combines bookmarks and mounted devices as list. */ @@ -82,6 +83,8 @@ private: void triggerItem(int index, Qt::MouseButton button); + QAction* buildGroupContextMenu(QMenu* menu, int index); + private: KItemListController* m_controller; PlacesItemModel* m_model;