summaryrefslogtreecommitdiff
path: root/src/images.cpp
blob: 1059867b446c52ebad481694705191ec2b34976f (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
/*  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 "images.h"
#include <QFile>
#include <QDebug>

#ifdef USE_RESOURCES
#include <QResource>
#endif

#ifdef SMCODE
#include "global.h"
#include "preferences.h"
#include "paths.h"
using namespace Global;
#endif

QString Images::current_theme;
QString Images::themes_path;

bool Images::is_internal = false;

#ifdef USE_RESOURCES
QString Images::last_resource_loaded;
bool Images::has_rcc = false;

QString Images::resourceFilename() {
	QString filename = QString::null;

	if ((!themes_path.isEmpty()) && (!current_theme.isEmpty())) {
		filename = themes_path +"/"+ current_theme +"/"+ current_theme +".rcc";
	}

	qDebug() << "Images::resourceFilename:" << filename;

	return filename;
}
#endif

void Images::setTheme(const QString & name) {
	current_theme = name;

	is_internal = (QFile::exists(":/" + current_theme + "/style.qss"));
	if (is_internal) {
		qDebug() << "Images::setTheme:" << current_theme << "is an internal theme";
		setThemesPath("");
		#ifdef USE_RESOURCES
		has_rcc = false;
		#endif
		return;
	}

#ifdef SMCODE
	QString dir = Paths::configPath() + "/themes/" + name;
	if (QFile::exists(dir)) {
		setThemesPath(Paths::configPath() + "/themes/");
	} else {
		setThemesPath(Paths::themesPath());
	}
#endif

#ifdef USE_RESOURCES
	if (!last_resource_loaded.isEmpty()) {
		qDebug() << "Images::setTheme: unloading" << last_resource_loaded;
		QResource::unregisterResource(last_resource_loaded);
		last_resource_loaded = QString::null;
	}

	QString rs_file = resourceFilename();
	if ((!rs_file.isEmpty()) && (QFile::exists(rs_file))) {
		qDebug() << "Images::setTheme: loading" << rs_file;
		QResource::registerResource(rs_file);
		last_resource_loaded = rs_file;
		has_rcc = true;
	} else {
		has_rcc = false;
	}
	qDebug() << "Images::setTheme: has_rcc:" << has_rcc;
#endif
}

void Images::setThemesPath(const QString & folder) {
	themes_path = folder;
	qDebug() << "Images::setThemesPath:" << themes_path;
}

QString Images::file(const QString & name) {
#ifdef SMCODE
	if (current_theme != pref->iconset) {
		setTheme(pref->iconset);
	}
#endif

	QString icon_name;
	if (!current_theme.isEmpty()) {
	#ifdef USE_RESOURCES
		if (has_rcc || is_internal) {
			icon_name = ":/" + current_theme + "/"+ name;
		} else {
			icon_name = themes_path +"/"+ current_theme + "/"+ name;
		}
	#else
		if (is_internal) {
			icon_name = ":/" + current_theme + "/"+ name;
		} else {
			icon_name = themes_path +"/"+ current_theme + "/"+ name;
		}
	#endif
	}

	bool has_extension = name.contains(".");
	if (!has_extension) icon_name += ".png";

	//qDebug() << "Images::file:" << icon_name;
	if ((icon_name.isEmpty()) || (!QFile::exists(icon_name))) {
		icon_name = ":/default-theme/" + name;
		if (!has_extension) icon_name += ".png";
	}

	//qDebug() << "Images::file:" << icon_name;
	return icon_name;
}


QPixmap Images::icon(QString name, int size) {
	QString icon_name = file(name);
	QPixmap p(icon_name);

	if (!p.isNull()) {
		if (size != -1) {
			p = resize(&p, size);
		}
	}

	return p;
}

QPixmap Images::resize(QPixmap *p, int size) {
	return QPixmap::fromImage( (*p).toImage().scaled(size,size,Qt::IgnoreAspectRatio,Qt::SmoothTransformation) );
}

QPixmap Images::flip(QPixmap *p) {
	return QPixmap::fromImage( (*p).toImage().mirrored(true, false) );
}

QPixmap Images::flippedIcon(QString name, int size) {
	QPixmap p = icon(name, size);
	p = flip(&p);
	return p;
}

#ifdef SMCODE
QString Images::styleSheet(){
	QString filename;
	filename = themesDirectory() + "/main.css";
	QFile file(filename);
	if (file.exists()) {
		file.open(QFile::ReadOnly | QFile::Text);
		QString css = QString::fromUtf8(file.readAll().constData());
		return css;
	}
	else
		return "";
}

QString Images::themesDirectory(){
	QString skin = pref->iconset;
	QString dirname;
	if (!skin.isEmpty()) {
		dirname = Paths::configPath() + "/themes/" + skin;
		if (!QFile::exists(dirname)) {
			dirname = Paths::themesPath() + "/" + skin ;
		}
	}
	return dirname;
}
#endif