Rev 137 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3 | paul | 1 | /*************************************************************************** |
2 | mspinbox.cpp - description |
||
3 | ------------------- |
||
4 | begin : Sat Jun 16 2001 |
||
5 | copyright : (C) 2001 by Franz Schmid |
||
6 | email : Franz.Schmid@altmuehlnet.de |
||
7 | ***************************************************************************/ |
||
8 | |||
9 | /*************************************************************************** |
||
10 | * * |
||
11 | * This program is free software; you can redistribute it and/or modify * |
||
12 | * it under the terms of the GNU General Public License as published by * |
||
13 | * the Free Software Foundation; either version 2 of the License, or * |
||
14 | * (at your option) any later version. * |
||
15 | * * |
||
16 | ***************************************************************************/ |
||
17 | |||
18 | #include "mspinbox.h" |
||
137 | Franz | 19 | #include <qapplication.h> |
20 | #include "fparser.h" |
||
3 | paul | 21 | |
22 | MSpinBox::MSpinBox(QWidget *pa, int s):QSpinBox(pa) |
||
23 | { |
||
24 | switch (s) |
||
25 | { |
||
138 | Franz | 26 | case 0: |
27 | Decimals = 1; |
||
28 | Width = 0; |
||
29 | break; |
||
3 | paul | 30 | case 1: |
31 | Decimals = 10; |
||
32 | Width = 1; |
||
33 | break; |
||
34 | case 2: |
||
35 | Decimals = 100; |
||
36 | Width = 2; |
||
37 | default: |
||
38 | Decimals = 100; |
||
39 | Width = 2; |
||
40 | break; |
||
41 | } |
||
42 | setValidator(0); |
||
77 | Franz | 43 | ed = editor(); |
3 | paul | 44 | } |
45 | |||
46 | bool MSpinBox::eventFilter( QObject* ob, QEvent* ev ) |
||
47 | { |
||
48 | bool retval = FALSE; |
||
49 | if ( ev->type() == QEvent::KeyPress ) |
||
50 | { |
||
51 | QKeyEvent* k = (QKeyEvent*)ev; |
||
52 | if (k->key() == Key_Shift) |
||
53 | { |
||
138 | Franz | 54 | setLineStep(QMAX(Decimals / 10, 1)); |
3 | paul | 55 | retval = true; |
56 | qApp->sendEvent( this, ev ); |
||
57 | return retval; |
||
58 | } |
||
59 | } |
||
60 | if ( ev->type() == QEvent::KeyRelease ) |
||
61 | { |
||
62 | QKeyEvent* k = (QKeyEvent*)ev; |
||
63 | if (k->key() == Key_Shift) |
||
64 | { |
||
65 | setLineStep(Decimals); |
||
66 | retval = true; |
||
67 | qApp->sendEvent( this, ev ); |
||
68 | return retval; |
||
69 | } |
||
70 | } |
||
71 | return QSpinBox::eventFilter(ob, ev); |
||
72 | } |
||
73 | |||
74 | QString MSpinBox::mapValueToText(int value) |
||
75 | { |
||
80 | Franz | 76 | double dez = Width == 1 ? 10.0 : 100.0; |
68 | Franz | 77 | return QString::number(static_cast<double>(value) / dez, 'f', Width); |
3 | paul | 78 | } |
79 | |||
80 | int MSpinBox::mapTextToValue(bool *) |
||
81 | { |
||
80 | Franz | 82 | double dez = Width == 1 ? 10.0 : 100.0; |
137 | Franz | 83 | FunctionParser fp; |
138 | Franz | 84 | QString ts = text(); |
85 | QString su = suffix(); |
||
86 | ts.replace(",", "."); |
||
87 | if (su == tr( " pt" )) |
||
88 | { |
||
89 | ts.replace("pt", ""); |
||
90 | ts.replace("mm", "/25.4*72"); |
||
91 | ts.replace("in", "*72"); |
||
92 | ts.replace("p", "*12"); |
||
93 | } |
||
94 | else if (su == tr( " mm" )) |
||
95 | { |
||
96 | ts.replace("pt", "/72*25.4"); |
||
97 | ts.replace("mm", ""); |
||
98 | ts.replace("in", "*25.4"); |
||
99 | ts.replace("p", "/12*25.4"); |
||
100 | } |
||
101 | else if (su == tr( " in" )) |
||
102 | { |
||
103 | ts.replace("pt", "/72"); |
||
104 | ts.replace("mm", "/25.4"); |
||
105 | ts.replace("in", ""); |
||
106 | ts.replace("p", "/6"); |
||
107 | } |
||
108 | else if (su == tr( " p" )) |
||
109 | { |
||
110 | ts.replace("pt", "/12"); |
||
111 | ts.replace("mm", "/25.4*6"); |
||
112 | ts.replace("in", "*6"); |
||
113 | ts.replace("p", ""); |
||
114 | } |
||
115 | else if (su != "") |
||
116 | { |
||
117 | ts.replace(su, " "); |
||
118 | } |
||
119 | int ret = fp.Parse(ts.latin1(), "", true); |
||
137 | Franz | 120 | if (ret >= 0) |
121 | return 0; |
||
122 | double erg = fp.Eval(NULL); |
||
123 | return qRound(erg*dez); |
||
3 | paul | 124 | } |
125 | |||
126 | void MSpinBox::setDecimals(int deci) |
||
127 | { |
||
128 | Decimals = deci; |
||
129 | setLineStep(Decimals); |
||
138 | Franz | 130 | if (deci < 10) |
131 | Width = 0; |
||
80 | Franz | 132 | if (deci > 9 && deci < 100) |
3 | paul | 133 | Width = 1; |
134 | if (deci > 99) |
||
135 | Width = 2; |
||
136 | } |