Debian Codegraph @

chart2/source/controller/main/ChartTransferable.cxx

raw
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 <sal/config.h>
21
22 #include <memory>
23
24 #include "ChartTransferable.hxx"
25
26 #include <sot/exchange.hxx>
27 #include <unotools/streamwrap.hxx>
28 #include <vcl/graph.hxx>
29 #include <svl/itempool.hxx>
30 #include <editeng/eeitem.hxx>
31 #include <editeng/fhgtitem.hxx>
32 #include <svx/svditer.hxx>
33 #include <svx/svdmodel.hxx>
34 #include <svx/unomodel.hxx>
35 #include <svx/svdview.hxx>
36 #include <osl/diagnose.h>
37
38 constexpr sal_uInt32 CHARTTRANSFER_OBJECTTYPE_DRAWMODEL = 1;
39
40 using namespace ::com::sun::star;
41
42 using ::com::sun::star::uno::Reference;
43
44 namespace chart
45 {
46
47 ChartTransferable::ChartTransferable(
48 SdrModel& rSdrModel,
49 SdrObject* pSelectedObj,
50 bool bDrawing)
51 : m_bDrawing(bDrawing)
52 {
53 SdrView aExchgView( rSdrModel );
54 SdrPageView* pPv = aExchgView.ShowSdrPage( rSdrModel.GetPage( 0 ));
55 if( pSelectedObj )
56 aExchgView.MarkObj( pSelectedObj, pPv );
57 else
58 aExchgView.MarkAllObj( pPv );
59 Graphic aGraphic( aExchgView.GetMarkedObjMetaFile(true));
60 m_xMetaFileGraphic.set( aGraphic.GetXGraphic());
61 if ( m_bDrawing )
62 {
63 m_xMarkedObjModel = aExchgView.CreateMarkedObjModel();
64 }
65 }
66
67 ChartTransferable::~ChartTransferable()
68 {}
69
70 void ChartTransferable::AddSupportedFormats()
71 {
72 if ( m_bDrawing )
73 {
74 AddFormat( SotClipboardFormatId::DRAWING );
75 }
76 AddFormat( SotClipboardFormatId::GDIMETAFILE );
77 AddFormat( SotClipboardFormatId::PNG );
78 AddFormat( SotClipboardFormatId::BITMAP );
79 }
80
81 bool ChartTransferable::GetData( const css::datatransfer::DataFlavor& rFlavor, const OUString& /*rDestDoc*/ )
82 {
83 SotClipboardFormatId nFormat = SotExchange::GetFormat( rFlavor );
84 bool bResult = false;
85
86 if( HasFormat( nFormat ))
87 {
88 if ( nFormat == SotClipboardFormatId::DRAWING )
89 {
90 bResult = SetObject(m_xMarkedObjModel.get(), CHARTTRANSFER_OBJECTTYPE_DRAWMODEL, rFlavor);
91 }
92 else if ( nFormat == SotClipboardFormatId::GDIMETAFILE )
93 {
94 Graphic aGraphic( m_xMetaFileGraphic );
95 bResult = SetGDIMetaFile( aGraphic.GetGDIMetaFile() );
96 }
97 else if( nFormat == SotClipboardFormatId::BITMAP )
98 {
99 Graphic aGraphic( m_xMetaFileGraphic );
100 bResult = SetBitmap(aGraphic.GetBitmap(), rFlavor);
101 }
102 }
103
104 return bResult;
105 }
106
107 bool ChartTransferable::WriteObject( SvStream& rOStm, void* pUserObject, sal_uInt32 nUserObjectId,
108 const datatransfer::DataFlavor& /* rFlavor */ )
109 {
110 // called from SetObject, put data into stream
111
112 bool bRet = false;
113 switch ( nUserObjectId )
114 {
115 case CHARTTRANSFER_OBJECTTYPE_DRAWMODEL:
116 {
117 SdrModel* pMarkedObjModel = static_cast< SdrModel* >( pUserObject );
118 if ( pMarkedObjModel )
119 {
120 rOStm.SetBufferSize( 0xff00 );
121
122 // for the changed pool defaults from drawing layer pool set those
123 // attributes as hard attributes to preserve them for saving
124 const SfxItemPool& rItemPool = pMarkedObjModel->GetItemPool();
125 const SvxFontHeightItem& rDefaultFontHeight = rItemPool.GetUserOrPoolDefaultItem( EE_CHAR_FONTHEIGHT );
126 sal_uInt16 nCount = pMarkedObjModel->GetPageCount();
127 for ( sal_uInt16 i = 0; i < nCount; ++i )
128 {
129 const SdrPage* pPage = pMarkedObjModel->GetPage( i );
130 SdrObjListIter aIter( pPage, SdrIterMode::DeepNoGroups );
131 while ( aIter.IsMore() )
132 {
133 SdrObject* pObj = aIter.Next();
134 const SvxFontHeightItem& rItem = pObj->GetMergedItem( EE_CHAR_FONTHEIGHT );
135 if ( rItem.GetHeight() == rDefaultFontHeight.GetHeight() )
136 {
137 pObj->SetMergedItem( rDefaultFontHeight );
138 }
139 }
140 }
141
142 Reference< io::XOutputStream > xDocOut( new utl::OOutputStreamWrapper( rOStm ) );
143 SvxDrawingLayerExport( pMarkedObjModel, xDocOut );
144
145 bRet = ( rOStm.GetError() == ERRCODE_NONE );
146 }
147 }
148 break;
149 default:
150 {
151 OSL_FAIL( "ChartTransferable::WriteObject: unknown object id" );
152 }
153 break;
154 }
155 return bRet;
156 }
157
158 } // namespace chart
159
160 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */