25 #include <QDateTimeEdit> 
   28 #include <QTextCharFormat> 
   29 #include <QCalendarWidget> 
   40   widget->setDateTime( QDateTime::currentDateTime() );
 
   49     mQgsDateTimeEdit = qgsEditor;
 
   54   QDateTimeEdit *qtEditor = 
dynamic_cast<QDateTimeEdit *
>( editor );
 
   57     mQDateTimeEdit = qtEditor;
 
   60   if ( !mQDateTimeEdit )
 
   62     QgsDebugMsg( QStringLiteral( 
"Date/time edit widget could not be initialized because provided widget is not a QDateTimeEdit." ) );
 
   63     QgsMessageLog::logMessage( tr( 
"Date/time edit widget could not be initialized because provided widget is not a QDateTimeEdit." ), tr( 
"UI forms" ), Qgis::MessageLevel::Warning );
 
   68   mQDateTimeEdit->setDisplayFormat( displayFormat );
 
   70   const bool calendar = 
config( QStringLiteral( 
"calendar_popup" ), 
true ).toBool();
 
   71   if ( calendar != mQDateTimeEdit->calendarPopup() )
 
   73     mQDateTimeEdit->setCalendarPopup( calendar );
 
   75   if ( calendar && mQDateTimeEdit->calendarWidget() )
 
   78     QTextCharFormat todayFormat;
 
   79     todayFormat.setBackground( QColor( 160, 180, 200 ) );
 
   80     mQDateTimeEdit->calendarWidget()->setDateTextFormat( QDate::currentDate(), todayFormat );
 
   83   const bool allowNull = 
config( QStringLiteral( 
"allow_null" ), 
true ).toBool();
 
   84   if ( mQgsDateTimeEdit )
 
   91                                    "For that the QGIS custom widget QgsDateTimeEdit needs to be used." ),
 
   92                                tr( 
"field widgets" ) );
 
   95   if ( mQgsDateTimeEdit )
 
  101     connect( mQDateTimeEdit, &QDateTimeEdit::dateTimeChanged, 
this,  &QgsDateTimeEditWrapper::dateTimeChanged );
 
  107   return mQgsDateTimeEdit || mQDateTimeEdit;
 
  112   if ( mQgsDateTimeEdit )
 
  116 void QgsDateTimeEditWrapper::dateTimeChanged( 
const QDateTime &dateTime )
 
  118   switch ( 
field().type() )
 
  120     case QVariant::DateTime:
 
  139       if ( !dateTime.isValid() || dateTime.isNull() )
 
  148         const bool fieldIsoFormat = 
config( QStringLiteral( 
"field_iso_format" ), 
false ).toBool();
 
  150         if ( fieldIsoFormat )
 
  171   if ( !mQDateTimeEdit )
 
  172     return QVariant( 
field().type() );
 
  175   if ( mQgsDateTimeEdit )
 
  177     dateTime = mQgsDateTimeEdit->
dateTime();
 
  181     dateTime = mQDateTimeEdit->dateTime();
 
  184   switch ( 
field().type() )
 
  186     case QVariant::DateTime:
 
  189       return dateTime.date();
 
  191       return dateTime.time();
 
  193       const bool fieldIsoFormat = 
config( QStringLiteral( 
"field_iso_format" ), 
false ).toBool();
 
  195       if ( fieldIsoFormat )
 
  197         return dateTime.toString( Qt::ISODate );
 
  201         return dateTime.toString( fieldFormat );
 
  204 #ifndef _MSC_VER // avoid warnings 
  209 void QgsDateTimeEditWrapper::updateValues( 
const QVariant &value, 
const QVariantList & )
 
  211   if ( !mQDateTimeEdit )
 
  216   switch ( 
field().type() )
 
  218     case QVariant::DateTime:
 
  219       dateTime = 
value.toDateTime();
 
  222       dateTime.setDate( 
value.toDate() );
 
  223       dateTime.setTime( QTime( 0, 0, 0 ) );
 
  226       dateTime.setDate( QDate::currentDate() );
 
  227       dateTime.setTime( 
value.toTime() );
 
  232       switch ( 
value.type() )
 
  234         case QVariant::DateTime:
 
  236           dateTime = 
value.toDateTime();
 
  241           dateTime.setDate( 
value.toDate() );
 
  242           dateTime.setTime( QTime( 0, 0, 0 ) );
 
  247           dateTime.setDate( QDate::currentDate() );
 
  248           dateTime.setTime( 
value.toTime() );
 
  253           const bool fieldIsoFormat = 
config( QStringLiteral( 
"field_iso_format" ), 
false ).toBool();
 
  255           if ( fieldIsoFormat )
 
  257             dateTime = QDateTime::fromString( 
value.toString(), Qt::ISODate );
 
  261             dateTime = QDateTime::fromString( 
value.toString(), fieldFormat );
 
  269   if ( mQgsDateTimeEdit )
 
  275     mQDateTimeEdit->setDateTime( dateTime );
 
  281   if ( !mQDateTimeEdit )
 
  284   mQDateTimeEdit->setEnabled( enabled );