Skip to content

Commit

Permalink
upgrade the table function and add the delete button
Browse files Browse the repository at this point in the history
  • Loading branch information
aliammari1 committed Jul 26, 2023
1 parent 2b8298c commit 2ddb761
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 117 deletions.
7 changes: 3 additions & 4 deletions gestionVoyages/voyage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ QList<Voyage> Voyage::getAllVoyagesSorted(QString order)
{
QList<Voyage> voyages;
QSqlQuery query;
query.prepare("SELECT * FROM VOYAGES ORDER BY :order");
query.bindValue(":order", order);
QString sqlQuery = "SELECT * FROM VOYAGES ORDER BY " + order;
query.prepare(sqlQuery);
query.exec();
while (query.next())
{
Expand All @@ -160,7 +160,7 @@ QList<Voyage> Voyage::getAllVoyagesSorted(QString order)
return voyages;
}

QList<Voyage> Voyage::searchVoyages(QString recher,int *size)
QList<Voyage> Voyage::searchVoyages(QString recher)
{
QList<Voyage> Q;
QSqlQuery query;
Expand All @@ -180,7 +180,6 @@ QList<Voyage> Voyage::searchVoyages(QString recher,int *size)
float montant = query.value(6).toFloat();
int nbper = query.value(7).toInt();
Q.append(Voyage(flightref, lieudep, lieuarr, datedep, datearr, airline, montant, nbper));
(*size)++;
}
}
return Q;
Expand Down
2 changes: 1 addition & 1 deletion gestionVoyages/voyage.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class Voyage
bool searchByFlightRef(QString ref);
QSqlQueryModel *sort(QString s);
QList<Voyage> getAllVoyages();
QList<Voyage> searchVoyages(QString recher,int *size);
QList<Voyage> searchVoyages(QString recher);
QList<Voyage> getAllVoyagesSorted(QString order);
int getVoyageCount(QString s, QString condition);
float calculateAverageCost(QString Sdep, QString Sarr);
Expand Down
2 changes: 1 addition & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ int main(int argc, char *argv[])
if (test)
{
w.show();
a.setOverrideCursor(QCursor(QPixmap("C:\\Users\\aliam\\OneDrive\\Images\\color.png")));
// a.setOverrideCursor(QCursor(QPixmap("C:\\Users\\aliam\\OneDrive\\Images\\color.png")));
QMessageBox::information(nullptr, QObject::tr("database is open"), QObject::tr("connection successful.\nClick Cancel to exit."), QMessageBox::Cancel);
}
else
Expand Down
140 changes: 38 additions & 102 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ MainWindow::MainWindow(QWidget *parent)
lineargradx = 1;
lineargrady = 1;
} });

connect(ui->lineEdit_rechercher, &QLineEdit::textChanged, [=](const QString &text) {
table("search", text);
});
connect(ui->comboBox_sort, &QComboBox::currentTextChanged, [=](const QString &text) {
table("sort", text);
});
timer->start(1000);
}

Expand Down Expand Up @@ -307,63 +312,21 @@ QList<QLabel *> MainWindow::createHeadBar()
}

// Populate the table in the UI with data from the database
void MainWindow::table()
void MainWindow::table(QString type,QString text)
{
QList<Voyage> voyages = voy.getAllVoyages();
int rowHeight = 30;
int columnWidth = 120;

QGridLayout *layout = new QGridLayout();
QList<QLabel *> headerLabels = createHeadBar();
QList<Voyage> voyages = {};

for (int i = 0; i < headerLabels.size(); i++)
{
layout->addWidget(headerLabels[i], 0, i);
}

for (int i = 0; i < voyages.size(); i++)
{
QStringList voyageData = {
voyages[i].getFlightref(),
voyages[i].getLieudep(),
voyages[i].getLieuarr(),
voyages[i].getDatedep().toString("dd/MM/yyyy"),
voyages[i].getDatearr().toString("dd/MM/yyyy"),
voyages[i].getAirline(),
QString::number(voyages[i].getMontant()),
QString::number(voyages[i].getNbper())};
if (type == "" && text == "")
voyages = voy.getAllVoyages();
else if (type == "sort")
voyages = voy.getAllVoyagesSorted(text);
else if (type == "search")
voyages = voy.searchVoyages(text);

for (int j = 0; j < 8; j++)
{
QLineEdit *lineEdit = new QLineEdit(voyageData[j]);
lineEdit->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
lineEdit->setStyleSheet("QLineEdit {"
"height: 30px;"
"}");
lineEdit->setReadOnly(true);

layout->addWidget(lineEdit, i + 1, j);
}
}

layout->setRowMinimumHeight(0, rowHeight);
layout->setHorizontalSpacing(0);
layout->setRowStretch(voyages.size() + 1, 1);
layout->setVerticalSpacing(10);

ui->scrollAreaWidgetContents->setLayout(layout);
ui->scrollAreaWidgetContents->layout()->deleteLater();
}

void MainWindow::on_lineEdit_rechercher_textChanged(const QString &arg1)
{
int size = 0;
QList<Voyage> voyages = voy.searchVoyages(arg1, &size);
int rowHeight = 30;
int columnWidth = 120;

qDeleteAll(ui->scrollAreaWidgetContents->findChildren<QWidget *>(QString(), Qt::FindDirectChildrenOnly));

QGridLayout *layout = new QGridLayout();
QList<QLabel *> headerLabels = createHeadBar();

Expand Down Expand Up @@ -391,60 +354,33 @@ void MainWindow::on_lineEdit_rechercher_textChanged(const QString &arg1)
lineEdit->setStyleSheet("QLineEdit {"
"height: 30px;"
"}");
lineEdit->setReadOnly(true);
if (j != 0)
{
lineEdit->setReadOnly(true);
}

layout->addWidget(lineEdit, i + 1, j);
}
}

layout->setRowMinimumHeight(0, rowHeight);
layout->setHorizontalSpacing(0);
layout->setRowStretch(voyages.size() + 1, 1);
layout->setVerticalSpacing(10);

ui->scrollAreaWidgetContents->setLayout(layout);
ui->scrollAreaWidgetContents->layout()->deleteLater();
}

// This function updates the UI with the sorted list of voyages based on the selected sorting criteria
void MainWindow::on_comboBox_sort_currentTextChanged(const QString &arg1)
{
QList<Voyage> voyages = voy.getAllVoyagesSorted(arg1);
int rowHeight = 30;
int columnWidth = 120;

qDeleteAll(ui->scrollAreaWidgetContents->findChildren<QWidget *>(QString(), Qt::FindDirectChildrenOnly));

QGridLayout *layout = new QGridLayout();
QList<QLabel *> headerLabels = createHeadBar();

for (int i = 0; i < headerLabels.size(); i++)
{
layout->addWidget(headerLabels[i], 0, i);
}

for (int i = 0; i < voyages.size(); i++)
{
QStringList voyageData = {
voyages[i].getFlightref(),
voyages[i].getLieudep(),
voyages[i].getLieuarr(),
voyages[i].getDatedep().toString("dd/MM/yyyy"),
voyages[i].getDatearr().toString("dd/MM/yyyy"),
voyages[i].getAirline(),
QString::number(voyages[i].getMontant()),
QString::number(voyages[i].getNbper())};

for (int j = 0; j < 8; j++)
{
QLineEdit *lineEdit = new QLineEdit(voyageData[j]);
lineEdit->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
lineEdit->setStyleSheet("QLineEdit {"
"height: 30px;"
"}");
lineEdit->setReadOnly(true);

layout->addWidget(lineEdit, i + 1, j);
if (j == 7)
{
QPushButton *deleteButton = new QPushButton("Delete");
deleteButton->setStyleSheet("QPushButton {"
"height: 30px;"
"background-color: red;"
"color: white;"
"border: none;"
"border-radius: 5px;"
"}"
"QPushButton:hover {"
"background-color: rgb(255, 0, 0, 150);"
"}");
connect(deleteButton, &QPushButton::clicked, [=]()
{
voy.supprimer(voyageData[0]);
voyageData[0];
table(); });
layout->addWidget(deleteButton, i + 1, j + 1);
}
}
}

Expand Down
10 changes: 1 addition & 9 deletions mainwindow.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H




#include <QMainWindow>
#include <QLabel>
#include <QtDebug>
Expand Down Expand Up @@ -39,7 +36,7 @@ class MainWindow : public QMainWindow
void style();
void controle();
QList<QLabel *> createHeadBar();
void table();
void table(QString type = "",QString text = "");
void profit();
void graphics();

Expand All @@ -55,20 +52,15 @@ private slots:

void on_pushButton_3_clicked();

void on_comboBox_sort_currentTextChanged(const QString &arg1);

void on_pushButton_clear_clicked();

void on_lineEdit_rechercher_textChanged(const QString &arg1);

void on_comboBox_lieu_From_currentTextChanged(const QString &arg1);

void on_comboBox_lieu_To_currentTextChanged(const QString &arg1);

void on_dateEdit_datedep_userDateChanged(const QDate &date);

void update_label();

private:
Ui::MainWindow *ui;
Voyage voy;
Expand Down

0 comments on commit 2ddb761

Please sign in to comment.