Bump version to 6.4-15
[LibreOffice.git] / svx / source / table / viewcontactoftableobj.cxx
blob10a6173fab56d988883a2298db09a263df6f36f4
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 "viewcontactoftableobj.hxx"
22 #include <svx/svdotable.hxx>
23 #include <com/sun/star/table/XTable.hpp>
24 #include <basegfx/polygon/b2dpolygontools.hxx>
25 #include <basegfx/polygon/b2dpolygon.hxx>
26 #include <drawinglayer/primitive2d/polygonprimitive2d.hxx>
27 #include <svx/sdr/primitive2d/sdrattributecreator.hxx>
28 #include <drawinglayer/primitive2d/groupprimitive2d.hxx>
29 #include <svx/sdr/primitive2d/sdrdecompositiontools.hxx>
30 #include <basegfx/matrix/b2dhommatrix.hxx>
31 #include <svx/sdr/attribute/sdrtextattribute.hxx>
32 #include <svx/sdr/primitive2d/svx_primitivetypes2d.hxx>
33 #include <editeng/borderline.hxx>
34 #include <drawinglayer/primitive2d/borderlineprimitive2d.hxx>
35 #include <sdr/attribute/sdrfilltextattribute.hxx>
36 #include <drawinglayer/attribute/sdrlineattribute.hxx>
37 #include <drawinglayer/attribute/sdrshadowattribute.hxx>
38 #include <drawinglayer/primitive2d/sdrdecompositiontools2d.hxx>
39 #include <drawinglayer/primitive2d/transformprimitive2d.hxx>
40 #include <basegfx/matrix/b2dhommatrixtools.hxx>
41 #include <svx/framelink.hxx>
42 #include <svx/framelinkarray.hxx>
43 #include <vcl/canvastools.hxx>
45 #include <cell.hxx>
46 #include "tablelayouter.hxx"
49 using editeng::SvxBorderLine;
50 using namespace com::sun::star;
53 namespace drawinglayer
55 namespace primitive2d
57 class SdrCellPrimitive2D : public BufferedDecompositionPrimitive2D
59 private:
60 basegfx::B2DHomMatrix const maTransform;
61 attribute::SdrFillTextAttribute const maSdrFTAttribute;
63 protected:
64 // local decomposition.
65 virtual void create2DDecomposition(Primitive2DContainer& rContainer, const geometry::ViewInformation2D& aViewInformation) const override;
67 public:
68 SdrCellPrimitive2D(
69 const basegfx::B2DHomMatrix& rTransform,
70 const attribute::SdrFillTextAttribute& rSdrFTAttribute)
71 : BufferedDecompositionPrimitive2D(),
72 maTransform(rTransform),
73 maSdrFTAttribute(rSdrFTAttribute)
77 // data access
78 const basegfx::B2DHomMatrix& getTransform() const { return maTransform; }
79 const attribute::SdrFillTextAttribute& getSdrFTAttribute() const { return maSdrFTAttribute; }
81 // compare operator
82 virtual bool operator==(const BasePrimitive2D& rPrimitive) const override;
84 // provide unique ID
85 DeclPrimitive2DIDBlock()
88 void SdrCellPrimitive2D::create2DDecomposition(Primitive2DContainer& rContainer, const geometry::ViewInformation2D& /*aViewInformation*/) const
90 // prepare unit polygon
91 const basegfx::B2DPolyPolygon aUnitPolyPolygon(basegfx::utils::createUnitPolygon());
93 // add fill
94 if(!getSdrFTAttribute().getFill().isDefault())
96 basegfx::B2DPolyPolygon aTransformed(aUnitPolyPolygon);
98 aTransformed.transform(getTransform());
99 rContainer.push_back(
100 createPolyPolygonFillPrimitive(
101 aTransformed,
102 getSdrFTAttribute().getFill(),
103 getSdrFTAttribute().getFillFloatTransGradient()));
105 else
107 // if no fill create one for HitTest and BoundRect fallback
108 rContainer.push_back(
109 createHiddenGeometryPrimitives2D(
110 true,
111 aUnitPolyPolygon,
112 getTransform()));
115 // add text
116 if(!getSdrFTAttribute().getText().isDefault())
118 rContainer.push_back(
119 createTextPrimitive(
120 aUnitPolyPolygon,
121 getTransform(),
122 getSdrFTAttribute().getText(),
123 attribute::SdrLineAttribute(),
124 true,
125 false));
129 bool SdrCellPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
131 if(BufferedDecompositionPrimitive2D::operator==(rPrimitive))
133 const SdrCellPrimitive2D& rCompare = static_cast<const SdrCellPrimitive2D&>(rPrimitive);
135 return (getTransform() == rCompare.getTransform()
136 && getSdrFTAttribute() == rCompare.getSdrFTAttribute());
139 return false;
142 // provide unique ID
143 ImplPrimitive2DIDBlock(SdrCellPrimitive2D, PRIMITIVE2D_ID_SDRCELLPRIMITIVE2D)
145 } // end of namespace primitive2d
146 } // end of namespace drawinglayer
148 namespace sdr
150 namespace contact
152 static svx::frame::Style impGetLineStyle(
153 const sdr::table::TableLayouter& rLayouter,
154 sal_Int32 nX,
155 sal_Int32 nY,
156 bool bHorizontal,
157 sal_Int32 nColCount,
158 sal_Int32 nRowCount,
159 bool bIsRTL)
161 if(nX >= 0 && nX <= nColCount && nY >= 0 && nY <= nRowCount)
163 const SvxBorderLine* pLine = rLayouter.getBorderLine(nX, nY, bHorizontal);
165 if(pLine)
167 // copy line content
168 SvxBorderLine aLine(*pLine);
170 // check for mirroring. This shall always be done when it is
171 // not a top- or rightmost line
172 bool bMirror(aLine.isDouble());
174 if(bMirror)
176 if(bHorizontal)
178 // mirror all bottom lines
179 bMirror = (0 != nY);
181 else
183 // mirror all left lines
184 bMirror = (bIsRTL ? 0 != nX : nX != nColCount);
188 if(bMirror)
190 aLine.SetMirrorWidths( );
193 const double fTwipsToMM(127.0 / 72.0);
194 return svx::frame::Style(&aLine, fTwipsToMM);
198 // no success, copy empty line
199 return svx::frame::Style();
202 drawinglayer::primitive2d::Primitive2DContainer ViewContactOfTableObj::createViewIndependentPrimitive2DSequence() const
204 const sdr::table::SdrTableObj& rTableObj = static_cast<const sdr::table::SdrTableObj&>(GetSdrObject());
205 const uno::Reference< css::table::XTable > xTable = rTableObj.getTable();
207 if(xTable.is())
209 // create primitive representation for table. Cell info goes
210 // directly to aRetval, Border info to aBorderSequence and added
211 // later to get the correct overlapping
212 drawinglayer::primitive2d::Primitive2DContainer aRetval;
213 const sal_Int32 nRowCount(xTable->getRowCount());
214 const sal_Int32 nColCount(xTable->getColumnCount());
215 const sal_Int32 nAllCount(nRowCount * nColCount);
217 if(nAllCount)
219 const sdr::table::TableLayouter& rTableLayouter(rTableObj.getTableLayouter());
220 const bool bIsRTL(css::text::WritingMode_RL_TB == rTableObj.GetWritingMode());
221 sdr::table::CellPos aCellPos;
222 sdr::table::CellRef xCurrentCell;
223 basegfx::B2IRectangle aCellArea;
225 // create range using the model data directly. This is in SdrTextObj::aRect which i will access using
226 // GetGeoRect() to not trigger any calculations. It's the unrotated geometry.
227 const basegfx::B2DRange aObjectRange = vcl::unotools::b2DRectangleFromRectangle(rTableObj.GetGeoRect());
229 // To create the CellBorderPrimitives, use the tolling from svx::frame::Array
230 // which is capable of creating the needed visualization. Fill it during the
231 // anyways needed run over the table.
232 svx::frame::Array aArray;
234 // initialize CellBorderArray for primitive creation
235 aArray.Initialize(nColCount, nRowCount);
237 // create single primitives per cell
238 for(aCellPos.mnRow = 0; aCellPos.mnRow < nRowCount; aCellPos.mnRow++)
240 // add RowHeight to CellBorderArray for primitive creation
241 aArray.SetRowHeight(aCellPos.mnRow, rTableLayouter.getRowHeight(aCellPos.mnRow));
243 for(aCellPos.mnCol = 0; aCellPos.mnCol < nColCount; aCellPos.mnCol++)
245 // add ColWidth to CellBorderArray for primitive creation, only
246 // needs to be done in the 1st run
247 if(0 == aCellPos.mnRow)
249 aArray.SetColWidth(aCellPos.mnCol, rTableLayouter.getColumnWidth(aCellPos.mnCol));
252 // access the cell
253 xCurrentCell.set(dynamic_cast< sdr::table::Cell* >(xTable->getCellByPosition(aCellPos.mnCol, aCellPos.mnRow).get()));
255 if(xCurrentCell.is())
257 // copy styles for current cell to CellBorderArray for primitive creation
258 aArray.SetCellStyleLeft(aCellPos.mnCol, aCellPos.mnRow, impGetLineStyle(rTableLayouter, aCellPos.mnCol, aCellPos.mnRow, false, nColCount, nRowCount, bIsRTL));
259 aArray.SetCellStyleRight(aCellPos.mnCol, aCellPos.mnRow, impGetLineStyle(rTableLayouter, aCellPos.mnCol + 1, aCellPos.mnRow, false, nColCount, nRowCount, bIsRTL));
260 aArray.SetCellStyleTop(aCellPos.mnCol, aCellPos.mnRow, impGetLineStyle(rTableLayouter, aCellPos.mnCol, aCellPos.mnRow, true, nColCount, nRowCount, bIsRTL));
261 aArray.SetCellStyleBottom(aCellPos.mnCol, aCellPos.mnRow, impGetLineStyle(rTableLayouter, aCellPos.mnCol, aCellPos.mnRow + 1, true, nColCount, nRowCount, bIsRTL));
263 // ignore merged cells (all except the top-left of a merged cell)
264 if(!xCurrentCell->isMerged())
266 // check if we are the top-left of a merged cell
267 const sal_Int32 nXSpan(xCurrentCell->getColumnSpan());
268 const sal_Int32 nYSpan(xCurrentCell->getRowSpan());
270 if(nXSpan > 1 || nYSpan > 1)
272 // if merged, set so at CellBorderArray for primitive creation
273 aArray.SetMergedRange(aCellPos.mnCol, aCellPos.mnRow, aCellPos.mnCol + nXSpan - 1, aCellPos.mnRow + nYSpan - 1);
278 if(xCurrentCell.is() && !xCurrentCell->isMerged())
280 if(rTableLayouter.getCellArea(xCurrentCell, aCellPos, aCellArea))
282 // create cell transformation matrix
283 basegfx::B2DHomMatrix aCellMatrix;
284 aCellMatrix.set(0, 0, static_cast<double>(aCellArea.getWidth()));
285 aCellMatrix.set(1, 1, static_cast<double>(aCellArea.getHeight()));
286 aCellMatrix.set(0, 2, static_cast<double>(aCellArea.getMinX()) + aObjectRange.getMinX());
287 aCellMatrix.set(1, 2, static_cast<double>(aCellArea.getMinY()) + aObjectRange.getMinY());
289 // handle cell fillings and text
290 const SfxItemSet& rCellItemSet = xCurrentCell->GetItemSet();
291 const sal_uInt32 nTextIndex(nColCount * aCellPos.mnRow + aCellPos.mnCol);
292 const SdrText* pSdrText = rTableObj.getText(nTextIndex);
293 drawinglayer::attribute::SdrFillTextAttribute aAttribute;
295 if(pSdrText)
297 // #i101508# take cell's local text frame distances into account
298 const sal_Int32 nLeft(xCurrentCell->GetTextLeftDistance());
299 const sal_Int32 nRight(xCurrentCell->GetTextRightDistance());
300 const sal_Int32 nUpper(xCurrentCell->GetTextUpperDistance());
301 const sal_Int32 nLower(xCurrentCell->GetTextLowerDistance());
303 aAttribute = drawinglayer::primitive2d::createNewSdrFillTextAttribute(
304 rCellItemSet,
305 pSdrText,
306 &nLeft,
307 &nUpper,
308 &nRight,
309 &nLower);
311 else
313 aAttribute = drawinglayer::primitive2d::createNewSdrFillTextAttribute(
314 rCellItemSet,
315 pSdrText);
318 // always create cell primitives for BoundRect and HitTest
320 const drawinglayer::primitive2d::Primitive2DReference xCellReference(
321 new drawinglayer::primitive2d::SdrCellPrimitive2D(
322 aCellMatrix, aAttribute));
323 aRetval.append(xCellReference);
330 // now create all CellBorderPrimitives
331 const drawinglayer::primitive2d::Primitive2DContainer aCellBorderPrimitives(aArray.CreateB2DPrimitiveArray());
333 if(!aCellBorderPrimitives.empty())
335 // this is already scaled (due to Table in non-uniform coordinates), so
336 // first transform removing scale
337 basegfx::B2DHomMatrix aTransform(
338 basegfx::utils::createScaleB2DHomMatrix(
339 1.0 / aObjectRange.getWidth(),
340 1.0 / aObjectRange.getHeight()));
342 // If RTL, mirror the whole unified table in X and move right.
343 // This is much easier than taking this into account for the whole
344 // index calculations
345 if(bIsRTL)
347 aTransform.scale(-1.0, 1.0);
348 aTransform.translate(1.0, 0.0);
351 // create object matrix
352 const GeoStat& rGeoStat(rTableObj.GetGeoStat());
353 const double fShearX(rGeoStat.nShearAngle ? tan((36000 - rGeoStat.nShearAngle) * F_PI18000) : 0.0);
354 const double fRotate(rGeoStat.nRotationAngle ? (36000 - rGeoStat.nRotationAngle) * F_PI18000 : 0.0);
355 const basegfx::B2DHomMatrix aObjectMatrix(basegfx::utils::createScaleShearXRotateTranslateB2DHomMatrix(
356 aObjectRange.getWidth(), aObjectRange.getHeight(), fShearX, fRotate,
357 aObjectRange.getMinX(), aObjectRange.getMinY()));
359 // add object matrix to transform. By doing so theoretically
360 // CellBorders could be also rotated/sheared for the first time ever.
361 // To completely make that work, the primitives already created in
362 // aRetval would also have to be based on ObjectMatrix, not only on
363 // ObjectRange as it currently is.
364 aTransform *= aObjectMatrix;
366 // create a transform primitive with this and embed CellBorders
367 // and append to retval
368 aRetval.append(
369 new drawinglayer::primitive2d::TransformPrimitive2D(
370 aTransform,
371 aCellBorderPrimitives));
375 if(!aRetval.empty())
377 // check and create evtl. shadow for created content
378 const SfxItemSet& rObjectItemSet = rTableObj.GetMergedItemSet();
379 const drawinglayer::attribute::SdrShadowAttribute aNewShadowAttribute(
380 drawinglayer::primitive2d::createNewSdrShadowAttribute(rObjectItemSet));
382 if(!aNewShadowAttribute.isDefault())
384 aRetval = drawinglayer::primitive2d::createEmbeddedShadowPrimitive(aRetval, aNewShadowAttribute);
388 return aRetval;
390 else
392 // take unrotated snap rect (direct model data) for position and size
393 const basegfx::B2DRange aObjectRange = vcl::unotools::b2DRectangleFromRectangle(rTableObj.GetGeoRect());
395 // create object matrix
396 const GeoStat& rGeoStat(rTableObj.GetGeoStat());
397 const double fShearX(rGeoStat.nShearAngle ? tan((36000 - rGeoStat.nShearAngle) * F_PI18000) : 0.0);
398 const double fRotate(rGeoStat.nRotationAngle ? (36000 - rGeoStat.nRotationAngle) * F_PI18000 : 0.0);
399 const basegfx::B2DHomMatrix aObjectMatrix(basegfx::utils::createScaleShearXRotateTranslateB2DHomMatrix(
400 aObjectRange.getWidth(), aObjectRange.getHeight(), fShearX, fRotate,
401 aObjectRange.getMinX(), aObjectRange.getMinY()));
403 // created an invisible outline for the cases where no visible content exists
404 const drawinglayer::primitive2d::Primitive2DReference xReference(
405 drawinglayer::primitive2d::createHiddenGeometryPrimitives2D(
406 aObjectMatrix));
408 return drawinglayer::primitive2d::Primitive2DContainer { xReference };
412 ViewContactOfTableObj::ViewContactOfTableObj(sdr::table::SdrTableObj& rTableObj)
413 : ViewContactOfSdrObj(rTableObj)
417 ViewContactOfTableObj::~ViewContactOfTableObj()
420 } // end of namespace contact
421 } // end of namespace sdr
423 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */