|
From: Tapio V. <aa...@us...> - 2011-01-13 11:09:25
|
Module: editor
Branch: master
Commit: 86798c9cb8ff2bbe617edcf57c21847d4e89fabb
Author: Tapio Vierros <tap...@gm...>
Date: Thu Jan 13 12:45:04 2011 +0200
Some kind of pixel/sec/pitch mappings.
---
notegraphwidget.cc | 33 ++++++++++++++++++++++++---------
notegraphwidget.hh | 10 ++++++++++
notelabel.cc | 24 +++++++++++++++---------
notes.hh | 7 +++++++
4 files changed, 56 insertions(+), 18 deletions(-)
diff --git a/notegraphwidget.cc b/notegraphwidget.cc
index 9b18115..adbd0c8 100644
--- a/notegraphwidget.cc
+++ b/notegraphwidget.cc
@@ -16,10 +16,11 @@ namespace {
}
-const int NoteGraphWidget::noteYStep = 40;
+const int NoteGraphWidget::noteYStep = 28;
NoteGraphWidget::NoteGraphWidget(QWidget *parent)
- : QLabel(parent), m_panHotSpot(), m_selectedNote(), m_selectedAction(NONE), m_actionHappened(), m_pitch("music.raw")
+ : QLabel(parent), m_pixPerSec(200), m_lowestNote(MusicalScale::getBaseId()), m_octaves(3),
+ m_panHotSpot(), m_selectedNote(), m_selectedAction(NONE), m_actionHappened(), m_pitch("music.raw")
{
unsigned width = m_pitch.width(), height = m_pitch.height;
QProgressDialog progress(tr("Rendering pitch data..."), tr("&Abort"), 0, width, this);
@@ -39,7 +40,7 @@ NoteGraphWidget::NoteGraphWidget(QWidget *parent)
progress.setValue(width);
// FIXME: Width should come from song length * pixPerSec
- setFixedSize(std::max(width, (unsigned)1024), height);
+ setFixedSize(std::max(width, (unsigned)1024), noteYStep * 12 * m_octaves);
setFocusPolicy(Qt::StrongFocus);
setWhatsThis(tr("Note graph that displays the song notes and allows you to manipulate them."));
@@ -83,7 +84,7 @@ void NoteGraphWidget::setLyrics(QString lyrics)
QString word;
ts >> word;
if (!word.isEmpty()) {
- m_notes.push_back(new NoteLabel(Note(word.toStdString()), this, QPoint(0, 2 * noteYStep)));
+ m_notes.push_back(new NoteLabel(Note(word.toStdString()), this, QPoint(0, n2px(m_lowestNote + 12 * m_octaves - 6))));
doOperation(opFromNote(*m_notes.back(), m_notes.size()-1), Operation::NO_EXEC);
}
}
@@ -95,11 +96,9 @@ void NoteGraphWidget::setLyrics(const Notes ¬es)
{
clear();
for (Notes::const_iterator it = notes.begin(); it != notes.end(); ++it) {
- // TODO: Implement proper seconds-to-pixels mapping and note height thingy
- const float sec2pix = 200;
if (it->type == Note::NORMAL || it->type == Note::GOLDEN || it->type == Note::FREESTYLE) {
- m_notes.push_back(new NoteLabel(*it, this, QPoint(it->begin*sec2pix, height() - it->note * noteYStep),
- QSize((it->end - it->begin)*sec2pix, 0), false));
+ m_notes.push_back(new NoteLabel(*it, this, QPoint(s2px(it->begin), n2px(it->note)),
+ QSize(s2px(it->length()), 0), false));
doOperation(opFromNote(*m_notes.back(), m_notes.size()-1), Operation::NO_EXEC);
}
}
@@ -252,7 +251,7 @@ void NoteGraphWidget::mouseReleaseEvent(QMouseEvent *event)
if (m_selectedNote) {
m_selectedNote->startResizing(0);
m_selectedNote->startDragging(QPoint());
- m_selectedNote->move(m_selectedNote->pos().x(), int(round(m_selectedNote->pos().y() / float(noteYStep))) * noteYStep);
+ m_selectedNote->move(m_selectedNote->pos().x(), n2px(px2n(m_selectedNote->pos().y())));
if (m_actionHappened) {
// Operation for undo stack & saving
Operation op("SETGEOM");
@@ -404,6 +403,22 @@ void NoteGraphWidget::doOperation(const Operation& op, Operation::OperationFlags
}
+int NoteGraphWidget::s2px(double sec) const {
+ return sec * m_pixPerSec;
+}
+double NoteGraphWidget::px2s(int px) const {
+ return px / m_pixPerSec;
+}
+int NoteGraphWidget::n2px(int note) const {
+ int highestNote = m_lowestNote + 12 * m_octaves;
+ return (highestNote - note) * noteYStep;
+}
+int NoteGraphWidget::px2n(int px) const {
+ int highestNote = m_lowestNote + 12 * m_octaves;
+ return highestNote - round(px / (float)noteYStep);
+}
+
+
void FloatingGap::addNote(NoteLabel* n)
{
diff --git a/notegraphwidget.hh b/notegraphwidget.hh
index 3a45f28..065229d 100644
--- a/notegraphwidget.hh
+++ b/notegraphwidget.hh
@@ -31,6 +31,12 @@ public:
NoteLabels& noteLabels() { return m_notes; }
void doOperation(const Operation& op, Operation::OperationFlags flags = Operation::NORMAL);
+ int s2px(double sec) const;
+ double px2s(int px) const;
+ int n2px(int note) const;
+ int px2n(int px) const;
+
+
signals:
void updateNoteInfo(NoteLabel*);
void operationDone(const Operation&);
@@ -46,6 +52,10 @@ protected:
private:
void finalizeNewLyrics();
+ double m_pixPerSec; ///< Pixels per second
+ int m_lowestNote; ///< Note id / midi pitch of the lowest note in the view
+ int m_octaves; ///< How many octaves are displayed in the view
+
int m_requiredWidth;
QPoint m_panHotSpot;
NoteLabel* m_selectedNote;
diff --git a/notelabel.cc b/notelabel.cc
index 3283ef7..a7a44c7 100644
--- a/notelabel.cc
+++ b/notelabel.cc
@@ -1,14 +1,15 @@
#include <QtGui>
+#include <iostream>
#include "notelabel.hh"
#include "notegraphwidget.hh"
namespace {
- static const int text_margin = 12; // Margin of the label texts
+ static const int text_margin = 3; // Margin of the label texts
}
const int NoteLabel::resize_margin = 5; // How many pixels is the resize area
const int NoteLabel::min_width = 10; // How many pixels is the resize area
-const int NoteLabel::default_size = 50; // The preferred size of notes
+const int NoteLabel::default_size = 100; // The preferred size of notes
NoteLabel::NoteLabel(const Note ¬e, QWidget *parent, const QPoint &position, const QSize &size, bool floating)
: QLabel(parent), m_note(note), m_selected(false), m_floating(floating), m_resizing(0), m_hotspot()
@@ -16,7 +17,7 @@ NoteLabel::NoteLabel(const Note ¬e, QWidget *parent, const QPoint &position,
createPixmap(size);
if (!position.isNull())
move(position);
-
+ std::cout << x() << " " << y() << " " << width() << std::endl;
setMouseTracking(true);
setMinimumSize(min_width, 10);
setAttribute(Qt::WA_DeleteOnClose);
@@ -25,21 +26,20 @@ NoteLabel::NoteLabel(const Note ¬e, QWidget *parent, const QPoint &position,
void NoteLabel::createPixmap(QSize size)
{
- QFontMetrics metric(font());
+ QFont font;
+ font.setStyleStrategy(QFont::ForceOutline);
+ QFontMetrics metric(font);
if (size.width() <= 0) {
size.rwidth() = default_size;
}
if (size.height() <= 0) {
- size.rheight() = metric.size(Qt::TextSingleLine, lyric()).height() + text_margin;
+ size.rheight() = metric.size(Qt::TextSingleLine, lyric()).height() + 2 * text_margin;
}
QImage image(size.width(), size.height(),
QImage::Format_ARGB32_Premultiplied);
image.fill(qRgba(0, 0, 0, 0));
- QFont font;
- font.setStyleStrategy(QFont::ForceOutline);
-
QLinearGradient gradient(0, 0, 0, image.height()-1);
gradient.setColorAt(0.0, Qt::white);
float ff = m_floating ? 1.0f : 0.5f;
@@ -67,7 +67,7 @@ void NoteLabel::createPixmap(QSize size)
painter.setFont(font);
painter.setBrush(Qt::black);
- painter.drawText(QRect(QPoint(6, 6), QSize(size.width()-text_margin, size.height()-text_margin)), Qt::AlignCenter, lyric());
+ painter.drawText(QRect(QPoint(text_margin, text_margin), QSize(size.width()-text_margin, size.height()-text_margin)), Qt::AlignCenter, lyric());
// Render sentence end indicator
if (m_note.lineBreak) {
@@ -85,6 +85,9 @@ void NoteLabel::createPixmap(QSize size)
void NoteLabel::resizeEvent(QResizeEvent *event)
{
createPixmap(event->size());
+ // Update note length
+ NoteGraphWidget* ngw = qobject_cast<NoteGraphWidget*>(parent());
+ if (ngw) m_note.end = m_note.begin + ngw->px2s(width());
}
void NoteLabel::mouseMoveEvent(QMouseEvent *event)
@@ -119,6 +122,9 @@ void NoteLabel::mouseMoveEvent(QMouseEvent *event)
}
}
+ // Update note pos
+ if (ngw) m_note.begin = ngw->px2s(m_note.begin);
+
event->ignore(); // Propagate event to parent
}
diff --git a/notes.hh b/notes.hh
index e5f8b9e..e2b6bb2 100644
--- a/notes.hh
+++ b/notes.hh
@@ -27,8 +27,13 @@ class MusicalScale {
double getNote(double freq) const;
/// get note offset for frequence
double getNoteOffset(double freq) const;
+ /// get octave number
+
+ /// get base id
+ static int getBaseId() { return m_baseId; }
};
+
/// stores duration of a note
struct Duration {
double begin, ///< beginning timestamp in seconds
@@ -62,6 +67,8 @@ struct Note {
int notePrev; ///< MIDI pitch of the previous note (should be same as note for everything but SLIDE)
std::string syllable; ///< lyrics syllable for that note
bool lineBreak; ///< is this note ending a syllable?
+ /// note length
+ double length() const { return end - begin; }
/// difference of n from note
double diff(double n) const { return diff(note, n); }
/// difference of n from note, so that note + diff(note, n) is n (mod 12)
|