20 thisButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
25 const std::string filename = buttonName +
"_" + std::to_string(state) +
".svg";
27 QFile file(QString::fromStdString(filename));
28 if (!file.open(QIODevice::ReadOnly)) {
32 QString svg = QString::fromUtf8(file.readAll());
33 const QColor foreground = state == 2
34 ? palette.color(QPalette::HighlightedText)
35 : palette.color(QPalette::WindowText);
36 const QColor selectedBackground = palette.color(QPalette::Highlight);
38 svg.replace(
"width=\"48\" height=\"48\"",
39 QString(
"width=\"%1\" height=\"%2\"").arg(iconSize.width()).arg(iconSize.height()));
40 svg.replace(
"currentColor", foreground.name(QColor::HexRgb));
41 svg.replace(
"#aaddff", selectedBackground.name(QColor::HexRgb), Qt::CaseInsensitive);
44 if (!pixmap.loadFromData(svg.toUtf8(),
"SVG")) {
59 const std::vector<std::string>& bicons,
60 bool vertical, QWidget* parent)
62 constexpr int distanceToMargin = 12;
65 for (
const auto& b : bicons) {
72 buttonsWidget->setIconSize(QSize(
static_cast<int>(h),
static_cast<int>(v)));
76 "background-color: transparent; "
77 "QListWidget::item:selected { background: transparent; border: none; }");
80 for (
auto& b : buttons) {
87 this, &GQTButtonsWidget::buttonWasPressed);
90 QBoxLayout* layout = vertical
91 ?
static_cast<QBoxLayout*
>(
new QVBoxLayout(
this))
92 :
static_cast<QBoxLayout*
>(
new QHBoxLayout(
this));
93 layout->setContentsMargins(0, 0, 0, 0);
98 double hsize = (h + distanceToMargin) * (buttons.size());
99 double vsize = v + distanceToMargin;
101 hsize = h + distanceToMargin;
102 vsize = (v + distanceToMargin) * (buttons.size());
104 buttonsWidget->setFixedSize(
static_cast<int>(hsize),
static_cast<int>(vsize));
109 "QListWidget { background-color: transparent; }"
110 "QListWidget::item { background: transparent; border: none; }"
111 "QListWidget::item:selected { background: transparent; border: none; outline: none; }"
123void GQTButtonsWidget::buttonWasPressed(QListWidgetItem* item) {
134void GQTButtonsWidget::refresh_icons() {
142 const int state = row == currentRow ? 2 : 1;
143 buttonsWidget->item(row)->setIcon(buttons[row]->iconForState(state, iconSize, palette));
148 QWidget::changeEvent(
event);
150 if (
event->type() == QEvent::PaletteChange ||
151 event->type() == QEvent::ApplicationPaletteChange ||
152 event->type() == QEvent::StyleChange) {
158 QWidget::showEvent(
event);
171 const std::vector<std::string>& titles,
172 bool vertical, QWidget* parent)
174 buttonPressedIndex(-1) {
175 QBoxLayout* layout = vertical
176 ?
static_cast<QBoxLayout*
>(
new QVBoxLayout(
this))
177 :
static_cast<QBoxLayout*
>(
new QHBoxLayout(
this));
180 QStringList buttonStrings;
181 for (
const auto& title : titles) {
182 buttonStrings.append(QString::fromStdString(title));
186 for (
int i = 0; i < buttonStrings.size(); ++i) {
187 QPushButton* button =
new QPushButton(buttonStrings[i],
this);
188 button->setCheckable(
true);
189 button->setFixedSize(buttonWidth, buttonHeight);
192 button->setStyleSheet(
193 "QPushButton { border: none; background-color: transparent; padding: 0; margin: 0; }"
194 "QPushButton:checked { background-color: transparent; }"
197 layout->addWidget(button);
198 buttons.push_back(button);
201 connect(button, &QPushButton::clicked,
this, [
this, i]() {
202 this->setButtonPressed(i);
204 connect(button, &QPushButton::toggled,
this, [
this]() {
213 if (buttonPressedIndex >= 0 && buttonPressedIndex < buttons.size())
214 return buttons[buttonPressedIndex]->isChecked();
220 if (index >= 0 && index < buttons.size())
221 buttons[index]->toggle();
226 if (index >= 0 && index < buttons.size())
227 return buttons[index]->isChecked();
231void GQTToggleButtonWidget::setButtonPressed(
int index) {
233 buttonPressedIndex = index;
239 for (
auto& b : buttons) {
240 b->setChecked(
false);
245 if (index < 0 || index >=
static_cast<int>(buttons.size())) {
return; }
248 while (svgIcons.size() <= index) { svgIcons.append(SvgIcon{}); }
250 const QSize sz = iconSize.isValid() ? iconSize
251 : QSize(buttons[index]->width() - 6, buttons[index]->height() - 6);
252 svgIcons[index] = SvgIcon{ svgResourcePath, sz };
256void GQTToggleButtonWidget::refresh_svg_icons() {
257 const QColor windowText = palette().color(QPalette::WindowText);
258 const QColor hlText = palette().color(QPalette::HighlightedText);
259 const QColor hlBg = palette().color(QPalette::Highlight);
261 for (
int i = 0; i < svgIcons.size(); ++i) {
262 const SvgIcon& entry = svgIcons[i];
263 if (entry.path.isEmpty() || i >=
static_cast<int>(buttons.size())) {
continue; }
266 if (!f.open(QIODevice::ReadOnly)) {
continue; }
268 const bool checked = buttons[i]->isChecked();
269 const QColor fg = checked ? hlText : windowText;
270 const QString bg = checked ? hlBg.name(QColor::HexRgb) :
"none";
272 QString svg = QString::fromUtf8(f.readAll());
273 svg.replace(
"width=\"48\" height=\"48\"",
274 QString(
"width=\"%1\" height=\"%2\"").arg(entry.size.width()).arg(entry.size.height()));
275 svg.replace(
"currentColor", fg.name(QColor::HexRgb));
276 svg.replace(
"#aaddff", bg, Qt::CaseInsensitive);
279 if (pix.loadFromData(svg.toUtf8(),
"SVG")) {
280 buttons[i]->setIcon(QIcon(pix));
281 buttons[i]->setIconSize(entry.size);
287 QWidget::changeEvent(
event);
288 if (
event->type() == QEvent::PaletteChange ||
289 event->type() == QEvent::ApplicationPaletteChange ||
290 event->type() == QEvent::StyleChange) {
296 QWidget::showEvent(
event);