| 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 <res_LegendPosition.hxx> |
| 21 |
#include <Legend.hxx> |
| 22 |
#include <LegendHelper.hxx> |
| 23 |
#include <ChartModel.hxx> |
| 24 |
#include <Diagram.hxx> |
| 25 |
|
| 26 |
#include <com/sun/star/chart2/LegendPosition.hpp> |
| 27 |
#include <com/sun/star/chart/ChartLegendExpansion.hpp> |
| 28 |
|
| 29 |
//itemset stuff |
| 30 |
#include <chartview/ChartSfxItemIds.hxx> |
| 31 |
#include <svl/intitem.hxx> |
| 32 |
#include <svl/eitem.hxx> |
| 33 |
#include <comphelper/diagnose_ex.hxx> |
| 34 |
#include <utility> |
| 35 |
#include <vcl/weld.hxx> |
| 36 |
|
| 37 |
namespace chart |
| 38 |
{ |
| 39 |
|
| 40 |
using namespace ::com::sun::star; |
| 41 |
using namespace ::com::sun::star::chart2; |
| 42 |
|
| 43 |
LegendPositionResources::LegendPositionResources(weld::Builder& rBuilder) |
| 44 |
: m_xRbtLeft(rBuilder.weld_radio_button(u"left"_ustr)) |
| 45 |
, m_xRbtRight(rBuilder.weld_radio_button(u"right"_ustr)) |
| 46 |
, m_xRbtTop(rBuilder.weld_radio_button(u"top"_ustr)) |
| 47 |
, m_xRbtBottom(rBuilder.weld_radio_button(u"bottom"_ustr)) |
| 48 |
{ |
| 49 |
impl_setRadioButtonToggleHdl(); |
| 50 |
} |
| 51 |
|
| 52 |
LegendPositionResources::LegendPositionResources(weld::Builder& rBuilder, |
| 53 |
uno::Reference< uno::XComponentContext > xCC) |
| 54 |
: m_xCC(std::move(xCC)) |
| 55 |
, m_xCbxShow(rBuilder.weld_check_button(u"show"_ustr)) |
| 56 |
, m_xRbtLeft(rBuilder.weld_radio_button(u"left"_ustr)) |
| 57 |
, m_xRbtRight(rBuilder.weld_radio_button(u"right"_ustr)) |
| 58 |
, m_xRbtTop(rBuilder.weld_radio_button(u"top"_ustr)) |
| 59 |
, m_xRbtBottom(rBuilder.weld_radio_button(u"bottom"_ustr)) |
| 60 |
{ |
| 61 |
m_xCbxShow->connect_toggled( LINK( this, LegendPositionResources, PositionEnableHdl ) ); |
| 62 |
impl_setRadioButtonToggleHdl(); |
| 63 |
} |
| 64 |
|
| 65 |
void LegendPositionResources::impl_setRadioButtonToggleHdl() |
| 66 |
{ |
| 67 |
m_xRbtLeft->connect_toggled( LINK( this, LegendPositionResources, PositionChangeHdl ) ); |
| 68 |
m_xRbtTop->connect_toggled( LINK( this, LegendPositionResources, PositionChangeHdl ) ); |
| 69 |
m_xRbtRight->connect_toggled( LINK( this, LegendPositionResources, PositionChangeHdl ) ); |
| 70 |
m_xRbtBottom->connect_toggled( LINK( this, LegendPositionResources, PositionChangeHdl ) ); |
| 71 |
} |
| 72 |
|
| 73 |
LegendPositionResources::~LegendPositionResources() |
| 74 |
{ |
| 75 |
} |
| 76 |
|
| 77 |
void LegendPositionResources::writeToResources( const rtl::Reference<::chart::ChartModel>& xChartModel ) |
| 78 |
{ |
| 79 |
try |
| 80 |
{ |
| 81 |
rtl::Reference< Diagram > xDiagram = xChartModel->getFirstChartDiagram(); |
| 82 |
rtl::Reference< Legend > xLegend = xDiagram->getLegend2(); |
| 83 |
if( xLegend.is() ) |
| 84 |
{ |
| 85 |
//show |
| 86 |
bool bShowLegend = false; |
| 87 |
xLegend->getPropertyValue( u"Show"_ustr ) >>= bShowLegend; |
| 88 |
if (m_xCbxShow) |
| 89 |
m_xCbxShow->set_active( bShowLegend ); |
| 90 |
PositionEnable(); |
| 91 |
|
| 92 |
//position |
| 93 |
chart2::LegendPosition ePos; |
| 94 |
xLegend->getPropertyValue( u"AnchorPosition"_ustr ) >>= ePos; |
| 95 |
switch( ePos ) |
| 96 |
{ |
| 97 |
case chart2::LegendPosition_LINE_START: |
| 98 |
m_xRbtLeft->set_active(true); |
| 99 |
break; |
| 100 |
case chart2::LegendPosition_PAGE_START: |
| 101 |
m_xRbtTop->set_active(true); |
| 102 |
break; |
| 103 |
case chart2::LegendPosition_PAGE_END: |
| 104 |
m_xRbtBottom->set_active(true); |
| 105 |
break; |
| 106 |
case chart2::LegendPosition_LINE_END: |
| 107 |
default: |
| 108 |
m_xRbtRight->set_active(true); |
| 109 |
break; |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
catch( const uno::Exception & ) |
| 114 |
{ |
| 115 |
DBG_UNHANDLED_EXCEPTION("chart2"); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
void LegendPositionResources::writeToModel( const rtl::Reference<::chart::ChartModel>& xChartModel ) const |
| 120 |
{ |
| 121 |
try |
| 122 |
{ |
| 123 |
bool bShowLegend = m_xCbxShow && m_xCbxShow->get_active(); |
| 124 |
ChartModel& rModel = *xChartModel; |
| 125 |
rtl::Reference< Legend > xProp = LegendHelper::getLegend(rModel, m_xCC, bShowLegend); |
| 126 |
if( xProp.is() ) |
| 127 |
{ |
| 128 |
//show |
| 129 |
xProp->setPropertyValue( u"Show"_ustr , uno::Any( bShowLegend )); |
| 130 |
|
| 131 |
//position |
| 132 |
chart2::LegendPosition eNewPos; |
| 133 |
css::chart::ChartLegendExpansion eExp = css::chart::ChartLegendExpansion_HIGH; |
| 134 |
|
| 135 |
if( m_xRbtLeft->get_active() ) |
| 136 |
eNewPos = chart2::LegendPosition_LINE_START; |
| 137 |
else if( m_xRbtRight->get_active() ) |
| 138 |
{ |
| 139 |
eNewPos = chart2::LegendPosition_LINE_END; |
| 140 |
} |
| 141 |
else if( m_xRbtTop->get_active() ) |
| 142 |
{ |
| 143 |
eNewPos = chart2::LegendPosition_PAGE_START; |
| 144 |
eExp = css::chart::ChartLegendExpansion_WIDE; |
| 145 |
} |
| 146 |
else if( m_xRbtBottom->get_active() ) |
| 147 |
{ |
| 148 |
eNewPos = chart2::LegendPosition_PAGE_END; |
| 149 |
eExp = css::chart::ChartLegendExpansion_WIDE; |
| 150 |
} |
| 151 |
|
| 152 |
xProp->setPropertyValue( u"AnchorPosition"_ustr , uno::Any( eNewPos )); |
| 153 |
xProp->setPropertyValue( u"Expansion"_ustr , uno::Any( eExp )); |
| 154 |
xProp->setPropertyValue( u"RelativePosition"_ustr , uno::Any()); |
| 155 |
} |
| 156 |
} |
| 157 |
catch( const uno::Exception & ) |
| 158 |
{ |
| 159 |
DBG_UNHANDLED_EXCEPTION("chart2" ); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
IMPL_LINK_NOARG(LegendPositionResources, PositionEnableHdl, weld::Toggleable&, void) |
| 164 |
{ |
| 165 |
PositionEnable(); |
| 166 |
} |
| 167 |
|
| 168 |
void LegendPositionResources::PositionEnable() |
| 169 |
{ |
| 170 |
bool bEnable = !m_xCbxShow || m_xCbxShow->get_active(); |
| 171 |
|
| 172 |
m_xRbtLeft->set_sensitive( bEnable ); |
| 173 |
m_xRbtTop->set_sensitive( bEnable ); |
| 174 |
m_xRbtRight->set_sensitive( bEnable ); |
| 175 |
m_xRbtBottom->set_sensitive( bEnable ); |
| 176 |
|
| 177 |
m_aChangeLink.Call(nullptr); |
| 178 |
} |
| 179 |
|
| 180 |
void LegendPositionResources::initFromItemSet( const SfxItemSet& rInAttrs ) |
| 181 |
{ |
| 182 |
if( const SfxInt32Item* pPosItem = rInAttrs.GetItemIfSet( SCHATTR_LEGEND_POS ) ) |
| 183 |
{ |
| 184 |
chart2::LegendPosition nLegendPosition = static_cast<chart2::LegendPosition>(pPosItem->GetValue()); |
| 185 |
switch( nLegendPosition ) |
| 186 |
{ |
| 187 |
case chart2::LegendPosition_LINE_START: |
| 188 |
m_xRbtLeft->set_active(true); |
| 189 |
break; |
| 190 |
case chart2::LegendPosition_PAGE_START: |
| 191 |
m_xRbtTop->set_active(true); |
| 192 |
break; |
| 193 |
case chart2::LegendPosition_LINE_END: |
| 194 |
m_xRbtRight->set_active(true); |
| 195 |
break; |
| 196 |
case chart2::LegendPosition_PAGE_END: |
| 197 |
m_xRbtBottom->set_active(true); |
| 198 |
break; |
| 199 |
default: |
| 200 |
break; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
const SfxBoolItem* pShowItem; |
| 205 |
if( m_xCbxShow && (pShowItem = rInAttrs.GetItemIfSet( SCHATTR_LEGEND_SHOW )) ) |
| 206 |
{ |
| 207 |
m_xCbxShow->set_active(pShowItem->GetValue()); |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
void LegendPositionResources::writeToItemSet( SfxItemSet& rOutAttrs ) const |
| 212 |
{ |
| 213 |
chart2::LegendPosition nLegendPosition = chart2::LegendPosition_LINE_END; |
| 214 |
if( m_xRbtLeft->get_active() ) |
| 215 |
nLegendPosition = chart2::LegendPosition_LINE_START; |
| 216 |
else if( m_xRbtTop->get_active() ) |
| 217 |
nLegendPosition = chart2::LegendPosition_PAGE_START; |
| 218 |
else if( m_xRbtRight->get_active() ) |
| 219 |
nLegendPosition = chart2::LegendPosition_LINE_END; |
| 220 |
else if( m_xRbtBottom->get_active() ) |
| 221 |
nLegendPosition = chart2::LegendPosition_PAGE_END; |
| 222 |
rOutAttrs.Put( SfxInt32Item(SCHATTR_LEGEND_POS, static_cast<sal_Int32>(nLegendPosition) ) ); |
| 223 |
|
| 224 |
rOutAttrs.Put( SfxBoolItem(SCHATTR_LEGEND_SHOW, !m_xCbxShow || m_xCbxShow->get_active()) ); |
| 225 |
} |
| 226 |
|
| 227 |
IMPL_LINK (LegendPositionResources, PositionChangeHdl, weld::Toggleable&, rRadio, void) |
| 228 |
{ |
| 229 |
//for each radio click there are coming two change events |
| 230 |
//first uncheck of previous button -> ignore that call |
| 231 |
//the second call gives the check of the new button |
| 232 |
if( rRadio.get_active() ) |
| 233 |
m_aChangeLink.Call(nullptr); |
| 234 |
} |
| 235 |
|
| 236 |
void LegendPositionResources::SetChangeHdl( const Link<LinkParamNone*,void>& rLink ) |
| 237 |
{ |
| 238 |
m_aChangeLink = rLink; |
| 239 |
} |
| 240 |
|
| 241 |
} //namespace chart |
| 242 |
|
| 243 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |