Posts Tagged ‘ pyqt

Sound Meter With PyQt

This is a simple reimplementation of QProgressBar, I am trying to build a sound meter with PyQt and I made a custom widget for meter. This is really in early dev stage. And this will be help you to know about reimplementing some stuff in a widget.

Test Video:

Here is the source code for Widget

MQProgressBar.py
[Python]
import os
from PyQt4 import QtGui, QtCore, uic

class MQProgressBar(QtGui.QProgressBar):

def __init__(self):
super(MQProgressBar, self).__init__()
self.setTextVisible(False)
self.__chunkOpen = False
self.__heightDiff = 0
self.__blockStyle = False

def setChunkOn(self, mode, value = 0):
if mode:
self.chunk_mode = QtGui.QFrame(self)
self.chunk_mode.setFrameShadow(QtGui.QFrame.Raised)
self.chunk_mode.setLineWidth(2)

if self.orientation() == QtCore.Qt.Vertical:
self.chunk_mode.setFrameShape(QtGui.QFrame.HLine)
else:
self.chunk_mode.setFrameShape(QtGui.QFrame.VLine)
self.__chunkOpen = True

def setValue(self,int):
get_current_val = self.value()
self.updateChunk(get_current_val)
QtGui.QProgressBar.setValue(self,int)

def updateChunk(self, value):
current_x = self.height()
current_y = self.width()
self.__heightDiff
update_val = 5
if value >= 99:
return
elif value > 30:
update_val = 10

if self.orientation() == QtCore.Qt.Vertical:
self.chunk_mode.move(0, ((current_x – 30) – (value * current_x / 100 – update_val)))
else:
self.chunk_mode.move(((value * current_y / 100 – 20)) – (current_y – 80), 0)

def setPeakColor(self,color):
self.chunk_mode.setPalette(QtGui.QPalette(color))

def resizeEvent(self, event):
current_x = self.height()
current_y = self.width()
value = self.value()
old_size = event.oldSize()

if self.orientation() == QtCore.Qt.Vertical:
old_hight = old_size.height()
updated_height = current_x – old_hight
self.chunk_mode.move(0, current_x – 20)
if value > 0:
self.chunk_mode.move(0, ((current_x – 30) – (value * current_x / 100 – 20)))
else:
old_hight = old_size.width()
updated_height = current_y – old_hight
self.chunk_mode.move( current_y – 80 , 0)
if value > 0:
self.chunk_mode.move(((value * current_y / 100 – 20)) – (current_y – 80), 0)

self.__heightDiff = updated_height
QtGui.QProgressBar.resizeEvent(self,event)

def peakVisible(self):
return self.__chunkOpen

def setMeterColor(self, start_color = “green” , end_color = “red”):
mode_val = “horizontal”
if self.orientation() == QtCore.Qt.Vertical:
mode_val = “vertical”
if self.__blockStyle:
self.setStyleSheet (“QProgressBar::chunk:%s {background: qlineargradient(x1: 0, y1: 0.5, x2: 1, y2: 0.5, stop: 0 %s, stop: 1 %s);margin-top: 2px; height: 10px;}” % (mode_val, end_color, end_color))

else:
self.setStyleSheet (“QProgressBar::chunk:%s {background-color: qlineargradient(x1: 1, y1: 0, x2: 1, y2: 1, stop: 0 %s, stop: 1 %s);}” % (mode_val,end_color, start_color))
else:
if self.__blockStyle:
self.setStyleSheet (“QProgressBar::chunk:%s {background: qlineargradient(x1: 0, y1: 0.5, x2: 1, y2: 0.5, stop: 0 %s, stop: 1 %s);margin-right: 2px; width: 10px;}” % (mode_val, end_color, end_color))
else:
self.setStyleSheet (“QProgressBar::chunk:%s {background-color: qlineargradient(x1: 0, y1: 1, x2: 1, y2: 1, stop: 0 %s, stop: 1 %s);}” % (mode_val,start_color, end_color))

