|
From: Tapio V. <aa...@us...> - 2011-01-10 14:37:28
|
Module: editor
Branch: master
Commit: 92ea1e7624644ce512262ab3c7cf39551c308625
Author: Tapio Vierros <tap...@gm...>
Date: Mon Jan 10 16:36:53 2011 +0200
Improved floating note handling.
* Takes width into account
* Smooth
* Still no constraints nor auto-scaling
---
notegraphwidget.cc | 42 +++++++++++++++++++++++++++++++-----------
notegraphwidget.hh | 9 +++++++--
notelabel.cc | 3 ++-
notelabel.hh | 1 +
4 files changed, 41 insertions(+), 14 deletions(-)
diff --git a/notegraphwidget.cc b/notegraphwidget.cc
index b0493e5..4b75b4d 100644
--- a/notegraphwidget.cc
+++ b/notegraphwidget.cc
@@ -31,6 +31,7 @@ void NoteGraphWidget::selectNote(NoteLabel* note)
void NoteGraphWidget::clear()
{
+ selectNote(NULL);
// Clear NoteLabels
const QObjectList &childlist = children();
for (QObjectList::const_iterator it = childlist.begin(); it != childlist.end(); ++it) {
@@ -57,9 +58,13 @@ void NoteGraphWidget::setLyrics(QString lyrics)
}
rebuildNoteList();
+ /*
// Set first and last to non-floating
- m_notes.front()->disableFloating();
- m_notes.back()->disableFloating();
+ if (m_notes.size() > 0)
+ m_notes.front()->disableFloating();
+ if (m_notes.size() > 1)
+ m_notes.back()->disableFloating();
+ */
m_requiredWidth = x + gap;
updateWidth();
@@ -76,28 +81,36 @@ void NoteGraphWidget::updateNotes()
// notes according to the fixed ones.
FloatingGap gap(0);
rebuildNoteList();
+
// Determine gaps between non-floating notes
for (NoteLabels::iterator it = m_notes.begin(); it != m_notes.end(); ++it) {
- NoteLabel *child = *it; //dynamic_cast<NoteLabel*>(*it);
+ NoteLabel *child = *it;
if (!child) continue;
- //std::cout << "Child, floating: " << child->isFloating() << std::endl;
if (child->isFloating()) {
// Add floating note to gap
gap.addNote(child);
} else {
// Fixed note encountered, handle the gap (divide notes evenly into it)
- std::cout << "Gap end, width: " << gap.width() << ", notes: " << gap.notes.size() << std::endl;
if (!gap.isEmpty()) {
gap.end = child->x();
- int step = gap.width() / (gap.notes.size() + 1);
- int x = gap.begin + step * 0.5;
- for (NoteLabels::iterator it2 = gap.notes.begin(); it2 != gap.notes.end(); ++it2, x += step) {
- (*it2)->move(x, (*it2)->y());
+ if (gap.width() >= gap.notesWidth()) {
+ // We have enough space - no resizing needed
+ int step = (gap.width() - gap.notesWidth()) / (gap.notes.size() + 1);
+ int x = gap.begin + step;
+ for (NoteLabels::iterator it2 = gap.notes.begin(); it2 != gap.notes.end(); ++it2) {
+ (*it2)->move(x, (*it2)->y());
+ x += step + (*it2)->width();
+ }
+ } else if (gap.width() <= gap.minWidth()) {
+ // We are at minimum width, enforce it
+ // TODO
+ } else {
+ // We can fit everything, if we make the notes smaller
+ // TODO
}
}
// Start a new gap
gap = FloatingGap(child->x() + child->width());
- std::cout << "New gap from: " << gap.begin << std::endl;
}
}
}
@@ -108,7 +121,7 @@ void NoteGraphWidget::rebuildNoteList()
const QObjectList &childlist = children();
for (QObjectList::const_iterator it = childlist.begin(); it != childlist.end(); ++it) {
NoteLabel *child = dynamic_cast<NoteLabel*>(*it);
- if (child) m_notes.push_back(child);
+ if (child && child->isVisible()) m_notes.push_back(child);
}
m_notes.sort(cmpNoteLabelPtr);
}
@@ -122,6 +135,7 @@ void NoteGraphWidget::mousePressEvent(QMouseEvent *event)
m_panHotSpot = event->pos();
return;
}
+ if (child->isHidden()) return;
QPoint hotSpot = event->pos() - child->pos();
@@ -251,4 +265,10 @@ void FloatingGap::addNote(NoteLabel* n)
{
notes.push_back(n);
end = n->x();
+ m_notesWidth += n->width();
+}
+
+int FloatingGap::minWidth() const
+{
+ return notes.size() * NoteLabel::min_width;
}
diff --git a/notegraphwidget.hh b/notegraphwidget.hh
index 8d4b1d4..a0c867e 100644
--- a/notegraphwidget.hh
+++ b/notegraphwidget.hh
@@ -46,14 +46,19 @@ private:
struct FloatingGap
{
- FloatingGap(int x): begin(x), end(x) {}
+ FloatingGap(int x): begin(x), end(x), m_notesWidth() {}
void addNote(NoteLabel* n);
- int width() const { return end - begin; }
bool isEmpty() const { return notes.empty(); }
+ int width() const { return end - begin; }
+ int minWidth() const;
+ int notesWidth() const { return m_notesWidth; }
int begin;
int end;
NoteLabels notes;
+
+private:
+ int m_notesWidth;
};
diff --git a/notelabel.cc b/notelabel.cc
index 7dab1f5..35bf489 100644
--- a/notelabel.cc
+++ b/notelabel.cc
@@ -7,6 +7,7 @@ namespace {
}
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
NoteLabel::NoteLabel(const QString &text, QWidget *parent, const QPoint &position, const QSize &size, bool floating)
: QLabel(parent), m_labelText(text), m_selected(false), m_floating(floating), m_resizing(0), m_hotspot()
@@ -16,7 +17,7 @@ NoteLabel::NoteLabel(const QString &text, QWidget *parent, const QPoint &positio
move(position);
setMouseTracking(true);
- setMinimumSize(10, 10);
+ setMinimumSize(min_width, 10);
setAttribute(Qt::WA_DeleteOnClose);
show();
}
diff --git a/notelabel.hh b/notelabel.hh
index 6607df9..d1a8e8b 100644
--- a/notelabel.hh
+++ b/notelabel.hh
@@ -6,6 +6,7 @@ class NoteLabel: public QLabel
{
public:
static const int resize_margin;
+ static const int min_width;
NoteLabel(const QString &text, QWidget *parent, const QPoint &position = QPoint(), const QSize &size = QSize(), bool floating = true);
|