Logo Search packages:      
Sourcecode: webissues version File versions  Download package

valueeditors.cpp

/**************************************************************************
* This file is part of the WebIssues program
* Copyright (C) 2006 Michał Męciński
* Copyright (C) 2007 WebIssues Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
**************************************************************************/

#include "valueeditors.h"

#include <QVariant>
#include <QLineEdit>
#include <QComboBox>
#include <QSpinBox>
#include <QDoubleSpinBox>
#include <QDateEdit>
#include <QDateTimeEdit>

#include "data/rdb/tableiterators.h"
#include "data/datamanager.h"
#include "data/rowhelpers.h"
#include "definitioninfo.h"
#include "datetimehelper.h"

using namespace WebIssues;

00030 TextValueEditor::TextValueEditor( const DefinitionInfo& info, QObject* parent, QWidget* parentWidget ) :
    AbstractValueEditor( parent )
{
    QLineEdit* edit = new QLineEdit( parentWidget );

    QVariant maxLength = info.metadata( "max-length" );
    if ( maxLength.isValid() )
        edit->setMaxLength( maxLength.toInt() );
    else
        edit->setMaxLength( 80 );

    setWidget( edit );
}

00044 TextValueEditor::~TextValueEditor()
{
}

00048 void TextValueEditor::setValue( const QString& value )
{
    QLineEdit* edit = (QLineEdit*)widget();

    edit->setText( value );
}

00055 QString TextValueEditor::value()
{
    QLineEdit* edit = (QLineEdit*)widget();

    QString text = edit->text().trimmed();
    edit->setText( text );

    return text;
}

00065 EnumValueEditor::EnumValueEditor( const DefinitionInfo& info, QObject* parent, QWidget* parentWidget ) :
    AbstractValueEditor( parent )
{
    QVariant editable = info.metadata( "editable" );
    bool isEditable = editable.isValid() ? editable.toBool() : false;

    QComboBox* combo = new QComboBox( parentWidget );
    combo->setEditable( isEditable );

    QVariant items = info.metadata( "items" );
    combo->addItems( items.toStringList() );

    if ( isEditable )
        combo->lineEdit()->setMaxLength( 80 );

    setWidget( combo );
}

00083 EnumValueEditor::~EnumValueEditor()
{
}

00087 void EnumValueEditor::setValue( const QString& value )
{
    QComboBox* combo = (QComboBox*)widget();

    int index = combo->findText( value );

    if ( index >= 0 )
        combo->setCurrentIndex( index );

    if ( combo->isEditable() )
        combo->setEditText( value );
}

00100 QString EnumValueEditor::value()
{
    QComboBox* combo = (QComboBox*)widget();

    QString text = combo->currentText().trimmed();

    if ( combo->isEditable() )
        combo->setEditText( text );

    return text;
}

00112 NumericValueEditor::NumericValueEditor( const DefinitionInfo& info, QObject* parent, QWidget* parentWidget ) :
    AbstractValueEditor( parent )
{
    QVariant decimal = info.metadata( "decimal" );
    m_decimal = decimal.isValid() ? decimal.toInt() : 0;

    if ( m_decimal > 0 ) {
        double step = 1;
        for ( int i = 0; i < m_decimal; i++ )
            step *= 0.1;

        double range = 1e12 * step;

        double min;
        QVariant minValue = info.metadata( "min-value" );
        if ( minValue.isValid() ) {
            min = minValue.toDouble();
            if ( min < -range )
                min = -range;
        } else {
            min = -range;
        }

        double max;
        QVariant maxValue = info.metadata( "max-value" );
        if ( maxValue.isValid() ) {
            max = maxValue.toDouble();
            if ( max > range )
                max = range;
        } else {
            max = range;
        }

        QDoubleSpinBox* spin = new QDoubleSpinBox( parentWidget );
        spin->setDecimals( m_decimal );
        spin->setRange( min, max );
        spin->setSingleStep( step );

        setWidget( spin );
    } else {
        QVariant minValue = info.metadata( "min-value" );
        int min = minValue.isValid() ? minValue.toInt() : INT_MIN;

        QVariant maxValue = info.metadata( "max-value" );
        int max = maxValue.isValid() ? maxValue.toInt() : INT_MAX;

        QSpinBox* spin = new QSpinBox( parentWidget );
        spin->setRange( min, max );

        setWidget( spin );
    }
}

