]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/dolphindetailsview.cpp
You need >> or either the "Your names" and "Your messages" placeholders for translato...
[dolphin.git] / src / dolphindetailsview.cpp
index 876f9852c7d0608066134d36aa23b2e7e2be8519..55851dc95da294b3f030b6fc2373f7adb1df3399 100644 (file)
@@ -24,6 +24,7 @@
 #include "dolphincontroller.h"
 #include "dolphinsettings.h"
 #include "dolphinsortfilterproxymodel.h"
+#include "draganddrophelper.h"
 #include "viewproperties.h"
 
 #include "dolphin_detailsmodesettings.h"
@@ -134,13 +135,8 @@ DolphinDetailsView::~DolphinDetailsView()
 bool DolphinDetailsView::event(QEvent* event)
 {
     if (event->type() == QEvent::Polish) {
-        // Assure that by respecting the available width that:
-        // - the 'Name' column is stretched as large as possible
-        // - the remaining columns are as small as possible
         QHeaderView* headerView = header();
-        headerView->setStretchLastSection(false);
-        headerView->setResizeMode(QHeaderView::ResizeToContents);
-        headerView->setResizeMode(0, QHeaderView::Stretch);
+        headerView->setResizeMode(QHeaderView::Fixed);
         headerView->setMovable(false);
 
         updateColumnVisibility();
@@ -217,6 +213,11 @@ void DolphinDetailsView::mouseReleaseEvent(QMouseEvent* event)
     }
 }
 
+void DolphinDetailsView::startDrag(Qt::DropActions supportedActions)
+{
+    DragAndDropHelper::startDrag(this, supportedActions);
+}
+
 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent* event)
 {
     if (event->mimeData()->hasUrls()) {
@@ -250,7 +251,8 @@ void DolphinDetailsView::dragMoveEvent(QDragMoveEvent* event)
         m_dragging = false;
     } else {
         m_dragging = true;
-        if (itemForIndex(index).isDir()) {
+        const KFileItem item = itemForIndex(index);
+        if (!item.isNull() && item.isDir()) {
             m_dropRect = visualRect(index);
         } else {
             m_dropRect.setSize(QSize()); // set as invalid
@@ -299,7 +301,7 @@ void DolphinDetailsView::paintEvent(QPaintEvent* event)
     // TODO: remove this code when the issue #160611 is solved in Qt 4.4
     if (m_dragging) {
         const QBrush& brush = m_viewOptions.palette.brush(QPalette::Normal, QPalette::Highlight);
-        DolphinController::drawHoverIndication(viewport(), m_dropRect, brush);
+        DragAndDropHelper::drawHoverIndication(viewport(), m_dropRect, brush);
     }
 }
 
@@ -320,34 +322,7 @@ void DolphinDetailsView::keyPressEvent(QKeyEvent* event)
 void DolphinDetailsView::resizeEvent(QResizeEvent* event)
 {
     QTreeView::resizeEvent(event);
-
-    // assure that the width of the name-column does not get too small
-    const int minWidth = 120;
-    QHeaderView* headerView = header();
-    bool useFixedWidth = (headerView->sectionSize(KDirModel::Name) <= minWidth)
-                         && (headerView->resizeMode(0) != QHeaderView::Fixed);
-    if (useFixedWidth) {
-        // the current width of the name-column is too small, hence
-        // use a fixed size
-        headerView->setResizeMode(QHeaderView::Fixed);
-        headerView->setResizeMode(0, QHeaderView::Fixed);
-        headerView->resizeSection(KDirModel::Name, minWidth);
-    } else if (headerView->resizeMode(0) != QHeaderView::Stretch) {
-        // check whether there is enough available viewport width
-        // to automatically resize the columns
-        const int availableWidth = viewport()->width();
-
-        int headerWidth = 0;
-        const int count = headerView->count();
-        for (int i = 0; i < count; ++i) {
-            headerWidth += headerView->sectionSize(i);
-        }
-
-        if (headerWidth < availableWidth) {
-            headerView->setResizeMode(QHeaderView::ResizeToContents);
-            headerView->setResizeMode(0, QHeaderView::Stretch);
-        }
-    }
+    resizeColumns();
 }
 
 void DolphinDetailsView::closeEvent(QCloseEvent* event)
@@ -512,6 +487,8 @@ void DolphinDetailsView::updateColumnVisibility()
             m_clearAdditionalInfo = false;
         }
     }
+
+    resizeColumns();
 }
 
 bool DolphinDetailsView::isZoomInPossible() const
@@ -581,4 +558,40 @@ KFileItemDelegate::Information DolphinDetailsView::infoForColumn(int columnIndex
     return info;
 }
 
+void DolphinDetailsView::resizeColumns()
+{
+    // Using the resize mode QHeaderView::ResizeToContents is too slow (it takes
+    // around 3 seconds for each (!) resize operation when having > 10000 items).
+    // This gets a problem especially when opening large directories, where several
+    // resize operations are received for showing the currently available items during
+    // loading (the application hangs around 20 seconds when loading > 10000 items).
+
+    QHeaderView* headerView = header();
+    QFontMetrics fontMetrics(viewport()->font());
+
+    int columnWidth[KDirModel::ColumnCount];
+    columnWidth[KDirModel::Size] = fontMetrics.width("00000 Items");
+    columnWidth[KDirModel::ModifiedTime] = fontMetrics.width("0000-00-00 00:00");
+    columnWidth[KDirModel::Permissions] = fontMetrics.width("xxxxxxxxxx");
+    columnWidth[KDirModel::Owner] = fontMetrics.width("xxxxxxxxxx");
+    columnWidth[KDirModel::Group] = fontMetrics.width("xxxxxxxxxx");
+    columnWidth[KDirModel::Type] = fontMetrics.width("XXXX Xxxxxxx");
+
+    int requiredWidth = 0;
+    for (int i = KDirModel::Size; i <= KDirModel::Type; ++i) {
+        if (!isColumnHidden(i)) {
+            columnWidth[i] += 20; // provide a default gap
+            requiredWidth += columnWidth[i];
+            headerView->resizeSection(i, columnWidth[i]);
+        }
+    }
+
+    // resize the name column in a way that the whole available width is used
+    columnWidth[KDirModel::Name] = viewport()->width() - requiredWidth;
+    if (columnWidth[KDirModel::Name] < 120) {
+        columnWidth[KDirModel::Name] = 120;
+    }
+    headerView->resizeSection(KDirModel::Name, columnWidth[KDirModel::Name]);
+}
+
 #include "dolphindetailsview.moc"