From a808a5b1bd7233fca597b176d3c9982f5fe5b9ea Mon Sep 17 00:00:00 2001 From: Louis-Joseph Fournier Date: Sat, 2 Jan 2016 15:20:19 +0100 Subject: [PATCH] Toise: generic object to center current mark --- qml/ScaleToise.qml | 87 ++++++--------------------------------------- qml/Toise.qml | 86 ++++++++++++++++++++++++++++++++++++++++++++ qml/TunerScreen.qml | 25 ++++++++----- qml/desktop.qrc | 1 + qml/sailfish.qrc | 1 + 5 files changed, 115 insertions(+), 85 deletions(-) create mode 100644 qml/Toise.qml diff --git a/qml/ScaleToise.qml b/qml/ScaleToise.qml index fab7327..a13e10e 100644 --- a/qml/ScaleToise.qml +++ b/qml/ScaleToise.qml @@ -6,100 +6,35 @@ import QtQuick 2.0 * Display notes from scale, with current note centered. */ -Item { +Toise { id: scale - property QtObject theme - // note or note + 12 * octave property int note: 1 + // en or fr + property int notes_style: 0 property variant notes_fr: [ "do", "do#", "ré", "mib", "mi", "fa", "fa#", "sol", "sol#", "la", "sib", "si"] property variant notes_en: [ "C", "C#", "D", "Eb", "E", "F", "F#", "G", "G#", "A", "Bb", "B"] property variant notes: [notes_en, notes_fr] - property int notes_style: 0 + // case colors property color colorAltered: "#40888888" property color colorNatural: "transparent" - property int h_margin: Math.max(height / 8, main_note.border_d) + // Toise parameters + index: note + marks: notes[notes_style] + nb_marks_displayed: Math.min(nb_marks, width / theme.fontSizeLarge * 0.8) - property int nb_notes: 12 - property int nb_notes_displayed: Math.min(nb_notes, width / theme.fontSizeLarge * 0.8) - - /// current note is on the middle - property double position: note - (nb_notes_displayed - 1) / 2 - - property int first_note: Math.floor(position) % nb_notes - property double delta: position - Math.floor(position) - - onFirst_noteChanged: console.log(first_note + " " + note + " " + nb_notes_displayed + " " + position) - - function note_name(i) { - return notes[notes_style][i]; + mark_color: function(note) { + if (isAltered(note)) return colorAltered; + else return colorNatural; } function isAltered(i) { return (i < 4 && (i & 1)) || (i > 5 && !(i & 1)) } - - Behavior on position { - NumberAnimation { - duration: 200 - easing.amplitude: nb_notes - } - } - - Row { - id: toise - anchors.top: parent.top - anchors.left: parent.left - anchors.leftMargin: - cellWidth * delta - anchors.bottom: parent.bottom - anchors.right: parent.right - anchors.topMargin: h_margin - anchors.bottomMargin: h_margin - property double cellWidth: parent.width / nb_notes_displayed - - Repeater { - model: nb_notes_displayed + 1 - Rectangle { - width: toise.cellWidth - height: toise.height - border.width: 1 - property int note: (index + nb_notes + first_note) % nb_notes - color: isAltered(note) ? colorAltered : colorNatural - - Text { - anchors.horizontalCenter: parent.horizontalCenter - anchors.verticalCenter: parent.verticalCenter - text: note_name(note) - color: theme.primaryColor - font.pixelSize: parent.height / 2 - } - } - } - } - - // Screen for actual note - Rectangle { - id: main_note - y: 0 - x: toise.cellWidth * (parent.nb_notes_displayed - 1) / 2 - border_d - width: toise.cellWidth + border_d * 2 - height: parent.height - - property int border_d: 10 - - color: "transparent" - border.width: border_d - border.color: "#a0777777" - } - -/* - onPositionChanged: { - console.log("note " + note_name(note % nb_notes) + " pos " + position + " first note " + first_note + " delta " + delta) - }*/ } diff --git a/qml/Toise.qml b/qml/Toise.qml new file mode 100644 index 0000000..160489c --- /dev/null +++ b/qml/Toise.qml @@ -0,0 +1,86 @@ +import QtQuick 2.0 + +/** + * Toise + * + * Display marks with current centered + */ + +Item { + id: scale + + property QtObject theme + + // mark index + property int index: 1 + // array of marks + property variant marks: [] + // function to get mark color + property var mark_color: function (id) { return "transparent" } + + property int h_margin: Math.max(height / 8, main_mark.border_d) + + property int nb_marks: marks.length + + property int nb_marks_displayed: nb_marks + + /// current mark is on the middle + property double position: index - (nb_marks_displayed - 1) / 2 + + property int first_mark: Math.floor(position) % nb_marks + property double delta: position - Math.floor(position) + + Behavior on position { + NumberAnimation { + duration: 200 + easing.amplitude: nb_marks + } + } + + Row { + id: toise + anchors.top: parent.top + anchors.left: parent.left + anchors.leftMargin: - cellWidth * delta + anchors.bottom: parent.bottom + anchors.right: parent.right + anchors.topMargin: h_margin + anchors.bottomMargin: h_margin + + property double cellWidth: parent.width / nb_marks_displayed + + Repeater { + model: nb_marks_displayed + 1 + Rectangle { + width: toise.cellWidth + height: toise.height + border.width: 1 + property int idx: (index + nb_marks + first_mark) % nb_marks + color: mark_color(idx) + + Text { + anchors.horizontalCenter: parent.horizontalCenter + anchors.verticalCenter: parent.verticalCenter + text: marks[idx] + color: theme.primaryColor + font.pixelSize: parent.height / 2 + } + } + } + } + + // Screen for actual mark + Rectangle { + id: main_mark + y: 0 + x: toise.cellWidth * (parent.nb_marks_displayed - 1) / 2 - border_d + width: toise.cellWidth + border_d * 2 + height: parent.height + + property int border_d: 10 + + color: "transparent" + border.width: border_d + border.color: "#a0777777" + } +} diff --git a/qml/TunerScreen.qml b/qml/TunerScreen.qml index 6878a5a..4d1eb35 100644 --- a/qml/TunerScreen.qml +++ b/qml/TunerScreen.qml @@ -6,6 +6,7 @@ import QtQuick 2.0 */ Item { + id: main /// tuner object property QtObject tuner /// theme corresponding to Silica Theme object @@ -28,17 +29,23 @@ Item { level: tuner.found ? tuner.deviation * 100 : -50 } - ScaleToise { - id: toise - theme: parent.theme - + Item { anchors.top: meter.bottom - anchors.topMargin: h_margin * 2 - anchors.horizontalCenter: parent.horizontalCenter + anchors.left: parent.left + anchors.right: parent.right + anchors.bottom: parent.bottom - width: meter.width - height: Math.max(width / 10, theme.fontSizeLarge * 1.8) + ScaleToise { + id: toise + theme: main.theme - note: tuner.note + toise.nb_notes * tuner.octave + anchors.horizontalCenter: parent.horizontalCenter + anchors.verticalCenter: parent.verticalCenter + + width: meter.width + height: Math.max(width / 10, theme.fontSizeLarge * 1.8) + + note: tuner.note + toise.nb_notes * tuner.octave + } } } diff --git a/qml/desktop.qrc b/qml/desktop.qrc index 0dd5d9e..47729ec 100644 --- a/qml/desktop.qrc +++ b/qml/desktop.qrc @@ -6,5 +6,6 @@ TunerScreen.qml CircleMeter.qml ScaleToise.qml +Toise.qml diff --git a/qml/sailfish.qrc b/qml/sailfish.qrc index 199445d..522404c 100644 --- a/qml/sailfish.qrc +++ b/qml/sailfish.qrc @@ -5,5 +5,6 @@ TunerScreen.qml CircleMeter.qml ScaleToise.qml +Toise.qml