Skip to content

Commit

Permalink
Fixed pyinstaller for Windows; Default font;
Browse files Browse the repository at this point in the history
  • Loading branch information
PromethiumL committed Nov 22, 2020
1 parent 7e2b05f commit 0d8e91b
Show file tree
Hide file tree
Showing 13 changed files with 159 additions and 65 deletions.
2 changes: 2 additions & 0 deletions JustChord/core/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@
({'U1', 'm3', 'P5'}, 'm', 0),
({'U1', 'M3', 'A5'}, '+', 1),
({'U1', 'm3', 'd5'}, 'dim', 1),
({'U1', 'A4', 'P5'}, 'lyd', 2),
({'U1', 'm2', 'P5'}, 'phry', 2),
({'U1', 'M3', 'P5', 'M6'}, '6', 1),
({'U1', 'M9', 'M3', 'P5', 'M6'}, '6/9', 1),
({'U1', 'M9', 'M3', 'M6'}, '6/9 no5', 1),
Expand Down
31 changes: 18 additions & 13 deletions JustChord/gui/app.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import traceback

from .chordwindow import *
from .staffwindow import *
from .keyboardwidget import *
from JustChord.gui.chordwindow import *
from JustChord.gui.staffwindow import *
from JustChord.gui.keyboardwidget import *


class JustChordMainWindow(QWidget):
Expand Down Expand Up @@ -115,7 +115,6 @@ def resizeEvent(self, e):
# grip.setVisible(self.show_size_grips)



default_midi_port = 0


Expand All @@ -132,14 +131,16 @@ def __init__(self):
# self.select_midi_mort_signal.connect(self.midi_in_wizard)

# Load Font
font_id = QFontDatabase.addApplicationFont('./assets/Gothic_A1/GothicA1-Light.ttf')
if font_id == -1:
raise Exception("default app font not found!")
font_id = QFontDatabase.addApplicationFont('./assets/Gothic_A1/GothicA1-Regular.ttf')
if font_id != -1:
print(QFontDatabase.applicationFontFamilies(int(font_id)))
# font_name = QFontDatabase.applicationFontFamilies(int(font_id))[0]
# print('css', 'QLabel {{ font: "{}"; }}'.format(font_name))
self.setStyleSheet('.QLabel { font: 24pt "Gothic A1"; }')
else:
print("default app font not found!")
self.setStyleSheet('.QLabel {{ font: "Arial"; }}')

print(QFontDatabase.applicationFontFamilies(int(font_id)))
font_name = QFontDatabase.applicationFontFamilies(int(font_id))[0]
# print('css', 'QLabel {{ font: "{}"; }}'.format(font_name))
self.setStyleSheet('.QLabel {{ font: "{}"; }}'.format(font_name))

# Create Window
jcMainWindow = JustChordMainWindow()
Expand All @@ -148,7 +149,7 @@ def __init__(self):
jcKeyboardWidget = KeyboardWidget()
jcKeyboardWidget.move(jcMainWindow.pos().x() + (jcMainWindow.width() - jcKeyboardWidget.width()) // 2,
jcMainWindow.pos().y() + jcMainWindow.height())

widget.Widget.monitor.trigger.connect(jcKeyboardWidget.updateNotes)
sys.exit(self.exec_())

except Exception as e:
Expand Down Expand Up @@ -221,5 +222,9 @@ def getListItems(self):
return [monitor.midiIn.get_port_name(p) for p in range(ports)]


if __name__ == '__main__':
def main():
app = JustChordApp()


if __name__ == '__main__':
main()
2 changes: 1 addition & 1 deletion JustChord/gui/chordwindow.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# coding:utf-8
from .widget import *
from JustChord.gui.widget import *
from JustChord.core import chord
import copy

Expand Down
14 changes: 13 additions & 1 deletion JustChord/gui/imports.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
import sys
import os

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

from . import monitor
from JustChord.gui import monitor


def debug(func):
def foo(*args, **kwargs):
print(func.__name__, args, kwargs)
return func(*args, **kwargs)

return foo


# @debug
def resource_path(path): # convert path for pyinstaller
path = path.replace('/', os.sep)
if hasattr(sys, '_MEIPASS'):
# print('has,', os.path.join(sys._MEIPASS, path))
return os.path.join(sys._MEIPASS, path)
# print('no,', os.path.join(os.path.abspath('.'), path))
return os.path.join(os.path.abspath('.'), path)
134 changes: 101 additions & 33 deletions JustChord/gui/keyboardwidget.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from JustChord.gui.imports import *
from . import widget
from JustChord.gui import widget

black_key_length_ratio = 0.6
keyboard_offsets = [
Expand All @@ -20,6 +20,9 @@
white_brush = QBrush(Qt.white, Qt.SolidPattern)
green_brush = QBrush(QColor(0x32, 0xf0, 0x80), Qt.SolidPattern)
dark_green_brush = QBrush(QColor(0x11, 0xc1, 0x57), Qt.SolidPattern)
blue_brush = QBrush(QColor(0, 0x90, 0xff), Qt.SolidPattern)
light_blue_brush = QBrush(QColor(0x44, 0x99, 0xff), Qt.SolidPattern)
dark_blue_brush = QBrush(QColor(0, 0x80, 0xdd), Qt.SolidPattern)


def is_black(pitch):
Expand Down Expand Up @@ -54,21 +57,35 @@ def count_accumulated_white_keys(lo, hi):
class KeyboardWidget(QWidget):
def __init__(self):
super().__init__()

self.min_pitch = 60
self.max_pitch = 37 + 60
self.pressed_notes = set()
self.sustained_notes = set()
self.min_pitch = 48
self.max_pitch = 37 + 48
self.mouse_pressed_notes = set()
self.mouse_sustained_notes = set()
self.midi_pressed_notes = set()
self.midi_sustained_notes = set()
self.is_sustain_down = False
self.mouse_current_pitch = None
self.keyboardHeight = 200 # height of the white key
# self.setStyleSheet('.KeyboardWidget { padding: 20px; }')

self.strokeWidth = 3
self.strokeWidth = 2
self.pen = QPen(Qt.darkGray, self.strokeWidth, Qt.SolidLine)
self.painter = QPainter(self)
self.painter.setPen(self.pen)
self.initUI()
self.pixmap = QPixmap(int((self.horizontal_unit() + 1) * self.range()), int(self.keyboardHeight))
self.generatePixmap()
self.update()
# try:
#
# except Exception as e:
# print(e)
# print('cannot connect to the MIDI monitor')

def updateNotes(self):
# print('update notes')
self.midi_pressed_notes = set(monitor.pressedNotes)
self.midi_sustained_notes = set(monitor.sustainedNotes)

def send_midi_msg_to_monitor(self, channel, pitch, velocity):
with widget.Widget.monitor.lock:
Expand Down Expand Up @@ -103,89 +120,138 @@ def initUI(self):

def paintEvent(self, e):
painter = QPainter(self)
painter.setPen(self.pen)
painter.drawPixmap(QPoint(0, 0), self.pixmap)

# painter.drawRect(0, 0, int(self.range() * self.horizontal_unit()), int(self.keyboardHeight))

painter.setPen(self.pen)
self.update()

for i in range(self.range()): # white keys
p = self.min_pitch + i
if is_black(p):
continue
else:
if p in self.pressed_notes:
notes_to_draw = self.mouse_pressed_notes | self.mouse_sustained_notes | self.midi_pressed_notes | self.midi_sustained_notes
for p in notes_to_draw:
if is_white(p):
if p in self.midi_pressed_notes:
painter.setBrush(blue_brush)
elif p in self.midi_sustained_notes:
painter.setBrush(light_blue_brush)
else:
painter.setBrush(Qt.NoBrush)

if p in self.mouse_pressed_notes:
painter.setBrush(green_brush)
elif p in self.sustained_notes:
elif p in self.mouse_sustained_notes:
# painter.setBrush(light_gray_brush)
painter.setBrush(green_brush)
else:
painter.setBrush(Qt.NoBrush)

x = count_accumulated_white_keys(self.min_pitch, p) * self.white_step()
painter.drawRect(
int(x), 0,
int(self.white_step()), self.keyboardHeight
)

for i in range(self.range()): # black keys
p = self.min_pitch + i
for p in range(self.min_pitch, self.max_pitch + 1):
if is_black(p):
if p in self.pressed_notes:
if p in self.midi_pressed_notes:
painter.setBrush(light_blue_brush)
elif p in self.midi_sustained_notes:
painter.setBrush(dark_blue_brush)
else:
painter.setBrush(gray_brush)

if p in self.mouse_pressed_notes:
painter.setBrush(green_brush)
elif p in self.sustained_notes:
elif p in self.mouse_sustained_notes:
# painter.setBrush(light_gray_brush)
painter.setBrush(dark_green_brush)
else:
painter.setBrush(gray_brush)
x = (p - self.min_pitch) * self.black_step()
painter.drawRect(
int(x), 0,
int(self.black_step()), int(self.keyboardHeight * black_key_length_ratio)
)

if self.is_sustain_down:
painter.setPen(QPen(QColor(0x32, 0xf0, 0x80), 5, Qt.SolidLine))

painter.drawLine(0, 0, self.width(), 0)
self.update()

def generatePixmap(self):
self.pixmap.fill(Qt.transparent)
# QColor("transparent") ?
painter = QPainter(self.pixmap)
painter.setPen(self.pen)
painter.setBrush(Qt.NoBrush)
for i in range(self.range()): # white keys
p = self.min_pitch + i
if is_black(p):
continue
else:
x = count_accumulated_white_keys(self.min_pitch, p) * self.white_step()
painter.drawRect(
int(x), 0,
int(self.white_step()), self.keyboardHeight
)
painter.setBrush(gray_brush)
for i in range(self.range()):
if is_black(i + self.min_pitch):
x = i * self.black_step()
painter.drawRect(
int(x), 0,
int(self.black_step()), int(self.keyboardHeight * black_key_length_ratio)
)
painter.end()

def resizeEvent(self, e):
# self.resize(int(self.horizontal_unit() * self.range()), int(self.keyboardHeight))
self.keyboardHeight = self.height()
self.pixmap = QPixmap(int((self.horizontal_unit() + 1) * self.range()), int(self.keyboardHeight))
self.generatePixmap()
self.update()


def keyPressEvent(self, e: QKeyEvent):
if e.isAutoRepeat():
return
if e.key() == Qt.Key_Space: # hold sustain pedal
self.is_sustain_down = True
print('sustain')


def keyReleaseEvent(self, e: QKeyEvent):
if e.isAutoRepeat():
return
if e.key() == Qt.Key_Space: # release sustain pedal
self.is_sustain_down = False
for p in list(self.sustained_notes):
if p not in self.pressed_notes:
for p in list(self.mouse_sustained_notes):
if p not in self.mouse_pressed_notes:
self.note_off(p)
self.sustained_notes.discard(p)
self.mouse_sustained_notes.discard(p)
print('released')


def mousePressEvent(self, e: QMouseEvent):
x = e.pos().x()
y = e.pos().y()
pitch = self.posToPitch(x, y)
if self.min_pitch <= pitch <= self.max_pitch:
self.addNote(pitch)


def removeNote(self, pitch):
if pitch in self.pressed_notes:
self.pressed_notes.discard(pitch)
if pitch in self.mouse_pressed_notes:
self.mouse_pressed_notes.discard(pitch)
if self.is_sustain_down:
self.sustained_notes.add(pitch)
self.mouse_sustained_notes.add(pitch)
else:
self.note_off(pitch)


def addNote(self, pitch):
self.mouse_current_pitch = pitch
self.note_on(pitch)
self.pressed_notes.add(pitch)
self.mouse_pressed_notes.add(pitch)
if self.is_sustain_down:
self.sustained_notes.add(pitch)
self.mouse_sustained_notes.add(pitch)


def mouseMoveEvent(self, e): # Glissing notes
x = e.pos().x()
Expand All @@ -197,11 +263,13 @@ def mouseMoveEvent(self, e): # Glissing notes
self.removeNote(self.mouse_current_pitch)
self.addNote(pitch)


def mouseReleaseEvent(self, e):
pitch = self.posToPitch(e.pos().x(), e.pos().y())
if pitch in self.pressed_notes:
if pitch in self.mouse_pressed_notes:
self.removeNote(pitch)


def posToPitch(self, x, y):
i = int(x // self.horizontal_unit())
if is_white(i):
Expand Down
Loading

0 comments on commit 0d8e91b

Please sign in to comment.