LibreOffice Module svx (master) 1
tabwin.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
21#include <tabwin.hxx>
22#include <fmservs.hxx>
23
24#include <svx/strings.hrc>
25#include <svx/svxids.hrc>
26#include <com/sun/star/sdb/CommandType.hpp>
27#include <com/sun/star/sdbc/XConnection.hpp>
29#include <comphelper/types.hxx>
30
31#include <helpids.h>
32#include <svx/fmshell.hxx>
33#include <fmshimp.hxx>
34
35#include <fmprop.hxx>
36
37#include <svx/dialmgr.hxx>
38#include <sfx2/bindings.hxx>
39#include <sfx2/objitem.hxx>
40#include <sfx2/frame.hxx>
43#include <tabwin.hrc>
44#include <utility>
45
48
49using namespace ::com::sun::star::sdbc;
50using namespace ::com::sun::star::sdb;
51using namespace ::com::sun::star::uno;
52using namespace ::com::sun::star::datatransfer;
53using namespace ::com::sun::star::beans;
54using namespace ::com::sun::star::lang;
55using namespace ::com::sun::star::form;
56using namespace ::com::sun::star::container;
57using namespace ::com::sun::star;
58using namespace ::svxform;
59using namespace ::svx;
60using namespace ::dbtools;
61
62struct ColumnInfo
63{
64 OUString sColumnName;
65 explicit ColumnInfo(OUString i_sColumnName)
66 : sColumnName(std::move(i_sColumnName))
67 {
68 }
69};
70
71void FmFieldWin::addToList(const uno::Reference< container::XNameAccess>& i_xColumns )
72{
73 const uno::Sequence< OUString > aEntries = i_xColumns->getElementNames();
74 for ( const OUString& rEntry : aEntries )
75 {
76 uno::Reference< beans::XPropertySet> xColumn(i_xColumns->getByName(rEntry),UNO_QUERY_THROW);
77 OUString sLabel;
78 if ( xColumn->getPropertySetInfo()->hasPropertyByName(FM_PROP_LABEL) )
79 xColumn->getPropertyValue(FM_PROP_LABEL) >>= sLabel;
80 m_aListBoxData.emplace_back(new ColumnInfo(rEntry));
81 OUString sId(weld::toId(m_aListBoxData.back().get()));
82 if ( !sLabel.isEmpty() )
83 m_xListBox->append(sId, sLabel);
84 else
85 m_xListBox->append(sId, rEntry);
86 }
87}
88
89IMPL_LINK(FmFieldWin, DragBeginHdl, bool&, rUnsetDragIcon, bool)
90{
91 rUnsetDragIcon = false;
92
93 ColumnInfo* pSelected = weld::fromId<ColumnInfo*>(m_xListBox->get_selected_id());
94 if (!pSelected)
95 {
96 // no drag without a field
97 return true;
98 }
99
100 svx::ODataAccessDescriptor aDescriptor;
101 aDescriptor[ DataAccessDescriptorProperty::DataSource ] <<= GetDatabaseName();
102 aDescriptor[ DataAccessDescriptorProperty::Connection ] <<= GetConnection().getTyped();
103 aDescriptor[ DataAccessDescriptorProperty::Command ] <<= GetObjectName();
104 aDescriptor[ DataAccessDescriptorProperty::CommandType ]<<= GetObjectType();
105 aDescriptor[ DataAccessDescriptorProperty::ColumnName ] <<= pSelected->sColumnName;
106
107 m_xHelper->setDescriptor(aDescriptor);
108
109 return false;
110}
111
113 : SfxModelessDialogController(_pBindings, _pMgr, _pParent, "svx/ui/formfielddialog.ui", "FormFieldDialog")
114 , SfxControllerItem(SID_FM_FIELDS_CONTROL, *_pBindings)
115 , comphelper::OPropertyChangeListener(m_aMutex)
116 , m_xListBox(m_xBuilder->weld_tree_view("treeview"))
117 , m_nObjectType(0)
118{
119 m_xDialog->set_help_id(HID_FIELD_SEL_WIN);
120 m_xListBox->set_help_id(HID_FIELD_SEL);
121
122 m_xListBox->connect_row_activated(LINK(this, FmFieldWin, RowActivatedHdl));
123 m_xHelper.set(new OColumnTransferable(
125 ));
127 m_xListBox->enable_drag_source(xHelper, DND_ACTION_COPY);
128 m_xListBox->connect_drag_begin(LINK(this, FmFieldWin, DragBeginHdl));
129
130 UpdateContent(nullptr);
131 m_xDialog->set_size_request(STD_WIN_SIZE_X, STD_WIN_SIZE_Y);
132}
133
135{
136 if (m_xChangeListener.is())
137 {
138 m_xChangeListener->dispose();
139 m_xChangeListener.clear();
140 }
142}
143
145{
146 return createSelectionControls();
147}
148
150{
151 ColumnInfo* pSelected = weld::fromId<ColumnInfo*>(m_xListBox->get_selected_id());
152 if (pSelected)
153 {
154 // build a descriptor for the currently selected field
155 ODataAccessDescriptor aDescr;
156 aDescr.setDataSource(GetDatabaseName());
157
158 aDescr[ DataAccessDescriptorProperty::Connection ] <<= GetConnection().getTyped();
159
160 aDescr[ DataAccessDescriptorProperty::Command ] <<= GetObjectName();
161 aDescr[ DataAccessDescriptorProperty::CommandType ] <<= GetObjectType();
162 aDescr[ DataAccessDescriptorProperty::ColumnName ] <<= pSelected->sColumnName;
163
164 // transfer this to the SFX world
165 SfxUnoAnyItem aDescriptorItem( SID_FM_DATACCESS_DESCRIPTOR, Any( aDescr.createPropertyValueSequence() ) );
166 const SfxPoolItem* pArgs[] =
167 {
168 &aDescriptorItem, nullptr
169 };
170
171 // execute the create slot
172 GetBindings().Execute( SID_FM_CREATE_FIELDCONTROL, pArgs );
173 }
174
175 return nullptr != pSelected;
176}
177
178void FmFieldWin::_propertyChanged(const css::beans::PropertyChangeEvent& evt)
179{
180 css::uno::Reference< css::form::XForm > xForm(evt.Source, css::uno::UNO_QUERY);
181 UpdateContent(xForm);
182}
183
184void FmFieldWin::StateChangedAtToolBoxControl(sal_uInt16 nSID, SfxItemState eState, const SfxPoolItem* pState)
185{
186 if (!pState || SID_FM_FIELDS_CONTROL != nSID)
187 return;
188
189 if (eState >= SfxItemState::DEFAULT)
190 {
191 FmFormShell* pShell = dynamic_cast<FmFormShell*>( static_cast<const SfxObjectItem*>(pState)->GetShell() );
192 UpdateContent(pShell);
193 }
194 else
195 UpdateContent(nullptr);
196}
197
199{
200 m_xListBox->clear();
201 m_aListBoxData.clear();
202 OUString aTitle(SvxResId(RID_STR_FIELDSELECTION));
203 m_xDialog->set_title(aTitle);
204
205 if (!pShell || !pShell->GetImpl())
206 return;
207
208 Reference<XForm> const xForm = pShell->GetImpl()->getCurrentForm_Lock();
209 if ( xForm.is() )
210 UpdateContent( xForm );
211}
212
213void FmFieldWin::UpdateContent(const css::uno::Reference< css::form::XForm > & xForm)
214{
215 try
216 {
217 // delete ListBox
218 m_xListBox->clear();
219 m_aListBoxData.clear();
220 OUString aTitle(SvxResId(RID_STR_FIELDSELECTION));
221 m_xDialog->set_title(aTitle);
222
223 if (!xForm.is())
224 return;
225
226 Reference< XPropertySet > xSet(xForm, UNO_QUERY);
227
228 m_aObjectName = ::comphelper::getString(xSet->getPropertyValue(FM_PROP_COMMAND));
229 m_aDatabaseName = ::comphelper::getString(xSet->getPropertyValue(FM_PROP_DATASOURCE));
230 m_nObjectType = ::comphelper::getINT32(xSet->getPropertyValue(FM_PROP_COMMANDTYPE));
231
232 // get the connection of the form
234 connectRowset( Reference< XRowSet >( xForm, UNO_QUERY ), ::comphelper::getProcessComponentContext(), nullptr ),
236 );
237 // TODO: When incompatible changes (such as extending the "virtualdbtools" interface by ensureRowSetConnection)
238 // are allowed, again, we should change this: dbtools should consistently use SharedConnection all over
239 // the place, and connectRowset should be replaced with ensureRowSetConnection
240
241 // get the fields of the object
242
243 if ( m_aConnection.is() && !m_aObjectName.isEmpty() )
244 {
245 Reference< XComponent > xKeepFieldsAlive;
246 Reference< XNameAccess > xColumns = getFieldsByCommandDescriptor( m_aConnection, m_nObjectType, m_aObjectName,xKeepFieldsAlive );
247 if ( xColumns.is() )
248 addToList(xColumns);
249 }
250
251 // set prefix
252 OUString aPrefix;
253
254 switch (m_nObjectType)
255 {
256 case CommandType::TABLE:
257 aPrefix = SvxResId(RID_RSC_TABWIN_PREFIX[0]);
258 break;
259 case CommandType::QUERY:
260 aPrefix = SvxResId(RID_RSC_TABWIN_PREFIX[1]);
261 break;
262 default:
263 aPrefix = SvxResId(RID_RSC_TABWIN_PREFIX[2]);
264 break;
265 }
266
267 // listen for changes at ControlSource in PropertySet
268 if (m_xChangeListener.is())
269 {
270 m_xChangeListener->dispose();
271 m_xChangeListener.clear();
272 }
273 m_xChangeListener = new ::comphelper::OPropertyChangeMultiplexer(this, xSet);
277
278 // set title
279 aTitle += " " + aPrefix + " " + m_aObjectName;
280 m_xDialog->set_title(aTitle);
281 }
282 catch( const Exception& )
283 {
284 TOOLS_WARN_EXCEPTION( "svx", "FmTabWin::UpdateContent" );
285 }
286}
287
289{
290 rInfo.bVisible = false;
291}
292
294
295FmFieldWinMgr::FmFieldWinMgr(vcl::Window* _pParent, sal_uInt16 _nId,
296 SfxBindings* _pBindings, SfxChildWinInfo const * _pInfo)
297 :SfxChildWindow(_pParent, _nId)
298{
299 auto xDlg = std::make_shared<FmFieldWin>(_pBindings, this, _pParent->GetFrameWeld());
300 SetController(xDlg);
301 SetHideNotDelete(true);
302 xDlg->Initialize(_pInfo);
303}
304
305/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XExecutableDialog > m_xDialog
sal_Int32 m_nObjectType
Definition: tabwin.hxx:45
const OUString & GetObjectName() const
Definition: tabwin.hxx:68
const ::dbtools::SharedConnection & GetConnection() const
Definition: tabwin.hxx:67
OUString m_aObjectName
Definition: tabwin.hxx:44
SfxBindings & GetBindings()
virtual void _propertyChanged(const css::beans::PropertyChangeEvent &evt) override
Definition: tabwin.cxx:178
std::vector< std::unique_ptr< ColumnInfo > > m_aListBoxData
Definition: tabwin.hxx:40
::dbtools::SharedConnection m_aConnection
Definition: tabwin.hxx:42
sal_Int32 GetObjectType() const
Definition: tabwin.hxx:69
bool createSelectionControls()
Definition: tabwin.cxx:149
rtl::Reference< svx::OColumnTransferable > m_xHelper
Definition: tabwin.hxx:48
OUString m_aDatabaseName
Definition: tabwin.hxx:43
virtual void StateChangedAtToolBoxControl(sal_uInt16 nSID, SfxItemState eState, const SfxPoolItem *pState) override
Definition: tabwin.cxx:184
virtual ~FmFieldWin() override
Definition: tabwin.cxx:134
const OUString & GetDatabaseName() const
Definition: tabwin.hxx:66
std::unique_ptr< weld::TreeView > m_xListBox
Definition: tabwin.hxx:39
void UpdateContent(FmFormShell const *)
Definition: tabwin.cxx:198
rtl::Reference< comphelper::OPropertyChangeMultiplexer > m_xChangeListener
Definition: tabwin.hxx:47
FmFieldWin(SfxBindings *pBindings, SfxChildWindow *pMgr, weld::Window *pParent)
Definition: tabwin.cxx:112
void FillInfo(SfxChildWinInfo &rInfo) const override
Definition: tabwin.cxx:288
void addToList(const css::uno::Reference< css::container::XNameAccess > &i_xColumns)
Definition: tabwin.cxx:71
FmXFormShell * GetImpl() const
Definition: fmshell.hxx:118
bool Execute(sal_uInt16 nSlot, const SfxPoolItem **pArgs=nullptr, SfxCallMode nCall=SfxCallMode::SLOT)
virtual void dispose()
class encapsulating the css::sdb::DataAccessDescriptor service.
const css::uno::Reference< INTERFACE > & getTyped() const
void reset(const css::uno::Reference< INTERFACE > &_rxComponent, AssignmentMode _eMode=TakeOwnership)
#define TOOLS_WARN_EXCEPTION(area, stream)
OUString SvxResId(TranslateId aId)
Definition: dialmgr.cxx:24
ScXMLEditAttributeMap::Entry const aEntries[]
constexpr OUStringLiteral FM_PROP_COMMAND
Definition: fmprop.hxx:119
constexpr OUStringLiteral FM_PROP_LABEL
Definition: fmprop.hxx:42
constexpr OUStringLiteral FM_PROP_COMMANDTYPE
Definition: fmprop.hxx:120
constexpr OUStringLiteral FM_PROP_DATASOURCE
Definition: fmprop.hxx:80
Reference< XColumn > xColumn
constexpr OStringLiteral HID_FIELD_SEL
Definition: helpids.h:48
constexpr OStringLiteral HID_FIELD_SEL_WIN
Definition: helpids.h:49
std::mutex m_aMutex
Shell * GetShell()
@ Exception
Reference< XNameAccess > getFieldsByCommandDescriptor(const Reference< XConnection > &_rxConnection, const sal_Int32 _nCommandType, const OUString &_rCommand, Reference< XComponent > &_rxKeepFieldsAlive, SQLExceptionInfo *_pErrorInfo)
Reference< XConnection > connectRowset(const Reference< XRowSet > &_rxRowSet, const Reference< XComponentContext > &_rxContext, const Reference< XWindow > &_rxParent)
class FmSearchEngine - Impl class for FmSearchDialog
long Long
OUString toId(const void *pValue)
SfxItemState
SFX_IMPL_MODELESSDIALOGCONTOLLER(SfxRecordingFloatWrapper_Impl, SID_RECORDING_FLOATWINDOW)
ColumnInfo(int num)
const tools::Long STD_WIN_SIZE_Y
Definition: tabwin.cxx:47
IMPL_LINK(FmFieldWin, DragBeginHdl, bool &, rUnsetDragIcon, bool)
Definition: tabwin.cxx:89
const tools::Long STD_WIN_SIZE_X
Definition: tabwin.cxx:46
IMPL_LINK_NOARG(FmFieldWin, RowActivatedHdl, weld::TreeView &, bool)
Definition: tabwin.cxx:144
#define DND_ACTION_COPY
OUString sId