Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / cui / source / options / optchart.cxx
blob5d6b15e9991f97507b232054a534598da75e6989
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>
30 void SvxDefaultColorOptPage::InsertColorEntry(const XColorEntry& rEntry, sal_Int32 nPos)
32 const Color& rColor = rEntry.GetColor();
33 const OUString& rStr = rEntry.GetName();
35 const StyleSettings& rStyleSettings = Application::GetSettings().GetStyleSettings();
36 Size aImageSize = rStyleSettings.GetListBoxPreviewDefaultPixelSize();
38 ScopedVclPtrInstance<VirtualDevice> xDevice;
39 xDevice->SetOutputSize(aImageSize);
40 const ::tools::Rectangle aRect(Point(0, 0), aImageSize);
41 xDevice->SetFillColor(rColor);
42 xDevice->SetLineColor(rStyleSettings.GetDisableColor());
43 xDevice->DrawRect(aRect);
45 m_xLbChartColors->insert(nullptr, nPos, &rStr, nullptr,
46 nullptr, xDevice.get(), false, nullptr);
48 if (nPos == -1)
49 aColorList.push_back( rColor );
50 else
52 ImpColorList::iterator it = aColorList.begin();
53 std::advance( it, nPos );
54 aColorList.insert( it, rColor );
58 void SvxDefaultColorOptPage::RemoveColorEntry(sal_Int32 nPos)
60 m_xLbChartColors->remove(nPos);
61 ImpColorList::iterator it = aColorList.begin();
62 std::advance(it, nPos);
63 aColorList.erase(it);
66 void SvxDefaultColorOptPage::ClearColorEntries()
68 aColorList.clear();
69 m_xLbChartColors->clear();
72 void SvxDefaultColorOptPage::ModifyColorEntry(const XColorEntry& rEntry, sal_Int32 nPos)
74 RemoveColorEntry(nPos);
75 InsertColorEntry(rEntry, nPos);
78 void SvxDefaultColorOptPage::FillBoxChartColorLB()
80 if (!m_SvxChartColorTableUniquePtr)
81 return;
83 m_xLbChartColors->freeze();
84 ClearColorEntries();
85 const tools::Long nCount(m_SvxChartColorTableUniquePtr->size());
86 for (tools::Long i = 0; i < nCount; ++i)
87 InsertColorEntry((*m_SvxChartColorTableUniquePtr)[i]);
88 m_xLbChartColors->thaw();
91 SvxDefaultColorOptPage::SvxDefaultColorOptPage(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet& rInAttrs)
92 : SfxTabPage(pPage, pController, "cui/ui/optchartcolorspage.ui", "OptChartColorsPage", &rInAttrs)
93 , m_xLbChartColors(m_xBuilder->weld_tree_view("colors"))
94 , m_xLbPaletteSelector(m_xBuilder->weld_combo_box("paletteselector"))
95 , m_xPBDefault(m_xBuilder->weld_button("default"))
96 , m_xPBAdd(m_xBuilder->weld_button("add"))
97 , m_xPBRemove(m_xBuilder->weld_button("delete"))
98 , m_xValSetColorBox(new SvxColorValueSet(m_xBuilder->weld_scrolled_window("tablewin", true)))
99 , m_xValSetColorBoxWin(new weld::CustomWeld(*m_xBuilder, "table", *m_xValSetColorBox))
101 m_xLbChartColors->set_size_request(-1, m_xLbChartColors->get_height_rows(16));
103 m_xPBDefault->connect_clicked( LINK( this, SvxDefaultColorOptPage, ResetToDefaults ) );
104 m_xPBAdd->connect_clicked( LINK( this, SvxDefaultColorOptPage, AddChartColor ) );
105 m_xPBRemove->connect_clicked( LINK( this, SvxDefaultColorOptPage, RemoveChartColor ) );
106 m_xValSetColorBox->SetSelectHdl( LINK( this, SvxDefaultColorOptPage, BoxClickedHdl ) );
107 m_xLbPaletteSelector->connect_changed( LINK( this, SvxDefaultColorOptPage, SelectPaletteLbHdl ) );
109 m_xValSetColorBox->SetStyle( m_xValSetColorBox->GetStyle()
110 | WB_ITEMBORDER | WB_NAMEFIELD | WB_VSCROLL );
112 if ( const SvxChartColorTableItem* pEditOptionsItem = rInAttrs.GetItemIfSet( SID_SCH_EDITOPTIONS, false ) )
114 m_SvxChartColorTableUniquePtr = std::make_unique<SvxChartColorTable>(
115 pEditOptionsItem->GetColorList());
117 else
119 m_SvxChartColorTableUniquePtr = std::make_unique<SvxChartColorTable>();
120 m_SvxChartColorTableUniquePtr->useDefault();
121 SvxChartOptions::SetDefaultColors(*m_SvxChartColorTableUniquePtr);
124 Construct();
127 SvxDefaultColorOptPage::~SvxDefaultColorOptPage()
129 m_xValSetColorBoxWin.reset();
130 m_xValSetColorBox.reset();
133 void SvxDefaultColorOptPage::Construct()
135 FillBoxChartColorLB();
136 FillPaletteLB();
138 m_xLbChartColors->select( 0 );
141 std::unique_ptr<SfxTabPage> SvxDefaultColorOptPage::Create( weld::Container* pPage, weld::DialogController* pController, const SfxItemSet* rAttrs )
143 return std::make_unique<SvxDefaultColorOptPage>( pPage, pController, *rAttrs );
146 bool SvxDefaultColorOptPage::FillItemSet( SfxItemSet* rOutAttrs )
148 if( m_SvxChartColorTableUniquePtr )
150 rOutAttrs->Put(SvxChartColorTableItem(SID_SCH_EDITOPTIONS, *m_SvxChartColorTableUniquePtr));
153 return true;
156 void SvxDefaultColorOptPage::Reset( const SfxItemSet* )
158 m_xLbChartColors->select( 0 );
161 void SvxDefaultColorOptPage::FillPaletteLB()
163 m_xLbPaletteSelector->clear();
164 std::vector<OUString> aPaletteList = aPaletteManager.GetPaletteList();
165 for (auto const& palette : aPaletteList)
166 m_xLbPaletteSelector->append_text(palette);
168 OUString aPaletteName(officecfg::Office::Common::UserColors::PaletteName::get());
169 m_xLbPaletteSelector->set_active_text(aPaletteName);
170 if (m_xLbPaletteSelector->get_active() != -1)
171 SelectPaletteLbHdl( *m_xLbPaletteSelector );
174 void SvxDefaultColorOptPage::SaveChartOptions()
176 if (m_SvxChartColorTableUniquePtr)
177 SvxChartOptions::SetDefaultColors(*m_SvxChartColorTableUniquePtr);
180 // event handlers
183 // ResetToDefaults
184 IMPL_LINK_NOARG(SvxDefaultColorOptPage, ResetToDefaults, weld::Button&, void)
186 if( m_SvxChartColorTableUniquePtr )
188 m_SvxChartColorTableUniquePtr->useDefault();
190 FillBoxChartColorLB();
192 m_xLbChartColors->grab_focus();
193 m_xLbChartColors->select( 0 );
194 m_xPBRemove->set_sensitive(true);
198 // AddChartColor
199 IMPL_LINK_NOARG(SvxDefaultColorOptPage, AddChartColor, weld::Button&, void)
201 if( m_SvxChartColorTableUniquePtr )
203 Color const black( 0x00, 0x00, 0x00 );
205 m_SvxChartColorTableUniquePtr->append(
206 XColorEntry(black, SvxChartColorTable::getDefaultName(m_SvxChartColorTableUniquePtr->size())));
208 FillBoxChartColorLB();
209 m_xLbChartColors->grab_focus();
210 m_xLbChartColors->select(m_SvxChartColorTableUniquePtr->size() - 1);
211 m_xPBRemove->set_sensitive(true);
215 // RemoveChartColor
216 IMPL_LINK_NOARG( SvxDefaultColorOptPage, RemoveChartColor, weld::Button&, void )
218 sal_Int32 nIndex = m_xLbChartColors->get_selected_index();
219 if (nIndex == -1)
220 return;
222 if( !m_SvxChartColorTableUniquePtr )
223 return;
225 OSL_ENSURE(m_SvxChartColorTableUniquePtr->size() > 1, "don't delete the last chart color");
227 std::unique_ptr<weld::Builder> xBuilder(Application::CreateBuilder(GetFrameWeld(), "cui/ui/querydeletechartcolordialog.ui"));
228 std::unique_ptr<weld::MessageDialog> xQuery(xBuilder->weld_message_dialog("QueryDeleteChartColorDialog"));
230 if (RET_YES != xQuery->run())
231 return;
233 m_SvxChartColorTableUniquePtr->remove(nIndex);
235 FillBoxChartColorLB();
237 m_xLbChartColors->grab_focus();
239 if (nIndex == m_xLbChartColors->n_children() && m_xLbChartColors->n_children() > 0)
240 m_xLbChartColors->select(m_SvxChartColorTableUniquePtr->size() - 1);
241 else if (m_xLbChartColors->n_children() > 0)
242 m_xLbChartColors->select( nIndex );
243 else
244 m_xPBRemove->set_sensitive(true);
247 IMPL_LINK_NOARG( SvxDefaultColorOptPage, SelectPaletteLbHdl, weld::ComboBox&, void)
249 sal_Int32 nPos = m_xLbPaletteSelector->get_active();
250 aPaletteManager.SetPalette( nPos );
251 aPaletteManager.ReloadColorSet( *m_xValSetColorBox );
252 m_xValSetColorBox->Resize();
255 IMPL_LINK_NOARG(SvxDefaultColorOptPage, BoxClickedHdl, ValueSet*, void)
257 sal_Int32 nIdx = m_xLbChartColors->get_selected_index();
258 if (nIdx != -1)
260 const XColorEntry aEntry(m_xValSetColorBox->GetItemColor(m_xValSetColorBox->GetSelectedItemId()), m_xLbChartColors->get_selected_text());
262 ModifyColorEntry(aEntry, nIdx);
263 m_SvxChartColorTableUniquePtr->replace(nIdx, aEntry);
265 m_xLbChartColors->select(nIdx); // reselect entry
269 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */