forked from luebking/virtuality
/
init.cpp
377 lines (336 loc) · 13.2 KB
/
init.cpp
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
* Virtuality Style for Qt4 and Qt5
* Copyright 2009-2014 by Thomas Lübking <thomas.luebking@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
*
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <QApplication>
#include <QDir>
#include <QFile>
#include <QPainter>
#include <QProcess>
#include <QSettings>
#include <QTimer>
#include <cmath>
#include "animator/tab.h"
#include "virtuality.h"
#include "hacks.h"
#include "makros.h"
#include "config.defaults"
#include <QtDebug>
using namespace BE;
#define SCALE(_N_) qRound((_N_)*config.scale)
static int clamp(int x, int l, int u)
{
return CLAMP(x,l,u);
}
#if 1
static int fontOffset(bool bold = false, int *extend = 0)
{
QString string = "qtipdfghjklöäyxbQWRTZIPÜSDFGHJKLÖÄYXVBN!()?ß\"";
QFont font;
font.setBold(bold);
QFontMetrics metrics(font);
QImage img(metrics.size(0, string) + QSize(4,4), QImage::Format_ARGB32);
img.fill(0xffffffff);
QPainter p(&img); p.setPen(Qt::black); p.setFont(font); p.drawText(img.rect(), Qt::AlignCenter, string); p.end();
int y1 = 0, y2 = 0;
for (int y = 0; y < img.height(); ++y)
{
QRgb *scanLine = (QRgb*)img.scanLine(y);
for (int x = 0; x < img.width(); ++x)
{
if (qRed(*scanLine++) < 128)
{ y1 = y; goto descent; }
}
}
descent:
for (int y = img.height()-1; y > -1; --y)
{
QRgb *scanLine = (QRgb*)img.scanLine(y);
for (int x = 0; x < img.width(); ++x)
{
if (qRed(*scanLine++) < 128)
{ y2 = img.height()-y-1; goto offset; }
}
}
offset:
if ( extend )
{
*extend = y1 + y2 - 4;
*extend -= qAbs(*extend)%2;
// qDebug() << y1 << y2 << *extend;
}
// qDebug() << y1 << y2;
if ( y1 == y2) // font is centered ... hopefully ...
return 0;
int off = (y2-y1)/2;
if ( !off )
{
off += (y1 < 2); // the font has a negtive extent to the top
off -= (y2 < 2); // the font has a negtive extent to the bottom
}
// qDebug() << "->" << off;
return off;
// return y2 - y1/2;
// return (y2-y1)/2;
}
#endif
#define readInt(_DEF_) iSettings->value(_DEF_).toInt()
#define readBool(_DEF_) iSettings->value(_DEF_).toBool()
#define readRole(_VAR_, _DEF_)\
config._VAR_##_role[0] = (QPalette::ColorRole) iSettings->value(_DEF_).toInt();\
FX::counterRole(config._VAR_##_role[0], config._VAR_##_role[1])
//, QPalette::_DEF_, FX::counterRole(QPalette::_DEF_))
void
Style::removeAppEventFilter()
{
// for plain qt apps that don't need the palette fix, otherwise we'd keep filtering the app for no reason
delete originalPalette;
originalPalette = 0;
qApp->removeEventFilter(this);
}
void
Style::setMenuIconsVisible(bool vis)
{
qApp->setAttribute(Qt::AA_DontShowIconsInMenus, !vis);
}
void
Style::readSettings(QString appName)
{
QSettings *iSettings = new QSettings("BE", "Style");
iSettings->beginGroup("Current");
//BEGIN read some user personal settings (i.e. not preset related)
config.leftHanded = readBool(LEFTHANDED) ? Qt::RightToLeft : Qt::LeftToRight;
// item single vs. double click, wizard appereance
config.macStyle = readBool(MACSTYLE);
config.dialogBtnLayout = readInt(DIALOG_BTN_LAYOUT);
config.fadeInactive = readBool(FADE_INACTIVE);
// Hacks ==================================
Hacks::config.windowMovement = readBool(HACK_WINDOWMOVE);
Hacks::config.fixGwenview = readBool(HACK_FIX_GWENVIEW);
Hacks::config.lockToolBars = readBool(HACK_TOOLBAR_LOCKING);
Hacks::config.lockDocks = readBool(HACK_DOCK_LOCKING);
Hacks::config.panning = readBool(HACK_PANNING);
Hacks::config.suspendFullscreenPlayers = readBool(HACK_SUSPEND_FULLSCREEN_PLAYERS);
if (Hacks::config.suspendFullscreenPlayers)
Hacks::config.suspendFullscreenPlayers = ( appName == "dragonplayer" || appName == "smplayer" ||
appName == "minitube" || appName == "vlc" );
Hacks::config.titleWidgets = readBool(HACK_TITLE_WIDGET);
Hacks::config.suppressBrightnessOSD = readBool(HACK_BRIGHTNESS_OSD);
// Font fixing offsets
config.fontOffset[0] = fontOffset(false, &config.fontExtent);
config.fontExtent = iSettings->value( "FontExtent", config.fontExtent ).toInt();
config.fontOffset[1] = fontOffset(true);
QStringList lst = iSettings->value( "FontOffset", QStringList() ).toStringList();
if ( lst.count() > 0 )
{
config.fontOffset[0] = lst.at(0).toInt();
if ( lst.count() > 1 )
config.fontOffset[1] = lst.at(1).toInt();
else
config.fontOffset[1] = config.fontOffset[0];
}
// PW Echo Char ===========================
config.input.pwEchoChar = ushort(iSettings->value(INPUT_PWECHOCHAR).toUInt());
config.menu.delay = readInt(MENU_DELAY);
const bool menuIconsVis = readBool(MENU_SHOWICONS);
// allow (K)Application to manipulate this first
QMetaObject::invokeMethod(this, "setMenuIconsVisible", Qt::QueuedConnection, Q_ARG(bool, menuIconsVis));
config.menu.indent = readBool(MENU_INDENT);
config.winBtnStyle = 2; // this is a kwin deco setting, TODO: read from there?
Animator::Tab::setTransition((Animator::Transition) readInt(TAB_TRANSITION));
Animator::Tab::setDuration(clamp(iSettings->value(TAB_DURATION).toUInt(), 150, 4000));
//END personal settings
//NOTICE we do not end group here, but below. this way it's open if we don't make use of presets
// Background ===========================
config.invert.docks = readBool(INVERT_DOCKS);
config.invert.headers = readBool(INVERT_HEADERS);
config.invert.menubars = readBool(INVERT_MENUBARS);
config.invert.menus = readBool(INVERT_MENUS);
config.invert.modals = (appType != KDM) && readBool(INVERT_MODALS);
config.invert.titlebars = readBool(INVERT_TITLEBARS);
config.invert.toolbars = readBool(INVERT_TOOLBARS);
config.bg.opacity = readInt(BG_OPACITY);
config.bg.modal.opacity = readInt(BG_MODAL_OPACITY);
if (config.bg.opacity || config.bg.modal.opacity)
config.bg.blur = readBool(BG_BLUR);
else
config.bg.blur = false;
config.bg.ringOverlay = readInt(BG_RING_OVERLAY);
// Buttons ===========================
config.btn.minHeight = readInt(BTN_MIN_HEIGHT);
// .Tool
config.btn.tool.disabledStyle = readInt(BTN_DISABLED_TOOLS);
//--------
config.slider.buttons = readBool(SLIDER_BUTTONS);
if (readBool(SHOW_MNEMONIC))
config.mnemonic = Qt::TextShowMnemonic;
else
config.mnemonic = Qt::TextHideMnemonic;
// Tabs ===========================
Animator::Tab::setFPS(25);
// General ===========================
QWidget dummy; // calling out to qApp->desktopWidget() would seem natural
// BUT BREAKS THE CLIPBOARD!
config.scale = (dummy.logicalDpiX() + dummy.logicalDpiY()) / 170.0f;
QFont fnt = qApp->font();
if (fnt.pointSizeF() > -1)
config.scale *= fnt.pointSizeF()/10.0f;
if (const char *scale = getenv("VIRTUALITY_SCALE")) {
bool ok = false;
const float envScale = QString(scale).toFloat(&ok); // CLAMP(envScale, 1.0f, 3.0f);
if (ok) {
if (envScale != config.scale) {
scale = getenv("VIRTUALITY_SCALE_FONT");
if (!qstrcmp(scale, "true")) {
if (fnt.pointSize() > -1)
fnt.setPointSizeF(fnt.pointSizeF()*envScale/config.scale);
else
fnt.setPixelSize(fnt.pixelSize()*envScale/config.scale);
qApp->setFont(fnt);
}
}
config.scale = envScale;
}
}
config.frame.roundness = SCALE(readInt(ROUNDNESS));
// config.strokeWidth = qMin(config.frame.roundness, SCALE(readInt(STROKE_WIDTH)));
config.strokeWidth = SCALE(readInt(STROKE_WIDTH));
halfStroke = 0.5*config.strokeWidth;
//NOTICE gtk-qt fails on several features
// a key problem seems to be fixed text colors
// also it will segfault if we hide scrollbar buttons
// so we adjust some settings here
if (appType == GTK || appType == OpenOffice)
{
config.bg.modal.opacity = 255;
config.invert.docks = config.invert.menubars = config.invert.menus = config.invert.modals =
config.invert.toolbars = config.invert.headers = false;
}
delete iSettings;
}
#undef readRole
#undef gradientType
void Style::initMetrics()
{
BE::Dpi::target.f1 = SCALE(1); BE::Dpi::target.f2 = SCALE(2);
BE::Dpi::target.f3 = SCALE(3); BE::Dpi::target.f4 = SCALE(4);
BE::Dpi::target.f5 = SCALE(5); BE::Dpi::target.f6 = SCALE(6);
BE::Dpi::target.f7 = SCALE(7); BE::Dpi::target.f8 = SCALE(8);
BE::Dpi::target.f9 = SCALE(9); BE::Dpi::target.f10 =SCALE(10);
BE::Dpi::target.f12 = SCALE(12); BE::Dpi::target.f13 = SCALE(13);
BE::Dpi::target.f16 = SCALE(16); BE::Dpi::target.f17 = SCALE(17);
BE::Dpi::target.f18 = SCALE(18); BE::Dpi::target.f20 = SCALE(20);
BE::Dpi::target.f22 = SCALE(22); BE::Dpi::target.f32 = SCALE(32);
BE::Dpi::target.f64 = SCALE(64); BE::Dpi::target.f80 = SCALE(80);
config.slider.thickness = SCALE(11);
}
#undef SCALE
extern const QString virtuality_revision();
void
Style::init()
{
QTime time; time.start();
// various workarounds... ==========================
appType = Unknown;
QString appName;
if (!qApp->inherits("KApplication") && getenv("GTK_QT_ENGINE_ACTIVE"))
{ appType = GTK; /*qWarning("BESPIN: Detected GKT+ application");*/ }
else if (qApp->inherits("GreeterApp"))
appType = KDM;
else
{
appName = QCoreApplication::applicationName();
if (appName.isEmpty() && !QCoreApplication::arguments().isEmpty())
appName = QCoreApplication::arguments().at(0).section('/', -1);
if (appName == "dolphin")
appType = Dolphin;
else if (appName == "konversation")
appType = Konversation;
else if (appName == "be.shell")
appType = BEshell;
else if (appName == "plasma" || appName.startsWith("plasma-") || appName == "krunner")
appType = Plasma;
else if (appName == "qbittorrent")
appType = QBittorrent;
else if (appName == "Designer" || appName == "designer")
appType = QtDesigner;
else if (appName == "kdevelop")
appType = KDevelop;
else if (appName == "kwin")
appType = KWin;
else if (appName == "amarok")
appType = Amarok;
else if ( appName == "gwenview" )
appType = Gwenview;
else if (appName == "OpenOffice.org" || appName == "soffice.bin")
appType = OpenOffice;
else if (appName == "vlc")
appType = VLC;
else if (appName == "kmail")
appType = KMail;
else if (appName == "arora")
appType = Arora;
else if ( appName == "konqueror")
appType = Konqueror;
else if ( appName == "Kde4ToolkitLibrary" )
{
appName = "opera";
appType = Opera;
}
}
// ==========================
readSettings(appName);
if (objectName() == "sienar") {
config.invert.docks = true;
config.invert.menubars = true;
config.invert.menus = true;
config.invert.modals = true;
config.invert.titlebars = true;
config.invert.toolbars = true;
config.bg.opacity = 0xff;
config.bg.modal.opacity = 250;
config.bg.blur = false;
config.frame.roundness = 6;
config.bg.ringOverlay = 2;
} else if (objectName() == "flynn") {
config.invert.docks = false;
config.invert.menubars = false;
config.invert.menus = false;
config.invert.modals = false;
config.invert.titlebars = false;
config.invert.toolbars = false;
config.bg.ringOverlay = 3;
// config.bg.opacity = readInt(BG_OPACITY);
// config.bg.modal.opacity = readInt(BG_MODAL_OPACITY);
// config.bg.blur = false;
// config.frame.roundness = SCALE(readInt(ROUNDNESS));
} else if (objectName() == "virtualbreeze") {
config.invert.docks = false;
config.invert.menubars = false;
config.invert.menus = true;
config.invert.modals = true;
config.invert.titlebars = false;
config.invert.toolbars = false;
config.bg.opacity = 0xff;
config.bg.modal.opacity = 250;
config.bg.blur = false;
// config.btn.tool.disabledStyle = readInt(BTN_DISABLED_TOOLS);
config.frame.roundness = 2;
config.bg.ringOverlay = 4;
}
initMetrics();
setProperty("VirtualityStyleRevision", virtuality_revision());
}