summaryrefslogtreecommitdiffstats
path: root/SQLiteStudio3/guiSQLiteStudio/constraints/columnuniqueandnotnullpanel.cpp
blob: 7c0f5a82ff803331adb5ff4cc50a77503dad0008 (plain) (blame)
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
#include "columnuniqueandnotnullpanel.h"
#include "ui_columnuniqueandnotnullpanel.h"
#include "parser/ast/sqlitecreatetable.h"
#include "parser/keywords.h"
#include "uiutils.h"

ColumnUniqueAndNotNullPanel::ColumnUniqueAndNotNullPanel(QWidget *parent) :
    ConstraintPanel(parent),
    ui(new Ui::ColumnUniqueAndNotNullPanel)
{
    ui->setupUi(this);
    init();
}

ColumnUniqueAndNotNullPanel::~ColumnUniqueAndNotNullPanel()
{
    delete ui;
}

void ColumnUniqueAndNotNullPanel::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
        case QEvent::LanguageChange:
            ui->retranslateUi(this);
            break;
        default:
            break;
    }
}

void ColumnUniqueAndNotNullPanel::init()
{
    ui->conflictCombo->addItems(getConflictAlgorithms());

    connect(ui->namedCheck, SIGNAL(toggled(bool)), this, SIGNAL(updateValidation()));
    connect(ui->namedEdit, SIGNAL(textChanged(QString)), this, SIGNAL(updateValidation()));
    connect(ui->namedCheck, SIGNAL(toggled(bool)), this, SLOT(updateState()));
    connect(ui->conflictCheck, SIGNAL(toggled(bool)), this, SLOT(updateState()));
    updateState();
}

void ColumnUniqueAndNotNullPanel::readConstraint()
{
    SqliteCreateTable::Column::Constraint* constr = dynamic_cast<SqliteCreateTable::Column::Constraint*>(constraint.data());

    if (!constr->name.isNull())
    {
        ui->namedCheck->setChecked(true);
        ui->namedEdit->setText(constr->name);
    }

    if (constr->onConflict != SqliteConflictAlgo::null)
    {
        ui->conflictCheck->setChecked(true);
        ui->conflictCombo->setCurrentText(sqliteConflictAlgo(constr->onConflict));
    }
}

void ColumnUniqueAndNotNullPanel::updateState()
{
    ui->namedEdit->setEnabled(ui->namedCheck->isChecked());
    ui->conflictCombo->setEnabled(ui->conflictCheck->isChecked());
}


bool ColumnUniqueAndNotNullPanel::validate()
{
    bool nameOk = true;
    if (ui->namedCheck->isChecked() && ui->namedEdit->text().isEmpty())
        nameOk = false;

    setValidState(ui->namedEdit, nameOk, tr("Enter a name of the constraint."));

    return nameOk;
}

void ColumnUniqueAndNotNullPanel::constraintAvailable()
{
    if (constraint.isNull())
        return;

    readConstraint();
}

void ColumnUniqueAndNotNullPanel::storeConfiguration()
{
    if (constraint.isNull())
        return;

    storeType();

    SqliteCreateTable::Column::Constraint* constr = dynamic_cast<SqliteCreateTable::Column::Constraint*>(constraint.data());
    if (ui->namedCheck->isChecked())
        constr->name = ui->namedEdit->text();

    if (ui->conflictCheck->isChecked() && ui->conflictCombo->currentIndex() > -1)
        constr->onConflict = sqliteConflictAlgo(ui->conflictCombo->currentText());
}