Skip to content

Commit

Permalink
fix graphics Scene and add comboBox for multi-chart
Browse files Browse the repository at this point in the history
  • Loading branch information
aliammari1 committed Jul 26, 2023
1 parent 2ddb761 commit 4bc161b
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 60 deletions.
1 change: 1 addition & 0 deletions assets/images/images.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
<qresource prefix="/">
<file>pdf.png</file>
<file>preview.png</file>
<file>plane.png</file>
</qresource>
</RCC>
Binary file added assets/images/plane.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/images/preview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 18 additions & 5 deletions gestionVoyages/voyage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,26 @@ QList<Voyage> Voyage::searchVoyages(QString recher)
return Q;
}

int Voyage::getVoyageCount(QString s, QString condition)
QList<int> Voyage::getVoyageCount(QString s,QList<QString>& v)
{
QSqlQuery query;
query.prepare("SELECT COUNT(" + s + ") FROM VOYAGES WHERE " + s + " = :condition");
query.bindValue(":condition", condition);
QList<int> chartData;
QSqlQuery query, query1;
QString condition = "";
query.prepare("SELECT " + s + " FROM VOYAGES");
query.exec();
return query.first() ? query.value(0).toInt() : 0;
while(query.next())
{
qDebug() << query.value(0).toString();
condition = query.value(0).toString();
v.append(condition);
query1.prepare("SELECT COUNT(" + s + ") FROM VOYAGES WHERE " + s + " = :condition");
query1.bindValue(":condition", condition);
query1.exec();
query1.first();
chartData.append(query1.value(0).toInt());
}
qDebug() << chartData;
return chartData;
}

/*
Expand Down
2 changes: 1 addition & 1 deletion gestionVoyages/voyage.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Voyage
QList<Voyage> getAllVoyages();
QList<Voyage> searchVoyages(QString recher);
QList<Voyage> getAllVoyagesSorted(QString order);
int getVoyageCount(QString s, QString condition);
QList<int> getVoyageCount(QString s,QList<QString>& v);
float calculateAverageCost(QString Sdep, QString Sarr);
};

Expand Down
109 changes: 59 additions & 50 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ MainWindow::MainWindow(QWidget *parent)
}
QObject::connect(A.getserial(), SIGNAL(readyRead()), this, SLOT(update_label())); // Launch serial communication

table(); // Initialize table
style(); // Set style
controle(); // Control input
charts(); // Initialize charts
profit(); // Calculate profit
graphics(); // Set graphics
table(); // Initialize table
style(); // Set style
controle(); // Control input
charts(ui->comboBox_chart->currentText()); // Initialize charts
profit(); // Calculate profit
graphics(); // Set graphics
//---------------initialisation de place de TabWidget------------------------------
ui->tabWidget->setGeometry(QRect(0, 0, 1600, 1000));
//---------------setting PushButton initialization color------------------------------
Expand All @@ -50,12 +50,13 @@ 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);
});
connect(ui->lineEdit_rechercher, &QLineEdit::textChanged, [=](const QString &text)
{ table("search", text); });
connect(ui->comboBox_sort, &QComboBox::currentTextChanged, [=](const QString &text)
{ table("sort", text); });
connect(ui->comboBox_chart, &QComboBox::currentTextChanged, [=](const QString &text)
{ charts(text); });

timer->start(1000);
}

Expand All @@ -81,7 +82,7 @@ void MainWindow::on_pushButton_ajouter_clicked()
{
profit();
table();
charts();
charts(ui->comboBox_chart->currentText());
QMessageBox::information(nullptr, QObject::tr("OK"), QObject::tr("Ajout effectué.\nClick Cancel to exit."), QMessageBox::Cancel);
}
else
Expand All @@ -97,7 +98,7 @@ void MainWindow::on_pushButton_delete_clicked()
{
profit();
table();
charts();
charts(ui->comboBox_chart->currentText());
QMessageBox::information(nullptr, QObject::tr("OK"), QObject::tr("Suppression effectué.\nClick Cancel to exit."), QMessageBox::Cancel);
}
else
Expand All @@ -121,7 +122,7 @@ void MainWindow::on_pushButton_update_clicked()
if (test)
{
profit();
charts();
charts(ui->comboBox_chart->currentText());
table();
QMessageBox::information(nullptr, QObject::tr("OK"), QObject::tr("update effectué.\nClick Cancel to exit."), QMessageBox::Cancel);
}
Expand Down Expand Up @@ -234,19 +235,19 @@ void MainWindow::on_pushButton_3_clicked()
}

// Generate a pie chart of voyages by departure location
void MainWindow::charts()
void MainWindow::charts(QString type)
{
qDeleteAll(ui->widget->findChildren<QWidget *>(QString(), Qt::FindDirectChildrenOnly));

QPieSeries *series = new QPieSeries();
series->append("Tunisia", voy.getVoyageCount("LIEUDEP", "Tunisia"));
series->append("France", voy.getVoyageCount("LIEUDEP", "France"));
series->append("United Kingdom", voy.getVoyageCount("LIEUDEP", "United Kingdom"));
series->setLabelsVisible();
series->setPieSize(300);
QList<QString> labels;
QPieSeries *seriesDep = new QPieSeries();
QList<int> numbers = voy.getVoyageCount(type, labels);
for (int i = 0; i < numbers.size(); i++)
seriesDep->append(labels[i], numbers[i]);
seriesDep->setLabelsVisible();
seriesDep->setPieSize(300);

QChart *chart = new QChart();
chart->addSeries(series);
chart->addSeries(seriesDep);
chart->setTitle("Number of flights by country");
chart->legend()->setVisible(true);
chart->legend()->setAlignment(Qt::AlignRight);
Expand All @@ -258,6 +259,7 @@ void MainWindow::charts()
QHBoxLayout *layout = new QHBoxLayout();
layout->addWidget(chartView);
ui->widget->setLayout(layout);
ui->widget->layout()->deleteLater();
}

// Apply custom styles to the UI
Expand Down Expand Up @@ -312,7 +314,7 @@ QList<QLabel *> MainWindow::createHeadBar()
}

// Populate the table in the UI with data from the database
void MainWindow::table(QString type,QString text)
void MainWindow::table(QString type, QString text)
{
QList<Voyage> voyages = {};

Expand All @@ -324,7 +326,6 @@ void MainWindow::table(QString type,QString text)
voyages = voy.searchVoyages(text);

int rowHeight = 30;
int columnWidth = 120;

qDeleteAll(ui->scrollAreaWidgetContents->findChildren<QWidget *>(QString(), Qt::FindDirectChildrenOnly));
QGridLayout *layout = new QGridLayout();
Expand Down Expand Up @@ -361,7 +362,7 @@ void MainWindow::table(QString type,QString text)

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

if (j == 7)
/*if (j == 7)
{
QPushButton *deleteButton = new QPushButton("Delete");
deleteButton->setStyleSheet("QPushButton {"
Expand All @@ -380,7 +381,7 @@ void MainWindow::table(QString type,QString text)
voyageData[0];
table(); });
layout->addWidget(deleteButton, i + 1, j + 1);
}
}*/
}
}

Expand Down Expand Up @@ -443,38 +444,46 @@ void MainWindow::update_label()
}

// This function creates a graphics scene and adds a plane and circles to it, then animates the plane's movement

void MainWindow::graphics()
{
QGraphicsScene *scene;
QGraphicsTextItem *text;
QGraphicsPixmapItem *pixmap;
QGraphicsEllipseItem *circle[50];
scene = new QGraphicsScene(this);
// Create a new graphics scene and set it as the scene for the graphics view
QGraphicsScene *scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);

// Set the background color of the scene
scene->setBackgroundBrush(QBrush(QColor(5, 24, 150)));
pixmap = scene->addPixmap(QPixmap("C:\\Users\\aliam\\OneDrive\\Images\\f1.png"));
pixmap->setPos(100, 0);

