2
For general Scribus (>=1.3.2) copyright and licensing information please refer
3
to the COPYING file provided with the program. Following this notice may exist
4
a copyright and/or license notice that predates the release of Scribus 1.3.2
5
for which a new license (GPL+exception) is in place.
7
/***************************************************************************
8
* Craig Bradney, cbradney@zip.com.au *
9
***************************************************************************/
18
#include "commonstrings.h"
19
#include "scrspinbox.h"
23
ScrSpinBox::ScrSpinBox(QWidget *parent, int unitIndex) : QDoubleSpinBox(parent), m_constants(NULL)
28
ScrSpinBox::ScrSpinBox(double minValue, double maxValue, QWidget *pa, int unitIndex) : QDoubleSpinBox(pa), m_constants(NULL)
35
void ScrSpinBox::init(int unitIndex)
37
m_unitIndex=unitIndex;
39
setSuffix(unitGetSuffixFromIndex(m_unitIndex));
40
setDecimals(unitGetPrecisionFromIndex(m_unitIndex));
42
lineEdit()->setValidator(0);
43
disconnect(this, SIGNAL(valueChanged(const QString &)), this, SLOT(textChanged()));
44
connect(this, SIGNAL(valueChanged(const QString &)), this, SLOT(textChanged()));
45
installEventFilter(this);
48
ScrSpinBox::~ScrSpinBox()
53
void ScrSpinBox::setParameters( int s )
55
if (s>=0 && s <=unitGetMaxIndex())
56
setDecimals(static_cast<int>(pow(10.0, s)));
61
void ScrSpinBox::setNewUnit(int unitIndex)
63
double oldUnitRatio=unitGetRatioFromIndex(m_unitIndex);
64
double oldVal = value() / oldUnitRatio;
65
double oldMax = maximum() / oldUnitRatio;
66
double oldMin = minimum() / oldUnitRatio;
67
setSuffix(unitGetSuffixFromIndex(unitIndex));
68
setDecimals(unitGetPrecisionFromIndex(unitIndex));
69
double newUnitRatio=unitGetRatioFromIndex(unitIndex);
70
setMinimum(oldMin * newUnitRatio);
71
setMaximum(oldMax * newUnitRatio);
73
m_unitIndex=unitIndex;
74
setValue(oldVal * newUnitRatio);
77
void ScrSpinBox::setValues(double min, double max, int deci, double val)
85
void ScrSpinBox::getValues(double *min, double *max, int *deci, double *val)
93
double ScrSpinBox::getValue(int unitIndex)
95
double val=value() / unitGetRatioFromIndex(m_unitIndex);
98
return val * unitGetRatioFromIndex(unitIndex);
101
void ScrSpinBox::setConstants(const QMap<QString, double>* constants)
103
m_constants = constants;
108
static const QString FinishTag("\xA0");
110
double ScrSpinBox::valueFromText ( const QString & text ) const
112
//Get all our units strings
113
//CB: Replaced by new CommonStrings versions
114
// QString trStrPT=unitGetStrFromIndex(SC_PT);
115
// QString trStrMM=unitGetStrFromIndex(SC_MM);
116
// QString trStrIN=unitGetStrFromIndex(SC_IN);
117
// QString trStrP =unitGetStrFromIndex(SC_P);
118
// QString trStrCM=unitGetStrFromIndex(SC_CM);
119
// QString trStrC =unitGetStrFromIndex(SC_C);
120
// QString strPT=unitGetUntranslatedStrFromIndex(SC_PT);
121
// QString strMM=unitGetUntranslatedStrFromIndex(SC_MM);
122
// QString strIN=unitGetUntranslatedStrFromIndex(SC_IN);
123
// QString strP =unitGetUntranslatedStrFromIndex(SC_P);
124
// QString strCM=unitGetUntranslatedStrFromIndex(SC_CM);
125
// QString strC =unitGetUntranslatedStrFromIndex(SC_C);
127
QString ts = text.trimmed();
129
QString su(unitGetStrFromIndex(m_unitIndex));
130
//Replace our pica XpY.Z format with (X*12+Y.Z)pt
131
if (CommonStrings::trStrP.localeAwareCompare(CommonStrings::strP)!=0)
132
ts.replace(CommonStrings::trStrP, CommonStrings::strP);
134
if (m_unitIndex==SC_PICAS)
135
rxP.setPattern("\\b(\\d+)"+CommonStrings::strP+"?(\\d+\\.?\\d*)?\\b");
137
rxP.setPattern("\\b(\\d+)"+CommonStrings::strP+"(\\d+\\.?\\d*)?\\b");
142
posP = rxP.indexIn(ts, posP);
145
// qDebug() << rxP.cap(1);
146
// qDebug() << rxP.cap(2);
147
QString replacement = QString("%1%2").arg(rxP.cap(1).toDouble()*(static_cast<double>(unitGetBaseFromIndex(SC_PICAS))) + rxP.cap(2).toDouble()).arg(CommonStrings::strPT);
148
ts.replace(posP, rxP.cap(0).length(), replacement);
152
// qDebug() << "##" << ts;
154
ts.replace(",", ".");
157
ts.replace(FinishTag, "");
161
ts = ts.left(ts.length()-su.length());
162
int pos = ts.length();
165
pos = ts.lastIndexOf(".", pos);
168
if (pos < static_cast<int>(ts.length()))
170
if (!ts[pos+1].isDigit())
171
ts.insert(pos+1, "0 ");
176
if (ts.endsWith("."))
178
//CB FParser doesn't handle unicode well/at all.
179
//So, instead of just getting the translated strings and
180
//sticking them in as variables in the parser, if they are
181
//not the same as the untranslated version, then we replace them.
182
//We lose the ability for now to have some strings in languages
183
//that might use them in variables.
184
//To return to previous functionality, remove the follow replacement ifs,
185
//S&R in the trStr* assignments trStrPT->strPT and remove the current str* ones.
186
//IE, send the translated strings through to the regexp.
187
if (CommonStrings::trStrPT.localeAwareCompare(CommonStrings::strPT)!=0)
188
ts.replace(CommonStrings::trStrPT, CommonStrings::strPT);
189
if (CommonStrings::trStrMM.localeAwareCompare(CommonStrings::strMM)!=0)
190
ts.replace(CommonStrings::trStrMM, CommonStrings::strMM);
191
if (CommonStrings::trStrIN.localeAwareCompare(CommonStrings::strIN)!=0)
192
ts.replace(CommonStrings::trStrIN, CommonStrings::strIN);
193
if (CommonStrings::trStrCM.localeAwareCompare(CommonStrings::strCM)!=0)
194
ts.replace(CommonStrings::trStrCM, CommonStrings::strCM);
195
if (CommonStrings::trStrC.localeAwareCompare(CommonStrings::strPT)!=0)
196
ts.replace(CommonStrings::trStrC, CommonStrings::strC);
197
//Replace in our typed text all of the units strings with *unitstring
198
QRegExp rx("\\b(\\d+)\\s*("+CommonStrings::strPT+"|"+CommonStrings::strMM+"|"+CommonStrings::strC+"|"+CommonStrings::strCM+"|"+CommonStrings::strIN+")\\b");
201
pos = rx.indexIn(ts, pos);
203
QString replacement = rx.cap(1) + "*" + rx.cap(2);
204
ts.replace(pos, rx.cap(0).length(), replacement);
208
//Add in the fparser constants using our unit strings, and the conversion factors.
210
// setFPConstants(fp);
211
fp.AddConstant(CommonStrings::strPT.toStdString(), value2value(1.0, SC_PT, m_unitIndex));
212
fp.AddConstant(CommonStrings::strMM.toStdString(), value2value(1.0, SC_MM, m_unitIndex));
213
fp.AddConstant(CommonStrings::strIN.toStdString(), value2value(1.0, SC_IN, m_unitIndex));
214
fp.AddConstant(CommonStrings::strP.toStdString(), value2value(1.0, SC_P, m_unitIndex));
215
fp.AddConstant(CommonStrings::strCM.toStdString(), value2value(1.0, SC_CM, m_unitIndex));
216
fp.AddConstant(CommonStrings::strC.toStdString(), value2value(1.0, SC_C, m_unitIndex));
218
fp.AddConstant("old", value());
221
QMap<QString, double>::ConstIterator itend = m_constants->constEnd();
222
QMap<QString, double>::ConstIterator it = m_constants->constBegin();
225
fp.AddConstant(it.key().toStdString(), it.value() * unitGetRatioFromIndex(m_unitIndex));
230
int ret = fp.Parse(ts.toStdString(), "", true);
231
// qDebug() << "fp return =" << ret;
234
double erg = fp.Eval(NULL);
235
// qDebug() << "fp value =" << erg;
239
QString ScrSpinBox::textFromValue ( double value ) const
241
if (m_unitIndex==SC_PICAS)
243
// QString r=QString("%1%2%3").arg((static_cast<int>(value))/12).arg(unitGetStrFromIndex(m_unitIndex)).arg(fabs(fmod(value, 12)));
244
int a=(static_cast<int>(value))/12;
245
double b=fabs(fmod(value, 12));
246
QString prefix((a==0 && value < 0.0) ? "-" : "");
247
return QString("%1%2%3%4").arg(prefix).arg(a).arg(unitGetStrFromIndex(m_unitIndex)).arg(b);
249
return QDoubleSpinBox::textFromValue ( value );
252
QValidator::State ScrSpinBox::validate ( QString & input, int & pos ) const
254
if (input.endsWith(FinishTag))
256
// qDebug() << "spinbox validate acceptable:" << input;
257
return QValidator::Acceptable;
261
// qDebug() << "spinbox validate intermediate:" << input;
262
return QValidator::Intermediate;
266
void ScrSpinBox::fixup ( QString & input ) const
268
if (!input.endsWith(FinishTag))
273
void ScrSpinBox::textChanged()
275
// qDebug() << "v:" << value() << "t:" << text() << "ct:" << cleanText();
278
bool ScrSpinBox::eventFilter( QObject* watched, QEvent* event )
281
/* Adding this to be sure that the IM* events are processed correctly i.e not intercepted by our KeyPress/Release handlers */
282
if (event->type() == QEvent::InputMethod)
283
return QDoubleSpinBox::eventFilter(watched, event);
285
if ( event->type() == QEvent::KeyPress )
287
QKeyEvent* k = (QKeyEvent*)event;
288
bool shiftB=k->modifiers() & Qt::ShiftModifier;
289
bool controlB=k->modifiers() & Qt::ControlModifier;
290
if (k->key() == Qt::Key_Shift && !controlB)
293
retval = QWidget::event(event);
295
else if (k->key() == Qt::Key_Control && !shiftB)
298
retval = QWidget::event(event);
300
else if ((k->key() == Qt::Key_Control && shiftB) || (k->key() == Qt::Key_Shift && controlB))
304
retval = QWidget::event(event);
306
else if ((k->key() == Qt::Key_Return) || (k->key() == Qt::Key_Enter) || (k->key() == Qt::Key_Tab))
310
// qDebug() << "eventFilter: interpretText";
311
QDoubleSpinBox::interpretText();
316
else if (event->type() == QEvent::KeyRelease )
318
QKeyEvent* k = (QKeyEvent*)event;
319
bool shiftB=k->modifiers() & Qt::ShiftModifier;
320
bool controlB=k->modifiers() & Qt::ControlModifier;
321
if ((k->key() == Qt::Key_Shift && !controlB) || (k->key() == Qt::Key_Control && !shiftB))
324
retval = QWidget::event(event);
326
else if (k->key() == Qt::Key_Shift && controlB)
329
retval = QWidget::event(event);
331
else if (k->key() == Qt::Key_Control && shiftB)
334
retval = QWidget::event(event);
338
if ( event->type() == QEvent::Wheel )
340
//If read only dont spin
343
QWheelEvent* k = (QWheelEvent*)event;
344
bool shiftB=k->modifiers() & Qt::ShiftModifier;
345
bool altB=k->modifiers() & Qt::AltModifier;
349
retval=QWidget::event(event);
351
else if (!shiftB && altB)
354
retval=QWidget::event(event);
356
else if (shiftB && altB)
359
retval=QWidget::event(event);
361
else if (!shiftB && !altB)
364
retval=QWidget::event(event);
368
return QDoubleSpinBox::eventFilter(watched, event);
372
// void ScrSpinBox::setSingleStepM(int val)
374
// QDoubleSpinBox::setSingleStep( val );
375
// // currLineStep = val * Decimals;
378
void ScrSpinBox::setTabAdvance(bool enable)
380
m_tabAdvance = enable;