00165 NumericValueEditor::~NumericValueEditor()
{
}

00169 void NumericValueEditor::setValue( const QString& value )
{
    if ( m_decimal > 0 ) {
        QDoubleSpinBox* spin = (QDoubleSpinBox*)widget();
        spin->setValue( value.toDouble() );
    } else {
        QSpinBox* spin = (QSpinBox*)widget();
        spin->setValue( value.toInt() );
    }
}

00180 QString NumericValueEditor::value()
{
    if ( m_decimal > 0 ) {
        QDoubleSpinBox* spin = (QDoubleSpinBox*)widget();
        return QString::number( spin->value(), 'f', m_decimal );
    } else {
        QSpinBox* spin = (QSpinBox*)widget();
        return QString::number( spin->value() );
    }
}

00191 DateTimeValueEditor::DateTimeValueEditor( const DefinitionInfo& info, QObject* parent, QWidget* parentWidget ) :
    AbstractValueEditor( parent )
{
    QVariant time = info.metadata( "time" );
    m_time = time.isValid() ? time.toBool() : false;

    if ( m_time ) {
        QDateTimeEdit* dateTime = new QDateTimeEdit( parentWidget );
        dateTime->setDisplayFormat( "yyyy-MM-dd hh:mm" );
        setWidget( dateTime );
    } else {
        QDateEdit* date = new QDateEdit( parentWidget );
        date->setDisplayFormat( "yyyy-MM-dd" );
        setWidget( date );
    }

    setValue( QString() );
}

00210 DateTimeValueEditor::~DateTimeValueEditor()
{
}

00214 void DateTimeValueEditor::setValue( const QString& value )
{
    if ( m_time ) {
        QDateTime dateTime = DateTimeHelper::parseDateTime( value );
        if ( dateTime.isNull() )
            dateTime = QDateTime::currentDateTime();

        QDateTimeEdit* dtWidget = (QDateTimeEdit*)widget();
        dtWidget->setDateTime( dateTime );
    } else {
        QDate date = DateTimeHelper::parseDate( value );
        if ( date.isNull() )
            date = QDate::currentDate();

        QDateEdit* dtWidget = (QDateEdit*)widget();
        dtWidget->setDate( date );
    }
}

00233 QString DateTimeValueEditor::value()
{
    if ( m_time ) {
        QDateTimeEdit* dtWidget = (QDateTimeEdit*)widget();
        return DateTimeHelper::formatDateTime( dtWidget->dateTime() );
    } else {
        QDateEdit* dtWidget = (QDateEdit*)widget();
        return DateTimeHelper::formatDate( dtWidget->date() );
    }
}

00244 UserValueEditor::UserValueEditor( const DefinitionInfo& info, int projectId, QObject* parent, QWidget* parentWidget ) :
    AbstractValueEditor( parent )
{
    QComboBox* combo = new QComboBox( parentWidget );

    QVariant members = info.metadata( "members" );
    bool onlyMembers = ( members.isValid() ? members.toBool() : false ) && projectId != 0;

    RDB::TableConstIterator<UserRow> it( dataManager->users() );
    QList<const UserRow*> users = localeAwareSortRows( it, &UserRow::name );

    for ( int i = 0; i < users.count(); i++ ) {
        const UserRow* user = users.at( i );
        if ( user->access() == NoAccess )
            continue;
        if ( onlyMembers && !dataManager->members()->find( user->userId(), projectId ) )
            continue;
        combo->addItem( user->name() );
    }

    setWidget( combo );
}

00267 UserValueEditor::~UserValueEditor()
{
}

00271 void UserValueEditor::setValue( const QString& value )
{
    QComboBox* combo = (QComboBox*)widget();

    int index = combo->findText( value );

    if ( index >= 0 )
        combo->setCurrentIndex( index );
}

00281 QString UserValueEditor::value()
{
    QComboBox* combo = (QComboBox*)widget();

    return combo->currentText();
}

#include "valueeditors.moc"

Generated by  Doxygen 1.6.0   Back to index