tdf#144694 In direct SQL dialog, activate options 'Run SQL command
[LibreOffice.git] / svx / source / table / tablertfexporter.cxx
blobadc97e6c754fc113c92a35ae7aece32d3db60736
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 .
21 #include <vector>
23 #include <com/sun/star/table/XTable.hpp>
24 #include <com/sun/star/beans/XPropertySet.hpp>
26 #include <comphelper/diagnose_ex.hxx>
27 #include <tools/stream.hxx>
28 #include <svtools/rtfkeywd.hxx>
29 #include <svtools/rtfout.hxx>
31 #include <editeng/eeitem.hxx>
32 #include <svx/sdtaitm.hxx>
33 #include <editeng/wghtitem.hxx>
34 #include <editeng/postitem.hxx>
35 #include <editeng/udlnitem.hxx>
37 #include <cell.hxx>
38 #include <svx/svdotable.hxx>
39 #include <svx/svdoutl.hxx>
40 #include <editeng/editeng.hxx>
41 #include <editeng/outlobj.hxx>
44 using namespace ::com::sun::star::uno;
45 using namespace ::com::sun::star::table;
46 using namespace ::com::sun::star::container;
47 using namespace ::com::sun::star::beans;
49 namespace sdr::table {
51 class SdrTableRtfExporter
53 public:
54 SdrTableRtfExporter( SvStream& rStrmP, SdrTableObj& rObj );
55 void Write();
56 void WriteRow( const Reference< XPropertySet >& xRowSet, sal_Int32 nRow, const std::vector< sal_Int32 >& aColumnStart );
57 void WriteCell( sal_Int32 nCol, sal_Int32 nRow );
59 private:
60 SvStream& mrStrm;
61 SdrTableObj& mrObj;
62 rtl::Reference< TableModel > mxTable;
65 void ExportAsRTF( SvStream& rStrm, SdrTableObj& rObj )
67 SdrTableRtfExporter aEx( rStrm, rObj );
68 aEx.Write();
71 constexpr OUString gsSize( u"Size"_ustr );
73 SdrTableRtfExporter::SdrTableRtfExporter( SvStream& rStrm, SdrTableObj& rObj )
74 : mrStrm( rStrm )
75 , mrObj( rObj )
76 , mxTable( rObj.getUnoTable() )
80 void SdrTableRtfExporter::Write()
82 mrStrm.WriteChar( '{' ).WriteOString( OOO_STRING_SVTOOLS_RTF_RTF );
83 mrStrm.WriteOString( OOO_STRING_SVTOOLS_RTF_ANSI ).WriteOString( SAL_NEWLINE_STRING );
85 Reference< XTableColumns > xColumns( mxTable->getColumns() );
86 const sal_Int32 nColCount = xColumns->getCount();
88 std::vector< sal_Int32 > aColumnStart;
89 aColumnStart.reserve( nColCount );
91 // determine right offset of cells
92 sal_Int32 nPos = 0;
93 for( sal_Int32 nCol = 0; nCol < nColCount; nCol++ ) try
95 Reference< XPropertySet > xSet( xColumns->getByIndex(nCol), UNO_QUERY_THROW );
96 sal_Int32 nWidth = 0;
97 xSet->getPropertyValue( gsSize ) >>= nWidth;
98 nPos += o3tl::toTwips(nWidth, o3tl::Length::mm100);
99 aColumnStart.push_back( nPos );
101 catch( Exception& )
103 TOOLS_WARN_EXCEPTION("svx", "");
106 // export rows
107 Reference< XTableRows > xRows( mxTable->getRows() );
108 const sal_Int32 nRowCount = xRows->getCount();
110 for( sal_Int32 nRow = 0; nRow < nRowCount; nRow++ ) try
112 Reference< XPropertySet > xRowSet( xRows->getByIndex(nRow), UNO_QUERY_THROW );
113 WriteRow( xRowSet, nRow, aColumnStart );
115 catch( Exception& )
117 TOOLS_WARN_EXCEPTION("svx", "");
120 mrStrm.WriteChar( '}' ).WriteOString( SAL_NEWLINE_STRING );
123 void SdrTableRtfExporter::WriteRow( const Reference< XPropertySet >& xRowSet, sal_Int32 nRow, const std::vector< sal_Int32 >& aColumnStart )
125 sal_Int32 nRowHeight = 0;
126 xRowSet->getPropertyValue( gsSize ) >>= nRowHeight;
128 mrStrm.WriteOString( OOO_STRING_SVTOOLS_RTF_TROWD ).WriteOString( OOO_STRING_SVTOOLS_RTF_TRGAPH ).WriteOString( "30" ).WriteOString( OOO_STRING_SVTOOLS_RTF_TRLEFT ).WriteOString( "-30" );
129 mrStrm.WriteOString( OOO_STRING_SVTOOLS_RTF_TRRH ).WriteOString( OString::number(nRowHeight) );
131 const sal_Int32 nColCount = mxTable->getColumnCount();
132 for( sal_Int32 nCol = 0; nCol < nColCount; nCol++ )
134 CellRef xCell( mxTable->getCell( nCol, nRow ) );
136 if( !xCell.is() )
137 continue;
139 mrStrm.WriteOString( OOO_STRING_SVTOOLS_RTF_CELLX ).WriteOString( OString::number(aColumnStart[nCol]) );
140 if ( (nCol & 0x0F) == 0x0F )
141 mrStrm.WriteOString( SAL_NEWLINE_STRING ); // prevent long lines
143 mrStrm.WriteOString( OOO_STRING_SVTOOLS_RTF_PARD ).WriteOString( OOO_STRING_SVTOOLS_RTF_PLAIN ).WriteOString( OOO_STRING_SVTOOLS_RTF_INTBL ).WriteOString( SAL_NEWLINE_STRING );
145 sal_uInt64 nStrmPos = mrStrm.Tell();
146 for( sal_Int32 nCol = 0; nCol < nColCount; nCol++ )
148 WriteCell( nCol, nRow );
149 if ( mrStrm.Tell() - nStrmPos > 255 )
151 mrStrm.WriteOString( SAL_NEWLINE_STRING );
152 nStrmPos = mrStrm.Tell();
155 mrStrm.WriteOString( OOO_STRING_SVTOOLS_RTF_ROW ).WriteOString( SAL_NEWLINE_STRING );
159 void SdrTableRtfExporter::WriteCell( sal_Int32 nCol, sal_Int32 nRow )
161 CellRef xCell( mxTable->getCell( nCol, nRow ) );
163 if( !xCell.is() || xCell->isMerged() )
165 mrStrm.WriteOString( OOO_STRING_SVTOOLS_RTF_CELL );
166 return ;
169 OUString aContent;
171 std::optional<OutlinerParaObject> pParaObj = xCell->CreateEditOutlinerParaObject();
173 if( !pParaObj && xCell->GetOutlinerParaObject() )
174 pParaObj = *xCell->GetOutlinerParaObject();
176 if(pParaObj)
178 // handle outliner attributes
179 SdrOutliner& rOutliner = mrObj.ImpGetDrawOutliner();
180 rOutliner.SetText(*pParaObj);
182 aContent = rOutliner.GetEditEngine().GetText();
184 rOutliner.Clear();
187 bool bResetAttr = false;
189 SdrTextHorzAdjust eHAdj = xCell->GetTextHorizontalAdjust();
191 const SfxItemSet& rCellSet = xCell->GetItemSet();
193 const SvxWeightItem& rWeightItem = rCellSet.Get( EE_CHAR_WEIGHT );
194 const SvxPostureItem& rPostureItem = rCellSet.Get( EE_CHAR_ITALIC );
195 const SvxUnderlineItem& rUnderlineItem = rCellSet.Get( EE_CHAR_UNDERLINE );
197 const char* pChar;
199 switch( eHAdj )
201 case SDRTEXTHORZADJUST_CENTER: pChar = OOO_STRING_SVTOOLS_RTF_QC; break;
202 case SDRTEXTHORZADJUST_BLOCK: pChar = OOO_STRING_SVTOOLS_RTF_QJ; break;
203 case SDRTEXTHORZADJUST_RIGHT: pChar = OOO_STRING_SVTOOLS_RTF_QR; break;
204 case SDRTEXTHORZADJUST_LEFT:
205 default: pChar = OOO_STRING_SVTOOLS_RTF_QL; break;
207 mrStrm.WriteOString( pChar );
209 if ( rWeightItem.GetWeight() >= WEIGHT_BOLD )
210 { // bold
211 bResetAttr = true;
212 mrStrm.WriteOString( OOO_STRING_SVTOOLS_RTF_B );
214 if ( rPostureItem.GetPosture() != ITALIC_NONE )
215 { // italic
216 bResetAttr = true;
217 mrStrm.WriteOString( OOO_STRING_SVTOOLS_RTF_I );
219 if ( rUnderlineItem.GetLineStyle() != LINESTYLE_NONE )
220 { // underline
221 bResetAttr = true;
222 mrStrm.WriteOString( OOO_STRING_SVTOOLS_RTF_UL );
225 mrStrm.WriteChar( ' ' );
226 RTFOutFuncs::Out_String( mrStrm, aContent );
227 mrStrm.WriteOString( OOO_STRING_SVTOOLS_RTF_CELL );
229 if ( bResetAttr )
230 mrStrm.WriteOString( OOO_STRING_SVTOOLS_RTF_PLAIN );
235 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */