LibreOffice Module svx (master)  1
DescriptionGenerator.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 #include <DescriptionGenerator.hxx>
21 #include <com/sun/star/beans/PropertyState.hpp>
22 #include <com/sun/star/beans/XPropertySet.hpp>
23 #include <com/sun/star/beans/XPropertyState.hpp>
24 #include <com/sun/star/container/XNamed.hpp>
25 #include <com/sun/star/drawing/XShape.hpp>
26 #include <vcl/svapp.hxx>
27 
28 // Includes for string resources.
29 #include <svx/strings.hrc>
30 #include <svx/dialmgr.hxx>
31 
32 #include "lookupcolorname.hxx"
33 
34 using namespace ::com::sun::star;
35 
36 namespace accessibility
37 {
38 DescriptionGenerator::DescriptionGenerator(const uno::Reference<drawing::XShape>& xShape)
39  : mxShape(xShape)
40  , mxSet(mxShape, uno::UNO_QUERY)
41  , mbIsFirstProperty(true)
42 {
43 }
44 
46 
47 void DescriptionGenerator::Initialize(const char* pResourceId)
48 {
49  // Get the string from the resource for the specified id.
50  OUString sPrefix;
51  {
52  SolarMutexGuard aGuard;
53  sPrefix = SvxResId(pResourceId);
54  }
55 
56  // Forward the call with the resulting string.
57  Initialize(sPrefix);
58 }
59 
61 {
63  if (!mxSet.is())
64  return;
65 
66  {
67  SolarMutexGuard aGuard;
68 
69  msDescription.append(' ');
70  msDescription.append(SvxResId(RID_SVXSTR_A11Y_WITH));
71  msDescription.append(' ');
72 
73  msDescription.append(SvxResId(RID_SVXSTR_A11Y_STYLE));
74  msDescription.append('=');
75  }
76 
77  try
78  {
79  if (mxSet.is())
80  {
81  uno::Any aValue = mxSet->getPropertyValue("Style");
82  uno::Reference<container::XNamed> xStyle(aValue, uno::UNO_QUERY);
83  if (xStyle.is())
84  msDescription.append(xStyle->getName());
85  }
86  else
87  msDescription.append("<no style>");
88  }
89  catch (const css::beans::UnknownPropertyException&)
90  {
91  msDescription.append("<unknown>");
92  }
93 }
94 
96 {
97  msDescription.append('.');
98  return msDescription.makeStringAndClear();
99 }
100 
101 void DescriptionGenerator::AddProperty(const OUString& sPropertyName, PropertyType aType)
102 {
103  uno::Reference<beans::XPropertyState> xState(mxShape, uno::UNO_QUERY);
104  if (!xState.is()
105  || xState->getPropertyState(sPropertyName) == beans::PropertyState_DEFAULT_VALUE)
106  return;
107 
108  if (!mxSet.is())
109  return;
110 
111  // Append a separator from previous Properties.
112  if (!mbIsFirstProperty)
113  msDescription.append(',');
114  else
115  {
116  SolarMutexGuard aGuard;
117 
118  msDescription.append(' ');
119  msDescription.append(SvxResId(RID_SVXSTR_A11Y_AND));
120  msDescription.append(' ');
121  mbIsFirstProperty = false;
122  }
123 
124  // Delegate to type specific property handling.
125  switch (aType)
126  {
127  case PropertyType::Color:
128  AddColor(sPropertyName);
129  break;
131  AddInteger(sPropertyName);
132  break;
133  }
134 }
135 
136 void DescriptionGenerator::AppendString(std::u16string_view sString)
137 {
138  msDescription.append(sString);
139 }
140 
144 void DescriptionGenerator::AddColor(const OUString& sPropertyName)
145 {
146  msDescription.append('=');
147 
148  try
149  {
150  tools::Long nValue(0);
151  if (mxSet.is())
152  {
153  uno::Any aValue = mxSet->getPropertyValue(sPropertyName);
154  aValue >>= nValue;
155  }
156 
157  msDescription.append(lookUpColorName(nValue));
158  }
159  catch (const css::beans::UnknownPropertyException&)
160  {
161  msDescription.append("<unknown>");
162  }
163 }
164 
165 void DescriptionGenerator::AddInteger(const OUString& sPropertyName)
166 {
167  msDescription.append('=');
168 
169  try
170  {
171  if (mxSet.is())
172  {
173  uno::Any aValue = mxSet->getPropertyValue(sPropertyName);
174  tools::Long nValue = 0;
175  aValue >>= nValue;
176  msDescription.append(nValue);
177  }
178  }
179  catch (const css::beans::UnknownPropertyException&)
180  {
181  msDescription.append("<unknown>");
182  }
183 }
184 
185 } // end of namespace accessibility
186 
187 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void AddInteger(const OUString &sPropertyName)
Add a property value formatted as integer to the description string.
OUStringBuffer msDescription
The description string that is build.
long Long
uno::Reference< drawing::XShape > const mxShape
void AddColor(const OUString &sPropertyName)
Add a property value formatted as color to the description string.
OUString SvxResId(const char *pId)
Definition: dialmgr.cxx:25
OUString sPrefix
css::uno::Reference< css::beans::XPropertySet > mxSet
Reference to the shape's property set.
OUString operator()(void)
Returns the description string and then resets it.
void Initialize(const OUString &sPrefix)
Initialize the description with the given prefix followed by the shape style in parentheses and a col...
bool mbIsFirstProperty
This flag is used to determine whether to insert a separator e.g.
css::uno::Reference< css::drawing::XShape > mxShape
Reference to the shape from which the properties are extracted.
void AppendString(std::u16string_view sString)
Append the given string as is to the current description.
OUString lookUpColorName(tools::Long color)
This is a color name lookup targeted to be used by the accessibility DescriptionGenerator
DescriptionGenerator(const css::uno::Reference< css::drawing::XShape > &xShape)
Creates a new description generator with an empty description string.
void AddProperty(const OUString &sPropertyName, PropertyType aType)
Add the given property name and its associated value to the description string.
sal_Int16 nValue
Definition: fmsrccfg.cxx:81