tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / sc / source / ui / drawfunc / graphsh.cxx
blob71420be473b7d8d92de97f113914230de57270b4
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 <sfx2/objface.hxx>
21 #include <vcl/EnumContext.hxx>
22 #include <sfx2/opengrf.hxx>
23 #include <svx/svdograf.hxx>
24 #include <svx/grfflt.hxx>
25 #include <svx/grafctrl.hxx>
26 #include <svx/compressgraphicdialog.hxx>
27 #include <svx/graphichelper.hxx>
28 #include <svx/svxids.hrc>
30 #include <graphsh.hxx>
31 #include <strings.hrc>
32 #include <viewdata.hxx>
33 #include <drawview.hxx>
34 #include <gridwin.hxx>
35 #include <scresid.hxx>
36 #include <svx/extedit.hxx>
38 #define ShellClass_ScGraphicShell
39 #include <scslots.hxx>
41 SFX_IMPL_INTERFACE(ScGraphicShell, ScDrawShell)
43 void ScGraphicShell::InitInterface_Impl()
45 GetStaticInterface()->RegisterObjectBar(SFX_OBJECTBAR_OBJECT,
46 SfxVisibilityFlags::Standard | SfxVisibilityFlags::Server,
47 ToolbarId::Graphic_Objectbar);
49 GetStaticInterface()->RegisterPopupMenu(u"graphic"_ustr);
53 ScGraphicShell::ScGraphicShell(ScViewData& rData) :
54 ScDrawShell(rData)
56 SetName(u"GraphicObject"_ustr);
57 SfxShell::SetContextName(vcl::EnumContext::GetContextName(vcl::EnumContext::Context::Graphic));
60 ScGraphicShell::~ScGraphicShell()
64 void ScGraphicShell::GetAttrState( SfxItemSet& rSet )
66 ScDrawView* pView = GetViewData().GetScDrawView();
68 if( pView )
69 SvxGrafAttrHelper::GetGrafAttrState( rSet, *pView );
72 void ScGraphicShell::Execute( SfxRequest& rReq )
74 ScDrawView* pView = GetViewData().GetScDrawView();
76 if( pView )
78 SvxGrafAttrHelper::ExecuteGrafAttr( rReq, *pView );
79 Invalidate();
83 void ScGraphicShell::GetFilterState( SfxItemSet& rSet )
85 ScDrawView* pView = GetViewData().GetScDrawView();
86 const SdrMarkList& rMarkList = pView->GetMarkedObjectList();
87 bool bEnable = false;
89 if( rMarkList.GetMarkCount() == 1 )
91 SdrObject* pObj = rMarkList.GetMark( 0 )->GetMarkedSdrObj();
93 if( auto pGraphicObj = dynamic_cast<SdrGrafObj*>( pObj) )
94 if( pGraphicObj->GetGraphicType() == GraphicType::Bitmap )
95 bEnable = true;
98 if( !bEnable )
99 SvxGraphicFilter::DisableGraphicFilterSlots( rSet );
102 void ScGraphicShell::ExecuteFilter( const SfxRequest& rReq )
104 ScDrawView* pView = GetViewData().GetScDrawView();
105 const SdrMarkList& rMarkList = pView->GetMarkedObjectList();
107 if( rMarkList.GetMarkCount() == 1 )
109 SdrObject* pObj = rMarkList.GetMark( 0 )->GetMarkedSdrObj();
111 if( auto pGraphicObj = dynamic_cast<SdrGrafObj*>( pObj) )
112 if( pGraphicObj->GetGraphicType() == GraphicType::Bitmap )
114 SvxGraphicFilter::ExecuteGrfFilterSlot( rReq, pGraphicObj->GetGraphicObject(),
115 [pView, pGraphicObj, pObj, rMarkList] (GraphicObject aFilterObj) -> void
117 if( SdrPageView* pPageView = pView->GetSdrPageView() )
119 rtl::Reference<SdrGrafObj> pFilteredObj = SdrObject::Clone(*pGraphicObj, pGraphicObj->getSdrModelFromSdrObject());
120 OUString aStr = rMarkList.GetMarkDescription() + " " + ScResId(SCSTR_UNDO_GRAFFILTER);
121 pView->BegUndo( aStr );
122 pFilteredObj->SetGraphicObject( aFilterObj );
123 pView->ReplaceObjectAtView( pObj, *pPageView, pFilteredObj.get() );
124 pView->EndUndo();
127 return;
130 Invalidate();
133 void ScGraphicShell::GetExternalEditState( SfxItemSet& rSet )
135 ScDrawView* pView = GetViewData().GetScDrawView();
136 const SdrMarkList& rMarkList = pView->GetMarkedObjectList();
137 bool bEnable = false;
138 if( rMarkList.GetMarkCount() == 1 )
140 SdrObject* pObj = rMarkList.GetMark( 0 )->GetMarkedSdrObj();
142 if( auto pGraphicObj = dynamic_cast<SdrGrafObj*>( pObj) )
143 if( pGraphicObj->GetGraphicType() == GraphicType::Bitmap )
144 bEnable = true;
147 if (GetObjectShell()->isExportLocked())
148 bEnable = false;
150 if( !bEnable )
151 rSet.DisableItem( SID_EXTERNAL_EDIT );
154 void ScGraphicShell::ExecuteExternalEdit( SAL_UNUSED_PARAMETER SfxRequest& )
156 ScDrawView* pView = GetViewData().GetScDrawView();
157 const SdrMarkList& rMarkList = pView->GetMarkedObjectList();
159 if( rMarkList.GetMarkCount() == 1 )
161 SdrObject* pObj = rMarkList.GetMark( 0 )->GetMarkedSdrObj();
163 if( auto pGraphicObj = dynamic_cast<SdrGrafObj*>( pObj) )
164 if( pGraphicObj->GetGraphicType() == GraphicType::Bitmap )
166 GraphicObject aGraphicObject( pGraphicObj->GetGraphicObject() );
167 m_ExternalEdits.push_back( std::make_unique<SdrExternalToolEdit>(
168 pView, pGraphicObj));
169 m_ExternalEdits.back()->Edit( &aGraphicObject );
173 Invalidate();
176 void ScGraphicShell::GetCompressGraphicState( SfxItemSet& rSet )
178 ScDrawView* pView = GetViewData().GetScDrawView();
179 const SdrMarkList& rMarkList = pView->GetMarkedObjectList();
180 bool bEnable = false;
181 if( rMarkList.GetMarkCount() == 1 )
183 SdrObject* pObj = rMarkList.GetMark( 0 )->GetMarkedSdrObj();
185 if( auto pGraphicObj = dynamic_cast<const SdrGrafObj*>( pObj) )
186 if( pGraphicObj->GetGraphicType() == GraphicType::Bitmap )
187 bEnable = true;
190 if( !bEnable )
191 rSet.DisableItem( SID_COMPRESS_GRAPHIC );
194 void ScGraphicShell::ExecuteCompressGraphic( SAL_UNUSED_PARAMETER SfxRequest& )
196 ScDrawView* pView = GetViewData().GetScDrawView();
197 const SdrMarkList& rMarkList = pView->GetMarkedObjectList();
199 if( rMarkList.GetMarkCount() == 1 )
201 SdrObject* pObj = rMarkList.GetMark( 0 )->GetMarkedSdrObj();
203 if( auto pGraphicObj = dynamic_cast<SdrGrafObj*>( pObj) )
204 if( pGraphicObj->GetGraphicType() == GraphicType::Bitmap )
206 CompressGraphicsDialog dialog(GetViewData().GetDialogParent(), pGraphicObj, GetViewData().GetBindings());
207 if (dialog.run() == RET_OK)
209 rtl::Reference<SdrGrafObj> pNewObject = dialog.GetCompressedSdrGrafObj();
210 SdrPageView* pPageView = pView->GetSdrPageView();
211 OUString aUndoString = rMarkList.GetMarkDescription() + " Compress";
212 pView->BegUndo( aUndoString );
213 pView->ReplaceObjectAtView( pObj, *pPageView, pNewObject.get() );
214 pView->EndUndo();
219 Invalidate();
222 void ScGraphicShell::GetCropGraphicState( SfxItemSet& rSet )
224 ScDrawView* pView = GetViewData().GetScDrawView();
225 const SdrMarkList& rMarkList = pView->GetMarkedObjectList();
226 bool bEnable = false;
227 if( rMarkList.GetMarkCount() == 1 )
229 SdrObject* pObj = rMarkList.GetMark( 0 )->GetMarkedSdrObj();
231 if( auto pGraphicObj = dynamic_cast<const SdrGrafObj*>( pObj) )
232 if( pGraphicObj->GetGraphicType() == GraphicType::Bitmap )
233 bEnable = true;
236 if( !bEnable )
237 rSet.DisableItem( SID_OBJECT_CROP );
240 void ScGraphicShell::ExecuteCropGraphic( SAL_UNUSED_PARAMETER SfxRequest& )
242 ScDrawView* pView = GetViewData().GetScDrawView();
243 const SdrMarkList& rMarkList = pView->GetMarkedObjectList();
245 if( rMarkList.GetMarkCount() == 1 )
247 SdrObject* pObj = rMarkList.GetMark( 0 )->GetMarkedSdrObj();
249 if( auto pGraphicObj = dynamic_cast<const SdrGrafObj*>( pObj) )
250 if( pGraphicObj->GetGraphicType() == GraphicType::Bitmap )
252 pView->SetEditMode(SdrViewEditMode::Edit);
253 pView->SetDragMode(SdrDragMode::Crop);
257 Invalidate();
260 void ScGraphicShell::ExecuteSaveGraphic( SAL_UNUSED_PARAMETER SfxRequest& /*rReq*/)
262 ScDrawView* pView = GetViewData().GetScDrawView();
263 const SdrMarkList& rMarkList = pView->GetMarkedObjectList();
264 if( rMarkList.GetMarkCount() == 1 )
266 const SdrGrafObj* pObj = dynamic_cast<const SdrGrafObj*>(rMarkList.GetMark( 0 )->GetMarkedSdrObj());
267 if( pObj && pObj->GetGraphicType() == GraphicType::Bitmap )
269 GraphicAttr aGraphicAttr = pObj->GetGraphicAttr();
270 short nState = RET_CANCEL;
271 vcl::Window* pWin = GetViewData().GetActiveWin();
272 weld::Window* pWinFrame = pWin ? pWin->GetFrameWeld() : nullptr;
273 if (aGraphicAttr != GraphicAttr()) // the image has been modified
275 if (pWin)
277 nState = GraphicHelper::HasToSaveTransformedImage(pWinFrame);
280 else
282 nState = RET_NO;
285 if (nState == RET_YES)
287 GraphicHelper::ExportGraphic(pWinFrame, pObj->GetTransformedGraphic(), u""_ustr);
289 else if (nState == RET_NO)
291 const GraphicObject& aGraphicObject(pObj->GetGraphicObject());
292 GraphicHelper::ExportGraphic(pWinFrame, aGraphicObject.GetGraphic(), u""_ustr);
297 Invalidate();
300 void ScGraphicShell::GetSaveGraphicState(SfxItemSet &rSet)
302 ScDrawView* pView = GetViewData().GetScDrawView();
303 const SdrMarkList& rMarkList = pView->GetMarkedObjectList();
304 bool bEnable = false;
305 if( rMarkList.GetMarkCount() == 1 )
307 SdrObject* pObj = rMarkList.GetMark( 0 )->GetMarkedSdrObj();
309 if( auto pGraphicObj = dynamic_cast<const SdrGrafObj*>( pObj) )
310 if( pGraphicObj->GetGraphicType() == GraphicType::Bitmap )
311 bEnable = true;
314 if (GetObjectShell()->isExportLocked())
315 bEnable = false;
317 if( !bEnable )
318 rSet.DisableItem( SID_SAVE_GRAPHIC );
321 void ScGraphicShell::ExecuteChangePicture( SAL_UNUSED_PARAMETER SfxRequest& /*rReq*/)
323 ScDrawView* pView = GetViewData().GetScDrawView();
324 const SdrMarkList& rMarkList = pView->GetMarkedObjectList();
326 if( rMarkList.GetMarkCount() == 1 )
328 SdrObject* pObj = rMarkList.GetMark( 0 )->GetMarkedSdrObj();
330 if( auto pGraphicObj = dynamic_cast<SdrGrafObj*>( pObj) )
331 if( pGraphicObj->GetGraphicType() == GraphicType::Bitmap )
333 vcl::Window* pWin = GetViewData().GetActiveWin();
334 SvxOpenGraphicDialog aDlg(ScResId(STR_INSERTGRAPHIC), pWin ? pWin->GetFrameWeld() : nullptr);
336 if( aDlg.Execute() == ERRCODE_NONE )
338 Graphic aGraphic;
339 ErrCode nError = aDlg.GetGraphic(aGraphic);
340 if( nError == ERRCODE_NONE )
342 rtl::Reference<SdrGrafObj> pNewObject(SdrObject::Clone(*pGraphicObj, pGraphicObj->getSdrModelFromSdrObject()));
343 pNewObject->SetGraphic( aGraphic );
344 SdrPageView* pPageView = pView->GetSdrPageView();
345 OUString aUndoString = rMarkList.GetMarkDescription() + " Change";
346 pView->BegUndo( aUndoString );
347 pView->ReplaceObjectAtView( pObj, *pPageView, pNewObject.get() );
348 pView->EndUndo();
354 Invalidate();
357 void ScGraphicShell::GetChangePictureState(SfxItemSet &rSet)
359 ScDrawView* pView = GetViewData().GetScDrawView();
360 const SdrMarkList& rMarkList = pView->GetMarkedObjectList();
361 bool bEnable = false;
362 if( rMarkList.GetMarkCount() == 1 )
364 SdrObject* pObj = rMarkList.GetMark( 0 )->GetMarkedSdrObj();
366 if( auto pGrafObj = dynamic_cast<const SdrGrafObj*>( pObj) )
367 if( pGrafObj->GetGraphicType() == GraphicType::Bitmap )
368 bEnable = true;
371 if( !bEnable )
372 rSet.DisableItem( SID_CHANGE_PICTURE );
376 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */