|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- // tonalisa - software to look at overtone-structures
- // Copyright (C) 2016 Dominik Schmidt-Philipp
- //
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this program. If not, see <http://www.gnu.org/licenses/>.
- //
- import QtQuick 2.3
- import QtQuick.Controls 1.2
- import QtQuick.LocalStorage 2.0
-
- Item {
- id: root
- width: parent.width; height:parent.height
-
- property var fr: []
- property var am: []
-
- property alias model: spectrumModel
- property alias list: spectrumList
- property var db;
-
- Rectangle {
- anchors.fill: parent
- }
-
- Button {
- id: resetPeaks
- text: "reset"
- x:0; y:0
- onClicked: {
- peakCalculator.resetPeaks()
- }
- }
- Button {
- id: rememberSpectrum
- x:80; y:0
- text: "save spectrum"
- onClicked: {
- var now = new Date();
- var peaks = catalog.peaks;
- var freqs = [];
- var amps = [];
- for (var i in peaks) {
- if(!(i%2)){
- freqs[Math.floor(i/2)] = peaks[i];
- } else {
- amps[Math.floor(i/2)] = peaks[i];
- }
- }
-
- spectrumList.model.append({
- "name":"random-"+Qt.formatDateTime(now,"yyMMdd HH:mm"),
- "baseFreq":peaks[0],
- "mode":"frequencies",
- "freq":JSON.stringify(freqs),
- "amp" :JSON.stringify(amps)
- });
- spectrumList.currentIndex = spectrumModel.count-1
- }
- }
- Button {
- id: deleteSpectrum
- text: "delete"
- anchors.right:parent.right;
- onClicked: {
- spectrumList.model.remove(spectrumList.currentIndex)
- }
- }
- Item {
- id:entryEdit
- x:2;y:30
- width:parent.width/2-4
- height:parent.height-40
- Rectangle {
- anchors.fill:parent
- color:"#ffffff"
- }
- TextField {
- id:sEname
- width:parent.width-5
- }
- }
-
-
- function initDatabase() {
- db = LocalStorage.openDatabaseSync(
- "ExtendedFrequencyToolDB",
- "0.2",
- "Scales and Spectra",
- 100000)
- db.transaction( function(tx) {
- console.log("... create 'spectra' table")
- tx.executeSql(
- 'CREATE TABLE IF NOT EXISTS spectra('
- +'id INTEGER,'
- +'name TEXT,'
- +'baseFreq REAL,'
- +'mode TEXT,'
- +'freq TEXT,'
- +'amp TEXT)'
- )
- });
- }
-
- function storeData() {
- if(!db) { return; }
- db.transaction( function(tx) {
- for (var i=0;i<spectrumModel.count;i++) {
- var result = tx.executeSql('SELECT * FROM spectra WHERE id = "'+i+'"');
- var item = spectrumModel.get(i)
- if(result.rows.length === 1) { // use update
- console.log("update")
- result = tx.executeSql('UPDATE spectra SET name=?, baseFreq=?, mode=?, freq=?, amp=? WHERE id="'+i+'"',[
- item.name,
- item.baseFreq,
- item.mode,
- item.freq,
- item.amp
- ]);
- } else { // use insert
- console.log("insert")
- result = tx.executeSql('INSERT INTO spectra VALUES (?,?,?,?,?,?)',[
- i,
- item.name,
- item.baseFreq,
- item.mode,
- item.freq,
- item.amp
- ])
- }
- }
- });
- }
-
- function readData() {
- if(!db){return;}
- db.transaction( function(tx){
- console.log("... read data from 'spectra' table")
- var result = tx.executeSql('SELECT * FROM spectra');
- if ( result.rows.length > 0 ) {
- for ( var i=0;i<result.rows.length;i++ ) {
- spectrumList.model.append(result.rows.item(i))
- }
- } else {
- console.log("no spectra in database")
- }
- });
- }
- Component.onCompleted: {
- initDatabase();
- readData();
- }
- Component.onDestruction: {
- storeData()
- }
-
- ListModel {
- id:spectrumModel
- }
-
- ListView {
- id:spectrumList
- anchors.fill:parent
- anchors.margins: 2
- anchors.leftMargin:parent.width/2+2
- anchors.topMargin:20
- clip:true
- model:spectrumModel
- delegate:spectrumDelegate
- //highlight:spectrumHighlight
- spacing:1
- focus:true
- onCurrentItemChanged: {
- var item = spectrumModel.get(currentIndex);
- var f = JSON.parse(item.freq)
- var a = JSON.parse(item.amp)
- switch (item.mode) {
- case "ratios":
- var realFreqs = [];
- for (var i=0; i<f.length;i++){
- realFreqs[i] = item.baseFreq * f[i];
- }
- break;
- case "frequencies":
- default:
- var realFreqs = f;
- }
-
- buffer.freq = realFreqs
- buffer.amp = a
- //root.fr = realFreqs
- //root.am = a
-
- sEname.text = item.name
- }
- }
- Component {
- id: spectrumDelegate
- Box {
- text:name
- }
- }
- }
|