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