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