LibreOffice Module extensions (master) 1
optiongrouplayouter.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
21#include <com/sun/star/awt/Size.hpp>
22#include <com/sun/star/awt/Point.hpp>
23#include <com/sun/star/container/XNameAccess.hpp>
24#include <com/sun/star/drawing/ShapeCollection.hpp>
25#include <com/sun/star/drawing/XShapes.hpp>
26#include <com/sun/star/drawing/XShapeGrouper.hpp>
27#include <com/sun/star/text/TextContentAnchorType.hpp>
28#include <com/sun/star/view/XSelectionSupplier.hpp>
29#include "groupboxwiz.hxx"
30#include "dbptools.hxx"
32
33
34namespace dbp
35{
36
37
38#define BUTTON_HEIGHT 300
39#define HEIGHT 450
40#define OFFSET 300
41#define MIN_WIDTH 600
42
43 using namespace ::com::sun::star::uno;
44 using namespace ::com::sun::star::drawing;
45 using namespace ::com::sun::star::beans;
46 using namespace ::com::sun::star::awt;
47 using namespace ::com::sun::star::container;
48 using namespace ::com::sun::star::lang;
49 using namespace ::com::sun::star::text;
50 using namespace ::com::sun::star::view;
51
52 OOptionGroupLayouter::OOptionGroupLayouter(const Reference< XComponentContext >& _rxContext)
53 :mxContext(_rxContext)
54 {
55 }
56
57
59 {
60 Reference< XShapes > xPageShapes = _rContext.xDrawPage;
61 if (!xPageShapes.is())
62 {
63 OSL_FAIL("OOptionGroupLayouter::OOptionGroupLayouter: missing the XShapes interface for the page!");
64 return;
65 }
66
67 Reference< XMultiServiceFactory > xDocFactory(_rContext.xDocumentModel, UNO_QUERY);
68 if (!xDocFactory.is())
69 {
70 OSL_FAIL("OOptionGroupLayouter::OOptionGroupLayouter: no document service factory!");
71 return;
72 }
73
74 // no. of buttons to create
75 sal_Int32 nRadioButtons = _rSettings.aLabels.size();
76
77 // the shape of the groupbox
78 css::awt::Size aControlShapeSize = _rContext.xObjectShape->getSize();
79 // maybe need to adjust the size if the control shapes
80 sal_Int32 nMinShapeHeight = BUTTON_HEIGHT*(nRadioButtons+1) + BUTTON_HEIGHT + BUTTON_HEIGHT/4;
81 if (aControlShapeSize.Height < nMinShapeHeight)
82 aControlShapeSize.Height = nMinShapeHeight;
83 if (aControlShapeSize.Width < MIN_WIDTH)
84 aControlShapeSize.Width = MIN_WIDTH;
85 _rContext.xObjectShape->setSize(aControlShapeSize);
86
87 // if we're working on a writer document, we need to anchor the shape
88 implAnchorShape(Reference< XPropertySet >(_rContext.xObjectShape, UNO_QUERY));
89
90 // shape collection (for grouping the shapes)
91 Reference< XShapes > xButtonCollection( ShapeCollection::create(mxContext) );
92 // first member : the shape of the control
93 xButtonCollection->add(_rContext.xObjectShape);
94
95 sal_Int32 nTempHeight = (aControlShapeSize.Height - BUTTON_HEIGHT/4) / (nRadioButtons + 1);
96
97 css::awt::Point aShapePosition = _rContext.xObjectShape->getPosition();
98
99 css::awt::Size aButtonSize(aControlShapeSize);
100 aButtonSize.Width = aControlShapeSize.Width - OFFSET;
101 aButtonSize.Height = HEIGHT;
102 css::awt::Point aButtonPosition;
103 aButtonPosition.X = aShapePosition.X + OFFSET;
104
105 OUString sElementsName("RadioGroup");
106 disambiguateName(Reference< XNameAccess >(_rContext.xForm, UNO_QUERY), sElementsName);
107
108 auto aLabelIter = _rSettings.aLabels.cbegin();
109 auto aValueIter = _rSettings.aValues.cbegin();
110 for (sal_Int32 i=0; i<nRadioButtons; ++i, ++aLabelIter, ++aValueIter)
111 {
112 aButtonPosition.Y = aShapePosition.Y + (i+1) * nTempHeight;
113
114 Reference< XPropertySet > xRadioModel(
115 xDocFactory->createInstance("com.sun.star.form.component.RadioButton"),
116 UNO_QUERY);
117
118 // the label
119 xRadioModel->setPropertyValue("Label", Any(*aLabelIter));
120 // the value
121 xRadioModel->setPropertyValue("RefValue", Any(*aValueIter));
122
123 // default selection
124 if (_rSettings.sDefaultField == *aLabelIter)
125 xRadioModel->setPropertyValue("DefaultState", Any(sal_Int16(1)));
126
127 // the connection to the database field
128 if (!_rSettings.sDBField.isEmpty())
129 xRadioModel->setPropertyValue("DataField", Any(_rSettings.sDBField));
130
131 // the name for the model
132 xRadioModel->setPropertyValue("Name", Any(sElementsName));
133
134 // create a shape for the radio button
135 Reference< XControlShape > xRadioShape(
136 xDocFactory->createInstance("com.sun.star.drawing.ControlShape"),
137 UNO_QUERY);
138 Reference< XPropertySet > xShapeProperties(xRadioShape, UNO_QUERY);
139
140 // if we're working on a writer document, we need to anchor the shape
141 implAnchorShape(xShapeProperties);
142
143 // position it
144 xRadioShape->setSize(aButtonSize);
145 xRadioShape->setPosition(aButtonPosition);
146 // knitting with the model
147 xRadioShape->setControl(Reference< XControlModel >(xRadioModel, UNO_QUERY));
148
149 // the name of the shape
150 // tdf#117282 com.sun.star.drawing.ControlShape *has* no property
151 // of type 'Name'. In older versions it was an error that this did
152 // not throw an UnknownPropertyException. Still, it was never set
153 // at the Shape/SdrObject and was lost.
154 // Thus - just do no tset it. It is/stays part of the FormControl
155 // data, so it will be shown in the FormControl dialogs. It is not
156 // shown/used in SdrObject::Name dialog (e.g. context menu/Name...)
157 // if (xShapeProperties.is())
158 // xShapeProperties->setPropertyValue("Name", makeAny(sElementsName));
159
160 // add to the page
161 xPageShapes->add(xRadioShape);
162 // add to the collection (for the later grouping)
163 xButtonCollection->add(xRadioShape);
164
165 // set the GroupBox as "LabelControl" for the RadioButton
166 // (_after_ having inserted the model into the page!)
167 xRadioModel->setPropertyValue("LabelControl", Any(_rContext.xObjectModel));
168 }
169
170 // group the shapes
171 try
172 {
173 Reference< XShapeGrouper > xGrouper(_rContext.xDrawPage, UNO_QUERY);
174 if (xGrouper.is())
175 {
176 Reference< XShapeGroup > xGroupedOptions = xGrouper->group(xButtonCollection);
177 Reference< XSelectionSupplier > xSelector(_rContext.xDocumentModel->getCurrentController(), UNO_QUERY);
178 if (xSelector.is())
179 xSelector->select(Any(xGroupedOptions));
180 }
181 }
182 catch(Exception&)
183 {
184 TOOLS_WARN_EXCEPTION("extensions.dbpilots",
185 "caught an exception while grouping the shapes!");
186 }
187 }
188
189
190 void OOptionGroupLayouter::implAnchorShape(const Reference< XPropertySet >& _rxShapeProps)
191 {
192 static constexpr OUStringLiteral s_sAnchorPropertyName = u"AnchorType";
193 Reference< XPropertySetInfo > xPropertyInfo;
194 if (_rxShapeProps.is())
195 xPropertyInfo = _rxShapeProps->getPropertySetInfo();
196 if (xPropertyInfo.is() && xPropertyInfo->hasPropertyByName(s_sAnchorPropertyName))
197 _rxShapeProps->setPropertyValue(s_sAnchorPropertyName, Any(TextContentAnchorType_AT_PAGE));
198 }
199
200
201} // namespace dbp
202
203
204/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OOptionGroupLayouter(const css::uno::Reference< css::uno::XComponentContext > &_rxContext)
void doLayout(const OControlWizardContext &_rContext, const OOptionGroupSettings &_rSettings)
static void implAnchorShape(const css::uno::Reference< css::beans::XPropertySet > &_rxShapeProps)
css::uno::Reference< css::uno::XComponentContext > mxContext
#define TOOLS_WARN_EXCEPTION(area, stream)
uno::Reference< uno::XComponentContext > mxContext
float u
@ Exception
void disambiguateName(const Reference< XNameAccess > &_rxContainer, OUString &_rElementsName)
Definition: dbptools.cxx:33
int i
#define OFFSET
#define BUTTON_HEIGHT
#define MIN_WIDTH
#define HEIGHT
css::uno::Reference< css::beans::XPropertySet > xObjectModel
css::uno::Reference< css::drawing::XControlShape > xObjectShape
css::uno::Reference< css::frame::XModel > xDocumentModel
css::uno::Reference< css::drawing::XDrawPage > xDrawPage
css::uno::Reference< css::beans::XPropertySet > xForm
std::vector< OUString > aLabels
Definition: groupboxwiz.hxx:33
std::vector< OUString > aValues
Definition: groupboxwiz.hxx:34