00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "kfilemetainfowidget.h"
00020
00021 #include <ktextedit.h>
00022 #include <klocale.h>
00023 #include <knuminput.h>
00024 #include <kcombobox.h>
00025 #include <klineedit.h>
00026 #include <kstringvalidator.h>
00027 #include <kdebug.h>
00028
00029 #include <QtGui/QLabel>
00030 #include <QtGui/QCheckBox>
00031 #include <QtGui/QDoubleSpinBox>
00032 #include <QtGui/QDateEdit>
00033 #include <QtGui/QPixmap>
00034 #include <QtGui/QImage>
00035 #include <QtGui/QLayout>
00036 #include <QtGui/QSizePolicy>
00037 #include <QtGui/QDoubleValidator>
00038
00039 class KFileMetaInfoWidgetPrivate
00040 {
00041 public:
00042 KFileMetaInfoWidgetPrivate(KFileMetaInfoWidget *qq)
00043 : q(qq)
00044 {
00045 }
00046
00047 void init(KFileMetaInfoItem item, KFileMetaInfoWidget::Mode mode);
00048
00049 KFileMetaInfoWidget *q;
00050 QVariant m_value;
00051 KFileMetaInfoItem m_item;
00052 QWidget *m_widget;
00053 QValidator *m_validator;
00054 bool m_dirty : 1;
00055 };
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067 KFileMetaInfoWidget::KFileMetaInfoWidget(KFileMetaInfoItem item,
00068 QValidator* val,
00069 QWidget* parent )
00070 : QWidget(parent), d(new KFileMetaInfoWidgetPrivate(this))
00071 {
00072 d->m_value = item.value();
00073 d->m_item = item;
00074 d->m_validator = val;
00075 d->init(item, ReadWrite);
00076 }
00077
00078 KFileMetaInfoWidget::KFileMetaInfoWidget(KFileMetaInfoItem item,
00079 Mode mode,
00080 QValidator* val,
00081 QWidget* parent)
00082 : QWidget(parent), d(new KFileMetaInfoWidgetPrivate(this))
00083 {
00084 d->m_value = item.value();
00085 d->m_item = item;
00086 d->m_validator = val;
00087 d->init(item, mode);
00088 }
00089
00090 void KFileMetaInfoWidgetPrivate::init(KFileMetaInfoItem item, KFileMetaInfoWidget::Mode mode)
00091 {
00092 kDebug(7033) << "*** item " << m_item.name()
00093 << " is a " << m_value.typeName() << endl;
00094
00095 if (m_item.isEditable() && !(mode & KFileMetaInfoWidget::ReadOnly))
00096 m_widget = q->makeWidget();
00097 else
00098 switch (m_value.type())
00099 {
00100 case QVariant::Image :
00101 m_widget = new QLabel(q);
00102 m_widget->setObjectName(QLatin1String("info image"));
00103 static_cast<QLabel*>(m_widget)->setPixmap(QPixmap::fromImage(m_value.value<QImage>()));
00104 break;
00105 case QVariant::Pixmap :
00106 m_widget = new QLabel(q);
00107 m_widget->setObjectName(QLatin1String("info pixmap"));
00108 static_cast<QLabel*>(m_widget)->setPixmap(m_value.value<QPixmap>());
00109 break;
00110 default:
00111 m_widget = new QLabel(item.name(), q);
00112 m_widget->setObjectName(QLatin1String("info label"));
00113 }
00114
00115 QHBoxLayout* lay = new QHBoxLayout(q);
00116 lay->setMargin(0);
00117 lay->addWidget(m_widget);
00118
00119 QSizePolicy sp = q->sizePolicy();
00120 sp.setVerticalPolicy(QSizePolicy::Minimum);
00121 q->setSizePolicy(sp);
00122 }
00123
00124 KFileMetaInfoWidget::~KFileMetaInfoWidget()
00125 {
00126 delete d;
00127 }
00128
00129 bool KFileMetaInfoWidget::apply()
00130 {
00131 return d->m_item.isEditable() && d->m_item.setValue(d->m_value);
00132 }
00133
00134 void KFileMetaInfoWidget::setValue(const QVariant &value)
00135 {
00136 d->m_value = value;
00137 }
00138
00139 QVariant KFileMetaInfoWidget::value() const
00140 {
00141 return d->m_value;
00142 }
00143
00144 QValidator* KFileMetaInfoWidget::validator() const
00145 {
00146 return d->m_validator;
00147 }
00148
00149 KFileMetaInfoItem KFileMetaInfoWidget::item() const
00150 {
00151 return d->m_item;
00152 }
00153
00154 QWidget* KFileMetaInfoWidget::makeWidget()
00155 {
00156 QString valClass;
00157 QWidget* w;
00158
00159 switch (d->m_value.type()) {
00160 case QVariant::Invalid:
00161
00162 w = new QLabel(i18n("<Error>"), this);
00163 w->setObjectName(QLatin1String("label"));
00164 break;
00165
00166 case QVariant::Int:
00167 case QVariant::UInt:
00168 w = makeIntWidget();
00169 break;
00170
00171 case QVariant::Bool:
00172 w = makeBoolWidget();
00173 break;
00174
00175 case QVariant::Double:
00176 w = makeDoubleWidget();
00177 break;
00178
00179
00180 case QVariant::Date:
00181 w = makeDateWidget();
00182 break;
00183
00184 case QVariant::Time:
00185 w = makeTimeWidget();
00186 break;
00187
00188 case QVariant::DateTime:
00189 w = makeDateTimeWidget();
00190 break;
00191
00192 #if 0
00193 case QVariant::Size:
00194 case QVariant::String:
00195 case QVariant::List:
00196 case QVariant::Map:
00197 case QVariant::StringList:
00198 case QVariant::Font:
00199 case QVariant::Pixmap:
00200 case QVariant::Brush:
00201 case QVariant::Rect:
00202 case QVariant::Color:
00203 case QVariant::Palette:
00204 case QVariant::ColorGroup:
00205 case QCoreVariant::Icon:
00206 case QVariant::Point:
00207 case QVariant::Image:
00208 case QVariant::CString:
00209 case QVariant::PointArray:
00210 case QVariant::Region:
00211 case QVariant::Bitmap:
00212 case QVariant::Cursor:
00213 case QVariant::ByteArray:
00214 case QVariant::BitArray:
00215 case QVariant::SizePolicy:
00216 case QVariant::KeySequence:
00217 #endif
00218 default:
00219 w = makeStringWidget();
00220 }
00221
00222 kDebug(7033) << "*** item " << d->m_item.name()
00223 << "is a " << d->m_item.value().typeName() << endl;
00224 if (d->m_validator)
00225 kDebug(7033) << " and validator is a "
00226 << d->m_validator->metaObject()->className() << endl;
00227
00228 kDebug(7033) << "*** created a " << w->metaObject()->className()
00229 << " for it\n";
00230
00231 return w;
00232 }
00233
00234
00235
00236
00237
00238 QWidget* KFileMetaInfoWidget::makeBoolWidget()
00239 {
00240 QCheckBox* cb = new QCheckBox(this);
00241 cb->setObjectName(QLatin1String("metainfo bool widget"));
00242 cb->setChecked(d->m_item.value().toBool());
00243 connect(cb, SIGNAL(toggled(bool)), this, SLOT(slotChanged(bool)));
00244 return cb;
00245 }
00246
00247 QWidget* KFileMetaInfoWidget::makeIntWidget()
00248 {
00249 QSpinBox* sb = new QSpinBox(this);
00250 sb->setObjectName(QLatin1String("metainfo integer widget"));
00251 sb->setValue(d->m_item.value().toInt());
00252
00253 if (d->m_validator) {
00254 if (QIntValidator* iv = qobject_cast<QIntValidator*>(d->m_validator)) {
00255 sb->setMinimum(iv->bottom());
00256 sb->setMaximum(iv->top());
00257 }
00258
00259
00260 }
00261
00262
00263 if (d->m_item.properties().type() == QVariant::UInt)
00264 sb->setMinimum(qMax(sb->minimum(), 0));
00265
00266 connect(sb, SIGNAL(valueChanged(int)), this, SLOT(slotChanged(int)));
00267 return sb;
00268 }
00269
00270 QWidget* KFileMetaInfoWidget::makeDoubleWidget()
00271 {
00272 double value = d->m_item.value().toDouble();
00273 KDoubleNumInput* dni = new KDoubleNumInput(qMin(0.0,value),qMax(0.0,value),value,this,0.01,2);
00274
00275
00276 if (d->m_validator) {
00277 if (QDoubleValidator* dv = qobject_cast<QDoubleValidator*>(d->m_validator)) {
00278 dni->setMinimum(dv->bottom());
00279 dni->setMaximum(dv->top());
00280 }
00281 reparentValidator(dni, d->m_validator);
00282 }
00283
00284 connect(dni, SIGNAL(valueChanged(double)), this, SLOT(slotChanged(double)));
00285 return dni;
00286 }
00287
00288 QWidget* KFileMetaInfoWidget::makeStringWidget()
00289 {
00290 if (KStringListValidator* val = qobject_cast<KStringListValidator*>(d->m_validator)) {
00291 KComboBox* b = new KComboBox(true, this);
00292 b->addItems(val->stringList());
00293 int i = b->findText(d->m_item.value().toString());
00294 if (i != -1)
00295 b->setCurrentIndex(i);
00296 else
00297 b->setEditText(d->m_item.value().toString());
00298 connect(b, SIGNAL(activated(const QString &)), this, SLOT(slotComboChanged(const QString &)));
00299 b->setValidator(val);
00300 reparentValidator(b, val);
00301 return b;
00302 }
00303
00304 if (d->m_item.properties().attributes() & PredicateProperties::MultiLine) {
00305 KTextEdit *edit = new KTextEdit( this );
00306 edit->setAcceptRichText(false);
00307 edit->setPlainText(d->m_item.value().toString());
00308 connect( edit, SIGNAL( textChanged() ),
00309 this, SLOT( slotMultiLineEditChanged() ));
00310
00311 if (d->m_validator)
00312 reparentValidator(edit, d->m_validator);
00313 return edit;
00314 }
00315
00316 KLineEdit* e = new KLineEdit(d->m_item.value().toString(), this);
00317 if (d->m_validator) {
00318 e->setValidator(d->m_validator);
00319 reparentValidator(e, d->m_validator);
00320 }
00321 connect(e, SIGNAL(textChanged(const QString&)),
00322 this, SLOT(slotLineEditChanged(const QString&)));
00323 return e;
00324 }
00325
00326 QWidget* KFileMetaInfoWidget::makeDateWidget()
00327 {
00328 QWidget *e = new QDateEdit(d->m_item.value().toDate(), this);
00329 connect(e, SIGNAL(valueChanged(const QDate&)),
00330 this, SLOT(slotDateChanged(const QDate&)));
00331 return e;
00332 }
00333
00334 QWidget* KFileMetaInfoWidget::makeTimeWidget()
00335 {
00336 return new QTimeEdit(d->m_item.value().toTime(), this);
00337 }
00338
00339 QWidget* KFileMetaInfoWidget::makeDateTimeWidget()
00340 {
00341 return new QDateTimeEdit(d->m_item.value().toDateTime(), this);
00342 }
00343
00344 void KFileMetaInfoWidget::reparentValidator( QWidget *widget,
00345 QValidator *validator )
00346 {
00347 if ( !validator->parent() )
00348 validator->setParent( widget );
00349 }
00350
00351
00352
00353
00354
00355 void KFileMetaInfoWidget::slotChanged(bool value)
00356 {
00357 Q_ASSERT(qobject_cast<QComboBox*>(d->m_widget));
00358 d->m_value = QVariant(value);
00359 emit valueChanged(d->m_value);
00360 d->m_dirty = true;
00361 }
00362
00363 void KFileMetaInfoWidget::slotChanged(int value)
00364 {
00365 Q_ASSERT(qobject_cast<QSpinBox*>(d->m_widget));
00366 d->m_value = QVariant(value);
00367 emit valueChanged(d->m_value);
00368 d->m_dirty = true;
00369 }
00370
00371 void KFileMetaInfoWidget::slotChanged(double value)
00372 {
00373 Q_ASSERT(qobject_cast<KDoubleNumInput*>(d->m_widget));
00374 d->m_value = QVariant(value);
00375 emit valueChanged(d->m_value);
00376 d->m_dirty = true;
00377 }
00378
00379 void KFileMetaInfoWidget::slotComboChanged(const QString &value)
00380 {
00381 Q_ASSERT(qobject_cast<KComboBox*>(d->m_widget));
00382 d->m_value = QVariant(value);
00383 emit valueChanged(d->m_value);
00384 d->m_dirty = true;
00385 }
00386
00387 void KFileMetaInfoWidget::slotLineEditChanged(const QString& value)
00388 {
00389 Q_ASSERT(qobject_cast<KLineEdit*>(d->m_widget));
00390 d->m_value = QVariant(value);
00391 emit valueChanged(d->m_value);
00392 d->m_dirty = true;
00393 }
00394
00395
00396 void KFileMetaInfoWidget::slotMultiLineEditChanged()
00397 {
00398 Q_ASSERT(qobject_cast<KTextEdit*>(d->m_widget));
00399 d->m_value = QVariant(static_cast<const KTextEdit*>(sender())->toPlainText());
00400 emit valueChanged(d->m_value);
00401 d->m_dirty = true;
00402 }
00403
00404 void KFileMetaInfoWidget::slotDateChanged(const QDate& value)
00405 {
00406 Q_ASSERT(qobject_cast<QDateEdit*>(d->m_widget));
00407 d->m_value = QVariant(value);
00408 emit valueChanged(d->m_value);
00409 d->m_dirty = true;
00410 }
00411
00412 void KFileMetaInfoWidget::slotTimeChanged(const QTime& value)
00413 {
00414 Q_ASSERT(qobject_cast<QTimeEdit*>(d->m_widget));
00415 d->m_value = QVariant(value);
00416 emit valueChanged(d->m_value);
00417 d->m_dirty = true;
00418 }
00419
00420 void KFileMetaInfoWidget::slotDateTimeChanged(const QDateTime& value)
00421 {
00422 Q_ASSERT(qobject_cast<QDateTimeEdit*>(d->m_widget));
00423 d->m_value = QVariant(value);
00424 emit valueChanged(d->m_value);
00425 d->m_dirty = true;
00426 }
00427
00428 #include "kfilemetainfowidget.moc"