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
/* -*- 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/.
 */

#include "ChartAreaPanel.hxx"

#include <ChartController.hxx>
#include <ViewElementListProvider.hxx>
#include <PropertyHelper.hxx>

#include <chartview/DrawModelWrapper.hxx>

#include <editeng/memberids.h>
#include <svx/xfltrit.hxx>
#include <svx/xflftrit.hxx>
#include <svx/xbtmpit.hxx>
#include <svx/unomid.hxx>
#include <vcl/svapp.hxx>

#include <svx/tbcontrl.hxx>

namespace chart { namespace sidebar {

namespace {

SvxColorToolBoxControl* getColorToolBoxControl(sfx2::sidebar::SidebarToolBox* pToolBoxColor)
{
    css::uno::Reference<css::frame::XToolbarController> xController = pToolBoxColor->GetFirstController();
    SvxColorToolBoxControl* pToolBoxColorControl = dynamic_cast<SvxColorToolBoxControl*>(xController.get());
    return pToolBoxColorControl;
}

OUString getCID(const css::uno::Reference<css::frame::XModel>& xModel)
{
    css::uno::Reference<css::frame::XController> xController(xModel->getCurrentController());
    css::uno::Reference<css::view::XSelectionSupplier> xSelectionSupplier(xController, css::uno::UNO_QUERY);
    if (!xSelectionSupplier.is())
        return OUString();

    css::uno::Any aAny = xSelectionSupplier->getSelection();
    if (!aAny.hasValue())
        return OUString();

    OUString aCID;
    aAny >>= aCID;

    return aCID;
}

css::uno::Reference<css::beans::XPropertySet> getPropSet(
        const css::uno::Reference<css::frame::XModel>& xModel)
{
    OUString aCID = getCID(xModel);
    css::uno::Reference<css::beans::XPropertySet> xPropSet =
        ObjectIdentifier::getObjectPropertySet(aCID, xModel);

    ObjectType eType = ObjectIdentifier::getObjectType(aCID);
    if (eType == OBJECTTYPE_DIAGRAM)
    {
        css::uno::Reference<css::chart2::XDiagram> xDiagram(
                xPropSet, css::uno::UNO_QUERY);
        if (!xDiagram.is())
            return xPropSet;

        xPropSet.set(xDiagram->getWall());
    }

    return xPropSet;
}

ChartController* getController(const css::uno::Reference<css::frame::XModel>& xModel)
{
    css::uno::Reference<css::frame::XController>xController = xModel->getCurrentController();
    if (!xController.is())
        throw std::exception();

    ChartController* pController = dynamic_cast<ChartController*>(xController.get());
    if (!pController)
        throw std::exception();

    return pController;
}

ViewElementListProvider getViewElementListProvider( const css::uno::Reference<css::frame::XModel>& xModel)
{
    ChartController* pController = getController(xModel);
    ViewElementListProvider aProvider = pController->getViewElementListProvider();
    return aProvider;
}

DrawModelWrapper* getDrawModelWrapper(const css::uno::Reference<css::frame::XModel>& xModel)
{
    ChartController* pController = getController(xModel);
    return pController->GetDrawModelWrapper();
}

XGradient getXGradientForName(const css::uno::Reference<css::frame::XModel>& xModel,
        const OUString& rName)
{
    try
    {
        ViewElementListProvider aProvider = getViewElementListProvider(xModel);
        XGradientListRef aRef = aProvider.GetGradientList();
        size_t n = aRef->Count();
        for (size_t i = 0; i < n; ++i)
        {
            const XGradientEntry* pGradient = aRef->GetGradient(i);
            if (!pGradient)
                continue;

            if (pGradient->GetName() == rName)
                return pGradient->GetGradient();
        }
    }
    catch (...)
    {
        // ignore exception
    }

    return XGradient();
}

XFillFloatTransparenceItem getXTransparencyGradientForName(const css::uno::Reference<css::frame::XModel>& xModel,
        const OUString& rName)
{
    css::uno::Reference<css::lang::XMultiServiceFactory> xFact(xModel, css::uno::UNO_QUERY);
    css::uno::Reference<css::container::XNameAccess> xNameAccess(
            xFact->createInstance("com.sun.star.drawing.TransparencyGradientTable"), css::uno::UNO_QUERY);
    if (!xNameAccess.is())
        return XFillFloatTransparenceItem();

    if (!xNameAccess->hasByName(rName))
        return XFillFloatTransparenceItem();

    css::uno::Any aAny = xNameAccess->getByName(rName);

    XFillFloatTransparenceItem aItem;
    aItem.SetName(rName);
    aItem.PutValue(aAny, MID_FILLGRADIENT);
    aItem.SetEnabled(true);

    return aItem;
}

XHatch getXHatchFromName(const css::uno::Reference<css::frame::XModel>& xModel,
        OUString& rName)
{
    try
    {
        ViewElementListProvider aProvider = getViewElementListProvider(xModel);
        XHatchListRef aRef = aProvider.GetHatchList();
        size_t n = aRef->Count();
        for (size_t i = 0; i < n; ++i)
        {
            const XHatchEntry* pHatch = aRef->GetHatch(i);
            if (!pHatch)
                continue;

            if (pHatch->GetName().equalsIgnoreAsciiCase(rName))
            {
                // we need to update the hatch name
                rName = pHatch->GetName();
                return pHatch->GetHatch();
            }
        }
    }
    catch (...)
    {
        // ignore exception
    }

    return XHatch();
}

GraphicObject getXBitmapFromName(const css::uno::Reference<css::frame::XModel>& xModel,
        const OUString& rName)
{
    try
    {
        ViewElementListProvider aProvider = getViewElementListProvider(xModel);
        XBitmapListRef aBmpRef = aProvider.GetBitmapList();
        XPatternListRef aPatRef = aProvider.GetPatternList();

        size_t n = aBmpRef->Count();
        for (size_t i = 0; i < n; ++i)
        {
            const XBitmapEntry* pBitmap = aBmpRef->GetBitmap(i);
            if (!pBitmap)
                continue;

            if (pBitmap->GetName().equalsIgnoreAsciiCase(rName))
            {
                return pBitmap->GetGraphicObject();
            }
        }

        // perhaps it's a pattern
        size_t m = aPatRef->Count();
        for (size_t i = 0; i < m; ++i)
        {
            const XBitmapEntry* pBitmap = aPatRef->GetBitmap(i);
            if (!pBitmap)
                continue;

            if (pBitmap->GetName().equalsIgnoreAsciiCase(rName))
            {
                return pBitmap->GetGraphicObject();
            }
        }
    }
    catch (...)
    {
        // ignore exception
    }

    return GraphicObject();
}

class PreventUpdate
{
public:
    explicit PreventUpdate(bool& bUpdate):
        mbUpdate(bUpdate)
    {
        mbUpdate = false;
    }

