LibreOffice Module sw (master)  1
prcntfld.cxx
Go to the documentation of this file.
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 <prcntfld.hxx>
21 #include <vcl/fieldvalues.hxx>
22 
23 SwPercentField::SwPercentField(std::unique_ptr<weld::MetricSpinButton> pControl)
24  : m_pField(std::move(pControl))
25  , nOldMax(0)
26  , nOldMin(0)
27  , nLastPercent(-1)
28  , nLastValue(-1)
29  , nOldDigits(m_pField->get_digits())
30  , eOldUnit(FieldUnit::NONE)
31  , bLockAutoCalculation(false)
32 {
33  int nMin, nMax;
34  m_pField->get_range(nMin, nMax, FieldUnit::TWIP);
36  m_pField->get_increments(nOldSpinSize, nOldPageSize, FieldUnit::NONE);
37 }
38 
40 {
41  int nRealValue = GetRealValue(eOldUnit);
42 
43  nRefValue = nValue;
44 
45  if (!bLockAutoCalculation && (m_pField->get_unit() == FieldUnit::PERCENT))
46  set_value(nRealValue, eOldUnit);
47 }
48 
49 void SwPercentField::ShowPercent(bool bPercent)
50 {
51  if ((bPercent && m_pField->get_unit() == FieldUnit::PERCENT)
52  || (!bPercent && m_pField->get_unit() != FieldUnit::PERCENT))
53  return;
54 
55  int nOldValue;
56 
57  if (bPercent)
58  {
59  nOldValue = get_value();
60 
61  eOldUnit = m_pField->get_unit();
62  nOldDigits = m_pField->get_digits();
63  m_pField->get_range(nOldMin, nOldMax, FieldUnit::NONE);
64  m_pField->get_increments(nOldSpinSize, nOldPageSize, FieldUnit::NONE);
65  m_pField->set_unit(FieldUnit::PERCENT);
66  m_pField->set_digits(0);
67 
68  int nCurrentWidth = vcl::ConvertValue(nOldMin, 0, nOldDigits, eOldUnit, FieldUnit::TWIP);
69  // round to 0.5 percent
70  int nPercent = nRefValue ? (((nCurrentWidth * 10) / nRefValue + 5) / 10) : 0;
71 
72  m_pField->set_range(std::max(1, nPercent), 100, FieldUnit::NONE);
73  m_pField->set_increments(5, 10, FieldUnit::NONE);
74  if (nOldValue != nLastValue)
75  {
76  nCurrentWidth = vcl::ConvertValue(nOldValue, 0, nOldDigits, eOldUnit, FieldUnit::TWIP);
77  nPercent = nRefValue ? (((nCurrentWidth * 10) / nRefValue + 5) / 10) : 0;
78  m_pField->set_value(nPercent, FieldUnit::NONE);
79  nLastPercent = nPercent;
80  nLastValue = nOldValue;
81  }
82  else
83  m_pField->set_value(nLastPercent, FieldUnit::NONE);
84  }
85  else
86  {
87  int nOldPercent = get_value(FieldUnit::PERCENT);
88 
89  nOldValue = Convert(get_value(), m_pField->get_unit(), eOldUnit);
90 
91  m_pField->set_unit(eOldUnit);
92  m_pField->set_digits(nOldDigits);
93  m_pField->set_range(nOldMin, nOldMax, FieldUnit::NONE);
94  m_pField->set_increments(nOldSpinSize, nOldPageSize, FieldUnit::NONE);
95 
96  if (nOldPercent != nLastPercent)
97  {
98  set_value(nOldValue, eOldUnit);
99  nLastPercent = nOldPercent;
100  nLastValue = nOldValue;
101  }
102  else
104  }
105 }
106 
107 void SwPercentField::set_value(int nNewValue, FieldUnit eInUnit)
108 {
109  if (m_pField->get_unit() != FieldUnit::PERCENT || eInUnit == FieldUnit::PERCENT)
110  m_pField->set_value(Convert(nNewValue, eInUnit, m_pField->get_unit()), FieldUnit::NONE);
111  else
112  {
113  // Overwrite output value, do not restore later
114  int nPercent, nCurrentWidth;
115  if (eInUnit == FieldUnit::TWIP)
116  {
117  nCurrentWidth
118  = vcl::ConvertValue(nNewValue, 0, nOldDigits, FieldUnit::TWIP, FieldUnit::TWIP);
119  }
120  else
121  {
122  int nValue = Convert(nNewValue, eInUnit, eOldUnit);
123  nCurrentWidth = vcl::ConvertValue(nValue, 0, nOldDigits, eOldUnit, FieldUnit::TWIP);
124  }
125  nPercent = nRefValue ? (((nCurrentWidth * 10) / nRefValue + 5) / 10) : 0;
126  m_pField->set_value(nPercent, FieldUnit::NONE);
127  }
128 }
129 
131 {
132  return Convert(m_pField->get_value(FieldUnit::NONE), m_pField->get_unit(), eOutUnit);
133 }
134 
135 void SwPercentField::set_min(int nNewMin, FieldUnit eInUnit)
136 {
137  if (m_pField->get_unit() != FieldUnit::PERCENT)
138  m_pField->set_min(nNewMin, eInUnit);
139  else
140  {
141  if (eInUnit == FieldUnit::NONE)
142  eInUnit = eOldUnit;
143  nOldMin = Convert(nNewMin, eInUnit, eOldUnit);
144 
145  int nPercent = Convert(nNewMin, eInUnit, FieldUnit::PERCENT);
146  m_pField->set_min(std::max(1, nPercent), FieldUnit::NONE);
147  }
148 }
149 
150 void SwPercentField::set_max(int nNewMax, FieldUnit eInUnit)
151 {
152  if (m_pField->get_unit() != FieldUnit::PERCENT)
153  m_pField->set_max(nNewMax, eInUnit);
154 }
155 
157 {
158  if (m_pField->get_unit() != FieldUnit::PERCENT)
159  nValue = m_pField->normalize(nValue);
160  else
161  nValue = nValue * ImpPower10(nOldDigits);
162  return nValue;
163 }
164 
166 {
167  if (m_pField->get_unit() != FieldUnit::PERCENT)
168  nValue = m_pField->denormalize(nValue);
169  else
170  {
171  int nFactor = ImpPower10(nOldDigits);
172  nValue = ((nValue + (nFactor / 2)) / nFactor);
173  }
174  return nValue;
175 }
176 
177 int SwPercentField::ImpPower10(sal_uInt16 n)
178 {
179  int nValue = 1;
180 
181  for (sal_uInt16 i = 0; i < n; ++i)
182  nValue *= 10;
183 
184  return nValue;
185 }
186 
188 {
189  if (m_pField->get_unit() != FieldUnit::PERCENT)
190  return get_value(eOutUnit);
191  else
192  return Convert(get_value(), m_pField->get_unit(), eOutUnit);
193 }
194 
195 int SwPercentField::Convert(int nValue, FieldUnit eInUnit, FieldUnit eOutUnit)
196 {
197  if (eInUnit == eOutUnit || (eInUnit == FieldUnit::NONE && eOutUnit == m_pField->get_unit())
198  || (eOutUnit == FieldUnit::NONE && eInUnit == m_pField->get_unit()))
199  return nValue;
200 
201  if (eInUnit == FieldUnit::PERCENT)
202  {
203  // Convert to metric
204  int nTwipValue = (nRefValue * nValue + 50) / 100;
205 
206  if (eOutUnit == FieldUnit::TWIP) // Only convert if necessary
207  return NormalizePercent(nTwipValue);
208  else
209  return vcl::ConvertValue(NormalizePercent(nTwipValue), 0, nOldDigits, FieldUnit::TWIP,
210  eOutUnit);
211  }
212 
213  if (eOutUnit == FieldUnit::PERCENT)
214  {
215  // Convert to percent
216  int nCurrentWidth;
217  nValue = DenormalizePercent(nValue);
218 
219  if (eInUnit == FieldUnit::TWIP) // Only convert if necessary
220  nCurrentWidth = nValue;
221  else
222  nCurrentWidth = vcl::ConvertValue(nValue, 0, nOldDigits, eInUnit, FieldUnit::TWIP);
223  // Round to 0.5 percent
224  return nRefValue ? (((nCurrentWidth * 1000) / nRefValue + 5) / 10) : 0;
225  }
226 
227  return vcl::ConvertValue(nValue, 0, nOldDigits, eInUnit, eOutUnit);
228 }
229 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
FieldUnit
int Convert(int nValue, FieldUnit eInUnit, FieldUnit eOutUnit)
Definition: prcntfld.cxx:195
void SetRefValue(int nValue)
Definition: prcntfld.cxx:39
void ShowPercent(bool bPercent)
Definition: prcntfld.cxx:49
bool bLockAutoCalculation
Definition: prcntfld.hxx:39
void set_min(int nNewMin, FieldUnit eInUnit)
Definition: prcntfld.cxx:135
std::unique_ptr< weld::MetricSpinButton > m_pField
Definition: prcntfld.hxx:28
SwPercentField(std::unique_ptr< weld::MetricSpinButton > pControl)
Definition: prcntfld.cxx:23
void set_value(int nNewValue, FieldUnit eInUnit=FieldUnit::NONE)
Definition: prcntfld.cxx:107
int i
FieldUnit eOldUnit
Definition: prcntfld.hxx:38
void set_max(int nNewMax, FieldUnit eInUnit)
Definition: prcntfld.cxx:150
int NormalizePercent(int nValue)
Definition: prcntfld.cxx:156
int get_value(FieldUnit eOutUnit=FieldUnit::NONE)
Definition: prcntfld.cxx:130
int GetRealValue(FieldUnit eOutUnit)
Definition: prcntfld.cxx:187
int DenormalizePercent(int nValue)
Definition: prcntfld.cxx:165
sal_Int64 ConvertValue(sal_Int64 nValue, sal_Int64 mnBaseValue, sal_uInt16 nDecDigits, FieldUnit eInUnit, FieldUnit eOutUnit)
sal_uInt16 nOldDigits
Definition: prcntfld.hxx:37
sal_Int16 nValue
static SAL_DLLPRIVATE int ImpPower10(sal_uInt16 n)
Definition: prcntfld.cxx:177