]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/panels/information/informationpanelcontent.cpp
mediawidget: bind arrow keys to slide in media
[dolphin.git] / src / panels / information / informationpanelcontent.cpp
index d731d4de79ce2233f3a9c64d6ba82876934f5b09..134c5e0567e5aa8959d9602a46ac2fe310476afb 100644 (file)
@@ -10,6 +10,7 @@
 #include <KIO/PreviewJob>
 #include <KIconEffect>
 #include <KIconLoader>
+#include <KIconUtils>
 #include <KJobWidgets>
 #include <KLocalizedString>
 #include <KSeparator>
@@ -23,9 +24,6 @@
 
 #include <Baloo/FileMetaDataWidget>
 
-#include <phonon/BackendCapabilities>
-#include <phonon/MediaObject>
-
 #include <QDialogButtonBox>
 #include <QGesture>
 #include <QLabel>
@@ -39,7 +37,7 @@
 #include <QVBoxLayout>
 
 #include "dolphin_informationpanelsettings.h"
-#include "phononwidget.h"
+#include "mediawidget.h"
 #include "pixmapviewer.h"
 
 const int PLAY_ARROW_SIZE = 24;
@@ -51,7 +49,7 @@ InformationPanelContent::InformationPanelContent(QWidget *parent)
     , m_previewJob(nullptr)
     , m_outdatedPreviewTimer(nullptr)
     , m_preview(nullptr)
-    , m_phononWidget(nullptr)
+    , m_mediaWidget(nullptr)
     , m_nameLabel(nullptr)
     , m_metaDataWidget(nullptr)
     , m_metaDataArea(nullptr)
@@ -76,11 +74,11 @@ InformationPanelContent::InformationPanelContent(QWidget *parent)
     m_preview->setMinimumWidth(minPreviewWidth);
     m_preview->setMinimumHeight(KIconLoader::SizeEnormous);
 
-    m_phononWidget = new PhononWidget(parent);
-    m_phononWidget->hide();
-    m_phononWidget->setMinimumWidth(minPreviewWidth);
-    m_phononWidget->setAutoPlay(InformationPanelSettings::previewsAutoPlay());
-    connect(m_phononWidget, &PhononWidget::hasVideoChanged, this, &InformationPanelContent::slotHasVideoChanged);
+    m_mediaWidget = new MediaWidget(parent);
+    m_mediaWidget->hide();
+    m_mediaWidget->setMinimumWidth(minPreviewWidth);
+    m_mediaWidget->setAutoPlay(InformationPanelSettings::previewsAutoPlay());
+    connect(m_mediaWidget, &MediaWidget::hasVideoChanged, this, &InformationPanelContent::slotHasVideoChanged);
 
     // name
     m_nameLabel = new QLabel(parent);
@@ -131,7 +129,7 @@ InformationPanelContent::InformationPanelContent(QWidget *parent)
     viewport->installEventFilter(this);
 
     layout->addWidget(m_preview);
-    layout->addWidget(m_phononWidget);
+    layout->addWidget(m_mediaWidget);
     layout->addWidget(m_nameLabel);
     layout->addWidget(new KSeparator());
     layout->addWidget(m_configureLabel);
@@ -178,6 +176,7 @@ void InformationPanelContent::refreshPixmapView()
     m_previewJob = new KIO::PreviewJob(KFileItemList() << m_item, QSize(m_preview->width(), m_preview->height()), &plugins);
     m_previewJob->setScaleType(KIO::PreviewJob::Unscaled);
     m_previewJob->setIgnoreMaximumSize(m_item.isLocalFile() && !m_item.isSlow());
+    m_previewJob->setDevicePixelRatio(devicePixelRatioF());
     if (m_previewJob->uiDelegate()) {
         KJobWidgets::setWindow(m_previewJob, this);
     }
@@ -201,7 +200,7 @@ void InformationPanelContent::refreshPreview()
         const bool isSearchUrl = itemUrl.scheme().contains(QLatin1String("search")) && m_item.localPath().isEmpty();
         if (isSearchUrl) {
             m_preview->show();
-            m_phononWidget->hide();
+            m_mediaWidget->hide();
 
             // in the case of a search-URL the URL is not readable for humans
             // (at least not useful to show in the Information Panel)
@@ -212,21 +211,22 @@ void InformationPanelContent::refreshPreview()
             const QString mimeType = m_item.mimetype();
             const bool isAnimatedImage = m_preview->isAnimatedMimeType(mimeType);
             m_isVideo = !isAnimatedImage && mimeType.startsWith(QLatin1String("video/"));
-            bool usePhonon = m_isVideo || mimeType.startsWith(QLatin1String("audio/"));
+            bool useMedia = m_isVideo || mimeType.startsWith(QLatin1String("audio/"));
 
-            if (usePhonon) {
+            if (useMedia) {
                 // change the cursor of the preview
                 m_preview->setCursor(Qt::PointingHandCursor);
-                m_preview->installEventFilter(m_phononWidget);
-                m_phononWidget->show();
+                m_preview->installEventFilter(m_mediaWidget);
+
+                m_mediaWidget->show();
 
                 // if the video is playing, has been paused or stopped
-                // we don't need to update the preview/phonon widget states
+                // we don't need to update the preview/media widget states
                 // unless the previewed file has changed,
                 // or the setting previewshown has changed
-                if ((m_phononWidget->state() != Phonon::State::PlayingState && m_phononWidget->state() != Phonon::State::PausedState
-                     && m_phononWidget->state() != Phonon::State::StoppedState)
-                    || m_item.targetUrl() != m_phononWidget->url() || (!m_preview->isVisible() && !m_phononWidget->isVisible())) {
+                if ((m_mediaWidget->state() != QMediaPlayer::PlayingState && m_mediaWidget->state() != QMediaPlayer::PausedState
+                     && m_mediaWidget->state() != QMediaPlayer::StoppedState)
+                    || m_item.targetUrl() != m_mediaWidget->url() || (!m_preview->isVisible() && !m_mediaWidget->isVisible())) {
                     if (InformationPanelSettings::previewsAutoPlay() && m_isVideo) {
                         // hides the preview now to avoid flickering when the autoplay video starts
                         m_preview->hide();
@@ -235,24 +235,24 @@ void InformationPanelContent::refreshPreview()
                         m_preview->show();
                     }
 
-                    m_phononWidget->setUrl(m_item.targetUrl(), m_isVideo ? PhononWidget::MediaKind::Video : PhononWidget::MediaKind::Audio);
+                    m_mediaWidget->setUrl(m_item.targetUrl(), m_isVideo ? MediaWidget::MediaKind::Video : MediaWidget::MediaKind::Audio);
                     adjustWidgetSizes(parentWidget()->width());
                 }
             } else {
                 if (isAnimatedImage) {
                     m_preview->setAnimatedImageFileName(itemUrl.toLocalFile());
                 }
-                // When we don't need it, hide the phonon widget first to avoid flickering
-                m_phononWidget->hide();
+                // When we don't need it, hide the media widget first to avoid flickering
+                m_mediaWidget->hide();
                 m_preview->show();
-                m_preview->removeEventFilter(m_phononWidget);
-                m_phononWidget->clearUrl();
+                m_preview->removeEventFilter(m_mediaWidget);
+                m_mediaWidget->clearUrl();
             }
         }
     } else {
         m_preview->stopAnimatedImage();
         m_preview->hide();
-        m_phononWidget->hide();
+        m_mediaWidget->hide();
     }
 }
 
@@ -285,7 +285,7 @@ void InformationPanelContent::showItems(const KFileItemList &items)
 
     m_metaDataWidget->setItems(items);
 
-    m_phononWidget->hide();
+    m_mediaWidget->hide();
 
     m_item = KFileItem();
 }
@@ -350,8 +350,9 @@ bool InformationPanelContent::gestureEvent(QGestureEvent *event)
 void InformationPanelContent::showIcon(const KFileItem &item)
 {
     m_outdatedPreviewTimer->stop();
-    QPixmap pixmap = QIcon::fromTheme(item.iconName()).pixmap(m_preview->height(), m_preview->width());
-    KIconLoader::global()->drawOverlays(item.overlays(), pixmap, KIconLoader::Desktop);
+    QIcon icon = QIcon::fromTheme(item.iconName());
+    QPixmap pixmap = KIconUtils::addOverlays(icon, item.overlays()).pixmap(m_preview->size(), devicePixelRatioF());
+    pixmap.setDevicePixelRatio(devicePixelRatioF());
     m_preview->setPixmap(pixmap);
 }
 
@@ -360,7 +361,22 @@ void InformationPanelContent::showPreview(const KFileItem &item, const QPixmap &
     m_outdatedPreviewTimer->stop();
 
     QPixmap p = pixmap;
-    KIconLoader::global()->drawOverlays(item.overlays(), p, KIconLoader::Desktop);
+    if (!item.overlays().isEmpty()) {
+        // Avoid scaling the images that are smaller than the preview size, to be consistent when there is no overlays
+        if (pixmap.height() < m_preview->height() && pixmap.width() < m_preview->width()) {
+            p = QPixmap(m_preview->size() * devicePixelRatioF());
+            p.fill(Qt::transparent);
+            p.setDevicePixelRatio(devicePixelRatioF());
+
+            QPainter painter(&p);
+            painter.drawPixmap(QPointF{m_preview->width() / 2.0 - pixmap.width() / pixmap.devicePixelRatioF() / 2,
+                                       m_preview->height() / 2.0 - pixmap.height() / pixmap.devicePixelRatioF() / 2}
+                                   .toPoint(),
+                               pixmap);
+        }
+        p = KIconUtils::addOverlays(p, item.overlays()).pixmap(m_preview->size(), devicePixelRatioF());
+        p.setDevicePixelRatio(devicePixelRatioF());
+    }
 
     if (m_isVideo) {
         // adds a play arrow overlay
@@ -369,8 +385,8 @@ void InformationPanelContent::showPreview(const KFileItem &item, const QPixmap &
         auto arrowSize = qMax(PLAY_ARROW_SIZE, maxDim / 8);
 
         // compute relative pixel positions
-        const int zeroX = static_cast<int>((p.width() / 2 - arrowSize / 2) / pixmap.devicePixelRatio());
-        const int zeroY = static_cast<int>((p.height() / 2 - arrowSize / 2) / pixmap.devicePixelRatio());
+        const int zeroX = static_cast<int>((p.width() / 2 - arrowSize / 2) / p.devicePixelRatio());
+        const int zeroY = static_cast<int>((p.height() / 2 - arrowSize / 2) / p.devicePixelRatio());
 
         QPolygon arrow;
         arrow << QPoint(zeroX, zeroY);
@@ -410,8 +426,8 @@ void InformationPanelContent::markOutdatedPreview()
         // use it until the preview is done
         showIcon(m_item);
     } else {
-        KIconEffect *iconEffect = KIconLoader::global()->iconEffect();
-        QPixmap disabledPixmap = iconEffect->apply(m_preview->pixmap(), KIconLoader::Desktop, KIconLoader::DisabledState);
+        QPixmap disabledPixmap = m_preview->pixmap();
+        KIconEffect::toDisabled(disabledPixmap);
         m_preview->setPixmap(disabledPixmap);
     }
 }
@@ -433,7 +449,7 @@ void InformationPanelContent::slotHasVideoChanged(bool hasVideo)
 
 void InformationPanelContent::setPreviewAutoPlay(bool autoPlay)
 {
-    m_phononWidget->setAutoPlay(autoPlay);
+    m_mediaWidget->setAutoPlay(autoPlay);
 }
 
 void InformationPanelContent::setNameLabelText(const QString &text)
@@ -484,8 +500,10 @@ void InformationPanelContent::adjustWidgetSizes(int width)
     // try to increase the preview as large as possible
     m_preview->setSizeHint(QSize(maxWidth, maxWidth));
 
-    if (m_phononWidget->isVisible()) {
+    if (m_mediaWidget->isVisible()) {
         // assure that the size of the video player is the same as the preview size
-        m_phononWidget->setVideoSize(QSize(maxWidth, maxWidth));
+        m_mediaWidget->setVideoSize(QSize(maxWidth, maxWidth));
     }
 }
+
+#include "moc_informationpanelcontent.cpp"