#include "windowrennliste.h" #include "ui_windowrennliste.h" #include "windowrace.h" #include #include #include #include using std::cout; using std::endl; using std::string; using std::vector; WindowRennliste::WindowRennliste(std::shared_ptr db, QWidget * parent) : QMainWindow(parent), ui(new Ui::WindowRennliste) { ui->setupUi(this); this->windowClose = false; this->selectedRow = 0; this->ui->tWRennliste->setSelectionBehavior(QAbstractItemView::SelectRows); this->ui->tWRennliste->setSelectionMode(QAbstractItemView::SingleSelection); QObject::connect(this->ui->tWRennliste, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(listClick(const QModelIndex &))); this->db = db; string statement; vector> fahrer, autos, rennid; statement = "select id_rennen from aktrennen group by id_rennen order by " "id_rennen DESC limit 1"; rennid = this->db->getData(statement, 1); statement = "select fahrershellid, autoshellid, fahrerdeaid, autodeaid " "from aktrennen where id_rennen like " + rennid.at(0).at(0).toStdString(); this->tableData = db->getData(statement, 4); this->ui->tWRennliste->setRowCount( static_cast(this->tableData.size())); this->ui->tWRennliste->setColumnCount(6); QStringList header; header << "Fahrer" << "Auto" << "Zeit" << "Fahrer" << "Auto" << "Zeit"; this->ui->tWRennliste->setHorizontalHeaderLabels(header); for (int i = 0; i < this->ui->tWRennliste->horizontalHeader()->count(); i++) { this->ui->tWRennliste->horizontalHeader()->setSectionResizeMode( i, QHeaderView::Stretch); } for (unsigned int i = 0; i < tableData.size(); i++) { statement = "select name from fahrer where id like " + tableData.at(i).at(0).toStdString(); fahrer = this->db->getData(statement, 1); this->ui->tWRennliste->setItem( static_cast(i), 0, new QTableWidgetItem(fahrer.at(0).at(0))); this->ui->tWRennliste->item(static_cast(i), 0) ->setFlags(Qt::ItemIsEnabled); statement = "select name from AutoKonfiguration where id_auto like " + tableData.at(i).at(1).toStdString() + " order by seit DESC"; autos = this->db->getData(statement, 1); this->ui->tWRennliste->setItem(static_cast(i), 1, new QTableWidgetItem(autos.at(0).at(0))); this->ui->tWRennliste->item(static_cast(i), 1) ->setFlags(Qt::ItemIsEnabled); this->ui->tWRennliste->setItem(static_cast(i), 2, new QTableWidgetItem()); statement = "select name from fahrer where id like " + tableData.at(i).at(2).toStdString(); fahrer = this->db->getData(statement, 1); this->ui->tWRennliste->setItem( static_cast(i), 3, new QTableWidgetItem(fahrer.at(0).at(0))); this->ui->tWRennliste->item(static_cast(i), 3) ->setFlags(Qt::ItemIsEnabled); statement = "select name from AutoKonfiguration where id_auto like " + tableData.at(i).at(3).toStdString() + " order by seit DESC"; autos = this->db->getData(statement, 1); this->ui->tWRennliste->setItem(static_cast(i), 4, new QTableWidgetItem(autos.at(0).at(0))); this->ui->tWRennliste->item(static_cast(i), 4) ->setFlags(Qt::ItemIsEnabled); this->ui->tWRennliste->setItem(static_cast(i), 5, new QTableWidgetItem()); } for (int i = 0; i < 6; i++) { this->ui->tWRennliste->item(0, i)->setBackground(Qt::green); } } void WindowRennliste::setWindowRace(std::shared_ptr instance) { this->instanceWindowRace = instance; } vector WindowRennliste::getDriverAndCarSettings() { vector vec; vec.push_back( this->ui->tWRennliste->item(static_cast(this->selectedRow) + 1, 0) ->text()); vec.push_back( this->ui->tWRennliste->item(static_cast(this->selectedRow) + 1, 1) ->text()); vec.push_back( this->ui->tWRennliste->item(static_cast(this->selectedRow) + 1, 3) ->text()); vec.push_back( this->ui->tWRennliste->item(static_cast(this->selectedRow) + 1, 4) ->text()); return vec; } vector WindowRennliste::getDriverAndCarId() { // shellFahrer shellAuto deaFahrer deaAuto return this->tableData.at(this->selectedRow); } void WindowRennliste::setBesttime(int shell, int dea) { this->ui->tWRennliste->item(static_cast(this->selectedRow), 2) ->setText(QString::number(shell / 1000.0)); this->ui->tWRennliste->item(static_cast(this->selectedRow), 5) ->setText(QString::number(dea / 1000.0)); // std::cout << static_cast(this->selectedRow) << std::endl; } void WindowRennliste::listClick(const QModelIndex & index) { QString driverShell = this->ui->tWRennliste->item(index.row(), 0)->text(); QString carShell = this->ui->tWRennliste->item(index.row(), 1)->text(); QString driverDea = this->ui->tWRennliste->item(index.row(), 3)->text(); QString carDea = this->ui->tWRennliste->item(index.row(), 4)->text(); vector vec; vec.push_back(driverShell); vec.push_back(carShell); vec.push_back(driverDea); vec.push_back(carDea); this->instanceWindowRace->setDriverAndCar(vec); this->instanceWindowRace->prepareNextRace(); this->setSelection(index.row()); this->sendIds(); } void WindowRennliste::closeEvent(QCloseEvent * event) { if (!this->windowClose) { this->setWindowState(Qt::WindowMinimized); event->ignore(); } else { event->accept(); } } void WindowRennliste::closeRaceList() { this->windowClose = true; this->close(); // delete this; } void WindowRennliste::setSelection(int row) { for (int i = 0; i < 6; i++) { this->ui->tWRennliste->item(static_cast(this->selectedRow), i) ->setBackground(Qt::yellow); } this->selectedRow = static_cast(row); for (int i = 0; i < 6; i++) { this->ui->tWRennliste->item(row, i)->setBackground(Qt::green); } this->sendIds(); vector vec; vec.push_back( this->ui->tWRennliste->item(static_cast(this->selectedRow), 0) ->text()); vec.push_back( this->ui->tWRennliste->item(static_cast(this->selectedRow), 1) ->text()); vec.push_back( this->ui->tWRennliste->item(static_cast(this->selectedRow), 3) ->text()); vec.push_back( this->ui->tWRennliste->item(static_cast(this->selectedRow), 4) ->text()); this->instanceWindowRace->setDriverAndCar(vec); } void WindowRennliste::sendIds() { this->instanceWindowRace->setDriverAndCarId(this->getDriverAndCarId()); } void WindowRennliste::changeSelection() { if (this->selectedRow + 1 < this->tableData.size()) { for (unsigned int i = 0; i < 6; i++) { this->ui->tWRennliste ->item(static_cast(this->selectedRow), static_cast(i)) ->setBackground(Qt::gray); this->ui->tWRennliste ->item(static_cast(this->selectedRow) + 1, static_cast(i)) ->setBackground(Qt::green); } this->selectedRow += 1; this->sendIds(); vector vec; vec.push_back( this->ui->tWRennliste->item(static_cast(this->selectedRow), 0) ->text()); vec.push_back( this->ui->tWRennliste->item(static_cast(this->selectedRow), 1) ->text()); vec.push_back( this->ui->tWRennliste->item(static_cast(this->selectedRow), 3) ->text()); vec.push_back( this->ui->tWRennliste->item(static_cast(this->selectedRow), 4) ->text()); this->instanceWindowRace->setDriverAndCar(vec); } } WindowRennliste::~WindowRennliste() { delete ui; }