|
- // 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
- }
- }
- }
|