LibreOffice Module forms (master) 1
GroupManager.hxx
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#pragma once
21
22#include <com/sun/star/awt/XControlModel.hpp>
23#include <com/sun/star/beans/XPropertySet.hpp>
24#include <com/sun/star/beans/XPropertyChangeListener.hpp>
25#include <com/sun/star/container/XContainerListener.hpp>
26#include <com/sun/star/container/XContainer.hpp>
28
29#include <map>
30#include <memory>
31#include <vector>
32
33/*
34 * The GroupManager listens at the StarForm for FormComponent insertion and removal as well as
35 * its properties "Name" and "TabIndex" and updates its Group using this information.
36 *
37 * The GroupManager manages a Group in which all Controls are sorted by TabIndex.
38 * It also manages an array of Groups, in which each FormComponent is assigned a
39 * Group according to its name.
40 * Each Group is activated using a Map, if they contain more than one element.
41 *
42 * The Groups manage the FormComponents internally using two arrays.
43 * In the GroupCompArray the Components are sorted by TabIndex and insertion position.
44 * Because this array is accessed via the FormComponent, we also have the GroupCompAccessArray
45 * in which the FormComponents are sorted by their storage address.
46 * Every element of the GroupCompArray has a pointer to the GroupCompArray.
47 */
48namespace frm
49{
50
51
52 template <class ELEMENT, class LESS_COMPARE>
53 sal_Int32 insert_sorted(::std::vector<ELEMENT>& _rArray, const ELEMENT& _rNewElement, const LESS_COMPARE& _rCompareOp)
54 {
55 typename ::std::vector<ELEMENT>::iterator aInsertPos = ::std::lower_bound(
56 _rArray.begin(),
57 _rArray.end(),
58 _rNewElement,
59 _rCompareOp
60 );
61 aInsertPos = _rArray.insert(aInsertPos, _rNewElement);
62 return aInsertPos - _rArray.begin();
63 }
64
65 template <class ELEMENT, class LESS_COMPARE>
66 bool seek_entry(const ::std::vector<ELEMENT>& _rArray, const ELEMENT& _rNewElement, sal_Int32& nPos, const LESS_COMPARE& _rCompareOp)
67 {
68 typename ::std::vector<ELEMENT>::const_iterator aExistentPos = ::std::lower_bound(
69 _rArray.begin(),
70 _rArray.end(),
71 _rNewElement,
72 _rCompareOp
73 );
74 if ((aExistentPos != _rArray.end()) && (*aExistentPos == _rNewElement))
75 { // we have a valid "lower or equal" element and it's really "equal"
76 nPos = aExistentPos - _rArray.begin();
77 return true;
78 }
79 nPos = -1;
80 return false;
81 }
82
83
85{
86 css::uno::Reference< css::beans::XPropertySet> m_xComponent;
87 css::uno::Reference< css::awt::XControlModel> m_xControlModel;
88 sal_Int32 m_nPos;
89 sal_Int16 m_nTabIndex;
90
91 friend class OGroupCompLess;
92
93public:
94 OGroupComp(const css::uno::Reference< css::beans::XPropertySet>& rxElement, sal_Int32 nInsertPos );
95 OGroupComp();
96
97 bool operator==( const OGroupComp& rComp ) const;
98
99 const css::uno::Reference< css::beans::XPropertySet>& GetComponent() const { return m_xComponent; }
100 const css::uno::Reference< css::awt::XControlModel>& GetControlModel() const { return m_xControlModel; }
101
102 sal_Int32 GetPos() const { return m_nPos; }
103 sal_Int16 GetTabIndex() const { return m_nTabIndex; }
104};
105
106
107class OGroupComp;
109{
110 css::uno::Reference< css::beans::XPropertySet> m_xComponent;
111
113
114 friend class OGroupCompAccLess;
115
116public:
117 OGroupCompAcc(const css::uno::Reference< css::beans::XPropertySet>& rxElement, OGroupComp _aGroupComp );
118
119 bool operator==( const OGroupCompAcc& rCompAcc ) const;
120
121 const OGroupComp& GetGroupComponent() const { return m_aGroupComp; }
122};
123
124class OGroup final
125{
126 std::vector<OGroupComp> m_aCompArray;
127 std::vector<OGroupCompAcc> m_aCompAccArray;
128
129 OUString m_aGroupName;
130 sal_uInt16 m_nInsertPos; // The insertion position of the GroupComps is determined by the Group
131
132 friend class OGroupLess;
133
134public:
135 explicit OGroup(OUString sGroupName);
136
137 const OUString& GetGroupName() const { return m_aGroupName; }
138 css::uno::Sequence< css::uno::Reference< css::awt::XControlModel> > GetControlModels() const;
139
140 void InsertComponent( const css::uno::Reference< css::beans::XPropertySet>& rxElement );
141 void RemoveComponent( const css::uno::Reference< css::beans::XPropertySet>& rxElement );
142 sal_uInt16 Count() const { return sal::static_int_cast< sal_uInt16 >(m_aCompArray.size()); }
143 const css::uno::Reference< css::beans::XPropertySet>& GetObject( sal_uInt16 nP ) const
144 { return m_aCompArray[nP].GetComponent(); }
145};
146
147typedef std::map<OUString, OGroup> OGroupArr;
148typedef std::vector<OGroupArr::iterator> OActiveGroups;
149
150
151class OGroupManager : public ::cppu::WeakImplHelper< css::beans::XPropertyChangeListener, css::container::XContainerListener>
152{
153 std::unique_ptr<OGroup>
154 m_pCompGroup; // Sort all Components by TabIndices
155 OGroupArr m_aGroupArr; // Sort all Components by group
156 OActiveGroups m_aActiveGroupMap; // This map contains all indices of all groups with more than 1 element
157
158 css::uno::Reference< css::container::XContainer >
160
161 // Helper functions
162 void InsertElement( const css::uno::Reference< css::beans::XPropertySet>& rxElement );
163 void RemoveElement( const css::uno::Reference< css::beans::XPropertySet>& rxElement );
164 void removeFromGroupMap(const OUString& _sGroupName,const css::uno::Reference< css::beans::XPropertySet>& _xSet);
165
166public:
167 explicit OGroupManager(const css::uno::Reference< css::container::XContainer >& _rxContainer);
168 virtual ~OGroupManager() override;
169
170// css::lang::XEventListener
171 virtual void SAL_CALL disposing(const css::lang::EventObject& _rSource) override;
172
173// css::beans::XPropertyChangeListener
174 virtual void SAL_CALL propertyChange(const css::beans::PropertyChangeEvent& evt) override;
175
176// css::container::XContainerListener
177 virtual void SAL_CALL elementInserted(const css::container::ContainerEvent& _rEvent) override;
178 virtual void SAL_CALL elementRemoved(const css::container::ContainerEvent& _rEvent) override;
179 virtual void SAL_CALL elementReplaced(const css::container::ContainerEvent& _rEvent) override;
180
181// Other functions
182 sal_Int32 getGroupCount() const;
183 void getGroup(sal_Int32 nGroup, css::uno::Sequence< css::uno::Reference< css::awt::XControlModel> >& _rGroup, OUString& Name);
184 void getGroupByName(const OUString& Name, css::uno::Sequence< css::uno::Reference< css::awt::XControlModel> >& _rGroup);
185 css::uno::Sequence< css::uno::Reference< css::awt::XControlModel> > getControlModels() const;
186
187 static OUString GetGroupName( const css::uno::Reference< css::beans::XPropertySet>& xComponent );
188};
189
190
191} // namespace frm
192
193
194/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const OGroupComp & GetGroupComponent() const
OGroupCompAcc(const css::uno::Reference< css::beans::XPropertySet > &rxElement, OGroupComp _aGroupComp)
bool operator==(const OGroupCompAcc &rCompAcc) const
css::uno::Reference< css::beans::XPropertySet > m_xComponent
OGroupComp m_aGroupComp
const css::uno::Reference< css::beans::XPropertySet > & GetComponent() const
css::uno::Reference< css::awt::XControlModel > m_xControlModel
sal_Int16 m_nTabIndex
sal_Int32 GetPos() const
sal_Int32 m_nPos
bool operator==(const OGroupComp &rComp) const
OGroupComp(const css::uno::Reference< css::beans::XPropertySet > &rxElement, sal_Int32 nInsertPos)
css::uno::Reference< css::beans::XPropertySet > m_xComponent
const css::uno::Reference< css::awt::XControlModel > & GetControlModel() const
sal_Int16 GetTabIndex() const
std::unique_ptr< OGroup > m_pCompGroup
virtual void SAL_CALL propertyChange(const css::beans::PropertyChangeEvent &evt) override
OGroupManager(const css::uno::Reference< css::container::XContainer > &_rxContainer)
css::uno::Sequence< css::uno::Reference< css::awt::XControlModel > > getControlModels() const
css::uno::Reference< css::container::XContainer > m_xContainer
static OUString GetGroupName(const css::uno::Reference< css::beans::XPropertySet > &xComponent)
void getGroupByName(const OUString &Name, css::uno::Sequence< css::uno::Reference< css::awt::XControlModel > > &_rGroup)
virtual void SAL_CALL elementReplaced(const css::container::ContainerEvent &_rEvent) override
void getGroup(sal_Int32 nGroup, css::uno::Sequence< css::uno::Reference< css::awt::XControlModel > > &_rGroup, OUString &Name)
virtual void SAL_CALL elementRemoved(const css::container::ContainerEvent &_rEvent) override
virtual ~OGroupManager() override
void InsertElement(const css::uno::Reference< css::beans::XPropertySet > &rxElement)
void RemoveElement(const css::uno::Reference< css::beans::XPropertySet > &rxElement)
virtual void SAL_CALL elementInserted(const css::container::ContainerEvent &_rEvent) override
virtual void SAL_CALL disposing(const css::lang::EventObject &_rSource) override
void removeFromGroupMap(const OUString &_sGroupName, const css::uno::Reference< css::beans::XPropertySet > &_xSet)
sal_Int32 getGroupCount() const
OActiveGroups m_aActiveGroupMap
const OUString & GetGroupName() const
const css::uno::Reference< css::beans::XPropertySet > & GetObject(sal_uInt16 nP) const
void RemoveComponent(const css::uno::Reference< css::beans::XPropertySet > &rxElement)
friend class OGroupLess
void InsertComponent(const css::uno::Reference< css::beans::XPropertySet > &rxElement)
std::vector< OGroupCompAcc > m_aCompAccArray
css::uno::Sequence< css::uno::Reference< css::awt::XControlModel > > GetControlModels() const
std::vector< OGroupComp > m_aCompArray
sal_uInt16 m_nInsertPos
OUString m_aGroupName
OGroup(OUString sGroupName)
sal_uInt16 Count() const
sal_uInt16 nPos
ListBox is a bit confusing / different from other form components, so here are a few notes:
Definition: BaseListBox.hxx:25
std::map< OUString, OGroup > OGroupArr
sal_Int32 insert_sorted(::std::vector< ELEMENT > &_rArray, const ELEMENT &_rNewElement, const LESS_COMPARE &_rCompareOp)
bool seek_entry(const ::std::vector< ELEMENT > &_rArray, const ELEMENT &_rNewElement, sal_Int32 &nPos, const LESS_COMPARE &_rCompareOp)
std::vector< OGroupArr::iterator > OActiveGroups