LCOV - code coverage report
Current view: top level - reportdesign/source/ui/dlg - DateTime.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 1 106 0.9 %
Date: 2015-06-13 12:38:46 Functions: 2 12 16.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : #include "DateTime.hxx"
      20             : #include <com/sun/star/beans/XPropertySet.hpp>
      21             : #include <tools/debug.hxx>
      22             : #include "RptResId.hrc"
      23             : #include "rptui_slotid.hrc"
      24             : #include "ModuleHelper.hxx"
      25             : #include "helpids.hrc"
      26             : #include <vcl/msgbox.hxx>
      27             : #include <connectivity/dbconversion.hxx>
      28             : #include <unotools/syslocale.hxx>
      29             : #include "UITools.hxx"
      30             : #include "RptDef.hxx"
      31             : #include "uistrings.hrc"
      32             : #include "ReportController.hxx"
      33             : #include <com/sun/star/report/XFormattedField.hpp>
      34             : #include <com/sun/star/util/Time.hpp>
      35             : #include <com/sun/star/util/NumberFormat.hpp>
      36             : #include <com/sun/star/util/XNumberFormatPreviewer.hpp>
      37             : #include <com/sun/star/util/XNumberFormatTypes.hpp>
      38             : #include <com/sun/star/i18n/NumberFormatIndex.hpp>
      39             : #include <comphelper/numbers.hxx>
      40             : #include <algorithm>
      41             : 
      42             : namespace rptui
      43             : {
      44             : using namespace ::com::sun::star;
      45             : using namespace ::comphelper;
      46             : 
      47             : 
      48             : // class ODateTimeDialog
      49             : 
      50           0 : ODateTimeDialog::ODateTimeDialog( vcl::Window* _pParent
      51             :                                            ,const uno::Reference< report::XSection >& _xHoldAlive
      52             :                                            ,OReportController* _pController)
      53             :     : ModalDialog( _pParent, "DateTimeDialog" , "modules/dbreport/ui/datetimedialog.ui" )
      54             : 
      55             :     , m_aDateControlling()
      56             :     , m_aTimeControlling()
      57             :     , m_pController(_pController)
      58           0 :     , m_xHoldAlive(_xHoldAlive)
      59             : {
      60           0 :     get(m_pDate,"date");
      61           0 :     get(m_pFTDateFormat,"datelistbox_label");
      62           0 :     get(m_pDateListBox,"datelistbox");
      63           0 :     get(m_pTime,"time");
      64           0 :     get(m_pFTTimeFormat,"timelistbox_label");
      65           0 :     get(m_pTimeListBox,"timelistbox");
      66           0 :     get(m_pPB_OK,"ok");
      67             : 
      68             : 
      69             :     try
      70             :     {
      71           0 :         SvtSysLocale aSysLocale;
      72           0 :         m_nLocale = aSysLocale.GetLanguageTag().getLocale();
      73             :         // Fill listbox with all well known date types
      74           0 :         InsertEntry(util::NumberFormat::DATE);
      75           0 :         InsertEntry(util::NumberFormat::TIME);
      76             :     }
      77           0 :     catch (const uno::Exception&)
      78             :     {
      79             :     }
      80             : 
      81           0 :     m_pDateListBox->SelectEntryPos(0);
      82             : 
      83           0 :     m_pTimeListBox->SelectEntryPos(0);
      84             : 
      85             :     // use nice enhancement, to toggle enable/disable if a checkbox is checked or not
      86           0 :     m_aDateControlling.enableOnCheckMark( *m_pDate, *m_pFTDateFormat, *m_pDateListBox);
      87           0 :     m_aTimeControlling.enableOnCheckMark( *m_pTime, *m_pFTTimeFormat, *m_pTimeListBox);
      88             : 
      89           0 :     CheckBox* aCheckBoxes[] = { m_pDate,m_pTime};
      90           0 :     for ( size_t i = 0 ; i < SAL_N_ELEMENTS(aCheckBoxes); ++i)
      91           0 :         aCheckBoxes[i]->SetClickHdl(LINK(this,ODateTimeDialog,CBClickHdl));
      92             : 
      93           0 : }
      94             : 
      95           0 :     void ODateTimeDialog::InsertEntry(sal_Int16 _nNumberFormatId)
      96             :     {
      97           0 :         const bool bTime = util::NumberFormat::TIME == _nNumberFormatId;
      98           0 :         ListBox* pListBox = m_pDateListBox;
      99           0 :         if ( bTime )
     100           0 :             pListBox = m_pTimeListBox;
     101             : 
     102           0 :         const uno::Reference< util::XNumberFormatter> xNumberFormatter = m_pController->getReportNumberFormatter();
     103           0 :         const uno::Reference< util::XNumberFormats> xFormats = xNumberFormatter->getNumberFormatsSupplier()->getNumberFormats();
     104           0 :         const uno::Sequence<sal_Int32> aFormatKeys = xFormats->queryKeys(_nNumberFormatId,m_nLocale,sal_True);
     105           0 :         const sal_Int32* pIter = aFormatKeys.getConstArray();
     106           0 :         const sal_Int32* pEnd  = pIter + aFormatKeys.getLength();
     107           0 :         for(;pIter != pEnd;++pIter)
     108             :         {
     109           0 :             const sal_Int16 nPos = pListBox->InsertEntry(getFormatStringByKey(*pIter,xFormats,bTime));
     110           0 :             pListBox->SetEntryData(nPos, reinterpret_cast<void*>(*pIter));
     111           0 :         }
     112           0 :     }
     113             : 
     114           0 : ODateTimeDialog::~ODateTimeDialog()
     115             : {
     116           0 :     disposeOnce();
     117           0 : }
     118             : 
     119           0 : void ODateTimeDialog::dispose()
     120             : {
     121           0 :     m_pDate.clear();
     122           0 :     m_pFTDateFormat.clear();
     123           0 :     m_pDateListBox.clear();
     124           0 :     m_pTime.clear();
     125           0 :     m_pFTTimeFormat.clear();
     126           0 :     m_pTimeListBox.clear();
     127           0 :     m_pPB_OK.clear();
     128           0 :     ModalDialog::dispose();
     129           0 : }
     130             : 
     131           0 : short ODateTimeDialog::Execute()
     132             : {
     133           0 :     short nRet = ModalDialog::Execute();
     134           0 :     if ( nRet == RET_OK && (m_pDate->IsChecked() || m_pTime->IsChecked()) )
     135             :     {
     136             :         try
     137             :         {
     138           0 :             sal_Int32 nLength = 0;
     139           0 :             uno::Sequence<beans::PropertyValue> aValues( 6 );
     140           0 :             aValues[nLength].Name = PROPERTY_SECTION;
     141           0 :             aValues[nLength++].Value <<= m_xHoldAlive;
     142             : 
     143           0 :             aValues[nLength].Name = PROPERTY_TIME_STATE;
     144           0 :             aValues[nLength++].Value <<= m_pTime->IsChecked();
     145             : 
     146           0 :             aValues[nLength].Name = PROPERTY_DATE_STATE;
     147           0 :             aValues[nLength++].Value <<= m_pDate->IsChecked();
     148             : 
     149           0 :             aValues[nLength].Name = PROPERTY_FORMATKEYDATE;
     150           0 :             aValues[nLength++].Value <<= getFormatKey(true);
     151             : 
     152           0 :             aValues[nLength].Name = PROPERTY_FORMATKEYTIME;
     153           0 :             aValues[nLength++].Value <<= getFormatKey(false);
     154             : 
     155           0 :             sal_Int32 nWidth = 0;
     156           0 :             if ( m_pDate->IsChecked() )
     157             :             {
     158           0 :                 OUString sDateFormat = m_pDateListBox->GetSelectEntry();
     159           0 :                 nWidth = LogicToLogic(PixelToLogic(Size(GetCtrlTextWidth(sDateFormat),0)).Width(),GetMapMode().GetMapUnit(),MAP_100TH_MM);
     160             :             }
     161           0 :             if ( m_pTime->IsChecked() )
     162             :             {
     163           0 :                 OUString sDateFormat = m_pTimeListBox->GetSelectEntry();
     164           0 :                 nWidth = ::std::max<sal_Int32>(LogicToLogic(PixelToLogic(Size(GetCtrlTextWidth(sDateFormat),0)).Width(),GetMapMode().GetMapUnit(),MAP_100TH_MM),nWidth);
     165             :             }
     166             : 
     167           0 :             if ( nWidth > 4000 )
     168             :             {
     169           0 :                 aValues[nLength].Name = PROPERTY_WIDTH;
     170           0 :                 aValues[nLength++].Value <<= nWidth;
     171             :             }
     172             : 
     173           0 :             m_pController->executeChecked(SID_DATETIME,aValues);
     174             :         }
     175           0 :         catch (const uno::Exception&)
     176             :         {
     177           0 :             nRet = RET_NO;
     178             :         }
     179             :     }
     180           0 :     return nRet;
     181             : }
     182             : 
     183           0 : OUString ODateTimeDialog::getFormatStringByKey(::sal_Int32 _nNumberFormatKey,const uno::Reference< util::XNumberFormats>& _xFormats,bool _bTime)
     184             : {
     185           0 :     uno::Reference< beans::XPropertySet> xFormSet = _xFormats->getByKey(_nNumberFormatKey);
     186             :     OSL_ENSURE(xFormSet.is(),"XPropertySet is null!");
     187           0 :     OUString sFormat;
     188           0 :     xFormSet->getPropertyValue("FormatString") >>= sFormat;
     189             : 
     190           0 :     double nValue = 0;
     191           0 :     if ( _bTime )
     192             :     {
     193           0 :         tools::Time aCurrentTime( tools::Time::SYSTEM );
     194           0 :         nValue = ::dbtools::DBTypeConversion::toDouble(::dbtools::DBTypeConversion::toTime(aCurrentTime.GetTime()));
     195             :     }
     196             :     else
     197             :     {
     198           0 :         Date aCurrentDate( Date::SYSTEM );
     199           0 :         static ::com::sun::star::util::Date STANDARD_DB_DATE(30,12,1899);
     200           0 :         nValue = ::dbtools::DBTypeConversion::toDouble(::dbtools::DBTypeConversion::toDate(static_cast<sal_Int32>(aCurrentDate.GetDate())),STANDARD_DB_DATE);
     201             :     }
     202             : 
     203           0 :     uno::Reference< util::XNumberFormatPreviewer> xPreviewer(m_pController->getReportNumberFormatter(),uno::UNO_QUERY);
     204             :     OSL_ENSURE(xPreviewer.is(),"XNumberFormatPreviewer is null!");
     205           0 :     return xPreviewer->convertNumberToPreviewString(sFormat,nValue,m_nLocale,sal_True);
     206             : }
     207             : 
     208           0 : IMPL_LINK( ODateTimeDialog, CBClickHdl, CheckBox*, _pBox )
     209             : {
     210             :    (void)_pBox;
     211             : 
     212           0 :      if ( _pBox == m_pDate || _pBox == m_pTime)
     213             :      {
     214           0 :          bool bDate = m_pDate->IsChecked();
     215           0 :         bool bTime = m_pTime->IsChecked();
     216           0 :         if (!bDate && !bTime)
     217             :         {
     218           0 :             m_pPB_OK->Disable();
     219             :         }
     220             :         else
     221             :         {
     222           0 :             m_pPB_OK->Enable();
     223             :         }
     224             :     }
     225           0 :     return 1L;
     226             : }
     227             : 
     228           0 : sal_Int32 ODateTimeDialog::getFormatKey(bool _bDate) const
     229             : {
     230             :     sal_Int32 nFormatKey;
     231           0 :     if ( _bDate )
     232             :     {
     233           0 :          nFormatKey = static_cast<sal_Int32>(reinterpret_cast<sal_IntPtr>(m_pDateListBox->GetSelectEntryData()));
     234             :     }
     235             :     else
     236             :     {
     237           0 :          nFormatKey = static_cast<sal_Int32>(reinterpret_cast<sal_IntPtr>(m_pTimeListBox->GetSelectEntryData()));
     238             :     }
     239           0 :     return nFormatKey;
     240             : }
     241             : 
     242           3 : } // rptui
     243             : 
     244             : 
     245             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11