choose.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # Copyright © 2024 Noah Vogt <noah@noahvogt.com>
  2. # This program is free software: you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation, either version 3 of the License, or
  5. # (at your option) any later version.
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. # You should have received a copy of the GNU General Public License
  11. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. from PyQt5.QtWidgets import ( # pylint: disable=no-name-in-module
  13. QDialog,
  14. QVBoxLayout,
  15. QRadioButton,
  16. QPushButton,
  17. QMessageBox,
  18. QButtonGroup,
  19. QScrollArea,
  20. QWidget,
  21. )
  22. class RadioButtonDialog(QDialog): # pylint: disable=too-few-public-methods
  23. def __init__(self, options: list[str], window_title: str):
  24. super().__init__()
  25. self.setWindowTitle(window_title)
  26. self.master_layout = QVBoxLayout(self)
  27. scroll_area_layout = self.get_scroll_area_layout()
  28. self.radio_button_group = QButtonGroup(self)
  29. self.chosen = ""
  30. for num, item in enumerate(options):
  31. radio_button = QRadioButton(item)
  32. if num == 0:
  33. radio_button.setChecked(True)
  34. self.radio_button_group.addButton(radio_button)
  35. scroll_area_layout.addWidget(radio_button)
  36. ok_button = QPushButton("OK")
  37. ok_button.clicked.connect(self.accept)
  38. self.master_layout.addWidget(ok_button)
  39. def get_scroll_area_layout(self):
  40. scroll_area = QScrollArea()
  41. scroll_area.setWidgetResizable(True)
  42. self.master_layout.addWidget(scroll_area)
  43. scroll_content = QWidget()
  44. scroll_area.setWidget(scroll_content)
  45. scroll_area_layout = QVBoxLayout(scroll_content)
  46. return scroll_area_layout
  47. def accept(self):
  48. selected_button = self.radio_button_group.checkedButton()
  49. if selected_button:
  50. self.chosen = selected_button.text()
  51. super().accept()
  52. else:
  53. QMessageBox.warning(
  54. self,
  55. "No Selection",
  56. "Please select an option before proceeding.",
  57. )