]> cloud.milkyroute.net Git - dolphin.git/commitdiff
provide an unintrusive frame for previews of images
authorPeter Penz <peter.penz19@gmail.com>
Wed, 5 Mar 2008 23:50:22 +0000 (23:50 +0000)
committerPeter Penz <peter.penz19@gmail.com>
Wed, 5 Mar 2008 23:50:22 +0000 (23:50 +0000)
svn path=/trunk/KDE/kdebase/apps/; revision=782785

src/dolphiniconsview.cpp
src/iconmanager.cpp
src/iconmanager.h

index af7f026e1c627b547c30f3e0a8d9601c67a1c163..e92d8ba3b95a8d8773aead89cca923ac4a156428 100644 (file)
@@ -468,7 +468,6 @@ void DolphinIconsView::updateGridSize(bool showPreview, int additionalInfoCount)
 
         size = previewSize;
     }
-    setIconSize(QSize(size, size));
 
     Q_ASSERT(additionalInfoCount >= 0);
     itemHeight += additionalInfoCount * m_font.pointSize() * 2;
@@ -478,8 +477,10 @@ void DolphinIconsView::updateGridSize(bool showPreview, int additionalInfoCount)
         // width for the text wrapping. To use the maximum item width
         // for text wrapping, it is used as decoration width.
         m_decorationSize = QSize(itemWidth, size);
+        setIconSize(QSize(itemWidth, size));
     } else {
         m_decorationSize = QSize(size, size);
+        setIconSize(QSize(size, size));
     }
 
     m_itemSize = QSize(itemWidth, itemHeight);
index 807b91a9ea3ae45a2947db4eef8cce3d2e13dd3f..4c5c802cd7f39dfed71643febfabb0dc5f944449 100644 (file)
@@ -24,7 +24,6 @@
 
 #include <kiconeffect.h>
 #include <kio/previewjob.h>
-#include <kdebug.h>
 #include <kdirlister.h>
 #include <konqmimedata.h>
 
@@ -32,6 +31,7 @@
 #include <QAbstractItemView>
 #include <QClipboard>
 #include <QColor>
+#include <QPainter>
 #include <QIcon>
 
 IconManager::IconManager(QAbstractItemView* parent, DolphinSortFilterProxyModel* model) :
@@ -104,7 +104,7 @@ void IconManager::updateIcons(const KFileItemList& items)
     }
 
     const QSize size = m_view->iconSize();
-    KIO::PreviewJob* job = KIO::filePreview(orderedItems, size.width(), size.height());
+    KIO::PreviewJob* job = KIO::filePreview(orderedItems, 128, 128);
     connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
             this, SLOT(replaceIcon(const KFileItem&, const QPixmap&)));
     connect(job, SIGNAL(finished(KJob*)),
@@ -140,10 +140,17 @@ void IconManager::replaceIcon(const KFileItem& item, const QPixmap& pixmap)
     const QModelIndex idx = m_dolphinModel->indexForItem(item);
     if (idx.isValid() && (idx.column() == 0)) {
         QPixmap icon = pixmap;
+
+        const QString mimeType = item.mimetype();
+        const QString mimeTypeGroup = mimeType.left(mimeType.indexOf('/'));
+        if ((mimeTypeGroup != "image") || !applyImageFrame(icon)) {
+            limitToSize(icon, m_view->iconSize());
+        }
+
         if (item.isHidden()) {
             if (!icon.hasAlpha()) {
                 // the semitransparent operation requires having an alpha mask
-                QPixmap alphaMask(icon.width(), icon.height());
+                QPixmap alphaMask(icon.size());
                 alphaMask.fill();
                 icon.setAlphaChannel(alphaMask);
             }
@@ -248,4 +255,71 @@ void IconManager::applyHiddenItemEffect(const KFileItem& hiddenItem)
     }
 }
 
+bool IconManager::applyImageFrame(QPixmap& icon)
+{
+    const QSize maxSize = m_view->iconSize();
+    if ((maxSize.width() <= 24) || (maxSize.height() <= 24)) {
+        // the maximum size is too small for a frame
+        return false;
+    }
+
+    const int frame = 4;
+    const int doubleFrame = frame * 2;
+
+    // resize the icon to the maximum size minus the space required for the frame
+    limitToSize(icon, QSize(maxSize.width() - doubleFrame, maxSize.height() - doubleFrame));
+
+    QPainter painter;
+    const QPalette palette = m_view->palette();
+    QPixmap framedIcon(icon.size().width() + doubleFrame, icon.size().height() + doubleFrame);
+    framedIcon.fill(palette.color(QPalette::Normal, QPalette::Base));
+    const int width = framedIcon.width() - 1;
+    const int height = framedIcon.height() - 1;
+
+    painter.begin(&framedIcon);
+    painter.drawPixmap(frame, frame, icon);
+
+    // draw a white frame around the icon
+    painter.setPen(Qt::NoPen);
+    painter.setBrush(palette.brush(QPalette::Normal, QPalette::Base));
+    painter.drawRect(0, 0, width, frame);
+    painter.drawRect(0, height - frame, width, frame);
+    painter.drawRect(0, frame, frame,  height - doubleFrame);
+    painter.drawRect(width - frame, frame, frame,  height - doubleFrame);
+
+    // add a border
+    painter.setPen(palette.color(QPalette::Text));
+    painter.setBrush(Qt::NoBrush);
+    painter.drawRect(0, 0, width, height);
+    painter.drawRect(1, 1, width - 2, height - 2);
+
+    // dimm image frame by 25 %
+    painter.setPen(QColor(0, 0, 0, 64));
+    painter.drawRect(frame, frame, width - doubleFrame, height - doubleFrame);
+    painter.end();
+
+    icon = framedIcon;
+
+    // provide an alpha channel for the border
+    QPixmap alphaChannel(icon.size());
+    alphaChannel.fill();
+
+    QPainter alphaPainter(&alphaChannel);
+    alphaPainter.setBrush(Qt::NoBrush);
+    alphaPainter.setPen(QColor(32, 32, 32));
+    alphaPainter.drawRect(0, 0, width, height);
+    alphaPainter.setPen(QColor(64, 64, 64));
+    alphaPainter.drawRect(1, 1, width - 2, height - 2);
+
+    icon.setAlphaChannel(alphaChannel);
+    return true;
+}
+
+void IconManager::limitToSize(QPixmap& icon, const QSize& maxSize)
+{
+    if ((icon.width() > maxSize.width()) || (icon.height() > maxSize.height())) {
+        icon = icon.scaled(maxSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
+    }
+}
+
 #include "iconmanager.moc"
index d6cf5332b6fef16d25208eb1fa9032e266436ae8..c96759b13de9b22248d9717ea3e0016773ef6bbe 100644 (file)
@@ -85,6 +85,19 @@ private:
     /** Applies an item effect to the hidden item \a hiddenItem. */
     void applyHiddenItemEffect(const KFileItem& hiddenItem);
 
+    /**
+     * Applies a frame around the icon. False is returned if
+     * no frame has been added because the icon is too small.
+     */
+    bool applyImageFrame(QPixmap& icon);
+
+    /**
+     * Resizes the icon to \a maxSize if the icon size does not
+     * fit into the maximum size. The aspect ratio of the icon
+     * is kept.
+     */
+    void limitToSize(QPixmap& icon, const QSize& maxSize);
+
 private:
     /**
      * Remembers the original pixmap for an item before