1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include "progressbar.h"
52#include "spectrum.h"
53#include <QPainter>
54
55ProgressBar::ProgressBar(QWidget *parent)
56 : QWidget(parent)
57 , m_bufferLength(0)
58 , m_recordPosition(0)
59 , m_playPosition(0)
60 , m_windowPosition(0)
61 , m_windowLength(0)
62{
63 setSizePolicy(hor: QSizePolicy::Preferred, ver: QSizePolicy::Fixed);
64 setMinimumHeight(30);
65#ifdef SUPERIMPOSE_PROGRESS_ON_WAVEFORM
66 setAutoFillBackground(false);
67#endif
68}
69
70ProgressBar::~ProgressBar()
71{
72
73}
74
75void ProgressBar::reset()
76{
77 m_bufferLength = 0;
78 m_recordPosition = 0;
79 m_playPosition = 0;
80 m_windowPosition = 0;
81 m_windowLength = 0;
82 update();
83}
84
85void ProgressBar::paintEvent(QPaintEvent * /*event*/)
86{
87 QPainter painter(this);
88
89 QColor bufferColor(0, 0, 255);
90 QColor windowColor(0, 255, 0);
91
92#ifdef SUPERIMPOSE_PROGRESS_ON_WAVEFORM
93 bufferColor.setAlphaF(0.5);
94 windowColor.setAlphaF(0.5);
95#else
96 painter.fillRect(rect(), Qt::black);
97#endif
98
99 if (m_bufferLength) {
100 QRect bar = rect();
101 const qreal play = qreal(m_playPosition) / m_bufferLength;
102 bar.setLeft(rect().left() + play * rect().width());
103 const qreal record = qreal(m_recordPosition) / m_bufferLength;
104 bar.setRight(rect().left() + record * rect().width());
105 painter.fillRect(bar, color: bufferColor);
106
107 QRect window = rect();
108 const qreal windowLeft = qreal(m_windowPosition) / m_bufferLength;
109 window.setLeft(rect().left() + windowLeft * rect().width());
110 const qreal windowWidth = qreal(m_windowLength) / m_bufferLength;
111 window.setWidth(windowWidth * rect().width());
112 painter.fillRect(window, color: windowColor);
113 }
114}
115
116void ProgressBar::bufferLengthChanged(qint64 bufferSize)
117{
118 m_bufferLength = bufferSize;
119 m_recordPosition = 0;
120 m_playPosition = 0;
121 m_windowPosition = 0;
122 m_windowLength = 0;
123 repaint();
124}
125
126void ProgressBar::recordPositionChanged(qint64 recordPosition)
127{
128 Q_ASSERT(recordPosition >= 0);
129 Q_ASSERT(recordPosition <= m_bufferLength);
130 m_recordPosition = recordPosition;
131 repaint();
132}
133
134void ProgressBar::playPositionChanged(qint64 playPosition)
135{
136 Q_ASSERT(playPosition >= 0);
137 Q_ASSERT(playPosition <= m_bufferLength);
138 m_playPosition = playPosition;
139 repaint();
140}
141
142void ProgressBar::windowChanged(qint64 position, qint64 length)
143{
144 Q_ASSERT(position >= 0);
145 Q_ASSERT(position <= m_bufferLength);
146 Q_ASSERT(position + length <= m_bufferLength);
147 m_windowPosition = position;
148 m_windowLength = length;
149 repaint();
150}
151

source code of qtmultimedia/examples/multimedia/spectrum/app/progressbar.cpp