From 5141dc5f3df0bdffff5dae943e6ae70c6ebc13ee Mon Sep 17 00:00:00 2001 From: Michael Gregorius Date: Sat, 15 Jul 2017 17:21:11 +0200 Subject: [PATCH] Fix problem with Qt5 and move painting code in one block Fix a problem with Qt5 by explicitly setting the pen width to 0 after setting the transformation. Reorder some of the other code to differentiate better between some calculations that are in done in preparation for the painting and the actual painting itself. --- src/tracks/Pattern.cpp | 61 ++++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/src/tracks/Pattern.cpp b/src/tracks/Pattern.cpp index 0b5c2bcaa..58737503e 100644 --- a/src/tracks/Pattern.cpp +++ b/src/tracks/Pattern.cpp @@ -948,21 +948,23 @@ void PatternView::paintEvent( QPaintEvent * ) minKey = qMin( minKey, key ); } - // set colour based on mute status - bool const drawAsLines = height() < 64; - QColor const noteColor = muted ? mutedColor() : painter.pen().brush().color(); + // If needed adjust the note range so that we always have paint a certain interval + int const minimalNoteRange = 12; // Always paint at least one octave + int const actualNoteRange = computeNoteRange(minKey, maxKey); - p.save(); + if (actualNoteRange < minimalNoteRange) + { + int missingNumberOfNotes = minimalNoteRange - actualNoteRange; + minKey = std::max(0, minKey - missingNumberOfNotes / 2); + maxKey = maxKey + missingNumberOfNotes / 2; + if (missingNumberOfNotes % 2 == 1) + { + // Put more range at the top to bias drawing towards the bottom + ++maxKey; + } + } - if (drawAsLines) - { - p.setPen( noteColor ); - } - else - { - p.setPen( noteColor.darker() ); - p.setRenderHint(QPainter::Antialiasing); - } + int const adjustedNoteRange = computeNoteRange(minKey, maxKey); // Transform such that [0, 1] x [0, 1] paints in the correct area float distanceToTop = textBoxHeight; @@ -987,26 +989,33 @@ void PatternView::paintEvent( QPaintEvent * ) } int const notesBorder = 4; // Border for the notes towards the top and bottom in pixels + + // The relavant painting code starts here + p.save(); + p.translate(0., distanceToTop + notesBorder); p.scale(width(), height() - distanceToTop - 2 * notesBorder); + // set colour based on mute status + QColor const noteColor = muted ? mutedColor() : painter.pen().brush().color(); - int const minimalNoteRange = 12; // Always paint at least one octave - int const actualNoteRange = computeNoteRange(minKey, maxKey); - - if (actualNoteRange < minimalNoteRange) + bool const drawAsLines = height() < 64; + if (drawAsLines) { - int missingNumberOfNotes = minimalNoteRange - actualNoteRange; - minKey = std::max(0, minKey - missingNumberOfNotes / 2); - maxKey = maxKey + missingNumberOfNotes / 2; - if (missingNumberOfNotes % 2 == 1) - { - // Put more range at the top to bias drawing towards the bottom - ++maxKey; - } + p.setPen( noteColor ); + } + else + { + p.setPen( noteColor.darker() ); + p.setRenderHint(QPainter::Antialiasing); } - int const adjustedNoteRange = computeNoteRange(minKey, maxKey); + // Needed for Qt5 although the documentation for QPainter::setPen(QColor) as it's used above + // states that it should already set a width of 0. + QPen pen = p.pen(); + pen.setWidth(0); + p.setPen(pen); + float const noteHeight = 1. / adjustedNoteRange; // scan through all the notes and draw them on the pattern