Branch libreoffice-5-0-4
[LibreOffice.git] / reportdesign / source / core / misc / reportformula.cxx
blob77d24b853751189f0c438f6dbac5473855ad9509
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "reportformula.hxx"
22 #include <rtl/ustrbuf.hxx>
25 namespace rptui
29 using ::com::sun::star::uno::Any;
31 namespace
34 static const char sExpressionPrefix[] = "rpt:";
35 static const char sFieldPrefix[] = "field:";
39 //= ReportFormula
42 ReportFormula::ReportFormula( const OUString& _rFormula )
43 :m_eType( Invalid )
45 impl_construct( _rFormula );
49 ReportFormula::ReportFormula( const BindType _eType, const OUString& _rFieldOrExpression )
50 :m_eType( _eType )
52 switch ( m_eType )
54 case Expression:
56 if ( _rFieldOrExpression.startsWith( sExpressionPrefix ) )
57 m_sCompleteFormula = _rFieldOrExpression;
58 else
59 m_sCompleteFormula = sExpressionPrefix + _rFieldOrExpression;
61 break;
63 case Field:
65 OUStringBuffer aBuffer;
66 aBuffer.append( sFieldPrefix );
67 aBuffer.appendAscii( "[" );
68 aBuffer.append( _rFieldOrExpression );
69 aBuffer.appendAscii( "]" );
70 m_sCompleteFormula = aBuffer.makeStringAndClear();
72 break;
73 default:
74 OSL_FAIL( "ReportFormula::ReportFormula: illegal bind type!" );
75 return;
78 m_sUndecoratedContent = _rFieldOrExpression;
81 ReportFormula::~ReportFormula()
85 void ReportFormula::impl_construct( const OUString& _rFormula )
87 m_sCompleteFormula = _rFormula;
89 // is it an ordinary expression?
90 if ( m_sCompleteFormula.startsWith( sExpressionPrefix ) )
92 sal_Int32 nPrefixLen = strlen(sExpressionPrefix);
93 m_eType = Expression;
94 m_sUndecoratedContent = m_sCompleteFormula.copy( nPrefixLen );
95 return;
98 /// does it refer to a field?
99 if ( m_sCompleteFormula.startsWith( sFieldPrefix ) )
101 sal_Int32 nPrefixLen = strlen(sFieldPrefix);
102 if ( ( m_sCompleteFormula.getLength() >= nPrefixLen + 2 )
103 && ( m_sCompleteFormula[ nPrefixLen ] == '[' )
104 && ( m_sCompleteFormula[ m_sCompleteFormula.getLength() - 1 ] == ']' )
107 m_eType = Field;
108 m_sUndecoratedContent = m_sCompleteFormula.copy( nPrefixLen + 1, m_sCompleteFormula.getLength() - nPrefixLen - 2 );
109 return;
113 m_eType = Invalid;
117 OUString ReportFormula::getBracketedFieldOrExpression() const
119 bool bIsField = ( getType() == Field );
120 OUStringBuffer aFieldContent;
121 if ( bIsField )
122 aFieldContent.appendAscii( "[" );
123 aFieldContent.append( getUndecoratedContent() );
124 if ( bIsField )
125 aFieldContent.appendAscii( "]" );
127 return aFieldContent.makeStringAndClear();
130 bool ReportFormula::isValid() const { return getType() != Invalid; }
131 ReportFormula& ReportFormula::operator=(class ReportFormula const & _rHd)
133 if ( this == &_rHd )
134 return *this;
135 m_eType = _rHd.m_eType;
136 m_sCompleteFormula = _rHd.m_sCompleteFormula;
137 m_sUndecoratedContent = _rHd.m_sUndecoratedContent;
138 return *this;
141 OUString ReportFormula::getEqualUndecoratedContent() const
143 OUStringBuffer aBuffer;
144 aBuffer.appendAscii( "=" );
145 aBuffer.append( getUndecoratedContent() );
146 return aBuffer.makeStringAndClear();
150 } // namespace rptui
153 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */