UI: Add AppUtil.

This commit is contained in:
Nodir Temirkhodjaev 2019-03-29 14:06:07 +05:00
parent 1c5b87ef30
commit 12c9e3d3a5
4 changed files with 83 additions and 0 deletions

View File

@ -48,6 +48,7 @@ SOURCES += \
translationmanager.cpp \
util/app/appinfomanager.cpp \
util/app/appinfoworker.cpp \
util/app/apputil.cpp \
util/conf/addressrange.cpp \
util/conf/confutil.cpp \
util/dateutil.cpp \
@ -111,6 +112,7 @@ HEADERS += \
translationmanager.h \
util/app/appinfomanager.h \
util/app/appinfoworker.h \
util/app/apputil.h \
util/conf/addressrange.h \
util/conf/confutil.h \
util/dateutil.h \

View File

@ -3,6 +3,7 @@
#include <QImage>
#include "appinfomanager.h"
#include "apputil.h"
AppInfoWorker::AppInfoWorker(AppInfoManager *manager) :
WorkerObject(manager)

View File

@ -0,0 +1,63 @@
#include "apputil.h"
#include <QDir>
#include <comdef.h>
#include <commctrl.h>
#include <commoncontrols.h>
#include <objbase.h>
#include <shellapi.h>
// Defined in qpixmap_win.cpp
Q_GUI_EXPORT QPixmap qt_pixmapFromWinHICON(HICON icon);
namespace {
static QPixmap pixmapFromImageList(int iImageList, const SHFILEINFO &info)
{
QPixmap result;
IImageList *imageList;
HICON hIcon;
if (SUCCEEDED(SHGetImageList(iImageList, IID_IImageList, reinterpret_cast<void **>(&imageList)))
&& SUCCEEDED(imageList->GetIcon(info.iIcon, ILD_TRANSPARENT, &hIcon))) {
result = qt_pixmapFromWinHICON(hIcon);
DestroyIcon(hIcon);
}
return result;
}
static QPixmap extractShellIcon(const QString &appPath)
{
const QString nativePath = QDir::toNativeSeparators(appPath);
const wchar_t *nativePathW = reinterpret_cast<const wchar_t *>(nativePath.utf16());
const UINT flags = SHGFI_SYSICONINDEX | SHGFI_USEFILEATTRIBUTES;
QPixmap pixmap;
SHFILEINFOW info;
ZeroMemory(&info, sizeof(SHFILEINFOW));
const HRESULT hr = SHGetFileInfoW(nativePathW, 0, &info,
sizeof(SHFILEINFOW), flags);
if (SUCCEEDED(hr)) {
pixmap = pixmapFromImageList(SHIL_JUMBO, info);
}
return pixmap;
}
}
AppUtil::AppUtil(QObject *parent) :
QObject(parent)
{
}
QPixmap AppUtil::getIcon(const QString &appPath)
{
return extractShellIcon(appPath);
}

17
src/ui/util/app/apputil.h Normal file
View File

@ -0,0 +1,17 @@
#ifndef APPUTIL_H
#define APPUTIL_H
#include <QObject>
#include <QPixmap>
class AppUtil : public QObject
{
Q_OBJECT
public:
explicit AppUtil(QObject *parent = nullptr);
Q_INVOKABLE static QPixmap getIcon(const QString &appPath);
};
#endif // APPUTIL_H