Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MultiStyleWidgetWrapper.cpp « widgets « mumble « src - github.com/mumble-voip/mumble.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: be5950df3a187164dce51a3849beca19ebc66289 (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
// Copyright 2020-2021 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.

#include "MultiStyleWidgetWrapper.h"

#include <QFont>
#include <QFontMetrics>
#include <QWidget>

const uint32_t MultiStyleWidgetWrapper::UNSET_FONTSIZE = 0;
const QString MultiStyleWidgetWrapper::UNSET_COLOR     = "";
const QString MultiStyleWidgetWrapper::UNSET_SELECTOR  = "*";

MultiStyleWidgetWrapper::MultiStyleWidgetWrapper(QWidget *widget) : m_widget(widget) {
}

void MultiStyleWidgetWrapper::setFontSize(uint32_t fontSize, bool isPixels) {
	if (!isPixels) {
		// Convert the font size to pixels
		QFont font;
		font.setPixelSize(fontSize);

		fontSize = QFontMetrics(font).height();
	}

	if (fontSize != m_fontSize) {
		m_fontSize = fontSize;

		updateStyleSheet();
	}
}

void MultiStyleWidgetWrapper::setFontSizeSelector(const QString &selector) {
	if (m_fontSizeSelector != selector) {
		m_fontSizeSelector = selector;

		updateStyleSheet();
	}
}

void MultiStyleWidgetWrapper::setBackgroundColor(const QString &color) {
	if (m_backgroundColor != color) {
		m_backgroundColor = color;

		updateStyleSheet();
	}
}

void MultiStyleWidgetWrapper::setBackgroundColorSelector(const QString &selector) {
	if (m_backgroundColorSelector != selector) {
		m_backgroundColorSelector = selector;

		updateStyleSheet();
	}
}

void MultiStyleWidgetWrapper::clearFontSize() {
	setFontSize(UNSET_FONTSIZE);
}

void MultiStyleWidgetWrapper::clearFontSizeSelector() {
	setFontSizeSelector(UNSET_SELECTOR);
}

void MultiStyleWidgetWrapper::clearBackgroundColor() {
	setBackgroundColor(UNSET_COLOR);
}

void MultiStyleWidgetWrapper::clearBackgroundColorSelector() {
	setBackgroundColorSelector(UNSET_SELECTOR);
}

void MultiStyleWidgetWrapper::updateStyleSheet() {
	QString styleSheet;

	if (m_fontSize != UNSET_FONTSIZE) {
		styleSheet += QString(" %1 { font-size: %2px; }").arg(m_fontSizeSelector).arg(m_fontSize);
	}
	if (m_backgroundColor != UNSET_COLOR) {
		styleSheet += QString(" %1 { background-color: %2; }").arg(m_backgroundColorSelector).arg(m_backgroundColor);
	}

	m_widget->setStyleSheet(styleSheet);
}

QWidget *MultiStyleWidgetWrapper::operator->() {
	return m_widget;
}