##// END OF EJS Templates
sync
Jeandet Alexis -
r8:34a197717298 default
parent child
Show More
@@ -1,258 +1,258
1 1 #include "registerwidget.h"
2 2 #include <QPaintEvent>
3 3 #include <QPainter>
4 4
5 5 registerWidget::registerWidget(const QString &name, qint32 address, QObject *parent) :
6 6 QObject(parent)
7 7 {
8 8 p_address = address;
9 9 p_value = 0;
10 p_addressEl = new regWidgetElement(QString("0x%1").arg(p_address,8,16).replace(" ","0"),QFont("Utopia", 12),10,4);
11 p_fieldsEl = new bitfieldsElement(QString("%1").arg(p_value,32,2).replace(" ","0"),QFont("Utopia", 12),4,4);
10 p_addressEl = new regWidgetElement(QString("0x%1").arg((uint)p_address,8,16).replace(" ","0"),QFont("Utopia", 12),10,4);
11 p_fieldsEl = new bitfieldsElement(QString("%1").arg((uint)p_value,32,2).replace(" ","0"),QFont("Utopia", 12),4,4);
12 12 p_nameEl = new regWidgetElement(name,QFont("Utopia", 12,QFont::Bold),4,4);
13 13 p_xMargins = 4;
14 14 p_yMargins = 4;
15 15 updateBoundingRect();
16 16 }
17 17
18 18 registerWidget::~registerWidget()
19 19 {
20 20 delete p_addressEl;
21 21 delete p_fieldsEl;
22 22 delete p_nameEl;
23 23 }
24 24
25 25 int registerWidget::contains(const QPointF &point)
26 26 {
27 27 return p_boundingRect.contains(point.x(),point.y());
28 28 }
29 29
30 30 QPoint registerWidget::paint(QPainter* painter, QPoint offset)
31 31 {
32 32 painter->save();
33 33 painter->translate(offset);
34 34 p_boundingRect.moveTopLeft(offset);
35 35 painter->translate(p_addressEl->paint(painter));
36 36 painter->translate(p_fieldsEl->paint(painter));
37 37 p_nameEl->paint(painter);
38 38 painter->restore();
39 39 int h=p_boundingRect.height();
40 40 int y=p_boundingRect.y();
41 41 return QPoint(0,p_boundingRect.height()+p_boundingRect.y()+p_yMargins);
42 42 }
43 43
44 44 QRect registerWidget::boundingRect()
45 45 {
46 46 return p_boundingRect;
47 47 }
48 48
49 49 uint registerWidget::cursorIndex()
50 50 {
51 51 return p_fieldsEl->cursorIndex();
52 52 }
53 53
54 54 uint registerWidget::cursorIndex(int xPos)
55 55 {
56 56 if(xPos>p_addressEl->boundingRect().width() && xPos<(p_addressEl->boundingRect().width()+p_fieldsEl->boundingRect().width()))
57 57 {
58 58 return p_fieldsEl->cursorIndex(xPos-p_addressEl->boundingRect().width());
59 59 }
60 60 }
61 61
62 62 qint32 registerWidget::address()
63 63 {
64 64 return p_address;
65 65 }
66 66
67 67 qint32 registerWidget::value()
68 68 {
69 69 return p_value;
70 70 }
71 71
72 72 void registerWidget::setValue(qint32 value)
73 73 {
74 74 this->p_value = value;
75 75 this->p_fieldsEl->setValue(QString("%1").arg((uint)p_value,32,2).replace(" ","0"));
76 76 emit this->repaint();
77 77 }
78 78
79 79 void registerWidget::blinkCursor()
80 80 {
81 81 p_fieldsEl->blinkCursor();
82 82 //repaint();
83 83 }
84 84
85 85 void registerWidget::moveCursorLeft(int count)
86 86 {
87 87 p_fieldsEl->moveCursorLeft(count);
88 88 p_fieldsEl->blinkCursor();
89 89 }
90 90
91 91 void registerWidget::moveCursorRight(int count)
92 92 {
93 93 p_fieldsEl->moveCursorRight(count);
94 94 p_fieldsEl->blinkCursor();
95 95 }
96 96
97 97 void registerWidget::enter(int index)
98 98 {
99 99 p_fieldsEl->enter(index);
100 100 }
101 101
102 102 void registerWidget::leave()
103 103 {
104 104 p_fieldsEl->leave();
105 105 }
106 106
107 107 void registerWidget::clear(int index)
108 108 {
109 109 p_value &= ~(1<<index);
110 110 this->p_fieldsEl->setValue(QString("%1").arg((uint)p_value,32,2).replace(" ","0"));
111 111 emit this->repaint();
112 112 }
113 113
114 114 void registerWidget::set(int index)
115 115 {
116 116 if(!this->p_fieldsEl->readonly(index))
117 117 {
118 118 p_value |= 1<<index;
119 119 this->p_fieldsEl->setValue(QString("%1").arg((uint)p_value,32,2).replace(" ","0"));
120 120 emit this->repaint();
121 121 }
122 122 }
123 123
124 124 void registerWidget::updateBoundingRect()
125 125 {
126 126 p_boundingRect.setHeight(p_fieldsEl->boundingRect().height()+(p_yMargins*2));
127 127 p_boundingRect.setWidth(p_fieldsEl->boundingRect().width()+p_addressEl->boundingRect().width()+p_nameEl->boundingRect().width()+(p_xMargins*2));
128 128 }
129 129
130 130
131 131
132 132 bitfieldsElement::bitfieldsElement(const QString &value, QFont font, int xMargin, int yMargin)
133 133 :regWidgetElement(value,font,xMargin,yMargin)
134 134 {
135 135 this->attributesLUT.append(new bitFieldAttribute(false,"UNSUSED"));
136 136 for(int i=0;i<32;i++)
137 137 {
138 138 attributesIndex[i] = 0;
139 139 }
140 140 updateBoundingRect();
141 141 p_blinkTextBgColor = QColor(Qt::black);
142 142 p_blinkTextColor = QColor(Qt::white);
143 143 p_cursorBlinkEnable = false;
144 144 p_dx=QFontMetrics(p_font).width("0")+2;
145 145 }
146 146
147 147 QPoint bitfieldsElement::paint(QPainter *painter)
148 148 {
149 149 painter->fillRect(4,4,p_boundingRec.width(),p_boundingRec.height(),Qt::darkGray);
150 150 painter->fillRect(0,0,p_boundingRec.width(),p_boundingRec.height(),Qt::white);
151 151 painter->setFont(p_font);
152 152 int xpos=p_xMargins;
153 153 for(int i=0;i<(32/4);i++)
154 154 {
155 155 for(int l = 0;l<4;l++)
156 156 {
157 157 if(p_cursorBlinkEnable == false || (31-p_cursorIndex) != ((i*4)+l))
158 158 {
159 159 if(attributesLUT[attributesIndex[31-((i*4)+l)]]->rw==false)
160 160 {
161 161 painter->fillRect(xpos-1,0,p_dx,p_boundingRec.height(),Qt::lightGray);
162 162 }
163 163 else
164 164 {
165 165 painter->fillRect(xpos-1,0,p_dx,p_boundingRec.height(),Qt::white);
166 166 }
167 167 painter->drawText(xpos,QFontMetrics(p_font).ascent()+p_yMargins,p_valueStr.at((i*4)+l));
168 168 }
169 169 else
170 170 {
171 171 QPen svg = painter->pen();
172 172 painter->setPen(p_blinkTextColor);
173 173 painter->fillRect(xpos-1,0,p_dx,p_boundingRec.height(),p_blinkTextBgColor);
174 174 painter->drawText(xpos,QFontMetrics(p_font).ascent()+p_yMargins,p_valueStr.at((i*4)+l));
175 175 painter->setPen(svg);
176 176 }
177 177 xpos+=p_dx;
178 178 }
179 179 if(i==3)
180 180 painter->drawLine(xpos+(p_xMargins/2),p_boundingRec.height()-6,xpos+(p_xMargins/2),p_boundingRec.height()+6);
181 181 else if(i<7)
182 182 painter->drawLine(xpos+(p_xMargins/2),p_boundingRec.height()-2,xpos+(p_xMargins/2),p_boundingRec.height()+2);
183 183 xpos+=p_xMargins;
184 184 }
185 185 painter->drawRect(0,0,p_boundingRec.width(),p_boundingRec.height());
186 186 return QPoint(p_boundingRec.width()+4+p_xMargins,0);
187 187 }
188 188
189 189 void bitfieldsElement::updateBoundingRect()
190 190 {
191 191 p_boundingRec.setHeight(QFontMetrics(p_font).boundingRect(p_valueStr).height()+(p_yMargins*2));
192 192 int width = (((4*(QFontMetrics(p_font).width("0") + 2)) + p_xMargins) * 8) + (p_xMargins);
193 193 p_boundingRec.setWidth(width);
194 194 }
195 195
196 196 void bitfieldsElement::setValue(const QString &value)
197 197 {
198 198 p_valueStr = value;
199 199 updateBoundingRect();
200 200 }
201 201
202 202 void bitfieldsElement::blinkCursor()
203 203 {
204 204 p_cursorBlinkEnable = !p_cursorBlinkEnable;
205 205 }
206 206
207 207 void bitfieldsElement::moveCursorLeft(int count)
208 208 {
209 209 p_cursorIndex+=count;
210 210 if(31<p_cursorIndex)
211 211 {
212 212 p_cursorIndex = 31;
213 213 }
214 214
215 215 }
216 216
217 217 void bitfieldsElement::moveCursorRight(int count)
218 218 {
219 219 p_cursorIndex -= count;
220 220 if(31<p_cursorIndex)
221 221 {
222 222 p_cursorIndex = 0;
223 223 }
224 224 }
225 225
226 226 void bitfieldsElement::enter(int index)
227 227 {
228 228 p_cursorIndex = index;
229 229 p_cursorBlinkEnable = true;
230 230 }
231 231
232 232 void bitfieldsElement::leave()
233 233 {
234 234 p_cursorBlinkEnable = false;
235 235 }
236 236
237 237 uint bitfieldsElement::cursorIndex()
238 238 {
239 239 return p_cursorIndex;
240 240 }
241 241
242 242 uint bitfieldsElement::cursorIndex(int xPos)
243 243 {
244 244 uint index=0;
245 245 xPos-=p_xMargins;
246 246 if(xPos<p_boundingRec.width())
247 247 {
248 248 index = 31 - ((4*xPos)/((4*p_dx)+p_xMargins));
249 249 }
250 250 return index;
251 251 }
252 252
253 253 void bitfieldsElement::setFont(QFont font)
254 254 {
255 255 p_font = font;
256 256 p_dx=QFontMetrics(p_font).width("0")+2;
257 257 updateBoundingRect();
258 258 }
General Comments 0
You need to be logged in to leave comments. Login now