KDELibs4Support

kfilemetainfowidget.cpp
1 /* This file is part of the KDE libraries
2  Copyright (C) 2001,2002 Rolf Magnus <[email protected]>
3 
4  library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License version 2 as published by the Free Software Foundation.
7 
8  This library is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  Library General Public License for more details.
12 
13  You should have received a copy of the GNU Library General Public License
14  along with this library; see the file COPYING.LIB. If not, write to
15  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  Boston, MA 02110-1301, USA.
17  */
18 
19 #include "kfilemetainfowidget.h"
20 
21 #include <ktextedit.h>
22 #include <klocalizedstring.h>
23 #include <kcombobox.h>
24 #include <klineedit.h>
25 #include <kstringvalidator.h>
26 #include <kdebug.h>
27 
28 #include <QLabel>
29 #include <QCheckBox>
30 #include <QDoubleSpinBox>
31 #include <QDateEdit>
32 #include <QPixmap>
33 #include <QImage>
34 #include <QLayout>
35 #include <QSizePolicy>
36 #include <QDoubleValidator>
37 
38 class KFileMetaInfoWidgetPrivate
39 {
40 public:
41  KFileMetaInfoWidgetPrivate(KFileMetaInfoWidget *qq)
42  : q(qq)
43  {
44  }
45 
46  void init(KFileMetaInfoItem item, KFileMetaInfoWidget::Mode mode);
47 
49  QVariant m_value; // the value will be saved here until apply() is called
50  KFileMetaInfoItem m_item;
51  QWidget *m_widget;
52  QValidator *m_validator;
53  bool m_dirty : 1;
54 };
55 
56 /*
57  Widgets used for different types:
58 
59  bool : QCheckBox
60  int : QSpinBox
61  QString : KComboBox if the validator is a KStringListValidator, else lineedit
62  QDateTime : QDateTimeEdit
63 
64 */
65 
66 KFileMetaInfoWidget::KFileMetaInfoWidget(KFileMetaInfoItem item,
67  QValidator *val,
68  QWidget *parent)
69  : QWidget(parent), d(new KFileMetaInfoWidgetPrivate(this))
70 {
71  d->m_value = item.value();
72  d->m_item = item;
73  d->m_validator = val;
74  d->init(item, ReadWrite);
75 }
76 
77 KFileMetaInfoWidget::KFileMetaInfoWidget(KFileMetaInfoItem item,
78  Mode mode,
79  QValidator *val,
80  QWidget *parent)
81  : QWidget(parent), d(new KFileMetaInfoWidgetPrivate(this))
82 {
83  d->m_value = item.value();
84  d->m_item = item;
85  d->m_validator = val;
86  d->init(item, mode);
87 }
88 
89 void KFileMetaInfoWidgetPrivate::init(KFileMetaInfoItem item, KFileMetaInfoWidget::Mode mode)
90 {
91  Q_UNUSED(item)
92  kDebug(7033) << "*** item " << m_item.name()
93  << " is a " << m_value.typeName() << endl;
94 
95  if (m_item.isEditable() && !(mode & KFileMetaInfoWidget::ReadOnly)) {
96  m_widget = q->makeWidget();
97  } else
98  switch (m_value.type()) {
99  case QVariant::Image :
100  m_widget = new QLabel(q);
101  m_widget->setObjectName(QLatin1String("info image"));
102  static_cast<QLabel *>(m_widget)->setPixmap(QPixmap::fromImage(m_value.value<QImage>()));
103  break;
104  case QVariant::Pixmap :
105  m_widget = new QLabel(q);
106  m_widget->setObjectName(QLatin1String("info pixmap"));
107  static_cast<QLabel *>(m_widget)->setPixmap(m_value.value<QPixmap>());
108  break;
109  default:
110  m_widget = new QLabel(m_value.toString(), q);
111  m_widget->setObjectName(QLatin1String("info label"));
112  }
113 
114  QHBoxLayout *lay = new QHBoxLayout(q);
115  lay->setContentsMargins(0, 0, 0, 0);
116  lay->addWidget(m_widget);
117 
118  QSizePolicy sp = q->sizePolicy();
120  q->setSizePolicy(sp);
121 }
122 
123 KFileMetaInfoWidget::~KFileMetaInfoWidget()
124 {
125  delete d;
126 }
127 
128 bool KFileMetaInfoWidget::apply()
129 {
130  return d->m_item.isEditable() && d->m_item.setValue(d->m_value);
131 }
132 
133 void KFileMetaInfoWidget::setValue(const QVariant &value)
134 {
135  d->m_value = value;
136 }
137 
138 QVariant KFileMetaInfoWidget::value() const
139 {
140  return d->m_value;
141 }
142 
143 QValidator *KFileMetaInfoWidget::validator() const
144 {
145  return d->m_validator;
146 }
147 
148 KFileMetaInfoItem KFileMetaInfoWidget::item() const
149 {
150  return d->m_item;
151 }
152 
153 QWidget *KFileMetaInfoWidget::makeWidget()
154 {
155  QString valClass;
156  QWidget *w;
157 
158  switch (d->m_value.type()) {
159  case QVariant::Invalid: // no type
160  // just make a label
161  w = new QLabel(i18n("<Error>"), this);
162  w->setObjectName(QLatin1String("label"));
163  break;
164 
165  case QVariant::Int: // an int
166  case QVariant::UInt: // an unsigned int
167  w = makeIntWidget();
168  break;
169 
170  case QVariant::Bool: // a bool
171  w = makeBoolWidget();
172  break;
173 
174  case QVariant::Double: // a double
175  w = makeDoubleWidget();
176  break;
177 
178  case QVariant::Date: // a QDate
179  w = makeDateWidget();
180  break;
181 
182  case QVariant::Time: // a QTime
183  w = makeTimeWidget();
184  break;
185 
186  case QVariant::DateTime: // a QDateTime
187  w = makeDateTimeWidget();
188  break;
189 
190 #if 0
191  case QVariant::Size: // a QSize
192  case QVariant::String: // a QString
193  case QVariant::List: // a QValueList
194  case QVariant::Map: // a QMap
195  case QVariant::StringList: // a QStringList
196  case QVariant::Font: // a QFont
197  case QVariant::Pixmap: // a QPixmap
198  case QVariant::Brush: // a QBrush
199  case QVariant::Rect: // a QRect
200  case QVariant::Color: // a QColor
201  case QVariant::Palette: // a QPalette
202  case QVariant::ColorGroup: // a QColorGroup
203  case QCoreVariant::Icon: // a QIconSet
204  case QVariant::Point: // a QPoint
205  case QVariant::Image: // a QImage
206  case QVariant::CString: // a QCString
207  case QVariant::PointArray: // a QPointArray
208  case QVariant::Region: // a QRegion
209  case QVariant::Bitmap: // a QBitmap
210  case QVariant::Cursor: // a QCursor
211  case QVariant::ByteArray: // a QByteArray
212  case QVariant::BitArray: // a QBitArray
213  case QVariant::SizePolicy: // a QSizePolicy
214  case QVariant::KeySequence: // a QKeySequence
215 #endif
216  default:
217  w = makeStringWidget();
218  }
219 
220  kDebug(7033) << "*** item " << d->m_item.name()
221  << "is a " << d->m_item.value().typeName() << endl;
222  if (d->m_validator)
223  kDebug(7033) << " and validator is a "
224  << d->m_validator->metaObject()->className() << endl;
225 
226  kDebug(7033) << "*** created a " << w->metaObject()->className()
227  << " for it\n";
228 
229  return w;
230 }
231 
232 // ****************************************************************
233 // now the different methods to make the widgets for specific types
234 // ****************************************************************
235 
236 QWidget *KFileMetaInfoWidget::makeBoolWidget()
237 {
238  QCheckBox *cb = new QCheckBox(this);
239  cb->setObjectName(QLatin1String("metainfo bool widget"));
240  cb->setChecked(d->m_item.value().toBool());
241  connect(cb, SIGNAL(toggled(bool)), this, SLOT(slotChanged(bool)));
242  return cb;
243 }
244 
245 QWidget *KFileMetaInfoWidget::makeIntWidget()
246 {
247  QSpinBox *sb = new QSpinBox(this);
248  sb->setObjectName(QLatin1String("metainfo integer widget"));
249  sb->setValue(d->m_item.value().toInt());
250 
251  if (d->m_validator) {
252  if (QIntValidator *iv = qobject_cast<QIntValidator *>(d->m_validator)) {
253  sb->setMinimum(iv->bottom());
254  sb->setMaximum(iv->top());
255  }
256  //reparentValidator(sb, m_validator);
257  //sb->setValidator(m_validator);
258  }
259 
260 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
261  // make sure that an uint cannot be set to a value < 0
262  if (d->m_item.properties().type() == QVariant::UInt) {
263  sb->setMinimum(qMax(sb->minimum(), 0));
264  }
265 #endif
266 
267  connect(sb, SIGNAL(valueChanged(int)), this, SLOT(slotChanged(int)));
268  return sb;
269 }
270 
271 QWidget *KFileMetaInfoWidget::makeDoubleWidget()
272 {
273  double value = d->m_item.value().toDouble();
274 
275  QDoubleSpinBox *dni = new QDoubleSpinBox(this);
276  dni->setMinimum(qMin(0.0, value)); //krazy:exclude=qminmax
277  dni->setMaximum(qMax(0.0, value)); //krazy:exclude=qminmax
278  dni->setValue(value);
279  dni->setSingleStep(0.01);
280  dni->setDecimals(2);
281 
282  if (d->m_validator) {
283  if (QDoubleValidator *dv = qobject_cast<QDoubleValidator *>(d->m_validator)) {
284  dni->setMinimum(dv->bottom());
285  dni->setMaximum(dv->top());
286  }
287  reparentValidator(dni, d->m_validator);
288  }
289 
290  connect(dni, SIGNAL(valueChanged(double)), this, SLOT(slotChanged(double)));
291  return dni;
292 }
293 
294 QWidget *KFileMetaInfoWidget::makeStringWidget()
295 {
296  if (KStringListValidator *val = qobject_cast<KStringListValidator *>(d->m_validator)) {
297  KComboBox *b = new KComboBox(true, this);
298  b->addItems(val->stringList());
299  int i = b->findText(d->m_item.value().toString());
300  if (i != -1) {
301  b->setCurrentIndex(i);
302  } else {
303  b->setEditText(d->m_item.value().toString());
304  }
305  connect(b, SIGNAL(activated(QString)), this, SLOT(slotComboChanged(QString)));
306  b->setValidator(val);
307  reparentValidator(b, val);
308  return b;
309  }
310 
311 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
312  if (d->m_item.properties().attributes() & PredicateProperties::MultiLine) {
313  KTextEdit *edit = new KTextEdit(this);
314  edit->setAcceptRichText(false);
315  edit->setPlainText(d->m_item.value().toString());
316  connect(edit, SIGNAL(textChanged()),
317  this, SLOT(slotMultiLineEditChanged()));
318  // can't use a validator with a KTextEdit, but we may need to delete it
319  if (d->m_validator) {
320  reparentValidator(edit, d->m_validator);
321  }
322  return edit;
323  }
324 #endif
325 
326  KLineEdit *e = new KLineEdit(d->m_item.value().toString(), this);
327  if (d->m_validator) {
328  e->setValidator(d->m_validator);
329  reparentValidator(e, d->m_validator);
330  }
331  connect(e, SIGNAL(textChanged(QString)),
332  this, SLOT(slotLineEditChanged(QString)));
333  return e;
334 }
335 
336 QWidget *KFileMetaInfoWidget::makeDateWidget()
337 {
338  QWidget *e = new QDateEdit(d->m_item.value().toDate(), this);
339  connect(e, SIGNAL(valueChanged(QDate)),
340  this, SLOT(slotDateChanged(QDate)));
341  return e;
342 }
343 
344 QWidget *KFileMetaInfoWidget::makeTimeWidget()
345 {
346  return new QTimeEdit(d->m_item.value().toTime(), this);
347 }
348 
349 QWidget *KFileMetaInfoWidget::makeDateTimeWidget()
350 {
351  return new QDateTimeEdit(d->m_item.value().toDateTime(), this);
352 }
353 
354 void KFileMetaInfoWidget::reparentValidator(QWidget *widget,
355  QValidator *validator)
356 {
357  if (!validator->parent()) {
358  validator->setParent(widget);
359  }
360 }
361 
362 // ****************************************************************
363 // now the slots that let us get notified if the value changed in the child
364 // ****************************************************************
365 
366 void KFileMetaInfoWidget::slotChanged(bool value)
367 {
368  Q_ASSERT(qobject_cast<QComboBox *>(d->m_widget));
369  d->m_value = QVariant(value);
370  emit valueChanged(d->m_value);
371  d->m_dirty = true;
372 }
373 
374 void KFileMetaInfoWidget::slotChanged(int value)
375 {
376  Q_ASSERT(qobject_cast<QSpinBox *>(d->m_widget));
377  d->m_value = QVariant(value);
378  emit valueChanged(d->m_value);
379  d->m_dirty = true;
380 }
381 
382 void KFileMetaInfoWidget::slotChanged(double value)
383 {
384  Q_ASSERT(qobject_cast<QSpinBox *>(d->m_widget));
385  d->m_value = QVariant(value);
386  emit valueChanged(d->m_value);
387  d->m_dirty = true;
388 }
389 
390 void KFileMetaInfoWidget::slotComboChanged(const QString &value)
391 {
392  Q_ASSERT(qobject_cast<KComboBox *>(d->m_widget));
393  d->m_value = QVariant(value);
394  emit valueChanged(d->m_value);
395  d->m_dirty = true;
396 }
397 
398 void KFileMetaInfoWidget::slotLineEditChanged(const QString &value)
399 {
400  Q_ASSERT(qobject_cast<KLineEdit *>(d->m_widget));
401  d->m_value = QVariant(value);
402  emit valueChanged(d->m_value);
403  d->m_dirty = true;
404 }
405 
406 // that may be a little expensive for long texts, but what can we do?
407 void KFileMetaInfoWidget::slotMultiLineEditChanged()
408 {
409  Q_ASSERT(qobject_cast<KTextEdit *>(d->m_widget));
410  d->m_value = QVariant(static_cast<const KTextEdit *>(sender())->toPlainText());
411  emit valueChanged(d->m_value);
412  d->m_dirty = true;
413 }
414 
415 void KFileMetaInfoWidget::slotDateChanged(const QDate &value)
416 {
417  Q_ASSERT(qobject_cast<QDateEdit *>(d->m_widget));
418  d->m_value = QVariant(value);
419  emit valueChanged(d->m_value);
420  d->m_dirty = true;
421 }
422 
423 void KFileMetaInfoWidget::slotTimeChanged(const QTime &value)
424 {
425  Q_ASSERT(qobject_cast<QTimeEdit *>(d->m_widget));
426  d->m_value = QVariant(value);
427  emit valueChanged(d->m_value);
428  d->m_dirty = true;
429 }
430 
431 void KFileMetaInfoWidget::slotDateTimeChanged(const QDateTime &value)
432 {
433  Q_ASSERT(qobject_cast<QDateTimeEdit *>(d->m_widget));
434  d->m_value = QVariant(value);
435  emit valueChanged(d->m_value);
436  d->m_dirty = true;
437 }
438 
439 #include "moc_kfilemetainfowidget.cpp"
void setMinimum(int min)
void setContentsMargins(int left, int top, int right, int bottom)
virtual const QMetaObject * metaObject() const const
A QValidator to (dis)allow certain strings.
QPixmap fromImage(const QImage &image, Qt::ImageConversionFlags flags)
void setEditText(const QString &text)
void setPlainText(const QString &text)
This attribute says that a string item is likely to be more than one line long, so for editing...
int findText(const QString &text, Qt::MatchFlags flags) const const
void setSingleStep(double val)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
void setObjectName(const QString &name)
void setDecimals(int prec)
void setValue(double val)
void setVerticalPolicy(QSizePolicy::Policy policy)
void setParent(QObject *parent)
const char * className() const const
QCA_EXPORT void init()
void setMaximum(int max)
void setChecked(bool)
QString i18n(const char *text, const TYPE &arg...)
void setValue(int val)
void setCurrentIndex(int index)
void setMinimum(double min)
void addItems(const QStringList &texts)
QObject * parent() const const
void setValidator(const QValidator *v)
void setMaximum(double max)
void setAcceptRichText(bool accept)
void setValidator(const QValidator *validator)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Tue Jul 7 2020 22:56:51 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.