]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/infosidebarpage.cpp
1) Make sure that size qprogressbar is really updated after each change
[dolphin.git] / src / infosidebarpage.cpp
index d2652e0d100763c4566e7751d57116b707bee5a0..b384f24e6d99e61915889ab55ed78bca00e14324 100644 (file)
 
 #include <config-nepomuk.h>
 
+#include <kdialog.h>
+#include <kdirnotify.h>
 #include <kfileplacesmodel.h>
 #include <klocale.h>
 #include <kstandarddirs.h>
 #include <kio/previewjob.h>
 #include <kfileitem.h>
-#include <kdialog.h>
 #include <kglobalsettings.h>
 #include <kfilemetainfo.h>
 #include <kiconeffect.h>
@@ -49,6 +50,7 @@
 
 InfoSidebarPage::InfoSidebarPage(QWidget* parent) :
     SidebarPage(parent),
+    m_initialized(false),
     m_pendingPreview(false),
     m_shownUrl(),
     m_urlCandidate(),
@@ -59,51 +61,6 @@ InfoSidebarPage::InfoSidebarPage(QWidget* parent) :
     m_metaDataWidget(0),
     m_metaTextLabel(0)
 {
-    const int spacing = KDialog::spacingHint();
-
-    m_timer = new QTimer(this);
-    m_timer->setSingleShot(true);
-    connect(m_timer, SIGNAL(timeout()),
-            this, SLOT(slotTimeout()));
-
-    QVBoxLayout* layout = new QVBoxLayout;
-    layout->setSpacing(spacing);
-
-    // name
-    m_nameLabel = new QLabel(this);
-    QFont font = m_nameLabel->font();
-    font.setBold(true);
-    m_nameLabel->setFont(font);
-    m_nameLabel->setAlignment(Qt::AlignHCenter);
-    m_nameLabel->setWordWrap(true);
-
-    // preview
-    m_preview = new PixmapViewer(this);
-    m_preview->setMinimumWidth(KIconLoader::SizeEnormous + KIconLoader::SizeMedium);
-    m_preview->setMinimumHeight(KIconLoader::SizeEnormous);
-
-    if (MetaDataWidget::metaDataAvailable()) {
-        // rating, comment and tags
-        m_metaDataWidget = new MetaDataWidget(this);
-    }
-
-    // general meta text information
-    m_metaTextLabel = new MetaTextLabel(this);
-    m_metaTextLabel->setMinimumWidth(spacing);
-
-    layout->addWidget(m_nameLabel);
-    layout->addWidget(new KSeparator(this));
-    layout->addWidget(m_preview);
-    layout->addWidget(new KSeparator(this));
-    if (m_metaDataWidget != 0) {
-        layout->addWidget(m_metaDataWidget);
-        layout->addWidget(new KSeparator(this));
-    }
-    layout->addWidget(m_metaTextLabel);
-
-    // ensure that widgets in the information side bar are aligned towards the top
-    layout->addStretch(1);
-    setLayout(layout);
 }
 
 InfoSidebarPage::~InfoSidebarPage()
@@ -121,14 +78,22 @@ void InfoSidebarPage::setUrl(const KUrl& url)
 {
     SidebarPage::setUrl(url);
     if (url.isValid() && !m_shownUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
-        cancelRequest();
-        m_shownUrl = url;
-        showItemInfo();
+        if (m_initialized) {
+            cancelRequest();
+            m_shownUrl = url;
+            showItemInfo();
+        } else {
+            m_shownUrl = url;
+        }
     }
 }
 
 void InfoSidebarPage::setSelection(const KFileItemList& selection)
 {
+    if (!m_initialized) {
+        return;
+    }
+
     m_selection = selection;
 
     const int count = selection.count();
@@ -145,6 +110,10 @@ void InfoSidebarPage::setSelection(const KFileItemList& selection)
 
 void InfoSidebarPage::requestDelayedItemInfo(const KFileItem& item)
 {
+    if (!m_initialized) {
+        return;
+    }
+
     cancelRequest();
 
     m_fileItem = KFileItem();
@@ -166,24 +135,32 @@ void InfoSidebarPage::showEvent(QShowEvent* event)
 {
     SidebarPage::showEvent(event);
     if (!event->spontaneous()) {
+        if (!m_initialized) {
+            // do a delayed initialization so that no performance
+            // penalty is given when Dolphin is started with a closed
+            // Information Panel
+            init();
+        }
         showItemInfo();
     }
 }
 
 void InfoSidebarPage::resizeEvent(QResizeEvent* event)
 {
-    // If the text inside the name label or the info label cannot
-    // get wrapped, then the maximum width of the label is increased
-    // so that the width of the information sidebar gets increased.
-    // To prevent this, the maximum width is adjusted to
-    // the current width of the sidebar.
-    const int maxWidth = event->size().width() - KDialog::spacingHint() * 4;
-    m_nameLabel->setMaximumWidth(maxWidth);
-
-    // try to increase the preview as large as possible
-    m_preview->setSizeHint(QSize(maxWidth, maxWidth));
-    m_urlCandidate = m_shownUrl; // reset the URL candidate if a resizing is done
-    m_timer->start(TimerDelay);
+    if (m_initialized) {
+        // If the text inside the name label or the info label cannot
+        // get wrapped, then the maximum width of the label is increased
+        // so that the width of the information sidebar gets increased.
+        // To prevent this, the maximum width is adjusted to
+        // the current width of the sidebar.
+        const int maxWidth = event->size().width() - KDialog::spacingHint() * 4;
+        m_nameLabel->setMaximumWidth(maxWidth);
+
+        // try to increase the preview as large as possible
+        m_preview->setSizeHint(QSize(maxWidth, maxWidth));
+        m_urlCandidate = m_shownUrl; // reset the URL candidate if a resizing is done
+        m_timer->start(TimerDelay);
+    }
 
     SidebarPage::resizeEvent(event);
 }
@@ -264,6 +241,69 @@ void InfoSidebarPage::showPreview(const KFileItem& item,
     }
 }
 
+void InfoSidebarPage::slotFileRenamed(const QString& source, const QString& dest)
+{
+    if (m_shownUrl == KUrl(source)) {
+        // the currently shown file has been renamed, hence update the item information
+        // for the renamed file
+        KFileItem item(KFileItem::Unknown, KFileItem::Unknown, KUrl(dest));
+        requestDelayedItemInfo(item);
+    }
+}
+
+void InfoSidebarPage::slotFilesAdded(const QString& directory)
+{
+    if (m_shownUrl == KUrl(directory)) {
+        // If the 'trash' icon changes because the trash has been emptied or got filled,
+        // the signal filesAdded("trash:/") will be emitted.
+        KFileItem item(KFileItem::Unknown, KFileItem::Unknown, KUrl(directory));
+        requestDelayedItemInfo(item);
+    }
+}
+
+void InfoSidebarPage::slotFilesChanged(const QStringList& files)
+{
+    foreach (const QString& fileName, files) {
+        if (m_shownUrl == KUrl(fileName)) {
+            showItemInfo();
+            break;
+        }
+    }
+}
+
+void InfoSidebarPage::slotFilesRemoved(const QStringList& files)
+{
+    foreach (const QString& fileName, files) {
+        if (m_shownUrl == KUrl(fileName)) {
+            // the currently shown item has been removed, show
+            // the parent directory as fallback
+            m_shownUrl = url();
+            showItemInfo();
+            break;
+        }
+    }
+}
+
+void InfoSidebarPage::slotEnteredDirectory(const QString& directory)
+{
+    if (m_shownUrl == KUrl(directory)) {
+        KFileItem item(KFileItem::Unknown, KFileItem::Unknown, KUrl(directory));
+        requestDelayedItemInfo(item);
+    }
+}
+
+void InfoSidebarPage::slotLeftDirectory(const QString& directory)
+{
+    if (m_shownUrl == KUrl(directory)) {
+        // The signal 'leftDirectory' is also emitted when a media
+        // has been unmounted. In this case no directory change will be
+        // done in Dolphin, but the Information Panel must be updated to
+        // indicate an invalid directory.
+        m_shownUrl = url();
+        showItemInfo();
+    }
+}
+
 bool InfoSidebarPage::applyPlace(const KUrl& url)
 {
     KFilePlacesModel* placesModel = DolphinSettings::instance().placesModel();
@@ -311,26 +351,27 @@ void InfoSidebarPage::showMetaInfo()
             m_metaTextLabel->add(i18nc("@label", "Size:"), KIO::convertSize(fileItem.size()));
             m_metaTextLabel->add(i18nc("@label", "Modified:"), fileItem.timeString());
 
-            // TODO: See convertMetaInfo below, find a way to display only interesting information
-            // in a readable way
-            const KFileMetaInfo::WhatFlags flags = KFileMetaInfo::Fastest |
-                                                   KFileMetaInfo::TechnicalInfo |
-                                                   KFileMetaInfo::ContentInfo |
-                                                   KFileMetaInfo::Thumbnail;
-            const QString path = fileItem.url().url();
-            const KFileMetaInfo fileMetaInfo(path, QString(), flags);
-            if (fileMetaInfo.isValid()) {
-                const QHash<QString, KFileMetaInfoItem>& items = fileMetaInfo.items();
-                QHash<QString, KFileMetaInfoItem>::const_iterator it = items.constBegin();
-                const QHash<QString, KFileMetaInfoItem>::const_iterator end = items.constEnd();
-                QString labelText;
-                while (it != end) {
-                    const KFileMetaInfoItem& metaInfoItem = it.value();
-                    const QVariant& value = metaInfoItem.value();
-                    if (value.isValid() && convertMetaInfo(metaInfoItem.name(), labelText)) {
-                        m_metaTextLabel->add(labelText, value.toString());
+            if (fileItem.isLocalFile()) {
+                // TODO: See convertMetaInfo below, find a way to display only interesting information
+                // in a readable way
+                const KFileMetaInfo::WhatFlags flags = KFileMetaInfo::Fastest |
+                                                       KFileMetaInfo::TechnicalInfo |
+                                                       KFileMetaInfo::ContentInfo;
+                const QString path = fileItem.url().path();
+                const KFileMetaInfo fileMetaInfo(path, QString(), flags);
+                if (fileMetaInfo.isValid()) {
+                    const QHash<QString, KFileMetaInfoItem>& items = fileMetaInfo.items();
+                    QHash<QString, KFileMetaInfoItem>::const_iterator it = items.constBegin();
+                    const QHash<QString, KFileMetaInfoItem>::const_iterator end = items.constEnd();
+                    QString labelText;
+                    while (it != end) {
+                        const KFileMetaInfoItem& metaInfoItem = it.value();
+                        const QVariant& value = metaInfoItem.value();
+                        if (value.isValid() && convertMetaInfo(metaInfoItem.name(), labelText)) {
+                            m_metaTextLabel->add(labelText, value.toString());
+                        }
+                        ++it;
                     }
-                    ++it;
                 }
             }
         }
@@ -347,7 +388,7 @@ void InfoSidebarPage::showMetaInfo()
             m_metaDataWidget->setFiles(urls);
         }
 
-        unsigned long int totalSize = 0;
+        quint64 totalSize = 0;
         foreach (const KFileItem& item, m_selection) {
             // Only count the size of files, not dirs to match what
             // DolphinViewContainer::selectionStatusBarText() does.
@@ -397,4 +438,67 @@ bool InfoSidebarPage::convertMetaInfo(const QString& key, QString& text) const
     return false;
 }
 
+void InfoSidebarPage::init()
+{
+    const int spacing = KDialog::spacingHint();
+
+    m_timer = new QTimer(this);
+    m_timer->setSingleShot(true);
+    connect(m_timer, SIGNAL(timeout()),
+            this, SLOT(slotTimeout()));
+
+    QVBoxLayout* layout = new QVBoxLayout;
+    layout->setSpacing(spacing);
+
+    // name
+    m_nameLabel = new QLabel(this);
+    QFont font = m_nameLabel->font();
+    font.setBold(true);
+    m_nameLabel->setFont(font);
+    m_nameLabel->setAlignment(Qt::AlignHCenter);
+    m_nameLabel->setWordWrap(true);
+    m_nameLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
+
+    // preview
+    m_preview = new PixmapViewer(this);
+    m_preview->setMinimumWidth(KIconLoader::SizeEnormous + KIconLoader::SizeMedium);
+    m_preview->setMinimumHeight(KIconLoader::SizeEnormous);
+
+    if (MetaDataWidget::metaDataAvailable()) {
+        // rating, comment and tags
+        m_metaDataWidget = new MetaDataWidget(this);
+        m_metaDataWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
+    }
+
+    // general meta text information
+    m_metaTextLabel = new MetaTextLabel(this);
+    m_metaTextLabel->setMinimumWidth(spacing);
+    m_metaTextLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
+
+    layout->addWidget(m_nameLabel);
+    layout->addWidget(new KSeparator(this));
+    layout->addWidget(m_preview);
+    layout->addWidget(new KSeparator(this));
+    if (m_metaDataWidget != 0) {
+        layout->addWidget(m_metaDataWidget);
+        layout->addWidget(new KSeparator(this));
+    }
+    layout->addWidget(m_metaTextLabel);
+
+    // ensure that widgets in the information side bar are aligned towards the top
+    layout->addStretch(1);
+    setLayout(layout);
+
+    org::kde::KDirNotify* dirNotify = new org::kde::KDirNotify(QString(), QString(),
+                                                               QDBusConnection::sessionBus(), this);
+    connect(dirNotify, SIGNAL(FileRenamed(QString, QString)), SLOT(slotFileRenamed(QString, QString)));
+    connect(dirNotify, SIGNAL(FilesAdded(QString)), SLOT(slotFilesAdded(QString)));
+    connect(dirNotify, SIGNAL(FilesChanged(QStringList)), SLOT(slotFilesChanged(QStringList)));
+    connect(dirNotify, SIGNAL(FilesRemoved(QStringList)), SLOT(slotFilesRemoved(QStringList)));
+    connect(dirNotify, SIGNAL(enteredDirectory(QString)), SLOT(slotEnteredDirectory(QString)));
+    connect(dirNotify, SIGNAL(leftDirectory(QString)), SLOT(slotLeftDirectory(QString)));
+
+    m_initialized = true;
+}
+
 #include "infosidebarpage.moc"