bump product version to 6.4.0.3
[LibreOffice.git] / reportdesign / source / ui / dlg / AddField.cxx
blob6c7728ebc54d863d2952bd869b8731287b722a66
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #include <AddField.hxx>
20 #include <UITools.hxx>
21 #include <svx/dbaexchange.hxx>
22 #include <svx/svdpagv.hxx>
23 #include <com/sun/star/sdb/CommandType.hpp>
24 #include <com/sun/star/util/URL.hpp>
25 #include <com/sun/star/sdb/XDocumentDataSource.hpp>
26 #include <com/sun/star/i18n/XCollator.hpp>
27 #include <com/sun/star/beans/XPropertyChangeListener.hpp>
29 #include <vcl/event.hxx>
30 #include <vcl/waitobj.hxx>
31 #include <vcl/svapp.hxx>
32 #include <vcl/settings.hxx>
34 #include <tools/diagnose_ex.h>
35 #include <rptui_slotid.hrc>
37 #include <connectivity/dbtools.hxx>
38 #include <core_resource.hxx>
39 #include <helpids.h>
40 #include <strings.hrc>
41 #include <strings.hxx>
42 #include <ColumnInfo.hxx>
44 #include <comphelper/processfactory.hxx>
45 #include <comphelper/property.hxx>
46 #include <svtools/imgdef.hxx>
47 #include <vcl/treelistentry.hxx>
49 namespace rptui
51 const long STD_WIN_SIZE_X = 180;
52 const long STD_WIN_SIZE_Y = 320;
54 using namespace ::com::sun::star;
55 using namespace sdbc;
56 using namespace sdb;
57 using namespace uno;
58 using namespace datatransfer;
59 using namespace beans;
60 using namespace lang;
61 using namespace container;
62 using namespace ::svx;
64 class OAddFieldWindowListBox: public SvTreeListBox
66 VclPtr<OAddFieldWindow> m_pTabWin;
68 public:
69 explicit OAddFieldWindowListBox(Window* pParent, OAddFieldWindow* pFieldWindow);
70 virtual ~OAddFieldWindowListBox() override;
71 virtual void dispose() override;
73 sal_Int8 AcceptDrop( const AcceptDropEvent& rEvt ) override;
74 sal_Int8 ExecuteDrop( const ExecuteDropEvent& rEvt ) override;
76 uno::Sequence< beans::PropertyValue > getSelectedFieldDescriptors();
78 protected:
79 // DragSourceHelper
80 virtual void StartDrag( sal_Int8 nAction, const Point& rPosPixel ) override;
82 private:
83 using SvTreeListBox::ExecuteDrop;
86 uno::Sequence< beans::PropertyValue > OAddFieldWindowListBox::getSelectedFieldDescriptors()
88 uno::Sequence< beans::PropertyValue > aArgs(GetSelectionCount());
89 sal_Int32 i = 0;
90 SvTreeListEntry* pSelected = FirstSelected();
91 while( pSelected )
93 // build a descriptor for the currently selected field
94 svx::ODataAccessDescriptor aDescriptor;
95 m_pTabWin->fillDescriptor(pSelected,aDescriptor);
96 aArgs[i++].Value <<= aDescriptor.createPropertyValueSequence();
97 pSelected = NextSelected(pSelected);
99 return aArgs;
102 // class OAddFieldWindowListBox
105 OAddFieldWindowListBox::OAddFieldWindowListBox(Window *pParent, OAddFieldWindow* pFieldWindow)
106 : SvTreeListBox(pParent, WB_TABSTOP|WB_BORDER|WB_SORT)
107 , m_pTabWin(pFieldWindow)
109 SetHelpId( HID_RPT_FIELD_SEL );
110 SetSelectionMode(SelectionMode::Multiple);
111 SetDragDropMode( DragDropMode::ALL );
112 SetHighlightRange( );
116 OAddFieldWindowListBox::~OAddFieldWindowListBox()
118 disposeOnce();
121 void OAddFieldWindowListBox::dispose()
123 m_pTabWin.clear();
124 SvTreeListBox::dispose();
127 sal_Int8 OAddFieldWindowListBox::AcceptDrop( const AcceptDropEvent& /*rEvt*/ )
129 return DND_ACTION_NONE;
133 sal_Int8 OAddFieldWindowListBox::ExecuteDrop( const ExecuteDropEvent& /*rEvt*/ )
135 return DND_ACTION_NONE;
139 void OAddFieldWindowListBox::StartDrag( sal_Int8 /*_nAction*/, const Point& /*_rPosPixel*/ )
141 if ( GetSelectionCount() < 1 )
142 // no drag without a field
143 return;
145 rtl::Reference<OMultiColumnTransferable> pDataContainer = new OMultiColumnTransferable(getSelectedFieldDescriptors());
147 EndSelection();
148 pDataContainer->StartDrag( this, DND_ACTION_COPYMOVE | DND_ACTION_LINK );
151 OAddFieldWindow::OAddFieldWindow(vcl::Window* pParent ,const uno::Reference< beans::XPropertySet >& _xRowSet)
152 : FloatingWindow(pParent, "FloatingField", "modules/dbreport/ui/floatingfield.ui")
153 , ::comphelper::OPropertyChangeListener(m_aMutex)
154 , ::comphelper::OContainerListener(m_aMutex)
155 , m_xRowSet(_xRowSet)
156 , m_pListBox(VclPtr<OAddFieldWindowListBox>::Create(get<vcl::Window>("box"), this))
157 , m_nCommandType(0)
158 , m_bEscapeProcessing(false)
160 get(m_aActions, "toolbox");
161 m_nSortUpId = m_aActions->GetItemId(0);
162 m_nSortDownId = m_aActions->GetItemId(1);
163 m_nRemoveSortId = m_aActions->GetItemId(2);
164 m_nInsertId = m_aActions->GetItemId(4);
165 get(m_aHelpText, "helptext");
167 SetHelpId( HID_RPT_FIELD_SEL_WIN );
168 SetBackground( Wallpaper( Application::GetSettings().GetStyleSettings().GetFaceColor()) );
169 SetMinOutputSizePixel(Size(STD_WIN_SIZE_X,STD_WIN_SIZE_Y));
171 m_aActions->SetLineSpacing(true);
172 m_aActions->SetBackground( Wallpaper( Application::GetSettings().GetStyleSettings().GetFaceColor()) );
174 m_aActions->SetSelectHdl(LINK(this, OAddFieldWindow, OnSortAction));
175 m_aActions->CheckItem(m_nSortUpId);
176 m_aActions->EnableItem(m_nInsertId, false);
178 m_pListBox->SetDoubleClickHdl(LINK( this, OAddFieldWindow, OnDoubleClickHdl ) );
179 m_pListBox->SetSelectHdl(LINK( this, OAddFieldWindow, OnSelectHdl ) );
180 m_pListBox->SetDeselectHdl(LINK( this, OAddFieldWindow, OnSelectHdl ) );
181 m_pListBox->SetDoubleClickHdl(LINK( this, OAddFieldWindow, OnDoubleClickHdl ) );
182 m_pListBox->set_expand(true);
183 m_pListBox->set_height_request(m_pListBox->GetTextHeight() * 8);
184 m_pListBox->set_width_request(m_pListBox->approximate_char_width() * 40);
185 m_pListBox->Show();
187 m_aHelpText->SetControlBackground( GetSettings().GetStyleSettings().GetFaceColor() );
189 SetSizePixel(Size(STD_WIN_SIZE_X,STD_WIN_SIZE_Y));
191 if ( m_xRowSet.is() )
195 // be notified when the settings of report definition change
196 m_pChangeListener = new ::comphelper::OPropertyChangeMultiplexer( this, m_xRowSet );
197 m_pChangeListener->addProperty( PROPERTY_COMMAND );
198 m_pChangeListener->addProperty( PROPERTY_COMMANDTYPE );
199 m_pChangeListener->addProperty( PROPERTY_ESCAPEPROCESSING );
200 m_pChangeListener->addProperty( PROPERTY_FILTER );
202 catch( const Exception& )
204 DBG_UNHANDLED_EXCEPTION("reportdesign");
209 OAddFieldWindow::~OAddFieldWindow()
211 disposeOnce();
214 void OAddFieldWindow::dispose()
216 if ( m_pListBox.get() )
218 SvTreeList* pModel = m_pListBox->GetModel();
219 sal_uLong nCount = pModel->GetEntryCount();
220 for(sal_uLong i = 0; i< nCount;++i)
222 delete static_cast<ColumnInfo*>(pModel->GetEntry(i)->GetUserData());
225 if (m_pChangeListener.is())
226 m_pChangeListener->dispose();
227 if ( m_pContainerListener.is() )
228 m_pContainerListener->dispose();
230 m_aActions.clear();
231 m_aHelpText.clear();
232 m_pListBox.disposeAndClear();
233 FloatingWindow::dispose();
236 void OAddFieldWindow::GetFocus()
238 if ( m_pListBox.get() )
239 m_pListBox->GrabFocus();
240 else
241 FloatingWindow::GetFocus();
244 uno::Sequence< beans::PropertyValue > OAddFieldWindow::getSelectedFieldDescriptors()
246 return m_pListBox->getSelectedFieldDescriptors();
250 bool OAddFieldWindow::PreNotify( NotifyEvent& _rNEvt )
252 if ( MouseNotifyEvent::KEYINPUT == _rNEvt.GetType() )
254 const vcl::KeyCode& rKeyCode = _rNEvt.GetKeyEvent()->GetKeyCode();
255 if ( ( 0 == rKeyCode.GetModifier() ) && ( KEY_RETURN == rKeyCode.GetCode() ) )
257 if ( m_aCreateLink.IsSet() )
259 m_aCreateLink.Call(*this);
260 return true;
265 return FloatingWindow::PreNotify( _rNEvt );
268 void OAddFieldWindow::_propertyChanged( const beans::PropertyChangeEvent& _evt )
270 OSL_ENSURE( _evt.Source == m_xRowSet, "OAddFieldWindow::_propertyChanged: where did this come from?" );
271 Update();
275 namespace
277 void lcl_addToList( OAddFieldWindowListBox& _rListBox, const uno::Sequence< OUString >& _rEntries )
279 for ( const OUString& rEntry : _rEntries )
280 _rListBox.InsertEntry( rEntry,nullptr,false,TREELIST_APPEND,new ColumnInfo(rEntry) );
282 void lcl_addToList( OAddFieldWindowListBox& _rListBox, const uno::Reference< container::XNameAccess>& i_xColumns )
284 const uno::Sequence< OUString > aEntries = i_xColumns->getElementNames();
285 for ( const OUString& rEntry : aEntries )
287 uno::Reference< beans::XPropertySet> xColumn(i_xColumns->getByName(rEntry),UNO_QUERY_THROW);
288 OUString sLabel;
289 if ( xColumn->getPropertySetInfo()->hasPropertyByName(PROPERTY_LABEL) )
290 xColumn->getPropertyValue(PROPERTY_LABEL) >>= sLabel;
291 if ( !sLabel.isEmpty() )
292 _rListBox.InsertEntry( sLabel,nullptr,false,TREELIST_APPEND,new ColumnInfo(rEntry,sLabel) );
293 else
294 _rListBox.InsertEntry( rEntry,nullptr,false,TREELIST_APPEND,new ColumnInfo(rEntry,sLabel) );
300 void OAddFieldWindow::Update()
302 SolarMutexGuard aSolarGuard;
304 if ( m_pContainerListener.is() )
305 m_pContainerListener->dispose();
306 m_pContainerListener = nullptr;
307 m_xColumns.clear();
311 // ListBox loeschen
312 m_pListBox->Clear();
313 const ToolBox::ImplToolItems::size_type nItemCount = m_aActions->GetItemCount();
314 for (ToolBox::ImplToolItems::size_type j = 0; j< nItemCount; ++j)
316 m_aActions->EnableItem(m_aActions->GetItemId(j),false);
319 OUString aTitle(RptResId(RID_STR_FIELDSELECTION));
320 SetText(aTitle);
321 if ( m_xRowSet.is() )
323 OUString sCommand( m_aCommandName );
324 sal_Int32 nCommandType( m_nCommandType );
325 bool bEscapeProcessing( m_bEscapeProcessing );
326 OUString sFilter( m_sFilter );
328 OSL_VERIFY( m_xRowSet->getPropertyValue( PROPERTY_COMMAND ) >>= sCommand );
329 OSL_VERIFY( m_xRowSet->getPropertyValue( PROPERTY_COMMANDTYPE ) >>= nCommandType );
330 OSL_VERIFY( m_xRowSet->getPropertyValue( PROPERTY_ESCAPEPROCESSING ) >>= bEscapeProcessing );
331 OSL_VERIFY( m_xRowSet->getPropertyValue( PROPERTY_FILTER ) >>= sFilter );
333 m_aCommandName = sCommand;
334 m_nCommandType = nCommandType;
335 m_bEscapeProcessing = bEscapeProcessing;
336 m_sFilter = sFilter;
338 // add the columns to the list
339 uno::Reference< sdbc::XConnection> xCon = getConnection();
340 if ( xCon.is() && !m_aCommandName.isEmpty() )
341 m_xColumns = dbtools::getFieldsByCommandDescriptor( xCon, GetCommandType(), GetCommand(), m_xHoldAlive );
342 if ( m_xColumns.is() )
344 lcl_addToList( *m_pListBox, m_xColumns );
345 uno::Reference< container::XContainer> xContainer(m_xColumns,uno::UNO_QUERY);
346 if ( xContainer.is() )
347 m_pContainerListener = new ::comphelper::OContainerListenerAdapter(this,xContainer);
350 // add the parameter columns to the list
351 uno::Reference< css::sdbc::XRowSet > xRowSet(m_xRowSet,uno::UNO_QUERY);
352 Sequence< OUString > aParamNames( getParameterNames( xRowSet ) );
353 lcl_addToList( *m_pListBox, aParamNames );
355 // set title
356 aTitle += " " + m_aCommandName;
357 SetText( aTitle );
358 if ( !m_aCommandName.isEmpty() )
360 for (ToolBox::ImplToolItems::size_type i = 0; i < nItemCount; ++i)
362 m_aActions->EnableItem(m_aActions->GetItemId(i));
365 OnSelectHdl(nullptr);
368 catch( const Exception& )
370 DBG_UNHANDLED_EXCEPTION("reportdesign");
374 uno::Reference< sdbc::XConnection> OAddFieldWindow::getConnection() const
376 return uno::Reference< sdbc::XConnection>(m_xRowSet->getPropertyValue( PROPERTY_ACTIVECONNECTION ),uno::UNO_QUERY);
379 void OAddFieldWindow::fillDescriptor(SvTreeListEntry const * _pSelected,svx::ODataAccessDescriptor& _rDescriptor)
381 if ( _pSelected && m_xColumns.is() )
383 uno::Reference<container::XChild> xChild(getConnection(),uno::UNO_QUERY);
384 if ( xChild.is( ) )
386 uno::Reference<sdb::XDocumentDataSource> xDocument( xChild->getParent(), uno::UNO_QUERY );
387 if ( xDocument.is() )
389 uno::Reference<frame::XModel> xModel(xDocument->getDatabaseDocument(),uno::UNO_QUERY);
390 if ( xModel.is() )
391 _rDescriptor[ DataAccessDescriptorProperty::DatabaseLocation ] <<= xModel->getURL();
395 _rDescriptor[ svx::DataAccessDescriptorProperty::Command ] <<= GetCommand();
396 _rDescriptor[ svx::DataAccessDescriptorProperty::CommandType ] <<= GetCommandType();
397 _rDescriptor[ svx::DataAccessDescriptorProperty::EscapeProcessing ] <<= GetEscapeProcessing();
398 _rDescriptor[ svx::DataAccessDescriptorProperty::Connection ] <<= getConnection();
400 ColumnInfo* pInfo = static_cast<ColumnInfo*>(_pSelected->GetUserData());
401 _rDescriptor[ svx::DataAccessDescriptorProperty::ColumnName ] <<= pInfo->sColumnName;
402 if ( m_xColumns->hasByName( pInfo->sColumnName ) )
403 _rDescriptor[ svx::DataAccessDescriptorProperty::ColumnObject ] = m_xColumns->getByName(pInfo->sColumnName);
407 void OAddFieldWindow::_elementInserted( const container::ContainerEvent& _rEvent )
409 if ( m_pListBox.get() )
411 OUString sName;
412 if ( (_rEvent.Accessor >>= sName) && m_xColumns->hasByName(sName) )
414 uno::Reference< beans::XPropertySet> xColumn(m_xColumns->getByName(sName),UNO_QUERY_THROW);
415 OUString sLabel;
416 if ( xColumn->getPropertySetInfo()->hasPropertyByName(PROPERTY_LABEL) )
417 xColumn->getPropertyValue(PROPERTY_LABEL) >>= sLabel;
418 if ( !sLabel.isEmpty() )
419 m_pListBox->InsertEntry( sLabel,nullptr,false,TREELIST_APPEND,new ColumnInfo(sName,sLabel) );
420 else
421 m_pListBox->InsertEntry( sName,nullptr,false,TREELIST_APPEND,new ColumnInfo(sName,sLabel) );
426 void OAddFieldWindow::_elementRemoved( const container::ContainerEvent& /*_rEvent*/ )
428 if ( m_pListBox.get() )
430 m_pListBox->Clear();
431 if ( m_xColumns.is() )
432 lcl_addToList( *m_pListBox, m_xColumns );
436 void OAddFieldWindow::_elementReplaced( const container::ContainerEvent& /*_rEvent*/ )
440 IMPL_LINK_NOARG( OAddFieldWindow, OnSelectHdl, SvTreeListBox*, void )
442 m_aActions->EnableItem(m_nInsertId, ( m_pListBox.get() && m_pListBox->GetSelectionCount() > 0 ));
445 IMPL_LINK_NOARG( OAddFieldWindow, OnDoubleClickHdl, SvTreeListBox*, bool )
447 m_aCreateLink.Call(*this);
449 return false;
452 IMPL_LINK_NOARG( OAddFieldWindow, OnSortAction, ToolBox*, void )
454 const sal_uInt16 nCurItem = m_aActions->GetCurItemId();
455 if (nCurItem == m_nInsertId)
456 OnDoubleClickHdl(nullptr);
457 else
459 if (nCurItem == m_nRemoveSortId || !m_aActions->IsItemChecked(nCurItem))
461 const ToolBox::ImplToolItems::size_type nItemCount = m_aActions->GetItemCount();
462 for (ToolBox::ImplToolItems::size_type j = 0; j< nItemCount; ++j)
464 const sal_uInt16 nItemId = m_aActions->GetItemId(j);
465 if ( nCurItem != nItemId )
466 m_aActions->CheckItem(nItemId,false);
468 SvSortMode eSortMode = SortNone;
469 if (nCurItem != m_nRemoveSortId)
471 m_aActions->CheckItem(nCurItem,!m_aActions->IsItemChecked(nCurItem));
472 if (m_aActions->IsItemChecked(m_nSortUpId))
473 eSortMode = SortAscending;
474 else if (m_aActions->IsItemChecked(m_nSortDownId))
475 eSortMode = SortDescending;
478 m_pListBox->GetModel()->SetSortMode(eSortMode);
479 if (nCurItem == m_nRemoveSortId)
480 Update();
482 m_pListBox->GetModel()->Resort();
488 } // namespace rptui
491 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */