Skip to content

Commit

Permalink
Learn C3 Widget
Browse files Browse the repository at this point in the history
  • Loading branch information
brentharts authored Nov 23, 2024
1 parent ceb606b commit 7d57d8e
Showing 1 changed file with 57 additions and 13 deletions.
70 changes: 57 additions & 13 deletions libguizag.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def ensure_c3_learn():
]
subprocess.check_call(cmd)

from PySide6.QtWidgets import QApplication, QWidget, QHBoxLayout, QVBoxLayout, QPushButton, QLabel, QFrame, QToolTip, QLineEdit, QSlider, QSizePolicy, QLayout
from PySide6.QtWidgets import QApplication, QWidget, QHBoxLayout, QVBoxLayout, QPushButton, QLabel, QFrame, QToolTip, QLineEdit, QSlider, QSizePolicy, QLayout, QTextEdit
from PySide6.QtCore import QTimer, Qt, QSize
from PySide6.QtGui import (
QFont,
Expand All @@ -83,7 +83,7 @@ def ensure_c3_learn():
)

else:
from PyQt6.QtWidgets import QApplication, QWidget, QHBoxLayout, QVBoxLayout, QPushButton, QLabel, QFrame, QToolTip, QLineEdit, QSlider, QSizePolicy, QLayout
from PyQt6.QtWidgets import QApplication, QWidget, QHBoxLayout, QVBoxLayout, QPushButton, QLabel, QFrame, QToolTip, QLineEdit, QSlider, QSizePolicy, QLayout, QTextEdit
from PyQt6.QtCore import QTimer, Qt, QSize
from PyQt6 import QtCore, QtGui, QtWidgets

Expand Down Expand Up @@ -195,6 +195,10 @@ def reset(self, parent=None):
layout.addWidget(self.materials_container)
#layout.addLayout(self.materials_layout)

self.learn_c3_widget = LearnC3(zoomout=3)
self.learn_c3_widget.setStyleSheet('background-color:white; color:black')
layout.addWidget(self.learn_c3_widget)

else:
self.glview = None
self.materials_layout = None
Expand Down Expand Up @@ -308,7 +312,7 @@ def toggle_fs(self):
self._is_fs = True
if self.glview:
self.glview.setFixedWidth(700)
self.glview.setFixedHeight(700)
self.glview.setFixedHeight(600)
self.update_active_materials()

def clear_object_popup(self):
Expand Down Expand Up @@ -1509,18 +1513,25 @@ def add(self, ob):

]
class LearnC3(QWidget):
def __init__(self):
def __init__(self, zoomout=0):
super().__init__()
self.zoomout = zoomout
self.resize(640, 700)
self.setWindowTitle('Learn C3')
self.main_vbox = vbox = QVBoxLayout()
# Add vertical layout to window
self.setLayout(self.main_vbox)
self.mds = []
path = os.path.join(C3_LEARN, 'old/content/Basics')
for md in os.listdir(path):
if md.endswith('.md'):
print(md)
self.mds.append( os.path.join(path, md) )
path = os.path.join(C3_LEARN, 'old/content/More')
for md in os.listdir(path):
if md.endswith('.md'):
self.mds.append( os.path.join(path, md) )
path = os.path.join(C3_LEARN, 'old/content/Try')
for md in os.listdir(path):
if md.endswith('.md'):
self.mds.append( os.path.join(path, md) )

self.load_random()
Expand All @@ -1530,17 +1541,40 @@ def load_random(self):
clear_layout(self.main_vbox)
md = choice(self.mds)
print('loading:', md)
self.setWindowTitle(md)
md = open(md).read()
rtf = self.parse_md(md)
lab = QLabel(rtf)
self.main_vbox.addWidget(lab)
html = self.parse_md(md)
self.edit = edit = QTextEdit()
edit.setHtml(html)
if self.zoomout:
edit.zoomOut(self.zoomout)
self.main_vbox.addWidget(edit)

btn = QPushButton('next', self.edit)
btn.clicked.connect(lambda b: self.load_random())

def parse_md(self, md):
o = []
in_backticks = False
for ln in md.splitlines():
print(ln)
if ln.startswith('title:'):
self.setWindowTitle(ln.split('title:')[-1].strip())
title = ln.split('title:')[-1].strip()
o.append('<h1>Example: %s</h1>' % title)
continue
elif ln.strip()=='---':
o.append('<hr/>')
continue
elif ln.strip()=='```':
if in_backticks:
in_backticks = False
o.append('</pre>')
else:
in_backticks = True
o.append('<pre style="background-color:lightgray">')
continue
elif ln.startswith('- '):
o.append('<span style="font-size:20px">•%s</span>' % ln)
continue
elif ln.startswith('weight:'):
continue
Expand All @@ -1553,10 +1587,20 @@ def parse_md(self, md):
for hl in open(html).read().splitlines():
if hl.strip().startswith('let defcod'):
code = hl[ hl.index('=')+1 : ].strip()
code = code.replace('\\t', '\t').replace('\\n', '<br/>')
o.append('<code>%s</code>' % code)
code = code.replace('\\t', '\t').replace('\\n', '\n')
if code.startswith('"') and code.endswith('";'):
code = code[1:-2]
code = code.replace('\\"', '"')
code = code.replace('<', '&lt;').replace('>', '&gt;')
o.append('<h1>C3 Code:</h1><pre>%s</pre>' % code)
continue
o.append(ln)
if in_backticks and ln.startswith('//'):
o.append('<i style="font-size:16px; background-color:gray; color:white">%s</i>' % ln)
elif in_backticks and ln.count('//')==1:
a,b = ln.split('//')
o.append('%s\t\t<i style="font-size:12px; background-color:gray; color:white">//%s</i>' % (a,b))
else:
o.append(ln)
return '<br/>'.join(o)


Expand Down

0 comments on commit 7d57d8e

Please sign in to comment.