Use correct object
[LibreOffice.git] / cui / source / options / optchart.cxx
blob4a457b167770702c0b94b76c1deced87c32949cf
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "optchart.hxx"
21 #include <svx/SvxColorValueSet.hxx>
22 #include <vcl/virdev.hxx>
23 #include <vcl/weld.hxx>
24 #include <vcl/settings.hxx>
25 #include <vcl/svapp.hxx>
26 #include <svx/svxids.hrc>
27 #include <osl/diagnose.h>
28 #include <officecfg/Office/Common.hxx>
29 #include <officecfg/Office/Chart.hxx>
31 void SvxDefaultColorOptPage::InsertColorEntry(const XColorEntry& rEntry, sal_Int32 nPos)
33 const Color& rColor = rEntry.GetColor();
34 const OUString& rStr = rEntry.GetName();
36 const StyleSettings& rStyleSettings = Application::GetSettings().GetStyleSettings();
37 Size aImageSize = rStyleSettings.GetListBoxPreviewDefaultPixelSize();
39 ScopedVclPtrInstance<VirtualDevice> xDevice;
40 xDevice->SetOutputSize(aImageSize);
41 const ::tools::Rectangle aRect(Point(0, 0), aImageSize);
42 xDevice->SetFillColor(rColor);
43 xDevice->SetLineColor(rStyleSettings.GetDisableColor());
44 xDevice->DrawRect(aRect);
46 m_xLbChartColors->insert(nullptr, nPos, &rStr, nullptr,
47 nullptr, xDevice.get(), false, nullptr);
49 if (nPos == -1)
50 aColorList.push_back( rColor );
51 else
53 ImpColorList::iterator it = aColorList.begin();
54 std::advance( it, nPos );
55 aColorList.insert( it, rColor );
59 void SvxDefaultColorOptPage::RemoveColorEntry(sal_Int32 nPos)
61 m_xLbChartColors->remove(nPos);
62 ImpColorList::iterator it = aColorList.begin();
63 std::advance(it, nPos);
64 aColorList.erase(it);
67 void SvxDefaultColorOptPage::ClearColorEntries()
69 aColorList.clear();
70 m_xLbChartColors->clear();
73 void SvxDefaultColorOptPage::ModifyColorEntry(const XColorEntry& rEntry, sal_Int32 nPos)
75 RemoveColorEntry(nPos);
76 InsertColorEntry(rEntry, nPos);
79 void SvxDefaultColorOptPage::FillBoxChartColorLB()
81 if (!m_SvxChartColorTableUniquePtr)
82 return;
84 m_xLbChartColors->freeze();
85 ClearColorEntries();
86 const tools::Long nCount(m_SvxChartColorTableUniquePtr->size());
87 for (tools::Long i = 0; i < nCount; ++i)
88 InsertColorEntry((*m_SvxChartColorTableUniquePtr)[i]);
89 m_xLbChartColors->thaw();
92 SvxDefaultColorOptPage::SvxDefaultColorOptPage(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet& rInAttrs)
93 : SfxTabPage(pPage, pController, u"cui/ui/optchartcolorspage.ui"_ustr, u"OptChartColorsPage"_ustr, &rInAttrs)
94 , m_xLbChartColors(m_xBuilder->weld_tree_view(u"colors"_ustr))
95 , m_xLbPaletteSelector(m_xBuilder->weld_combo_box(u"paletteselector"_ustr))
96 , m_xPBDefault(m_xBuilder->weld_button(u"default"_ustr))
97 , m_xPBAdd(m_xBuilder->weld_button(u"add"_ustr))
98 , m_xPBRemove(m_xBuilder->weld_button(u"delete"_ustr))
99 , m_xValSetColorBox(new SvxColorValueSet(m_xBuilder->weld_scrolled_window(u"tablewin"_ustr, true)))
100 , m_xValSetColorBoxWin(new weld::CustomWeld(*m_xBuilder, u"table"_ustr, *m_xValSetColorBox))
102 m_xLbChartColors->set_size_request(-1, m_xLbChartColors->get_height_rows(16));
104 if (officecfg::Office::Chart::DefaultColor::Series::isReadOnly())
106 m_xPBDefault->set_sensitive(false);
107 m_xPBAdd->set_sensitive(false);
108 m_xPBRemove->set_sensitive(false);
109 m_xValSetColorBoxWin->set_sensitive(false);
112 m_xPBDefault->connect_clicked( LINK( this, SvxDefaultColorOptPage, ResetToDefaults ) );
113 m_xPBAdd->connect_clicked( LINK( this, SvxDefaultColorOptPage, AddChartColor ) );
114 m_xPBRemove->connect_clicked( LINK( this, SvxDefaultColorOptPage, RemoveChartColor ) );
115 m_xValSetColorBox->SetSelectHdl( LINK( this, SvxDefaultColorOptPage, BoxClickedHdl ) );
116 m_xLbPaletteSelector->connect_changed( LINK( this, SvxDefaultColorOptPage, SelectPaletteLbHdl ) );
118 m_xValSetColorBox->SetStyle( m_xValSetColorBox->GetStyle()
119 | WB_ITEMBORDER | WB_NAMEFIELD | WB_VSCROLL );
121 if ( const SvxChartColorTableItem* pEditOptionsItem = rInAttrs.GetItemIfSet( SID_SCH_EDITOPTIONS, false ) )
123 m_SvxChartColorTableUniquePtr = std::make_unique<SvxChartColorTable>(
124 pEditOptionsItem->GetColorList());
126 else
128 m_SvxChartColorTableUniquePtr = std::make_unique<SvxChartColorTable>();
129 m_SvxChartColorTableUniquePtr->useDefault();
130 SvxChartOptions::SetDefaultColors(*m_SvxChartColorTableUniquePtr);
133 Construct();
136 SvxDefaultColorOptPage::~SvxDefaultColorOptPage()
138 m_xValSetColorBoxWin.reset();
139 m_xValSetColorBox.reset();
142 void SvxDefaultColorOptPage::Construct()
144 FillBoxChartColorLB();
145 FillPaletteLB();
147 m_xLbChartColors->select( 0 );
150 std::unique_ptr<SfxTabPage> SvxDefaultColorOptPage::Create( weld::Container* pPage, weld::DialogController* pController, const SfxItemSet* rAttrs )
152 return std::make_unique<SvxDefaultColorOptPage>( pPage, pController, *rAttrs );
155 OUString SvxDefaultColorOptPage::GetAllStrings()
157 OUString sAllStrings;
158 OUString labels[] = { u"label20"_ustr, u"label1"_ustr };
160 for (const auto& label : labels)
162 if (const auto pString = m_xBuilder->weld_label(label))
163 sAllStrings += pString->get_label() + " ";
166 OUString buttons[] = { u"add"_ustr, u"delete"_ustr, u"default"_ustr };
168 for (const auto& btn : buttons)
170 if (const auto pString = m_xBuilder->weld_button(btn))
171 sAllStrings += pString->get_label() + " ";
174 return sAllStrings.replaceAll("_", "");
177 bool SvxDefaultColorOptPage::FillItemSet( SfxItemSet* rOutAttrs )
179 if( m_SvxChartColorTableUniquePtr )
181 rOutAttrs->Put(SvxChartColorTableItem(SID_SCH_EDITOPTIONS, *m_SvxChartColorTableUniquePtr));
184 return true;
187 void SvxDefaultColorOptPage::Reset( const SfxItemSet* )
189 m_xLbChartColors->select( 0 );
192 void SvxDefaultColorOptPage::FillPaletteLB()
194 m_xLbPaletteSelector->clear();
195 std::vector<OUString> aPaletteList = aPaletteManager.GetPaletteList();
196 for (auto const& palette : aPaletteList)
197 m_xLbPaletteSelector->append_text(palette);
199 OUString aPaletteName(officecfg::Office::Common::UserColors::PaletteName::get());
200 m_xLbPaletteSelector->set_active_text(aPaletteName);
201 if (m_xLbPaletteSelector->get_active() != -1)
202 SelectPaletteLbHdl( *m_xLbPaletteSelector );
205 void SvxDefaultColorOptPage::SaveChartOptions()
207 if (m_SvxChartColorTableUniquePtr)
208 SvxChartOptions::SetDefaultColors(*m_SvxChartColorTableUniquePtr);
211 // event handlers
214 // ResetToDefaults
215 IMPL_LINK_NOARG(SvxDefaultColorOptPage, ResetToDefaults, weld::Button&, void)
217 if( m_SvxChartColorTableUniquePtr )
219 m_SvxChartColorTableUniquePtr->useDefault();
221 FillBoxChartColorLB();
223 m_xLbChartColors->grab_focus();
224 m_xLbChartColors->select( 0 );
225 m_xPBRemove->set_sensitive(true);
229 // AddChartColor
230 IMPL_LINK_NOARG(SvxDefaultColorOptPage, AddChartColor, weld::Button&, void)
232 if( m_SvxChartColorTableUniquePtr )
234 Color const black( 0x00, 0x00, 0x00 );
236 m_SvxChartColorTableUniquePtr->append(
237 XColorEntry(black, SvxChartColorTable::getDefaultName(m_SvxChartColorTableUniquePtr->size())));
239 FillBoxChartColorLB();
240 m_xLbChartColors->grab_focus();
241 m_xLbChartColors->select(m_SvxChartColorTableUniquePtr->size() - 1);
242 m_xPBRemove->set_sensitive(true);
246 // RemoveChartColor
247 IMPL_LINK_NOARG( SvxDefaultColorOptPage, RemoveChartColor, weld::Button&, void )
249 sal_Int32 nIndex = m_xLbChartColors->get_selected_index();
250 if (nIndex == -1)
251 return;
253 if( !m_SvxChartColorTableUniquePtr )
254 return;
256 OSL_ENSURE(m_SvxChartColorTableUniquePtr->size() > 1, "don't delete the last chart color");
258 std::unique_ptr<weld::Builder> xBuilder(Application::CreateBuilder(GetFrameWeld(), u"cui/ui/querydeletechartcolordialog.ui"_ustr));
259 std::unique_ptr<weld::MessageDialog> xQuery(xBuilder->weld_message_dialog(u"QueryDeleteChartColorDialog"_ustr));
261 if (RET_YES != xQuery->run())
262 return;
264 m_SvxChartColorTableUniquePtr->remove(nIndex);
266 FillBoxChartColorLB();
268 m_xLbChartColors->grab_focus();
270 if (nIndex == m_xLbChartColors->n_children() && m_xLbChartColors->n_children() > 0)
271 m_xLbChartColors->select(m_SvxChartColorTableUniquePtr->size() - 1);
272 else if (m_xLbChartColors->n_children() > 0)
273 m_xLbChartColors->select( nIndex );
274 else
275 m_xPBRemove->set_sensitive(true);
278 IMPL_LINK_NOARG( SvxDefaultColorOptPage, SelectPaletteLbHdl, weld::ComboBox&, void)
280 sal_Int32 nPos = m_xLbPaletteSelector->get_active();
281 aPaletteManager.SetPalette( nPos );
282 aPaletteManager.ReloadColorSet( *m_xValSetColorBox );
283 m_xValSetColorBox->Resize();
286 IMPL_LINK_NOARG(SvxDefaultColorOptPage, BoxClickedHdl, ValueSet*, void)
288 sal_Int32 nIdx = m_xLbChartColors->get_selected_index();
289 if (nIdx != -1)
291 const XColorEntry aEntry(m_xValSetColorBox->GetItemColor(m_xValSetColorBox->GetSelectedItemId()), m_xLbChartColors->get_selected_text());
293 ModifyColorEntry(aEntry, nIdx);
294 m_SvxChartColorTableUniquePtr->replace(nIdx, aEntry);
296 m_xLbChartColors->select(nIdx); // reselect entry
300 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */