diff --git a/src/compiler/parser/parser.go b/src/compiler/parser/parser.go index 54d865c..d5a7c16 100644 --- a/src/compiler/parser/parser.go +++ b/src/compiler/parser/parser.go @@ -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", "", - 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()) diff --git a/src/mdemory-app-qt/main.go b/src/mdemory-app-qt/main.go index 84b4fa9..19103ba 100644 --- a/src/mdemory-app-qt/main.go +++ b/src/mdemory-app-qt/main.go @@ -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 workingPath string = "/home/jorenchik/Code/mdemory/memorybase" +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 - elBackText := widgets.NewQLabel2( - fmt.Sprintf("- %s", backText), - nil, - 0, - ) - hBack.AddWidget(elBackText, 0, 0) + 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) @@ -91,6 +97,63 @@ func CreateMdem(frontText string, backText string) *widgets.QWidget { return wMdem } + +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 @@ -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) @@ -174,32 +241,16 @@ func main() { shuffle.SetText("Shuffle") practice.SetText("Practice") - mdemScroll := widgets.NewQScrollArea(nil) - hMdemScroll := widgets.NewQVBoxLayout() - mdemScroll.SetLayout(hMdemScroll) + mdemScroll := widgets.NewQScrollArea(nil) + 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) + rightLayout.AddWidget(mdemScroll, 1, 0) + // CreateMdems(nil, hMdemScroll) // Pagination pagination := widgets.NewQWidget(nil, 0) diff --git a/src/mdemory-app-qt/mdemory-app-qt b/src/mdemory-app-qt/mdemory-app-qt index 6586b49..172f9d8 100755 Binary files a/src/mdemory-app-qt/mdemory-app-qt and b/src/mdemory-app-qt/mdemory-app-qt differ