Skip to content

Instantly share code, notes, and snippets.

View MaurizioB's full-sized avatar

MaurizioB

View GitHub Profile
@MaurizioB
MaurizioB / romanencoder.py
Last active June 26, 2022 02:47
Roman numeral encoding, based on XKCD#2637
import re
from PyQt5.QtWidgets import (QApplication, QVBoxLayout, QWidget, QLabel, QTextEdit)
'''
Inspired by https://xkcd.com/2637/
Based on the Python code of tools/roman.py
'''
RomanNumbers = 'ivxlcdmIVXLCDM'
@MaurizioB
MaurizioB / highlightHover.py
Created September 29, 2021 16:55
QTextEdit with highlight on hover
from PyQt5 import QtCore, QtGui, QtWidgets
from random import randrange, choice
from string import ascii_lowercase as letters
class HighlightTextEdit(QtWidgets.QTextEdit):
_highlightColor = QtGui.QColor('#FFFF00')
_highlightUnderline = False
highlightPos = -1, -1
def __init__(self, *args, **kwargs):
@MaurizioB
MaurizioB / gist:faedf525b4beb8910d3d25ebff66b984
Created July 17, 2021 16:22
QPushButton with embedded QLabel
from PyQt5 import QtWidgets
import sys
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QWidget()
mainLayout = QtWidgets.QGridLayout(window)
labels = []
for r in range(3):
for c in range(4):
button = QtWidgets.QPushButton()
@MaurizioB
MaurizioB / movecolumntest.py
Created June 12, 2021 00:21
QAbstractTableModel item selection odd behavior when moving columns
try:
from PySide2 import QtCore, QtWidgets
print(QtCore.__version__)
except:
from PyQt5 import QtCore, QtWidgets
print(QtCore.QT_VERSION_STR)
class Model(QtCore.QAbstractTableModel):
def __init__(self,):
super().__init__()
@MaurizioB
MaurizioB / simpledial.py
Last active June 11, 2021 13:57
Simple QDial subclass with paint event override
from PyQt5 import QtCore, QtGui, QtWidgets
class SimpleDial(QtWidgets.QDial):
def paintEvent(self, event):
# create a QStyleOption for the dial, and initialize it with the basic properties
# that will be used for the configuration of the painter
opt = QtWidgets.QStyleOptionSlider()
self.initStyleOption(opt)
# construct a QRectF that uses the minimum between width and height,
@MaurizioB
MaurizioB / pianokbmap.json
Created February 10, 2021 20:53
Qt piano keyboard
{
"be": {
"basic": {
"name": "Belgian",
"include": ["latin","level3.ralt_switch"],
"keys": {
"0": {
"0": "w",
"6": ",",
"7": ";",
@MaurizioB
MaurizioB / grabtest.py
Created June 13, 2020 17:17
Click based screen grab workaround for systems not supporting Qt mouseGrab()
from PyQt5 import QtCore, QtGui, QtWidgets
class FakeCursor(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.setWindowFlags(QtCore.Qt.FramelessWindowHint | QtCore.Qt.ToolTip)
self.setFixedSize(16, 16)
path = QtGui.QPainterPath()
path.moveTo(0, 8)
@MaurizioB
MaurizioB / slices.py
Last active May 27, 2020 00:13
Equally slice splitt a list based on step
def getSlices(l, step):
# stepCount, rest = divmod(len(l) - 1, step)
rest = (len(l) - 1) % step
if not rest:
return l[::step]
stepCount = len(l) // (rest + 1)
indexes = [0]
maxIndex = len(l)
i = 0
# maybe done = step for short/odd lists?
'''
Different ways to doSomething(row, column) based on the clicked button
'''
class GuiBase(QtWidgets.QWidget):
def doSomething(self, row, column):
print(row, column)
'''
@MaurizioB
MaurizioB / midi_timings.py
Created August 30, 2018 23:20
Timing of MIDI events in seconds
#!/usr/bin/env python2.7
import sys
import midi
def getTempoMap(pattern, maxTick=None):
#create a first tempo (default to 120, if there is no tempo event)
tempoMap = [[0, .5 / pattern.resolution]]
lastTick = 0
for track in pattern: