This repository was archived by the owner on Apr 17, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustomerselectiondialog.cpp
More file actions
106 lines (92 loc) · 3.87 KB
/
customerselectiondialog.cpp
File metadata and controls
106 lines (92 loc) · 3.87 KB
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
#include "customerselectiondialog.h"
#include <QVBoxLayout>
#include <QHeaderView>
#include <QMessageBox>
#include <QFile>
#include <QTextStream>
CustomerSelectionDialog::CustomerSelectionDialog(QWidget *parent) : QDialog(parent) {
searchLineEdit = new QLineEdit(this);
searchButton = new QPushButton(tr("Search"), this);
selectUserButton = new QPushButton(tr("Select User"), this);
usersTable = new QTableWidget(this);
usersTable->setColumnCount(5); // Match the number of columns to your CSV file
usersTable->setHorizontalHeaderLabels({"First Name", "Last Name", "Phone Number", "Email", "Address"});
usersTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
usersTable->setSelectionMode(QAbstractItemView::SingleSelection);
usersTable->setSelectionBehavior(QAbstractItemView::SelectRows);
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(searchLineEdit);
layout->addWidget(searchButton);
layout->addWidget(usersTable);
layout->addWidget(selectUserButton);
connect(searchButton, &QPushButton::clicked, this, &CustomerSelectionDialog::onSearchClicked);
connect(selectUserButton, &QPushButton::clicked, this, &CustomerSelectionDialog::onSelectUserClicked);
resize(800,600);
populateTableWithUsers();
}
void CustomerSelectionDialog::populateTableWithUsers() {
QFile file("users.csv");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox::warning(this, "Warning", "Could not open users.csv file.");
return;
}
QTextStream in(&file);
QString line = in.readLine(); // Skip header line
while (!in.atEnd()) {
line = in.readLine();
QStringList userData = line.split(',');
int currentRow = usersTable->rowCount();
usersTable->insertRow(currentRow);
for (int i = 0; i < userData.size(); i++) {
usersTable->setItem(currentRow, i, new QTableWidgetItem(userData.at(i)));
}
}
file.close();
}
void CustomerSelectionDialog::onSearchClicked() {
QString searchText = searchLineEdit->text().trimmed();
searchUsers(searchText);
}
void CustomerSelectionDialog::searchUsers(const QString &text) {
// Iterate over all rows and hide those that do not match the search query
for (int i = 0; i < usersTable->rowCount(); ++i) {
bool match = false; // A flag to indicate if any of the columns contain the search text
for (int j = 0; j < usersTable->columnCount(); ++j) {
QTableWidgetItem *item = usersTable->item(i, j);
if (item->text().contains(text, Qt::CaseInsensitive)) {
match = true;
break; // If we find a match, no need to check further; move to the next row
}
}
usersTable->setRowHidden(i, !match); // Hide the row if there is no match
}
}
void CustomerSelectionDialog::onSelectUserClicked() {
int selectedRow = usersTable->currentRow();
if (selectedRow == -1) {
QMessageBox::warning(this, "Warning", "Please select a user first.");
return;
}
QStringList userData;
for (int col = 0; col < usersTable->columnCount(); col++) {
userData << usersTable->item(selectedRow, col)->text();
}
// Now create and show the CustomerDetailsDialog with the selected user's data
CustomerDetailsDialog detailsDialog(userData, this);
detailsDialog.exec(); // Show the dialog as a modal window
}
QStringList CustomerSelectionDialog::getSelectedUserData() const {
int selectedRow = usersTable->currentRow();
QStringList userData;
if (selectedRow != -1) {
for (int col = 0; col < usersTable->columnCount(); col++) {
QTableWidgetItem *item = usersTable->item(selectedRow, col);
if (item) {
userData << item->text();
} else {
userData << "";
}
}
}
return userData;
}