KProperty

timeedit.cpp
1 /* This file is part of the KDE project
2  Copyright (C) 2004 Cedric Pasteur <[email protected]>
3  Copyright (C) 2004 Alexander Dymo <[email protected]>
4  Copyright (C) 2012 Friedrich W. H. Kossebau <[email protected]>
5  Copyright (C) 2015 Jarosław Staniek <[email protected]>
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public License
18  along with this library; see the file COPYING.LIB. If not, write to
19  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21 */
22 
23 #include "timeedit.h"
24 #include "KPropertyUtils.h"
25 
26 #include <QLocale>
27 
28 class Q_DECL_HIDDEN KPropertyTimeEditor::Private
29 {
30 public:
31  Private() {
32  }
33 };
34 
35 KPropertyTimeEditor::KPropertyTimeEditor(const KProperty* prop, QWidget* parent)
36  : QTimeEdit(parent), d(new Private)
37 {
38  setFrame(false);
39  setContentsMargins(0,1,0,0);
40 
41  if (prop->hasOptions()) {
42  const QTime minTime = prop->option("min", minimumTime()).toTime();
43  const QTime maxTime = prop->option("max", maximumTime()).toTime();
44  if (minTime.isValid() && maxTime.isValid() && minTime <= maxTime) {
45  setTimeRange(minTime, maxTime);
46  }
47  const QString minValueText(prop->option("minValueText").toString());
48  if (!minValueText.isEmpty()) {
49  setSpecialValueText(minValueText);
50  }
51  }
52 
53  connect(this, SIGNAL(timeChanged(QTime)), this, SLOT(onTimeChanged()));
54 }
55 
56 KPropertyTimeEditor::~KPropertyTimeEditor()
57 {
58  delete d;
59 }
60 
61 QVariant KPropertyTimeEditor::value() const
62 {
63  return QVariant(time());
64 }
65 
66 void KPropertyTimeEditor::setValue(const QVariant& value)
67 {
68  blockSignals(true);
69  setTime(value.toTime());
70  blockSignals(false);
71 }
72 
73 void KPropertyTimeEditor::paintEvent(QPaintEvent* event)
74 {
75  QTimeEdit::paintEvent(event);
76  KPropertyWidgetsFactory::paintTopGridLine(this);
77 }
78 
79 
80 void KPropertyTimeEditor::onTimeChanged()
81 {
82  emit commitData(this);
83 }
84 
85 
87 KPropertyTimeDelegate::KPropertyTimeDelegate()
88 {
89 }
90 
91 QString KPropertyTimeDelegate::valueToString(const QVariant& value, const QLocale &locale) const
92 {
93  if (locale.language() == QLocale::C) {
94  if (value.isNull()) {
95  return QString();
96  }
97  return value.toTime().toString(Qt::ISODate);
98  }
99  const QString defaultTimeFormat = locale.timeFormat(QLocale::ShortFormat);
100  return value.toTime().toString(defaultTimeFormat);
101 }
102 
103 QWidget* KPropertyTimeDelegate::createEditor(int type, QWidget* parent,
104  const QStyleOptionViewItem& option, const QModelIndex& index) const
105 {
106  Q_UNUSED(type);
107  Q_UNUSED(option);
108 
109  KProperty* prop = KPropertyUtils::propertyForIndex(index);
110  if (!prop) {
111  return nullptr;
112  }
113  return new KPropertyTimeEditor(prop, parent);
114 }
QString toString(Qt::DateFormat format) const const
QTime toTime() const const
bool isValid() const const
QVariant option(const char *name, const QVariant &defaultValue=QVariant()) const
Returns value of given option Option is set if returned value is not null. If there is no option for ...
Definition: KProperty.cpp:564
bool isNull() const const
QString timeFormat(QLocale::FormatType format) const const
virtual void paintEvent(QPaintEvent *event) override
bool hasOptions() const
Returns true if at least one option is specified for this property If there are no options defined tr...
Definition: KProperty.cpp:570
QLocale::Language language() const const
The base class representing a single property.
Definition: KProperty.h:95
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun Aug 9 2020 23:10:57 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.