LibreOffice Module reportdesign (master)  1
FormattedFieldBeautifier.cxx
Go to the documentation of this file.
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 
22 
23 #include <com/sun/star/report/XFormattedField.hpp>
24 #include <com/sun/star/awt/XVclWindowPeer.hpp>
25 
26 #include <RptObject.hxx>
27 #include <RptModel.hxx>
28 #include <RptPage.hxx>
29 #include <ReportSection.hxx>
30 #include <ReportController.hxx>
31 #include <strings.hxx>
32 #include <reportformula.hxx>
33 
34 #include <svtools/extcolorcfg.hxx>
35 
36 // DBG_UNHANDLED_EXCEPTION
37 #include <tools/diagnose_ex.h>
38 
39 namespace rptui
40 {
41  using namespace ::com::sun::star;
42 
43 
45  :m_rReportController(_aController)
46  ,m_nTextColor(COL_AUTO)
47  {
48  }
49 
50 
52  {
53  if (m_nTextColor == COL_AUTO)
54  {
57  }
58  return m_nTextColor;
59  }
60 
61 
63  {
64  }
65 
66 
67  void FormattedFieldBeautifier::setPlaceholderText( const uno::Reference< uno::XInterface >& _rxComponent )
68  {
69  try
70  {
71  uno::Reference< report::XFormattedField > xControlModel( _rxComponent, uno::UNO_QUERY );
72  if ( xControlModel.is() )
73  {
74  OUString sDataField = xControlModel->getDataField();
75 
76  if ( !sDataField.isEmpty() )
77  {
78  ReportFormula aFormula( sDataField );
79  bool bSet = true;
80  if ( aFormula.getType() == ReportFormula::Field )
81  {
82  const OUString& sColumnName = aFormula.getFieldName();
83  OUString sLabel = m_rReportController.getColumnLabel_throw(sColumnName);
84  if ( !sLabel.isEmpty() )
85  {
86  sDataField = "=" + sLabel;
87  bSet = false;
88  }
89  }
90  if ( bSet )
91  sDataField = aFormula.getEqualUndecoratedContent();
92  }
93 
94  setPlaceholderText( getVclWindowPeer( xControlModel ), sDataField );
95  }
96  }
97  catch (const uno::Exception &)
98  {
99  DBG_UNHANDLED_EXCEPTION("reportdesign");
100  }
101  }
102 
103 
104  void FormattedFieldBeautifier::setPlaceholderText( const uno::Reference< awt::XVclWindowPeer >& _xVclWindowPeer, const OUString& _rText )
105  {
106  OSL_ENSURE( _xVclWindowPeer.is(), "FormattedFieldBeautifier::setPlaceholderText: invalid peer!" );
107  if ( !_xVclWindowPeer.is() )
108  throw uno::RuntimeException();
109 
110  // the actual text
111  _xVclWindowPeer->setProperty(PROPERTY_TEXT, uno::makeAny(_rText));
112  // the text color
113  _xVclWindowPeer->setProperty(PROPERTY_TEXTCOLOR, uno::makeAny(getTextColor()));
114  // font->italic
115  uno::Any aFontDescriptor = _xVclWindowPeer->getProperty(PROPERTY_FONTDESCRIPTOR);
116  awt::FontDescriptor aFontDescriptorStructure;
117  aFontDescriptor >>= aFontDescriptorStructure;
118  aFontDescriptorStructure.Slant = css::awt::FontSlant_ITALIC;
119  _xVclWindowPeer->setProperty(PROPERTY_FONTDESCRIPTOR, uno::makeAny(aFontDescriptorStructure));
120  }
121 
122 
123  void FormattedFieldBeautifier::notifyPropertyChange( const beans::PropertyChangeEvent& _rEvent )
124  {
125  if ( _rEvent.PropertyName != "DataField" )
126  // not interested in
127  return;
128 
129  setPlaceholderText( _rEvent.Source );
130  }
131 
132 
133  void FormattedFieldBeautifier::handle( const uno::Reference< uno::XInterface >& _rxElement )
134  {
135  setPlaceholderText( _rxElement );
136  }
137 
138 
139  void FormattedFieldBeautifier::notifyElementInserted( const uno::Reference< uno::XInterface >& _rxElement )
140  {
141  handle( _rxElement );
142  }
143 
144 
145  uno::Reference<awt::XVclWindowPeer> FormattedFieldBeautifier::getVclWindowPeer(const uno::Reference< report::XReportComponent >& _xComponent)
146  {
147  uno::Reference<awt::XVclWindowPeer> xVclWindowPeer;
148 
149  std::shared_ptr<OReportModel> pModel = m_rReportController.getSdrModel();
150 
151  uno::Reference<report::XSection> xSection(_xComponent->getSection());
152  if ( xSection.is() )
153  {
154  OReportPage *pPage = pModel->getPage(xSection);
155  const size_t nIndex = pPage->getIndexOf(_xComponent);
156  if (nIndex < pPage->GetObjCount() )
157  {
158  SdrObject *pObject = pPage->GetObj(nIndex);
159  OUnoObject* pUnoObj = dynamic_cast<OUnoObject*>(pObject);
160  if ( pUnoObj ) // this doesn't need to be done for shapes
161  {
162  OSectionWindow* pSectionWindow = m_rReportController.getSectionWindow(xSection);
163  if (pSectionWindow != nullptr)
164  {
165  OReportSection& aOutputDevice = pSectionWindow->getReportSection(); // OutputDevice
166  OSectionView& aSdrView = aOutputDevice.getSectionView(); // SdrView
167  uno::Reference<awt::XControl> xControl = pUnoObj->GetUnoControl(aSdrView, aOutputDevice);
168  xVclWindowPeer.set( xControl->getPeer(), uno::UNO_QUERY);
169  }
170  }
171  }
172  }
173  return xVclWindowPeer;
174  }
175 }
176 
177 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int32 nIndex
FormattedFieldBeautifier(const OReportController &_aObserver)
css::uno::Reference< css::awt::XVclWindowPeer > getVclWindowPeer(const css::uno::Reference< css::report::XReportComponent > &_xComponent)
constexpr::Color COL_AUTO(ColorTransparency, 0xFF, 0xFF, 0xFF, 0xFF)
SdrObject * GetObj(size_t nNum) const
size_t getIndexOf(const css::uno::Reference< css::report::XReportComponent > &_xObject)
returns the index inside the object list which belongs to the report component.
Definition: RptPage.cxx:56
void notifyPropertyChange(const css::beans::PropertyChangeEvent &_rEvent) override
void setPlaceholderText(const css::uno::Reference< css::uno::XInterface > &_rxComponent)
EmbeddedObjectRef * pObject
OSectionWindow * getSectionWindow(const css::uno::Reference< css::report::XSection > &_xSection) const
#define DBTEXTBOXBOUNDCONTENT
Definition: strings.hxx:275
#define PROPERTY_TEXTCOLOR
Definition: strings.hxx:202
OReportSection & getReportSection()
void notifyElementInserted(const css::uno::Reference< css::uno::XInterface > &_rxElement) override
#define DBG_UNHANDLED_EXCEPTION(...)
#define CFG_REPORTDESIGNER
Definition: strings.hxx:251
void handle(const css::uno::Reference< css::uno::XInterface > &_rxElement) override
#define PROPERTY_FONTDESCRIPTOR
Definition: strings.hxx:80
const OReportController & m_rReportController
css::uno::Reference< css::awt::XControl > GetUnoControl(const SdrView &_rView, const OutputDevice &_rOut) const
ExtendedColorConfigValue GetColorValue(const OUString &_sComponentName, const OUString &_sName) const
#define PROPERTY_TEXT
Definition: strings.hxx:201
OUString getColumnLabel_throw(const OUString &i_sColumnName) const
const std::shared_ptr< rptui::OReportModel > & getSdrModel() const
return the SdrModel of the real model
OSectionView & getSectionView() const