aboutsummaryrefslogtreecommitdiff
path: root/VMProtect/main.cc
blob: 6c973088bd9e29e2be2e8d1d4197ac3d3bd3ca20 (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "../core/objects.h"
#include "../core/osutils.h"
#include "application.h"
#include "moc/moc_application.cc"
#include "mainwindow.h"
#include "help_browser.h"

#ifndef VMP_GNU
#ifndef _DEBUG
#include <QtCore\QtPlugin>
Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin);
#endif
#endif

class PatchedProxyStyle : public QProxyStyle
{
public:
	PatchedProxyStyle() : QProxyStyle("windows") {}

	int styleHint(StyleHint hint, const QStyleOption *opt, const QWidget *widget,
		QStyleHintReturn *returnData) const
	{
		int ret = 0;
		switch (hint) 
		{
		case QStyle::SH_MainWindow_SpaceBelowMenuBar:
			ret = 0;
			break;
		default:
			ret = QProxyStyle::styleHint(hint, opt, widget, returnData);
			break;
		}
		return ret;
	}

	void drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const
	{
		if(element == QStyle::PE_FrameFocusRect) 
		{
			if (dynamic_cast<const QTabBar *>(widget))
			{
				// compensate 'const int OFFSET = 1 + pixelMetric(PM_DefaultFrameWidth)' at qcommonstyle.cpp:1891
				const_cast<QStyleOption *>(option)->rect.adjust(-8, -3, 8, 3);
			}
		}
		QProxyStyle::drawPrimitive(element, option, painter, widget);
	}
};

/*
 Application
 */

Application::Application(int &argc, char **argv) 
	: QApplication(argc, argv) 
{
	installStylesheet();

	QStringList arguments = QCoreApplication::arguments();
	if (arguments.length() >= 3 && 0 == arguments.at(1).compare("--help"))
	{
		help_filename_ = arguments.at(2);
	}
#ifdef VMP_GNU
	if (isHelpMode()) {
		QIcon icon(":/images/help_icon.png");
#ifdef __APPLE__
		void qt_mac_set_app_icon(const QIcon &);
		qt_mac_set_app_icon(icon);
#endif
		setWindowIcon(icon);
	}
#ifdef __unix__
	else
	{
		QIcon icon(":/images/logo.png");
		setWindowIcon(icon);
	}
#endif
#else
	HICON icon = static_cast<HICON>(LoadImageA(GetModuleHandle(NULL), 
		MAKEINTRESOURCEA(isHelpMode() ? 3 : 1),
		IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_LOADTRANSPARENT));
	setWindowIcon(QtWin::fromHICON(icon));
	::DestroyIcon(icon);
#endif
}

double Application::stylesheetScaleFactor_ = 96.0 / 72.0, Application::devicePixelRatio_ = 1.0;
void Application::initScalingConstants()
{
	double dpi = 96.0;
	QScreen *srn = QApplication::primaryScreen();
	if (srn)
		dpi = (double)srn->logicalDotsPerInch();

	devicePixelRatio_ = 
#ifdef __APPLE__
		1.4 *  // проверял на виртуалке (RENDER_DPI для мака, говорят, примерно во столько раз меньше), но подозреваю, что на макбуке с ретиной получим сюрприз
#endif
		devicePixelRatio();

	double dprOverride = QString(qgetenv("VMP_PIXEL_RATIO")).toDouble();
	if (dprOverride != 0.0)
	{
		devicePixelRatio_ = dprOverride;
	}
	stylesheetScaleFactor_ = dpi * devicePixelRatio_ / 72.0; // 'point' to pixel
}

Application::~Application()
{
	QFile t(os::CombineThisAppDataDirectory("styles.qss.tmp").c_str());
	if (t.exists())
		t.remove();
}

bool Application::event(QEvent *event)
{
	switch (event->type()) {
	case QEvent::FileOpen:
		if (isHelpMode() == false) 
		{
			emit fileOpenEvent(static_cast<QFileOpenEvent *>(event)->file());
			return true;
		}
	default:
		return QApplication::event(event);
	}

}

void Application::installStylesheet()
{
	initScalingConstants();
	std::string qssFileName = os::CombineThisAppDataDirectory("styles.qss");
	if (!QFile::exists(qssFileName.c_str()))
		qssFileName = ":/styles.qss";

	QFile f(qssFileName.c_str());
	if (f.open(QIODevice::ReadOnly))
	{
		QByteArray qssData = f.readAll();
		QByteArray qssDataTransformed = transformStylesheet(qssData);
		QFile t(os::CombineThisAppDataDirectory("styles.qss.tmp").c_str());
		if (t.open(QIODevice::WriteOnly | QIODevice::Truncate))
		{
			t.write(qssDataTransformed);
		}
		setStyleSheet(qssDataTransformed);
	}
}

QByteArray Application::transformStylesheet(const QByteArray &qssData)
{
	enum {
		OUTSIDE, INSIDE
	} state = OUTSIDE;
	QByteArray ret;
	ret.reserve(qssData.length() * 2);
	QString current;
	for(int idx = 0; idx < qssData.length(); ++idx)
	{
		char ch = qssData[idx];
		switch(state)
		{
		case OUTSIDE:
			if (ch == '<')
				state = INSIDE;
			else
				ret.append(ch);
			break;
		case INSIDE:
			if (ch == '>')
			{
				double val = stylesheetScaleFactor_ * current.toDouble();
				char buf[30];
				sprintf_s(buf, "%dpx", int(val + 0.5));
				ret.append(buf);
				current.clear();
				state = OUTSIDE;
			} else
			{
				current.append(ch);
			}
			break;
		}
	}
	return ret;
}

int main(int argc, char *argv[])
{
	QString file_name;

#ifdef VMP_GNU
	file_name = QString::fromUtf8(argv[0]);
#else
	wchar_t **argv_w = CommandLineToArgvW(GetCommandLineW(), &argc);
	if (argc)
		file_name = QString::fromWCharArray(argv_w[0]);
	LocalFree(argv_w);
#endif

	QDir dir = QFileInfo(file_name).absoluteDir();
#ifdef __APPLE__
	dir.cdUp();
#endif
	dir.cd("PlugIns");

	//QApplication::setLibraryPaths(QStringList(dir.absolutePath()));
	QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
	QApplication::setStyle(new PatchedProxyStyle());

	Application app(argc, argv);
	std::auto_ptr<QMainWindow> win;
	if(app.isHelpMode())
	{
		win.reset(new HelpBrowser(app.helpFileName()));
		win->show();
	} else
	{
		win.reset(new MainWindow);
		win->connect(&app, SIGNAL(fileOpenEvent(const QString &)), win.get(), SLOT(loadFile(const QString &)));
		win->showMaximized();
	}
	return app.exec();
}