kfilemetainfowidget.cpp Source File (kio Library)

| # kio Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

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

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:13:32 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001