summaryrefslogtreecommitdiff
path: root/sc/source/filter/xml/XMLTableShapeResizer.cxx
blob: 415057c219f2350153abebdfa11d6baf13e1fd16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_sc.hxx"
#include "XMLTableShapeResizer.hxx"
#include "unonames.hxx"
#include "document.hxx"
#include "xmlimprt.hxx"
#include "chartlis.hxx"
#include "XMLConverter.hxx"
#include "rangeutl.hxx"
#include "reftokenhelper.hxx"
#include <tools/debug.hxx>
#include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
#include <com/sun/star/table/XColumnRowRange.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>

#include <memory>
#include <vector>

using namespace ::com::sun::star;
using ::std::auto_ptr;
using ::std::vector;
using ::rtl::OUString;

ScMyShapeResizer::ScMyShapeResizer(ScXMLImport& rTempImport)
    : rImport(rTempImport),
    aShapes(),
    pCollection(NULL)
{
}

ScMyShapeResizer::~ScMyShapeResizer()
{
}

sal_Bool ScMyShapeResizer::IsOLE(uno::Reference< drawing::XShape >& rShape) const
{
    return rShape->getShapeType().equals(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.drawing.OLE2Shape")));
}

void ScMyShapeResizer::CreateChartListener(ScDocument* pDoc,
    const rtl::OUString& rName,
    const rtl::OUString* pRangeList)
{
    if (!pDoc || !pRangeList)
        // These are minimum required.
        return;

    if (!pRangeList->getLength())
    {
        pDoc->AddOLEObjectToCollection(rName);
        return;
    }

    OUString aRangeStr;
    ScRangeStringConverter::GetStringFromXMLRangeString(aRangeStr, *pRangeList, pDoc);
    if (!aRangeStr.getLength())
    {
        pDoc->AddOLEObjectToCollection(rName);
        return;
    }

    if (!pCollection)
        pCollection = pDoc->GetChartListenerCollection();

    if (!pCollection)
        return;

    auto_ptr< vector<ScSharedTokenRef> > pRefTokens(new vector<ScSharedTokenRef>);
    ScRefTokenHelper::compileRangeRepresentation(*pRefTokens, aRangeStr, pDoc);
    if (!pRefTokens->empty())
    {
        ScChartListener* pCL(new ScChartListener(rName, pDoc, pRefTokens.release()));

        //for loading binary files e.g.
        //if we have the flat filter we need to set the dirty flag thus the visible charts get repainted
        //otherwise the charts keep their first visual representation which was created at a moment where the calc itself was not loaded completly and is incorect therefor
        if( (rImport.getImportFlags() & IMPORT_ALL) == IMPORT_ALL )
            pCL->SetDirty( TRUE );
        else
        {
            // #i104899# If a formula cell is already dirty, further changes aren't propagated.
            // This can happen easily now that row heights aren't updated for all sheets.
            pDoc->InterpretDirtyCells( *pCL->GetRangeList() );
        }

        pCollection->Insert( pCL );
        pCL->StartListeningTo();
    }
}

void ScMyShapeResizer::AddShape(uno::Reference <drawing::XShape>& rShape,
    rtl::OUString* pRangeList,
    table::CellAddress& rStartAddress, table::CellAddress& rEndAddress,
    sal_Int32 nEndX, sal_Int32 nEndY)
{
    ScMyToResizeShape aShape;
    aShape.xShape.set(rShape);
    aShape.pRangeList = pRangeList;
    aShape.aEndCell = rEndAddress;
    aShape.aStartCell = rStartAddress;
    aShape.nEndY = nEndY;
    aShape.nEndX = nEndX;
    aShapes.push_back(aShape);
}

void ScMyShapeResizer::GetNewShapeSizePos(ScDocument* pDoc, const Rectangle& rStartRect,
                                          const table::CellAddress& rEndCell,
                                          awt::Point& rPoint, awt::Size& rSize,
                                          sal_Int32& rEndX, sal_Int32& rEndY) const
{
    awt::Point aRefPoint;
    BOOL bNegativePage(pDoc->IsNegativePage(rEndCell.Sheet));
    if (bNegativePage)
        aRefPoint.X = rStartRect.Right();
    else
        aRefPoint.X = rStartRect.Left();
    aRefPoint.Y = rStartRect.Top();
    Rectangle aRect(pDoc->GetMMRect(
        static_cast<SCCOL>(rEndCell.Column), static_cast<SCROW>(rEndCell.Row),
        static_cast<SCCOL>(rEndCell.Column), static_cast<SCROW>(rEndCell.Row), rEndCell.Sheet ));
    if (bNegativePage)
        rEndX = -rEndX + aRect.Right();
    else
        rEndX += aRect.Left();
    rEndY += aRect.Top();
    rPoint.X += aRefPoint.X;
    if (bNegativePage)
    {
        if (rPoint.X < rStartRect.Left())
            rPoint.X = rStartRect.Left() + 2; // increment by 2 100th_mm because the cellwidth is internal in twips
    }
    else
    {
        if (rPoint.X > rStartRect.Right())
            rPoint.X = rStartRect.Right() - 2; // decrement by 2 100th_mm because the cellwidth is internal in twips
    }
    rPoint.Y += aRefPoint.Y;
    if (rPoint.Y > rStartRect.Bottom())
        rPoint.Y = rStartRect.Bottom() - 2; // decrement by 2 100th_mm because the cellheight is internal in twips
    if (bNegativePage)
    {
        rSize.Width = -(rEndX - rPoint.X);
    }
    else
        rSize.Width = rEndX - rPoint.X;
    rSize.Height = rEndY - rPoint.Y;
}

void ScMyShapeResizer::ResizeShapes()
{
    if (!aShapes.empty() && rImport.GetModel().is())
    {
        rtl::OUString sRowHeight(RTL_CONSTASCII_USTRINGPARAM(SC_UNONAME_CELLHGT));
        rtl::OUString sPersistName (RTL_CONSTASCII_USTRINGPARAM("PersistName"));
        rtl::OUString sCaptionPoint( RTL_CONSTASCII_USTRINGPARAM( "CaptionPoint" ));
        rtl::OUString sConnectorShape( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.drawing.ConnectorShape") );
        rtl::OUString sCaptionShape( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.drawing.CaptionShape") );
        rtl::OUString sStartShape(RTL_CONSTASCII_USTRINGPARAM("StartShape"));
        rtl::OUString sEndShape(RTL_CONSTASCII_USTRINGPARAM("EndShape"));
        rtl::OUString sStartPosition(RTL_CONSTASCII_USTRINGPARAM("StartPosition"));
        rtl::OUString sEndPosition(RTL_CONSTASCII_USTRINGPARAM("EndPosition"));
        uno::Reference<table::XCellRange> xTableRow;
        uno::Reference<sheet::XSpreadsheet> xSheet;
        uno::Reference<table::XTableRows> xTableRows;
        sal_Int32 nOldRow(-1);
        sal_Int32 nOldSheet(-1);
        ScMyToResizeShapes::iterator aItr(aShapes.begin());
        ScMyToResizeShapes::iterator aEndItr(aShapes.end());
        uno::Reference <sheet::XSpreadsheetDocument> xSpreadDoc( rImport.GetModel(), uno::UNO_QUERY );
        if ( xSpreadDoc.is() )
        {
            uno::Reference<container::XIndexAccess> xIndex( xSpreadDoc->getSheets(), uno::UNO_QUERY );
            ScDocument* pDoc(rImport.GetDocument());
            if ( pDoc && xIndex.is() )
            {
                rImport.LockSolarMutex();
                while (aItr != aEndItr)
                {
                    // #i78086# invalid cell position is used to call CreateChartListener only
                    if ( aItr->aEndCell.Sheet >= 0 )
                    {
                        if ((nOldSheet != aItr->aEndCell.Sheet) || !xSheet.is())
                        {
                            nOldSheet = aItr->aEndCell.Sheet;
                            xSheet.set(xIndex->getByIndex(nOldSheet), uno::UNO_QUERY);
                            if (xSheet.is())
                            {
                                uno::Reference<table::XColumnRowRange> xColumnRowRange (xSheet, uno::UNO_QUERY);
                                if (xColumnRowRange.is())
                                    xTableRows = xColumnRowRange->getRows();
                            }
                        }
                        if (xTableRows.is())
                        {
                            if (nOldRow != aItr->aEndCell.Row || !xTableRow.is())
                            {
                                nOldRow = aItr->aEndCell.Row;
                                xTableRows->getByIndex(nOldRow) >>= xTableRow;
                            }
                            if (xTableRow.is())
                            {
                                uno::Reference <beans::XPropertySet> xRowProperties(xTableRow, uno::UNO_QUERY);
                                if (xRowProperties.is())
                                {
                                    sal_Int32 nHeight;
                                    if (xRowProperties->getPropertyValue(sRowHeight) >>= nHeight)
                                    {
                                        Rectangle aRec = pDoc->GetMMRect(static_cast<SCCOL>(aItr->aStartCell.Column), static_cast<SCROW>(aItr->aStartCell.Row),
                                            static_cast<SCCOL>(aItr->aStartCell.Column), static_cast<SCROW>(aItr->aStartCell.Row), aItr->aStartCell.Sheet);
                                        awt::Point aPoint(aItr->xShape->getPosition());
                                        awt::Size aSize(aItr->xShape->getSize());
                                        if (pDoc->IsNegativePage(static_cast<SCTAB>(nOldSheet)))
                                            aPoint.X += aSize.Width;
                                        if (aItr->nEndY >= 0 && aItr->nEndX >= 0)
                                        {
                                            if (aItr->xShape->getShapeType().equals(sConnectorShape))
                                            {
                                                //#103122#; handle connected Connectorshapes
                                                uno::Reference<beans::XPropertySet> xShapeProps (aItr->xShape, uno::UNO_QUERY);
                                                if(xShapeProps.is())
                                                {
                                                    uno::Reference<drawing::XShape> xStartShape(xShapeProps->getPropertyValue(  sStartShape ), uno::UNO_QUERY);
                                                    uno::Reference<drawing::XShape> xEndShape(xShapeProps->getPropertyValue( sEndShape ), uno::UNO_QUERY);
                                                    if (!xStartShape.is() && !xEndShape.is())
                                                    {
                                                        awt::Size aOldSize(aSize);
                                                        GetNewShapeSizePos(pDoc, aRec, aItr->aEndCell, aPoint, aSize, aItr->nEndX, aItr->nEndY);
                                                        aItr->xShape->setPosition(aPoint);
                                                        if( (aSize.Width != aOldSize.Width) ||
                                                            (aSize.Height != aOldSize.Height) )
                                                            aItr->xShape->setSize(aSize);
                                                    }
                                                    else if (xStartShape.is() && xEndShape.is())
                                                    {
                                                        // do nothing, because they are connected
                                                    }
                                                    else
                                                    {
                                                        // only one point is connected, the other should be moved

                                                        rtl::OUString sProperty;
                                                        if (xStartShape.is())
                                                        {
                                                            awt::Point aEndPoint;
                                                            xShapeProps->getPropertyValue(sEndPosition) >>= aEndPoint;
                                                            aPoint.X = aRec.Left() + aEndPoint.X;
                                                            aPoint.Y = aRec.Top() + aEndPoint.Y;
                                                            sProperty = sEndPosition;
                                                        }
                                                        else
                                                        {
                                                            awt::Point aStartPoint;
                                                            xShapeProps->getPropertyValue(sStartPosition) >>= aStartPoint;
                                                            aPoint.X = aRec.Left() + aStartPoint.X;
                                                            aPoint.Y = aRec.Top() + aStartPoint.Y;
                                                            sProperty = sStartPosition;
                                                        }
                                                        xShapeProps->setPropertyValue(sProperty, uno::makeAny(aPoint));
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                awt::Size aOldSize(aSize);
                                                GetNewShapeSizePos(pDoc, aRec, aItr->aEndCell, aPoint, aSize, aItr->nEndX, aItr->nEndY);
                                                if (pDoc->IsNegativePage(static_cast<SCTAB>(nOldSheet)))
                                                    aPoint.X -= aSize.Width;
                                                aItr->xShape->setPosition(aPoint);
                                                if( (aSize.Width != aOldSize.Width) ||
                                                    (aSize.Height != aOldSize.Height) )
                                                    aItr->xShape->setSize(aSize);
                                            }
                                        }
                                        else
                                        {
                                            if (aItr->xShape->getShapeType().equals(sCaptionShape))
                                            {
                                                Rectangle aRectangle(aPoint.X, aPoint.Y, aPoint.X + aSize.Width, aPoint.Y + aSize.Height);

                                                awt::Point aCaptionPoint;
                                                uno::Reference< beans::XPropertySet > xShapeProps(aItr->xShape, uno::UNO_QUERY);
                                                if (xShapeProps.is())
                                                {
                                                    try
                                                    {
                                                        xShapeProps->getPropertyValue( sCaptionPoint ) >>= aCaptionPoint;
                                                    }
                                                    catch ( uno::Exception& )
                                                    {
                                                        DBG_ERROR("This Captionshape has no CaptionPoint property.");
                                                    }
                                                }
                                                Point aCorePoint(aPoint.X, aPoint.Y);
                                                Point aCoreCaptionPoint(aCaptionPoint.X, aCaptionPoint.Y);
                                                aCoreCaptionPoint += aCorePoint;
                                                aRectangle.Union(Rectangle(aCoreCaptionPoint, aCoreCaptionPoint));

                                                Point aBeforeRightBottomPoint(aRectangle.BottomRight());

                                                aRectangle += aRec.TopLeft();
                                                if (aRectangle.Left() > aRec.Right())
                                                    aRectangle -= (Point(aRectangle.Left() - aRec.Right() + 2, 0));
                                                if (aRectangle.Top() > aRec.Bottom())
                                                    aRectangle -= (Point(0, aRectangle.Top() - aRec.Bottom() + 2));

                                                Point aDifferencePoint(aRectangle.BottomRight() - aBeforeRightBottomPoint);
                                                aPoint.X += aDifferencePoint.X();
                                                aPoint.Y += aDifferencePoint.Y();

                                                aItr->xShape->setPosition(aPoint);
                                            }
                                            else
                                            {
                                                // #96159# it is possible, that shapes have a negative position
                                                // this is now handled here
                                                DBG_ERROR("no or negative end address of this shape");
                                                awt::Point aRefPoint;
                                                aRefPoint.X = aRec.Left();
                                                aRefPoint.Y = aRec.Top();
                                                aPoint.X += aRefPoint.X;
                                                if (aPoint.X > aRec.Right())
                                                    aPoint.X = aRec.Right() - 2; // decrement by 2 100th_mm because the cellheight is internal in twips
                                                aPoint.Y += aRefPoint.Y;
                                                if (aPoint.Y > aRec.Bottom())
                                                    aPoint.Y = aRec.Bottom() - 2; // decrement by 2 100th_mm because the cellheight is internal in twips
                                                aItr->xShape->setPosition(aPoint);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        else
                    {
                            DBG_ERROR("something wents wrong");
                    }
                    }
                    // #i78086# call CreateChartListener also for invalid position (anchored to sheet)
                    if (IsOLE(aItr->xShape))
                    {
                        uno::Reference < beans::XPropertySet > xShapeProps ( aItr->xShape, uno::UNO_QUERY );
                        uno::Reference < beans::XPropertySetInfo > xShapeInfo(xShapeProps->getPropertySetInfo());
                        rtl::OUString sName;
                        if (xShapeProps.is() && xShapeInfo.is() && xShapeInfo->hasPropertyByName(sPersistName) &&
                            (xShapeProps->getPropertyValue(sPersistName) >>= sName))
                            CreateChartListener(pDoc, sName, aItr->pRangeList);
                    }
                    if (aItr->pRangeList)
                        delete aItr->pRangeList;
                    aItr = aShapes.erase(aItr);
                }
                rImport.UnlockSolarMutex();
//              if (pCollection)
//                  pDoc->SetChartListenerCollection(pCollection);
            }
        }
    }
}