1 // copyright (c) 2017-2021 hors<horsicq@gmail.com>
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 
10 // The above copyright notice and this permission notice shall be included in all
11 // copies or substantial portions of the Software.
12 
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 // SOFTWARE.
20 //
21 #include "xdatetimeeditx.h"
22 
XDateTimeEditX(QWidget * pParent)23 XDateTimeEditX::XDateTimeEditX(QWidget *pParent): QDateTimeEdit(pParent)
24 {
25     g_nValue=0;
26     g_dtType=DT_TYPE_UNKNOWN;
27 
28     connect(this,SIGNAL(dateTimeChanged(const QDateTime &)),this,SLOT(_setDateTime(const QDateTime &)));
29 }
30 
setType(XDateTimeEditX::DT_TYPE dtType)31 void XDateTimeEditX::setType(XDateTimeEditX::DT_TYPE dtType)
32 {
33     this->g_dtType=dtType;
34 
35     if(dtType==DT_TYPE_POSIX)
36     {
37         setDisplayFormat("yyyy-MM-dd hh:mm:ss");
38         QDateTime dt;
39         dt=dt.fromString("1970-01-01 00:00:00","yyyy-MM-dd hh:mm:ss");
40         setMinimumDateTime(QDateTime(dt));
41         setDateTime(dt);
42     }
43 }
44 
setValue(quint64 nValue)45 void XDateTimeEditX::setValue(quint64 nValue)
46 {
47     if(this->g_nValue!=nValue)
48     {
49         this->g_nValue=nValue;
50 
51         if(g_dtType==DT_TYPE_POSIX)
52         {
53             QDateTime dt;
54             dt.setMSecsSinceEpoch((quint64)nValue*1000);
55 
56             setDateTime(dt);
57         }
58 
59         emit valueChanged(nValue);
60     }
61 }
62 
getValue()63 quint64 XDateTimeEditX::getValue()
64 {
65     return g_nValue;
66 }
67 
_setDateTime(const QDateTime & dt)68 void XDateTimeEditX::_setDateTime(const QDateTime &dt)
69 {
70     quint64 nCurrentValue=0;
71 
72     if(g_dtType==DT_TYPE_POSIX)
73     {
74         nCurrentValue=(quint64)dt.toMSecsSinceEpoch()/1000;
75     }
76 
77     if(g_nValue!=nCurrentValue)
78     {
79         g_nValue=nCurrentValue;
80 
81         emit valueChanged(nCurrentValue);
82     }
83 }
84