Coverage Report

Created: 2026-11-06 11:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sw/source/uibase/table/tablemgr.cxx
Line
Count
Source
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 <com/sun/star/chart/ChartDataRowSource.hpp>
21
#include <com/sun/star/chart2/data/XDataProvider.hpp>
22
#include <com/sun/star/chart2/data/XDataReceiver.hpp>
23
#include <com/sun/star/chart2/XChartDocument.hpp>
24
#include <com/sun/star/beans/PropertyState.hpp>
25
#include <com/sun/star/embed/EmbedVerbs.hpp>
26
#include <com/sun/star/embed/XEmbeddedObject.hpp>
27
#include <com/sun/star/frame/XModel3.hpp>
28
29
#include <comphelper/classids.hxx>
30
#include <svx/charthelper.hxx>
31
#include <svtools/embedhlp.hxx>
32
33
#include <edtwin.hxx>
34
#include <wrtsh.hxx>
35
#include <view.hxx>
36
#include <swundo.hxx>
37
#include <tablemgr.hxx>
38
#include <frmfmt.hxx>
39
#include <swabstdlg.hxx>
40
#include <swcli.hxx>
41
#include <docsh.hxx>
42
#include <unotbl.hxx>
43
#include <unochart.hxx>
44
45
#include <comphelper/lok.hxx>
46
47
using namespace ::com::sun::star;
48
49
// Determine the width
50
SwTwips SwTableFUNC::GetColWidth(sal_uInt16 nNum) const
51
0
{
52
0
    SwTwips nWidth = 0;
53
54
0
    if( m_aCols.Count() > 0 )
55
0
    {
56
0
        if(m_aCols.Count() == GetColCount())
57
0
        {
58
0
            if(nNum == m_aCols.Count())
59
0
                nWidth = m_aCols.GetRight() - m_aCols[nNum-1];
60
0
            else
61
0
            {
62
0
                if(nNum == 0)
63
0
                    nWidth = m_aCols[nNum] - m_aCols.GetLeft();
64
0
                else
65
0
                    nWidth = m_aCols[nNum] - m_aCols[nNum-1];
66
0
            }
67
0
        }
68
0
        else
69
0
        {
70
0
            SwTwips nRValid = nNum < GetColCount() ?
71
0
                              m_aCols[GetRightSeparator(nNum)] :
72
0
                              m_aCols.GetRight();
73
0
            SwTwips nLValid = nNum ?
74
0
                              m_aCols[GetRightSeparator(nNum - 1)] :
75
0
                              m_aCols.GetLeft();
76
0
            nWidth = nRValid - nLValid;
77
0
        }
78
0
    }
79
0
    else
80
0
        nWidth = m_aCols.GetRight();
81
0
    return nWidth;
82
0
}
83
84
SwTwips SwTableFUNC::GetMaxColWidth( sal_uInt16 nNum ) const
85
0
{
86
0
    OSL_ENSURE(nNum <= m_aCols.Count(), "Index out of Area");
87
88
0
    if ( GetColCount() > 0 )
89
0
    {
90
        // The maximum width arises from the own width and
91
        // the width of each of the neighbor cells reduced by MINLAY.
92
0
        SwTwips nMax;
93
0
        if(nNum == 0)
94
0
            nMax = GetColWidth(1) - MINLAY;
95
0
        else
96
0
        {
97
0
            nMax = GetColWidth(nNum-1);
98
0
            if(nNum == GetColCount())
99
0
                nMax -= MINLAY;
100
0
            else
101
0
                nMax += GetColWidth(nNum+1) - 2 * MINLAY;
102
0
        }
103
0
        return nMax + GetColWidth(nNum);
104
0
    }
105
0
    else
106
0
        return GetColWidth(nNum);
107
0
}
108
109
void SwTableFUNC::SetColWidth(sal_uInt16 nNum, SwTwips nNewWidth )
110
0
{
111
    // set current width
112
    // move all of the following
113
0
    bool bCurrentOnly = false;
114
115
0
    if ( m_aCols.Count() > 0 )
116
0
    {
117
0
        if(m_aCols.Count() != GetColCount())
118
0
            bCurrentOnly = true;
119
0
        SwTwips nWidth = GetColWidth(nNum);
120
121
0
        int nDiff = static_cast<int>(nNewWidth - nWidth);
122
0
        if( !nNum )
123
0
            m_aCols[ GetRightSeparator(0) ] += nDiff;
124
0
        else if( nNum < GetColCount()  )
125
0
        {
126
0
            if(nDiff < GetColWidth(nNum + 1) - MINLAY)
127
0
                m_aCols[ GetRightSeparator(nNum) ] += nDiff;
128
0
            else
129
0
            {
130
0
                int nDiffLeft = nDiff - static_cast<int>(GetColWidth(nNum + 1)) + int(MINLAY);
131
0
                m_aCols[ GetRightSeparator(nNum) ] += (nDiff - nDiffLeft);
132
0
                m_aCols[ GetRightSeparator(nNum - 1) ] -= nDiffLeft;
133
0
            }
134
0
        }
135
0
        else
136
0
            m_aCols[ GetRightSeparator(nNum-1) ] -= nDiff;
137
0
    }
138
0
    else
139
0
        m_aCols.SetRight( std::min( nNewWidth, SwTwips(m_aCols.GetRightMax()) ) );
140
141
0
    m_pSh->StartAllAction();
142
0
    m_pSh->SetTabCols( m_aCols, bCurrentOnly );
143
0
    m_pSh->EndAllAction();
144
0
}
145
146
void SwTableFUNC::InitTabCols()
147
0
{
148
0
    OSL_ENSURE(m_pSh, "no Shell");
149
150
0
    if( m_pFormat && m_pSh)
151
0
        m_pSh->GetTabCols( m_aCols );
152
0
}
153
154
SwTableFUNC::SwTableFUNC(SwWrtShell *pShell)
155
0
    : m_pFormat(pShell->GetTableFormat()),
156
0
      m_pSh(pShell)
157
0
{
158
0
}
159
160
SwTableFUNC::~SwTableFUNC()
161
0
{
162
0
}
163
164
void SwTableFUNC::UpdateChart()
165
0
{
166
    //Update of the fields triggered by the user, all Charts of
167
    //the table will be brought up to date
168
0
    SwFrameFormat *pFormat2 = m_pSh->GetTableFormat();
169
0
    if ( pFormat2 && m_pSh->HasOLEObj( pFormat2->GetName() ) )
170
0
    {
171
0
        m_pSh->StartAllAction();
172
0
        m_pSh->UpdateCharts( pFormat2->GetName() );
173
0
        m_pSh->EndAllAction();
174
0
    }
175
0
}
176
177
uno::Reference< frame::XModel > SwTableFUNC::InsertChart(
178
        uno::Reference< chart2::data::XDataProvider > const &rxDataProvider,
179
        bool bFillWithData,
180
        const OUString &rCellRange,
181
        SwFlyFrameFormat** ppFlyFrameFormat )
182
0
{
183
0
    uno::Reference< frame::XModel > xChartModel;
184
0
    m_pSh->StartUndo( SwUndoId::UI_INSERT_CHART );
185
0
    m_pSh->StartAllAction();
186
187
0
    UIName aName;
188
0
    if (m_pSh->IsCursorInTable())
189
0
    {
190
0
        aName = m_pSh->GetTableFormat()->GetName();
191
        // insert node before table
192
0
        m_pSh->MoveTable( GotoCurrTable, fnTableStart );
193
0
        m_pSh->Up( false );
194
0
        if ( m_pSh->IsCursorInTable() )
195
0
        {
196
0
            if ( aName != m_pSh->GetTableFormat()->GetName() )
197
0
                m_pSh->Down( false ); // two adjacent tables
198
0
        }
199
0
        m_pSh->SplitNode();
200
0
    }
201
202
    // insert chart
203
0
    OUString aObjName;
204
0
    comphelper::EmbeddedObjectContainer aCnt;
205
0
    uno::Reference < embed::XEmbeddedObject > xObj =
206
0
        aCnt.CreateEmbeddedObject( SvGlobalName( SO3_SCH_CLASSID ).GetByteSequence(), aObjName );
207
208
0
    ::svt::EmbeddedObjectRef aEmbObjRef( xObj, css::embed::Aspects::MSOLE_CONTENT );
209
0
    if ( xObj.is() )
210
0
    {
211
212
0
        SwFlyFrameFormat* pTmp = nullptr;
213
0
        m_pSh->InsertOleObject( aEmbObjRef, &pTmp );
214
0
        if (ppFlyFrameFormat)
215
0
            *ppFlyFrameFormat = pTmp;
216
217
0
        xChartModel.set( xObj->getComponent(), uno::UNO_QUERY );
218
0
        if( xChartModel.is() )
219
0
        {
220
            // Create a default chart type.
221
0
            uno::Reference<chart2::XChartDocument> xChartDoc(xChartModel, uno::UNO_QUERY);
222
0
            if (xChartDoc.is())
223
0
                xChartDoc->createDefaultChart();
224
225
0
            xChartModel->lockControllers(); //#i79578# don't request a new replacement image for charts too often - block change notifications
226
0
        }
227
228
        // set the table name at the OLE-node
229
0
        if (!aName.isEmpty())
230
0
            m_pSh->SetChartName( aName );
231
0
    }
232
0
    m_pSh->EndAllAction();
233
234
0
    if (xObj.is())
235
0
    {
236
        // Let the chart be activated after the inserting
237
0
        SfxInPlaceClient* pClient = m_pSh->GetView().FindIPClient( xObj, &m_pSh->GetView().GetEditWin() );
238
0
        if ( !pClient )
239
0
        {
240
0
            pClient = new SwOleClient( &m_pSh->GetView(), &m_pSh->GetView().GetEditWin(), aEmbObjRef );
241
0
            m_pSh->SetCheckForOLEInCaption( true );
242
0
        }
243
0
        m_pSh->CalcAndSetScale( aEmbObjRef );
244
        //#50270# We don't need to handle errors,
245
        //this does the DoVerb in the SfxViewShell.
246
0
        ErrCodeMsg nErr = pClient->DoVerb(embed::EmbedVerbs::MS_OLEVERB_SHOW);
247
0
        (void) nErr;
248
249
        // #i121334#
250
0
        ChartHelper::AdaptDefaultsForChart( xObj );
251
0
    }
252
253
0
    uno::Reference< chart2::data::XDataReceiver > xDataReceiver( xChartModel, uno::UNO_QUERY );
254
0
    if (bFillWithData && xDataReceiver.is() && rxDataProvider.is())
255
0
    {
256
0
        xDataReceiver->attachDataProvider( rxDataProvider );
257
258
0
        uno::Reference< util::XNumberFormatsSupplier > xNumberFormatsSupplier( m_pSh->GetView().GetDocShell()->GetModel(), uno::UNO_QUERY );
259
0
        xDataReceiver->attachNumberFormatsSupplier( xNumberFormatsSupplier );
260
261
        // default values for ranges that do not consist of a single row or column
262
0
        bool bHasCategories = true;
263
0
        bool bFirstCellAsLabel = true;
264
0
        chart::ChartDataRowSource eDataRowSource = chart::ChartDataRowSource_COLUMNS;
265
266
0
        SwRangeDescriptor aDesc;
267
0
        FillRangeDescriptor( aDesc, rCellRange );
268
0
        bool bSingleRowCol = aDesc.nTop == aDesc.nBottom || aDesc.nLeft == aDesc.nRight;
269
0
        if (bSingleRowCol)
270
0
        {
271
0
            aDesc.Normalize();
272
0
            sal_Int32 nRowLen = aDesc.nRight  - aDesc.nLeft + 1;
273
0
            sal_Int32 nColLen = aDesc.nBottom - aDesc.nTop + 1;
274
275
0
            bHasCategories = false;
276
0
            if (nRowLen == 1 && nColLen == 1)
277
0
                bFirstCellAsLabel   = false;
278
0
            else if (nRowLen > 1)
279
0
                eDataRowSource = chart::ChartDataRowSource_ROWS;
280
0
            else if (nColLen > 1)
281
0
                eDataRowSource = chart::ChartDataRowSource_COLUMNS;
282
0
            else {
283
0
                OSL_FAIL("unexpected state" );
284
0
            }
285
0
        }
286
287
0
        uno::Sequence< beans::PropertyValue > aArgs{
288
0
            beans::PropertyValue(
289
0
                u"CellRangeRepresentation"_ustr, -1,
290
0
                uno::Any( rCellRange ), beans::PropertyState_DIRECT_VALUE ),
291
0
            beans::PropertyValue(
292
0
                u"HasCategories"_ustr, -1,
293
0
                uno::Any( bHasCategories ), beans::PropertyState_DIRECT_VALUE ),
294
0
            beans::PropertyValue(
295
0
                u"FirstCellAsLabel"_ustr, -1,
296
0
                uno::Any( bFirstCellAsLabel ), beans::PropertyState_DIRECT_VALUE ),
297
0
            beans::PropertyValue(
298
0
                u"DataRowSource"_ustr, -1,
299
0
                uno::Any( eDataRowSource ), beans::PropertyState_DIRECT_VALUE )
300
0
        };
301
0
        xDataReceiver->setArguments( aArgs );
302
0
    }
303
304
0
    m_pSh->EndUndo( SwUndoId::UI_INSERT_CHART );
305
306
0
    if( xChartModel.is() )
307
0
        xChartModel->unlockControllers(); //#i79578# don't request a new replacement image for charts too often
308
0
    return xChartModel;
309
0
}
310
311
sal_uInt16  SwTableFUNC::GetCurColNum() const
312
0
{
313
0
    const size_t nPos = m_pSh->GetCurTabColNum();
314
0
    size_t nCount = 0;
315
0
    for( size_t i = 0; i < nPos; i++ )
316
0
        if(m_aCols.IsHidden(i))
317
0
            nCount ++;
318
0
    return nPos - nCount;
319
0
}
320
321
sal_uInt16  SwTableFUNC::GetColCount() const
322
0
{
323
0
    size_t nCount = 0;
324
0
    for(size_t i = 0; i < m_aCols.Count(); i++ )
325
0
        if(m_aCols.IsHidden(i))
326
0
            nCount ++;
327
0
    return m_aCols.Count() - nCount;
328
0
}
329
330
int SwTableFUNC::GetRightSeparator(int nNum) const
331
0
{
332
0
    OSL_ENSURE( nNum < static_cast<int>(GetColCount()) ,"Index out of range");
333
0
    int i = 0;
334
0
    while( nNum >= 0 )
335
0
    {
336
0
        if( !m_aCols.IsHidden(i) )
337
0
            nNum--;
338
0
        i++;
339
0
    }
340
0
    return i - 1;
341
0
}
342
343
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */