kfilemetainfowidget.cpp
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 $Id: kfilemetainfowidget.cpp 465272 2005-09-29 09:47:40Z mueller $ 00019 */ 00020 00021 #include "kfilemetainfowidget.h" 00022 00023 #include <keditcl.h> 00024 #include <klocale.h> 00025 #include <knuminput.h> 00026 #include <kcombobox.h> 00027 #include <klineedit.h> 00028 #include <kstringvalidator.h> 00029 #include <kdebug.h> 00030 00031 #include <qlabel.h> 00032 #include <qcheckbox.h> 00033 #include <qspinbox.h> 00034 #include <qdatetimeedit.h> 00035 #include <qpixmap.h> 00036 #include <qimage.h> 00037 #include <qlayout.h> 00038 #include <qvalidator.h> 00039 00040 /* 00041 Widgets used for different types: 00042 00043 bool : QCheckBox 00044 int : QSpinBox 00045 QString : KComboBox if the validator is a KStringListValidator, else lineedit 00046 QDateTime : QDateTimeEdit 00047 00048 */ 00049 00050 KFileMetaInfoWidget::KFileMetaInfoWidget(KFileMetaInfoItem item, 00051 QValidator* val, 00052 QWidget* parent, const char* name) 00053 : QWidget(parent, name), 00054 m_value(item.value()), 00055 m_item(item), 00056 m_validator(val) 00057 { 00058 init(item, ReadWrite); 00059 } 00060 00061 KFileMetaInfoWidget::KFileMetaInfoWidget(KFileMetaInfoItem item, 00062 Mode mode, 00063 QValidator* val, 00064 QWidget* parent, const char* name) 00065 : QWidget(parent, name), 00066 m_value(item.value()), 00067 m_item(item), 00068 m_validator(val) 00069 { 00070 init(item, mode); 00071 } 00072 00073 void KFileMetaInfoWidget::init(KFileMetaInfoItem item, Mode mode) 00074 { 00075 kdDebug(7033) << "*** item " << m_item.key() 00076 << " is a " << value().typeName() << endl; 00077 00078 if (m_item.isEditable() && !(mode & ReadOnly)) 00079 m_widget = makeWidget(); 00080 else 00081 switch (m_value.type()) 00082 { 00083 case QVariant::Image : 00084 m_widget = new QLabel(this, "info image"); 00085 static_cast<QLabel*>(m_widget)->setPixmap(QPixmap(m_value.toImage())); 00086 break; 00087 case QVariant::Pixmap : 00088 m_widget = new QLabel(this, "info pixmap"); 00089 static_cast<QLabel*>(m_widget)->setPixmap(m_value.toPixmap()); 00090 break; 00091 default: 00092 m_widget = new QLabel(item.string(true), this, "info label"); 00093 } 00094 00095 (new QHBoxLayout(this))->addWidget(m_widget); 00096 } 00097 00098 KFileMetaInfoWidget::~KFileMetaInfoWidget() 00099 { 00100 } 00101 00102 QWidget* KFileMetaInfoWidget::makeWidget() 00103 { 00104 QString valClass; 00105 QWidget* w; 00106 00107 switch (m_value.type()) 00108 { 00109 case QVariant::Invalid: // no type 00110 // just make a label 00111 w = new QLabel(i18n("<Error>"), this, "label"); 00112 break; 00113 00114 case QVariant::Int: // an int 00115 case QVariant::UInt: // an unsigned int 00116 w = makeIntWidget(); 00117 break; 00118 00119 case QVariant::Bool: // a bool 00120 w = makeBoolWidget(); 00121 break; 00122 00123 case QVariant::Double: // a double 00124 w = makeDoubleWidget(); 00125 break; 00126 00127 00128 case QVariant::Date: // a QDate 00129 w = makeDateWidget(); 00130 break; 00131 00132 case QVariant::Time: // a QTime 00133 w = makeTimeWidget(); 00134 break; 00135 00136 case QVariant::DateTime: // a QDateTime 00137 w = makeDateTimeWidget(); 00138 break; 00139 00140 #if 0 00141 case QVariant::Size: // a QSize 00142 case QVariant::String: // a QString 00143 case QVariant::List: // a QValueList 00144 case QVariant::Map: // a QMap 00145 case QVariant::StringList: // a QStringList 00146 case QVariant::Font: // a QFont 00147 case QVariant::Pixmap: // a QPixmap 00148 case QVariant::Brush: // a QBrush 00149 case QVariant::Rect: // a QRect 00150 case QVariant::Color: // a QColor 00151 case QVariant::Palette: // a QPalette 00152 case QVariant::ColorGroup: // a QColorGroup 00153 case QVariant::IconSet: // a QIconSet 00154 case QVariant::Point: // a QPoint 00155 case QVariant::Image: // a QImage 00156 case QVariant::CString: // a QCString 00157 case QVariant::PointArray: // a QPointArray 00158 case QVariant::Region: // a QRegion 00159 case QVariant::Bitmap: // a QBitmap 00160 case QVariant::Cursor: // a QCursor 00161 case QVariant::ByteArray: // a QByteArray 00162 case QVariant::BitArray: // a QBitArray 00163 case QVariant::SizePolicy: // a QSizePolicy 00164 case QVariant::KeySequence: // a QKeySequence 00165 #endif 00166 default: 00167 w = makeStringWidget(); 00168 } 00169 00170 kdDebug(7033) << "*** item " << m_item.key() 00171 << "is a " << m_item.value().typeName() << endl; 00172 if (m_validator) 00173 kdDebug(7033) << " and validator is a " << m_validator->className() << endl; 00174 00175 kdDebug(7033) << "*** created a " << w->className() << " for it\n"; 00176 00177 return w; 00178 } 00179 00180 // **************************************************************** 00181 // now the different methods to make the widgets for specific types 00182 // **************************************************************** 00183 00184 QWidget* KFileMetaInfoWidget::makeBoolWidget() 00185 { 00186 QCheckBox* cb = new QCheckBox(this, "metainfo bool widget"); 00187 cb->setChecked(m_item.value().toBool()); 00188 connect(cb, SIGNAL(toggled(bool)), this, SLOT(slotChanged(bool))); 00189 return cb; 00190 } 00191 00192 QWidget* KFileMetaInfoWidget::makeIntWidget() 00193 { 00194 QSpinBox* sb = new QSpinBox(this, "metainfo integer widget"); 00195 sb->setValue(m_item.value().toInt()); 00196 00197 if (m_validator) 00198 { 00199 if (m_validator->inherits("QIntValidator")) 00200 { 00201 sb->setMinValue(static_cast<QIntValidator*>(m_validator)->bottom()); 00202 sb->setMaxValue(static_cast<QIntValidator*>(m_validator)->top()); 00203 } 00204 reparentValidator(sb, m_validator); 00205 sb->setValidator(m_validator); 00206 } 00207 00208 // make sure that an uint cannot be set to a value < 0 00209 if (m_item.type() == QVariant::UInt) 00210 sb->setMinValue(QMAX(sb->minValue(), 0)); 00211 00212 connect(sb, SIGNAL(valueChanged(int)), this, SLOT(slotChanged(int))); 00213 return sb; 00214 } 00215 00216 QWidget* KFileMetaInfoWidget::makeDoubleWidget() 00217 { 00218 KDoubleNumInput* dni = new KDoubleNumInput(m_item.value().toDouble(), 00219 this, "metainfo double widget"); 00220 00221 00222 if (m_validator) 00223 { 00224 if (m_validator->inherits("QDoubleValidator")) 00225 { 00226 dni->setMinValue(static_cast<QDoubleValidator*>(m_validator)->bottom()); 00227 dni->setMaxValue(static_cast<QDoubleValidator*>(m_validator)->top()); 00228 } 00229 reparentValidator(dni, m_validator); 00230 } 00231 00232 connect(dni, SIGNAL(valueChanged(double)), this, SLOT(slotChanged(double))); 00233 return dni; 00234 } 00235 00236 QWidget* KFileMetaInfoWidget::makeStringWidget() 00237 { 00238 if (m_validator && m_validator->inherits("KStringListValidator")) 00239 { 00240 KComboBox* b = new KComboBox(true, this, "metainfo combobox"); 00241 KStringListValidator* val = static_cast<KStringListValidator*> 00242 (m_validator); 00243 b->insertStringList(val->stringList()); 00244 b->setCurrentText(m_item.value().toString()); 00245 connect(b, SIGNAL(activated(const QString &)), this, SLOT(slotComboChanged(const QString &))); 00246 b->setValidator(val); 00247 reparentValidator(b, val); 00248 return b; 00249 } 00250 00251 if ( m_item.attributes() & KFileMimeTypeInfo::MultiLine ) { 00252 KEdit *edit = new KEdit( this ); 00253 edit->setText( m_item.value().toString() ); 00254 connect( edit, SIGNAL( textChanged() ), 00255 this, SLOT( slotMultiLineEditChanged() )); 00256 // can't use a validator with a QTextEdit, but we may need to delete it 00257 if ( m_validator ) 00258 reparentValidator( edit, m_validator ); 00259 return edit; 00260 } 00261 00262 KLineEdit* e = new KLineEdit(m_item.value().toString(), this); 00263 if (m_validator) 00264 { 00265 e->setValidator(m_validator); 00266 reparentValidator(e, m_validator); 00267 } 00268 connect(e, SIGNAL(textChanged(const QString&)), 00269 this, SLOT(slotLineEditChanged(const QString&))); 00270 return e; 00271 } 00272 00273 QWidget* KFileMetaInfoWidget::makeDateWidget() 00274 { 00275 QWidget *e = new QDateEdit(m_item.value().toDate(), this); 00276 connect(e, SIGNAL(valueChanged(const QDate&)), 00277 this, SLOT(slotDateChanged(const QDate&))); 00278 return e; 00279 } 00280 00281 QWidget* KFileMetaInfoWidget::makeTimeWidget() 00282 { 00283 return new QTimeEdit(m_item.value().toTime(), this); 00284 } 00285 00286 QWidget* KFileMetaInfoWidget::makeDateTimeWidget() 00287 { 00288 return new QDateTimeEdit(m_item.value().toDateTime(), this); 00289 } 00290 00291 void KFileMetaInfoWidget::reparentValidator( QWidget *widget, 00292 QValidator *validator ) 00293 { 00294 if ( !validator->parent() ) 00295 widget->insertChild( validator ); 00296 } 00297 00298 // **************************************************************** 00299 // now the slots that let us get notified if the value changed in the child 00300 // **************************************************************** 00301 00302 void KFileMetaInfoWidget::slotChanged(bool value) 00303 { 00304 Q_ASSERT(m_widget->inherits("QComboBox")); 00305 m_value = QVariant(value); 00306 emit valueChanged(m_value); 00307 m_dirty = true; 00308 } 00309 00310 void KFileMetaInfoWidget::slotChanged(int value) 00311 { 00312 Q_ASSERT(m_widget->inherits("QSpinBox")); 00313 m_value = QVariant(value); 00314 emit valueChanged(m_value); 00315 m_dirty = true; 00316 } 00317 00318 void KFileMetaInfoWidget::slotChanged(double value) 00319 { 00320 Q_ASSERT(m_widget->inherits("KDoubleNumInput")); 00321 m_value = QVariant(value); 00322 emit valueChanged(m_value); 00323 m_dirty = true; 00324 } 00325 00326 void KFileMetaInfoWidget::slotComboChanged(const QString &value) 00327 { 00328 Q_ASSERT(m_widget->inherits("KComboBox")); 00329 m_value = QVariant(value); 00330 emit valueChanged(m_value); 00331 m_dirty = true; 00332 } 00333 00334 void KFileMetaInfoWidget::slotLineEditChanged(const QString& value) 00335 { 00336 Q_ASSERT(m_widget->inherits("KLineEdit")); 00337 m_value = QVariant(value); 00338 emit valueChanged(m_value); 00339 m_dirty = true; 00340 } 00341 00342 // that may be a little expensive for long texts, but what can we do? 00343 void KFileMetaInfoWidget::slotMultiLineEditChanged() 00344 { 00345 Q_ASSERT(m_widget->inherits("QTextEdit")); 00346 m_value = QVariant( static_cast<const QTextEdit*>( sender() )->text() ); 00347 emit valueChanged(m_value); 00348 m_dirty = true; 00349 } 00350 00351 void KFileMetaInfoWidget::slotDateChanged(const QDate& value) 00352 { 00353 Q_ASSERT(m_widget->inherits("QDateEdit")); 00354 m_value = QVariant(value); 00355 emit valueChanged(m_value); 00356 m_dirty = true; 00357 } 00358 00359 void KFileMetaInfoWidget::slotTimeChanged(const QTime& value) 00360 { 00361 Q_ASSERT(m_widget->inherits("QTimeEdit")); 00362 m_value = QVariant(value); 00363 emit valueChanged(m_value); 00364 m_dirty = true; 00365 } 00366 00367 void KFileMetaInfoWidget::slotDateTimeChanged(const QDateTime& value) 00368 { 00369 Q_ASSERT(m_widget->inherits("QDateTimeEdit")); 00370 m_value = QVariant(value); 00371 emit valueChanged(m_value); 00372 m_dirty = true; 00373 } 00374 00375 #include "kfilemetainfowidget.moc"