LibreOffice Module extensions (master) 1
propertyeditor.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 "handlerhelper.hxx"
21#include "propertyeditor.hxx"
22#include "browserpage.hxx"
23#include "linedescriptor.hxx"
24
25#include <tools/debug.hxx>
26#include <utility>
27#include <osl/diagnose.h>
28
29namespace pcr
30{
31 using ::com::sun::star::uno::Any;
32 using ::com::sun::star::inspection::XPropertyControl;
33 using ::com::sun::star::uno::Reference;
34
35 OPropertyEditor::OPropertyEditor(const css::uno::Reference<css::uno::XComponentContext>& rContext, weld::Builder& rBuilder)
36 : m_xContainer(rBuilder.weld_container("box"))
37 , m_xTabControl(rBuilder.weld_notebook("tabcontrol"))
38 , m_xControlHoldingParent(rBuilder.weld_container("controlparent")) // controls initially have this parent before they are moved
39 , m_xContext(rContext)
40 , m_pListener(nullptr)
41 , m_pObserver(nullptr)
42 , m_nNextId(1)
43 , m_bHasHelpSection(false)
44 {
46
47 m_xTabControl->connect_leave_page(LINK(this, OPropertyEditor, OnPageDeactivate));
48 m_xTabControl->connect_enter_page(LINK(this, OPropertyEditor, OnPageActivate));
49 }
50
52 {
54 ClearAll();
55 }
56
58 {
59 m_nNextId=1;
60
61 m_aPropertyPageIds.clear();
62 m_aShownPages.clear();
63 m_aHiddenPages.clear();
64
65 int nCount = m_xTabControl->get_n_pages();
66 for (int i = nCount - 1; i >= 0; --i)
67 {
68 OUString sID = m_xTabControl->get_page_ident(i);
69 m_xTabControl->remove_page(sID);
70 }
71
72 assert(m_xTabControl->get_n_pages() == 0);
73 }
74
76 {
77 return m_xTabControl->get_preferred_size();
78 }
79
81 {
82 // commit all of my pages, if necessary
83 for (const auto& page : m_aShownPages)
84 {
85 OBrowserPage* pPage = page.second.xPage.get();
86 if (pPage && pPage->getListBox().IsModified() )
87 pPage->getListBox().CommitModified();
88 }
89 }
90
91 OBrowserPage* OPropertyEditor::getPage(const OUString& rPropertyName)
92 {
93 OBrowserPage* pPage = nullptr;
94 MapStringToPageId::const_iterator aPropertyPageIdPos = m_aPropertyPageIds.find(rPropertyName);
95 if (aPropertyPageIdPos != m_aPropertyPageIds.end())
96 pPage = getPage(aPropertyPageIdPos->second);
97 return pPage;
98 }
99
100 const OBrowserPage* OPropertyEditor::getPage( const OUString& _rPropertyName ) const
101 {
102 return const_cast< OPropertyEditor* >( this )->getPage( _rPropertyName );
103 }
104
106 {
107 OBrowserPage* pPage = nullptr;
108 auto aPagePos = m_aShownPages.find(rPageId);
109 if (aPagePos != m_aShownPages.end())
110 pPage = aPagePos->second.xPage.get();
111 return pPage;
112 }
113
114 const OBrowserPage* OPropertyEditor::getPage(sal_uInt16 rPageId) const
115 {
116 return const_cast<OPropertyEditor*>(this)->getPage(rPageId);
117 }
118
119 sal_uInt16 OPropertyEditor::AppendPage(const OUString& rText, const OUString& rHelpId)
120 {
121 // obtain a new id
122 sal_uInt16 nId = m_nNextId++;
123 // insert the id
124 OUString sIdent = OUString::number(nId);
125 m_xTabControl->append_page(sIdent, rText);
126
127 // create a new page
128 auto xPage = std::make_unique<OBrowserPage>(m_xTabControl->get_page(sIdent), m_xControlHoldingParent.get());
129 // some knittings
130 xPage->getListBox().SetListener(m_pListener);
131 xPage->getListBox().SetObserver(m_pObserver);
132 xPage->getListBox().EnableHelpSection(m_bHasHelpSection);
133 xPage->SetHelpId(rHelpId);
134
135 m_aShownPages[nId] = PropertyPage(m_xTabControl->get_n_pages() - 1, rText, std::move(xPage));
136
137 // immediately activate the page
138 m_xTabControl->set_current_page(sIdent);
139
140 return nId;
141 }
142
143 void OPropertyEditor::SetHelpId( const OUString& rHelpId )
144 {
145 m_xTabControl->set_help_id(rHelpId);
146 }
147
148 void OPropertyEditor::RemovePage(sal_uInt16 nID)
149 {
150 auto aPagePos = m_aShownPages.find(nID);
151 if (aPagePos == m_aShownPages.end())
152 return;
153
154 m_aShownPages.erase(aPagePos);
155 OUString sIdent(OUString::number(nID));
156 m_xTabControl->remove_page(sIdent);
157 }
158
159 void OPropertyEditor::SetPage(sal_uInt16 nId)
160 {
161 m_xTabControl->set_current_page(OUString::number(nId));
162 }
163
165 {
166 return m_xTabControl->get_current_page_ident().toUInt32();
167 }
168
169 void OPropertyEditor::forEachPage( PageOperation _pOperation )
170 {
171 int nCount = m_xTabControl->get_n_pages();
172 for (int i = 0; i < nCount; ++i)
173 {
174 sal_uInt16 nID = m_xTabControl->get_page_ident(i).toUInt32();
175 OBrowserPage* pPage = getPage(nID);
176 if (!pPage)
177 continue;
178 (this->*_pOperation)( *pPage, nullptr );
179 }
180 }
181
183 {
185 }
186
188 {
189 m_pListener = pListener;
191 }
192
194 {
196 }
197
199 {
200 m_pObserver = _pObserver;
202 }
203
205 {
206 m_bHasHelpSection = bEnable;
208 }
209
210 void OPropertyEditor::SetHelpText( const OUString& rHelpText )
211 {
212 int nCount = m_xTabControl->get_n_pages();
213 for (int i = 0; i < nCount; ++i)
214 {
215 sal_uInt16 nID = m_xTabControl->get_page_ident(i).toUInt32();
216 OBrowserPage* pPage = getPage(nID);
217 if (!pPage)
218 continue;
219 setHelpSectionText( *pPage, &rHelpText );
220 }
221 }
222
224 {
226 }
227
228 void OPropertyEditor::setHelpSectionText( OBrowserPage& rPage, const void* pPointerToOUString )
229 {
230 OSL_ENSURE( pPointerToOUString, "OPropertyEditor::setHelpSectionText: invalid argument!" );
231 if ( !pPointerToOUString )
232 return;
233
234 const OUString& rText( *static_cast<const OUString*>(pPointerToOUString) );
235 rPage.getListBox().SetHelpText( rText );
236 }
237
238 void OPropertyEditor::InsertEntry( const OLineDescriptor& rData, sal_uInt16 nPageId, sal_uInt16 nPos )
239 {
240 // let the current page handle this
241 OBrowserPage* pPage = getPage(nPageId);
242 DBG_ASSERT( pPage, "OPropertyEditor::InsertEntry: don't have such a page!" );
243 if ( !pPage )
244 return;
245
246 pPage->getListBox().InsertEntry( rData, nPos );
247
248 OSL_ENSURE( m_aPropertyPageIds.find( rData.sName ) == m_aPropertyPageIds.end(),
249 "OPropertyEditor::InsertEntry: property already present in the map!" );
250 m_aPropertyPageIds.emplace( rData.sName, nPageId );
251 }
252
253 void OPropertyEditor::RemoveEntry( const OUString& rName )
254 {
255 OBrowserPage* pPage = getPage( rName );
256 if ( pPage )
257 {
258 OSL_VERIFY( pPage->getListBox().RemoveEntry( rName ) );
259
260 OSL_ENSURE( m_aPropertyPageIds.find( rName ) != m_aPropertyPageIds.end(),
261 "OPropertyEditor::RemoveEntry: property not present in the map!" );
262 m_aPropertyPageIds.erase( rName );
263 }
264 }
265
267 {
268 OBrowserPage* pPage = getPage( rData.sName );
269 if ( pPage )
271 }
272
273 void OPropertyEditor::SetPropertyValue( const OUString& rEntryName, const Any& _rValue, bool _bUnknownValue )
274 {
275 OBrowserPage* pPage = getPage( rEntryName );
276 if ( pPage )
277 pPage->getListBox().SetPropertyValue( rEntryName, _rValue, _bUnknownValue );
278 }
279
280 sal_uInt16 OPropertyEditor::GetPropertyPos( const OUString& rEntryName ) const
281 {
282 sal_uInt16 nVal=EDITOR_LIST_ENTRY_NOTFOUND;
283 const OBrowserPage* pPage = getPage( rEntryName );
284 if ( pPage )
285 nVal = pPage->getListBox().GetPropertyPos( rEntryName );
286 return nVal;
287 }
288
289 void OPropertyEditor::ShowPropertyPage(sal_uInt16 nPageId, bool bShow)
290 {
291 assert((m_aHiddenPages.find(nPageId) != m_aHiddenPages.end() ||
292 m_aShownPages.find(nPageId) != m_aShownPages.end()) && "page doesn't exist");
293 OUString sIdent(OUString::number(nPageId));
294 if (!bShow)
295 {
296 auto aPagePos = m_aShownPages.find(nPageId);
297 if (aPagePos != m_aShownPages.end())
298 {
299 aPagePos->second.xPage->detach();
300 m_xTabControl->remove_page(sIdent);
301
302 m_aHiddenPages[nPageId] = std::move(aPagePos->second);
303 m_aShownPages.erase(aPagePos);
304 }
305 }
306 else
307 {
308 auto aPagePos = m_aHiddenPages.find(nPageId);
309 if (aPagePos != m_aHiddenPages.end())
310 {
311 m_xTabControl->insert_page(sIdent, aPagePos->second.sLabel, aPagePos->second.nPos);
312 aPagePos->second.xPage->reattach(m_xTabControl->get_page(sIdent));
313
314 m_aShownPages[nPageId] = std::move(aPagePos->second);
315 m_aHiddenPages.erase(aPagePos);
316 }
317 }
318 }
319
320 void OPropertyEditor::EnablePropertyControls( const OUString& rEntryName, sal_Int16 nControls, bool bEnable )
321 {
322 for (const auto& rPage : m_aShownPages)
323 {
324 OBrowserPage* pPage = rPage.second.xPage.get();
325 if (pPage)
326 pPage->getListBox().EnablePropertyControls( rEntryName, nControls, bEnable );
327 }
328 }
329
330 void OPropertyEditor::EnablePropertyLine( const OUString& rEntryName, bool bEnable )
331 {
332 for (const auto& rPage : m_aShownPages)
333 {
334 OBrowserPage* pPage = rPage.second.xPage.get();
335 if (pPage)
336 pPage->getListBox().EnablePropertyLine( rEntryName, bEnable );
337 }
338 }
339
340 Reference< XPropertyControl > OPropertyEditor::GetPropertyControl(const OUString& rEntryName)
341 {
342 Reference< XPropertyControl > xControl;
343 // let the current page handle this
344 OBrowserPage* pPage = getPage(m_xTabControl->get_current_page_ident().toUInt32());
345 if (pPage)
346 xControl = pPage->getListBox().GetPropertyControl(rEntryName);
347 return xControl;
348 }
349
350 IMPL_LINK(OPropertyEditor, OnPageActivate, const OUString&, rNewPage, void)
351 {
352 m_aPageActivationHandler.Call(rNewPage);
353 }
354
355 IMPL_LINK(OPropertyEditor, OnPageDeactivate, const OUString&, rIdent, bool)
356 {
357 // commit the data on the current (to-be-deactivated) tab page
358 // (79404)
359 OBrowserPage* pCurrentPage = getPage(rIdent.toUInt32());
360 if (!pCurrentPage)
361 return true;
362
363 if (pCurrentPage->getListBox().IsModified())
364 pCurrentPage->getListBox().CommitModified();
365
366 return true;
367 }
368
370 : nPos(0)
371 {
372 }
373
374 OPropertyEditor::PropertyPage::PropertyPage(sal_uInt16 nPagePos, OUString aLabel, std::unique_ptr<OBrowserPage> pPage)
375 : nPos(nPagePos), sLabel(std::move(aLabel)), xPage(std::move(pPage))
376 {
377 }
378
379} // namespace pcr
380
381/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_uInt16 nPageId
#define EDITOR_LIST_REPLACE_EXISTING
non-UNO version of the XPropertyControlObserver
void ChangeEntry(const OLineDescriptor &, ListBoxLines::size_type nPos)
void EnablePropertyControls(const OUString &_rEntryName, sal_Int16 _nControls, bool _bEnable)
bool RemoveEntry(const OUString &_rName)
css::uno::Reference< css::inspection::XPropertyControl > GetPropertyControl(const OUString &rEntryName)
sal_uInt16 GetPropertyPos(std::u16string_view rEntryName) const
void SetPropertyValue(const OUString &rEntryName, const css::uno::Any &rValue, bool _bUnknownValue)
void EnableHelpSection(bool _bEnable)
void EnablePropertyLine(const OUString &_rEntryName, bool _bEnable)
void SetObserver(IPropertyControlObserver *_pObserver)
void SetHelpText(const OUString &_rHelpText)
void InsertEntry(const OLineDescriptor &, sal_uInt16 nPos)
void SetListener(IPropertyLineListener *_pListener)
OBrowserListBox & getListBox()
Definition: browserpage.hxx:42
IPropertyControlObserver * m_pObserver
void EnablePropertyControls(const OUString &_rEntryName, sal_Int16 _nControls, bool _bEnable)
void forEachPage(PageOperation _pOperation)
OPropertyEditor(const css::uno::Reference< css::uno::XComponentContext > &rContext, weld::Builder &rBuilder)
void RemoveEntry(const OUString &_rName)
void SetControlObserver(IPropertyControlObserver *)
css::uno::Reference< css::uno::XComponentContext > m_xContext
std::map< sal_uInt16, PropertyPage > m_aShownPages
css::uno::Reference< css::inspection::XPropertyControl > GetPropertyControl(const OUString &rEntryName)
Size get_preferred_size() const
sal_uInt16 GetPropertyPos(const OUString &rEntryName) const
std::unique_ptr< weld::Container > m_xControlHoldingParent
void RemovePage(sal_uInt16 nID)
MapStringToPageId m_aPropertyPageIds
std::map< sal_uInt16, PropertyPage > m_aHiddenPages
void ChangeEntry(const OLineDescriptor &)
void SetHelpText(const OUString &_rHelpText)
void enableHelpSection(OBrowserPage &_rPage, const void *)
std::unique_ptr< weld::Notebook > m_xTabControl
sal_uInt16 GetCurPage() const
void ShowPropertyPage(sal_uInt16 _nPageId, bool _bShow)
static void setHelpSectionText(OBrowserPage &_rPage, const void *_pPointerToOUString)
void EnableHelpSection(bool _bEnable)
void SetPropertyValue(const OUString &_rEntryName, const css::uno::Any &_rValue, bool _bUnknownValue)
void SetLineListener(IPropertyLineListener *)
void setPageLineListener(OBrowserPage &_rPage, const void *)
sal_uInt16 AppendPage(const OUString &r, const OUString &_rHelpId)
void InsertEntry(const OLineDescriptor &, sal_uInt16 _nPageId, sal_uInt16 nPos=EDITOR_LIST_APPEND)
void SetPage(sal_uInt16)
void EnablePropertyLine(const OUString &_rEntryName, bool _bEnable)
void setPageControlObserver(OBrowserPage &_rPage, const void *)
IPropertyLineListener * m_pListener
void SetHelpId(const OUString &sHelpId)
OBrowserPage * getPage(sal_uInt16 _rPageId)
static void setBuilderParent(const css::uno::Reference< css::uno::XComponentContext > &rContext, weld::Widget *pParent)
static void clearBuilderParent(const css::uno::Reference< css::uno::XComponentContext > &rContext)
int nCount
#define DBG_ASSERT(sCon, aError)
Reference< XComponentContext > m_xContext
Definition: filehandler.cxx:78
sal_uInt16 nPos
int i
a property handler for any virtual string properties
Definition: browserline.cxx:39
IMPL_LINK(OBrowserLine, OnButtonClicked, weld::Button &, rButton, void)
Reference< XNameAccess > m_xContainer
sal_Int16 nId
#define EDITOR_LIST_ENTRY_NOTFOUND
Definition: pcrcommon.hxx:23
OUString aLabel