]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/views/dolphinview.cpp
Revert the 2.0 decision to always use KB for file-sizes
[dolphin.git] / src / views / dolphinview.cpp
index 890a39d898f574ea72d0e0a5298baaa7d214fe29..872853642a01c157f886afab194097b1f76aa16f 100644 (file)
@@ -20,6 +20,8 @@
 
 #include "dolphinview.h"
 
+#include <config-nepomuk.h>
+
 #include <QAbstractItemView>
 #include <QApplication>
 #include <QBoxLayout>
@@ -41,6 +43,7 @@
 #include <KLocale>
 #include <kitemviews/kfileitemmodel.h>
 #include <kitemviews/kfileitemlistview.h>
+#include <kitemviews/kitemlistheader.h>
 #include <kitemviews/kitemlistselectionmanager.h>
 #include <kitemviews/kitemlistview.h>
 #include <kitemviews/kitemlistcontroller.h>
 #include "views/tooltips/tooltipmanager.h"
 #include "zoomlevelinfo.h"
 
+#ifdef HAVE_NEPOMUK
+    #include <Nepomuk/ResourceManager>
+#endif
+
 namespace {
     const int MaxModeEnum = DolphinView::CompactView;
 };
@@ -142,7 +149,7 @@ DolphinView::DolphinView(const KUrl& url, QWidget* parent) :
     connect(controller, SIGNAL(itemContextMenuRequested(int,QPointF)), this, SLOT(slotItemContextMenuRequested(int,QPointF)));
     connect(controller, SIGNAL(viewContextMenuRequested(QPointF)), this, SLOT(slotViewContextMenuRequested(QPointF)));
     connect(controller, SIGNAL(headerContextMenuRequested(QPointF)), this, SLOT(slotHeaderContextMenuRequested(QPointF)));
-    connect(controller, SIGNAL(itemPressed(int,Qt::MouseButton)), this, SLOT(hideToolTip()));
+    connect(controller, SIGNAL(mouseButtonPressed(int,Qt::MouseButtons)), this, SLOT(slotMouseButtonPressed(int,Qt::MouseButtons)));
     connect(controller, SIGNAL(itemHovered(int)), this, SLOT(slotItemHovered(int)));
     connect(controller, SIGNAL(itemUnhovered(int)), this, SLOT(slotItemUnhovered(int)));
     connect(controller, SIGNAL(itemDropEvent(int,QGraphicsSceneDragDropEvent*)), this, SLOT(slotItemDropEvent(int,QGraphicsSceneDragDropEvent*)));
@@ -161,6 +168,8 @@ DolphinView::DolphinView(const KUrl& url, QWidget* parent) :
             this, SLOT(slotSortRoleChangedByHeader(QByteArray,QByteArray)));
     connect(view, SIGNAL(visibleRolesChanged(QList<QByteArray>,QList<QByteArray>)),
             this, SLOT(slotVisibleRolesChangedByHeader(QList<QByteArray>,QList<QByteArray>)));
+    connect(view->header(), SIGNAL(columnWidthChanged(QByteArray,qreal,qreal)),
+            this, SLOT(slotHeaderColumnWidthChanged(QByteArray,qreal,qreal)));
 
     KItemListSelectionManager* selectionManager = controller->selectionManager();
     connect(selectionManager, SIGNAL(selectionChanged(QSet<int>,QSet<int>)),
@@ -530,9 +539,12 @@ QString DolphinView::statusBarText() const
 
     if (fileCount > 0 && folderCount > 0) {
         summary = i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
-                        foldersText, filesText, fileSizeText(totalFileSize));
+                        foldersText, filesText,
+                        KGlobal::locale()->formatByteSize(totalFileSize));
     } else if (fileCount > 0) {
-        summary = i18nc("@info:status files (size)", "%1 (%2)", filesText, fileSizeText(totalFileSize));
+        summary = i18nc("@info:status files (size)", "%1 (%2)",
+                        filesText,
+                        KGlobal::locale()->formatByteSize(totalFileSize));
     } else if (folderCount > 0) {
         summary = foldersText;
     }
@@ -774,42 +786,140 @@ void DolphinView::slotViewContextMenuRequested(const QPointF& pos)
 
 void DolphinView::slotHeaderContextMenuRequested(const QPointF& pos)
 {
-    QWeakPointer<KMenu> menu = new KMenu(QApplication::activeWindow());
+    ViewProperties props(url());
+
+    QPointer<KMenu> menu = new KMenu(QApplication::activeWindow());
 
     KItemListView* view = m_container->controller()->view();
     const QSet<QByteArray> visibleRolesSet = view->visibleRoles().toSet();
 
+    bool nepomukRunning = false;
+    bool indexingEnabled = false;
+#ifdef HAVE_NEPOMUK
+    nepomukRunning = (Nepomuk::ResourceManager::instance()->init() == 0);
+    if (nepomukRunning) {
+        KConfig config("nepomukserverrc");
+        indexingEnabled = config.group("Service-nepomukfileindexer").readEntry("autostart", false);
+    }
+#endif
+
+    QString groupName;
+    QMenu* groupMenu = 0;
+
     // Add all roles to the menu that can be shown or hidden by the user
     const QList<KFileItemModel::RoleInfo> rolesInfo = KFileItemModel::rolesInformation();
     foreach (const KFileItemModel::RoleInfo& info, rolesInfo) {
-        if (info.role != "name") {
-            const QString text = fileItemModel()->roleDescription(info.role);
+        if (info.role == "name") {
+            // It should not be possible to hide the "name" role
+            continue;
+        }
+
+        const QString text = fileItemModel()->roleDescription(info.role);
+        QAction* action = 0;
+        if (info.group.isEmpty()) {
+            action = menu->addAction(text);
+        } else {
+            if (!groupMenu || info.group != groupName) {
+                groupName = info.group;
+                groupMenu = menu->addMenu(groupName);
+            }
 
-            QAction* action = menu.data()->addAction(text);
-            action->setCheckable(true);
-            action->setChecked(visibleRolesSet.contains(info.role));
-            action->setData(info.role);
+            action = groupMenu->addAction(text);
         }
-    }
 
-    QAction* action = menu.data()->exec(pos.toPoint());
-    if (action) {
-        // Show or hide the selected role
-        const QByteArray selectedRole = action->data().toByteArray();
+        action->setCheckable(true);
+        action->setChecked(visibleRolesSet.contains(info.role));
+        action->setData(info.role);
 
-        ViewProperties props(url());
-        QList<QByteArray> visibleRoles = view->visibleRoles();
-        if (action->isChecked()) {
-            visibleRoles.append(selectedRole);
+        const bool enable = (!info.requiresNepomuk && !info.requiresIndexer) ||
+                            (info.requiresNepomuk && nepomukRunning) ||
+                            (info.requiresIndexer && indexingEnabled);
+        action->setEnabled(enable);
+    }
+
+    menu->addSeparator();
+
+    QActionGroup* widthsGroup = new QActionGroup(menu);
+    const bool autoColumnWidths = props.headerColumnWidths().isEmpty();
+
+    QAction* autoAdjustWidthsAction = menu->addAction(i18nc("@action:inmenu", "Automatic Column Widths"));
+    autoAdjustWidthsAction->setCheckable(true);
+    autoAdjustWidthsAction->setChecked(autoColumnWidths);
+    autoAdjustWidthsAction->setActionGroup(widthsGroup);
+
+    QAction* customWidthsAction = menu->addAction(i18nc("@action:inmenu", "Custom Column Widths"));
+    customWidthsAction->setCheckable(true);
+    customWidthsAction->setChecked(!autoColumnWidths);
+    customWidthsAction->setActionGroup(widthsGroup);
+
+    QAction* action = menu->exec(pos.toPoint());
+    if (menu && action) {
+        KItemListHeader* header = view->header();
+
+        if (action == autoAdjustWidthsAction) {
+            // Clear the column-widths from the viewproperties and turn on
+            // the automatic resizing of the columns
+            props.setHeaderColumnWidths(QList<int>());
+            header->setAutomaticColumnResizing(true);
+        } else if (action == customWidthsAction) {
+            // Apply the current column-widths as custom column-widths and turn
+            // off the automatic resizing of the columns
+            QList<int> columnWidths;
+            foreach (const QByteArray& role, view->visibleRoles()) {
+                columnWidths.append(header->columnWidth(role));
+            }
+            props.setHeaderColumnWidths(columnWidths);
+            header->setAutomaticColumnResizing(false);
         } else {
-            visibleRoles.removeOne(selectedRole);
+            // Show or hide the selected role
+            const QByteArray selectedRole = action->data().toByteArray();
+
+            QList<QByteArray> visibleRoles = view->visibleRoles();
+            if (action->isChecked()) {
+                visibleRoles.append(selectedRole);
+            } else {
+                visibleRoles.removeOne(selectedRole);
+            }
+
+            view->setVisibleRoles(visibleRoles);
+            props.setVisibleRoles(visibleRoles);
+
+            QList<int> columnWidths;
+            if (!header->automaticColumnResizing()) {
+                foreach (const QByteArray& role, view->visibleRoles()) {
+                    columnWidths.append(header->columnWidth(role));
+                }
+            }
+            props.setHeaderColumnWidths(columnWidths);
         }
+    }
+
+    delete menu;
+}
+
+void DolphinView::slotHeaderColumnWidthChanged(const QByteArray& role, qreal current, qreal previous)
+{
+    Q_UNUSED(previous);
+
+    const QList<QByteArray> visibleRoles = m_container->visibleRoles();
 
-        view->setVisibleRoles(visibleRoles);
-        props.setVisibleRoles(visibleRoles);
+    ViewProperties props(url());
+    QList<int> columnWidths = props.headerColumnWidths();
+    if (columnWidths.count() != visibleRoles.count()) {
+        columnWidths.clear();
+        columnWidths.reserve(visibleRoles.count());
+        const KItemListHeader* header = m_container->controller()->view()->header();
+        foreach (const QByteArray& role, visibleRoles) {
+            const int width = header->columnWidth(role);
+            columnWidths.append(width);
+        }
     }
 
-    delete menu.data();
+    const int roleIndex = visibleRoles.indexOf(role);
+    Q_ASSERT(roleIndex >= 0 && roleIndex < columnWidths.count());
+    columnWidths[roleIndex] = current;
+
+    props.setHeaderColumnWidths(columnWidths);
 }
 
 void DolphinView::slotItemHovered(int index)
@@ -858,6 +968,11 @@ void DolphinView::slotItemDropEvent(int index, QGraphicsSceneDragDropEvent* even
     if (!error.isEmpty()) {
         emit errorMessage(error);
     }
+
+    if (destUrl == url()) {
+        // Mark the dropped urls as selected.
+        markPastedUrlsAsSelected(event->mimeData());
+    }
 }
 
 void DolphinView::slotModelChanged(KItemModelBase* current, KItemModelBase* previous)
@@ -873,6 +988,22 @@ void DolphinView::slotModelChanged(KItemModelBase* current, KItemModelBase* prev
     m_versionControlObserver->setModel(fileItemModel);
 }
 
+void DolphinView::slotMouseButtonPressed(int itemIndex, Qt::MouseButtons buttons)
+{
+    hideToolTip();
+
+    if (itemIndex < 0) {
+        // Trigger the history navigation only when clicking on the viewport:
+        // Above an item the XButtons provide a simple way to select items in
+        // the singleClick mode.
+        if (buttons & Qt::XButton1) {
+            emit goBackRequested();
+        } else if (buttons & Qt::XButton2) {
+            emit goForwardRequested();
+        }
+    }
+}
+
 void DolphinView::slotSelectionChanged(const QSet<int>& current, const QSet<int>& previous)
 {
     const int currentCount = current.count();
@@ -1046,6 +1177,8 @@ void DolphinView::updateViewState()
     }
 
     if (!m_selectedUrls.isEmpty()) {
+        clearSelection();
+
         KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
         QSet<int> selectedItems = selectionManager->selectedItems();
         const KFileItemModel* model = fileItemModel();
@@ -1103,9 +1236,7 @@ void DolphinView::slotDirListerStarted(const KUrl& url)
 void DolphinView::slotLoadingCompleted()
 {
     // Update the view-state. This has to be done using a Qt::QueuedConnection
-    // because the view might not be in its final state yet (the view also
-    // listens to the completed()-signal from KDirLister and the order of
-    // of slots is undefined).
+    // because the view might not be in its final state yet.
     QTimer::singleShot(0, this, SLOT(updateViewState()));
 
     emit finishedPathLoading(url());
@@ -1264,6 +1395,24 @@ void DolphinView::applyViewProperties()
         }
     }
 
+    KItemListView* itemListView = m_container->controller()->view();
+    if (itemListView->isHeaderVisible()) {
+        KItemListHeader* header = itemListView->header();
+        const QList<int> headerColumnWidths = props.headerColumnWidths();
+        const int rolesCount = m_visibleRoles.count();
+        if (headerColumnWidths.count() == rolesCount) {
+            header->setAutomaticColumnResizing(false);
+
+            QHash<QByteArray, qreal> columnWidths;
+            for (int i = 0; i < rolesCount; ++i) {
+                columnWidths.insert(m_visibleRoles[i], headerColumnWidths[i]);
+            }
+            header->setColumnWidths(columnWidths);
+        } else {
+            header->setAutomaticColumnResizing(true);
+        }
+    }
+
     m_container->endTransaction();
 }
 
@@ -1301,8 +1450,13 @@ QMimeData* DolphinView::selectionMimeData() const
 
 void DolphinView::markPastedUrlsAsSelected(const QMimeData* mimeData)
 {
-    const KUrl::List urls = KUrl::List::fromMimeData(mimeData);
-    markUrlsAsSelected(urls);
+    const KUrl::List sourceUrls = KUrl::List::fromMimeData(mimeData);
+    KUrl::List destUrls;
+    foreach (const KUrl& source, sourceUrls) {
+        KUrl destination(url().url() + "/" + source.fileName());
+        destUrls << destination;
+    }
+    markUrlsAsSelected(destUrls);
 }
 
 void DolphinView::updateWritableState()
@@ -1320,27 +1474,4 @@ void DolphinView::updateWritableState()
     }
 }
 
-QString DolphinView::fileSizeText(KIO::filesize_t fileSize)
-{
-    const KLocale* locale = KGlobal::locale();
-    const unsigned int multiplier = (locale->binaryUnitDialect() == KLocale::MetricBinaryDialect)
-                                    ? 1000 : 1024;
-
-    QString text;
-    if (fileSize < multiplier) {
-        // Show the size in bytes
-        text = locale->formatByteSize(fileSize, 0, KLocale::DefaultBinaryDialect, KLocale::UnitByte);
-    } else if (fileSize < multiplier * multiplier) {
-        // Show the size in kilobytes and always round up. This is done
-        // for consistency with the values shown e.g. in the "Size" column
-        // of the details-view.
-        fileSize += (multiplier / 2) - 1;
-        text = locale->formatByteSize(fileSize, 0, KLocale::DefaultBinaryDialect, KLocale::UnitKiloByte);
-    } else {
-        // Show the size in the best fitting unit having one decimal
-        text = locale->formatByteSize(fileSize, 1);
-    }
-    return text;
-}
-
 #include "dolphinview.moc"