Rev 194 | Rev 225 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
#include "qmdisqualified.h"#include "consts.h"#include "structs.h"#include "proto.h"#include <QHeaderView>#include <QPushButton>qmDisqualified::qmDisqualified(QWidget *parent) :QWidget(parent){inPopulate = false;dirty = true;resize ( 600,400);QVBoxLayout *verticalLayout = new QVBoxLayout(this);verticalLayout->setContentsMargins(0, 0, 0, 0);QGroupBox *groupBox = new QGroupBox("Disqualified and Non Equestrian");verticalLayout->addWidget(groupBox);QVBoxLayout *verticalLayout2 = new QVBoxLayout(groupBox);tableWidget = new QTableWidget(groupBox);tableWidget->setObjectName(QString::fromUtf8("tableWidget"));//tableWidget->setGeometry(QRect(10, 20, 501, 421));tableWidget->setAlternatingRowColors(true);tableWidget->horizontalHeader()->setProperty("showSortIndicator", QVariant(true));tableWidget->verticalHeader()->setDefaultSectionSize(20);tableWidget->verticalHeader()->setProperty("showSortIndicator", QVariant(false));verticalLayout2->addWidget(tableWidget);buttonBox = new QDialogButtonBox();pb_restore = buttonBox->addButton("Restore",QDialogButtonBox::ActionRole );pb_save = buttonBox->addButton("Save",QDialogButtonBox::ActionRole );verticalLayout->addWidget(buttonBox);connect(pb_save, SIGNAL(clicked()), this, SLOT(save()) );connect(pb_restore, SIGNAL(clicked()), this, SLOT(cancel()) );connect(tableWidget, SIGNAL(itemChanged(QTableWidgetItem*)), this, SLOT(tableItemChanged(QTableWidgetItem *)));updateChanged (false);}qmDisqualified::~qmDisqualified(){}void qmDisqualified::save(void){team_type team_buf;for ( int ii = 0; ii < tableWidget->rowCount(); ii++){QTableWidgetItem *item;item = tableWidget->item(ii, 0);if ( item ){if ( item->data(Qt::UserRole + 1).toBool() ){int team = item->data(Qt::EditRole).toInt();if ( team ){qDebug("Detected change:%d", team);g_record( team, &team_buf );item = tableWidget->item(ii,2);team_buf.flags.disqualified = ( item->checkState () == Qt::Checked);item = tableWidget->item(ii,3);team_buf.flags.non_equestrian = ( item->checkState () == Qt::Checked);//TODO: If nonEquestrian, then also disqualified ?? Original is.item = tableWidget->item(ii,4);team_buf.flags.valid = ( item->checkState () == Qt::Checked);put_team_record( team, &team_buf );}}}}populate();}void qmDisqualified::cancel(void){populate();}void qmDisqualified::populate(void){team_type team_buf;inPopulate = TRUE;/*** Delete existing entries in the table*/tableWidget->clearContents();tableWidget->setRowCount(config.max_team+1);tableWidget->setColumnCount(6);tableWidget->setSortingEnabled(FALSE);/*** Scan all the team data*/for ( int team = config.min_team; team <= config.max_team; team++){tableWidget->hideRow ( team );if( valid_field( team ) ){g_record( team, &team_buf );if( team_buf.flags.valid || true ){qmDisqualifiedItem *item;tableWidget->showRow( team );item = new qmDisqualifiedItem(team);item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);tableWidget->setItem(team, 0, item );item = new qmDisqualifiedItem(team_buf.name);item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);tableWidget->setItem(team, 1, item );tableWidget->setItem(team, 2, new qmDisqualifiedItem("Disq", team_buf.flags.disqualified, team) );tableWidget->setItem(team, 3, new qmDisqualifiedItem("Non Equest",team_buf.flags.non_equestrian,team) );tableWidget->setItem(team, 4, new qmDisqualifiedItem("Enable", team_buf.flags.valid,team) );item = new qmDisqualifiedItem("Bad Times", team_buf.flags.bad_times);item->setFlags(Qt::ItemIsSelectable/* | Qt::ItemIsEnabled*/);tableWidget->setItem(team, 5, item );}}}tableWidget->sortByColumn(0,Qt::AscendingOrder);tableWidget->setSortingEnabled(TRUE);tableWidget->resizeColumnsToContents();inPopulate = FALSE;updateChanged(false);}void qmDisqualified::tableItemChanged(QTableWidgetItem *item){if ( !inPopulate ){if ( item->column() ){qDebug ("DataChanged");item->setData(Qt::BackgroundRole, QBrush(QColor(255,0,0,50)));// Mark first item in row to help detect changesint row = item->row();item->tableWidget()->item(row,0)->setData(Qt::UserRole + 1, true );item->tableWidget()->item(row,0)->setData(Qt::BackgroundRole, QBrush(QColor(255,0,0,50)));}updateChanged(true);}}void qmDisqualified::updateChanged(bool newDirty){if (newDirty != dirty){dirty = newDirty;if (dirty){pb_save->setEnabled(true);pb_save->setStyleSheet("background-color: rgb(255, 0, 0);");}else{pb_save->setEnabled(false);pb_save->setStyleSheet("");}}}void qmDisqualified::showEvent ( QShowEvent * event ){qDebug("qmDisqualified::showEvent");if ( ! event->spontaneous() && !dirty ){populate();}}qmDisqualifiedItem::qmDisqualifiedItem ( const QString & text, int type ) : QTableWidgetItem(text, type+QTableWidgetItem::UserType){number = 0;}qmDisqualifiedItem::qmDisqualifiedItem ( int value, int type ) : QTableWidgetItem(type+QTableWidgetItem::UserType){number = value;setData(0,value);}qmDisqualifiedItem::qmDisqualifiedItem ( const QString & text, bool checked, int num, int type ) : QTableWidgetItem(text, type+QTableWidgetItem::UserType){setCheckState(checked ? Qt::Checked : Qt::Unchecked);number = num;}// Special sorting, based on type// type == 0 : Text// type == 1 : Number// type == 2 : Checked//bool qmDisqualifiedItem::operator< ( const QTableWidgetItem & other ) const{const qmDisqualifiedItem * other_widget = dynamic_cast<const qmDisqualifiedItem*>(&other);if (other_widget && other_widget->type() == QTableWidgetItem::UserType + 2 ){if (other_widget->checkState() == checkState() ){// qDebug ("Same(%d): %d : %d", other_widget->number < number, other_widget->number, number);if (this->tableWidget()->horizontalHeader()->sortIndicatorOrder() != Qt::AscendingOrder ){return other_widget->number < number;}else{return number < other_widget->number;}}return (other_widget->checkState() < checkState() );}return ( QTableWidgetItem::operator< (other) );}