scrollable mdems with proper deletion

This commit is contained in:
jorenchik
2024-09-08 09:45:44 +03:00
parent e0ab96513f
commit f154f1a255
3 changed files with 108 additions and 57 deletions

View File

@@ -19,15 +19,15 @@ type SingleAnswerQuestion struct {
}
type Choice struct {
answer string
isCorrect bool
Answer string
IsCorrect bool
}
type MultipleChoiceQuestion struct {
id string
question string
choices []Choice
section string
ID string
Question string
Choices []Choice
Section string
}
func (question SingleAnswerQuestion) ToString() string {
@@ -46,16 +46,16 @@ func (question MultipleChoiceQuestion) ToString() string {
acc += fmt.Sprintf(
"%20s: section: %-10s id: %-10s %-30s",
"<Multi choice>",
question.section,
question.id,
question.question,
question.Section,
question.ID,
question.Question,
)
for _, el := range question.choices {
for _, el := range question.Choices {
opener := '-'
if el.isCorrect {
if el.IsCorrect {
opener = '+'
}
acc += fmt.Sprintf("\t%c %s\n", opener, strings.Trim(el.answer, "\t\n "))
acc += fmt.Sprintf("\t%c %s\n", opener, strings.Trim(el.Answer, "\t\n "))
}
return acc
}
@@ -238,20 +238,20 @@ func ParseQuestions(tokens []lexer.Token) ([]Question, error) {
}
if len(questionElements) > 1 {
question := MultipleChoiceQuestion{
id: id,
question: question,
ID: id,
Question: question,
}
choices := []Choice{}
for k := 0; k < len(questionElements); k++ {
choice := Choice{}
choice.answer = questionElements[k].content
choice.isCorrect = !questionElements[k].isDash
choice.Answer = questionElements[k].content
choice.IsCorrect = !questionElements[k].isDash
choices = append(choices, choice)
}
if section != "" {
question.section = section
question.Section = section
}
question.choices = choices
question.Choices = choices
questions = append(questions, question)
if DEBUG {
fmt.Printf("%s", question.ToString())

View File

@@ -3,20 +3,22 @@ package main
import (
"fmt"
"os"
"strings"
"time"
"github.com/jorenchik/mdemory/src/compiler/api"
"github.com/jorenchik/mdemory/src/compiler/parser"
"github.com/therecipe/qt/core"
"github.com/therecipe/qt/widgets"
)
var mdems []*widgets.QWidget
var hMdemScroll *widgets.QVBoxLayout
var mdemSpacer *widgets.QSpacerItem
var spacerInitialized bool = false
var workingPath string = "/home/jorenchik/Code/mdemory/memorybase"
// type Mdem struct {
// frontText string;
// widget *widgets.QWidget;
// }
func CreateMdem(frontText string, backText string) *widgets.QWidget {
func CreateMdem(frontText string, backTexts []string) *widgets.QWidget {
// frontText := "What is the capital of Latvia?"
// DefineMdem
@@ -31,6 +33,7 @@ func CreateMdem(frontText string, backText string) *widgets.QWidget {
wFront := widgets.NewQWidget(nil, 0)
hFront := widgets.NewQHBoxLayout()
wFront.SetMinimumHeight(60)
// wFront.SetMaximumHeight(60)
wFront.SetLayout(hFront)
wFront.SetProperty("first", core.NewQVariant1(true));
wMdem.SetStyleSheet(fmt.Sprintf(`
@@ -62,12 +65,15 @@ func CreateMdem(frontText string, backText string) *widgets.QWidget {
vMdem.AddWidget(wBack, 0, 0)
// AddBackContent
for i := range(backTexts) {
backText := strings.Trim(backTexts[i], " \t\n")
elBackText := widgets.NewQLabel2(
fmt.Sprintf("- %s", backText),
nil,
0,
)
hBack.AddWidget(elBackText, 0, 0)
}
hBack.AddStretch(1)
vMdem.AddWidget(wFront, 0, 0)
@@ -92,6 +98,63 @@ func CreateMdem(frontText string, backText string) *widgets.QWidget {
}
func CreateMdems(questions *[]parser.Question) {
if spacerInitialized {
hMdemScroll.RemoveItem(mdemSpacer)
}
for i := range(mdems) {
hMdemScroll.RemoveWidget(mdems[i])
mdems[i].Hide()
mdems[i].DeleteLater()
}
// destroy widgets
mdems = nil
for i := range(*questions) {
question := (*questions)[i]
switch question.(type) {
case parser.SingleAnswerQuestion:
mdems = append(
mdems,
CreateMdem(
question.(parser.SingleAnswerQuestion).Question,
[]string{
question.(parser.SingleAnswerQuestion).Answer,
},
),
)
case parser.MultipleChoiceQuestion:
var answers []string
choices := question.(parser.MultipleChoiceQuestion).Choices
for i := range(choices) {
answers = append(answers, choices[i].Answer)
}
mdems = append(
mdems,
CreateMdem(
question.(parser.MultipleChoiceQuestion).Question,
answers,
),
)
}
}
for i := 0; i < len(mdems); i++ {
hMdemScroll.AddWidget(mdems[i], 0, 0)
}
if !spacerInitialized {
mdemSpacer = widgets.NewQSpacerItem(
40,
40,
widgets.QSizePolicy__Minimum,
widgets.QSizePolicy__Expanding,
)
spacerInitialized = true
}
hMdemScroll.AddItem(mdemSpacer)
}
func main() {
// InitApp
app := widgets.NewQApplication(len(os.Args), os.Args)
@@ -133,7 +196,8 @@ func main() {
)
return
}
_, err = api.Compile(string(fileContents))
start := time.Now().UnixMicro()
questions, err := api.Compile(string(fileContents))
if (err != nil) {
widgets.QMessageBox_Critical(
nil,
@@ -142,8 +206,11 @@ func main() {
widgets.QMessageBox__Ok,
widgets.QMessageBox__Ok,
)
return
}
duration := float32(time.Now().UnixMicro() - start) / 1000
fmt.Printf("Compilation took %.3fms", duration)
CreateMdems(&questions)
})
deckLabel := widgets.NewQLabel2("Decks", nil, 0)
@@ -175,31 +242,15 @@ func main() {
practice.SetText("Practice")
mdemScroll := widgets.NewQScrollArea(nil)
hMdemScroll := widgets.NewQVBoxLayout()
mdemScroll.SetLayout(hMdemScroll)
mdemContainer := widgets.NewQWidget(nil, 0)
hMdemScroll = widgets.NewQVBoxLayout()
mdemScroll.SetWidget(mdemContainer)
mdemScroll.SetWidgetResizable(true)
mdemContainer.SetLayout(hMdemScroll)
// CreateMdems
var mdems []*widgets.QWidget
mdems = append(
mdems, CreateMdem("What is the capital of Latvia?", "Riga"),
)
mdems = append(
mdems, CreateMdem("What is the capital of Estonia?", "Tallin"),
)
mdems = append(
mdems, CreateMdem("What is the capital of Lithuania?", "Vilnius"),
)
mdems = append(
mdems, CreateMdem("What is the capital of Croatia?", "Zagreb"),
)
mdems = append(
mdems, CreateMdem("What is the capital of Chechia?", "Prague"),
)
for i := 0; i < len(mdems); i++ {
hMdemScroll.AddWidget(mdems[i], 0, 0)
rightLayout.AddWidget(mdemScroll, 1, 0)
}
hMdemScroll.AddStretch(1)
// CreateMdems(nil, hMdemScroll)
// Pagination
pagination := widgets.NewQWidget(nil, 0)

Binary file not shown.