Changeset 14195


Ignore:
Timestamp:
Sep 8, 2013, 6:53:11 PM (8 years ago)
Author:
jordan
Message:

(trunk, qt) #5483: use native windows icons, more win portability goodness from mikedld

Location:
trunk/qt
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/qt/utils.cc

    r13730 r14195  
    1313#include <iostream>
    1414
     15#ifdef WIN32
     16 #include <windows.h>
     17 #include <shellapi.h>
     18#endif
     19
    1520#include <QApplication>
    1621#include <QDataStream>
     
    2025#include <QInputDialog>
    2126#include <QObject>
     27#include <QPixmapCache>
    2228#include <QSet>
    2329#include <QStyle>
     
    3137****
    3238***/
     39
     40#if defined(WIN32) && QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
     41// Should be in QtWinExtras soon, but for now let's import it manually
     42extern QPixmap qt_pixmapFromWinHICON(HICON icon);
     43#endif
    3344
    3445QString
     
    5667}
    5768
    58 const QIcon&
     69#ifdef WIN32
     70namespace
     71{
     72  void
     73  addAssociatedFileIcon (const QFileInfo& fileInfo, UINT iconSize, QIcon& icon)
     74  {
     75    QString const pixmapCacheKey = QLatin1String ("tr_file_ext_") +
     76      QString::number (iconSize) + "_" + fileInfo.suffix ();
     77    QPixmap pixmap;
     78    if (!QPixmapCache::find (pixmapCacheKey, &pixmap))
     79      {
     80        const QString filename = fileInfo.fileName ();
     81
     82        SHFILEINFO shellFileInfo;
     83        if (::SHGetFileInfoW ((const wchar_t*) filename.utf16 (), FILE_ATTRIBUTE_NORMAL,
     84                              &shellFileInfo, sizeof(shellFileInfo),
     85                              SHGFI_ICON | iconSize | SHGFI_USEFILEATTRIBUTES) != 0)
     86          {
     87            if (shellFileInfo.hIcon != NULL)
     88              {
     89#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
     90                pixmap = qt_pixmapFromWinHICON (shellFileInfo.hIcon);
     91#else
     92                pixmap = QPixmap::fromWinHICON (shellFileInfo.hIcon);
     93#endif
     94                ::DestroyIcon (shellFileInfo.hIcon);
     95              }
     96          }
     97
     98        QPixmapCache::insert (pixmapCacheKey, pixmap);
     99      }
     100
     101    if (!pixmap.isNull ())
     102      icon.addPixmap (pixmap);
     103  }
     104} // namespace
     105#endif
     106
     107QIcon
    59108Utils :: guessMimeIcon( const QString& filename )
    60109{
     110#ifdef WIN32
     111  QIcon icon;
     112
     113  if (!filename.isEmpty ())
     114    {
     115      const QFileInfo fileInfo (filename);
     116
     117      addAssociatedFileIcon (fileInfo, SHGFI_SMALLICON, icon);
     118      addAssociatedFileIcon (fileInfo, 0, icon);
     119      addAssociatedFileIcon (fileInfo, SHGFI_LARGEICON, icon);
     120    }
     121
     122  if (icon.isNull ())
     123    icon = QApplication::style ()->standardIcon (QStyle::SP_FileIcon);
     124
     125  return icon;
     126#else
    61127    enum { DISK, DOCUMENT, PICTURE, VIDEO, ARCHIVE, AUDIO, APP, TYPE_COUNT };
    62128    static QIcon fallback;
     
    117183
    118184    return fallback;
     185#endif
    119186}
    120187
  • trunk/qt/utils.h

    r14150 r14195  
    3232    public:
    3333        static QString remoteFileChooser( QWidget * parent, const QString& title, const QString& myPath, bool dir, bool local );
    34         static const QIcon& guessMimeIcon( const QString& filename );
     34        static QIcon guessMimeIcon( const QString& filename );
    3535        // Test if string is UTF-8 or not
    3636        static bool isValidUtf8 ( const char *s );
Note: See TracChangeset for help on using the changeset viewer.