00001 /******************************************************************** 00002 *================== ЗАО "ННН" ========================* 00003 *********************************************************************/ 00012 /* 00013 * Support E-mail: lit-uriy@yandex.ru 00014 * Лицензия: GNU GPLv2 00015 * TabSize: 4 00016 * Encodding: UTF-8 (w/o BOM) 00017 * 00018 * 00019 * 00020 * 00021 * Log: 00022 * Date Athor Description 00023 * 00024 * 00025 ********************************************************************/ 00026 00027 00028 00029 #include <QtGui> 00030 00031 #include "ystddelegate.h" 00032 00038 YStdDelegate::YStdDelegate(TypeEditor type, QObject *parent) 00039 : QItemDelegate(parent) 00040 { 00041 typeeditor = type; 00042 } 00043 00044 00048 QWidget *YStdDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem &, 00049 const QModelIndex &) const 00050 { 00051 QWidget *editor; 00052 00053 switch(typeeditor) 00054 { 00055 case TBool: 00056 QComboBox *cmbox = new QComboBox(parent); 00057 cmbox->addItem("0"); 00058 cmbox->addItem("1"); 00059 return static_cast<QWidget *>(cmbox); 00060 case TQDateTimeEdit: 00061 editor = static_cast<QDateTimeEdit *>(new QDateTimeEdit(parent)); 00062 break; 00063 default: 00064 editor = static_cast<QSpinBox *>(new QSpinBox(parent)); 00065 } 00066 return editor; 00067 } 00068 00072 void YStdDelegate::setEditorData(QWidget *editor, 00073 const QModelIndex &index) const 00074 { 00075 QVariant data = index.model()->data(index, Qt::DisplayRole); 00076 00077 switch(typeeditor) 00078 { 00079 case TBool: 00080 QComboBox *cmbox = static_cast<QComboBox *>(editor); 00081 if (data.toInt()) 00082 { 00083 cmbox->setCurrentIndex(cmbox->findText("1")); 00084 } 00085 else 00086 { 00087 cmbox->setCurrentIndex(cmbox->findText("0")); 00088 } 00089 break; 00090 case TQDateTimeEdit: 00091 QDateTimeEdit *dtedit = static_cast<QDateTimeEdit *>(editor); 00092 00093 dtedit->setDateTime(data.toDateTime()); 00094 break; 00095 default: 00096 QSpinBox *spinBox = static_cast<QSpinBox *>(editor); 00097 spinBox->setValue(data.toInt()); 00098 } 00099 00100 00101 } 00102 00106 void YStdDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, 00107 const QModelIndex &index) const 00108 { 00109 QVariant data; 00110 00111 switch(typeeditor) 00112 { 00113 case TBool: 00114 QComboBox *cmbox = static_cast<QComboBox *>(editor); 00115 data = cmbox->currentText(); 00116 break; 00117 case TQDateTimeEdit: 00118 QDateTimeEdit *dtedit = static_cast<QDateTimeEdit *>(editor); 00119 data = dtedit->dateTime(); 00120 break; 00121 default: 00122 QSpinBox *spinBox = static_cast<QSpinBox *>(editor); 00123 spinBox->interpretText(); 00124 data = spinBox->value(); 00125 } 00126 00127 00128 model->setData(index, data, Qt::EditRole); 00129 } 00130 00131 void YStdDelegate::updateEditorGeometry(QWidget *editor, 00132 const QStyleOptionViewItem &option, const QModelIndex &/* index */) const 00133 { 00134 editor->setGeometry(option.rect); 00135 }
Copyright © 2008 ЗАО "ННН" | Trademarks | YStdDelegate_Example 1.0.0 |