bump product version to 6.3.0.0.beta1
[LibreOffice.git] / reportdesign / source / ui / dlg / CondFormat.cxx
blob0fa1180884baf3b4da436de5598d8afd6d6269e0
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 .
20 #include <CondFormat.hxx>
22 #include <strings.hxx>
23 #include <strings.hrc>
24 #include <rptui_slotid.hrc>
25 #include <core_resource.hxx>
26 #include <UITools.hxx>
27 #include <ReportController.hxx>
28 #include "Condition.hxx"
30 #include <com/sun/star/beans/XPropertySet.hpp>
31 #include <com/sun/star/lang/IllegalArgumentException.hpp>
33 #include <toolkit/helper/vclunohelper.hxx>
35 #include <vcl/settings.hxx>
37 #include <tools/debug.hxx>
38 #include <tools/diagnose_ex.h>
40 #include <comphelper/property.hxx>
42 #include <algorithm>
43 #include <UndoActions.hxx>
46 namespace rptui
50 using ::com::sun::star::uno::Reference;
51 using ::com::sun::star::uno::UNO_QUERY_THROW;
52 using ::com::sun::star::uno::UNO_QUERY;
53 using ::com::sun::star::uno::Exception;
54 using ::com::sun::star::lang::IllegalArgumentException;
55 using ::com::sun::star::uno::Sequence;
56 using ::com::sun::star::beans::PropertyValue;
57 using ::com::sun::star::uno::Any;
59 using namespace ::com::sun::star::report;
62 // UpdateLocker
64 class UpdateLocker
66 vcl::Window& m_rWindow;
68 public:
69 explicit UpdateLocker( vcl::Window& _rWindow )
70 :m_rWindow( _rWindow )
72 _rWindow.SetUpdateMode( false );
74 ~UpdateLocker()
76 m_rWindow.SetUpdateMode( true );
80 void ConditionalFormattingDialog::impl_setPrefHeight(bool bFirst)
82 if (!m_bConstructed && !bFirst)
83 return;
85 //allow dialog to resize itself
86 size_t nCount = impl_getConditionCount();
87 if (nCount)
89 long nHeight = m_aConditions[0]->get_preferred_size().Height();
90 size_t nVisibleConditions = ::std::min(nCount, MAX_CONDITIONS);
91 nHeight *= nVisibleConditions;
92 if (nHeight != m_pScrollWindow->get_height_request())
94 m_pScrollWindow->set_height_request(nHeight);
95 if (!isCalculatingInitialLayoutSize() && !bFirst)
96 setOptimalLayoutSize();
101 // class ConditionalFormattingDialog
102 ConditionalFormattingDialog::ConditionalFormattingDialog(
103 vcl::Window* _pParent, const Reference< XReportControlModel >& _rxFormatConditions, ::rptui::OReportController& _rController )
104 :ModalDialog( _pParent, "CondFormat", "modules/dbreport/ui/condformatdialog.ui" )
105 ,m_rController( _rController )
106 ,m_xFormatConditions( _rxFormatConditions )
107 ,m_bDeletingCondition( false )
108 ,m_bConstructed( false )
110 get(m_pConditionPlayground, "condPlaygroundDrawingarea");
111 get(m_pScrollWindow, "scrolledwindow");
112 m_pScrollWindow->setUserManagedScrolling(true);
113 m_pCondScroll = &(m_pScrollWindow->getVertScrollBar());
115 OSL_ENSURE( m_xFormatConditions.is(), "ConditionalFormattingDialog::ConditionalFormattingDialog: ReportControlModel is NULL -> Prepare for GPF!" );
117 m_xCopy.set( m_xFormatConditions->createClone(), UNO_QUERY_THROW );
119 m_pCondScroll->SetScrollHdl( LINK( this, ConditionalFormattingDialog, OnScroll ) );
121 impl_initializeConditions();
123 impl_setPrefHeight(true);
125 m_bConstructed = true;
128 ConditionalFormattingDialog::~ConditionalFormattingDialog()
130 disposeOnce();
133 void ConditionalFormattingDialog::dispose()
136 for (auto& rxCondition : m_aConditions)
138 rxCondition.disposeAndClear();
141 m_aConditions.clear();
142 m_pConditionPlayground.clear();
143 m_pScrollWindow.clear();
144 m_pCondScroll.clear();
145 ModalDialog::dispose();
148 void ConditionalFormattingDialog::impl_updateConditionIndicies()
150 sal_Int32 nIndex = 0;
151 for (const auto& rxCondition : m_aConditions)
153 rxCondition->setConditionIndex( nIndex, impl_getConditionCount() );
154 ++nIndex;
158 void ConditionalFormattingDialog::impl_conditionCountChanged()
160 if ( m_aConditions.empty() )
161 impl_addCondition_nothrow( 0 );
163 impl_setPrefHeight(false);
165 impl_updateScrollBarRange();
166 impl_updateConditionIndicies();
167 impl_layoutAll();
170 void ConditionalFormattingDialog::addCondition( size_t _nAddAfterIndex )
172 OSL_PRECOND( _nAddAfterIndex < impl_getConditionCount(), "ConditionalFormattingDialog::addCondition: illegal condition index!" );
173 impl_addCondition_nothrow( _nAddAfterIndex + 1 );
177 void ConditionalFormattingDialog::deleteCondition( size_t _nCondIndex )
179 impl_deleteCondition_nothrow( _nCondIndex );
183 void ConditionalFormattingDialog::impl_addCondition_nothrow( size_t _nNewCondIndex )
185 UpdateLocker aLockUpdates( *this );
189 if ( _nNewCondIndex > static_cast<size_t>(m_xCopy->getCount()) )
190 throw IllegalArgumentException();
192 Reference< XFormatCondition > xCond = m_xCopy->createFormatCondition();
193 ::comphelper::copyProperties(m_xCopy.get(),xCond.get());
194 m_xCopy->insertByIndex( _nNewCondIndex, makeAny( xCond ) );
195 VclPtrInstance<Condition> pCon( m_pConditionPlayground, *this, m_rController );
196 pCon->setCondition( xCond );
197 pCon->reorderWithinParent(_nNewCondIndex);
198 m_aConditions.insert( m_aConditions.begin() + _nNewCondIndex, pCon );
200 catch( const Exception& )
202 DBG_UNHANDLED_EXCEPTION("reportdesign");
205 impl_conditionCountChanged();
207 impl_ensureConditionVisible( _nNewCondIndex );
211 void ConditionalFormattingDialog::impl_focusCondition( size_t _nCondIndex )
213 OSL_PRECOND( _nCondIndex < impl_getConditionCount(),
214 "ConditionalFormattingDialog::impl_focusCondition: illegal index!" );
216 impl_ensureConditionVisible( _nCondIndex );
217 m_aConditions[ _nCondIndex ]->GrabFocus();
221 void ConditionalFormattingDialog::impl_deleteCondition_nothrow( size_t _nCondIndex )
223 UpdateLocker aLockUpdates( *this );
225 OSL_PRECOND( _nCondIndex < impl_getConditionCount(),
226 "ConditionalFormattingDialog::impl_deleteCondition_nothrow: illegal index!" );
228 bool bLastCondition = ( impl_getConditionCount() == 1 );
230 bool bSetNewFocus = false;
231 size_t nNewFocusIndex( _nCondIndex );
234 if ( !bLastCondition )
235 m_xCopy->removeByIndex( _nCondIndex );
237 Conditions::iterator pos = m_aConditions.begin() + _nCondIndex;
238 if ( bLastCondition )
240 Reference< XFormatCondition > xFormatCondition( m_xCopy->getByIndex( 0 ), UNO_QUERY_THROW );
241 xFormatCondition->setFormula( OUString() );
242 (*pos)->setCondition( xFormatCondition );
244 else
246 bSetNewFocus = (*pos)->HasChildPathFocus();
247 m_bDeletingCondition = true;
248 m_aConditions.erase( pos );
249 m_bDeletingCondition = false;
252 if ( bSetNewFocus )
254 if ( nNewFocusIndex >= impl_getConditionCount() )
255 nNewFocusIndex = impl_getConditionCount() - 1;
258 catch( const Exception& )
260 DBG_UNHANDLED_EXCEPTION("reportdesign");
263 impl_conditionCountChanged();
264 if ( bSetNewFocus )
265 impl_focusCondition( nNewFocusIndex );
269 void ConditionalFormattingDialog::impl_moveCondition_nothrow( size_t _nCondIndex, bool _bMoveUp )
271 size_t nOldConditionIndex( _nCondIndex );
272 size_t nNewConditionIndex( _bMoveUp ? _nCondIndex - 1 : _nCondIndex + 1 );
274 // do this in two steps, so we don't become inconsistent if any of the UNO actions fails
275 Any aMovedCondition;
276 Condition *pMovedCondition;
279 aMovedCondition = m_xCopy->getByIndex( static_cast<sal_Int32>(nOldConditionIndex) );
280 m_xCopy->removeByIndex( static_cast<sal_Int32>(nOldConditionIndex) );
282 Conditions::iterator aRemovePos( m_aConditions.begin() + nOldConditionIndex );
283 pMovedCondition = aRemovePos->get();
284 m_aConditions.erase( aRemovePos );
286 catch( const Exception& )
288 DBG_UNHANDLED_EXCEPTION("reportdesign");
289 return;
294 m_xCopy->insertByIndex( static_cast<sal_Int32>(nNewConditionIndex), aMovedCondition );
295 m_aConditions.insert( m_aConditions.begin() + nNewConditionIndex, pMovedCondition );
297 catch( const Exception& )
299 DBG_UNHANDLED_EXCEPTION("reportdesign");
302 // at least the two swapped conditions need to know their new index
303 impl_updateConditionIndicies();
305 // re-layout all conditions
306 impl_layoutConditions();
308 // ensure the moved condition is visible
309 impl_ensureConditionVisible( nNewConditionIndex );
312 IMPL_LINK( ConditionalFormattingDialog, OnScroll, ScrollBar*, /*_pNotInterestedIn*/, void )
314 size_t nFirstCondIndex( impl_getFirstVisibleConditionIndex() );
315 size_t nFocusCondIndex = impl_getFocusedConditionIndex( nFirstCondIndex );
317 impl_layoutConditions();
319 if ( nFocusCondIndex < nFirstCondIndex )
320 impl_focusCondition( nFirstCondIndex );
321 else if ( nFocusCondIndex >= nFirstCondIndex + MAX_CONDITIONS )
322 impl_focusCondition( nFirstCondIndex + MAX_CONDITIONS - 1 );
325 void ConditionalFormattingDialog::impl_layoutConditions()
327 if (m_aConditions.empty())
328 return;
329 long nConditionHeight = m_aConditions[0]->get_preferred_size().Height();
330 Point aConditionPos(0, -1 * nConditionHeight * impl_getFirstVisibleConditionIndex());
331 m_pConditionPlayground->SetPosPixel(aConditionPos);
334 void ConditionalFormattingDialog::impl_layoutAll()
336 // condition's positions
337 impl_layoutConditions();
339 // scrollbar visibility
340 if ( m_aConditions.size() <= MAX_CONDITIONS )
341 // normalize the position, so it can, in all situations, be used as top index
342 m_pCondScroll->SetThumbPos( 0 );
345 void ConditionalFormattingDialog::impl_initializeConditions()
349 sal_Int32 nCount = m_xCopy->getCount();
350 for ( sal_Int32 i = 0; i < nCount ; ++i )
352 VclPtrInstance<Condition> pCon( m_pConditionPlayground, *this, m_rController );
353 Reference< XFormatCondition > xCond( m_xCopy->getByIndex(i), UNO_QUERY );
354 pCon->reorderWithinParent(i);
355 pCon->setCondition( xCond );
356 pCon->updateToolbar( xCond.get() );
357 m_aConditions.push_back( pCon );
360 catch(Exception&)
362 OSL_FAIL("Can not access format condition!");
365 impl_conditionCountChanged();
368 void ConditionalFormattingDialog::applyCommand(size_t _nCondIndex, sal_uInt16 _nCommandId, const ::Color& rColor)
370 OSL_PRECOND( _nCommandId, "ConditionalFormattingDialog::applyCommand: illegal command id!" );
373 Reference< XReportControlFormat > xReportControlFormat( m_xCopy->getByIndex( _nCondIndex ), UNO_QUERY_THROW );
375 Sequence< PropertyValue > aArgs(3);
377 aArgs[0].Name = REPORTCONTROLFORMAT;
378 aArgs[0].Value <<= xReportControlFormat;
380 aArgs[1].Name = CURRENT_WINDOW;
381 aArgs[1].Value <<= VCLUnoHelper::GetInterface(this);
383 aArgs[2].Name = PROPERTY_FONTCOLOR;
384 aArgs[2].Value <<= rColor;
386 // we use this way to create undo actions
387 m_rController.executeUnChecked(_nCommandId,aArgs);
388 m_aConditions[ _nCondIndex ]->updateToolbar(xReportControlFormat);
390 catch( Exception& )
392 DBG_UNHANDLED_EXCEPTION("reportdesign");
397 void ConditionalFormattingDialog::moveConditionUp( size_t _nCondIndex )
399 OSL_PRECOND( _nCondIndex > 0, "ConditionalFormattingDialog::moveConditionUp: cannot move up the first condition!" );
400 if ( _nCondIndex > 0 )
401 impl_moveCondition_nothrow( _nCondIndex, true );
405 void ConditionalFormattingDialog::moveConditionDown( size_t _nCondIndex )
407 OSL_PRECOND( _nCondIndex < impl_getConditionCount(), "ConditionalFormattingDialog::moveConditionDown: cannot move down the last condition!" );
408 if ( _nCondIndex < impl_getConditionCount() )
409 impl_moveCondition_nothrow( _nCondIndex, false );
413 OUString ConditionalFormattingDialog::getDataField() const
415 OUString sDataField;
418 sDataField = m_xFormatConditions->getDataField();
420 catch( const Exception& )
422 DBG_UNHANDLED_EXCEPTION("reportdesign");
424 return sDataField;
428 short ConditionalFormattingDialog::Execute()
430 short nRet = ModalDialog::Execute();
431 if ( nRet == RET_OK )
433 const OUString sUndoAction( RptResId( RID_STR_UNDO_CONDITIONAL_FORMATTING ) );
434 const UndoContext aUndoContext( m_rController.getUndoManager(), sUndoAction );
437 sal_Int32 j(0), i(0);
438 for ( Conditions::const_iterator cond = m_aConditions.begin();
439 cond != m_aConditions.end();
440 ++cond, ++i
443 Reference< XFormatCondition > xCond( m_xCopy->getByIndex(i), UNO_QUERY_THROW );
444 (*cond)->fillFormatCondition( xCond );
446 if ( (*cond)->isEmpty() )
447 continue;
449 Reference< XFormatCondition > xNewCond;
450 bool bAppend = j >= m_xFormatConditions->getCount();
451 if ( bAppend )
453 xNewCond = m_xFormatConditions->createFormatCondition();
454 m_xFormatConditions->insertByIndex( i, makeAny( xNewCond ) );
456 else
457 xNewCond.set( m_xFormatConditions->getByIndex(j), UNO_QUERY );
458 ++j;
460 ::comphelper::copyProperties(xCond.get(),xNewCond.get());
463 for ( sal_Int32 k = m_xFormatConditions->getCount()-1; k >= j; --k )
464 m_xFormatConditions->removeByIndex(k);
466 ::comphelper::copyProperties( m_xCopy.get(), m_xFormatConditions.get() );
468 catch ( const Exception& )
470 DBG_UNHANDLED_EXCEPTION("reportdesign");
471 nRet = RET_NO;
474 return nRet;
478 bool ConditionalFormattingDialog::PreNotify( NotifyEvent& _rNEvt )
480 switch ( _rNEvt.GetType() )
482 case MouseNotifyEvent::KEYINPUT:
484 const KeyEvent* pKeyEvent( _rNEvt.GetKeyEvent() );
485 const vcl::KeyCode& rKeyCode = pKeyEvent->GetKeyCode();
486 if ( rKeyCode.IsMod1() && rKeyCode.IsMod2() )
488 if ( rKeyCode.GetCode() == 0x0508 )
490 impl_deleteCondition_nothrow( impl_getFocusedConditionIndex( 0 ) );
491 return true;
493 if ( rKeyCode.GetCode() == 0x0507 ) // +
495 impl_addCondition_nothrow( impl_getFocusedConditionIndex( impl_getConditionCount() - 1 ) + 1 );
496 return true;
499 break;
501 case MouseNotifyEvent::GETFOCUS:
503 if (m_bDeletingCondition)
504 break;
506 if (!m_pConditionPlayground) //e.g. during dispose
507 break;
509 const vcl::Window* pGetFocusWindow( _rNEvt.GetWindow() );
511 // determine whether the new focus window is part of an (currently invisible) condition
512 const vcl::Window* pConditionCandidate = pGetFocusWindow->GetParent();
513 const vcl::Window* pPlaygroundCandidate = pConditionCandidate ? pConditionCandidate->GetParent() : nullptr;
514 while ( pPlaygroundCandidate
515 && ( pPlaygroundCandidate != this )
516 && ( pPlaygroundCandidate != m_pConditionPlayground )
519 pConditionCandidate = pConditionCandidate->GetParent();
520 pPlaygroundCandidate = pConditionCandidate ? pConditionCandidate->GetParent() : nullptr;
522 if (pConditionCandidate && pPlaygroundCandidate == m_pConditionPlayground)
524 impl_ensureConditionVisible( dynamic_cast< const Condition& >( *pConditionCandidate ).getConditionIndex() );
526 break;
528 default:
529 break;
532 return ModalDialog::PreNotify( _rNEvt );
536 size_t ConditionalFormattingDialog::impl_getFirstVisibleConditionIndex() const
538 return static_cast<size_t>(m_pCondScroll->GetThumbPos());
542 size_t ConditionalFormattingDialog::impl_getLastVisibleConditionIndex() const
544 return ::std::min( impl_getFirstVisibleConditionIndex() + MAX_CONDITIONS, impl_getConditionCount() ) - 1;
548 size_t ConditionalFormattingDialog::impl_getFocusedConditionIndex( sal_Int32 _nFallBackIfNone ) const
550 auto cond = std::find_if(m_aConditions.begin(), m_aConditions.end(),
551 [](const VclPtr<Condition>& rxCondition) { return rxCondition->HasChildPathFocus(); });
552 if (cond != m_aConditions.end())
553 return static_cast<size_t>(std::distance(m_aConditions.begin(), cond));
554 return _nFallBackIfNone;
558 void ConditionalFormattingDialog::impl_updateScrollBarRange()
560 long nMax = ( impl_getConditionCount() > MAX_CONDITIONS ) ? impl_getConditionCount() - MAX_CONDITIONS + 1 : 0;
562 m_pCondScroll->SetRangeMin( 0 );
563 m_pCondScroll->SetRangeMax( nMax );
564 m_pCondScroll->SetVisibleSize( 1 );
568 void ConditionalFormattingDialog::impl_scrollTo( size_t _nTopCondIndex )
570 OSL_PRECOND( _nTopCondIndex + MAX_CONDITIONS <= impl_getConditionCount(),
571 "ConditionalFormattingDialog::impl_scrollTo: illegal index!" );
572 m_pCondScroll->SetThumbPos( _nTopCondIndex );
573 OnScroll( m_pCondScroll );
577 void ConditionalFormattingDialog::impl_ensureConditionVisible( size_t _nCondIndex )
579 OSL_PRECOND( _nCondIndex < impl_getConditionCount(),
580 "ConditionalFormattingDialog::impl_ensureConditionVisible: illegal index!" );
582 if ( _nCondIndex < impl_getFirstVisibleConditionIndex() )
583 impl_scrollTo( _nCondIndex );
584 else if ( _nCondIndex > impl_getLastVisibleConditionIndex() )
585 impl_scrollTo( _nCondIndex - MAX_CONDITIONS + 1 );
589 } // rptui
592 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */