summaryrefslogtreecommitdiff
path: root/sc/source/core/tool/chartpos.cxx
blob: db2469d853e57736050938c241e298b9ae68e87f (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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <chartpos.hxx>
#include <document.hxx>
#include <rechead.hxx>
#include <osl/diagnose.h>

#include <memory>

namespace
{
    bool lcl_hasValueDataButNoDates( const ScDocument* pDocument, SCCOL nCol, SCROW nRow, SCTAB nTab )
    {
        bool bReturn = false;
        if (pDocument->HasValueData( nCol, nRow, nTab ))
        {
            //treat dates like text #i25706#
            sal_uInt32 nNumberFormat = pDocument->GetNumberFormat( ScAddress( nCol, nRow, nTab ) );
            SvNumFormatType nType = pDocument->GetFormatTable()->GetType(nNumberFormat);
            bool bIsDate(nType & SvNumFormatType::DATE);
            bReturn = !bIsDate;
        }
        return bReturn;
    }
}

ScChartPositioner::ScChartPositioner( ScDocument* pDoc, SCTAB nTab,
                    SCCOL nStartColP, SCROW nStartRowP, SCCOL nEndColP, SCROW nEndRowP) :
        pDocument( pDoc ),
        pPositionMap( nullptr ),
        eGlue( ScChartGlue::NA ),
        nStartCol(0),
        nStartRow(0),
        bColHeaders( false ),
        bRowHeaders( false ),
        bDummyUpperLeft( false )
{
    SetRangeList( ScRange( nStartColP, nStartRowP, nTab, nEndColP, nEndRowP, nTab ) );
    CheckColRowHeaders();
}

ScChartPositioner::ScChartPositioner( ScDocument* pDoc, const ScRangeListRef& rRangeList ) :
        aRangeListRef( rRangeList ),
        pDocument( pDoc ),
        pPositionMap( nullptr ),
        eGlue( ScChartGlue::NA ),
        nStartCol(0),
        nStartRow(0),
        bColHeaders( false ),
        bRowHeaders( false ),
        bDummyUpperLeft( false )
{
    if ( aRangeListRef.is() )
        CheckColRowHeaders();
}

ScChartPositioner::ScChartPositioner( const ScChartPositioner& rPositioner ) :
        aRangeListRef( rPositioner.aRangeListRef ),
        pDocument(rPositioner.pDocument),
        pPositionMap( nullptr ),
        eGlue(rPositioner.eGlue),
        nStartCol(rPositioner.nStartCol),
        nStartRow(rPositioner.nStartRow),
        bColHeaders(rPositioner.bColHeaders),
        bRowHeaders(rPositioner.bRowHeaders),
        bDummyUpperLeft( rPositioner.bDummyUpperLeft )
{
}

ScChartPositioner::~ScChartPositioner()
{
}

void ScChartPositioner::SetRangeList( const ScRange& rRange )
{
    aRangeListRef = new ScRangeList;
    aRangeListRef->Append( rRange );
    InvalidateGlue();
}

void ScChartPositioner::GlueState()
{
    if ( eGlue != ScChartGlue::NA )
        return;
    bDummyUpperLeft = false;
    ScRange* pR;
    if ( aRangeListRef->size() <= 1 )
    {
        if (  !aRangeListRef->empty() )
        {
            pR = aRangeListRef->front();
            if ( pR->aStart.Tab() == pR->aEnd.Tab() )
                eGlue = ScChartGlue::NONE;
            else
                eGlue = ScChartGlue::Cols;  // several tables column by column
            nStartCol = pR->aStart.Col();
            nStartRow = pR->aStart.Row();
        }
        else
        {
            InvalidateGlue();
            nStartCol = 0;
            nStartRow = 0;
        }
        return;
    }

    pR = aRangeListRef->front();
    nStartCol = pR->aStart.Col();
    nStartRow = pR->aStart.Row();
    SCCOL nMaxCols, nEndCol;
    SCROW nMaxRows, nEndRow;
    nMaxCols = nEndCol = 0;
    nMaxRows = nEndRow = 0;

    // <= so 1 extra pass after last item
    for ( size_t i = 1, nRanges = aRangeListRef->size(); i <= nRanges; ++i )
    {   // detect spanning/surrounding area etc.
        SCCOLROW nTmp, n1, n2;
        if ( (n1 = pR->aStart.Col()) < nStartCol ) nStartCol = static_cast<SCCOL>(n1  );
        if ( (n2 = pR->aEnd.Col()  ) > nEndCol   ) nEndCol   = static_cast<SCCOL>(n2  );
        if ( (nTmp = n2 - n1 + 1   ) > nMaxCols  ) nMaxCols  = static_cast<SCCOL>(nTmp);
        if ( (n1 = pR->aStart.Row()) < nStartRow ) nStartRow = static_cast<SCROW>(n1  );
        if ( (n2 = pR->aEnd.Row()  ) > nEndRow   ) nEndRow   = static_cast<SCROW>(n2  );
        if ( (nTmp = n2 - n1 + 1   ) > nMaxRows  ) nMaxRows  = static_cast<SCROW>(nTmp);

        // in last pass; i = nRanges so don't use at()
        if ( i < nRanges )
            pR = (*aRangeListRef)[i];
    }
    SCCOL nC = nEndCol - nStartCol + 1;
    if ( nC == 1 )
    {
        eGlue = ScChartGlue::Rows;
        return;
    }
    SCROW nR = nEndRow - nStartRow + 1;
    if ( nR == 1 )
    {
        eGlue = ScChartGlue::Cols;
        return;
    }
    sal_uLong nCR = static_cast<sal_uLong>(nC) * nR;

    /*
    TODO:
    First do it simple without bit masking. A maximum of 8MB could be allocated
    this way (256 Cols x 32000 Rows). That could be reduced to 2MB by
    using 2 Bits per entry, but it is faster this way.
    Another optimization would be to store only used rows/columns in the array, but
    would mean another iteration of the RangeList indirect access to the array. */

    enum class CellState : sal_uInt8 { Hole, Occupied, Free, Glue };
    CellState* p;
    std::unique_ptr<CellState[]> pA(new CellState[ nCR ]);
    memset( pA.get(), 0, nCR * sizeof(CellState) );

    SCCOL nCol, nCol1, nCol2;
    SCROW nRow, nRow1, nRow2;
    for ( size_t i = 0, nRanges = aRangeListRef->size(); i < nRanges; ++i )
    {   // mark selections as used in 2D
        pR = (*aRangeListRef)[i];
        nCol1 = pR->aStart.Col() - nStartCol;
        nCol2 = pR->aEnd.Col() - nStartCol;
        nRow1 = pR->aStart.Row() - nStartRow;
        nRow2 = pR->aEnd.Row() - nStartRow;
        for ( nCol = nCol1; nCol <= nCol2; nCol++ )
        {
            p = pA.get() + static_cast<sal_uLong>(nCol) * nR + nRow1;
            for ( nRow = nRow1; nRow <= nRow2; nRow++, p++ )
                *p = CellState::Occupied;
        }
    }
    bool bGlue = true;

    bool bGlueCols = false;
    for ( nCol = 0; bGlue && nCol < nC; nCol++ )
    {   // iterate columns and try to mark as unused
        p = pA.get() + static_cast<sal_uLong>(nCol) * nR;
        for ( nRow = 0; bGlue && nRow < nR; nRow++, p++ )
        {
            if ( *p == CellState::Occupied )
            {   // If there's one right in the middle, we can't combine.
                // If it were at the edge, we could combine, if in this Column
                // in every set line, one is set.
                if ( nRow > 0 && nCol > 0 )
                    bGlue = false; // nCol==0 can be DummyUpperLeft
                else
                    nRow = nR;
            }
            else
                *p = CellState::Free;
        }
        if ( bGlue && *(p = (pA.get() + (((static_cast<sal_uLong>(nCol)+1) * nR) - 1))) == CellState::Free )
        {   // mark column as totally unused
            *p = CellState::Glue;
            bGlueCols = true; // one unused column at least
        }
    }

    bool bGlueRows = false;
    for ( nRow = 0; bGlue && nRow < nR; nRow++ )
    {   // iterate rows and try to mark as unused
        p = pA.get() + nRow;
        for ( nCol = 0; bGlue && nCol < nC; nCol++, p+=nR )
        {
            if ( *p == CellState::Occupied )
            {
                if ( nCol > 0 && nRow > 0 )
                    bGlue = false; // nRow==0 can be DummyUpperLeft
                else
                    nCol = nC;
            }
            else
                *p = CellState::Free;
        }
        if ( bGlue && *(p = (pA.get() + (((static_cast<sal_uLong>(nC)-1) * nR) + nRow))) == CellState::Free )
        {   // mark row as totally unused
            *p = CellState::Glue;
            bGlueRows = true; // one unused row at least
        }
    }

    // If n=1: The upper left corner could be automagically pulled in for labeling
    p = pA.get() + 1;
    for ( sal_uLong n = 1; bGlue && n < nCR; n++, p++ )
    {   // An untouched field means we could neither reach it through rows nor columns,
        // thus we can't combine anything
        if ( *p == CellState::Hole )
            bGlue = false;
    }
    if ( bGlue )
    {
        if ( bGlueCols && bGlueRows )
            eGlue = ScChartGlue::Both;
        else if ( bGlueRows )
            eGlue = ScChartGlue::Rows;
        else
            eGlue = ScChartGlue::Cols;
        if ( pA[0] != CellState::Occupied )
            bDummyUpperLeft = true;
    }
    else
    {
        eGlue = ScChartGlue::NONE;
    }
}

void ScChartPositioner::CheckColRowHeaders()
{
    SCCOL nCol1, nCol2, iCol;
    SCROW nRow1, nRow2, iRow;
    SCTAB nTab1, nTab2;

    bool bColStrings = true;
    bool bRowStrings = true;
    GlueState();
    if ( aRangeListRef->size() == 1 )
    {
        aRangeListRef->front()->GetVars( nCol1, nRow1, nTab1, nCol2, nRow2, nTab2 );
        if ( nCol1 > nCol2 || nRow1 > nRow2 )
            bColStrings = bRowStrings = false;
        else
        {
            for (iCol=nCol1; iCol<=nCol2 && bColStrings; iCol++)
            {
                if (lcl_hasValueDataButNoDates( pDocument, iCol, nRow1, nTab1 ))
                        bColStrings = false;
            }
            for (iRow=nRow1; iRow<=nRow2 && bRowStrings; iRow++)
            {
                if (lcl_hasValueDataButNoDates( pDocument, nCol1, iRow, nTab1 ))
                        bRowStrings = false;
            }
        }
    }
    else
    {
        bool bVert = (eGlue == ScChartGlue::NONE || eGlue == ScChartGlue::Rows);
        for ( size_t i = 0, nRanges = aRangeListRef->size();
              (i < nRanges) && (bColStrings || bRowStrings);
              ++i
            )
        {
            ScRange* pR = (*aRangeListRef)[i];
            pR->GetVars( nCol1, nRow1, nTab1, nCol2, nRow2, nTab2 );
            bool bTopRow = (nRow1 == nStartRow);
            if ( bRowStrings && (bVert || nCol1 == nStartCol) )
            {   // NONE or ROWS: RowStrings in every selection possible
                // COLS or BOTH: only from first column
                if ( nCol1 <= nCol2 )
                    for (iRow=nRow1; iRow<=nRow2 && bRowStrings; iRow++)
                    {
                        if (lcl_hasValueDataButNoDates( pDocument, nCol1, iRow, nTab1 ))
                                bRowStrings = false;
                    }
            }
            if ( bColStrings && bTopRow )
            {   // ColStrings only from first row
                if ( nRow1 <= nRow2 )
                    for (iCol=nCol1; iCol<=nCol2 && bColStrings; iCol++)
                    {
                        if (lcl_hasValueDataButNoDates( pDocument, iCol, nRow1, nTab1 ))
                                bColStrings = false;
                    }
            }
        }
    }
    bColHeaders = bColStrings;
    bRowHeaders = bRowStrings;
}

const ScChartPositionMap* ScChartPositioner::GetPositionMap()
{
    CreatePositionMap();
    return pPositionMap.get();
}

void ScChartPositioner::CreatePositionMap()
{
    if ( eGlue == ScChartGlue::NA && pPositionMap )
    {
        pPositionMap.reset();
    }

    if ( pPositionMap )
        return ;

    SCSIZE nColAdd = bRowHeaders ? 1 : 0;
    SCSIZE nRowAdd = bColHeaders ? 1 : 0;

    SCCOL nCol, nCol1, nCol2;
    SCROW nRow, nRow1, nRow2;
    SCTAB nTab, nTab1, nTab2;

    //  real size (without hidden rows/columns)

    SCSIZE nColCount = 0;
    SCSIZE nRowCount = 0;

    GlueState();

    const bool bNoGlue = (eGlue == ScChartGlue::NONE);
    std::unique_ptr<ColumnMap> pCols( new ColumnMap );
    SCROW nNoGlueRow = 0;
    for ( size_t i = 0, nRanges = aRangeListRef->size(); i < nRanges; ++i )
    {
        ScRange* pR = (*aRangeListRef)[i];
        pR->GetVars( nCol1, nRow1, nTab1, nCol2, nRow2, nTab2 );
        for ( nTab = nTab1; nTab <= nTab2; nTab++ )
        {
            // nTab in ColKey to allow to have the same col/row in another table
            sal_uLong nInsCol = (static_cast<sal_uLong>(nTab) << 16) | (bNoGlue ? 0 :
                    static_cast<sal_uLong>(nCol1));
            for ( nCol = nCol1; nCol <= nCol2; ++nCol, ++nInsCol )
            {
                RowMap* pCol = nullptr;
                ColumnMap::const_iterator it = pCols->find( nInsCol );
                if ( it == pCols->end() )
                {
                    pCol = new RowMap;
                    pCols->emplace(nInsCol, pCol);
                }
                else
                    pCol = it->second;

                // in other table a new ColKey already was created,
                // the rows must be equal to be filled with Dummy
                sal_uLong nInsRow = (bNoGlue ? nNoGlueRow : nRow1);
                for ( nRow = nRow1; nRow <= nRow2; nRow++, nInsRow++ )
                {
                    if ( pCol->find( nInsRow ) == pCol->end() )
                    {
                        pCol->emplace( nInsRow, new ScAddress( nCol, nRow, nTab ) );
                    }
                }
            }
        }
        // For NoGlue: associated tables will be rendered as ColGlue
        nNoGlueRow += nRow2 - nRow1 + 1;
    }

    // count of data
    nColCount = static_cast< SCSIZE >( pCols->size());
    if ( !pCols->empty() )
    {
        RowMap* pCol = pCols->begin()->second;
        if ( bDummyUpperLeft )
            (*pCol)[ 0 ] = nullptr; // Dummy for labeling
        nRowCount = static_cast< SCSIZE >( pCol->size());
    }
    else
        nRowCount = 0;
    if ( nColCount > 0 )
        nColCount -= nColAdd;
    if ( nRowCount > 0 )
        nRowCount -= nRowAdd;

    if ( nColCount==0 || nRowCount==0 )
    {   // create an entry without data
        RowMap* pCol;
        if ( !pCols->empty() )
            pCol = pCols->begin()->second;
        else
        {
            pCol = new RowMap;
            (*pCols)[ 0 ] = pCol;
        }
        nColCount = 1;
        if ( !pCol->empty() )
        {   // cannot be if nColCount==0 || nRowCount==0
            ScAddress* pPos = pCol->begin()->second;
            if ( pPos )
            {
                sal_uLong nCurrentKey = pCol->begin()->first;
                delete pPos;
                (*pCol)[ nCurrentKey ] = nullptr;
            }
        }
        else
            (*pCol)[ 0 ] = nullptr;
        nRowCount = 1;
        nColAdd = 0;
        nRowAdd = 0;
    }
    else
    {
        if ( bNoGlue )
        {   // fill gaps with Dummies, first column is master
            RowMap* pFirstCol = pCols->begin()->second;
            sal_uLong nCount = pFirstCol->size();
            RowMap::const_iterator it1 = pFirstCol->begin();
            for ( sal_uLong n = 0; n < nCount; n++, ++it1 )
            {
                sal_uLong nKey = it1->first;
                for (ColumnMap::const_iterator it2 = ++pCols->begin(); it2 != pCols->end(); ++it2 )
                    it2->second->emplace( nKey, nullptr ); // no data
            }
        }
    }

    pPositionMap.reset( new ScChartPositionMap( static_cast<SCCOL>(nColCount), static_cast<SCROW>(nRowCount),
        static_cast<SCCOL>(nColAdd), static_cast<SCROW>(nRowAdd), *pCols ) );

    //  cleanup
    for (ColumnMap::const_iterator it = pCols->begin(); it != pCols->end(); ++it )
    {   // Only delete tables, not the ScAddress*!
        delete it->second;
    }
}

void ScChartPositioner::InvalidateGlue()
{
    eGlue = ScChartGlue::NA;
    pPositionMap.reset();
}

ScChartPositionMap::ScChartPositionMap( SCCOL nChartCols, SCROW nChartRows,
            SCCOL nColAdd, SCROW nRowAdd, ColumnMap& rCols ) :
        ppData( new ScAddress* [ nChartCols * nChartRows ] ),
        ppColHeader( new ScAddress* [ nChartCols ] ),
        ppRowHeader( new ScAddress* [ nChartRows ] ),
        nCount( static_cast<sal_uLong>(nChartCols) * nChartRows ),
        nColCount( nChartCols ),
        nRowCount( nChartRows )
{
    OSL_ENSURE( nColCount && nRowCount, "ScChartPositionMap without dimension" );

    ColumnMap::const_iterator pColIter = rCols.begin();
    RowMap* pCol1 = pColIter->second;
    RowMap::const_iterator pPos1Iter;

    // row header
    pPos1Iter = pCol1->begin();
    if ( nRowAdd )
        ++pPos1Iter;
    if ( nColAdd )
    {   // independent
        SCROW nRow = 0;
        for ( ; nRow < nRowCount && pPos1Iter != pCol1->end(); nRow++ )
        {
            ppRowHeader[ nRow ] = pPos1Iter->second;
            ++pPos1Iter;
        }
        for ( ; nRow < nRowCount; nRow++ )
            ppRowHeader[ nRow ] = nullptr;
    }
    else
    {   // copy
        SCROW nRow = 0;
        for ( ; nRow < nRowCount && pPos1Iter != pCol1->end(); nRow++ )
        {
            ppRowHeader[ nRow ] = pPos1Iter->second ?
                new ScAddress( *pPos1Iter->second ) : nullptr;
            ++pPos1Iter;
        }
        for ( ; nRow < nRowCount; nRow++ )
            ppRowHeader[ nRow ] = nullptr;
    }
    if ( nColAdd )
    {
        ++pColIter;
    }

    // data column by column and column-header
    sal_uLong nIndex = 0;
    for ( SCCOL nCol = 0; nCol < nColCount; nCol++ )
    {
        if ( pColIter != rCols.end() )
        {
            RowMap* pCol2 = pColIter->second;
            RowMap::const_iterator pPosIter = pCol2->begin();
            if ( pPosIter != pCol2->end() )
            {
                if ( nRowAdd )
                {
                    ppColHeader[ nCol ] = pPosIter->second; // independent
                    ++pPosIter;
                }
                else
                    ppColHeader[ nCol ] = pPosIter->second ?
                        new ScAddress( *pPosIter->second ) : nullptr;
            }

            SCROW nRow = 0;
            for ( ; nRow < nRowCount && pPosIter != pCol2->end(); nRow++, nIndex++ )
            {
                ppData[ nIndex ] = pPosIter->second;
                ++pPosIter;
            }
            for ( ; nRow < nRowCount; nRow++, nIndex++ )
                ppData[ nIndex ] = nullptr;

            ++pColIter;
        }
        else
        {
            ppColHeader[ nCol ] = nullptr;
            for ( SCROW nRow = 0; nRow < nRowCount; nRow++, nIndex++ )
            {
                ppData[ nIndex ] = nullptr;
            }
        }
    }
}

ScChartPositionMap::~ScChartPositionMap()
{
    for ( sal_uLong nIndex=0; nIndex < nCount; nIndex++ )
    {
        delete ppData[nIndex];
    }
    for ( SCCOL j=0; j < nColCount; j++ )
    {
        delete ppColHeader[j];
    }
    for ( SCROW i=0; i < nRowCount; i++ )
    {
        delete ppRowHeader[i];
    }
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */