LCOV - code coverage report
Current view: top level - writerfilter/source/dmapper - FormControlHelper.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 24 166 14.5 %
Date: 2012-08-25 Functions: 7 13 53.8 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 24 364 6.6 %

           Branch data     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                 :            : 
      20                 :            : #include <math.h>
      21                 :            : 
      22                 :            : #include <com/sun/star/awt/XControlModel.hpp>
      23                 :            : #include <com/sun/star/beans/XPropertySet.hpp>
      24                 :            : #include <com/sun/star/container/XIndexContainer.hpp>
      25                 :            : #include <com/sun/star/container/XNamed.hpp>
      26                 :            : #include <com/sun/star/drawing/XControlShape.hpp>
      27                 :            : #include <com/sun/star/drawing/XDrawPage.hpp>
      28                 :            : #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
      29                 :            : #include <com/sun/star/form/XForm.hpp>
      30                 :            : #include <com/sun/star/form/XFormComponent.hpp>
      31                 :            : #include <com/sun/star/form/XFormsSupplier.hpp>
      32                 :            : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      33                 :            : #include <com/sun/star/text/TextContentAnchorType.hpp>
      34                 :            : #include <com/sun/star/text/VertOrientation.hpp>
      35                 :            : #include <com/sun/star/uno/Any.hxx>
      36                 :            : #include <com/sun/star/uno/Type.hxx>
      37                 :            : 
      38                 :            : #include "FormControlHelper.hxx"
      39                 :            : #include <xmloff/odffields.hxx>
      40                 :            : #include <comphelper/stlunosequence.hxx>
      41                 :            : 
      42                 :            : namespace writerfilter {
      43                 :            : namespace dmapper {
      44                 :            : 
      45                 :            : using namespace ::com::sun::star;
      46                 :            : 
      47                 :          6 : struct FormControlHelper::FormControlHelper_Impl
      48                 :            : {
      49                 :            :     FieldId m_eFieldId;
      50                 :            :     awt::Size aSize;
      51                 :            :     uno::Reference<drawing::XDrawPage> rDrawPage;
      52                 :            :     uno::Reference<form::XForm> rForm;
      53                 :            :     uno::Reference<form::XFormComponent> rFormComponent;
      54                 :            :     uno::Reference<lang::XMultiServiceFactory> rServiceFactory;
      55                 :            :     uno::Reference<text::XTextDocument> rTextDocument;
      56                 :            : 
      57                 :            :     uno::Reference<drawing::XDrawPage> getDrawPage();
      58                 :            :     uno::Reference<lang::XMultiServiceFactory> getServiceFactory();
      59                 :            :     uno::Reference<form::XForm> getForm();
      60                 :            :     uno::Reference<container::XIndexContainer> getFormComps();
      61                 :            : };
      62                 :            : 
      63                 :          0 : uno::Reference<drawing::XDrawPage> FormControlHelper::FormControlHelper_Impl::getDrawPage()
      64                 :            : {
      65         [ #  # ]:          0 :     if (! rDrawPage.is())
      66                 :            :     {
      67                 :            :         uno::Reference<drawing::XDrawPageSupplier>
      68         [ #  # ]:          0 :             xDrawPageSupplier(rTextDocument, uno::UNO_QUERY);
      69         [ #  # ]:          0 :         if (xDrawPageSupplier.is())
      70 [ #  # ][ #  # ]:          0 :             rDrawPage = xDrawPageSupplier->getDrawPage();
                 [ #  # ]
      71                 :            :     }
      72                 :            : 
      73                 :          0 :     return rDrawPage;
      74                 :            : }
      75                 :            : 
      76                 :          0 : uno::Reference<lang::XMultiServiceFactory> FormControlHelper::FormControlHelper_Impl::getServiceFactory()
      77                 :            : {
      78         [ #  # ]:          0 :     if (! rServiceFactory.is())
      79         [ #  # ]:          0 :         rServiceFactory = uno::Reference<lang::XMultiServiceFactory>(rTextDocument, uno::UNO_QUERY);
      80                 :            : 
      81                 :          0 :     return rServiceFactory;
      82                 :            : }
      83                 :            : 
      84                 :          0 : uno::Reference<form::XForm> FormControlHelper::FormControlHelper_Impl::getForm()
      85                 :            : {
      86         [ #  # ]:          0 :     if (! rForm.is())
      87                 :            :     {
      88 [ #  # ][ #  # ]:          0 :         uno::Reference<form::XFormsSupplier> xFormsSupplier(getDrawPage(), uno::UNO_QUERY);
      89                 :            : 
      90         [ #  # ]:          0 :         if (xFormsSupplier.is())
      91                 :            :         {
      92 [ #  # ][ #  # ]:          0 :             uno::Reference<container::XNameContainer> xFormsNamedContainer(xFormsSupplier->getForms());
      93 [ #  # ][ #  # ]:          0 :             static OUString sDOCXForm("DOCX-Standard");
      94                 :            : 
      95                 :          0 :             OUString sFormName(sDOCXForm);
      96                 :          0 :             sal_uInt16 nUnique = 0;
      97                 :            : 
      98 [ #  # ][ #  # ]:          0 :             while (xFormsNamedContainer->hasByName(sFormName))
                 [ #  # ]
      99                 :            :             {
     100                 :          0 :                 ++nUnique;
     101                 :          0 :                 sFormName = sDOCXForm;
     102                 :          0 :                 sFormName += OUString::valueOf(nUnique);
     103                 :            :             }
     104                 :            : 
     105 [ #  # ][ #  # ]:          0 :             uno::Reference<uno::XInterface> xForm(getServiceFactory()->createInstance("com.sun.star.form.component.Form"));
                 [ #  # ]
     106         [ #  # ]:          0 :             if (xForm.is())
     107                 :            :             {
     108                 :            :                 uno::Reference<beans::XPropertySet>
     109         [ #  # ]:          0 :                     xFormProperties(xForm, uno::UNO_QUERY);
     110         [ #  # ]:          0 :                 uno::Any aAny(sFormName);
     111 [ #  # ][ #  # ]:          0 :                 xFormProperties->setPropertyValue("Name", aAny);
     112                 :            :             }
     113                 :            : 
     114 [ #  # ][ #  # ]:          0 :             rForm = uno::Reference<form::XForm>(xForm, uno::UNO_QUERY);
     115                 :            : 
     116         [ #  # ]:          0 :             uno::Reference<container::XIndexContainer> xForms(xFormsNamedContainer, uno::UNO_QUERY);
     117         [ #  # ]:          0 :             uno::Any aAny(xForm);
     118 [ #  # ][ #  # ]:          0 :             xForms->insertByIndex(xForms->getCount(), aAny);
         [ #  # ][ #  # ]
     119                 :          0 :         }
     120                 :            :     }
     121                 :            : 
     122                 :          0 :     return rForm;
     123                 :            : }
     124                 :            : 
     125                 :          0 : uno::Reference<container::XIndexContainer> FormControlHelper::FormControlHelper_Impl::getFormComps()
     126                 :            : {
     127         [ #  # ]:          0 :     uno::Reference<container::XIndexContainer> xIndexContainer(getForm(), uno::UNO_QUERY);
     128                 :            : 
     129                 :          0 :     return xIndexContainer;
     130                 :            : }
     131                 :            : 
     132                 :          3 : FormControlHelper::FormControlHelper(FieldId eFieldId,
     133                 :            :                                      uno::Reference<text::XTextDocument> rTextDocument,
     134                 :            :                                      FFDataHandler::Pointer_t pFFData)
     135 [ +  - ][ +  - ]:          3 :     : m_pFFData(pFFData), m_pImpl(new FormControlHelper_Impl)
                 [ +  - ]
     136                 :            : {
     137                 :          3 :     m_pImpl->m_eFieldId = eFieldId;
     138         [ +  - ]:          3 :     m_pImpl->rTextDocument = rTextDocument;
     139                 :          3 : }
     140                 :            : 
     141         [ +  - ]:          3 : FormControlHelper::~FormControlHelper()
     142                 :            : {
     143                 :          3 : }
     144                 :            : 
     145                 :          0 : bool FormControlHelper::createCheckbox(uno::Reference<text::XTextRange> xTextRange,
     146                 :            :                                        const OUString & rControlName)
     147                 :            : {
     148         [ #  # ]:          0 :     if ( !m_pFFData )
     149                 :          0 :         return false;
     150                 :            :     uno::Reference<lang::XMultiServiceFactory>
     151         [ #  # ]:          0 :         xServiceFactory(m_pImpl->getServiceFactory());
     152                 :            : 
     153         [ #  # ]:          0 :     if (! xServiceFactory.is())
     154                 :          0 :         return false;
     155                 :            : 
     156 [ #  # ][ #  # ]:          0 :     uno::Reference<uno::XInterface> xInterface = xServiceFactory->createInstance("com.sun.star.form.component.CheckBox");
     157                 :            : 
     158         [ #  # ]:          0 :     if (!xInterface.is())
     159                 :          0 :         return false;
     160                 :            : 
     161 [ #  # ][ #  # ]:          0 :     m_pImpl->rFormComponent = uno::Reference<form::XFormComponent>(xInterface, uno::UNO_QUERY);
     162         [ #  # ]:          0 :     if (!m_pImpl->rFormComponent.is())
     163                 :          0 :         return false;
     164                 :            : 
     165         [ #  # ]:          0 :     uno::Reference<beans::XPropertySet> xPropSet(xInterface, uno::UNO_QUERY);
     166                 :            : 
     167         [ #  # ]:          0 :     sal_uInt32 nCheckBoxHeight = 16 * m_pFFData->getCheckboxHeight();
     168                 :            : 
     169 [ #  # ][ #  # ]:          0 :     if (m_pFFData->getCheckboxAutoHeight())
     170                 :            :     {
     171         [ #  # ]:          0 :         uno::Reference<beans::XPropertySet> xTextRangeProps(xTextRange, uno::UNO_QUERY);
     172                 :            : 
     173                 :            :         try
     174                 :            :         {
     175                 :          0 :             float fCheckBoxHeight = 0.0;
     176 [ #  # ][ #  # ]:          0 :             xTextRangeProps->getPropertyValue("CharHeight") >>= fCheckBoxHeight;
                 [ #  # ]
     177                 :          0 :             nCheckBoxHeight = static_cast<sal_uInt32>(floor(fCheckBoxHeight * 35.3));
     178                 :            :         }
     179         [ #  # ]:          0 :         catch (beans::UnknownPropertyException &)
     180                 :            :         {
     181                 :          0 :         }
     182                 :            :     }
     183                 :            : 
     184                 :          0 :     m_pImpl->aSize.Width = nCheckBoxHeight;
     185                 :          0 :     m_pImpl->aSize.Height = m_pImpl->aSize.Width;
     186                 :            : 
     187                 :          0 :     uno::Any aAny;
     188 [ #  # ][ #  # ]:          0 :     if (!m_pFFData->getStatusText().isEmpty())
     189                 :            :     {
     190 [ #  # ][ #  # ]:          0 :         aAny <<= m_pFFData->getStatusText();
     191                 :            : 
     192 [ #  # ][ #  # ]:          0 :         xPropSet->setPropertyValue("HelpText", aAny);
     193                 :            :     }
     194                 :            : 
     195 [ #  # ][ #  # ]:          0 :     aAny <<= m_pFFData->getCheckboxChecked();
     196 [ #  # ][ #  # ]:          0 :     xPropSet->setPropertyValue("DefaultState", aAny);
     197                 :            : 
     198 [ #  # ][ #  # ]:          0 :     if (!m_pFFData->getHelpText().isEmpty())
     199                 :            :     {
     200 [ #  # ][ #  # ]:          0 :         aAny <<= m_pFFData->getHelpText();
     201 [ #  # ][ #  # ]:          0 :         xPropSet->setPropertyValue("HelpF1Text", aAny);
     202                 :            :     }
     203                 :            : 
     204         [ #  # ]:          0 :     aAny <<= rControlName;
     205 [ #  # ][ #  # ]:          0 :     xPropSet->setPropertyValue("Name", aAny);
     206                 :            : 
     207                 :          0 :     return true;
     208                 :            : }
     209                 :            : 
     210                 :          3 : bool FormControlHelper::processField(uno::Reference<text::XFormField> xFormField)
     211                 :            : {
     212                 :          3 :     bool bRes = true;
     213 [ +  - ][ +  - ]:          3 :     uno::Reference<container::XNameContainer> xNameCont = xFormField->getParameters();
     214         [ +  - ]:          3 :     uno::Reference<container::XNamed> xNamed( xFormField, uno::UNO_QUERY );
     215 [ +  - ][ +  - ]:          3 :     if ( m_pFFData && xNamed.is() && xNameCont.is() )
         [ +  - ][ +  - ]
     216                 :            :     {
     217                 :            : 
     218         [ -  + ]:          3 :         if (m_pImpl->m_eFieldId == FIELD_FORMTEXT )
     219                 :            :         {
     220 [ #  # ][ #  # ]:          0 :             xFormField->setFieldType(ODF_FORMTEXT);
     221 [ #  # ][ #  # ]:          0 :             if (  !m_pFFData->getName().isEmpty() )
     222                 :            :             {
     223 [ #  # ][ #  # ]:          0 :                 xNamed->setName( m_pFFData->getName() );
                 [ #  # ]
     224                 :            :             }
     225                 :            :         }
     226         [ +  - ]:          3 :         else if (m_pImpl->m_eFieldId == FIELD_FORMCHECKBOX )
     227                 :            :         {
     228 [ +  - ][ +  - ]:          3 :             xFormField->setFieldType(ODF_FORMCHECKBOX);
     229         [ +  - ]:          3 :             uno::Reference<beans::XPropertySet> xPropSet(xFormField, uno::UNO_QUERY);
     230                 :          3 :             uno::Any aAny;
     231 [ +  - ][ +  - ]:          3 :             aAny <<= m_pFFData->getCheckboxChecked();
     232         [ +  - ]:          3 :             if ( xPropSet.is() )
     233 [ +  - ][ +  - ]:          3 :                 xPropSet->setPropertyValue("Checked", aAny);
     234                 :          3 :             OUString sName;
     235                 :            :         }
     236         [ #  # ]:          0 :         else if (m_pImpl->m_eFieldId == FIELD_FORMDROPDOWN )
     237                 :            :         {
     238 [ #  # ][ #  # ]:          0 :             xFormField->setFieldType(ODF_FORMDROPDOWN);
     239         [ #  # ]:          0 :             uno::Sequence< OUString > sItems;
     240 [ #  # ][ #  # ]:          0 :             sItems.realloc( m_pFFData->getDropDownEntries().size() );
     241 [ #  # ][ #  # ]:          0 :             ::std::copy( m_pFFData->getDropDownEntries().begin(), m_pFFData->getDropDownEntries().end(), ::comphelper::stl_begin(sItems));
         [ #  # ][ #  # ]
     242         [ #  # ]:          0 :             if ( sItems.getLength() )
     243                 :            :             {
     244 [ #  # ][ #  # ]:          0 :                 if ( xNameCont->hasByName(ODF_FORMDROPDOWN_LISTENTRY) )
                 [ #  # ]
     245 [ #  # ][ #  # ]:          0 :                     xNameCont->replaceByName(ODF_FORMDROPDOWN_LISTENTRY, uno::makeAny( sItems ) );
                 [ #  # ]
     246                 :            :                 else
     247 [ #  # ][ #  # ]:          0 :                     xNameCont->insertByName(ODF_FORMDROPDOWN_LISTENTRY, uno::makeAny( sItems ) );
                 [ #  # ]
     248                 :            : 
     249         [ #  # ]:          0 :                 sal_Int32 nResult = m_pFFData->getDropDownResult().toInt32();
     250         [ #  # ]:          0 :                 if ( nResult )
     251                 :            :                 {
     252 [ #  # ][ #  # ]:          0 :                     if ( xNameCont->hasByName(ODF_FORMDROPDOWN_RESULT) )
                 [ #  # ]
     253 [ #  # ][ #  # ]:          0 :                         xNameCont->replaceByName(ODF_FORMDROPDOWN_RESULT, uno::makeAny( nResult ) );
                 [ #  # ]
     254                 :            :                     else
     255 [ #  # ][ #  # ]:          0 :                         xNameCont->insertByName(ODF_FORMDROPDOWN_RESULT, uno::makeAny( nResult ) );
                 [ #  # ]
     256                 :            :                 }
     257         [ #  # ]:          0 :             }
     258                 :            :         }
     259                 :            :     }
     260                 :            :     else
     261                 :          0 :         bRes = false;
     262                 :          3 :     return bRes;
     263                 :            : }
     264                 :            : 
     265                 :          0 : bool FormControlHelper::insertControl(uno::Reference<text::XTextRange> xTextRange)
     266                 :            : {
     267                 :          0 :     bool bCreated = false;
     268         [ #  # ]:          0 :     if ( !m_pFFData )
     269                 :          0 :         return false;
     270 [ #  # ][ #  # ]:          0 :     uno::Reference<container::XNameContainer> xFormCompsByName(m_pImpl->getForm(), uno::UNO_QUERY);
     271         [ #  # ]:          0 :     uno::Reference<container::XIndexContainer> xFormComps(m_pImpl->getFormComps());
     272         [ #  # ]:          0 :     if (! xFormComps.is())
     273                 :          0 :         return false;
     274                 :            : 
     275 [ #  # ][ #  # ]:          0 :     static OUString sControl("Control");
     276                 :            : 
     277                 :          0 :     sal_Int32 nControl = 0;
     278                 :          0 :     bool bDone = false;
     279                 :          0 :     OUString sControlName;
     280                 :            : 
     281         [ #  # ]:          0 :     do
     282                 :            :     {
     283                 :          0 :         OUString sTmp(sControl);
     284                 :          0 :         sTmp += OUString::valueOf(nControl);
     285                 :            : 
     286                 :          0 :         nControl++;
     287 [ #  # ][ #  # ]:          0 :         if (! xFormCompsByName->hasByName(sTmp))
                 [ #  # ]
     288                 :            :         {
     289                 :          0 :             sControlName = sTmp;
     290                 :          0 :             bDone = true;
     291                 :          0 :         }
     292                 :            :     }
     293                 :          0 :     while (! bDone);
     294                 :            : 
     295         [ #  # ]:          0 :     switch (m_pImpl->m_eFieldId)
     296                 :            :     {
     297                 :            :     case FIELD_FORMCHECKBOX:
     298         [ #  # ]:          0 :         bCreated = createCheckbox(xTextRange, sControlName);
     299                 :          0 :         break;
     300                 :            :     default:
     301                 :          0 :         break;
     302                 :            :     }
     303                 :            : 
     304         [ #  # ]:          0 :     if (!bCreated)
     305                 :          0 :         return false;
     306                 :            : 
     307         [ #  # ]:          0 :     uno::Any aAny(m_pImpl->rFormComponent);
     308 [ #  # ][ #  # ]:          0 :     xFormComps->insertByIndex(xFormComps->getCount(), aAny);
         [ #  # ][ #  # ]
     309                 :            : 
     310 [ #  # ][ #  # ]:          0 :     if (! m_pImpl->getServiceFactory().is())
     311                 :          0 :         return false;
     312                 :            : 
     313 [ #  # ][ #  # ]:          0 :     uno::Reference<uno::XInterface> xInterface = m_pImpl->getServiceFactory()->createInstance("com.sun.star.drawing.ControlShape");
                 [ #  # ]
     314                 :            : 
     315         [ #  # ]:          0 :     if (! xInterface.is())
     316                 :          0 :         return false;
     317                 :            : 
     318         [ #  # ]:          0 :     uno::Reference<drawing::XShape> xShape(xInterface, uno::UNO_QUERY);
     319                 :            : 
     320         [ #  # ]:          0 :     if (! xShape.is())
     321                 :          0 :         return false;
     322                 :            : 
     323 [ #  # ][ #  # ]:          0 :     xShape->setSize(m_pImpl->aSize);
     324                 :            : 
     325         [ #  # ]:          0 :     uno::Reference<beans::XPropertySet> xShapeProps(xShape, uno::UNO_QUERY);
     326                 :            : 
     327                 :          0 :     sal_uInt16 nTmp = text::TextContentAnchorType_AS_CHARACTER;
     328         [ #  # ]:          0 :     aAny <<= nTmp;
     329                 :            : 
     330 [ #  # ][ #  # ]:          0 :     xShapeProps->setPropertyValue("AnchorType", aAny);
     331                 :            : 
     332                 :          0 :     nTmp = text::VertOrientation::CENTER;
     333         [ #  # ]:          0 :     aAny <<= nTmp;
     334 [ #  # ][ #  # ]:          0 :     xShapeProps->setPropertyValue("VertOrient", aAny);
     335                 :            : 
     336         [ #  # ]:          0 :     aAny <<= xTextRange;
     337                 :            : 
     338 [ #  # ][ #  # ]:          0 :     xShapeProps->setPropertyValue("TextRange", aAny);
     339                 :            : 
     340         [ #  # ]:          0 :     uno::Reference<drawing::XControlShape> xControlShape(xShape, uno::UNO_QUERY);
     341         [ #  # ]:          0 :     uno::Reference<awt::XControlModel> xControlModel(m_pImpl->rFormComponent, uno::UNO_QUERY);
     342 [ #  # ][ #  # ]:          0 :     xControlShape->setControl(xControlModel);
     343                 :            : 
     344 [ #  # ][ #  # ]:          0 :     m_pImpl->getDrawPage()->add(xShape);
                 [ #  # ]
     345                 :            : 
     346                 :          0 :     return true;
     347                 :            : }
     348                 :            : 
     349 [ +  - ][ +  - ]:         60 : }}
     350                 :            : 
     351                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10