• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

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     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:     // no type
00161             // just make a label
00162             w = new QLabel(i18n("<Error>"), this);
00163             w->setObjectName(QLatin1String("label"));
00164             break;
00165 
00166         case QVariant::Int:         // an int
00167         case QVariant::UInt:        // an unsigned int
00168             w = makeIntWidget();
00169             break;
00170 
00171         case QVariant::Bool:        // a bool
00172             w = makeBoolWidget();
00173             break;
00174 
00175         case QVariant::Double:      // a double
00176             w = makeDoubleWidget();
00177             break;
00178 
00179 
00180         case QVariant::Date:        // a QDate
00181             w = makeDateWidget();
00182             break;
00183 
00184         case QVariant::Time:        // a QTime
00185             w = makeTimeWidget();
00186             break;
00187 
00188         case QVariant::DateTime:    // a QDateTime
00189             w = makeDateTimeWidget();
00190             break;
00191 
00192 #if 0
00193         case QVariant::Size:        // a QSize
00194         case QVariant::String:      // a QString
00195         case QVariant::List:        // a QValueList
00196         case QVariant::Map:         // a QMap
00197         case QVariant::StringList:  //  a QStringList
00198         case QVariant::Font:        // a QFont
00199         case QVariant::Pixmap:      // a QPixmap
00200         case QVariant::Brush:       // a QBrush
00201         case QVariant::Rect:        // a QRect
00202         case QVariant::Color:       // a QColor
00203         case QVariant::Palette:     // a QPalette
00204         case QVariant::ColorGroup:  // a QColorGroup
00205         case QCoreVariant::Icon:     // a QIconSet
00206         case QVariant::Point:       // a QPoint
00207         case QVariant::Image:       // a QImage
00208         case QVariant::CString:     // a QCString
00209         case QVariant::PointArray:  // a QPointArray
00210         case QVariant::Region:      // a QRegion
00211         case QVariant::Bitmap:      // a QBitmap
00212         case QVariant::Cursor:      // a QCursor
00213         case QVariant::ByteArray:   // a QByteArray
00214         case QVariant::BitArray:    // a QBitArray
00215         case QVariant::SizePolicy:  // a QSizePolicy
00216         case QVariant::KeySequence: // a QKeySequence
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 // now the different methods to make the widgets for specific types
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         //reparentValidator(sb, m_validator);
00259         //sb->setValidator(m_validator);
00260     }
00261 
00262     // make sure that an uint cannot be set to a value < 0
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         // can't use a validator with a KTextEdit, but we may need to delete it
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 // now the slots that let us get notified if the value changed in the child
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 // that may be a little expensive for long texts, but what can we do?
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"

KIO

Skip menu "KIO"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal