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::OPropertyChangeListener2()
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 {
137 std::unique_lock g(m_aMutex);
138 if (m_xChangeListener.is())
139 {
140 m_xChangeListener->dispose(g);
141 m_xChangeListener.clear();
142 }
143 }
145}
146
148{
149 return createSelectionControls();
150}
151
153{
154 ColumnInfo* pSelected = weld::fromId<ColumnInfo*>(m_xListBox->get_selected_id());
155 if (pSelected)
156 {
157 // build a descriptor for the currently selected field
158 ODataAccessDescriptor aDescr;
159 aDescr.setDataSource(GetDatabaseName());
160
161 aDescr[ DataAccessDescriptorProperty::Connection ] <<= GetConnection().getTyped();
162
163 aDescr[ DataAccessDescriptorProperty::Command ] <<= GetObjectName();
164 aDescr[ DataAccessDescriptorProperty::CommandType ] <<= GetObjectType();
165 aDescr[ DataAccessDescriptorProperty::ColumnName ] <<= pSelected->sColumnName;
166
167 // transfer this to the SFX world
168 SfxUnoAnyItem aDescriptorItem( SID_FM_DATACCESS_DESCRIPTOR, Any( aDescr.createPropertyValueSequence() ) );
169 const SfxPoolItem* pArgs[] =
170 {
171 &aDescriptorItem, nullptr
172 };
173
174 // execute the create slot
175 GetBindings().Execute( SID_FM_CREATE_FIELDCONTROL, pArgs );
176 }
177
178 return nullptr != pSelected;
179}
180
181void FmFieldWin::_propertyChanged(const css::beans::PropertyChangeEvent& evt)
182{
183 css::uno::Reference< css::form::XForm > xForm(evt.Source, css::uno::UNO_QUERY);
184 UpdateContent(xForm);
185}
186
187void FmFieldWin::StateChangedAtToolBoxControl(sal_uInt16 nSID, SfxItemState eState, const SfxPoolItem* pState)
188{
189 if (!pState || SID_FM_FIELDS_CONTROL != nSID)
190 return;
191
192 if (eState >= SfxItemState::DEFAULT)
193 {
194 FmFormShell* pShell = dynamic_cast<FmFormShell*>( static_cast<const SfxObjectItem*>(pState)->GetShell() );
195 UpdateContent(pShell);
196 }
197 else
198 UpdateContent(nullptr);
199}
200
202{
203 m_xListBox->clear();
204 m_aListBoxData.clear();
205 OUString aTitle(SvxResId(RID_STR_FIELDSELECTION));
206 m_xDialog->set_title(aTitle);
207
208 if (!pShell || !pShell->GetImpl())
209 return;
210
211 Reference<XForm> const xForm = pShell->GetImpl()->getCurrentForm_Lock();
212 if ( xForm.is() )
213 UpdateContent( xForm );
214}
215
216void FmFieldWin::UpdateContent(const css::uno::Reference< css::form::XForm > & xForm)
217{
218 try
219 {
220 // delete ListBox
221 m_xListBox->clear();
222 m_aListBoxData.clear();
223 OUString aTitle(SvxResId(RID_STR_FIELDSELECTION));
224 m_xDialog->set_title(aTitle);
225
226 if (!xForm.is())
227 return;
228
229 Reference< XPropertySet > xSet(xForm, UNO_QUERY);
230
231 m_aObjectName = ::comphelper::getString(xSet->getPropertyValue(FM_PROP_COMMAND));
232 m_aDatabaseName = ::comphelper::getString(xSet->getPropertyValue(FM_PROP_DATASOURCE));
233 m_nObjectType = ::comphelper::getINT32(xSet->getPropertyValue(FM_PROP_COMMANDTYPE));
234
235 // get the connection of the form
237 connectRowset( Reference< XRowSet >( xForm, UNO_QUERY ), ::comphelper::getProcessComponentContext(), nullptr ),
239 );
240 // TODO: When incompatible changes (such as extending the "virtualdbtools" interface by ensureRowSetConnection)
241 // are allowed, again, we should change this: dbtools should consistently use SharedConnection all over
242 // the place, and connectRowset should be replaced with ensureRowSetConnection
243
244 // get the fields of the object
245
246 if ( m_aConnection.is() && !m_aObjectName.isEmpty() )
247 {
248 Reference< XComponent > xKeepFieldsAlive;
249 Reference< XNameAccess > xColumns = getFieldsByCommandDescriptor( m_aConnection, m_nObjectType, m_aObjectName,xKeepFieldsAlive );
250 if ( xColumns.is() )
251 addToList(xColumns);
252 }
253
254 // set prefix
255 OUString aPrefix;
256
257 switch (m_nObjectType)
258 {
259 case CommandType::TABLE:
260 aPrefix = SvxResId(RID_RSC_TABWIN_PREFIX[0]);
261 break;
262 case CommandType::QUERY:
263 aPrefix = SvxResId(RID_RSC_TABWIN_PREFIX[1]);
264 break;
265 default:
266 aPrefix = SvxResId(RID_RSC_TABWIN_PREFIX[2]);
267 break;
268 }
269
270 // listen for changes at ControlSource in PropertySet
271 std::unique_lock g(m_aMutex);
272 if (m_xChangeListener.is())
273 {
274 m_xChangeListener->dispose(g);
275 m_xChangeListener.clear();
276 }
277 m_xChangeListener = new ::comphelper::OPropertyChangeMultiplexer2(m_aMutex, g, this, xSet);
281
282 // set title
283 aTitle += " " + aPrefix + " " + m_aObjectName;
284 m_xDialog->set_title(aTitle);
285 }
286 catch( const Exception& )
287 {
288 TOOLS_WARN_EXCEPTION( "svx", "FmTabWin::UpdateContent" );
289 }
290}
291
293{
294 rInfo.bVisible = false;
295}
296
298
299FmFieldWinMgr::FmFieldWinMgr(vcl::Window* _pParent, sal_uInt16 _nId,
300 SfxBindings* _pBindings, SfxChildWinInfo const * _pInfo)
301 :SfxChildWindow(_pParent, _nId)
302{
303 auto xDlg = std::make_shared<FmFieldWin>(_pBindings, this, _pParent->GetFrameWeld());
304 SetController(xDlg);
305 SetHideNotDelete(true);
306 xDlg->Initialize(_pInfo);
307}
308
309/* 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:181
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:152
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:187
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
std::mutex m_aMutex
Definition: tabwin.hxx:38
void UpdateContent(FmFormShell const *)
Definition: tabwin.cxx:201
rtl::Reference< comphelper::OPropertyChangeMultiplexer2 > 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:292
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 OUStringLiteral HID_FIELD_SEL_WIN
Definition: helpids.h:49
constexpr OUStringLiteral HID_FIELD_SEL
Definition: helpids.h:48
Shell * GetShell()
@ Exception
class SvxPropertySetInfoPool
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:147
#define DND_ACTION_COPY
OUString sId