|
From: Tapio V. <aa...@us...> - 2011-02-22 15:08:15
|
Author: Tapio Vierros <tap...@gm...>
Date: Tue Feb 22 17:07:33 2011 +0200
Use RAII QPainter.
Possibly fixes SeekHandle not showing on Mac (was missing end()).
---
notegraphwidget.cc | 18 ++++++++----------
notelabel.cc | 27 +++++++++++++--------------
pitchvis.cc | 52 ++++++++++++++++++++++++++--------------------------
3 files changed, 47 insertions(+), 50 deletions(-)
diff --git a/notegraphwidget.cc b/notegraphwidget.cc
index 737dc52..c649ba8 100644
--- a/notegraphwidget.cc
+++ b/notegraphwidget.cc
@@ -180,8 +180,7 @@ void NoteGraphWidget::paintEvent(QPaintEvent*) {
int x1, y1, x2, y2;
calcViewport(x1, y1, x2, y2);
- QPainter painter;
- painter.begin(this);
+ QPainter painter(this);
// PitchVis pixmap
if (!m_pixmap.isNull())
@@ -216,8 +215,6 @@ void NoteGraphWidget::paintEvent(QPaintEvent*) {
painter.setPen(pen);
painter.drawRect(QRect(m_mouseHotSpot, mousep));
}
-
- painter.end();
}
void NoteGraphWidget::updatePixmap(const QImage &image, const QPoint &position)
@@ -724,12 +721,13 @@ SeekHandle::SeekHandle(QWidget *parent)
gradient.setColorAt(0.75, QColor(255,255,0,0));
gradient.setColorAt(1.00, QColor(255,255,0,0));
- QPainter painter;
- painter.begin(&image);
- painter.setRenderHint(QPainter::Antialiasing);
- painter.setBrush(gradient);
- painter.setPen(Qt::NoPen);
- painter.drawRect(QRect(0, 0, image.width(), image.height()));
+ {
+ QPainter painter(&image);
+ painter.setRenderHint(QPainter::Antialiasing);
+ painter.setBrush(gradient);
+ painter.setPen(Qt::NoPen);
+ painter.drawRect(QRect(0, 0, image.width(), image.height()));
+ }
setPixmap(QPixmap::fromImage(image));
setMouseTracking(true);
diff --git a/notelabel.cc b/notelabel.cc
index ffc0d03..d551e5d 100644
--- a/notelabel.cc
+++ b/notelabel.cc
@@ -55,24 +55,23 @@ void NoteLabel::createPixmap()
gradient.setColorAt(1.0, QColor(100 * ff, 120 * ff, 100 * ff, alpha));
}
- QPainter painter;
- painter.begin(&image);
- painter.setRenderHint(QPainter::Antialiasing);
- painter.setPen(isSelected() ? Qt::red : Qt::black); // Hilight selected note
- painter.setBrush(gradient);
- painter.drawRoundedRect(QRectF(0.5, 0.5, image.width()-1, image.height()-1), 8, 8);
-
- painter.setFont(font);
- 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) {
- painter.setPen(QPen(QBrush(QColor(255, 0, 0)), 4));
- painter.drawLine(2, 0, 2, image.height()-1);
+ {
+ QPainter painter(&image);
+ painter.setRenderHint(QPainter::Antialiasing);
+ painter.setPen(isSelected() ? Qt::red : Qt::black); // Hilight selected note
+ painter.setBrush(gradient);
+ painter.drawRoundedRect(QRectF(0.5, 0.5, image.width()-1, image.height()-1), 8, 8);
+
+ painter.setFont(font);
+ 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) {
+ painter.setPen(QPen(QBrush(QColor(255, 0, 0)), 4));
+ painter.drawLine(2, 0, 2, image.height()-1);
+ }
}
- painter.end();
-
setPixmap(QPixmap::fromImage(image));
setStatusTip(tr("Lyric: ") + lyric());
diff --git a/pitchvis.cc b/pitchvis.cc
index c799967..8383f49 100644
--- a/pitchvis.cc
+++ b/pitchvis.cc
@@ -144,34 +144,34 @@ void PitchVis::renderer() {
QSettings settings; // Default QSettings parameters given in main()
bool aa = settings.value("anti-aliasing", true).toBool();
- QPainter painter;
- painter.begin(&image);
- if (aa) painter.setRenderHint(QPainter::Antialiasing);
- painter.fillRect(image.rect(), QColor(NoteGraphWidget::BGColor)); // Otherwise the image will have all kinds of carbage
-
- QPen pen;
- pen.setWidth(8);
- pen.setCapStyle(Qt::RoundCap);
-
- PitchVis::Paths const& paths = getPaths();
- for (PitchVis::Paths::const_iterator it = paths.begin(), itend = paths.end(); it != itend; ++it) {
- PitchPath::Fragments const& fragments = it->fragments;
- int oldx, oldy;
- // Only render paths in view
- if (widget->s2px(fragments.back().time) < x1) continue;
- else if (widget->s2px(fragments.front().time) > x2) break;
- // Iterate through the path points
- for (PitchPath::Fragments::const_iterator it2 = fragments.begin(), it2end = fragments.end(); it2 != it2end; ++it2) {
- // TODO: Take y-size into account (change also the paint calls in NoteGraphWidget)
- int x = widget->s2px(it2->time) - x1;
- int y = widget->n2px(it2->note);
- pen.setColor(QColor(32 + 64 * it->channel, clamp<int>(127 + it2->level, 32, 255), 32, 128));
- painter.setPen(pen);
- if (it2 != fragments.begin()) painter.drawLine(oldx, oldy, x, y);
- oldx = x; oldy = y;
+ {
+ QPainter painter(&image);
+ if (aa) painter.setRenderHint(QPainter::Antialiasing);
+ painter.fillRect(image.rect(), QColor(NoteGraphWidget::BGColor)); // Otherwise the image will have all kinds of carbage
+
+ QPen pen;
+ pen.setWidth(8);
+ pen.setCapStyle(Qt::RoundCap);
+
+ PitchVis::Paths const& paths = getPaths();
+ for (PitchVis::Paths::const_iterator it = paths.begin(), itend = paths.end(); it != itend; ++it) {
+ PitchPath::Fragments const& fragments = it->fragments;
+ int oldx, oldy;
+ // Only render paths in view
+ if (widget->s2px(fragments.back().time) < x1) continue;
+ else if (widget->s2px(fragments.front().time) > x2) break;
+ // Iterate through the path points
+ for (PitchPath::Fragments::const_iterator it2 = fragments.begin(), it2end = fragments.end(); it2 != it2end; ++it2) {
+ // TODO: Take y-size into account (change also the paint calls in NoteGraphWidget)
+ int x = widget->s2px(it2->time) - x1;
+ int y = widget->n2px(it2->note);
+ pen.setColor(QColor(32 + 64 * it->channel, clamp<int>(127 + it2->level, 32, 255), 32, 128));
+ painter.setPen(pen);
+ if (it2 != fragments.begin()) painter.drawLine(oldx, oldy, x, y);
+ oldx = x; oldy = y;
+ }
}
}
- painter.end();
// Send the image
// This is actually delivered by the reciever's event loop thread, and not called directly from here
|