PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/main.qml

https://gitlab.com/asmw/quadsh-mid-sauzee
QML | 224 lines | 192 code | 32 blank | 0 comment | 13 complexity | 6e6d971c43404d801cb4ce7629c7e995 MD5 | raw file
  1. import QtQuick 2.7
  2. import QtQuick.Controls 2.0
  3. import QtQuick.Layouts 1.0
  4. import QtQuick.Dialogs 1.2
  5. import QtMultimedia 5.6
  6. import Qt.labs.settings 1.0
  7. ApplicationWindow {
  8. id: root
  9. visible: true
  10. width: 640
  11. height: 480
  12. title: qsTr("Hello World")
  13. property var referenceSounds: {
  14. 0: 'qrc:///sounds/badumtss.wav',
  15. 1: 'qrc:///sounds/sad_trombone.wav',
  16. 2: 'qrc:///sounds/burp1.wav',
  17. 3: 'qrc:///sounds/burp2.wav',
  18. 4: 'qrc:///sounds/fart1.wav',
  19. 5: 'qrc:///sounds/fart2.wav',
  20. 6: 'qrc:///sounds/crickets.wav',
  21. 7: 'qrc:///sounds/whip.wav',
  22. }
  23. Settings {
  24. id: settings
  25. property int version: 110
  26. property alias volume: settingsPage.volume
  27. property string sounds: JSON.stringify(referenceSounds)
  28. property int rows: 4
  29. property int columns: 2
  30. property bool restartOnClick: false
  31. }
  32. signal updateSounds()
  33. property int currentIndex
  34. function selectSound(index) {
  35. currentIndex = index
  36. if(Qt.platform.os === "android") {
  37. fileDialog.provideExistingFileName()
  38. } else {
  39. fileSelector.open();
  40. }
  41. }
  42. Component {
  43. id: androidFileDialogComponent
  44. Connections {
  45. target: fileDialog
  46. onExistingFileNameReady: {
  47. root.sounds[currentIndex] = "file://" + encodeURIComponent(result);
  48. saveSounds();
  49. }
  50. }
  51. }
  52. Loader {
  53. enabled: Qt.platform.os === "android"
  54. sourceComponent: androidFileDialogComponent
  55. }
  56. property var sounds: ({})
  57. property bool initialized: false
  58. Component.onCompleted: {
  59. loadSounds()
  60. initialized = true
  61. }
  62. function loadSounds() {
  63. root.sounds = JSON.parse(settings.sounds);
  64. root.updateSounds()
  65. }
  66. onSoundsChanged: saveSounds()
  67. function saveSounds() {
  68. if(!initialized) return
  69. settings.sounds = JSON.stringify(root.sounds)
  70. root.updateSounds()
  71. }
  72. SwipeView {
  73. id: swipeView
  74. anchors.fill: parent
  75. currentIndex: tabBar.currentIndex
  76. Page {
  77. id: buttonPage
  78. Frame {
  79. anchors.fill: parent
  80. GridLayout {
  81. id: buttonLayout
  82. columns: settings.columns
  83. anchors.fill: parent
  84. Repeater {
  85. model: settings.rows * settings.columns
  86. onModelChanged: root.updateSounds()
  87. Button {
  88. id: button
  89. Layout.fillHeight: true
  90. Layout.fillWidth: true
  91. Layout.maximumWidth: buttonLayout.width/settings.columns
  92. property string name: ""
  93. text: name === "" ? "Sound " + index : name
  94. Rectangle {
  95. anchors.left: parent.left
  96. anchors.top: parent.top
  97. anchors.leftMargin: 5
  98. anchors.topMargin: 10
  99. width: button.height/10
  100. height: width
  101. radius: height
  102. color: buttonSound.playbackState === Audio.PlayingState ? "green" : "transparent"
  103. }
  104. onClicked: {
  105. if(buttonSound.hasAudio) {
  106. if(settings.restartOnClick) {
  107. buttonSound.stop();
  108. buttonSound.play();
  109. } else {
  110. if(buttonSound.playbackState === Audio.PlayingState) {
  111. buttonSound.stop();
  112. } else {
  113. buttonSound.play();
  114. }
  115. }
  116. } else {
  117. root.selectSound(index)
  118. }
  119. }
  120. onPressAndHold: root.selectSound(index)
  121. Connections {
  122. target: root
  123. onUpdateSounds: buttonSound.load()
  124. }
  125. Audio {
  126. id: buttonSound
  127. audioRole: Audio.GameRole
  128. volume: settings.volume
  129. onSourceChanged: {
  130. if(!buttonSound.metaData.title) {
  131. button.name = source.toString().replace(/^.*[\\\/]/, '')
  132. } else {
  133. button.name = buttonSound.metaData.title
  134. }
  135. }
  136. function load() {
  137. source = root.sounds.hasOwnProperty(index) ? root.sounds[index] : ""
  138. }
  139. }
  140. Rectangle {
  141. id: playBar
  142. height: 3
  143. visible: buttonSound.playbackState === Audio.PlayingState
  144. color: "black"
  145. anchors.left: parent.left
  146. anchors.bottom: parent.bottom
  147. width: parent.width
  148. onVisibleChanged: if(visible) {
  149. playAnimation.restart();
  150. } else {
  151. playBar.width = 0
  152. }
  153. NumberAnimation {
  154. id: playAnimation
  155. duration: buttonSound.duration
  156. target: playBar
  157. property: "width"
  158. from: 0
  159. to: button.width
  160. }
  161. }
  162. }
  163. }
  164. }
  165. }
  166. FileDialog {
  167. id: fileSelector
  168. title: qsTr("Select a sound")
  169. onAccepted: {
  170. console.log("accepted", fileUrl.toString())
  171. root.sounds[currentIndex] = fileUrl.toString()
  172. saveSounds()
  173. }
  174. nameFilters: ["Sounds (*.mp3 *.wav)"]
  175. }
  176. }
  177. SettingsPage {
  178. id: settingsPage
  179. settings: settings
  180. }
  181. }
  182. footer: TabBar {
  183. id: tabBar
  184. currentIndex: swipeView.currentIndex
  185. TabButton {
  186. text: qsTr("Sounds")
  187. }
  188. TabButton {
  189. text: qsTr("Settings")
  190. }
  191. }
  192. }