From 362817d1834f2ada3ea4552a25fa39bbbb540f8c Mon Sep 17 00:00:00 2001 From: Peter Penz Date: Sat, 26 Nov 2011 01:05:58 +0100 Subject: Folders Panel fixes The following functionality from Dolphin 1.x has been ported to the new view-engine: - Allow expanding/collapsing of items - Automatically select the current item - Context menu for items Related improvements to the view-engine: - Make the expanding/collapsing interface already accessible in the base classes KItemModelBase and KItemListView. If no expanding/collapsing is supported at all by derived models (which is usually the default case) simply not reimplementing those 3 methods is sufficient and it does not introduce an additional complexity like in QAbstractItemModel/QModelIndex. - Automatically handle the expanding/collapsing in KItemListController. This also includes the key-handling, which is quite special for expandable items. - Don't let KItemListView automatically scroll to the current item if the current item got changed. The automatic scrolling should only be done if the current item has been changed by the user. Hence this functionality has been moved to the KItemListController which currently only triggers the automatic scrolling if the current item has been changed by the keyboard (we might extend the usecases later if required). --- src/panels/folders/folderspanel.cpp | 134 ++++++++++++++++++++++++------------ 1 file changed, 90 insertions(+), 44 deletions(-) (limited to 'src/panels/folders/folderspanel.cpp') diff --git a/src/panels/folders/folderspanel.cpp b/src/panels/folders/folderspanel.cpp index cf458e508..484d8c2ed 100644 --- a/src/panels/folders/folderspanel.cpp +++ b/src/panels/folders/folderspanel.cpp @@ -37,6 +37,7 @@ #include #include #include +#include #include #include @@ -45,11 +46,9 @@ FoldersPanel::FoldersPanel(QWidget* parent) : Panel(parent), - m_setLeafVisible(false), - m_mouseButtons(Qt::NoButton), + m_updateCurrentItem(false), m_dirLister(0), - m_controller(0), - m_leafDir() + m_controller(0) { setLayoutDirection(Qt::LeftToRight); } @@ -121,7 +120,6 @@ bool FoldersPanel::urlChanged() } if (m_dirLister) { - m_setLeafVisible = true; loadTree(url()); } @@ -158,6 +156,10 @@ void FoldersPanel::showEvent(QShowEvent* event) const qreal itemHeight = qMax(int(KIconLoader::SizeSmall), styleOption.fontMetrics.height()); view->setItemSize(QSizeF(-1, itemHeight + 2 * styleOption.margin)); view->setItemLayout(KFileItemListView::DetailsLayout); + // Set the opacity to 0 initially. The opacity will be increased after the loading of the initial tree + // has been finished in slotLoadingCompleted(). This prevents an unnecessary animation-mess when + // opening the folders panel. + view->setOpacity(0); KFileItemModel* model = new KFileItemModel(m_dirLister, this); // Use a QueuedConnection to give the view the possibility to react first on the @@ -168,6 +170,12 @@ void FoldersPanel::showEvent(QShowEvent* event) m_controller = container->controller(); m_controller->setView(view); m_controller->setModel(model); + m_controller->setSelectionBehavior(KItemListController::SingleSelection); + + connect(m_controller, SIGNAL(itemActivated(int)), this, SLOT(slotItemActivated(int))); + connect(m_controller, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int))); + connect(m_controller, SIGNAL(itemContextMenuRequested(int,QPointF)), this, SLOT(slotItemContextMenuRequested(int,QPointF))); + connect(m_controller, SIGNAL(viewContextMenuRequested(QPointF)), this, SLOT(slotViewContextMenuRequested(QPointF))); // TODO: Check whether it makes sense to make an explicit API for KItemListContainer // to make the background transparent. @@ -194,22 +202,6 @@ void FoldersPanel::showEvent(QShowEvent* event) Panel::showEvent(event); } -void FoldersPanel::contextMenuEvent(QContextMenuEvent* event) -{ - Panel::contextMenuEvent(event); - - KFileItem item; - /*const QModelIndex index = m_treeView->indexAt(event->pos()); - if (index.isValid()) { - const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index); - item = m_dolphinModel->itemForIndex(dolphinModelIndex); - }*/ - - QPointer contextMenu = new TreeViewContextMenu(this, item); - contextMenu->open(); - delete contextMenu; -} - void FoldersPanel::keyPressEvent(QKeyEvent* event) { const int key = event->key(); @@ -221,17 +213,65 @@ void FoldersPanel::keyPressEvent(QKeyEvent* event) } } -void FoldersPanel::updateMouseButtons() +void FoldersPanel::slotItemActivated(int index) { - m_mouseButtons = QApplication::mouseButtons(); + const KFileItem item = fileItemModel()->fileItem(index); + if (!item.isNull()) { + emit changeUrl(item.url(), Qt::LeftButton); + } +} + +void FoldersPanel::slotItemMiddleClicked(int index) +{ + const KFileItem item = fileItemModel()->fileItem(index); + if (!item.isNull()) { + emit changeUrl(item.url(), Qt::MiddleButton); + } +} + +void FoldersPanel::slotItemContextMenuRequested(int index, const QPointF& pos) +{ + Q_UNUSED(pos); + + const KFileItem fileItem = fileItemModel()->fileItem(index); + + QWeakPointer contextMenu = new TreeViewContextMenu(this, fileItem); + contextMenu.data()->open(); + if (contextMenu.data()) { + delete contextMenu.data(); + } +} + +void FoldersPanel::slotViewContextMenuRequested(const QPointF& pos) +{ + Q_UNUSED(pos); + + QWeakPointer contextMenu = new TreeViewContextMenu(this, KFileItem()); + contextMenu.data()->open(); + if (contextMenu.data()) { + delete contextMenu.data(); + } } void FoldersPanel::slotLoadingCompleted() { - const int index = fileItemModel()->index(url()); - if (index >= 0) { - m_controller->selectionManager()->setCurrentItem(index); + if (m_controller->view()->opacity() == 0) { + // The loading of the initial tree after opening the Folders panel + // has been finished. Trigger the increasing of the opacity after + // a short delay to give the view the chance to finish its internal + // animations. + // TODO: Check whether it makes sense to allow accessing the + // view-internal delay for usecases like this. + QTimer::singleShot(250, this, SLOT(startFadeInAnimation())); + } + + if (!m_updateCurrentItem) { + return; } + + const int index = fileItemModel()->index(url()); + updateCurrentItem(index); + m_updateCurrentItem = false; } void FoldersPanel::slotHorizontalScrollBarMoved(int value) @@ -250,10 +290,21 @@ void FoldersPanel::slotVerticalScrollBarMoved(int value) //m_treeView->setAutoHorizontalScroll(FoldersPanelSettings::autoScrolling()); } +void FoldersPanel::startFadeInAnimation() +{ + QPropertyAnimation* anim = new QPropertyAnimation(m_controller->view(), "opacity", this); + anim->setStartValue(0); + anim->setEndValue(1); + anim->setEasingCurve(QEasingCurve::InOutQuad); + anim->start(QAbstractAnimation::DeleteWhenStopped); + anim->setDuration(200); +} + void FoldersPanel::loadTree(const KUrl& url) { Q_ASSERT(m_dirLister); - m_leafDir = url; + + m_updateCurrentItem = false; KUrl baseUrl; if (url.isLocalFile()) { @@ -266,6 +317,7 @@ void FoldersPanel::loadTree(const KUrl& url) } if (m_dirLister->url() != baseUrl) { + m_updateCurrentItem = true; m_dirLister->stop(); m_dirLister->openUrl(baseUrl, KDirLister::Reload); } @@ -273,29 +325,23 @@ void FoldersPanel::loadTree(const KUrl& url) KFileItemModel* model = fileItemModel(); const int index = model->index(url); if (index >= 0) { - m_controller->selectionManager()->setCurrentItem(index); + updateCurrentItem(index); } else { + m_updateCurrentItem = true; model->setExpanded(QSet() << url); + // slotLoadingCompleted() will be invoked after the model has + // expanded the url } } -void FoldersPanel::selectLeafDirectory() +void FoldersPanel::updateCurrentItem(int index) { - /*const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_leafDir); - const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex); - - if (proxyIndex.isValid()) { - QItemSelectionModel* selModel = m_treeView->selectionModel(); - selModel->setCurrentIndex(proxyIndex, QItemSelectionModel::ClearAndSelect); - - if (m_setLeafVisible) { - // Invoke scrollToLeaf() asynchronously. This assures that - // the horizontal scrollbar is shown after resizing the column - // (otherwise the scrollbar might hide the leaf). - QTimer::singleShot(0, this, SLOT(scrollToLeaf())); - m_setLeafVisible = false; - } - }*/ + KItemListSelectionManager* selectionManager = m_controller->selectionManager(); + selectionManager->setCurrentItem(index); + selectionManager->clearSelection(); + selectionManager->setSelected(index); + + m_controller->view()->scrollToItem(index); } KFileItemModel* FoldersPanel::fileItemModel() const -- cgit v1.3