src/platform/qt/utils.h (view raw)
1/* Copyright (c) 2013-2017 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#pragma once
7
8#include <mgba/core/core.h>
9
10#include <QRect>
11#include <QSize>
12#include <QString>
13
14namespace QGBA {
15
16QString niceSizeFormat(size_t filesize);
17QString nicePlatformFormat(mPlatform platform);
18
19inline void lockAspectRatio(const QSize& ref, QSize& size) {
20 if (size.width() * ref.height() > size.height() * ref.width()) {
21 size.setWidth(size.height() * ref.width() / ref.height());
22 } else if (size.width() * ref.height() < size.height() * ref.width()) {
23 size.setHeight(size.width() * ref.height() / ref.width());
24 }
25}
26
27inline void lockIntegerScaling(const QSize& ref, QSize& size) {
28 if (size.width() >= ref.width()) {
29 size.setWidth(size.width() - size.width() % ref.width());
30 }
31 if (size.height() >= ref.height()) {
32 size.setHeight(size.height() - size.height() % ref.height());
33 }
34}
35
36inline QRect clampSize(const QSize& ref, const QSize& size, bool aspectRatio, bool integerScaling) {
37 QSize ds = size;
38 if (aspectRatio) {
39 lockAspectRatio(ref, ds);
40 }
41 if (integerScaling) {
42 QGBA::lockIntegerScaling(ref, ds);
43 }
44 QPoint origin = QPoint((size.width() - ds.width()) / 2, (size.height() - ds.height()) / 2);
45 return QRect(origin, ds);
46}
47
48}