// Add a pixmap item to the scene and set its position and opacity
QGraphicsPixmapItem *plane = scene->addPixmap(QPixmap(":/plane.png").scaled(100, 100, Qt::KeepAspectRatio, Qt::SmoothTransformation).transformed(QTransform().rotate(180)));
plane->setPos(220, 5);
plane->setOpacity(1);

// Add 50 circle items to the scene and set their positions
QGraphicsEllipseItem *circles[50];
int x = 10;
for (int i = 0; i < 50; i++)
{
circle[i] = scene->addEllipse(-520, 0, 10, 10, QPen(Qt::white), Qt::white);
circle[i]->setPos(QPoint(-10 - x, ui->graphicsView->geometry().height() / 2 - 6));
circles[i] = scene->addEllipse(0, 0, 10, 10, QPen(Qt::white), Qt::white);
circles[i]->setPos(QPoint(-ui->graphicsView->geometry().width() / 2 - x, ui->graphicsView->geometry().height() / 2 - 50));
x -= 20;
}
text = scene->addText("Tayerni", QFont("Outfit", 18));

// Add a text item to the scene and set its position and font
QGraphicsTextItem *text = scene->addText("Tayerni", QFont("Outfit", 18));
text->setDefaultTextColor(QColor(255, 255, 255));
text->setPos(QPoint(-ui->graphicsView->geometry().width() / 2, 0));
text->setPos(QPoint(-ui->graphicsView->geometry().width() / 2, -20));

QTimer *t = new QTimer();
connect(t, &QTimer::timeout, [=]()
// Create a timer that animates the plane item across the scene
QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout, [=]()
{
QTimeLine *timer = new QTimeLine(5000);
QGraphicsItemAnimation *animation_plane = new QGraphicsItemAnimation();
animation_plane->setItem(pixmap);
animation_plane->setTimeLine(timer);
animation_plane->setTranslationAt(0,100,0);
animation_plane->setTranslationAt(0.5,-800,00);
animation_plane->setTranslationAt(1,100,0);
timer->start(); });
t->start(5000);
QTimeLine *timeLine = new QTimeLine(5000);
QGraphicsItemAnimation *animation = new QGraphicsItemAnimation();
animation->setItem(plane);
animation->setTimeLine(timeLine);
animation->setTranslationAt(0, 220, 0);
animation->setTranslationAt(1, -800, 0); // Only move the plane in one direction
timeLine->start(); });
timer->start(5000);
}
3 changes: 2 additions & 1 deletion mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <QLineSeries>
#include <QPieSeries>
#include <QPieSlice>
#include <QGraphicsColorizeEffect>

#include "assets/files/mybutton.h"
#include "gestionVoyages/voyage.h"
Expand All @@ -32,7 +33,7 @@ class MainWindow : public QMainWindow
MainWindow(QWidget *parent = nullptr);
~MainWindow();

void charts();
void charts(QString type = "");
void style();
void controle();
QList<QLabel *> createHeadBar();
Expand Down
26 changes: 23 additions & 3 deletions mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -657,9 +657,9 @@ background-color:rgba(5, 24, 150);
<property name="geometry">
<rect>
<x>410</x>
<y>470</y>
<width>460</width>
<height>221</height>
<y>440</y>
<width>531</width>
<height>261</height>
</rect>
</property>
<property name="layoutDirection">
Expand Down Expand Up @@ -1017,6 +1017,26 @@ background-color:rgba(5, 24, 150);
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
</widget>
<widget class="QComboBox" name="comboBox_chart">
<property name="geometry">
<rect>
<x>430</x>
<y>460</y>
<width>111</width>
<height>22</height>
</rect>
</property>
<item>
<property name="text">
<string>LIEUARR</string>
</property>
</item>
<item>
<property name="text">
<string>LIEUDEP</string>
</property>
</item>
</widget>
</widget>
</widget>
</widget>
Expand Down

0 comments on commit 4bc161b

Please sign in to comment.