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 "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 
InsertColorEntry(const XColorEntry & rEntry,sal_Int32 nPos)30 void SvxDefaultColorOptPage::InsertColorEntry(const XColorEntry& rEntry, sal_Int32 nPos)
31 {
32     const Color& rColor = rEntry.GetColor();
33     const OUString& rStr = rEntry.GetName();
34 
35     const StyleSettings& rStyleSettings = Application::GetSettings().GetStyleSettings();
36     Size aImageSize = rStyleSettings.GetListBoxPreviewDefaultPixelSize();
37 
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);
44 
45     m_xLbChartColors->insert(nullptr, nPos, &rStr, nullptr,
46                              nullptr, xDevice.get(), false, nullptr);
47 
48     if (nPos == -1)
49         aColorList.push_back( rColor );
50     else
51     {
52         ImpColorList::iterator it = aColorList.begin();
53         std::advance( it, nPos );
54         aColorList.insert( it, rColor );
55     }
56 }
57 
RemoveColorEntry(sal_Int32 nPos)58 void SvxDefaultColorOptPage::RemoveColorEntry(sal_Int32 nPos)
59 {
60     m_xLbChartColors->remove(nPos);
61     ImpColorList::iterator it = aColorList.begin();
62     std::advance(it, nPos);
63     aColorList.erase(it);
64 }
65 
ClearColorEntries()66 void SvxDefaultColorOptPage::ClearColorEntries()
67 {
68     aColorList.clear();
69     m_xLbChartColors->clear();
70 }
71 
ModifyColorEntry(const XColorEntry & rEntry,sal_Int32 nPos)72 void SvxDefaultColorOptPage::ModifyColorEntry(const XColorEntry& rEntry, sal_Int32 nPos)
73 {
74     RemoveColorEntry(nPos);
75     InsertColorEntry(rEntry, nPos);
76 }
77 
FillBoxChartColorLB()78 void SvxDefaultColorOptPage::FillBoxChartColorLB()
79 {
80     if (!m_SvxChartColorTableUniquePtr)
81         return;
82 
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();
89 }
90 
SvxDefaultColorOptPage(weld::Container * pPage,weld::DialogController * pController,const SfxItemSet & rInAttrs)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))
100 {
101     m_xLbChartColors->set_size_request(-1, m_xLbChartColors->get_height_rows(16));
102 
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 ) );
108 
109     m_xValSetColorBox->SetStyle( m_xValSetColorBox->GetStyle()
110                                     | WB_ITEMBORDER | WB_NAMEFIELD | WB_VSCROLL );
111 
112     m_SvxChartOptionsUniquePtr.reset(new SvxChartOptions);
113 
114     const SfxPoolItem* pItem = nullptr;
115     if ( rInAttrs.GetItemState( SID_SCH_EDITOPTIONS, false, &pItem ) == SfxItemState::SET )
116     {
117         m_SvxChartColorTableUniquePtr = std::make_unique<SvxChartColorTable>(
118             static_cast<const SvxChartColorTableItem*>(pItem)->GetColorList());
119     }
120     else
121     {
122         m_SvxChartColorTableUniquePtr = std::make_unique<SvxChartColorTable>();
123         m_SvxChartColorTableUniquePtr->useDefault();
124         m_SvxChartOptionsUniquePtr->SetDefaultColors(*m_SvxChartColorTableUniquePtr);
125     }
126 
127     Construct();
128 }
129 
~SvxDefaultColorOptPage()130 SvxDefaultColorOptPage::~SvxDefaultColorOptPage()
131 {
132     m_xValSetColorBoxWin.reset();
133     m_xValSetColorBox.reset();
134 }
135 
Construct()136 void SvxDefaultColorOptPage::Construct()
137 {
138     FillBoxChartColorLB();
139     FillPaletteLB();
140 
141     m_xLbChartColors->select( 0 );
142 }
143 
Create(weld::Container * pPage,weld::DialogController * pController,const SfxItemSet * rAttrs)144 std::unique_ptr<SfxTabPage> SvxDefaultColorOptPage::Create( weld::Container* pPage, weld::DialogController* pController, const SfxItemSet* rAttrs )
145 {
146     return std::make_unique<SvxDefaultColorOptPage>( pPage, pController, *rAttrs );
147 }
148 
FillItemSet(SfxItemSet * rOutAttrs)149 bool SvxDefaultColorOptPage::FillItemSet( SfxItemSet* rOutAttrs )
150 {
151     if( m_SvxChartColorTableUniquePtr )
152     {
153         rOutAttrs->Put(SvxChartColorTableItem(SID_SCH_EDITOPTIONS, *m_SvxChartColorTableUniquePtr));
154     }
155 
156     return true;
157 }
158 
Reset(const SfxItemSet *)159 void SvxDefaultColorOptPage::Reset( const SfxItemSet* )
160 {
161     m_xLbChartColors->select( 0 );
162 }
163 
FillPaletteLB()164 void SvxDefaultColorOptPage::FillPaletteLB()
165 {
166     m_xLbPaletteSelector->clear();
167     std::vector<OUString> aPaletteList = aPaletteManager.GetPaletteList();
168     for (auto const& palette : aPaletteList)
169         m_xLbPaletteSelector->append_text(palette);
170 
171     OUString aPaletteName(officecfg::Office::Common::UserColors::PaletteName::get());
172     m_xLbPaletteSelector->set_active_text(aPaletteName);
173     if (m_xLbPaletteSelector->get_active() != -1)
174         SelectPaletteLbHdl( *m_xLbPaletteSelector );
175 }
176 
SaveChartOptions()177 void SvxDefaultColorOptPage::SaveChartOptions()
178 {
179     if (m_SvxChartOptionsUniquePtr && m_SvxChartColorTableUniquePtr)
180     {
181         m_SvxChartOptionsUniquePtr->SetDefaultColors(*m_SvxChartColorTableUniquePtr);
182         m_SvxChartOptionsUniquePtr->Commit();
183     }
184 }
185 
186 // event handlers
187 
188 
189 // ResetToDefaults
IMPL_LINK_NOARG(SvxDefaultColorOptPage,ResetToDefaults,weld::Button &,void)190 IMPL_LINK_NOARG(SvxDefaultColorOptPage, ResetToDefaults, weld::Button&, void)
191 {
192     if( m_SvxChartColorTableUniquePtr )
193     {
194         m_SvxChartColorTableUniquePtr->useDefault();
195 
196         FillBoxChartColorLB();
197 
198         m_xLbChartColors->grab_focus();
199         m_xLbChartColors->select( 0 );
200         m_xPBRemove->set_sensitive(true);
201     }
202 }
203 
204 // AddChartColor
IMPL_LINK_NOARG(SvxDefaultColorOptPage,AddChartColor,weld::Button &,void)205 IMPL_LINK_NOARG(SvxDefaultColorOptPage, AddChartColor, weld::Button&, void)
206 {
207     if( m_SvxChartColorTableUniquePtr )
208     {
209         Color const black( 0x00, 0x00, 0x00 );
210 
211         m_SvxChartColorTableUniquePtr->append(
212             XColorEntry(black, SvxChartColorTable::getDefaultName(m_SvxChartColorTableUniquePtr->size())));
213 
214         FillBoxChartColorLB();
215         m_xLbChartColors->grab_focus();
216         m_xLbChartColors->select(m_SvxChartColorTableUniquePtr->size() - 1);
217         m_xPBRemove->set_sensitive(true);
218     }
219 }
220 
221 // RemoveChartColor
IMPL_LINK_NOARG(SvxDefaultColorOptPage,RemoveChartColor,weld::Button &,void)222 IMPL_LINK_NOARG( SvxDefaultColorOptPage, RemoveChartColor, weld::Button&, void )
223 {
224     sal_Int32 nIndex = m_xLbChartColors->get_selected_index();
225     if (nIndex == -1)
226         return;
227 
228     if( !m_SvxChartColorTableUniquePtr )
229         return;
230 
231     OSL_ENSURE(m_SvxChartColorTableUniquePtr->size() > 1, "don't delete the last chart color");
232 
233     std::unique_ptr<weld::Builder> xBuilder(Application::CreateBuilder(GetFrameWeld(), "cui/ui/querydeletechartcolordialog.ui"));
234     std::unique_ptr<weld::MessageDialog> xQuery(xBuilder->weld_message_dialog("QueryDeleteChartColorDialog"));
235 
236     if (RET_YES != xQuery->run())
237         return;
238 
239     m_SvxChartColorTableUniquePtr->remove(nIndex);
240 
241     FillBoxChartColorLB();
242 
243     m_xLbChartColors->grab_focus();
244 
245     if (nIndex == m_xLbChartColors->n_children() && m_xLbChartColors->n_children() > 0)
246         m_xLbChartColors->select(m_SvxChartColorTableUniquePtr->size() - 1);
247     else if (m_xLbChartColors->n_children() > 0)
248         m_xLbChartColors->select( nIndex );
249     else
250         m_xPBRemove->set_sensitive(true);
251 }
252 
IMPL_LINK_NOARG(SvxDefaultColorOptPage,SelectPaletteLbHdl,weld::ComboBox &,void)253 IMPL_LINK_NOARG( SvxDefaultColorOptPage, SelectPaletteLbHdl, weld::ComboBox&, void)
254 {
255     sal_Int32 nPos = m_xLbPaletteSelector->get_active();
256     aPaletteManager.SetPalette( nPos );
257     aPaletteManager.ReloadColorSet( *m_xValSetColorBox );
258     m_xValSetColorBox->Resize();
259 }
260 
IMPL_LINK_NOARG(SvxDefaultColorOptPage,BoxClickedHdl,ValueSet *,void)261 IMPL_LINK_NOARG(SvxDefaultColorOptPage, BoxClickedHdl, ValueSet*, void)
262 {
263     sal_Int32 nIdx = m_xLbChartColors->get_selected_index();
264     if (nIdx != -1)
265     {
266         const XColorEntry aEntry(m_xValSetColorBox->GetItemColor(m_xValSetColorBox->GetSelectedItemId()), m_xLbChartColors->get_selected_text());
267 
268         ModifyColorEntry(aEntry, nIdx);
269         m_SvxChartColorTableUniquePtr->replace(nIdx, aEntry);
270 
271         m_xLbChartColors->select(nIdx);  // reselect entry
272     }
273 }
274 
275 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
276