summaryrefslogtreecommitdiff
path: root/src/settings/StudySettingsDialog.cpp
blob: b38bf6781971e13183a7bfaf6a497c32a0b43fd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "StudySettingsDialog.h"

StudySettingsDialog::StudySettingsDialog(QWidget *parent):
    QDialog(parent)
    {
    initData();
    createUi();
    updateControls();
    }

void StudySettingsDialog::initData()
    {
    settings = *StudySettings::inst();
    }

void StudySettingsDialog::createUi()
{
    createControls();
    setLayout(createMainLayout());
    setWindowTitle(tr("Study settings"));
}

void StudySettingsDialog::createControls()
{
    dayShiftBox = createSpinBox(0, 12);
    newCardsShareBox = createSpinBox(1, 99);
    randomnessBox = createSpinBox(1, 99);
    cardsDayLimitBox = createSpinBox(1, 500);
    newCardsDayLimitBox = createSpinBox(0, 200);
    limitForAddingNewCardsBox = createSpinBox(20, 500);
    showRandomlyCB = new QCheckBox(tr("Add new cards in random order"));
    createButtonBox();
}

QSpinBox* StudySettingsDialog::createSpinBox(int min, int max)
{
    QSpinBox* spinBox = new QSpinBox;
    spinBox->setMinimum(min);
    spinBox->setMaximum(max);
    return spinBox;
}

void StudySettingsDialog::createButtonBox()
{
    buttonBox = new QDialogButtonBox(
        QDialogButtonBox::Ok | QDialogButtonBox::Cancel |
        QDialogButtonBox::RestoreDefaults, Qt::Horizontal);
    connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
    connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
    connect(buttonBox, SIGNAL(clicked(QAbstractButton*)),
        SLOT(dialogButtonClicked(QAbstractButton*)));
}

QBoxLayout* StudySettingsDialog::createMainLayout()
{
    QHBoxLayout* upperPaddedLt = new QHBoxLayout;
    upperPaddedLt->addLayout(createUpperLayout());
    upperPaddedLt->addStretch();

    QVBoxLayout* mainLt = new QVBoxLayout;
    mainLt->addLayout(upperPaddedLt);
    mainLt->addWidget(showRandomlyCB);
    mainLt->addWidget(createLimitsGroup());
    mainLt->addWidget(buttonBox);
    return mainLt;
}

QGridLayout* StudySettingsDialog::createUpperLayout()
{
    QGridLayout* layout = new QGridLayout;
    int row = 0;
    addUpperGridLine(layout, row++, tr("Day starts at, o'clock:"), dayShiftBox);
    addUpperGridLine(layout, row++, tr("Share of new cards:"), newCardsShareBox, "%");
    addUpperGridLine(layout, row++, tr("Repetition interval randomness:"), randomnessBox, "%");
    return layout;
}

void StudySettingsDialog::addUpperGridLine(QGridLayout* layout, int row, const QString& label,
        QWidget* widget, const QString& unitLabel)
{
    QLabel* labelWidget = new QLabel(label);
    layout->addWidget(labelWidget, row, 0);
    layout->addWidget(widget, row, 1);
    layout->addWidget(new QLabel(unitLabel), row, 2);
}

QGroupBox* StudySettingsDialog::createLimitsGroup()
{
    QGridLayout* layout = new QGridLayout;
    layout->setColumnStretch(0, 1);
    int row = 0;
    addLimitsGridLine(layout, row++, tr("Day reviews limit:"), cardsDayLimitBox);
    addLimitsGridLine(layout, row++, tr("Day limit of new cards:"), newCardsDayLimitBox);
    addLimitsGridLine(layout, row++,
        tr("Don't add new cards after scheduled cards threshold:"),
        limitForAddingNewCardsBox);
    QGroupBox* group = new QGroupBox(tr("Limits"));
    group->setLayout(layout);
    return group;
}

void StudySettingsDialog::addLimitsGridLine(QGridLayout* layout, int row, const QString& label,
        QWidget* widget)
{
    QLabel* labelWidget = new QLabel(label);
    labelWidget->setWordWrap(true);
    layout->addWidget(labelWidget, row, 0);
    layout->addWidget(widget, row, 1);
}

const StudySettings StudySettingsDialog::getSettings()
{
    settings.showRandomly = showRandomlyCB->isChecked();
    settings.newCardsShare = newCardsShareBox->value() / 100.;
    settings.schedRandomness = randomnessBox->value() / 100.;
    settings.cardsDayLimit = cardsDayLimitBox->value();
    settings.newCardsDayLimit = newCardsDayLimitBox->value();
    settings.limitForAddingNewCards = limitForAddingNewCardsBox->value();
    settings.dayShift = dayShiftBox->value();
    return settings;
}

void StudySettingsDialog::updateControls()
{
    showRandomlyCB->setChecked( settings.showRandomly );
    newCardsShareBox->setValue( settings.newCardsShare * 100 );
    randomnessBox->setValue( settings.schedRandomness * 100 );
    cardsDayLimitBox->setValue( settings.cardsDayLimit );
    newCardsDayLimitBox->setValue( settings.newCardsDayLimit );
    limitForAddingNewCardsBox->setValue(settings.limitForAddingNewCards);
    dayShiftBox->setValue( settings.dayShift );
}

void StudySettingsDialog::dialogButtonClicked( QAbstractButton* aButton )
    {
    if(buttonBox->buttonRole(aButton) == QDialogButtonBox::ResetRole)
        {
        settings = StudySettings();
        updateControls();
        }
    }