Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / extensions / source / propctrlr / selectlabeldialog.cxx
blob2f963ea81c5c14f4a1d1964ab6b0669d1f1afe44
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 "selectlabeldialog.hxx"
21 #include <strings.hrc>
22 #include <bitmaps.hlst>
23 #include "formbrowsertools.hxx"
24 #include "formstrings.hxx"
25 #include <com/sun/star/form/FormComponentType.hpp>
26 #include <com/sun/star/container/XChild.hpp>
27 #include <com/sun/star/container/XIndexAccess.hpp>
28 #include <com/sun/star/sdbc/XResultSet.hpp>
29 #include <com/sun/star/beans/XPropertySet.hpp>
30 #include <com/sun/star/lang/XServiceInfo.hpp>
31 #include <comphelper/property.hxx>
32 #include <comphelper/types.hxx>
33 #include <sal/log.hxx>
34 #include <tools/debug.hxx>
37 namespace pcr
41 using namespace ::com::sun::star::uno;
42 using namespace ::com::sun::star::container;
43 using namespace ::com::sun::star::beans;
44 using namespace ::com::sun::star::form;
45 using namespace ::com::sun::star::sdbc;
46 using namespace ::com::sun::star::lang;
49 // OSelectLabelDialog
50 OSelectLabelDialog::OSelectLabelDialog(weld::Window* pParent, Reference< XPropertySet > const & _xControlModel)
51 : GenericDialogController(pParent, "modules/spropctrlr/ui/labelselectiondialog.ui", "LabelSelectionDialog")
52 , m_xControlModel(_xControlModel)
53 , m_bLastSelected(false)
54 , m_bHaveAssignableControl(false)
55 , m_xMainDesc(m_xBuilder->weld_label("label"))
56 , m_xControlTree(m_xBuilder->weld_tree_view("control"))
57 , m_xNoAssignment(m_xBuilder->weld_check_button("noassignment"))
59 m_xControlTree->connect_changed(LINK(this, OSelectLabelDialog, OnEntrySelected));
60 m_xControlTree->set_size_request(-1, m_xControlTree->get_height_rows(8));
62 // fill the description
63 OUString sDescription = m_xMainDesc->get_label();
64 sal_Int16 nClassID = FormComponentType::CONTROL;
65 if (::comphelper::hasProperty(PROPERTY_CLASSID, m_xControlModel))
66 nClassID = ::comphelper::getINT16(m_xControlModel->getPropertyValue(PROPERTY_CLASSID));
68 sDescription = sDescription.replaceAll("$controlclass$",
69 GetUIHeadlineName(nClassID, makeAny(m_xControlModel)));
70 OUString sName = ::comphelper::getString(m_xControlModel->getPropertyValue(PROPERTY_NAME));
71 sDescription = sDescription.replaceAll("$controlname$", sName);
72 m_xMainDesc->set_label(sDescription);
74 // search for the root of the form hierarchy
75 Reference< XChild > xCont(m_xControlModel, UNO_QUERY);
76 Reference< XInterface > xSearch( xCont.is() ? xCont->getParent() : Reference< XInterface > ());
77 Reference< XResultSet > xParentAsResultSet(xSearch, UNO_QUERY);
78 while (xParentAsResultSet.is())
80 xCont.set(xSearch, UNO_QUERY);
81 xSearch = xCont.is() ? xCont->getParent() : Reference< XInterface > ();
82 xParentAsResultSet.set(xSearch, UNO_QUERY);
85 // and insert all entries below this root into the listbox
86 if (xSearch.is())
88 // check which service the allowed components must support
89 sal_Int16 nClassId = 0;
90 try { nClassId = ::comphelper::getINT16(m_xControlModel->getPropertyValue(PROPERTY_CLASSID)); } catch(...) { }
91 m_sRequiredService = (FormComponentType::RADIOBUTTON == nClassId) ? OUStringLiteral(SERVICE_COMPONENT_GROUPBOX) : OUStringLiteral(SERVICE_COMPONENT_FIXEDTEXT);
92 m_aRequiredControlImage = (FormComponentType::RADIOBUTTON == nClassId) ? OUStringLiteral(RID_EXTBMP_GROUPBOX) : OUStringLiteral(RID_EXTBMP_FIXEDTEXT);
94 // calc the currently set label control (so InsertEntries can calc m_xInitialSelection)
95 Any aCurrentLabelControl( m_xControlModel->getPropertyValue(PROPERTY_CONTROLLABEL) );
96 DBG_ASSERT((aCurrentLabelControl.getValueTypeClass() == TypeClass_INTERFACE) || !aCurrentLabelControl.hasValue(),
98 "OSelectLabelDialog::OSelectLabelDialog : invalid ControlLabel property !");
99 if (aCurrentLabelControl.hasValue())
100 aCurrentLabelControl >>= m_xInitialLabelControl;
102 // insert the root
103 OUString sRootName(PcrRes(RID_STR_FORMS));
104 OUString aFormImage(RID_EXTBMP_FORMS);
105 m_xControlTree->insert(nullptr, -1, &sRootName, nullptr,
106 nullptr, nullptr, &aFormImage, false, nullptr);
108 // build the tree
109 m_xInitialSelection.reset();
110 m_bHaveAssignableControl = false;
111 std::unique_ptr<weld::TreeIter> xRoot = m_xControlTree->make_iterator();
112 m_xControlTree->get_iter_first(*xRoot);
113 InsertEntries(xSearch, *xRoot);
114 m_xControlTree->expand_row(*xRoot);
117 if (m_xInitialSelection)
119 m_xControlTree->scroll_to_row(*m_xInitialSelection);
120 m_xControlTree->select(*m_xInitialSelection);
122 else
124 m_xControlTree->scroll_to_row(0);
125 m_xControlTree->unselect_all();
126 m_xNoAssignment->set_active(true);
129 if (!m_bHaveAssignableControl)
130 { // no controls which can be assigned
131 m_xNoAssignment->set_active(true);
132 m_xNoAssignment->set_sensitive(false);
135 m_xLastSelected = m_xControlTree->make_iterator(nullptr);
137 m_xNoAssignment->connect_toggled(LINK(this, OSelectLabelDialog, OnNoAssignmentClicked));
138 OnNoAssignmentClicked(*m_xNoAssignment);
141 OSelectLabelDialog::~OSelectLabelDialog()
145 sal_Int32 OSelectLabelDialog::InsertEntries(const Reference< XInterface > & _xContainer, weld::TreeIter& rContainerEntry)
147 Reference< XIndexAccess > xContainer(_xContainer, UNO_QUERY);
148 if (!xContainer.is())
149 return 0;
151 sal_Int32 nChildren = 0;
152 OUString sName;
153 Reference< XPropertySet > xAsSet;
154 for (sal_Int32 i=0; i<xContainer->getCount(); ++i)
156 xContainer->getByIndex(i) >>= xAsSet;
157 if (!xAsSet.is())
159 SAL_INFO("extensions.propctrlr", "OSelectLabelDialog::InsertEntries : strange : a form component which isn't a property set !");
160 continue;
163 if (!::comphelper::hasProperty(PROPERTY_NAME, xAsSet))
164 // we need at least a name for displaying ...
165 continue;
166 sName = ::comphelper::getString(xAsSet->getPropertyValue(PROPERTY_NAME));
168 // we need to check if the control model supports the required service
169 Reference< XServiceInfo > xInfo(xAsSet, UNO_QUERY);
170 if (!xInfo.is())
171 continue;
173 if (!xInfo->supportsService(m_sRequiredService))
174 { // perhaps it is a container
175 Reference< XIndexAccess > xCont(xAsSet, UNO_QUERY);
176 if (xCont.is() && xCont->getCount())
177 { // yes -> step down
178 OUString aFormImage(RID_EXTBMP_FORM);
180 m_xControlTree->insert(&rContainerEntry, -1, &sName, nullptr,
181 nullptr, nullptr, &aFormImage, false, nullptr);
182 auto xIter = m_xControlTree->make_iterator(&rContainerEntry);
183 m_xControlTree->iter_nth_child(*xIter, nChildren);
184 sal_Int32 nContChildren = InsertEntries(xCont, *xIter);
185 if (nContChildren)
187 m_xControlTree->expand_row(*xIter);
188 ++nChildren;
190 else
191 { // oops, no valid children -> remove the entry
192 m_xControlTree->remove(*xIter);
195 continue;
198 // get the label
199 if (!::comphelper::hasProperty(PROPERTY_LABEL, xAsSet))
200 continue;
202 OUString sDisplayName = OUStringBuffer(
203 ::comphelper::getString(xAsSet->getPropertyValue(PROPERTY_LABEL))).
204 append(" (").append(sName).append(')').
205 makeStringAndClear();
207 // all requirements met -> insert
208 m_xUserData.emplace_back(new Reference<XPropertySet>(xAsSet));
209 OUString sId(OUString::number(reinterpret_cast<sal_Int64>(m_xUserData.back().get())));
210 m_xControlTree->insert(&rContainerEntry, -1, &sDisplayName, &sId, nullptr, nullptr, &m_aRequiredControlImage, false, nullptr);
212 if (m_xInitialLabelControl == xAsSet)
214 m_xInitialSelection = m_xControlTree->make_iterator(&rContainerEntry);
215 m_xControlTree->iter_nth_child(*m_xInitialSelection, nChildren);
218 ++nChildren;
219 m_bHaveAssignableControl = true;
222 return nChildren;
225 IMPL_LINK(OSelectLabelDialog, OnEntrySelected, weld::TreeView&, rLB, void)
227 DBG_ASSERT(&rLB == m_xControlTree.get(), "OSelectLabelDialog::OnEntrySelected : where did this come from ?");
228 std::unique_ptr<weld::TreeIter> xIter = m_xControlTree->make_iterator();
229 bool bSelected = m_xControlTree->get_selected(xIter.get());
230 OUString sData = bSelected ? m_xControlTree->get_id(*xIter) : OUString();
231 if (!sData.isEmpty())
232 m_xSelectedControl.set(*reinterpret_cast<Reference<XPropertySet>*>(sData.toInt64()));
233 m_xNoAssignment->set_active(sData.isEmpty());
236 IMPL_LINK(OSelectLabelDialog, OnNoAssignmentClicked, weld::ToggleButton&, rButton, void)
238 DBG_ASSERT(&rButton == m_xNoAssignment.get(), "OSelectLabelDialog::OnNoAssignmentClicked : where did this come from ?");
240 if (m_xNoAssignment->get_active())
242 m_bLastSelected = m_xControlTree->get_selected(m_xLastSelected.get());
244 else
246 DBG_ASSERT(m_bHaveAssignableControl, "OSelectLabelDialog::OnNoAssignmentClicked");
247 // search the first assignable entry
248 auto xSearch = m_xControlTree->make_iterator(nullptr);
249 bool bSearch = m_xControlTree->get_iter_first(*xSearch);
250 while (bSearch)
252 if (m_xControlTree->get_id(*xSearch).toInt64())
253 break;
254 bSearch = m_xControlTree->iter_next(*xSearch);
256 // and select it
257 if (bSearch)
259 m_xControlTree->copy_iterator(*xSearch, *m_xLastSelected);
260 m_xControlTree->select(*m_xLastSelected);
261 m_bLastSelected = true;
265 if (m_bLastSelected)
267 if (!m_xNoAssignment->get_active())
268 m_xControlTree->select(*m_xLastSelected);
269 else
270 m_xControlTree->unselect(*m_xLastSelected);
273 } // namespace pcr
276 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */