Line data Source code
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 :
10 : #include "colorformat.hxx"
11 : #include "colorscale.hxx"
12 :
13 : #include "colorformat.hrc"
14 : #include "document.hxx"
15 :
16 : #include <svx/xtable.hxx>
17 : #include <svx/drawitem.hxx>
18 : #include <vcl/msgbox.hxx>
19 :
20 : namespace {
21 :
22 0 : void SetType(const ScColorScaleEntry* pEntry, ListBox& rLstBox)
23 : {
24 0 : rLstBox.SelectEntryPos(pEntry->GetType());
25 0 : }
26 :
27 0 : void GetType(const ListBox& rLstBox, const Edit& rEd, ScColorScaleEntry* pEntry, SvNumberFormatter* pNumberFormatter,
28 : ScDocument* pDoc, const ScAddress& rPos )
29 : {
30 0 : double nVal = 0;
31 0 : sal_uInt32 nIndex = 0;
32 0 : pEntry->SetType(static_cast<ScColorScaleEntryType>(rLstBox.GetSelectEntryPos()));
33 0 : switch(rLstBox.GetSelectEntryPos())
34 : {
35 : case COLORSCALE_AUTO:
36 : case COLORSCALE_MIN:
37 : case COLORSCALE_MAX:
38 0 : break;
39 : case COLORSCALE_PERCENTILE:
40 : case COLORSCALE_VALUE:
41 : case COLORSCALE_PERCENT:
42 0 : pNumberFormatter->IsNumberFormat( rEd.GetText(), nIndex, nVal );
43 0 : pEntry->SetValue(nVal);
44 0 : break;
45 : case COLORSCALE_FORMULA:
46 0 : pEntry->SetFormula(rEd.GetText(), pDoc, rPos);
47 0 : break;
48 : }
49 0 : }
50 :
51 0 : void SetValue( ScColorScaleEntry* pEntry, Edit& aEdit)
52 : {
53 0 : if(pEntry->GetType() == COLORSCALE_FORMULA)
54 0 : aEdit.SetText(pEntry->GetFormula(formula::FormulaGrammar::GRAM_DEFAULT));
55 0 : else if(pEntry->GetType() != COLORSCALE_MIN && pEntry->GetType() != COLORSCALE_MAX)
56 0 : aEdit.SetText(OUString::valueOf(pEntry->GetValue()));
57 : else
58 0 : aEdit.Disable();
59 0 : }
60 :
61 : }
62 :
63 0 : ScDataBarSettingsDlg::ScDataBarSettingsDlg(Window* pWindow, const ScDataBarFormatData& rData, ScDocument* pDoc, const ScAddress& rPos):
64 : ModalDialog( pWindow, ScResId( RID_SCDLG_DATABAR ) ),
65 : maBtnOk( this, ScResId( BTN_OK ) ),
66 : maBtnCancel( this, ScResId( BTN_CANCEL ) ),
67 : maFlBarColors( this, ScResId( FL_BAR_COLORS ) ),
68 : maFlAxes( this, ScResId( FL_AXIS ) ),
69 : maFlValues( this, ScResId( FL_VALUES ) ),
70 : maFtMin( this, ScResId( FT_MINIMUM ) ),
71 : maFtMax( this, ScResId( FT_MAXIMUM ) ),
72 : maFtPositive( this, ScResId( FT_POSITIVE ) ),
73 : maFtNegative( this, ScResId( FT_NEGATIVE ) ),
74 : maFtPosition( this, ScResId( FT_POSITION ) ),
75 : maFtAxisColor( this, ScResId( FT_COLOR_AXIS ) ),
76 : maLbPos( this, ScResId( LB_POS ) ),
77 : maLbNeg( this, ScResId( LB_NEG ) ),
78 : maLbAxisCol( this, ScResId( LB_COL_AXIS ) ),
79 : maLbTypeMin( this, ScResId( LB_TYPE ) ),
80 : maLbTypeMax( this, ScResId( LB_TYPE ) ),
81 : maLbAxisPos( this, ScResId( LB_AXIS_POSITION ) ),
82 : maEdMin( this, ScResId( ED_MIN ) ),
83 : maEdMax( this, ScResId( ED_MAX ) ),
84 : maStrWarnSameValue( SC_RESSTR( STR_WARN_SAME_VALUE ) ),
85 0 : mpNumberFormatter( pDoc->GetFormatTable() ),
86 : mpDoc(pDoc),
87 0 : maPos(rPos)
88 : {
89 0 : Init();
90 0 : FreeResource();
91 :
92 0 : maLbPos.SelectEntry( rData.maPositiveColor );
93 0 : if(rData.mpNegativeColor)
94 0 : maLbNeg.SelectEntry( *rData.mpNegativeColor );
95 :
96 0 : switch (rData.meAxisPosition)
97 : {
98 : case databar::NONE:
99 0 : maLbAxisPos.SelectEntryPos(2);
100 0 : break;
101 : case databar::AUTOMATIC:
102 0 : maLbAxisPos.SelectEntryPos(0);
103 0 : break;
104 : case databar::MIDDLE:
105 0 : maLbAxisPos.SelectEntryPos(1);
106 0 : break;
107 : }
108 0 : ::SetType(rData.mpLowerLimit.get(), maLbTypeMin);
109 0 : ::SetType(rData.mpUpperLimit.get(), maLbTypeMax);
110 0 : SetValue(rData.mpLowerLimit.get(), maEdMin);
111 0 : SetValue(rData.mpUpperLimit.get(), maEdMax);
112 0 : maLbAxisCol.SelectEntry(rData.maAxisColor);
113 :
114 0 : TypeSelectHdl(NULL);
115 0 : }
116 :
117 0 : void ScDataBarSettingsDlg::Init()
118 : {
119 0 : SfxObjectShell* pDocSh = SfxObjectShell::Current();
120 0 : XColorListRef pColorTable;
121 :
122 : DBG_ASSERT( pDocSh, "DocShell not found!" );
123 :
124 0 : if ( pDocSh )
125 : {
126 0 : const SfxPoolItem* pItem = pDocSh->GetItem( SID_COLOR_TABLE );
127 0 : if ( pItem != NULL )
128 0 : pColorTable = ( (SvxColorListItem*)pItem )->GetColorList();
129 : }
130 0 : if ( pColorTable.is() )
131 : {
132 : // filling the line color box
133 0 : maLbPos.SetUpdateMode( false );
134 0 : maLbNeg.SetUpdateMode( false );
135 0 : maLbAxisCol.SetUpdateMode( false );
136 :
137 0 : for ( long i = 0; i < pColorTable->Count(); ++i )
138 : {
139 0 : XColorEntry* pEntry = pColorTable->GetColor(i);
140 0 : maLbPos.InsertEntry( pEntry->GetColor(), pEntry->GetName() );
141 0 : maLbNeg.InsertEntry( pEntry->GetColor(), pEntry->GetName() );
142 0 : maLbAxisCol.InsertEntry( pEntry->GetColor(), pEntry->GetName() );
143 :
144 0 : if(pEntry->GetColor() == Color(COL_LIGHTRED))
145 0 : maLbNeg.SelectEntryPos(i);
146 0 : if(pEntry->GetColor() == Color(COL_BLACK))
147 0 : maLbAxisCol.SelectEntryPos(i);
148 0 : if(pEntry->GetColor() == Color(COL_LIGHTBLUE))
149 0 : maLbPos.SelectEntryPos(i);
150 : }
151 0 : maLbPos.SetUpdateMode( sal_True );
152 0 : maLbNeg.SetUpdateMode( sal_True );
153 0 : maLbAxisCol.SetUpdateMode( sal_True );
154 : }
155 0 : maBtnOk.SetClickHdl( LINK( this, ScDataBarSettingsDlg, OkBtnHdl ) );
156 :
157 0 : Point aPoint(maLbTypeMax.GetPosPixel().X(), maFtMax.GetPosPixel().Y());
158 0 : maLbTypeMax.SetPosPixel(aPoint);
159 :
160 0 : maLbTypeMin.SetSelectHdl( LINK( this, ScDataBarSettingsDlg, TypeSelectHdl ) );
161 0 : maLbTypeMax.SetSelectHdl( LINK( this, ScDataBarSettingsDlg, TypeSelectHdl ) );
162 :
163 0 : }
164 :
165 : namespace {
166 :
167 0 : void GetAxesPosition(ScDataBarFormatData* pData, const ListBox& rLbox)
168 : {
169 0 : switch(rLbox.GetSelectEntryPos())
170 : {
171 : case 0:
172 0 : pData->meAxisPosition = databar::AUTOMATIC;
173 0 : break;
174 : case 1:
175 0 : pData->meAxisPosition = databar::MIDDLE;
176 0 : break;
177 : case 2:
178 0 : pData->meAxisPosition = databar::NONE;
179 0 : break;
180 : }
181 0 : }
182 :
183 : }
184 :
185 0 : ScDataBarFormatData* ScDataBarSettingsDlg::GetData()
186 : {
187 0 : ScDataBarFormatData* pData = new ScDataBarFormatData();
188 0 : pData->maPositiveColor = maLbPos.GetSelectEntryColor();
189 0 : pData->mpNegativeColor.reset(new Color(maLbNeg.GetSelectEntryColor()));
190 0 : pData->mbGradient = true; //FIXME
191 0 : pData->mpUpperLimit.reset(new ScColorScaleEntry());
192 0 : pData->mpLowerLimit.reset(new ScColorScaleEntry());
193 0 : pData->maAxisColor = maLbAxisCol.GetSelectEntryColor();
194 :
195 0 : ::GetType(maLbTypeMin, maEdMin, pData->mpLowerLimit.get(), mpNumberFormatter, mpDoc, maPos);
196 0 : ::GetType(maLbTypeMax, maEdMax, pData->mpUpperLimit.get(), mpNumberFormatter, mpDoc, maPos);
197 0 : GetAxesPosition(pData, maLbAxisPos);
198 :
199 0 : return pData;
200 : }
201 :
202 0 : IMPL_LINK_NOARG( ScDataBarSettingsDlg, OkBtnHdl )
203 : {
204 : //check that min < max
205 0 : bool bWarn = false;
206 0 : sal_Int32 nSelectMin = maLbTypeMin.GetSelectEntryPos();
207 0 : if( nSelectMin == COLORSCALE_MAX )
208 0 : bWarn = true;
209 0 : sal_Int32 nSelectMax = maLbTypeMax.GetSelectEntryPos();
210 0 : if( nSelectMax == COLORSCALE_MIN )
211 0 : bWarn = true;
212 :
213 0 : if(!bWarn && maLbTypeMin.GetSelectEntryPos() == maLbTypeMax.GetSelectEntryPos())
214 : {
215 :
216 0 : if(nSelectMax != COLORSCALE_FORMULA && nSelectMax != COLORSCALE_AUTO)
217 : {
218 0 : OUString aMinString = maEdMin.GetText();
219 0 : OUString aMaxString = maEdMax.GetText();
220 0 : double nMinValue = 0;
221 0 : sal_uInt32 nIndex = 0;
222 0 : mpNumberFormatter->IsNumberFormat(aMinString, nIndex, nMinValue);
223 0 : nIndex = 0;
224 0 : double nMaxValue = 0;
225 0 : mpNumberFormatter->IsNumberFormat(aMaxString, nIndex, nMaxValue);
226 0 : if(rtl::math::approxEqual(nMinValue, nMaxValue) || nMinValue > nMaxValue)
227 0 : bWarn = true;
228 : }
229 : }
230 :
231 0 : if(bWarn)
232 : {
233 : //show warning message and don't close
234 0 : WarningBox aWarn(this, WB_OK, maStrWarnSameValue );
235 0 : aWarn.Execute();
236 : }
237 : else
238 : {
239 0 : EndDialog(RET_OK);
240 : }
241 0 : return 0;
242 : }
243 :
244 0 : IMPL_LINK_NOARG( ScDataBarSettingsDlg, TypeSelectHdl )
245 : {
246 0 : sal_Int32 nSelectMin = maLbTypeMin.GetSelectEntryPos();
247 0 : if( nSelectMin <= COLORSCALE_MAX)
248 0 : maEdMin.Disable();
249 : else
250 : {
251 0 : maEdMin.Enable();
252 0 : if(maEdMin.GetText().isEmpty())
253 : {
254 0 : if(nSelectMin == COLORSCALE_PERCENTILE || nSelectMin == COLORSCALE_PERCENT)
255 0 : maEdMin.SetText(OUString::number(50));
256 : else
257 0 : maEdMin.SetText(OUString::number(0));
258 : }
259 : }
260 :
261 0 : sal_Int32 nSelectMax = maLbTypeMax.GetSelectEntryPos();
262 0 : if(nSelectMax <= COLORSCALE_MAX)
263 0 : maEdMax.Disable();
264 : else
265 : {
266 0 : maEdMax.Enable();
267 0 : if(maEdMax.GetText().isEmpty())
268 : {
269 0 : if(nSelectMax == COLORSCALE_PERCENTILE || nSelectMax == COLORSCALE_PERCENT)
270 0 : maEdMax.SetText(OUString::number(50));
271 : else
272 0 : maEdMax.SetText(OUString::number(0));
273 : }
274 : }
275 0 : return 0;
276 93 : }
277 :
278 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|