| 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 |
#pragma once |
| 20 |
|
| 21 |
#include <unotools/eventlisteneradapter.hxx> |
| 22 |
#include <svl/itemset.hxx> |
| 23 |
|
| 24 |
#include <utility> |
| 25 |
|
| 26 |
namespace com::sun::star::beans { class XPropertySet; } |
| 27 |
namespace com::sun::star::beans { class XPropertySetInfo; } |
| 28 |
|
| 29 |
namespace chart::wrapper { |
| 30 |
|
| 31 |
|
| 32 |
/** This class serves for conversion between properties of an XPropertySet and |
| 33 |
SfxItems in SfxItemSets. |
| 34 |
|
| 35 |
With this helper classes, you can feed dialogs with XPropertySets and let |
| 36 |
those modify by the dialogs. |
| 37 |
|
| 38 |
You must implement GetWhichPairs() such that an SfxItemSet created with |
| 39 |
CreateEmptyItemSet() is able to hold all items that may be mapped. |
| 40 |
|
| 41 |
You also have to implement GetItemProperty(), in order to return the |
| 42 |
property name for a given which-id together with the corresponding member-id |
| 43 |
that has to be used for conversion in QueryValue/PutValue. |
| 44 |
|
| 45 |
FillSpecialItem and ApplySpecialItem may be used for special handling of |
| 46 |
individual item, e.g. if you need member-ids in QueryValue/PutValue |
| 47 |
|
| 48 |
A typical use could be the following: |
| 49 |
|
| 50 |
::comphelper::ChartTypeItemConverter aItemConverter( xPropertySet, GetItemPool() ); |
| 51 |
SfxItemSet aItemSet = aItemConverter.CreateEmptyItemSet(); |
| 52 |
aItemConverter.FillItemSet( aItemSet ); |
| 53 |
bool bChanged = false; |
| 54 |
|
| 55 |
MyDialog aDlg( aItemSet ); |
| 56 |
if( aDlg.Execute() == RET_OK ) |
| 57 |
{ |
| 58 |
const SfxItemSet* pOutItemSet = aDlg.GetOutputItemSet(); |
| 59 |
if( pOutItemSet ) |
| 60 |
bChanged = aItemConverter.ApplyItemSet( *pOutItemSet ); |
| 61 |
} |
| 62 |
|
| 63 |
if( bChanged ) |
| 64 |
{ |
| 65 |
[ apply model changes to view ] |
| 66 |
} |
| 67 |
*/ |
| 68 |
class ItemConverter : |
| 69 |
public ::utl::OEventListenerAdapter |
| 70 |
{ |
| 71 |
public: |
| 72 |
/** Construct an item converter that uses the given property set for |
| 73 |
reading/writing converted items |
| 74 |
*/ |
| 75 |
ItemConverter( |
| 76 |
css::uno::Reference< css::beans::XPropertySet > xPropertySet , |
| 77 |
SfxItemPool& rItemPool ); |
| 78 |
virtual ~ItemConverter() override; |
| 79 |
|
| 80 |
typedef sal_uInt16 tWhichIdType; |
| 81 |
typedef OUString tPropertyNameType; |
| 82 |
typedef sal_uInt8 tMemberIdType; |
| 83 |
|
| 84 |
typedef std::pair< tPropertyNameType, tMemberIdType > tPropertyNameWithMemberId; |
| 85 |
|
| 86 |
/** applies all properties that can be mapped to items into the given item |
| 87 |
set. |
| 88 |
|
| 89 |
Call this method before opening a dialog. |
| 90 |
|
| 91 |
@param rOutItemSet |
| 92 |
the SfxItemSet is filled with all items that are a result of a |
| 93 |
conversion from a property of the internal XPropertySet. |
| 94 |
*/ |
| 95 |
virtual void FillItemSet( SfxItemSet & rOutItemSet ) const; |
| 96 |
|
| 97 |
/** applies all properties that are results of a conversion from all items |
| 98 |
in rItemSet to the internal XPropertySet. |
| 99 |
|
| 100 |
Call this method after a dialog was closed with OK |
| 101 |
|
| 102 |
@return true, if any properties have been changed, false otherwise. |
| 103 |
*/ |
| 104 |
virtual bool ApplyItemSet( const SfxItemSet & rItemSet ); |
| 105 |
|
| 106 |
/** creates an empty item set using the given pool or a common pool if empty |
| 107 |
(see GetItemPool) and allowing all items given in the ranges returned by |
| 108 |
GetWhichPairs. |
| 109 |
*/ |
| 110 |
SfxItemSet CreateEmptyItemSet() const; |
| 111 |
|
| 112 |
/** Invalidates all items in rDestSet, that are set (state SfxItemState::SET) in |
| 113 |
both item sets (rDestSet and rSourceSet) and have differing content. |
| 114 |
*/ |
| 115 |
static void InvalidateUnequalItems( SfxItemSet &rDestSet, const SfxItemSet &rSourceSet ); |
| 116 |
|
| 117 |
protected: |
| 118 |
|
| 119 |
/** implement this method to provide an array of which-ranges |
| 120 |
*/ |
| 121 |
virtual const WhichRangesContainer& GetWhichPairs() const = 0; |
| 122 |
|
| 123 |
/** implement this method to return a Property object for a given which id. |
| 124 |
|
| 125 |
@param rOutProperty |
| 126 |
If true is returned, this contains the property name and the |
| 127 |
corresponding Member-Id. |
| 128 |
|
| 129 |
@return true, if the item can be mapped to a property. |
| 130 |
*/ |
| 131 |
virtual bool GetItemProperty( tWhichIdType nWhichId, tPropertyNameWithMemberId & rOutProperty ) const = 0; |
| 132 |
|
| 133 |
/** for items that can not be mapped directly to a property. |
| 134 |
|
| 135 |
This method is called from FillItemSet(), if GetItemProperty() returns |
| 136 |
false. |
| 137 |
|
| 138 |
The default implementation does nothing except showing an assertion |
| 139 |
|
| 140 |
@throws css::uno::Exception |
| 141 |
*/ |
| 142 |
virtual void FillSpecialItem( sal_uInt16 nWhichId, SfxItemSet & rOutItemSet ) const; |
| 143 |
|
| 144 |
/** for items that can not be mapped directly to a property. |
| 145 |
|
| 146 |
This method is called from ApplyItemSet(), if GetItemProperty() returns |
| 147 |
false. |
| 148 |
|
| 149 |
The default implementation returns just false and shows an assertion |
| 150 |
|
| 151 |
@return true if the item changed a property, false otherwise. |
| 152 |
|
| 153 |
@throws css::uno::Exception |
| 154 |
*/ |
| 155 |
virtual bool ApplySpecialItem( sal_uInt16 nWhichId, const SfxItemSet & rItemSet ); |
| 156 |
|
| 157 |
/// Returns the pool |
| 158 |
SfxItemPool & GetItemPool() const { return m_rItemPool;} |
| 159 |
|
| 160 |
/** Returns the XPropertySet that was given in the CTOR and is used to apply |
| 161 |
items in ApplyItemSet(). |
| 162 |
*/ |
| 163 |
const css::uno::Reference< css::beans::XPropertySet >& GetPropertySet() const { return m_xPropertySet;} |
| 164 |
|
| 165 |
// ____ ::utl::OEventListenerAdapter ____ |
| 166 |
virtual void _disposing( const css::lang::EventObject& rSource ) override; |
| 167 |
|
| 168 |
protected: |
| 169 |
/** sets a new property set, that you get with GetPropertySet(). It should |
| 170 |
not be necessary to use this method. It is introduced to allow changing |
| 171 |
the regression type of a regression curve which changes the object |
| 172 |
identity. |
| 173 |
*/ |
| 174 |
void resetPropertySet( const css::uno::Reference< css::beans::XPropertySet > & xPropSet ); |
| 175 |
|
| 176 |
private: |
| 177 |
css::uno::Reference< css::beans::XPropertySet > m_xPropertySet; |
| 178 |
css::uno::Reference< css::beans::XPropertySetInfo > m_xPropertySetInfo; |
| 179 |
|
| 180 |
SfxItemPool& m_rItemPool; |
| 181 |
}; |
| 182 |
|
| 183 |
} |
| 184 |
|
| 185 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |