summaryrefslogtreecommitdiff
path: root/src/hdpisupport.cpp
blob: 11091bf04dc82a421f77d42a5bdebdc08e3f18d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*  smplayer, GUI front-end for mplayer.
    Copyright (C) 2006-2018 Ricardo Villalba <rvm@users.sourceforge.net>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "hdpisupport.h"
#include <QSettings>
#include <QApplication>

//#define DEBUG

#ifdef DEBUG
#include <QDebug>
#endif

HDPISupport * HDPISupport::instance_obj = 0;

HDPISupport * HDPISupport::instance() {
	if (instance_obj == 0) {
		instance_obj = new HDPISupport();
	}
	return instance_obj;
}

HDPISupport::HDPISupport(const QString & config_path)
#ifdef Q_OS_WIN
	: enabled(true)
#else
	: enabled(false)
#endif
	, auto_scale(true)
	, scale_factor(1)
	, pixel_ratio(2)
{
	instance_obj = this;

#ifdef HDPI_STORE_DATA
	set = 0;
	setConfigPath(config_path);
#else
	apply();
#endif
}

HDPISupport::~HDPISupport() {
#ifdef HDPI_STORE_DATA
	if (set) {
		save();
		delete set;
	}
#endif
	instance_obj = 0;
}

#ifdef HDPI_STORE_DATA
void HDPISupport::setConfigPath(const QString & config_path) {
	#ifdef DEBUG
	qDebug() << "HDPISupport::setConfigPath:" << config_path;
	#endif

	if (set) {
		delete set;
		set = 0;
	}

	if (!config_path.isEmpty()) {
		QString inifile = config_path + "/hdpi.ini";
		#ifdef DEBUG
		qDebug() << "HDPISupport::setConfigPath: ini file:" << inifile;
		#endif
		set = new QSettings(inifile, QSettings::IniFormat);
		load();
	}

	apply();
}

bool HDPISupport::load() {
	#ifdef DEBUG
	qDebug("HDPISupport::load");
	#endif

	if (!set) return false;

	set->beginGroup("hdpisupport");
	enabled = set->value("enabled", enabled).toBool();
	auto_scale = set->value("auto_scale", auto_scale).toBool();
	scale_factor = set->value("scale_factor", scale_factor).toDouble();
	pixel_ratio = set->value("pixel_ratio", pixel_ratio).toInt();
	set->endGroup();

	return true;
}

bool HDPISupport::save() {
	#ifdef DEBUG
	qDebug("HDPISupport::save");
	#endif

	if (!set) return false;

	set->beginGroup("hdpisupport");
	set->setValue("enabled", enabled);
	set->setValue("auto_scale", auto_scale);
	set->setValue("scale_factor", scale_factor);
	set->setValue("pixel_ratio", pixel_ratio);
	set->endGroup();

	return true;
}
#endif

void HDPISupport::apply() {
	#ifdef DEBUG
	qDebug("HDPISupport::apply");
	#endif

	if (enabled) {
		#if QT_VERSION >= 0x050400 && QT_VERSION < 0x050600
		if (qgetenv("QT_DEVICE_PIXEL_RATIO").isEmpty()) {
			qputenv("QT_DEVICE_PIXEL_RATIO", QByteArray("auto"));
		}
		if (!auto_scale) {
			qputenv("QT_DEVICE_PIXEL_RATIO", QByteArray::number(pixel_ratio));
		}
		#elif QT_VERSION >= 0x050600
		QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
		if (!auto_scale) {
			qputenv("QT_SCALE_FACTOR", QByteArray::number(scale_factor));
		}
		#endif
	}
}