1/* This file is part of the KDE project
2 Copyright (C) 2007 Dag Andersen <danders@get2net.dk>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17* Boston, MA 02110-1301, USA.
18*/
19
20#ifndef KPTDURATIONSPINBOX_H
21#define KPTDURATIONSPINBOX_H
22
23#include "kptduration.h"
24#include "kplatomodels_export.h"
25
26#include <QSpinBox>
27
28namespace KPlato
29{
30
31/**
32 * The DurationSpinBox provides a spinbox and a line edit to display and edit durations.
33 *
34 * DurationSpinBox is a QDoubleSpinBox with the addition of adjustable units,
35 * defined as Duration::Unit.
36 * The unit can be Day, Hour, Minute, Second and Millisecond.
37 * The user can select the unit the duration is displayed in by placing the cursor
38 * on the unit and step up or -down.
39 * Maximum- and minimum unit can be set with setMaximumUnit() and setMinimumUnit().
40 * Defaults are: maximum unit Day, minimum unit Hour.
41 *
42 */
43class KPLATOMODELS_EXPORT DurationSpinBox : public QDoubleSpinBox
44{
45 Q_OBJECT
46public:
47 explicit DurationSpinBox(QWidget *parent = 0);
48
49 /// Return the current unit
50 Duration::Unit unit() const { return m_unit; }
51
52 /// step the value steps step. If inside unit, steps unit +/- 1 step.
53 virtual void stepBy( int steps );
54 /// Set maximum unit to @p unit.
55 void setMaximumUnit( Duration::Unit unit );
56 /// Set maximum unit to @p unit.
57 void setMinimumUnit( Duration::Unit unit );
58
59 double valueFromText( const QString & text ) const;
60 QString textFromValue ( double value ) const;
61 QValidator::State validate ( QString & input, int & pos ) const;
62
63signals:
64 void unitChanged( int );
65
66public slots:
67 /// Set the current unit.
68 /// If unit is outside minimum- or maximum unit, the limit is adjusted.
69 void setUnit( Duration::Unit unit);
70
71protected slots:
72 void editorTextChanged( const QString &text );
73
74protected:
75 void keyPressEvent( QKeyEvent * event );
76 StepEnabled stepEnabled () const;
77
78 void stepUnitUp();
79 void stepUnitDown();
80
81 QString extractUnit ( const QString &text ) const;
82 QString extractValue ( const QString &text ) const;
83
84 /// If unit is outside minimum- or maximum unit, the limit is used.
85 void updateUnit( Duration::Unit unit);
86
87 bool isOnUnit() const;
88
89private:
90 Duration::Unit m_unit;
91 Duration::Unit m_minunit;
92 Duration::Unit m_maxunit;
93};
94
95} //namespace KPlato
96
97#endif
98