diff --git a/src/Charts/RideEditor.cpp b/src/Charts/RideEditor.cpp index 6bdc0d807..5b3734afb 100644 --- a/src/Charts/RideEditor.cpp +++ b/src/Charts/RideEditor.cpp @@ -1298,6 +1298,139 @@ void CellDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, } } +//---------------------------------------------------------------------- +// XData Cell item delegate +//---------------------------------------------------------------------- + +// Cell editor - item delegate +XDataCellDelegate::XDataCellDelegate(XDataEditor *xdataEditor, QObject *parent) : QItemDelegate(parent), xdataEditor(xdataEditor) {} + +// setup editor for edit of field!! +QWidget *XDataCellDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &index) const +{ + // what are we editing? + switch(index.column()) { + + case 0 : + { + + QTimeEdit *timeEdit = new QTimeEdit(parent); + timeEdit->setDisplayFormat ("hh:mm:ss.zzz"); + connect(timeEdit, SIGNAL(editingFinished()), this, SLOT(commitAndCloseEditor())); + return timeEdit; + + } + break; + + default: + { + QDoubleSpinBox *valueEdit = new QDoubleSpinBox(parent); + connect(valueEdit, SIGNAL(editingFinished()), this, SLOT(commitAndCloseEditor())); + return valueEdit; + } + break; + } +} + +// user hit tab or return so save away the data to our model +void XDataCellDelegate::commitAndCloseEditor() +{ + QDoubleSpinBox *editor = qobject_cast(sender()); + emit commitData(editor); + emit closeEditor(editor); +} + +// We don't set anything because the data is saved within the view not the model! +void XDataCellDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const +{ + // what are we editing? + switch(index.column()) { + + case 0 : + { + + int seconds, msecs; + secsMsecs(index.model()->data(index, Qt::DisplayRole).toDouble(), seconds, msecs); + + QTime value = QTime(0,0,0,0).addSecs(seconds).addMSecs(msecs); + QTimeEdit *timeEdit = qobject_cast(editor); + timeEdit->setTime(value); + + } + break; + + default: + { + QDoubleSpinBox *valueEdit = qobject_cast(editor); + double value = index.model()->data(index, Qt::DisplayRole).toString().toDouble(); + valueEdit->setValue(value); + } + } +} + +void XDataCellDelegate::updateEditorGeometry(QWidget *editor, + const QStyleOptionViewItem &option, + const QModelIndex &/*index*/) const +{ + if (editor) editor->setGeometry(option.rect); +} + +// We don't set anything because the data is saved within the view not the model! +void XDataCellDelegate::setModelData(QWidget *editor, QAbstractItemModel *, const QModelIndex &index) const +{ + switch(index.column()) { + + case 0 : + { + double seconds; + QTime midnight(0,0,0,0); + QTimeEdit *timeEdit = qobject_cast(editor); + seconds = (double)midnight.secsTo(timeEdit->time()) + (double)timeEdit->time().msec() / (double)1000.00; + xdataEditor->setModelValue(index.row(), index.column(), seconds); + + } + break; + + default: + { + QDoubleSpinBox *valueEdit = qobject_cast(editor); + QString value = QString("%1").arg(valueEdit->value()); + xdataEditor->setModelValue(index.row(), index.column(), valueEdit->value()); + } + break; + } +} + +// anomalies are underlined in red, otherwise straight paintjob +void XDataCellDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, + const QModelIndex &index) const +{ + // what are we editing? + QString value; + switch(index.column()) { + + case 0: + { + int seconds, msecs; + secsMsecs(index.model()->data(index, Qt::DisplayRole).toDouble(), seconds, msecs); + value = QTime(0,0,0,0).addSecs(seconds).addMSecs(msecs).toString("hh:mm:ss.zzz"); + } + break; + + default: + { + value = index.model()->data(index, Qt::DisplayRole).toString(); + } + break; + } + + // normal render + QStyleOptionViewItem myOption = option; + myOption.displayAlignment = Qt::AlignLeft | Qt::AlignVCenter; + drawDisplay(painter, myOption, myOption.rect, value); + drawFocus(painter, myOption, myOption.rect); +} + //---------------------------------------------------------------------- // handle GC Signals //---------------------------------------------------------------------- @@ -2798,6 +2931,7 @@ XDataEditor::XDataEditor(QWidget *parent, QString xdata) : QWidget(parent), xdat table->setContextMenuPolicy(Qt::CustomContextMenu); table->setSelectionMode(QAbstractItemView::ContiguousSelection); table->setGridStyle(Qt::NoPen); + table->setItemDelegate(new XDataCellDelegate(this)); QVBoxLayout *mainLayout = new QVBoxLayout(this); setContentsMargins(0,0,0,0); @@ -2843,3 +2977,9 @@ void XDataEditor::setRideItem(RideItem *item) { model->setRide(item->ride()); } + +void +XDataEditor::setModelValue(int row, int col, double value) +{ + model->setValue(row, col, value); +} diff --git a/src/Charts/RideEditor.h b/src/Charts/RideEditor.h index 7575da2f3..7cd2c4a3f 100644 --- a/src/Charts/RideEditor.h +++ b/src/Charts/RideEditor.h @@ -42,6 +42,7 @@ class EditorData; class CellDelegate; +class XDataCellDelegate; class RideModel; class FindDialog; class AnomalyDialog; @@ -224,6 +225,40 @@ private: }; +class XDataCellDelegate : public QItemDelegate +{ + Q_OBJECT + G_OBJECT + + +public: + XDataCellDelegate(XDataEditor *, QObject *parent = 0); + + // setup editor in the cell - QDoubleSpinBox + QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const; + + // Fetch data from model ready for editing + void setEditorData(QWidget *editor, const QModelIndex &index) const; + + // Save data back to model after editing + void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const; + + // override stanard painter to underline anomalies in red + void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const; + + void updateEditorGeometry(QWidget *editor, + const QStyleOptionViewItem &option, + const QModelIndex &index) const; + +private slots: + + void commitAndCloseEditor(); + +private: + XDataEditor *xdataEditor; + +}; + class AnomalyDialog : public QWidget { Q_OBJECT @@ -359,6 +394,8 @@ class XDataEditor : public QWidget { Q_OBJECT + friend class ::XDataCellDelegate; + public: XDataEditor(QWidget *parent, QString xdata); void setRideItem(RideItem*); @@ -366,11 +403,12 @@ class XDataEditor : public QWidget public slots: void configChanged(); - private: + protected: QString xdata; XDataTableModel *model; QTableView *table; + void setModelValue(int row, int column, double value); }; #endif // _GC_RideEditor_h diff --git a/src/FileIO/XDataTableModel.cpp b/src/FileIO/XDataTableModel.cpp index d94d64060..20c7de1ee 100644 --- a/src/FileIO/XDataTableModel.cpp +++ b/src/FileIO/XDataTableModel.cpp @@ -224,7 +224,7 @@ XDataTableModel::removeColumns (int , int , const QModelIndex &) void XDataTableModel::setValue(int row, int column, double value) { - //ride->command->setPointValue(row, headingsType[column], value); + ride->command->setXDataPointValue(xdata, row, column, value); } double