def setBlockStyle(self, mode):
self.__blockStyle = mode
self.setMeterColor()

def setMeterBorder(self, color):
self.setStyleSheet(“%sQProgressBar {width: 25px;border: 2px solid %s; border-radius: 8px; background: #494E58;text-align: center;padding: 0px;}” % (self.styleSheet(), color))
[/Python]

Here is the example window which has the MQProgressBar implemented.

progress_test.py
[Python]
import os
import sys
import random

from PyQt4 import QtGui, QtCore, uic

import MQProgressBar as MQProgressBar

class PBRTEST(QtGui.QWidget):
def __init__(self):
super(PBRTEST, self).__init__()
self.initUI()

def initUI(self):
self.mypb = MQProgressBar.MQProgressBar()
self.mypb.setOrientation(QtCore.Qt.Vertical)
self.mypb.setChunkOn(True)
self.mypb.setPeakColor(QtGui.QColor(253, 215, 42))
self.mypb.setMeterColor(“green”,”yellow”)
self.mypb.setMeterBorder(“black”)

self.timer = QtCore.QBasicTimer()
self.step = 0

self.mypb1 = MQProgressBar.MQProgressBar()
self.mypb1.setOrientation(QtCore.Qt.Vertical)
self.mypb1.setChunkOn(True)
self.mypb1.setPeakColor(QtGui.QColor(250, 0, 0))
self.mypb1.setMeterColor(“black”,”red”)

self.mypb2 = MQProgressBar.MQProgressBar()
self.mypb2.setOrientation(QtCore.Qt.Horizontal)
self.mypb2.setChunkOn(True)
self.mypb2.setPeakColor(QtGui.QColor(250, 0, 0))
self.mypb2.setMeterColor(“black”,”red”)
self.mypb2.setBlockStyle(True)
self.mypb2.setMeterBorder(“yellow”)

self.mypb3 = MQProgressBar.MQProgressBar()
self.mypb3.setOrientation(QtCore.Qt.Vertical)
self.mypb3.setChunkOn(True)
self.mypb3.setPeakColor(QtGui.QColor(0, 250, 250))
self.mypb3.setMeterColor(“black”,”green”)
self.mypb3.setBlockStyle(True)
self.mypb3.setMeterBorder(“blue”)

self.mypb4 = MQProgressBar.MQProgressBar()
self.mypb4.setOrientation(QtCore.Qt.Vertical)
self.mypb4.setChunkOn(True)
self.mypb4.setPeakColor(QtGui.QColor(0, 250, 250))

self.btn = QtGui.QPushButton(‘Start’, self)
self.btn.clicked.connect(self.doAction)

hbox = QtGui.QHBoxLayout()
hbox.addWidget(self.mypb)
hbox.addWidget(self.mypb1)
hbox.addWidget(self.mypb2)
hbox.addWidget(self.mypb3)
hbox.addWidget(self.mypb4)

vbox = QtGui.QVBoxLayout()
vbox.addLayout(hbox)
vbox.addWidget(self.btn)

self.setLayout(vbox)
self.setGeometry(1500, 1500, 450, 500)
self.setWindowTitle(‘Progress Test’)
self.show()

def gen_rand_val(self):
rand_val = random.randint(0,100)
self.mypb1.setValue(rand_val)
self.mypb3.setValue(rand_val)

def timerEvent(self, e):

if self.step >= 100:
self.timer.stop()
return

self.step = self.step + 1
self.mypb.setValue(self.step)
self.gen_rand_val()
self.mypb2.setValue(self.step)
self.mypb4.setValue(self.step)

def doAction(self):

if self.timer.isActive():
self.timer.stop()
else:
self.timer.start(100, self)

def run():

app = QtGui.QApplication(sys.argv)
ex = PBRTEST()
ex.show()
sys.exit(app.exec_())

if __name__ == “__main__”:
run()
[/Python]