    ~PreventUpdate()
    {
        mbUpdate = true;
    }

private:
    bool& mbUpdate;
};

}

VclPtr<vcl::Window> ChartAreaPanel::Create(
        vcl::Window* pParent,
        const css::uno::Reference<css::frame::XFrame>& rxFrame,
        ChartController* pController)
{
    if (pParent == nullptr)
        throw css::lang::IllegalArgumentException("no parent Window given to ChartAxisPanel::Create", nullptr, 0);
    if (!rxFrame.is())
        throw css::lang::IllegalArgumentException("no XFrame given to ChartAxisPanel::Create", nullptr, 1);

    return VclPtr<ChartAreaPanel>::Create(
                        pParent, rxFrame, pController);
}

ChartAreaPanel::ChartAreaPanel(vcl::Window* pParent,
        const css::uno::Reference<css::frame::XFrame>& rxFrame,
        ChartController* pController):
    svx::sidebar::AreaPropertyPanelBase(pParent, rxFrame),
    mxModel(pController->getModel()),
    mxListener(new ChartSidebarModifyListener(this)),
    mxSelectionListener(new ChartSidebarSelectionListener(this)),
    mbUpdate(true),
    mbModelValid(true),
    maFillColorWrapper(mxModel, getColorToolBoxControl(mpToolBoxColor.get()), "FillColor")
{
    std::vector<ObjectType> aAcceptedTypes { OBJECTTYPE_PAGE, OBJECTTYPE_DIAGRAM,
        OBJECTTYPE_DATA_SERIES, OBJECTTYPE_DATA_POINT,
        OBJECTTYPE_TITLE, OBJECTTYPE_LEGEND};
    mxSelectionListener->setAcceptedTypes(aAcceptedTypes);
    Initialize();<--- Virtual function 'updateData' is called from constructor 'ChartAreaPanel(vcl::Window*pParent,const css::uno::Reference&rxFrame,ChartController*pController)' at line 273. Dynamic binding is not used.
}

ChartAreaPanel::~ChartAreaPanel()
{
    disposeOnce();
}

void ChartAreaPanel::dispose()
{
    css::uno::Reference<css::util::XModifyBroadcaster> xBroadcaster(mxModel, css::uno::UNO_QUERY_THROW);
    xBroadcaster->removeModifyListener(mxListener);

    css::uno::Reference<css::view::XSelectionSupplier> xSelectionSupplier(mxModel->getCurrentController(), css::uno::UNO_QUERY);
    if (xSelectionSupplier.is())
        xSelectionSupplier->removeSelectionChangeListener(mxSelectionListener.get());

    AreaPropertyPanelBase::dispose();
}

void ChartAreaPanel::Initialize()
{
    css::uno::Reference<css::util::XModifyBroadcaster> xBroadcaster(mxModel, css::uno::UNO_QUERY_THROW);
    xBroadcaster->addModifyListener(mxListener);

    css::uno::Reference<css::view::XSelectionSupplier> xSelectionSupplier(mxModel->getCurrentController(), css::uno::UNO_QUERY);
    if (xSelectionSupplier.is())
        xSelectionSupplier->addSelectionChangeListener(mxSelectionListener.get());

    SvxColorToolBoxControl* pToolBoxColor = getColorToolBoxControl(mpToolBoxColor.get());
    pToolBoxColor->setColorSelectFunction(maFillColorWrapper);

    updateData();
}

void ChartAreaPanel::setFillTransparence(const XFillTransparenceItem& rItem)<--- The function 'setFillTransparence' is never used.
{
    PreventUpdate aProtector(mbUpdate);
    css::uno::Reference<css::beans::XPropertySet> xPropSet = getPropSet(mxModel);
    if (!xPropSet.is())
        return;

    xPropSet->setPropertyValue("FillTransparence", css::uno::Any(rItem.GetValue()));
}

void ChartAreaPanel::setFillFloatTransparence(<--- The function 'setFillFloatTransparence' is never used.
        const XFillFloatTransparenceItem& rItem)
{
    PreventUpdate aProtector(mbUpdate);
    css::uno::Reference<css::beans::XPropertySet> xPropSet = getPropSet(mxModel);
    if (!xPropSet.is())
        return;

    if (!rItem.IsEnabled())
    {
        xPropSet->setPropertyValue("FillTransparenceGradientName", css::uno::Any(OUString()));
        return;
    }

    OUString aName = rItem.GetName();
    css::uno::Any aGradientVal;
    rItem.QueryValue(aGradientVal, MID_FILLGRADIENT);
    OUString aNewName = PropertyHelper::addTransparencyGradientUniqueNameToTable(aGradientVal, css::uno::Reference<css::lang::XMultiServiceFactory>(mxModel, css::uno::UNO_QUERY_THROW), aName);
    xPropSet->setPropertyValue("FillTransparenceGradientName", css::uno::Any(aNewName));
}

void ChartAreaPanel::setFillStyle(const XFillStyleItem& rItem)
{
    PreventUpdate aProtector(mbUpdate);
    css::uno::Reference<css::beans::XPropertySet> xPropSet = getPropSet(mxModel);
    if (!xPropSet.is())
        return;

    xPropSet->setPropertyValue("FillStyle", css::uno::Any(rItem.GetValue()));
}

void ChartAreaPanel::setFillStyleAndColor(const XFillStyleItem* pStyleItem,<--- The function 'setFillStyleAndColor' is never used.
        const XFillColorItem& rColorItem)
{
    css::uno::Reference<css::beans::XPropertySet> xPropSet = getPropSet(mxModel);
    if (!xPropSet.is())
        return;

    if (pStyleItem)
        xPropSet->setPropertyValue("FillStyle", css::uno::Any(pStyleItem->GetValue()));
    xPropSet->setPropertyValue("FillColor", css::uno::Any(rColorItem.GetValue()));
}

void ChartAreaPanel::setFillStyleAndGradient(const XFillStyleItem* pStyleItem,<--- The function 'setFillStyleAndGradient' is never used.
        const XFillGradientItem& rGradientItem)
{
    PreventUpdate aProtector(mbUpdate);
    css::uno::Reference<css::beans::XPropertySet> xPropSet = getPropSet(mxModel);
    if (!xPropSet.is())
        return;

    if (pStyleItem)
        xPropSet->setPropertyValue("FillStyle", css::uno::Any(pStyleItem->GetValue()));
    xPropSet->setPropertyValue("FillGradientName", css::uno::Any(rGradientItem.GetValue()));
}

void ChartAreaPanel::setFillStyleAndHatch(const XFillStyleItem* pStyleItem,<--- The function 'setFillStyleAndHatch' is never used.
        const XFillHatchItem& rHatchItem)
{
    PreventUpdate aProtector(mbUpdate);
    css::uno::Reference<css::beans::XPropertySet> xPropSet = getPropSet(mxModel);
    if (!xPropSet.is())
        return;

    if (pStyleItem)
        xPropSet->setPropertyValue("FillStyle", css::uno::Any(pStyleItem->GetValue()));
    xPropSet->setPropertyValue("FillHatchName", css::uno::Any(rHatchItem.GetValue()));
}

void ChartAreaPanel::setFillStyleAndBitmap(const XFillStyleItem* pStyleItem,<--- The function 'setFillStyleAndBitmap' is never used.
        const XFillBitmapItem& rBitmapItem)
{
    PreventUpdate aProtector(mbUpdate);
    css::uno::Reference<css::beans::XPropertySet> xPropSet = getPropSet(mxModel);
    if (!xPropSet.is())
        return;

    if (pStyleItem)
        xPropSet->setPropertyValue("FillStyle", css::uno::Any(pStyleItem->GetValue()));

    css::uno::Any aBitmap;
    rBitmapItem.QueryValue(aBitmap, MID_BITMAP);
    OUString aPreferredName = rBitmapItem.GetName();
    aBitmap <<= PropertyHelper::addBitmapUniqueNameToTable(aBitmap, css::uno::Reference<css::lang::XMultiServiceFactory>(mxModel, css::uno::UNO_QUERY_THROW), aPreferredName);
    xPropSet->setPropertyValue("FillBitmapName", aBitmap);
}

void ChartAreaPanel::updateData()
{
    if (!mbUpdate || !mbModelValid)
        return;

    css::uno::Reference<css::beans::XPropertySet> xPropSet = getPropSet(mxModel);
    if (!xPropSet.is())
        return;

    css::uno::Reference<css::beans::XPropertySetInfo> xInfo(xPropSet->getPropertySetInfo());
    if (!xInfo.is())
        return;

    SolarMutexGuard aGuard;
    if (xInfo->hasPropertyByName("FillStyle"))
    {
        css::drawing::FillStyle eFillStyle = css::drawing::FillStyle_SOLID;
        xPropSet->getPropertyValue("FillStyle") >>= eFillStyle;
        XFillStyleItem aFillStyleItem(eFillStyle);
        updateFillStyle(false, true, &aFillStyleItem);
    }

    if (xInfo->hasPropertyByName("FillTransparence"))
    {
        sal_uInt16 nFillTransparence = 0;
        xPropSet->getPropertyValue("FillTransparence") >>= nFillTransparence;
        SfxUInt16Item aTransparenceItem(0, nFillTransparence);
        updateFillTransparence(false, true, &aTransparenceItem);
    }

    if (xInfo->hasPropertyByName("FillGradientName"))
    {
       OUString aGradientName;
       xPropSet->getPropertyValue("FillGradientName") >>= aGradientName;
       XGradient aGradient = getXGradientForName(mxModel, aGradientName);
       XFillGradientItem aGradientItem(aGradientName, aGradient);
       updateFillGradient(false, true, &aGradientItem);
    }

    if (xInfo->hasPropertyByName("FillHatchName"))
    {
        OUString aHatchName;
        xPropSet->getPropertyValue("FillHatchName") >>= aHatchName;
        XHatch aHatch = getXHatchFromName(mxModel, aHatchName);
        XFillHatchItem aHatchItem(aHatchName, aHatch);
        updateFillHatch(false, true, &aHatchItem);
    }

    if (xInfo->hasPropertyByName("FillBitmapName"))
    {
        OUString aBitmapName;
        xPropSet->getPropertyValue("FillBitmapName") >>= aBitmapName;
        GraphicObject aBitmap = getXBitmapFromName(mxModel, aBitmapName);
        XFillBitmapItem aBitmapItem(aBitmapName, aBitmap);
        XFillBitmapItem* pBitmapItem = nullptr;
        DrawModelWrapper* pModelWrapper = nullptr;
        try
        {
            pModelWrapper = getDrawModelWrapper(mxModel);
            if (pModelWrapper)
            {
                pBitmapItem = aBitmapItem.checkForUniqueItem(&pModelWrapper->getSdrModel());
            }
        }
        catch (...)
        {
        }
        updateFillBitmap(false, true, pBitmapItem ? pBitmapItem : &aBitmapItem);
        delete pBitmapItem;
    }

    if (xInfo->hasPropertyByName("FillTransparenceGradientName"))
    {
        OUString aFillFloatTransparenceName;
        xPropSet->getPropertyValue("FillTransparenceGradientName") >>= aFillFloatTransparenceName;
        XFillFloatTransparenceItem aFillFloatTransparenceItem = getXTransparencyGradientForName(mxModel, aFillFloatTransparenceName);
        updateFillFloatTransparence(false, true, &aFillFloatTransparenceItem);

        maFillColorWrapper.updateData();
    }

    if (xInfo->hasPropertyByName("FillColor"))
    {
        sal_uInt32 nFillColor = 0;
        xPropSet->getPropertyValue("FillColor") >>= nFillColor;
        XFillColorItem aFillColorItem("", Color(nFillColor));
        updateFillColor(true, &aFillColorItem);
    }
}

void ChartAreaPanel::modelInvalid()
{
    mbModelValid = false;
}

void ChartAreaPanel::selectionChanged(bool bCorrectType)
{
    if (bCorrectType)
        updateData();
}

void ChartAreaPanel::SelectionInvalid()
{
}

void ChartAreaPanel::updateModel(
        css::uno::Reference<css::frame::XModel> xModel)
{
    if (mbModelValid)
    {
        css::uno::Reference<css::util::XModifyBroadcaster> xBroadcaster(mxModel, css::uno::UNO_QUERY_THROW);
        xBroadcaster->removeModifyListener(mxListener);
    }

    mxModel = xModel;
    mbModelValid = true;

    css::uno::Reference<css::util::XModifyBroadcaster> xBroadcasterNew(mxModel, css::uno::UNO_QUERY_THROW);
    xBroadcasterNew->addModifyListener(mxListener);

    css::uno::Reference<css::view::XSelectionSupplier> xSelectionSupplier(mxModel->getCurrentController(), css::uno::UNO_QUERY);
    if (xSelectionSupplier.is())
        xSelectionSupplier->addSelectionChangeListener(mxSelectionListener.get());
}


} }

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