/**
  @brief Initialise un widget avec une liste de parametres
  @param widget Widget parent
  @param list Liste des parametres
  @remarks Les noms des widgets doivent correspondres avec les noms de paramètres
*/
void Configurable::configToWidget(QObject* widget,ConfigParamList& list){

    //scan les elements enfants
    for(ConfigParamList::const_iterator cur = list.begin(); cur != list.end(); cur++){
        QString name = cur->first;
        QString value = cur->second->getValue();
        QWidget* child = widget->findChild<QWidget*>(name);
        if(child==0){
            QPRINT("configToWidget: "+name+" not found");
            continue;
        }

        // QLineEdit ?
        QLineEdit *lineEdit = qobject_cast<QLineEdit *>(child);
        if(lineEdit){
           lineEdit->setText(value);
           continue;
        }

        // QComboBox ?
        QComboBox *comboBox = qobject_cast<QComboBox *>(child);
        if(comboBox){
            comboBox->setCurrentIndex(comboBox->findText(value));
            continue;
         }

        // QSpinBox ?
        QSpinBox *spinBox = qobject_cast<QSpinBox *>(child);
        if(spinBox){
            spinBox->setValue(value.toInt());
            continue;
         }


        // QDoubleSpinBox ?
        QDoubleSpinBox *doubleSpinBox = qobject_cast<QDoubleSpinBox *>(child);
        if(doubleSpinBox){
            doubleSpinBox->setValue(value.toInt());
            continue;
         }

        // QTextEdit ?
        QTextEdit *textEdit = qobject_cast<QTextEdit *>(child);
        if(textEdit){
            textEdit->setPlainText(value);
            continue;
         }

        // QPlainTextEdit ?
        QPlainTextEdit *plainTextEdit = qobject_cast<QPlainTextEdit *>(child);
        if(plainTextEdit){
            plainTextEdit->setPlainText(value);
            continue;
         }

        // QTimeEdit ?
        QTimeEdit *timeEdit = qobject_cast<QTimeEdit *>(child);
        if(timeEdit){
            timeEdit->setTime(QTime::fromString(value));
            continue;
         }

        // QDateTimeEdit ?
        QDateTimeEdit *dateTimeEdit = qobject_cast<QDateTimeEdit *>(child);
        if(dateTimeEdit){
            timeEdit->setDateTime(QDateTime::fromString(value));
            continue;
         }

        // QDateEdit ?
        QDateEdit *dateEdit = qobject_cast<QDateEdit *>(child);
        if(dateEdit){
            dateEdit->setDate(QDate::fromString(value));
            continue;
         }

        // QDial ?
        QDial *dial = qobject_cast<QDial *>(child);
        if(dial){
            dial->setValue(value.toInt());
            continue;
         }

        // QSlider ?
        QSlider *slider = qobject_cast<QSlider *>(child);
        if(slider){
            slider->setValue(value.toInt());
            continue;
         }

    }

}
void CalendarDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const {
    QDate value = index.model()->data(index, Qt::EditRole).toDate();
    QDateEdit *de = static_cast<QDateEdit*>(editor);
    if (value.year() > 2007) de->setDate(value);
    else de->setDate(QDate::currentDate());
}
void DateEditDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const{
    QDate value = index.model()->data(index,Qt::DisplayRole).toDate();
    QDateEdit *date = static_cast<QDateEdit*>(editor);
    date->setDate(value);
}
Example #4
0
QWidget*  MultiDelegate::createEditor( QWidget* parent,
                                     const QStyleOptionViewItem& option,
                                     const QModelIndex& index)  const
{
    const QAbstractItemModel*  model = index.model();
    QVariant  value = model->data( index, Qt::EditRole);
    switch (value.type()) {
    case QMetaType::QTime:
    {
        QTimeEdit*  editor = new QTimeEdit( parent);
        editor->setMaximumWidth( editor->sizeHint().width());

        //// Get value snapshot into editor
        editor->setTime( value.toTime());
        return editor;
    }
    case QMetaType::QDate:
    {
        QDateEdit*  editor = new QDateEdit( parent);
        setupCalenderWidget( editor);
        editor->setMaximumWidth( editor->sizeHint().width());

        //// Get value snapshot into editor
        editor->setDate( value.toDate());
        return editor;
    }
    case QMetaType::QDateTime:
    {
        QDateTimeEdit*  editor = new QDateTimeEdit( parent);
        setupCalenderWidget( editor);
        editor->setMaximumWidth( editor->sizeHint().width());

        editor->setDateTime( value.toDateTime());
        return editor;
    }
    case QMetaType::QImage:
        // Fall throu
    case QMetaType::QPixmap:
        // Fall throu
    case QMetaType::QIcon:
    {
        PixmapViewer*  editor = new PixmapViewer( parent);
        connect( editor, SIGNAL(finished(int)), this, SLOT(closeEmittingEditor()));
        return editor;
    }
    case QMetaType::QStringList:
    {
        QVariant  varList = index.model()->data( index, ItemDataRole::EnumList);
        if (varList.isNull())  break;  // Not a enum-list, fall to std

        QListWidget*  editor = new QListWidget( parent);
        foreach (const QString& bitItemText, varList.toStringList()) {
            QListWidgetItem* bitItem = new QListWidgetItem( bitItemText, editor);
            bitItem->setFlags(bitItem->flags() | Qt::ItemIsUserCheckable);
            bitItem->setCheckState(Qt::Unchecked);
        }
        int width  = editor->sizeHintForColumn(0) + 25;
        int height = editor->sizeHintForRow(0) * editor->count() + 10;
        editor->setMinimumWidth( width);
        editor->setMaximumWidth( width);
        editor->setMinimumHeight( height);
        editor->setMaximumHeight( height);

        //// Get value snapshot into editor
        QStringList  valList = value.toStringList();
        int  itemCount = editor->count();
        for (int i = 0; i < itemCount; ++i) {
            QListWidgetItem*  bitItem = editor->item(i);
            bool  isActive = valList.contains( bitItem->text());
            bitItem->setCheckState( isActive ? Qt::Checked : Qt::Unchecked);
        }
        return editor;
    }
    case QMetaType::QString:
    {
        QVariant  varList = index.model()->data( index, ItemDataRole::EnumList);
        if (varList.isNull())  break;  // Not a enum-list, fall to std

        QComboBox*  editor = new QComboBox( parent);
        editor->setSizeAdjustPolicy(QComboBox::AdjustToContents);
        editor->addItems( varList.toStringList());
        editor->setMaximumWidth( editor->minimumSizeHint().width());

        //// Get value snapshot into editor
        editor->setCurrentIndex( editor->findText( value.toString()));
        return editor;
    }
    default:;
    }

    if (index.column() == 0) {
        emit itemEditTrigged( index);
        return 0;  // No inline editor
    }

    QWidget*  editor = QItemDelegate::createEditor( parent, option, index);

    //// Get value snapshot into editor
    QItemDelegate::setEditorData( editor, index);
    return editor;
}
Example #5
0
void tst_QItemDelegate::dateTimeEditor()
{
    QFETCH(QTime, time);
    QFETCH(QDate, date);

    QTableWidgetItem *item1 = new QTableWidgetItem;
    item1->setData(Qt::DisplayRole, time);

    QTableWidgetItem *item2 = new QTableWidgetItem;
    item2->setData(Qt::DisplayRole, date);

    QTableWidgetItem *item3 = new QTableWidgetItem;
    item3->setData(Qt::DisplayRole, QDateTime(date, time));

    QTableWidget widget(1, 3);
    widget.setItem(0, 0, item1);
    widget.setItem(0, 1, item2);
    widget.setItem(0, 2, item3);
    widget.show();

    widget.editItem(item1);

    QTestEventLoop::instance().enterLoop(1);

    QTimeEdit *timeEditor = qFindChild<QTimeEdit *>(widget.viewport());
    QVERIFY(timeEditor);
    QCOMPARE(timeEditor->time(), time);
    // The data must actually be different in order for the model
    // to be updated.
    timeEditor->setTime(time.addSecs(60));

    widget.clearFocus();
    qApp->setActiveWindow(&widget);
    widget.setFocus();
    widget.editItem(item2);

    QTestEventLoop::instance().enterLoop(1);

    QDateEdit *dateEditor = qFindChild<QDateEdit *>(widget.viewport());
    QVERIFY(dateEditor);
    QCOMPARE(dateEditor->date(), date);
    dateEditor->setDate(date.addDays(60));

    widget.clearFocus();
    widget.setFocus();
    widget.editItem(item3);

    QTestEventLoop::instance().enterLoop(1);

    QList<QDateTimeEdit *> dateTimeEditors = widget.findChildren<QDateTimeEdit *>();
    QDateTimeEdit *dateTimeEditor = 0;
    foreach(dateTimeEditor, dateTimeEditors)
        if (dateTimeEditor->metaObject()->className() == QLatin1String("QDateTimeEdit"))
            break;
    QVERIFY(dateTimeEditor);
    QCOMPARE(dateTimeEditor->date(), date);
    QCOMPARE(dateTimeEditor->time(), time);
    dateTimeEditor->setTime(time.addSecs(600));
    widget.clearFocus();

    QVERIFY(item1->data(Qt::EditRole).userType() == QMetaType::QTime);
    QVERIFY(item2->data(Qt::EditRole).userType() == QMetaType::QDate);
    QVERIFY(item3->data(Qt::EditRole).userType() == QMetaType::QDateTime);
}