1
0
mirror of https://github.com/arsenetar/dupeguru.git synced 2024-10-31 22:05:58 +00:00
dupeguru/qt/radio_box.py

89 lines
2.8 KiB
Python
Raw Normal View History

2019-09-10 00:54:28 +00:00
# Created On: 2010-06-02
# Copyright 2015 Hardcoded Software (http://www.hardcoded.net)
#
# This software is licensed under the "GPLv3" License as described in the "LICENSE" file,
# which should be included with this package. The terms are also available at
2019-09-10 00:54:28 +00:00
# http://www.gnu.org/licenses/gpl-3.0.html
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import QWidget, QHBoxLayout, QRadioButton
2022-05-09 00:22:08 +00:00
from qt.util import horizontal_spacer
2019-09-10 00:54:28 +00:00
2019-09-10 00:54:28 +00:00
class RadioBox(QWidget):
def __init__(self, parent=None, items=None, spread=True, **kwargs):
# If spread is False, insert a spacer in the layout so that the items don't use all the
# space they're given but rather align left.
if items is None:
items = []
super().__init__(parent, **kwargs)
self._buttons = []
self._labels = items
self._selected_index = 0
2021-08-24 05:12:23 +00:00
self._spacer = horizontal_spacer() if not spread else None
2019-09-10 00:54:28 +00:00
self._layout = QHBoxLayout(self)
self._update_buttons()
# --- Private
2019-09-10 00:54:28 +00:00
def _update_buttons(self):
if self._spacer is not None:
self._layout.removeItem(self._spacer)
to_remove = self._buttons[len(self._labels) :]
2019-09-10 00:54:28 +00:00
for button in to_remove:
self._layout.removeWidget(button)
button.setParent(None)
del self._buttons[len(self._labels) :]
to_add = self._labels[len(self._buttons) :]
2019-09-10 00:54:28 +00:00
for _ in to_add:
button = QRadioButton(self)
self._buttons.append(button)
self._layout.addWidget(button)
button.toggled.connect(self.buttonToggled)
if self._spacer is not None:
self._layout.addItem(self._spacer)
if not self._buttons:
return
for button, label in zip(self._buttons, self._labels):
button.setText(label)
self._update_selection()
2019-09-10 00:54:28 +00:00
def _update_selection(self):
self._selected_index = max(0, min(self._selected_index, len(self._buttons) - 1))
2019-09-10 00:54:28 +00:00
selected = self._buttons[self._selected_index]
selected.setChecked(True)
# --- Event Handlers
2019-09-10 00:54:28 +00:00
def buttonToggled(self):
for i, button in enumerate(self._buttons):
if button.isChecked():
self._selected_index = i
self.itemSelected.emit(i)
break
# --- Signals
2019-09-10 00:54:28 +00:00
itemSelected = pyqtSignal(int)
# --- Properties
2019-09-10 00:54:28 +00:00
@property
def buttons(self):
return self._buttons[:]
2019-09-10 00:54:28 +00:00
@property
def items(self):
return self._labels[:]
2019-09-10 00:54:28 +00:00
@items.setter
def items(self, value):
self._labels = value
self._update_buttons()
2019-09-10 00:54:28 +00:00
@property
def selected_index(self):
return self._selected_index
2019-09-10 00:54:28 +00:00
@selected_index.setter
def selected_index(self, value):
self._selected_index = value
self._update_selection()