]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Prevent code duplication by moving the duplications into the DolphinController.
authorPeter Penz <peter.penz19@gmail.com>
Thu, 21 Feb 2008 11:56:31 +0000 (11:56 +0000)
committerPeter Penz <peter.penz19@gmail.com>
Thu, 21 Feb 2008 11:56:31 +0000 (11:56 +0000)
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

src/dolphincolumnwidget.cpp
src/dolphincolumnwidget.h
src/dolphincontroller.cpp
src/dolphincontroller.h
src/dolphindetailsview.cpp
src/dolphindetailsview.h
src/dolphiniconsview.cpp
src/dolphiniconsview.h

index f8db0e865e8d3bfc682ea144cc48783e828303ee..6f4f10d364df15ecfbc6018937d450dac6f5c36b 100644 (file)
@@ -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"
index 25da35bd8bf6693760c4804bf8591c030de6f0b2..78f0d3ccb06868e1d07442f2e8be55e097c7d256 100644 (file)
@@ -133,8 +133,6 @@ private:
     /** Used by DolphinColumnWidget::setActive(). */
     void deactivate();
 
-    KFileItem itemForIndex(const QModelIndex& index) const;
-
 private:
     bool m_active;
     DolphinColumnView* m_view;
index f5fd5e83670b4dd38801a7bef7478b789c531246..1bd59fffaf8150707295572c81e459d112bc407e 100644 (file)
@@ -19,6 +19,9 @@
 
 #include "dolphincontroller.h"
 
+#include <kdirmodel.h>
+#include <QAbstractProxyModel>
+
 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<QAbstractProxyModel*>(view->model());
+    KDirModel* dirModel = static_cast<KDirModel*>(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()
index e08e9e49031acf958b2873152efd2be5b03a0845..17e0a4a71431fe9cd91c42a5c445919f177f1b8a 100644 (file)
@@ -25,6 +25,7 @@
 #include <QtCore/QObject>
 #include <libdolphin_export.h>
 
+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
index 3f23795ba408b285b907e5777c2c698a4cea1966..f364900b6b7f9c12829e9a68c5bba6566162c7a6 100644 (file)
@@ -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<QAbstractProxyModel*>(model());
-    KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
-    const QModelIndex dirIndex = proxyModel->mapToSource(index);
-    return dirModel->itemForIndex(dirIndex);
-}
-
 KFileItemDelegate::Information DolphinDetailsView::infoForColumn(int columnIndex) const
 {
     KFileItemDelegate::Information info = KFileItemDelegate::NoInformation;
index 90dfcade5568bc157e79b5c8257484ab47bfea91..92c451e63138b70a4d2735ecf73972b75b7c557f 100644 (file)
@@ -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;
 
     /**
index b1ffd401c60a509d326566380fb690dbca38e131..f51671e8a5a3f3e2b5bb25ac2c7d5785d539f534 100644 (file)
@@ -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<QAbstractProxyModel*>(model());
-    KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
-    const QModelIndex dirIndex = proxyModel->mapToSource(index);
-    return dirModel->itemForIndex(dirIndex);
-}
-
 int DolphinIconsView::additionalInfoCount() const
 {
     const DolphinView* view = m_controller->dolphinView();
index e14d6582fb8389aac531af96896b38ba3cee3c67..f1210a97de80e67487f99a949582f78ea3ad036e 100644 (file)
@@ -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.