Subversion Repositories svn1-original

Rev

Rev 335 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*============================================================================
**
**  Project/Product : 
**  Filename        : timedelegate.h
**  Author(s)       : DDP
**
**  Description     : A delgated widget to assist in the editing of time items
**                    within a table
**
**
***==========================================================================*/

#include <QtGui>
#include "timedelegate.h"

/*----------------------------------------------------------------------------
** FUNCTION           : timeDelegate
**
** DESCRIPTION        : Construct a new object
**
**                      A delegate that allows the user to change integer
**                      values from the model using a date time widget.
**
** INPUTS             : parent      - Parent object
**
** RETURNS            :
**
----------------------------------------------------------------------------*/

timeDelegate::timeDelegate(QObject *parent)
    : QStyledItemDelegate(parent)
{
}

/*----------------------------------------------------------------------------
** FUNCTION           : createEditor
**
** DESCRIPTION        : Create an editor widget
**
**
** INPUTS             : parent
**                      option
**                      index
**
** RETURNS            : A QTimeEditor widget
**
----------------------------------------------------------------------------*/


QWidget *timeDelegate::createEditor(QWidget *parent,
    const QStyleOptionViewItem &/* option */,
    const QModelIndex &/* index */) const
{
    QTimeEdit *editor = new QTimeEdit(parent);
    editor->setDisplayFormat("hh:mm:ss");

    return editor;
}

/*----------------------------------------------------------------------------
** FUNCTION           : setEditorData
**
** DESCRIPTION        : Inserts model data into the editor widget
**
**
** INPUTS             : editor      - Ref to editor widget
**                      index       -
**
** RETURNS            :
**
----------------------------------------------------------------------------*/

void timeDelegate::setEditorData(QWidget *editor,
                                    const QModelIndex &index) const
{
    QTime value = index.model()->data(index, Qt::EditRole).toTime();

    QTimeEdit *timeEdit = static_cast<QTimeEdit*>(editor);
    timeEdit->setTime(value);
}

/*----------------------------------------------------------------------------
** FUNCTION           : setModelData
**
** DESCRIPTION        : Extract data from the editor widget and update the model
**
**
** INPUTS             : editor      - Target widget
**                      model       - Current model
**                      index       - Index into model
**
** RETURNS            :
**
----------------------------------------------------------------------------*/

void timeDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                   const QModelIndex &index) const
{
    QTimeEdit *timeEdit = static_cast<QTimeEdit*>(editor);
    timeEdit->interpretText();
    QTime value = timeEdit->time();

    //model->setData(index, "--:--:--", Qt::DisplayRole);
    model->setData(index, value, Qt::EditRole);
}

/*----------------------------------------------------------------------------
** FUNCTION           : updateEditorGeometry
**
** DESCRIPTION        : Updates the geometry of the editor widget
**
**
** INPUTS             : editor      - Target widget
**                      option      -
*                       index
**
** RETURNS            :
**
----------------------------------------------------------------------------*/

void timeDelegate::updateEditorGeometry(QWidget *editor,
    const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
    editor->setGeometry(option.rect);
}

/*----------------------------------------------------------------------------
** FUNCTION           : displayText
**
** DESCRIPTION        : Control the manner in which the text is displayed
**
**
** INPUTS             : value           - Data from the model
**                      locale          - Current local
**
** RETURNS            : Text to be displayed
**
----------------------------------------------------------------------------*/


QString timeDelegate::displayText ( const QVariant & value, const QLocale & locale ) const
{
    if ( value.toTime().isValid())
    {
        return (value.toTime().toString("hh:mm:ss"));
    }
    return (QString("--:--:--"));
}