示例#1
0
void NWaveformSlider::checkForUpdate()
{
	if (!m_waveBuilder)
		return;

	bool needsUpdate = FALSE;
	if (m_oldValue != value() || m_oldSize != size())
		needsUpdate = TRUE;

	float pos;
	int index;
	m_waveBuilder->positionAndIndex(pos, index);

	if (!(pos < 0 || index < 0) &&
		!(m_oldBuildPos == pos &&
		m_oldIndex == index &&
		m_waveImage.size() == size()))
	{
		m_oldBuildPos = pos;
		m_oldIndex = index;

		if (m_waveImage.size() != size())
			m_waveImage = QImage(size(), QImage::Format_ARGB32_Premultiplied);

		QPainter painter(&m_waveImage);
		m_waveImage.fill(0);

		painter.setRenderHint(QPainter::Antialiasing);
		painter.setBrush(wave_bg);
		QPen wavePen;
		wavePen.setWidth(0);
		wavePen.setColor(wave_border);
		painter.setPen(wavePen);

		painter.translate(1, 1);
		painter.scale((qreal)(width() - 1) / m_oldIndex * m_oldBuildPos, (height() - 2) / 2);
		painter.translate(0, 1);

		QPainterPath pathPos;
		QPainterPath pathNeg;
		NWaveformPeaks *peaks = m_waveBuilder->peaks();
		for (int i = 0; i < m_oldIndex; ++i) {
			pathPos.lineTo(i, peaks->positive(i));
			pathNeg.lineTo(i, peaks->negative(i));
		}
		QPainterPath fullPath(pathNeg);
		fullPath.connectPath(pathPos.toReversed());
		fullPath.closeSubpath();
		painter.drawPath(fullPath);

		needsUpdate = TRUE;
	}

	if (needsUpdate)
		update();
}
示例#2
0
void NWaveformSlider::checkForUpdate()
{
    if (!m_waveBuilder)
        return;

    float builderPos;
    int builderIndex;
    m_waveBuilder->positionAndIndex(builderPos, builderIndex);

    if (m_oldSize != size() || m_oldBuilderIndex != builderIndex)
        m_needsUpdate = true;

    if ((builderPos != 0.0 && builderPos != 1.0) && m_timer->interval() != FAST_INTERVAL)
        m_timer->setInterval(FAST_INTERVAL);
    else
    if ((builderPos == 0.0 || builderPos == 1.0) && m_timer->interval() != IDLE_INTERVAL)
        m_timer->setInterval(IDLE_INTERVAL);

    if (m_needsUpdate) {
        QPainter painter;
        QImage waveImage;
        QImage backgroundImage;
        waveImage = backgroundImage = m_progressPlayingImage = m_progressPausedImage = m_remainingPlayingImage = m_remainingPausedImage = QImage(size(), QImage::Format_ARGB32_Premultiplied);

        m_oldBuilderPos = builderPos;
        m_oldBuilderIndex = builderIndex;
        m_oldSize = size();


        // waveform >>
        waveImage.fill(0);
        painter.begin(&waveImage);
        painter.setBrush(m_waveBackground);
        QPen wavePen;
        wavePen.setWidth(0);
        wavePen.setColor(m_waveBorderColor);
        painter.setPen(wavePen);

        painter.translate(1, 1);
        painter.scale((qreal)(width() - 1) / m_oldBuilderIndex * m_oldBuilderPos, (height() - 2) / 2);
        painter.translate(0, 1);

        QPainterPath pathPos;
        QPainterPath pathNeg;
        NWaveformPeaks *peaks = m_waveBuilder->peaks();
        for (int i = 0; i < m_oldBuilderIndex; ++i) {
            pathPos.lineTo(i, peaks->positive(i));
            pathNeg.lineTo(i, peaks->negative(i));
        }
        QPainterPath fullPath(pathNeg);
        fullPath.connectPath(pathPos.toReversed());
        fullPath.closeSubpath();
        painter.setRenderHint(QPainter::Antialiasing);
        painter.drawPath(fullPath);
        painter.end();
        // << waveform


        // main background >>
        painter.begin(&backgroundImage);
        backgroundImage.fill(0);
        painter.setPen(Qt::NoPen);
        painter.setBrush(m_background);
        painter.setRenderHint(QPainter::Antialiasing);
        painter.drawRoundedRect(rect(), m_radius, m_radius);
        painter.end();
        // << main background


        QList<QImage *> images; images << &m_progressPlayingImage << &m_progressPausedImage << &m_remainingPlayingImage << &m_remainingPausedImage;
        QList<QPainter::CompositionMode> modes; modes << m_playingComposition << m_pausedComposition << m_playingComposition << m_pausedComposition;
        QList<QBrush> brushes; brushes << m_progressPlayingBackground << m_progressPausedBackground << m_remainingPlayingBackground << m_remainingPausedBackground;
        for (int i = 0; i < images.size(); ++i) {
            painter.begin(images[i]);
            // background
            images[i]->fill(0);
            // + overlay
            painter.setPen(Qt::NoPen);
            painter.setBrush(brushes[i]);
            painter.setRenderHint(QPainter::Antialiasing);
            painter.drawRoundedRect(rect(), m_radius, m_radius);
            // + waveform
            painter.setCompositionMode(modes[i]);
            painter.drawImage(0, 0, waveImage);
            painter.setCompositionMode(QPainter::CompositionMode_DestinationOver);
            painter.drawImage(0, 0, backgroundImage);
            painter.end();
        }

        update();
        m_needsUpdate = false;
    }
}