From: Peter Penz Date: Thu, 21 Feb 2008 11:56:31 +0000 (+0000) Subject: Prevent code duplication by moving the duplications into the DolphinController. X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/commitdiff_plain/e287058acb772b55b679a694062361d3fb0e8f89 Prevent code duplication by moving the duplications into the DolphinController. Maybe it might be a good idea to let the DolphinController be aware also about his QAbstractItemView -> it might be possible to directly connect signals of the dolphin view implementations with the controller. I'll check this... (I did not backport this cleanup as I think it has too many changes to be handled as bugfix) CCMAIL: faure@kde.org CCMAIL: edulix@gmail.com svn path=/trunk/KDE/kdebase/apps/; revision=777719 --- diff --git a/src/dolphincolumnwidget.cpp b/src/dolphincolumnwidget.cpp index f8db0e865..6f4f10d36 100644 --- a/src/dolphincolumnwidget.cpp +++ b/src/dolphincolumnwidget.cpp @@ -277,7 +277,7 @@ void DolphinColumnWidget::dragMoveEvent(QDragMoveEvent* event) m_dropRect.setSize(QSize()); // set as invalid if (index.isValid()) { - const KFileItem item = itemForIndex(index); + const KFileItem item = m_view->m_controller->itemForIndex(index, this); if (!item.isNull() && item.isDir()) { m_dropRect = visualRect(index); } @@ -295,7 +295,7 @@ void DolphinColumnWidget::dropEvent(QDropEvent* event) const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData()); if (!urls.isEmpty()) { const QModelIndex index = indexAt(event->pos()); - const KFileItem item = itemForIndex(index); + const KFileItem item = m_view->m_controller->itemForIndex(index, this); m_view->m_controller->indicateDroppedUrls(urls, url(), item); @@ -344,21 +344,7 @@ void DolphinColumnWidget::mousePressEvent(QMouseEvent* event) void DolphinColumnWidget::keyPressEvent(QKeyEvent* event) { QListView::keyPressEvent(event); - - const QItemSelectionModel* selModel = selectionModel(); - const QModelIndex currentIndex = selModel->currentIndex(); - const bool trigger = currentIndex.isValid() - && (event->key() == Qt::Key_Return) - && (selModel->selectedIndexes().count() > 0); - if(trigger) { - const QModelIndexList indexList = selModel->selectedIndexes(); - foreach (const QModelIndex &index, indexList) { - KFileItem item = itemForIndex(index); - if (!item.isNull()) { - triggerItem(index); - } - } - } + m_view->m_controller->handleKeyPressEvent(event, this); } void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent* event) @@ -400,15 +386,12 @@ void DolphinColumnWidget::selectionChanged(const QItemSelection& selected, const void DolphinColumnWidget::triggerItem(const QModelIndex& index) { - const KFileItem item = itemForIndex(index); - m_view->m_controller->triggerItem(item); + m_view->m_controller->triggerItem(index, this); } void DolphinColumnWidget::slotEntered(const QModelIndex& index) { - const QModelIndex dirIndex = m_proxyModel->mapToSource(index); - const KFileItem item = m_dolphinModel->itemForIndex(dirIndex); - m_view->m_controller->emitItemEntered(item); + m_view->m_controller->emitItemEntered(index, this); } void DolphinColumnWidget::requestActivation() @@ -473,11 +456,4 @@ void DolphinColumnWidget::deactivate() updateBackground(); } -KFileItem DolphinColumnWidget::itemForIndex(const QModelIndex& index) const -{ - const QModelIndex dirIndex = m_proxyModel->mapToSource(index); - return m_dolphinModel->itemForIndex(dirIndex); -} - - #include "dolphincolumnwidget.moc" diff --git a/src/dolphincolumnwidget.h b/src/dolphincolumnwidget.h index 25da35bd8..78f0d3ccb 100644 --- a/src/dolphincolumnwidget.h +++ b/src/dolphincolumnwidget.h @@ -133,8 +133,6 @@ private: /** Used by DolphinColumnWidget::setActive(). */ void deactivate(); - KFileItem itemForIndex(const QModelIndex& index) const; - private: bool m_active; DolphinColumnView* m_view; diff --git a/src/dolphincontroller.cpp b/src/dolphincontroller.cpp index f5fd5e836..1bd59fffa 100644 --- a/src/dolphincontroller.cpp +++ b/src/dolphincontroller.cpp @@ -19,6 +19,9 @@ #include "dolphincontroller.h" +#include +#include + DolphinController::DolphinController(DolphinView* dolphinView) : QObject(dolphinView), m_zoomInPossible(false), @@ -96,14 +99,46 @@ void DolphinController::triggerZoomOut() emit zoomOut(); } -void DolphinController::triggerItem(const KFileItem& item) +void DolphinController::handleKeyPressEvent(QKeyEvent* event, QAbstractItemView* view) +{ + const QItemSelectionModel* selModel = view->selectionModel(); + const QModelIndex currentIndex = selModel->currentIndex(); + const bool trigger = currentIndex.isValid() + && (event->key() == Qt::Key_Return) + && (selModel->selectedIndexes().count() > 0); + if (trigger) { + const QModelIndexList indexList = selModel->selectedIndexes(); + foreach (const QModelIndex& index, indexList) { + triggerItem(index, view); + } + } +} + +KFileItem DolphinController::itemForIndex(const QModelIndex& index, QAbstractItemView* view) const { - emit itemTriggered(item); + QAbstractProxyModel* proxyModel = static_cast(view->model()); + KDirModel* dirModel = static_cast(proxyModel->sourceModel()); + const QModelIndex dirIndex = proxyModel->mapToSource(index); + return dirModel->itemForIndex(dirIndex); } -void DolphinController::emitItemEntered(const KFileItem& item) +void DolphinController::triggerItem(const QModelIndex& index, QAbstractItemView* view) { - emit itemEntered(item); + const KFileItem item = itemForIndex(index, view); + if (index.isValid() && (index.column() == KDirModel::Name)) { + emit itemTriggered(item); + } else { + view->clearSelection(); + emit itemEntered(item); + } +} + +void DolphinController::emitItemEntered(const QModelIndex& index, QAbstractItemView* view) +{ + KFileItem item = itemForIndex(index, view); + if (!item.isNull()) { + emit itemEntered(item); + } } void DolphinController::emitViewportEntered() diff --git a/src/dolphincontroller.h b/src/dolphincontroller.h index e08e9e490..17e0a4a71 100644 --- a/src/dolphincontroller.h +++ b/src/dolphincontroller.h @@ -25,6 +25,7 @@ #include #include +class QAbstractItemView; class DolphinView; class KUrl; class QBrush; @@ -59,6 +60,7 @@ class QWidget; * - setZoomInPossible() * - setZoomOutPossible() * - triggerItem() + * - handleKeyPressEvent() * - emitItemEntered() * - emitViewportEntered() * @@ -192,17 +194,32 @@ public: void setZoomOutPossible(bool possible); bool isZoomOutPossible() const; + /** + * Should be invoked in each view implementation whenever a key has been + * pressed. If the selection model of \a view is not empty and + * the return key has been pressed, the selected items will get triggered. + */ + void handleKeyPressEvent(QKeyEvent* event, QAbstractItemView* view); + + /** + * Returns the file item for the proxy index \a index of the view \a view. + */ + KFileItem itemForIndex(const QModelIndex& index, QAbstractItemView* view) const; + public slots: /** - * Emits the signal itemTriggered(). The method should be invoked by the - * controller parent whenever the user has triggered an item. */ - void triggerItem(const KFileItem& item); + * Emits the signal itemTriggered() if the file item for the index \a index + * is not null. The method should be invoked by the + * controller parent whenever the user has triggered an item. + */ + void triggerItem(const QModelIndex& index, QAbstractItemView* view); /** - * Emits the signal itemEntered(). The method should be invoked by - * the controller parent whenever the mouse cursor is above an item. + * Emits the signal itemEntered() if the file item for the index \a index + * is not null. The method should be invoked by the controller parent + * whenever the mouse cursor is above an item. */ - void emitItemEntered(const KFileItem& item); + void emitItemEntered(const QModelIndex& index, QAbstractItemView* view); /** * Emits the signal viewportEntered(). The method should be invoked by diff --git a/src/dolphindetailsview.cpp b/src/dolphindetailsview.cpp index 3f23795ba..f364900b6 100644 --- a/src/dolphindetailsview.cpp +++ b/src/dolphindetailsview.cpp @@ -310,7 +310,7 @@ void DolphinDetailsView::dragMoveEvent(QDragMoveEvent* event) m_dragging = false; } else { m_dragging = true; - const KFileItem item = itemForIndex(index); + const KFileItem item = m_controller->itemForIndex(index, this); if (!item.isNull() && item.isDir()) { m_dropRect = visualRect(index); } else { @@ -333,7 +333,7 @@ void DolphinDetailsView::dropEvent(QDropEvent* event) const QModelIndex index = indexAt(event->pos()); KFileItem item; if (index.isValid() && (index.column() == DolphinModel::Name)) { - item = itemForIndex(index); + item = m_controller->itemForIndex(index, this); } m_controller->indicateDroppedUrls(urls, m_controller->url(), @@ -372,21 +372,7 @@ void DolphinDetailsView::paintEvent(QPaintEvent* event) void DolphinDetailsView::keyPressEvent(QKeyEvent* event) { QTreeView::keyPressEvent(event); - - const QItemSelectionModel* selModel = selectionModel(); - const QModelIndex currentIndex = selModel->currentIndex(); - const bool trigger = currentIndex.isValid() - && (event->key() == Qt::Key_Return) - && (selModel->selectedIndexes().count() > 0); - if(trigger) { - const QModelIndexList indexList = selModel->selectedIndexes(); - foreach (const QModelIndex &index, indexList) { - KFileItem item = itemForIndex(index); - if (!item.isNull()) { - triggerItem(index); - } - } - } + m_controller->handleKeyPressEvent(event, this); } void DolphinDetailsView::resizeEvent(QResizeEvent* event) @@ -434,7 +420,7 @@ void DolphinDetailsView::slotEntered(const QModelIndex& index) const QPoint pos = viewport()->mapFromGlobal(QCursor::pos()); const int nameColumnWidth = header()->sectionSize(DolphinModel::Name); if (pos.x() < nameColumnWidth) { - m_controller->emitItemEntered(itemForIndex(index)); + m_controller->emitItemEntered(index, this); } else { m_controller->emitViewportEntered(); @@ -486,13 +472,7 @@ void DolphinDetailsView::zoomOut() void DolphinDetailsView::triggerItem(const QModelIndex& index) { - const KFileItem item = itemForIndex(index); - if (index.isValid() && (index.column() == KDirModel::Name)) { - m_controller->triggerItem(item); - } else { - clearSelection(); - m_controller->emitItemEntered(item); - } + m_controller->triggerItem(index, this); } void DolphinDetailsView::configureColumns(const QPoint& pos) @@ -618,14 +598,6 @@ QPoint DolphinDetailsView::contentsPos() const return QPoint(0, y); } -KFileItem DolphinDetailsView::itemForIndex(const QModelIndex& index) const -{ - QAbstractProxyModel* proxyModel = static_cast(model()); - KDirModel* dirModel = static_cast(proxyModel->sourceModel()); - const QModelIndex dirIndex = proxyModel->mapToSource(index); - return dirModel->itemForIndex(dirIndex); -} - KFileItemDelegate::Information DolphinDetailsView::infoForColumn(int columnIndex) const { KFileItemDelegate::Information info = KFileItemDelegate::NoInformation; diff --git a/src/dolphindetailsview.h b/src/dolphindetailsview.h index 90dfcade5..92c451e63 100644 --- a/src/dolphindetailsview.h +++ b/src/dolphindetailsview.h @@ -150,8 +150,6 @@ private: /** Return the upper left position in pixels of the viewport content. */ QPoint contentsPos() const; - KFileItem itemForIndex(const QModelIndex& index) const; - KFileItemDelegate::Information infoForColumn(int columnIndex) const; /** diff --git a/src/dolphiniconsview.cpp b/src/dolphiniconsview.cpp index b1ffd401c..f51671e8a 100644 --- a/src/dolphiniconsview.cpp +++ b/src/dolphiniconsview.cpp @@ -244,7 +244,7 @@ void DolphinIconsView::dragMoveEvent(QDragMoveEvent* event) m_dropRect.setSize(QSize()); // set as invalid if (index.isValid()) { - const KFileItem item = itemForIndex(index); + const KFileItem item = m_controller->itemForIndex(index, this); if (!item.isNull() && item.isDir()) { m_dropRect = visualRect(index); } else { @@ -265,7 +265,7 @@ void DolphinIconsView::dropEvent(QDropEvent* event) const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData()); if (!urls.isEmpty()) { const QModelIndex index = indexAt(event->pos()); - const KFileItem item = itemForIndex(index); + const KFileItem item = m_controller->itemForIndex(index, this); m_controller->indicateDroppedUrls(urls, m_controller->url(), item); @@ -292,21 +292,7 @@ void DolphinIconsView::paintEvent(QPaintEvent* event) void DolphinIconsView::keyPressEvent(QKeyEvent* event) { KCategorizedView::keyPressEvent(event); - - const QItemSelectionModel* selModel = selectionModel(); - const QModelIndex currentIndex = selModel->currentIndex(); - const bool trigger = currentIndex.isValid() - && (event->key() == Qt::Key_Return) - && (selModel->selectedIndexes().count() > 0); - if(trigger) { - const QModelIndexList indexList = selModel->selectedIndexes(); - foreach (const QModelIndex &index, indexList) { - KFileItem item = itemForIndex(index); - if (!item.isNull()) { - triggerItem(index); - } - } - } + m_controller->handleKeyPressEvent(event, this); } void DolphinIconsView::wheelEvent(QWheelEvent* event) @@ -314,7 +300,7 @@ void DolphinIconsView::wheelEvent(QWheelEvent* event) // let Ctrl+wheel events propagate to the DolphinView for icon zooming if ((event->modifiers() & Qt::ControlModifier) == Qt::ControlModifier) { event->ignore(); - return; + return; } KCategorizedView::wheelEvent(event); // if the icons are aligned left to right, the vertical wheel event should @@ -334,12 +320,12 @@ void DolphinIconsView::wheelEvent(QWheelEvent* event) void DolphinIconsView::triggerItem(const QModelIndex& index) { - m_controller->triggerItem(itemForIndex(index)); + m_controller->triggerItem(index, this); } void DolphinIconsView::slotEntered(const QModelIndex& index) { - m_controller->emitItemEntered(itemForIndex(index)); + m_controller->emitItemEntered(index, this); } void DolphinIconsView::slotShowPreviewChanged() @@ -516,14 +502,6 @@ void DolphinIconsView::updateGridSize(bool showPreview, int additionalInfoCount) m_controller->setZoomOutPossible(isZoomOutPossible()); } -KFileItem DolphinIconsView::itemForIndex(const QModelIndex& index) const -{ - QAbstractProxyModel* proxyModel = static_cast(model()); - KDirModel* dirModel = static_cast(proxyModel->sourceModel()); - const QModelIndex dirIndex = proxyModel->mapToSource(index); - return dirModel->itemForIndex(dirIndex); -} - int DolphinIconsView::additionalInfoCount() const { const DolphinView* view = m_controller->dolphinView(); diff --git a/src/dolphiniconsview.h b/src/dolphiniconsview.h index e14d6582f..f1210a97d 100644 --- a/src/dolphiniconsview.h +++ b/src/dolphiniconsview.h @@ -90,8 +90,6 @@ private: */ void updateGridSize(bool showPreview, int additionalInfoCount); - KFileItem itemForIndex(const QModelIndex& index) const; - /** * Returns the number of additional information lines that should * be shown below the item name.