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 
45 const long STD_WIN_SIZE_X = 120;
46 const long STD_WIN_SIZE_Y = 150;
47 
48 using namespace ::com::sun::star::sdbc;
49 using namespace ::com::sun::star::sdb;
50 using namespace ::com::sun::star::uno;
51 using namespace ::com::sun::star::datatransfer;
52 using namespace ::com::sun::star::beans;
53 using namespace ::com::sun::star::lang;
54 using namespace ::com::sun::star::form;
55 using namespace ::com::sun::star::container;
56 using namespace ::com::sun::star;
57 using namespace ::svxform;
58 using namespace ::svx;
59 using namespace ::dbtools;
60 
61 struct ColumnInfo
62 {
63  OUString sColumnName;
64  explicit ColumnInfo(const OUString& i_sColumnName)
65  : sColumnName(i_sColumnName)
66  {
67  }
68 };
69 
70 void 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(OUString::number(reinterpret_cast<sal_Int64>(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 
88 IMPL_LINK(FmFieldWin, DragBeginHdl, bool&, rUnsetDragIcon, bool)
89 {
90  rUnsetDragIcon = false;
91 
92  ColumnInfo* pSelected = reinterpret_cast<ColumnInfo*>(m_xListBox->get_selected_id().toInt64());
93  if (!pSelected)
94  {
95  // no drag without a field
96  return true;
97  }
98 
99  svx::ODataAccessDescriptor aDescriptor;
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 
143 IMPL_LINK_NOARG(FmFieldWin, RowActivatedHdl, weld::TreeView&, bool)
144 {
145  return createSelectionControls();
146 }
147 
149 {
150  ColumnInfo* pSelected = reinterpret_cast<ColumnInfo*>(m_xListBox->get_selected_id().toInt64());
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, makeAny( 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 
177 void 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 
183 void FmFieldWin::StateChanged(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 
212 void 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);
274  m_xChangeListener->addProperty(FM_PROP_COMMAND);
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 
294 FmFieldWinMgr::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: */
osl::Mutex m_aMutex
#define DND_ACTION_COPY
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
virtual void _propertyChanged(const css::beans::PropertyChangeEvent &evt) override
Definition: tabwin.cxx:177
FmXFormShell * GetImpl() const
Definition: fmshell.hxx:118
const long STD_WIN_SIZE_Y
Definition: tabwin.cxx:46
#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
IMPL_LINK(FmFieldWin, DragBeginHdl, bool &, rUnsetDragIcon, bool)
Definition: tabwin.cxx:88
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
void UpdateContent(FmFormShell const *)
Definition: tabwin.cxx:197
std::vector< std::unique_ptr< ColumnInfo > > m_aListBoxData
Definition: tabwin.hxx:40
FmFieldWin(SfxBindings *pBindings, SfxChildWindow *pMgr, weld::Window *pParent)
Definition: tabwin.cxx:111
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:45
virtual ~FmFieldWin() override
Definition: tabwin.cxx:133
SFX_IMPL_MODELESSDIALOGCONTOLLER(SfxRecordingFloatWrapper_Impl, SID_RECORDING_FLOATWINDOW)
class encapsulating the css::sdb::DataAccessDescriptor service.
SfxItemState
::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:183
#define FM_PROP_COMMANDTYPE
Definition: fmprop.hxx:118
bool createSelectionControls()
Definition: tabwin.cxx:148
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
IMPL_LINK_NOARG(FmFieldWin, RowActivatedHdl, weld::TreeView &, bool)
Definition: tabwin.cxx:143
css::uno::Any SAL_CALL makeAny(const SharedUNOComponent< INTERFACE, COMPONENT > &value)