choose.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. QCalendarWidget,
  22. QFileDialog,
  23. )
  24. from PyQt5.QtCore import QDate # pylint: disable=no-name-in-module
  25. class RadioButtonDialog(QDialog): # pylint: disable=too-few-public-methods
  26. def __init__(self, options: list[str], window_title: str):
  27. super().__init__()
  28. self.setWindowTitle(window_title)
  29. self.master_layout = QVBoxLayout(self)
  30. scroll_area_layout = self.get_scroll_area_layout()
  31. self.radio_button_group = QButtonGroup(self)
  32. self.chosen = ""
  33. for num, item in enumerate(options):
  34. radio_button = QRadioButton(item)
  35. if num == 0:
  36. radio_button.setChecked(True)
  37. self.radio_button_group.addButton(radio_button)
  38. scroll_area_layout.addWidget(radio_button)
  39. ok_button = QPushButton("OK")
  40. ok_button.clicked.connect(self.accept)
  41. self.master_layout.addWidget(ok_button)
  42. def get_scroll_area_layout(self):
  43. scroll_area = QScrollArea()
  44. scroll_area.setWidgetResizable(True)
  45. self.master_layout.addWidget(scroll_area)
  46. scroll_content = QWidget()
  47. scroll_area.setWidget(scroll_content)
  48. scroll_area_layout = QVBoxLayout(scroll_content)
  49. return scroll_area_layout
  50. def accept(self):
  51. selected_button = self.radio_button_group.checkedButton()
  52. if selected_button:
  53. self.chosen = selected_button.text()
  54. super().accept()
  55. else:
  56. QMessageBox.warning(
  57. self,
  58. "No Selection",
  59. "Please select an option before proceeding.",
  60. )
  61. class DatePickerDialog(QDialog): # pylint: disable=too-few-public-methods
  62. def __init__(self, parent=None):
  63. super().__init__(parent)
  64. self.setWindowTitle("Select a Date")
  65. self.setMinimumWidth(300)
  66. # Layout
  67. layout = QVBoxLayout()
  68. # Calendar widget
  69. self.calendar = QCalendarWidget(self)
  70. self.calendar.setGridVisible(True)
  71. self.calendar.setSelectedDate(QDate.currentDate())
  72. layout.addWidget(self.calendar)
  73. # OK button
  74. self.ok_button = QPushButton("OK", self)
  75. self.ok_button.clicked.connect(self.accept)
  76. layout.addWidget(self.ok_button)
  77. self.setLayout(layout)
  78. def selected_date(self):
  79. return self.calendar.selectedDate()
  80. def get_mp3_file_via_picker_dialog(parent=None) -> str | None:
  81. dialog = QFileDialog(parent)
  82. dialog.setFileMode(QFileDialog.ExistingFile)
  83. dialog.setNameFilter("MP3 Files (*.mp3)")
  84. if dialog.exec_():
  85. selected_files = dialog.selectedFiles()
  86. # Return the first selected file
  87. return selected_files[0]
  88. return None