KIO
kfilemetainfowidget.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2001,2002 Rolf Magnus <ramagnus@kde.org> 00003 00004 library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License version 2 as published by the Free Software Foundation. 00007 00008 This library is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 Library General Public License for more details. 00012 00013 You should have received a copy of the GNU Library General Public License 00014 along with this library; see the file COPYING.LIB. If not, write to 00015 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00016 Boston, MA 02110-1301, USA. 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; // the value will be saved here until apply() is called 00051 KFileMetaInfoItem m_item; 00052 QWidget *m_widget; 00053 QValidator *m_validator; 00054 bool m_dirty : 1; 00055 }; 00056 00057 /* 00058 Widgets used for different types: 00059 00060 bool : QCheckBox 00061 int : QSpinBox 00062 QString : KComboBox if the validator is a KStringListValidator, else lineedit 00063 QDateTime : QDateTimeEdit 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 Q_UNUSED(item) 00093 kDebug(7033) << "*** item " << m_item.name() 00094 << " is a " << m_value.typeName() << endl; 00095 00096 if (m_item.isEditable() && !(mode & KFileMetaInfoWidget::ReadOnly)) 00097 m_widget = q->makeWidget(); 00098 else 00099 switch (m_value.type()) 00100 { 00101 case QVariant::Image : 00102 m_widget = new QLabel(q); 00103 m_widget->setObjectName(QLatin1String("info image")); 00104 static_cast<QLabel*>(m_widget)->setPixmap(QPixmap::fromImage(m_value.value<QImage>())); 00105 break; 00106 case QVariant::Pixmap : 00107 m_widget = new QLabel(q); 00108 m_widget->setObjectName(QLatin1String("info pixmap")); 00109 static_cast<QLabel*>(m_widget)->setPixmap(m_value.value<QPixmap>()); 00110 break; 00111 default: 00112 m_widget = new QLabel(m_value.toString(), q); 00113 m_widget->setObjectName(QLatin1String("info label")); 00114 } 00115 00116 QHBoxLayout* lay = new QHBoxLayout(q); 00117 lay->setMargin(0); 00118 lay->addWidget(m_widget); 00119 00120 QSizePolicy sp = q->sizePolicy(); 00121 sp.setVerticalPolicy(QSizePolicy::Minimum); 00122 q->setSizePolicy(sp); 00123 } 00124 00125 KFileMetaInfoWidget::~KFileMetaInfoWidget() 00126 { 00127 delete d; 00128 } 00129 00130 bool KFileMetaInfoWidget::apply() 00131 { 00132 return d->m_item.isEditable() && d->m_item.setValue(d->m_value); 00133 } 00134 00135 void KFileMetaInfoWidget::setValue(const QVariant &value) 00136 { 00137 d->m_value = value; 00138 } 00139 00140 QVariant KFileMetaInfoWidget::value() const 00141 { 00142 return d->m_value; 00143 } 00144 00145 QValidator* KFileMetaInfoWidget::validator() const 00146 { 00147 return d->m_validator; 00148 } 00149 00150 KFileMetaInfoItem KFileMetaInfoWidget::item() const 00151 { 00152 return d->m_item; 00153 } 00154 00155 QWidget* KFileMetaInfoWidget::makeWidget() 00156 { 00157 QString valClass; 00158 QWidget* w; 00159 00160 switch (d->m_value.type()) { 00161 case QVariant::Invalid: // no type 00162 // just make a label 00163 w = new QLabel(i18n("<Error>"), this); 00164 w->setObjectName(QLatin1String("label")); 00165 break; 00166 00167 case QVariant::Int: // an int 00168 case QVariant::UInt: // an unsigned int 00169 w = makeIntWidget(); 00170 break; 00171 00172 case QVariant::Bool: // a bool 00173 w = makeBoolWidget(); 00174 break; 00175 00176 case QVariant::Double: // a double 00177 w = makeDoubleWidget(); 00178 break; 00179 00180 00181 case QVariant::Date: // a QDate 00182 w = makeDateWidget(); 00183 break; 00184 00185 case QVariant::Time: // a QTime 00186 w = makeTimeWidget(); 00187 break; 00188 00189 case QVariant::DateTime: // a QDateTime 00190 w = makeDateTimeWidget(); 00191 break; 00192 00193 #if 0 00194 case QVariant::Size: // a QSize 00195 case QVariant::String: // a QString 00196 case QVariant::List: // a QValueList 00197 case QVariant::Map: // a QMap 00198 case QVariant::StringList: // a QStringList 00199 case QVariant::Font: // a QFont 00200 case QVariant::Pixmap: // a QPixmap 00201 case QVariant::Brush: // a QBrush 00202 case QVariant::Rect: // a QRect 00203 case QVariant::Color: // a QColor 00204 case QVariant::Palette: // a QPalette 00205 case QVariant::ColorGroup: // a QColorGroup 00206 case QCoreVariant::Icon: // a QIconSet 00207 case QVariant::Point: // a QPoint 00208 case QVariant::Image: // a QImage 00209 case QVariant::CString: // a QCString 00210 case QVariant::PointArray: // a QPointArray 00211 case QVariant::Region: // a QRegion 00212 case QVariant::Bitmap: // a QBitmap 00213 case QVariant::Cursor: // a QCursor 00214 case QVariant::ByteArray: // a QByteArray 00215 case QVariant::BitArray: // a QBitArray 00216 case QVariant::SizePolicy: // a QSizePolicy 00217 case QVariant::KeySequence: // a QKeySequence 00218 #endif 00219 default: 00220 w = makeStringWidget(); 00221 } 00222 00223 kDebug(7033) << "*** item " << d->m_item.name() 00224 << "is a " << d->m_item.value().typeName() << endl; 00225 if (d->m_validator) 00226 kDebug(7033) << " and validator is a " 00227 << d->m_validator->metaObject()->className() << endl; 00228 00229 kDebug(7033) << "*** created a " << w->metaObject()->className() 00230 << " for it\n"; 00231 00232 return w; 00233 } 00234 00235 // **************************************************************** 00236 // now the different methods to make the widgets for specific types 00237 // **************************************************************** 00238 00239 QWidget* KFileMetaInfoWidget::makeBoolWidget() 00240 { 00241 QCheckBox* cb = new QCheckBox(this); 00242 cb->setObjectName(QLatin1String("metainfo bool widget")); 00243 cb->setChecked(d->m_item.value().toBool()); 00244 connect(cb, SIGNAL(toggled(bool)), this, SLOT(slotChanged(bool))); 00245 return cb; 00246 } 00247 00248 QWidget* KFileMetaInfoWidget::makeIntWidget() 00249 { 00250 QSpinBox* sb = new QSpinBox(this); 00251 sb->setObjectName(QLatin1String("metainfo integer widget")); 00252 sb->setValue(d->m_item.value().toInt()); 00253 00254 if (d->m_validator) { 00255 if (QIntValidator* iv = qobject_cast<QIntValidator*>(d->m_validator)) { 00256 sb->setMinimum(iv->bottom()); 00257 sb->setMaximum(iv->top()); 00258 } 00259 //reparentValidator(sb, m_validator); 00260 //sb->setValidator(m_validator); 00261 } 00262 00263 #ifndef KDE_NO_DEPRECATED 00264 // make sure that an uint cannot be set to a value < 0 00265 if (d->m_item.properties().type() == QVariant::UInt) 00266 sb->setMinimum(qMax(sb->minimum(), 0)); 00267 #endif 00268 00269 connect(sb, SIGNAL(valueChanged(int)), this, SLOT(slotChanged(int))); 00270 return sb; 00271 } 00272 00273 QWidget* KFileMetaInfoWidget::makeDoubleWidget() 00274 { 00275 double value = d->m_item.value().toDouble(); 00276 00277 KDoubleNumInput* dni = new KDoubleNumInput(qMin(0.0,value), //krazy:exclude=qminmax 00278 qMax(0.0,value), //krazy:exclude=qminmax 00279 value, this, 0.01 ,2); 00280 00281 00282 if (d->m_validator) { 00283 if (QDoubleValidator* dv = qobject_cast<QDoubleValidator*>(d->m_validator)) { 00284 dni->setMinimum(dv->bottom()); 00285 dni->setMaximum(dv->top()); 00286 } 00287 reparentValidator(dni, d->m_validator); 00288 } 00289 00290 connect(dni, SIGNAL(valueChanged(double)), this, SLOT(slotChanged(double))); 00291 return dni; 00292 } 00293 00294 QWidget* KFileMetaInfoWidget::makeStringWidget() 00295 { 00296 if (KStringListValidator* val = qobject_cast<KStringListValidator*>(d->m_validator)) { 00297 KComboBox* b = new KComboBox(true, this); 00298 b->addItems(val->stringList()); 00299 int i = b->findText(d->m_item.value().toString()); 00300 if (i != -1) 00301 b->setCurrentIndex(i); 00302 else 00303 b->setEditText(d->m_item.value().toString()); 00304 connect(b, SIGNAL(activated(const QString &)), this, SLOT(slotComboChanged(const QString &))); 00305 b->setValidator(val); 00306 reparentValidator(b, val); 00307 return b; 00308 } 00309 00310 #ifndef KDE_NO_DEPRECATED 00311 if (d->m_item.properties().attributes() & PredicateProperties::MultiLine) { 00312 KTextEdit *edit = new KTextEdit( this ); 00313 edit->setAcceptRichText(false); 00314 edit->setPlainText(d->m_item.value().toString()); 00315 connect( edit, SIGNAL( textChanged() ), 00316 this, SLOT( slotMultiLineEditChanged() )); 00317 // can't use a validator with a KTextEdit, but we may need to delete it 00318 if (d->m_validator) 00319 reparentValidator(edit, d->m_validator); 00320 return edit; 00321 } 00322 #endif 00323 00324 KLineEdit* e = new KLineEdit(d->m_item.value().toString(), this); 00325 if (d->m_validator) { 00326 e->setValidator(d->m_validator); 00327 reparentValidator(e, d->m_validator); 00328 } 00329 connect(e, SIGNAL(textChanged(const QString&)), 00330 this, SLOT(slotLineEditChanged(const QString&))); 00331 return e; 00332 } 00333 00334 QWidget* KFileMetaInfoWidget::makeDateWidget() 00335 { 00336 QWidget *e = new QDateEdit(d->m_item.value().toDate(), this); 00337 connect(e, SIGNAL(valueChanged(const QDate&)), 00338 this, SLOT(slotDateChanged(const QDate&))); 00339 return e; 00340 } 00341 00342 QWidget* KFileMetaInfoWidget::makeTimeWidget() 00343 { 00344 return new QTimeEdit(d->m_item.value().toTime(), this); 00345 } 00346 00347 QWidget* KFileMetaInfoWidget::makeDateTimeWidget() 00348 { 00349 return new QDateTimeEdit(d->m_item.value().toDateTime(), this); 00350 } 00351 00352 void KFileMetaInfoWidget::reparentValidator( QWidget *widget, 00353 QValidator *validator ) 00354 { 00355 if ( !validator->parent() ) 00356 validator->setParent( widget ); 00357 } 00358 00359 // **************************************************************** 00360 // now the slots that let us get notified if the value changed in the child 00361 // **************************************************************** 00362 00363 void KFileMetaInfoWidget::slotChanged(bool value) 00364 { 00365 Q_ASSERT(qobject_cast<QComboBox*>(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(int value) 00372 { 00373 Q_ASSERT(qobject_cast<QSpinBox*>(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::slotChanged(double value) 00380 { 00381 Q_ASSERT(qobject_cast<KDoubleNumInput*>(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::slotComboChanged(const QString &value) 00388 { 00389 Q_ASSERT(qobject_cast<KComboBox*>(d->m_widget)); 00390 d->m_value = QVariant(value); 00391 emit valueChanged(d->m_value); 00392 d->m_dirty = true; 00393 } 00394 00395 void KFileMetaInfoWidget::slotLineEditChanged(const QString& value) 00396 { 00397 Q_ASSERT(qobject_cast<KLineEdit*>(d->m_widget)); 00398 d->m_value = QVariant(value); 00399 emit valueChanged(d->m_value); 00400 d->m_dirty = true; 00401 } 00402 00403 // that may be a little expensive for long texts, but what can we do? 00404 void KFileMetaInfoWidget::slotMultiLineEditChanged() 00405 { 00406 Q_ASSERT(qobject_cast<KTextEdit*>(d->m_widget)); 00407 d->m_value = QVariant(static_cast<const KTextEdit*>(sender())->toPlainText()); 00408 emit valueChanged(d->m_value); 00409 d->m_dirty = true; 00410 } 00411 00412 void KFileMetaInfoWidget::slotDateChanged(const QDate& value) 00413 { 00414 Q_ASSERT(qobject_cast<QDateEdit*>(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::slotTimeChanged(const QTime& value) 00421 { 00422 Q_ASSERT(qobject_cast<QTimeEdit*>(d->m_widget)); 00423 d->m_value = QVariant(value); 00424 emit valueChanged(d->m_value); 00425 d->m_dirty = true; 00426 } 00427 00428 void KFileMetaInfoWidget::slotDateTimeChanged(const QDateTime& value) 00429 { 00430 Q_ASSERT(qobject_cast<QDateTimeEdit*>(d->m_widget)); 00431 d->m_value = QVariant(value); 00432 emit valueChanged(d->m_value); 00433 d->m_dirty = true; 00434 } 00435 00436 #include "kfilemetainfowidget.moc"
KDE 4.6 API Reference