You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

214 satır
4.7KB

  1. // tonalisa - software to look at overtone-structures
  2. // Copyright (C) 2016 Dominik Schmidt-Philipp
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. //
  17. import QtQuick 2.3
  18. import QtQuick.Controls 1.2
  19. import QtQuick.LocalStorage 2.0
  20. Item {
  21. id: root
  22. width: parent.width; height:parent.height
  23. property var fr: []
  24. property var am: []
  25. property alias model: spectrumModel
  26. property alias list: spectrumList
  27. property var db;
  28. Rectangle {
  29. anchors.fill: parent
  30. }
  31. Button {
  32. id: resetPeaks
  33. text: "reset"
  34. x:0; y:0
  35. onClicked: {
  36. peakCalculator.resetPeaks()
  37. }
  38. }
  39. Button {
  40. id: rememberSpectrum
  41. x:80; y:0
  42. text: "save spectrum"
  43. onClicked: {
  44. var now = new Date();
  45. var peaks = catalog.peaks;
  46. var freqs = [];
  47. var amps = [];
  48. for (var i in peaks) {
  49. if(!(i%2)){
  50. freqs[Math.floor(i/2)] = peaks[i];
  51. } else {
  52. amps[Math.floor(i/2)] = peaks[i];
  53. }
  54. }
  55. spectrumList.model.append({
  56. "name":"random-"+Qt.formatDateTime(now,"yyMMdd HH:mm"),
  57. "baseFreq":peaks[0],
  58. "mode":"frequencies",
  59. "freq":JSON.stringify(freqs),
  60. "amp" :JSON.stringify(amps)
  61. });
  62. spectrumList.currentIndex = spectrumModel.count-1
  63. }
  64. }
  65. Button {
  66. id: deleteSpectrum
  67. text: "delete"
  68. anchors.right:parent.right;
  69. onClicked: {
  70. spectrumList.model.remove(spectrumList.currentIndex)
  71. }
  72. }
  73. Item {
  74. id:entryEdit
  75. x:2;y:30
  76. width:parent.width/2-4
  77. height:parent.height-40
  78. Rectangle {
  79. anchors.fill:parent
  80. color:"#ffffff"
  81. }
  82. TextField {
  83. id:sEname
  84. width:parent.width-5
  85. }
  86. }
  87. function initDatabase() {
  88. db = LocalStorage.openDatabaseSync(
  89. "ExtendedFrequencyToolDB",
  90. "0.2",
  91. "Scales and Spectra",
  92. 100000)
  93. db.transaction( function(tx) {
  94. console.log("... create 'spectra' table")
  95. tx.executeSql(
  96. 'CREATE TABLE IF NOT EXISTS spectra('
  97. +'id INTEGER,'
  98. +'name TEXT,'
  99. +'baseFreq REAL,'
  100. +'mode TEXT,'
  101. +'freq TEXT,'
  102. +'amp TEXT)'
  103. )
  104. });
  105. }
  106. function storeData() {
  107. if(!db) { return; }
  108. db.transaction( function(tx) {
  109. for (var i=0;i<spectrumModel.count;i++) {
  110. var result = tx.executeSql('SELECT * FROM spectra WHERE id = "'+i+'"');
  111. var item = spectrumModel.get(i)
  112. if(result.rows.length === 1) { // use update
  113. console.log("update")
  114. result = tx.executeSql('UPDATE spectra SET name=?, baseFreq=?, mode=?, freq=?, amp=? WHERE id="'+i+'"',[
  115. item.name,
  116. item.baseFreq,
  117. item.mode,
  118. item.freq,
  119. item.amp
  120. ]);
  121. } else { // use insert
  122. console.log("insert")
  123. result = tx.executeSql('INSERT INTO spectra VALUES (?,?,?,?,?,?)',[
  124. i,
  125. item.name,
  126. item.baseFreq,
  127. item.mode,
  128. item.freq,
  129. item.amp
  130. ])
  131. }
  132. }
  133. });
  134. }
  135. function readData() {
  136. if(!db){return;}
  137. db.transaction( function(tx){
  138. console.log("... read data from 'spectra' table")
  139. var result = tx.executeSql('SELECT * FROM spectra');
  140. if ( result.rows.length > 0 ) {
  141. for ( var i=0;i<result.rows.length;i++ ) {
  142. spectrumList.model.append(result.rows.item(i))
  143. }
  144. } else {
  145. console.log("no spectra in database")
  146. }
  147. });
  148. }
  149. Component.onCompleted: {
  150. initDatabase();
  151. readData();
  152. }
  153. Component.onDestruction: {
  154. storeData()
  155. }
  156. ListModel {
  157. id:spectrumModel
  158. }
  159. ListView {
  160. id:spectrumList
  161. anchors.fill:parent
  162. anchors.margins: 2
  163. anchors.leftMargin:parent.width/2+2
  164. anchors.topMargin:20
  165. clip:true
  166. model:spectrumModel
  167. delegate:spectrumDelegate
  168. //highlight:spectrumHighlight
  169. spacing:1
  170. focus:true
  171. onCurrentItemChanged: {
  172. var item = spectrumModel.get(currentIndex);
  173. var f = JSON.parse(item.freq)
  174. var a = JSON.parse(item.amp)
  175. switch (item.mode) {
  176. case "ratios":
  177. var realFreqs = [];
  178. for (var i=0; i<f.length;i++){
  179. realFreqs[i] = item.baseFreq * f[i];
  180. }
  181. break;
  182. case "frequencies":
  183. default:
  184. var realFreqs = f;
  185. }
  186. buffer.freq = realFreqs
  187. buffer.amp = a
  188. //root.fr = realFreqs
  189. //root.am = a
  190. sEname.text = item.name
  191. }
  192. }
  193. Component {
  194. id: spectrumDelegate
  195. Box {
  196. text:name
  197. }
  198. }
  199. }