| 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 | #include <sdr/properties/e3dsceneproperties.hxx> |
| 21 | #include <svl/itemset.hxx> |
| 22 | #include <svl/whiter.hxx> |
| 23 | #include <svx/svddef.hxx> |
| 24 | #include <svx/scene3d.hxx> |
| 25 | #include <osl/diagnose.h> |
| 26 | #include <memory> |
| 27 | |
| 28 | |
| 29 | namespace sdr::properties |
| 30 | { |
| 31 | E3dSceneProperties::E3dSceneProperties(SdrObject& rObj) |
| 32 | : E3dProperties(rObj) |
| 33 | { |
| 34 | } |
| 35 | |
| 36 | E3dSceneProperties::E3dSceneProperties(const E3dSceneProperties& rProps, SdrObject& rObj) |
| 37 | : E3dProperties(rProps, rObj) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | E3dSceneProperties::~E3dSceneProperties() |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | std::unique_ptr<BaseProperties> E3dSceneProperties::Clone(SdrObject& rObj) const |
| 46 | { |
| 47 | return std::unique_ptr<BaseProperties>(new E3dSceneProperties(*this, rObj)); |
| 48 | } |
| 49 | |
| 50 | const SfxItemSet& E3dSceneProperties::GetMergedItemSet() const |
| 51 | { |
| 52 | // prepare ItemSet |
| 53 | if(moItemSet) |
| 54 | { |
| 55 | // filter for SDRATTR_3DSCENE_ items, only keep those items |
| 56 | SfxItemSetFixed<SDRATTR_3DSCENE_FIRST, SDRATTR_3DSCENE_LAST> aNew(*moItemSet->GetPool()); |
| 57 | aNew.Put(*moItemSet); |
| 58 | moItemSet->ClearItem(); |
| 59 | moItemSet->Put(aNew); |
| 60 | } |
| 61 | else |
| 62 | { |
| 63 | // No ItemSet yet, force local ItemSet |
| 64 | GetObjectItemSet(); |
| 65 | } |
| 66 | |
| 67 | // collect all ItemSets of contained 3d objects |
| 68 | const SdrObjList* pSub(static_cast<const E3dScene&>(GetSdrObject()).GetSubList()); |
| 69 | OSL_ENSURE(nullptr != pSub, "Children of SdrObject expected (!)"); |
| 70 | |
| 71 | if (pSub) |
| 72 | for (const rtl::Reference<SdrObject>& pObj : *pSub) |
| 73 | { |
| 74 | if(dynamic_cast<const E3dCompoundObject* >(pObj.get())) |
| 75 | { |
| 76 | const SfxItemSet& rSet = pObj->GetMergedItemSet(); |
| 77 | SfxWhichIter aIter(rSet); |
| 78 | sal_uInt16 nWhich(aIter.FirstWhich()); |
| 79 | |
| 80 | while(nWhich) |
| 81 | { |
| 82 | // Leave out the SDRATTR_3DSCENE_ range, this would only be double |
| 83 | // and always equal. |
| 84 | if(nWhich <= SDRATTR_3DSCENE_FIRST || nWhich >= SDRATTR_3DSCENE_LAST) |
| 85 | { |
| 86 | if(SfxItemState::INVALID == aIter.GetItemState(false)) |
| 87 | { |
| 88 | moItemSet->InvalidateItem(nWhich); |
| 89 | } |
| 90 | else |
| 91 | { |
| 92 | moItemSet->MergeValue(rSet.Get(nWhich)); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | nWhich = aIter.NextWhich(); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // call parent |
| 102 | return E3dProperties::GetMergedItemSet(); |
| 103 | } |
| 104 | |
| 105 | void E3dSceneProperties::SetMergedItemSet(const SfxItemSet& rSet, bool bClearAllItems, bool bAdjustTextFrameWidthAndHeight) |
| 106 | { |
| 107 | // Set SDRATTR_3DOBJ_ range at contained objects. |
| 108 | const SdrObjList* pSub(static_cast<const E3dScene&>(GetSdrObject()).GetSubList()); |
| 109 | OSL_ENSURE(nullptr != pSub, "Children of SdrObject expected (!)"); |
| 110 | const size_t nCount(nullptr == pSub ? 0 : pSub->GetObjCount()); |
| 111 | |
| 112 | if(nCount) |
| 113 | { |
| 114 | // Generate filtered ItemSet which contains all but the SDRATTR_3DSCENE items. |
| 115 | // #i50808# Leak fix, Clone produces a new instance and we get ownership here |
| 116 | std::unique_ptr<SfxItemSet> xNewSet(rSet.Clone()); |
| 117 | |
| 118 | for(sal_uInt16 b(SDRATTR_3DSCENE_FIRST); b <= SDRATTR_3DSCENE_LAST; b++) |
| 119 | { |
| 120 | xNewSet->ClearItem(b); |
| 121 | } |
| 122 | |
| 123 | if(xNewSet->Count()) |
| 124 | { |
| 125 | for (const rtl::Reference<SdrObject>& pObj : *pSub) |
| 126 | { |
| 127 | if(dynamic_cast<const E3dCompoundObject* >(pObj.get())) |
| 128 | { |
| 129 | // set merged ItemSet at contained 3d object. |
| 130 | pObj->SetMergedItemSet(*xNewSet, bClearAllItems, bAdjustTextFrameWidthAndHeight); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | // call parent. This will set items on local object, too. |
| 137 | E3dProperties::SetMergedItemSet(rSet, bClearAllItems, bAdjustTextFrameWidthAndHeight); |
| 138 | } |
| 139 | |
| 140 | void E3dSceneProperties::SetMergedItem(const SfxPoolItem& rItem) |
| 141 | { |
| 142 | const SdrObjList* pSub(static_cast<const E3dScene&>(GetSdrObject()).GetSubList()); |
| 143 | OSL_ENSURE(nullptr != pSub, "Children of SdrObject expected (!)"); |
| 144 | if (pSub) |
| 145 | for (const rtl::Reference<SdrObject>& pObj : *pSub) |
| 146 | pObj->SetMergedItem(rItem); |
| 147 | |
| 148 | // #i43809# call parent. This will set items on local object, too. |
| 149 | E3dProperties::SetMergedItem(rItem); |
| 150 | } |
| 151 | |
| 152 | void E3dSceneProperties::ClearMergedItem(const sal_uInt16 nWhich) |
| 153 | { |
| 154 | const SdrObjList* pSub(static_cast<const E3dScene&>(GetSdrObject()).GetSubList()); |
| 155 | OSL_ENSURE(nullptr != pSub, "Children of SdrObject expected (!)"); |
| 156 | if (pSub) |
| 157 | for (const rtl::Reference<SdrObject>& pObj : *pSub) |
| 158 | pObj->ClearMergedItem(nWhich); |
| 159 | |
| 160 | // #i43809# call parent. This will clear items on local object, too. |
| 161 | E3dProperties::ClearMergedItem(nWhich); |
| 162 | } |
| 163 | |
| 164 | void E3dSceneProperties::PostItemChange(const sal_uInt16 nWhich) |
| 165 | { |
| 166 | // call parent |
| 167 | E3dProperties::PostItemChange(nWhich); |
| 168 | |
| 169 | // local changes |
| 170 | E3dScene& rObj = static_cast<E3dScene&>(GetSdrObject()); |
| 171 | rObj.StructureChanged(); |
| 172 | |
| 173 | switch(nWhich) |
| 174 | { |
| 175 | case SDRATTR_3DSCENE_PERSPECTIVE : |
| 176 | case SDRATTR_3DSCENE_DISTANCE : |
| 177 | case SDRATTR_3DSCENE_FOCAL_LENGTH : |
| 178 | { |
| 179 | // #83387#, #83391# |
| 180 | // one common function for the camera attributes |
| 181 | // since SetCamera() sets all three back to the ItemSet |
| 182 | Camera3D aSceneCam(rObj.GetCamera()); |
| 183 | bool bChange(false); |
| 184 | |
| 185 | // for SDRATTR_3DSCENE_PERSPECTIVE: |
| 186 | if(aSceneCam.GetProjection() != rObj.GetPerspective()) |
| 187 | { |
| 188 | aSceneCam.SetProjection(rObj.GetPerspective()); |
| 189 | bChange = true; |
| 190 | } |
| 191 | |
| 192 | // for SDRATTR_3DSCENE_DISTANCE: |
| 193 | basegfx::B3DPoint aActualPosition(aSceneCam.GetPosition()); |
| 194 | double fNew = rObj.GetDistance(); |
| 195 | |
| 196 | if(fNew != aActualPosition.getZ()) |
| 197 | { |
| 198 | aSceneCam.SetPosition(basegfx::B3DPoint(aActualPosition.getX(), aActualPosition.getY(), fNew)); |
| 199 | bChange = true; |
| 200 | } |
| 201 | |
| 202 | // for SDRATTR_3DSCENE_FOCAL_LENGTH: |
| 203 | fNew = rObj.GetFocalLength() / 100.0; |
| 204 | |
| 205 | if(aSceneCam.GetFocalLength() != fNew) |
| 206 | { |
| 207 | aSceneCam.SetFocalLength(fNew); |
| 208 | bChange = true; |
| 209 | } |
| 210 | |
| 211 | // for all |
| 212 | if(bChange) |
| 213 | { |
| 214 | rObj.SetCamera(aSceneCam); |
| 215 | } |
| 216 | |
| 217 | break; |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | void E3dSceneProperties::SetStyleSheet(SfxStyleSheet* pNewStyleSheet, bool bDontRemoveHardAttr, |
| 223 | bool bBroadcast, bool bAdjustTextFrameWidthAndHeight) |
| 224 | { |
| 225 | const SdrObjList* pSub(static_cast<const E3dScene&>(GetSdrObject()).GetSubList()); |
| 226 | OSL_ENSURE(nullptr != pSub, "Children of SdrObject expected (!)"); |
| 227 | if (!pSub) |
| 228 | return; |
| 229 | for (const rtl::Reference<SdrObject>& pObj : *pSub) |
| 230 | { |
| 231 | if(bBroadcast) |
| 232 | pObj->SetStyleSheet(pNewStyleSheet, bDontRemoveHardAttr); |
| 233 | else |
| 234 | pObj->NbcSetStyleSheet(pNewStyleSheet, bDontRemoveHardAttr, bAdjustTextFrameWidthAndHeight); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | SfxStyleSheet* E3dSceneProperties::GetStyleSheet() const |
| 239 | { |
| 240 | SfxStyleSheet* pRetval = nullptr; |
| 241 | |
| 242 | const SdrObjList* pSub(static_cast<const E3dScene&>(GetSdrObject()).GetSubList()); |
| 243 | OSL_ENSURE(nullptr != pSub, "Children of SdrObject expected (!)"); |
| 244 | if (!pSub) |
| 245 | return pRetval; |
| 246 | for (const rtl::Reference<SdrObject>& pObj : *pSub) |
| 247 | { |
| 248 | SfxStyleSheet* pCandidate = pObj->GetStyleSheet(); |
| 249 | |
| 250 | if(pRetval) |
| 251 | { |
| 252 | if(pCandidate != pRetval) |
| 253 | { |
| 254 | // different StyleSheelts, return none |
| 255 | return nullptr; |
| 256 | } |
| 257 | } |
| 258 | else |
| 259 | { |
| 260 | pRetval = pCandidate; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | return pRetval; |
| 265 | } |
| 266 | |
| 267 | void E3dSceneProperties::SetSceneItemsFromCamera() |
| 268 | { |
| 269 | // force ItemSet |
| 270 | GetObjectItemSet(); |
| 271 | |
| 272 | E3dScene& rObj = static_cast<E3dScene&>(GetSdrObject()); |
| 273 | const Camera3D& aSceneCam(rObj.GetCamera()); |
| 274 | |
| 275 | // ProjectionType |
| 276 | moItemSet->Put(Svx3DPerspectiveItem(aSceneCam.GetProjection())); |
| 277 | |
| 278 | // CamPos |
| 279 | moItemSet->Put(makeSvx3DDistanceItem(static_cast<sal_uInt32>(aSceneCam.GetPosition().getZ() + 0.5))); |
| 280 | |
| 281 | // FocalLength |
| 282 | moItemSet->Put(makeSvx3DFocalLengthItem(static_cast<sal_uInt32>((aSceneCam.GetFocalLength() * 100.0) + 0.5))); |
| 283 | } |
| 284 | } // end of namespace |
| 285 | |
| 286 | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |