Guest User

Untitled

a guest
Mar 6th, 2014
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | None | 0 0
  1. class RetinaImageProvider : public QQuickImageProvider {
  2. public:
  3.     RetinaImageProvider()
  4.       : QQuickImageProvider(QQuickImageProvider::Pixmap)
  5.     {
  6.     }
  7.  
  8.     virtual QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
  9.     {
  10. #ifdef Q_OS_ANDROID
  11.         QString localFile("assets:/qml/img/" + id);
  12. #else
  13.         QString localFile("qml/img/" + id);
  14. #endif
  15.         bool retina(false);
  16.         if (qApp->devicePixelRatio() * QGuiApplication::primaryScreen()->geometry().height() > 768) {
  17.             const int dotIndex = localFile.lastIndexOf(QLatin1Char('.'));
  18.             if (dotIndex != -1) {
  19.                 QString retinaFile = localFile;
  20.                 retinaFile.insert(dotIndex, QStringLiteral("@2x"));
  21.                 if (QFile(retinaFile).exists()) {
  22.                     localFile = retinaFile;
  23.                     retina = true;
  24.                 }
  25.             }
  26.         }
  27.  
  28.         QPixmap pixmap(localFile);
  29.         int width(pixmap.width());
  30.         int height(pixmap.height());
  31.         *size = QSize(width, height);
  32.         if (requestedSize.width() > 0 || requestedSize.height() > 0) {
  33.             pixmap = pixmap.scaled(QSize(
  34.                 requestedSize.width() > 0 ? requestedSize.width() : width,
  35.                 requestedSize.height() > 0 ? requestedSize.height() : height));
  36.         }
  37.         return pixmap;
  38.     }
  39. };
Advertisement
Add Comment
Please, Sign In to add comment