mirror of
https://github.com/jorenchik/mdemory.git
synced 2026-03-22 00:26:21 +00:00
pagination behaviour fixes + shuffling
This commit is contained in:
@@ -1,13 +1,21 @@
|
||||
package main
|
||||
|
||||
// TODO: open flashcard with many answers and switch mdem file,
|
||||
// it remains opens with the same options -> Rehide the answers.
|
||||
// iter: 1
|
||||
// [1] 2 3 4 5 6 7
|
||||
|
||||
// 2
|
||||
// press 4
|
||||
// 1 2 3 [4] 5 6 7
|
||||
// 4 (page num) - 2
|
||||
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
"strconv"
|
||||
"math/rand"
|
||||
|
||||
"github.com/jorenchik/mdemory/src/compiler/api"
|
||||
"github.com/jorenchik/mdemory/src/compiler/parser"
|
||||
@@ -15,10 +23,33 @@ import (
|
||||
"github.com/therecipe/qt/widgets"
|
||||
)
|
||||
|
||||
var hMdemScroll *widgets.QVBoxLayout
|
||||
var mdemSpacer *widgets.QSpacerItem
|
||||
var spacerInitialized bool = false
|
||||
var workingPath string = "/home/jorenchik/Code/mdemory/memorybase"
|
||||
type Page struct {
|
||||
start int;
|
||||
end int;
|
||||
}
|
||||
|
||||
type PaginationButton struct {
|
||||
button *widgets.QToolButton;
|
||||
pageIdx int;
|
||||
}
|
||||
|
||||
var deckListLabel *widgets.QLabel
|
||||
var hMdemScroll *widgets.QVBoxLayout
|
||||
var mdemSpacer *widgets.QSpacerItem
|
||||
var spacerInitialized bool = false
|
||||
var workingPath string = "/home/jorenchik/Code/mdemory/memorybase"
|
||||
var questions [] parser.Question
|
||||
var paginationButtons []*widgets.QToolButton
|
||||
var paginationLabel *widgets.QLabel
|
||||
|
||||
// var currentPage = 0
|
||||
var PER_PAGE = 10
|
||||
var pages []Page
|
||||
var currentPage = -1
|
||||
var prevButton *widgets.QToolButton
|
||||
var firstButton *widgets.QToolButton
|
||||
var lastButton *widgets.QToolButton
|
||||
var nextButton *widgets.QToolButton
|
||||
|
||||
type Mdem struct {
|
||||
wMdem *widgets.QWidget;
|
||||
@@ -147,6 +178,7 @@ func CreateMdems(questions *[]parser.Question) {
|
||||
}
|
||||
}
|
||||
|
||||
// destroy widgets
|
||||
for i := range *questions {
|
||||
question := (*questions)[i]
|
||||
switch question.(type) {
|
||||
@@ -157,7 +189,7 @@ func CreateMdems(questions *[]parser.Question) {
|
||||
answer := strings.Trim(
|
||||
question.(parser.SingleAnswerQuestion).Answer, " \t\n",
|
||||
)
|
||||
mdems[i].backLabels[0].SetText(answer)
|
||||
mdems[i].backLabels[0].SetText(fmt.Sprintf("- %s", answer))
|
||||
if mdems[i].wBack.IsVisible() {
|
||||
mdems[i].wBack.Hide()
|
||||
}
|
||||
@@ -172,7 +204,7 @@ func CreateMdems(questions *[]parser.Question) {
|
||||
if k < len(mdems[i].backLabels) {
|
||||
mdems[i].backLabels[k].SetText(answer)
|
||||
} else {
|
||||
label := widgets.NewQLabel2(answer, nil, 0)
|
||||
label := widgets.NewQLabel2(fmt.Sprintf("- %s", answer), nil, 0)
|
||||
mdems[i].backLabels = append(
|
||||
mdems[i].backLabels,
|
||||
label,
|
||||
@@ -199,6 +231,120 @@ func CreateMdems(questions *[]parser.Question) {
|
||||
hMdemScroll.AddItem(mdemSpacer)
|
||||
}
|
||||
|
||||
func MakePages() {
|
||||
pages = nil
|
||||
for i := 0; i < (len(questions) / PER_PAGE) + 1; i++ {
|
||||
startingIndex := PER_PAGE * i
|
||||
amount := PER_PAGE
|
||||
if (i == len(questions) / PER_PAGE) {
|
||||
amount = len(questions) % PER_PAGE
|
||||
}
|
||||
pages = append(
|
||||
pages,
|
||||
Page{startingIndex, startingIndex + amount},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
var PAGINATION_COUNT int = 8
|
||||
var DISTANCE int = PAGINATION_COUNT / 2 - 1
|
||||
|
||||
func SwitchPage(pageIdx int) {
|
||||
currentPage = pageIdx
|
||||
for i := range(paginationButtons) {
|
||||
paginationButtons[i].Hide()
|
||||
}
|
||||
l := 0
|
||||
paginationLabel.SetText(fmt.Sprintf("Page: %d", pageIdx + 1))
|
||||
for i := range(mdems) {
|
||||
if mdems[i].wBack.IsVisible() {
|
||||
mdems[i].wBack.Hide()
|
||||
mdems[i].showButton.SetText("Show")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
for k := -DISTANCE; k <= DISTANCE; k = k + 1 {
|
||||
if pageIdx + k >= 0 && pageIdx + k < len(pages) {
|
||||
button := paginationButtons[l]
|
||||
button.SetText(fmt.Sprintf("%d", pageIdx + k + 1))
|
||||
if pageIdx + k != pageIdx {
|
||||
button.Show()
|
||||
} else {
|
||||
button.Hide()
|
||||
}
|
||||
l++
|
||||
}
|
||||
}
|
||||
|
||||
if pageIdx > 0 && len(pages) > 1 {
|
||||
firstButton.Show()
|
||||
} else {
|
||||
firstButton.Hide()
|
||||
}
|
||||
if pageIdx < len(pages) - 1 && len(pages) > 1 {
|
||||
lastButton.Show()
|
||||
} else {
|
||||
lastButton.Hide()
|
||||
}
|
||||
if len(pages) > 0 &&
|
||||
currentPage < len(pages) - 1 {
|
||||
nextButton.Show()
|
||||
} else {
|
||||
nextButton.Hide()
|
||||
}
|
||||
if len(pages) > 0 &&
|
||||
currentPage >= 1 {
|
||||
prevButton.Show()
|
||||
} else {
|
||||
prevButton.Hide()
|
||||
}
|
||||
|
||||
page := pages[pageIdx]
|
||||
pageSlice := questions[page.start:page.end]
|
||||
CreateMdems(&pageSlice)
|
||||
}
|
||||
|
||||
func OpenModelFile(
|
||||
model *widgets.QFileSystemModel,
|
||||
index *core.QModelIndex,
|
||||
) {
|
||||
if model.IsDir(index) {
|
||||
return
|
||||
}
|
||||
filePath := model.FilePath(index)
|
||||
fileContents, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
widgets.QMessageBox_Critical(
|
||||
nil,
|
||||
"Compilation error",
|
||||
err.Error(),
|
||||
widgets.QMessageBox__Ok,
|
||||
widgets.QMessageBox__Ok,
|
||||
)
|
||||
return
|
||||
}
|
||||
deckListLabel.SetText(fmt.Sprintf("Mdem: %s", filePath))
|
||||
start := time.Now().UnixMicro()
|
||||
questions, err = api.Compile(string(fileContents))
|
||||
if err != nil {
|
||||
widgets.QMessageBox_Critical(
|
||||
nil,
|
||||
"Compilation error",
|
||||
err.Error(),
|
||||
widgets.QMessageBox__Ok,
|
||||
widgets.QMessageBox__Ok,
|
||||
)
|
||||
return
|
||||
}
|
||||
duration := float32(time.Now().UnixMicro()-start) / 1000
|
||||
fmt.Printf("Compilation took %.3fms", duration)
|
||||
|
||||
MakePages()
|
||||
SwitchPage(0)
|
||||
}
|
||||
|
||||
func main() {
|
||||
// InitApp
|
||||
app := widgets.NewQApplication(len(os.Args), os.Args)
|
||||
@@ -224,41 +370,11 @@ func main() {
|
||||
mdemList.HideColumn(1)
|
||||
mdemList.HideColumn(2)
|
||||
mdemList.HideColumn(3)
|
||||
mdemList.ConnectDoubleClicked(func(index *core.QModelIndex) {
|
||||
if model.IsDir(index) {
|
||||
return
|
||||
}
|
||||
filePath := model.FilePath(index)
|
||||
fileContents, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
widgets.QMessageBox_Critical(
|
||||
nil,
|
||||
"Compilation error",
|
||||
err.Error(),
|
||||
widgets.QMessageBox__Ok,
|
||||
widgets.QMessageBox__Ok,
|
||||
)
|
||||
return
|
||||
}
|
||||
start := time.Now().UnixMicro()
|
||||
questions, err := api.Compile(string(fileContents))
|
||||
if err != nil {
|
||||
widgets.QMessageBox_Critical(
|
||||
nil,
|
||||
"Compilation error",
|
||||
err.Error(),
|
||||
widgets.QMessageBox__Ok,
|
||||
widgets.QMessageBox__Ok,
|
||||
)
|
||||
return
|
||||
}
|
||||
duration := float32(time.Now().UnixMicro()-start) / 1000
|
||||
fmt.Printf("Compilation took %.3fms", duration)
|
||||
|
||||
// Take a slice (mock pagination for now)
|
||||
questions = questions[0:10]
|
||||
CreateMdems(&questions)
|
||||
})
|
||||
mdemList.ConnectDoubleClicked(
|
||||
func(index *core.QModelIndex) {
|
||||
OpenModelFile(model, index)
|
||||
},
|
||||
)
|
||||
|
||||
deckLabel := widgets.NewQLabel2("Decks", nil, 0)
|
||||
deckList := widgets.NewQListView(nil)
|
||||
@@ -272,7 +388,7 @@ func main() {
|
||||
|
||||
top := widgets.NewQWidget(nil, 0)
|
||||
hTop := widgets.NewQHBoxLayout()
|
||||
deckListLabel := widgets.NewQLabel2("Mdem: file.mdem", nil, 0)
|
||||
deckListLabel = widgets.NewQLabel(nil, 0)
|
||||
top.SetLayout(hTop)
|
||||
rightLayout.AddWidget(top, 0, 0)
|
||||
|
||||
@@ -286,6 +402,15 @@ func main() {
|
||||
hTop.AddWidget(practice, 0, 0)
|
||||
refresh.SetText("Refresh")
|
||||
shuffle.SetText("Shuffle")
|
||||
shuffle.ConnectClicked(
|
||||
func(checked bool) {
|
||||
for i := range questions {
|
||||
j := rand.Intn(i + 1)
|
||||
questions[i], questions[j] = questions[j], questions[i]
|
||||
}
|
||||
SwitchPage(0)
|
||||
},
|
||||
)
|
||||
practice.SetText("Practice")
|
||||
|
||||
mdemScroll := widgets.NewQScrollArea(nil)
|
||||
@@ -304,26 +429,77 @@ func main() {
|
||||
hMdemScroll.AddWidget(mdem.wMdem, 0, 0)
|
||||
}
|
||||
|
||||
// CreateMdems
|
||||
rightLayout.AddWidget(mdemScroll, 1, 0)
|
||||
// Pagination
|
||||
|
||||
// MakePagination
|
||||
pagination := widgets.NewQWidget(nil, 0)
|
||||
hPagination := widgets.NewQHBoxLayout()
|
||||
pagination.SetLayout(hPagination)
|
||||
|
||||
// TEMP: mock pagination
|
||||
prevButton := widgets.NewQToolButton(nil)
|
||||
firstButton = widgets.NewQToolButton(nil)
|
||||
firstButton.SetText("<<")
|
||||
hPagination.AddWidget(firstButton, 0, 0)
|
||||
firstButton.Hide()
|
||||
firstButton.ConnectClicked(func(checked bool) {
|
||||
if len(pages) > 0 {
|
||||
SwitchPage(0)
|
||||
}
|
||||
})
|
||||
|
||||
prevButton = widgets.NewQToolButton(nil)
|
||||
prevButton.SetText("<")
|
||||
hPagination.AddWidget(prevButton, 0, 0)
|
||||
for i := 0; i < 5; i++ {
|
||||
button := widgets.NewQToolButton(nil)
|
||||
button.SetText(fmt.Sprintf("%d", i+1))
|
||||
hPagination.AddWidget(button, 0, 0)
|
||||
prevButton.Hide()
|
||||
prevButton.ConnectClicked(func(checked bool) {
|
||||
if len(pages) > 0 {
|
||||
SwitchPage(currentPage - 1)
|
||||
}
|
||||
})
|
||||
|
||||
for i := 0; i < PAGINATION_COUNT; i++ {
|
||||
elButton := widgets.NewQToolButton(nil)
|
||||
elButton.SetText(fmt.Sprintf("%d", i+1))
|
||||
hPagination.AddWidget(elButton, 0, 0)
|
||||
elButton.Hide()
|
||||
elButton.ConnectClicked(func(checked bool) {
|
||||
pageNum, err := strconv.ParseInt(elButton.Text(), 10, 64)
|
||||
_ = err
|
||||
pageIdx := int(pageNum) - 1
|
||||
if (pageIdx < len(pages)) {
|
||||
SwitchPage(pageIdx)
|
||||
}
|
||||
})
|
||||
paginationButtons = append(
|
||||
paginationButtons,
|
||||
elButton,
|
||||
)
|
||||
}
|
||||
nextButton := widgets.NewQToolButton(nil)
|
||||
|
||||
nextButton = widgets.NewQToolButton(nil)
|
||||
nextButton.SetText(">")
|
||||
hPagination.AddWidget(nextButton, 0, 0)
|
||||
nextButton.Hide()
|
||||
nextButton.ConnectClicked(func(checked bool) {
|
||||
if len(pages) > 0 {
|
||||
SwitchPage(currentPage + 1)
|
||||
}
|
||||
})
|
||||
|
||||
lastButton = widgets.NewQToolButton(nil)
|
||||
lastButton.SetText(">>")
|
||||
hPagination.AddWidget(lastButton, 0, 0)
|
||||
lastButton.Hide()
|
||||
lastButton.ConnectClicked(func(checked bool) {
|
||||
if len(pages) > 0 {
|
||||
SwitchPage(len(pages) - 1)
|
||||
}
|
||||
})
|
||||
|
||||
hPagination.AddStretch(1)
|
||||
paginationLabel = widgets.NewQLabel(nil, 0)
|
||||
hPagination.AddWidget(paginationLabel, 0, 0)
|
||||
|
||||
rightLayout.AddWidget(pagination, 0, 0)
|
||||
|
||||
hSplitter.AddWidget(leftWidget)
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user