/
editcustumerdialog.cpp
204 lines (198 loc) · 9.65 KB
/
editcustumerdialog.cpp
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include "editcustumerdialog.h"
#include "ui_editcustumerdialog.h"
#include <QSqlTableModel>
#include <QLineEdit>
#include <QList>
#include <QItemSelectionModel>
#include <QDebug>
#include <QDataWidgetMapper>
#include <QSqlRelationalTableModel>
#include <QSqlRelationalDelegate>
#include <QSqlError>
EditCustumerDialog::EditCustumerDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::EditCustumerDialog)
{
ui->setupUi(this);
//Removes "What's it?" button
this->setWindowFlags(Qt::Dialog | Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint);
//Make Connections
QObject::connect(this->ui->edit_pushButton,SIGNAL(clicked()),this,SLOT(beginEditing()));
QObject::connect(this->ui->save_pushButton_3,SIGNAL(clicked()),this,SLOT(endEditing()));
QObject::connect(this->ui->cancel_pushButton_2,SIGNAL(clicked()),this,SLOT(endEditing()));
//Sets Items for State Combobox
this->ui->state_comboBox->addItems(global_config.usa_states);
//Disables Edit, Cancel and Save Buttons
this->ui->edit_pushButton->setEnabled(false);
this->ui->cancel_pushButton_2->setEnabled(false);
this->ui->save_pushButton_3->setEnabled(false);
//Disables all LineEdits
this->ui->personal_info_groupBox->setEnabled(false);
this->ui->address_info_groupBox_2->setEnabled(false);
//Defines Model Headers
this->customer_model = new QSqlTableModel();
customer_model->setTable("customer");
//Define Headers
customer_model->setHeaderData(customer_model->fieldIndex("id"), Qt::Horizontal, tr("#"));
customer_model->setHeaderData(customer_model->fieldIndex("name"), Qt::Horizontal, tr("Name"));
customer_model->setHeaderData(customer_model->fieldIndex("email"), Qt::Horizontal, tr("Email"));
customer_model->setHeaderData(customer_model->fieldIndex("phone1"), Qt::Horizontal, tr("Phone 1"));
customer_model->setHeaderData(customer_model->fieldIndex("phone2"), Qt::Horizontal, tr("Phone 2"));
customer_model->setHeaderData(customer_model->fieldIndex("birthday"), Qt::Horizontal, tr("Birthday Date"));
customer_model->setHeaderData(customer_model->fieldIndex("zip_code"), Qt::Horizontal, tr("Zip-Code"));
customer_model->setHeaderData(customer_model->fieldIndex("address"), Qt::Horizontal, tr("Address"));
customer_model->setHeaderData(customer_model->fieldIndex("state"), Qt::Horizontal, tr("State"));
customer_model->setHeaderData(customer_model->fieldIndex("city"), Qt::Horizontal, tr("City"));
//customer_model->setEditStrategy(QSqlTableModel::OnManualSubmit);
customer_model->select();
//Sets Model on the Table View
this->ui->customer_tableView->setModel(this->customer_model);
//Disables Edit on Table
this->ui->customer_tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
//Hides some columns on Table View
this->ui->customer_tableView->hideColumn(customer_model->fieldIndex("points"));
//Resizes the column according to its content
this->ui->customer_tableView->resizeColumnsToContents();
//Defines params for selection
this->ui->customer_tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
this->ui->customer_tableView->setSelectionMode(QAbstractItemView::SingleSelection);
//Hides Vertical Header
this->ui->customer_tableView->verticalHeader()->setVisible(false);
//Fills the search ComboBox
QStringList list = QString("#,Name,Email,Phone,Birthday,Address,Zip-Code,State,City").split(",");
this->ui->comboBox->addItems(list);
//Connect Selection Changed Signal
connect(this->ui->customer_tableView->selectionModel(),SIGNAL(selectionChanged(QItemSelection,QItemSelection)),this,SLOT(selectionChanged(QItemSelection,QItemSelection)));
connect(this->ui->search_lineEdit_2,SIGNAL(textEdited(QString)),this,SLOT(searchTextChanged(QString)));
connect(this->ui->comboBox,SIGNAL(currentTextChanged(QString)),this,SLOT(combobox_text_changed(QString)));
//Creates Data Mapper
customer_mapper = new QDataWidgetMapper;
customer_mapper->setModel(this->customer_model);
customer_mapper->addMapping(this->ui->id_lineEdit, this->customer_model->fieldIndex("id"));
customer_mapper->addMapping(this->ui->name_lineEdit_2, this->customer_model->fieldIndex("name"));
customer_mapper->addMapping(this->ui->email_lineEdit_4, this->customer_model->fieldIndex("email"));
customer_mapper->addMapping(this->ui->phone1_lineEdit_2, this->customer_model->fieldIndex("phone1"));
customer_mapper->addMapping(this->ui->phone2_lineEdit_3, this->customer_model->fieldIndex("phone2"));
customer_mapper->addMapping(this->ui->birthday_dateEdit, this->customer_model->fieldIndex("birthday"));
customer_mapper->addMapping(this->ui->address_lineEdit_5, this->customer_model->fieldIndex("address"));
customer_mapper->addMapping(this->ui->zip_code_lineEdit_6, this->customer_model->fieldIndex("zip_code"));
customer_mapper->addMapping(this->ui->state_comboBox, this->customer_model->fieldIndex("state"));
customer_mapper->addMapping(this->ui->city_lineEdit_7, this->customer_model->fieldIndex("city"));
customer_mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
//customer_mapper->toFirst();
//Creates connection for selection of Row on Table
connect(this->ui->customer_tableView->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
customer_mapper, SLOT(setCurrentModelIndex(QModelIndex)));
//Update ComboBox Text
this->combobox_text_changed(this->ui->comboBox->currentText());
connect(this->ui->search_dateEdit,SIGNAL(dateChanged(QDate)),this,SLOT(searchDateChanged(QDate)));
}
void EditCustumerDialog::combobox_text_changed(QString text){
QString field;
QString other="";
this->ui->search_lineEdit_2->setVisible(true);
this->ui->search_dateEdit->setVisible(false);
this->searchTextChanged(QString());
if (text=="#") {
field = "id";
this->ui->search_lineEdit_2->setVisible(true);
this->ui->search_dateEdit->setVisible(false);
this->ui->search_lineEdit_2->setInputMask("D0000");
}else
if(text=="Name")
{
field = "name";
this->ui->search_lineEdit_2->setInputMask("");
}else
if(text=="Phone")
{
field = "phone1";
other = " OR phone2 LIKE '%%2%'";
this->ui->search_lineEdit_2->setInputMask("0000000000");
}else
if(text=="Email")
{
field = "email";
this->ui->search_lineEdit_2->setInputMask("");
}else
if(text=="Birthday")
{
field = "birthday";
this->ui->search_lineEdit_2->setVisible(false);
this->ui->search_dateEdit->setDate(QDate::currentDate());
this->ui->search_dateEdit->setVisible(true);
}
this->filter = QString("%1 LIKE '%%2%'"+other).arg(field);
}
void EditCustumerDialog::selectionChanged(const QItemSelection & selected, const QItemSelection & deselected){
Q_UNUSED(deselected)
if(selected.indexes().length()){
this->ui->edit_pushButton->setEnabled(true);
//int id = selected.indexes().at(0).data(0).toInt();
//qDebug()<<id;
}else{
this->ui->edit_pushButton->setEnabled(false);
}
}
void EditCustumerDialog::searchTextChanged(QString text){
if(!text.isEmpty()){
this->customer_model->setFilter(QString(filter).arg(text));
}else{
this->customer_model->setFilter("");
}
}
void EditCustumerDialog::searchDateChanged(QDate date){
QString tmp_filter = QString(filter).arg(date.toString(global_config.date_format));
qDebug() << tmp_filter;
this->customer_model->setFilter(tmp_filter);
//int row = this->ui->customer_tableView->selectionModel()->currentIndex().row();
qDebug() << this->customer_model->data(this->ui->customer_tableView->selectionModel()->currentIndex()).toString();
}
EditCustumerDialog::~EditCustumerDialog()
{
delete ui;
}
void EditCustumerDialog::beginEditing(){
//Disables Edit Button
this->ui->edit_pushButton->setEnabled(false);
//Enables Save Button
this->ui->save_pushButton_3->setEnabled(true);
//Enables Cancel Button
this->ui->cancel_pushButton_2->setEnabled(true);
//Enables all LineEdits
this->ui->personal_info_groupBox->setEnabled(true);
this->ui->address_info_groupBox_2->setEnabled(true);
/*
QList<QLineEdit *> lineEdits = this->findChildren<QLineEdit *>();
for(int i=0;i<lineEdits.length();++i){
lineEdits.at(i)->setEnabled(true);
}
*/
this->ui->customers_groupBox_3->setEnabled(false);
}
void EditCustumerDialog::endEditing(){
//Enables Edit Button
this->ui->edit_pushButton->setEnabled(true);
//Disables Save Button
this->ui->save_pushButton_3->setEnabled(false);
//Disables Cancel Button
this->ui->cancel_pushButton_2->setEnabled(false);
//Disables all LineEdits
this->ui->personal_info_groupBox->setEnabled(false);
this->ui->address_info_groupBox_2->setEnabled(false);
//Enable table GroupBox and Search Form
this->ui->customers_groupBox_3->setEnabled(true);
this->ui->search_lineEdit_2->setEnabled(true);
//Mapper
customer_mapper->setCurrentIndex(customer_mapper->currentIndex());
}
void EditCustumerDialog::on_save_pushButton_3_clicked()
{
if(customer_mapper->submit()){
qDebug()<<"Record successfull edited";
this->ui->customer_tableView->resizeColumnsToContents();
this->ui->customer_tableView->resizeRowsToContents();
}else{
qDebug()<<"An error occorred while saving table "<<customer_model->lastError().text();
}
}