QuakeGod
2023-05-30 d3687ba77720285bac5e7ff51e7e65437e71c00f
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
#include "dialogconfig.h"
#include "ui_dialogconfig.h"
 
DialogConfig::DialogConfig(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogConfig)
{
    ui->setupUi(this);
}
 
DialogConfig::~DialogConfig()
{
    delete ui;
}
 
int DialogConfig::ShowParams()
{
    QString s1;
/*
 *     ui->comboBox_DB_Type->setDuplicatesEnabled(false);
 
    int n=ui->comboBox_DB_Type->count();
    int found=0;
    int foundindex=0;
    for (int i=0;i<n;i++)
    {
        s1=ui->comboBox_DB_Type->itemText(i);
        if (s1.compare(dbconn.DB_Type,Qt::CaseInsensitive)==0)
        {
            found=1;
            foundindex=i;
            break;
 
        }
    }
    if (!found) {ui->comboBox_DB_Type->insertItem(0,dbconn.DB_Type);}
    else {ui->comboBox_DB_Type->setCurrentIndex(foundindex);}
 
 */
 
    QStringList driverList = QSqlDatabase::drivers();
    ui->comboBox_DB_Type->clear();
    ui->comboBox_DB_Type->addItems(driverList);
 
    ui->comboBox_DB_Type->setCurrentText(dbconn.DB_Type);
    ui->comboBox_DB_Type->setEditText(dbconn.DB_Type);
 
    ui->lineEdit_hostname->setText(dbconn.db_HostName);
    ui->lineEdit_port->setText(QString::number(dbconn.db_Port));
    ui->lineEdit_dbname->setText(dbconn.db_Name);
    ui->lineEdit_username->setText(dbconn.db_UserName);
    ui->lineEdit_password->setEchoMode(QLineEdit::EchoMode::Password);
    ui->lineEdit_password->setText(dbconn.db_Password);
 
    return 0;
}
 
int DialogConfig::GetParams()
{
    dbconn.DB_Type=ui->comboBox_DB_Type->currentText();
    dbconn.db_HostName=ui->lineEdit_hostname->text();
    dbconn.db_Port=ui->lineEdit_port->text().toInt();
    dbconn.db_Name=ui->lineEdit_dbname->text();
    dbconn.db_UserName=ui->lineEdit_username->text();
    dbconn.db_Password=ui->lineEdit_password->text();
    return 0;
}
 
void DialogConfig::on_pushButton_TestConn_clicked()
{
    QString s1;
 
    GetParams();
    ui->label_result->setText("test result");
 
    {
        QSqlDatabase dbtest;
 
        dbtest=QSqlDatabase::addDatabase(dbconn.DB_Type,"testdb");
 
        dbtest.setHostName(dbconn.db_HostName);
        dbtest.setPort(dbconn.db_Port);
        dbtest.setDatabaseName(dbconn.db_Name);
        dbtest.setUserName(dbconn.db_UserName);
        dbtest.setPassword(dbconn.db_Password);
        bool ok = dbtest.open();
        if (ok){
            s1="connected OK";
            ui->label_result->setText(s1);
            // QMessageBox::information(nullptr,"infor","link success");
 
        }
        else {
            s1="connected failed:  ";
            s1+= dbtest.lastError().text();
            ui->label_result->setText(s1);
            //        QMessageBox::information(nullptr,"info","link failed " + s1);
            //       qDebug()<<"Error open datebase because "<<db.lastError().text();
        }
    }
    QSqlDatabase::removeDatabase("testdb");
}
 
void DialogConfig::on_pushButton_Test_PLC_clicked()
{
 
}
 
void DialogConfig::on_pushButton_Test_Network_clicked()
{
 
}
 
void DialogConfig::on_pushButton_OK_clicked()
{
    GetParams();
    done(Accepted);
}
 
void DialogConfig::on_pushButton_CANCEL_clicked()
{
    done(Rejected);
}