mirror of
https://github.com/jorenchik/mdemory.git
synced 2026-03-22 00:26:21 +00:00
scrollable mdems with proper deletion
This commit is contained in:
@@ -19,15 +19,15 @@ type SingleAnswerQuestion struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Choice struct {
|
type Choice struct {
|
||||||
answer string
|
Answer string
|
||||||
isCorrect bool
|
IsCorrect bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type MultipleChoiceQuestion struct {
|
type MultipleChoiceQuestion struct {
|
||||||
id string
|
ID string
|
||||||
question string
|
Question string
|
||||||
choices []Choice
|
Choices []Choice
|
||||||
section string
|
Section string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (question SingleAnswerQuestion) ToString() string {
|
func (question SingleAnswerQuestion) ToString() string {
|
||||||
@@ -46,16 +46,16 @@ func (question MultipleChoiceQuestion) ToString() string {
|
|||||||
acc += fmt.Sprintf(
|
acc += fmt.Sprintf(
|
||||||
"%20s: section: %-10s id: %-10s %-30s",
|
"%20s: section: %-10s id: %-10s %-30s",
|
||||||
"<Multi choice>",
|
"<Multi choice>",
|
||||||
question.section,
|
question.Section,
|
||||||
question.id,
|
question.ID,
|
||||||
question.question,
|
question.Question,
|
||||||
)
|
)
|
||||||
for _, el := range question.choices {
|
for _, el := range question.Choices {
|
||||||
opener := '-'
|
opener := '-'
|
||||||
if el.isCorrect {
|
if el.IsCorrect {
|
||||||
opener = '+'
|
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
|
return acc
|
||||||
}
|
}
|
||||||
@@ -238,20 +238,20 @@ func ParseQuestions(tokens []lexer.Token) ([]Question, error) {
|
|||||||
}
|
}
|
||||||
if len(questionElements) > 1 {
|
if len(questionElements) > 1 {
|
||||||
question := MultipleChoiceQuestion{
|
question := MultipleChoiceQuestion{
|
||||||
id: id,
|
ID: id,
|
||||||
question: question,
|
Question: question,
|
||||||
}
|
}
|
||||||
choices := []Choice{}
|
choices := []Choice{}
|
||||||
for k := 0; k < len(questionElements); k++ {
|
for k := 0; k < len(questionElements); k++ {
|
||||||
choice := Choice{}
|
choice := Choice{}
|
||||||
choice.answer = questionElements[k].content
|
choice.Answer = questionElements[k].content
|
||||||
choice.isCorrect = !questionElements[k].isDash
|
choice.IsCorrect = !questionElements[k].isDash
|
||||||
choices = append(choices, choice)
|
choices = append(choices, choice)
|
||||||
}
|
}
|
||||||
if section != "" {
|
if section != "" {
|
||||||
question.section = section
|
question.Section = section
|
||||||
}
|
}
|
||||||
question.choices = choices
|
question.Choices = choices
|
||||||
questions = append(questions, question)
|
questions = append(questions, question)
|
||||||
if DEBUG {
|
if DEBUG {
|
||||||
fmt.Printf("%s", question.ToString())
|
fmt.Printf("%s", question.ToString())
|
||||||
|
|||||||
@@ -3,20 +3,22 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/jorenchik/mdemory/src/compiler/api"
|
"github.com/jorenchik/mdemory/src/compiler/api"
|
||||||
|
"github.com/jorenchik/mdemory/src/compiler/parser"
|
||||||
"github.com/therecipe/qt/core"
|
"github.com/therecipe/qt/core"
|
||||||
"github.com/therecipe/qt/widgets"
|
"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 {
|
func CreateMdem(frontText string, backTexts []string) *widgets.QWidget {
|
||||||
// frontText string;
|
|
||||||
// widget *widgets.QWidget;
|
|
||||||
// }
|
|
||||||
|
|
||||||
func CreateMdem(frontText string, backText string) *widgets.QWidget {
|
|
||||||
// frontText := "What is the capital of Latvia?"
|
// frontText := "What is the capital of Latvia?"
|
||||||
|
|
||||||
// DefineMdem
|
// DefineMdem
|
||||||
@@ -31,6 +33,7 @@ func CreateMdem(frontText string, backText string) *widgets.QWidget {
|
|||||||
wFront := widgets.NewQWidget(nil, 0)
|
wFront := widgets.NewQWidget(nil, 0)
|
||||||
hFront := widgets.NewQHBoxLayout()
|
hFront := widgets.NewQHBoxLayout()
|
||||||
wFront.SetMinimumHeight(60)
|
wFront.SetMinimumHeight(60)
|
||||||
|
// wFront.SetMaximumHeight(60)
|
||||||
wFront.SetLayout(hFront)
|
wFront.SetLayout(hFront)
|
||||||
wFront.SetProperty("first", core.NewQVariant1(true));
|
wFront.SetProperty("first", core.NewQVariant1(true));
|
||||||
wMdem.SetStyleSheet(fmt.Sprintf(`
|
wMdem.SetStyleSheet(fmt.Sprintf(`
|
||||||
@@ -62,12 +65,15 @@ func CreateMdem(frontText string, backText string) *widgets.QWidget {
|
|||||||
|
|
||||||
vMdem.AddWidget(wBack, 0, 0)
|
vMdem.AddWidget(wBack, 0, 0)
|
||||||
// AddBackContent
|
// AddBackContent
|
||||||
elBackText := widgets.NewQLabel2(
|
for i := range(backTexts) {
|
||||||
fmt.Sprintf("- %s", backText),
|
backText := strings.Trim(backTexts[i], " \t\n")
|
||||||
nil,
|
elBackText := widgets.NewQLabel2(
|
||||||
0,
|
fmt.Sprintf("- %s", backText),
|
||||||
)
|
nil,
|
||||||
hBack.AddWidget(elBackText, 0, 0)
|
0,
|
||||||
|
)
|
||||||
|
hBack.AddWidget(elBackText, 0, 0)
|
||||||
|
}
|
||||||
hBack.AddStretch(1)
|
hBack.AddStretch(1)
|
||||||
|
|
||||||
vMdem.AddWidget(wFront, 0, 0)
|
vMdem.AddWidget(wFront, 0, 0)
|
||||||
@@ -91,6 +97,63 @@ func CreateMdem(frontText string, backText string) *widgets.QWidget {
|
|||||||
return wMdem
|
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() {
|
func main() {
|
||||||
// InitApp
|
// InitApp
|
||||||
@@ -133,7 +196,8 @@ func main() {
|
|||||||
)
|
)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_, err = api.Compile(string(fileContents))
|
start := time.Now().UnixMicro()
|
||||||
|
questions, err := api.Compile(string(fileContents))
|
||||||
if (err != nil) {
|
if (err != nil) {
|
||||||
widgets.QMessageBox_Critical(
|
widgets.QMessageBox_Critical(
|
||||||
nil,
|
nil,
|
||||||
@@ -142,8 +206,11 @@ func main() {
|
|||||||
widgets.QMessageBox__Ok,
|
widgets.QMessageBox__Ok,
|
||||||
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)
|
deckLabel := widgets.NewQLabel2("Decks", nil, 0)
|
||||||
@@ -174,32 +241,16 @@ func main() {
|
|||||||
shuffle.SetText("Shuffle")
|
shuffle.SetText("Shuffle")
|
||||||
practice.SetText("Practice")
|
practice.SetText("Practice")
|
||||||
|
|
||||||
mdemScroll := widgets.NewQScrollArea(nil)
|
mdemScroll := widgets.NewQScrollArea(nil)
|
||||||
hMdemScroll := widgets.NewQVBoxLayout()
|
mdemContainer := widgets.NewQWidget(nil, 0)
|
||||||
mdemScroll.SetLayout(hMdemScroll)
|
hMdemScroll = widgets.NewQVBoxLayout()
|
||||||
|
mdemScroll.SetWidget(mdemContainer)
|
||||||
|
mdemScroll.SetWidgetResizable(true)
|
||||||
|
mdemContainer.SetLayout(hMdemScroll)
|
||||||
|
|
||||||
// CreateMdems
|
// CreateMdems
|
||||||
var mdems []*widgets.QWidget
|
rightLayout.AddWidget(mdemScroll, 1, 0)
|
||||||
mdems = append(
|
// CreateMdems(nil, hMdemScroll)
|
||||||
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)
|
|
||||||
|
|
||||||
// Pagination
|
// Pagination
|
||||||
pagination := widgets.NewQWidget(nil, 0)
|
pagination := widgets.NewQWidget(nil, 0)
|
||||||
|
|||||||
Binary file not shown.
Reference in New Issue
Block a user