##// END OF EJS Templates
added outdated marker for registers
jeandet -
r14:9180c6ebb055 default
parent child
Show More
@@ -1,241 +1,253
1 1 #include "peripheralwidget.h"
2 2
3 3 peripheralWidget::peripheralWidget(const QString &name, qint32 baseAddress, QWidget *parent) :
4 4 QWidget(parent)
5 5 {
6 6 p_name = name;
7 7 p_timer = new QTimer(this);
8 8 p_timer->setInterval(500);
9 9 p_baseAddress = baseAddress;
10 10 p_header = p_name + QString(" @0x%1").arg((uint)p_baseAddress,8,16);
11 11 setAttribute(Qt::WA_AlwaysShowToolTips);
12 12 setMouseTracking(true);
13 13 setFocusPolicy(Qt::StrongFocus);
14 14 selectedReg = -1;
15 15 registersWdgts.clear();
16 16 connect(p_timer,SIGNAL(timeout()),this,SLOT(blinkCursor()));
17 17 setFont(QFont("Utopia", 14,QFont::Bold));
18 18 }
19 19
20 20 void peripheralWidget::blinkCursor()
21 21 {
22 22 if(selectedReg!=-1)
23 23 registersWdgts.at(selectedReg)->blinkCursor();
24 24 repaint();
25 25 }
26 26
27 27 void peripheralWidget::addRegister(const QString &name, qint32 address)
28 28 {
29 29 /*TODO Should regs by address*/
30 30 registersWdgts.append(new registerWidget(name,address));
31 31 connect(registersWdgts.last(),SIGNAL(repaint()),this,SLOT(repaint()));
32 32 }
33 33
34 34 void peripheralWidget::leave()
35 35 {
36 36 if(selectedReg!=-1)
37 37 {
38 38 p_timer->stop();
39 39 registersWdgts.at(selectedReg)->leave();
40 40 selectedReg = -1;
41 41 repaint();
42 42 }
43 43 }
44 44
45 45 void peripheralWidget::enter(int cursorIndex, bool fromTop)
46 46 {
47 47 if(cursorIndex>=0 && cursorIndex<32)
48 48 {
49 49 if(fromTop)
50 50 {
51 51 registersWdgts.at(0)->enter(cursorIndex);
52 52 selectedReg = 0;
53 53 }
54 54 else
55 55 {
56 56 registersWdgts.at(registersWdgts.count()-1)->enter(cursorIndex);
57 57 selectedReg = registersWdgts.count()-1;
58 58 }
59 59 p_timer->start();
60 60 this->setFocus();
61 61 }
62 62 }
63 63
64 64 void peripheralWidget::mousePressEvent(QMouseEvent *event)
65 65 {
66 66 p_timer->stop();
67 67 if(selectedReg!=-1)
68 68 {
69 69 registersWdgts.at(selectedReg)->leave();
70 70 selectedReg = -1;
71 71 }
72 72 for(int i=0; i<registersWdgts.count();i++)
73 73 {
74 74 if(registersWdgts.at(i)->contains(event->pos()))
75 75 {
76 76 registersWdgts.at(i)->enter(registersWdgts.at(i)->cursorIndex(event->pos().x()));
77 77 selectedReg = i;
78 78 p_timer->start();
79 79 emit clicked(this);
80 80 }
81 81 }
82 82 repaint();
83 83 }
84 84
85 85 void peripheralWidget::mouseMoveEvent(QMouseEvent *event)
86 86 {
87 87 bool match=false;
88 88 if(event->buttons()==Qt::LeftButton)
89 89 {
90 90 for(int i=0; i<registersWdgts.count();i++)
91 91 {
92 92 if(registersWdgts.at(i)->contains(event->pos()))
93 93 {
94 94 registersWdgts.at(i)->updateSelection(registersWdgts.at(i)->cursorIndex(event->pos().x()));
95 95 }
96 96 }
97 97 }
98 98 else
99 99 {
100 100 for(int i=0; i<registersWdgts.count();i++)
101 101 {
102 102 if(registersWdgts.at(i)->contains(event->pos()))
103 103 {
104 104 match = true;
105 105 int bitfieldIndex=registersWdgts.at(i)->cursorIndex(event->pos().x());
106 106
107 107 QString toolTipText ="<b>< font color='Black'>"+registersWdgts.at(i)->bitFieldName(bitfieldIndex) +"</b><br />";
108 108 toolTipText+= "Hexadecimal=<b>< font color='Blue'>"+registersWdgts.at(i)->bitFieldToHex(bitfieldIndex)+"</b>";
109 109 toolTipText+= " Decimal=<b>< font color='BlueViolet'>"+registersWdgts.at(i)->bitFieldToDec(bitfieldIndex)+"</b><br />";
110 110 toolTipText+= registersWdgts.at(i)->bitFieldDesc(bitfieldIndex);
111 111 QToolTip::showText(event->globalPos(),toolTipText,(QWidget*)this);
112 112 }
113 113 }
114 114 if(!match)QToolTip::hideText();
115 115 }
116 116 }
117 117
118 118 void peripheralWidget::mouseReleaseEvent(QMouseEvent *event)
119 119 {
120 120 Q_UNUSED(event)
121 121 }
122 122
123 123 void peripheralWidget::keyPressEvent(QKeyEvent *event)
124 124 {
125 125 if(this->selectedReg!=-1){
126 126 if(event->modifiers()==Qt::ControlModifier)
127 127 {
128 128 switch(event->key())
129 129 {
130 130 case Qt::Key_Up:
131 131 registersWdgts.at(selectedReg)->set(registersWdgts.at(selectedReg)->cursorIndex());
132 132 break;
133 133 case Qt::Key_Down:
134 134 registersWdgts.at(selectedReg)->clear(registersWdgts.at(selectedReg)->cursorIndex());
135 135 break;
136 136 case Qt::Key_W:
137 137 emit writeRegSig(registersWdgts.at(selectedReg)->address(),registersWdgts.at(selectedReg)->value());
138 registersWdgts.at(selectedReg)->setUpdated(true);
139 registersWdgts.at(selectedReg)->repaint();
138 140 break;
139 141 case Qt::Key_R:
140 142 qint32 value;
141 143 value = emit readRegSig(registersWdgts.at(selectedReg)->address());
142 144 registersWdgts.at(selectedReg)->setValue(value);
143 145 break;
144 146 default:
145 147 break;
146 148 }
147 149 }
148 150 else
149 151 {
150 152 switch(event->key())
151 153 {
152 154 case Qt::Key_0:
153 155 registersWdgts.at(selectedReg)->clear(registersWdgts.at(selectedReg)->cursorIndex());
154 156 registersWdgts.at(selectedReg)->moveCursorRight(1);
155 157 break;
156 158 case Qt::Key_1:
157 159 registersWdgts.at(selectedReg)->set(registersWdgts.at(selectedReg)->cursorIndex());
158 160 registersWdgts.at(selectedReg)->moveCursorRight(1);
159 161 break;
160 162 case Qt::Key_Right:
161 163 registersWdgts.at(selectedReg)->moveCursorRight(1);
162 164 this->repaint();
163 165 break;
164 166 case Qt::Key_Left:
165 167 registersWdgts.at(selectedReg)->moveCursorLeft(1);
166 168 this->repaint();
167 169 break;
168 170 case Qt::Key_Up:
169 171 up();
170 172 break;
171 173 case Qt::Key_Down:
172 174 down();
173 175 break;
176 case Qt::Key_W:
177 emit writeRegSig(registersWdgts.at(selectedReg)->address(),registersWdgts.at(selectedReg)->value());
178 registersWdgts.at(selectedReg)->setUpdated(true);
179 registersWdgts.at(selectedReg)->repaint();
180 break;
181 case Qt::Key_R:
182 qint32 value;
183 value = emit readRegSig(registersWdgts.at(selectedReg)->address());
184 registersWdgts.at(selectedReg)->setValue(value);
185 break;
174 186 default:
175 187 break;
176 188 }
177 189 }
178 190
179 191 }
180 192 }
181 193
182 194 void peripheralWidget::paintEvent(QPaintEvent *event)
183 195 {
184 196 Q_UNUSED(event)
185 197
186 198 QPainter painter(this);
187 199 QPoint offset=QPoint(0,0);
188 200 int nameWidth = fontMetrics().width(p_header);
189 201 if(registersWdgts.count()==0)
190 202 {
191 203 setMinimumSize(2*nameWidth+10,fontMetrics().height()+10);
192 204 }
193 205 painter.drawText((this->minimumWidth()/2)-nameWidth,4,fontMetrics().width(p_header),fontMetrics().height()+4,Qt::AlignCenter,p_header);
194 206 offset+=QPoint(0,fontMetrics().height()+8);
195 207 for(int i=0;i<registersWdgts.count();i++)
196 208 {
197 209 offset = registersWdgts.at(i)->paint(&painter,offset);
198 210 }
199 211 if(registersWdgts.count()>0)
200 212 {
201 213 setMinimumSize(registersWdgts.first()->boundingRect().width(),offset.y());
202 214 }
203 215 updateGeometry();
204 216
205 217 }
206 218
207 219 void peripheralWidget::up()
208 220 {
209 221 if(selectedReg!=-1)
210 222 {
211 223 if(selectedReg >0)
212 224 {
213 225 registersWdgts.at(selectedReg-1)->enter(registersWdgts.at(selectedReg)->cursorIndex());
214 226 registersWdgts.at(selectedReg)->leave();
215 227 selectedReg-=1;
216 228 repaint();
217 229 }
218 230 else
219 231 {
220 232 emit upSig(this,registersWdgts.at(selectedReg)->cursorIndex());
221 233 }
222 234 }
223 235 }
224 236
225 237 void peripheralWidget::down()
226 238 {
227 239 if(selectedReg!=-1)
228 240 {
229 241 if(selectedReg <(registersWdgts.count()-1))
230 242 {
231 243 registersWdgts.at(selectedReg+1)->enter(registersWdgts.at(selectedReg)->cursorIndex());
232 244 registersWdgts.at(selectedReg)->leave();
233 245 selectedReg+=1;
234 246 repaint();
235 247 }
236 248 else
237 249 {
238 250 emit downSig(this,registersWdgts.at(selectedReg)->cursorIndex());
239 251 }
240 252 }
241 253 }
@@ -1,393 +1,411
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 10 p_addressEl = new regWidgetElement(QString("0x%1").arg((uint)p_address,8,16).replace(" ","0"),QFont("Utopia", 12),10,4);
11 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 = 6;
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 return QPoint(0,p_boundingRect.height()+p_boundingRect.y()+p_yMargins);
40 40 }
41 41
42 42 QRect registerWidget::boundingRect()
43 43 {
44 44 return p_boundingRect;
45 45 }
46 46
47 47 uint registerWidget::cursorIndex()
48 48 {
49 49 return p_fieldsEl->cursorIndex();
50 50 }
51 51
52 52 uint registerWidget::cursorIndex(int xPos)
53 53 {
54 54 if(xPos>p_addressEl->boundingRect().width() && xPos<(p_addressEl->boundingRect().width()+p_fieldsEl->boundingRect().width()))
55 55 {
56 56 return p_fieldsEl->cursorIndex(xPos-p_addressEl->boundingRect().width());
57 57 }
58 58 return 0;
59 59 }
60 60
61 61 void registerWidget::updateSelection(int index)
62 62 {
63 63 p_fieldsEl->updateSelection(index);
64 64 emit this->repaint();
65 65 }
66 66
67 67 qint32 registerWidget::address()
68 68 {
69 69 return p_address;
70 70 }
71 71
72 72 qint32 registerWidget::value()
73 73 {
74 74 return p_value;
75 75 }
76 76
77 77 void registerWidget::setBitFieldAttribute(uint startIndex, uint stopIndex, const QString &name, const QString &description, bool rw)
78 78 {
79 79 if(startIndex<=stopIndex && stopIndex<32)
80 80 {
81 81 int index= p_fieldsEl->addAttribute(name,description,rw );
82 82 for(uint i=startIndex;i<=stopIndex;i++)
83 83 {
84 84 p_fieldsEl->setAttribute(i,index);
85 85 }
86 86 }
87 87 }
88 88
89 89 QString registerWidget::bitFieldDesc(int bitIndex)
90 90 {
91 91 if(bitIndex>=0 && bitIndex<32)
92 92 {
93 93 return p_fieldsEl->description(bitIndex);
94 94 }
95 95 return QString("Out of range");
96 96 }
97 97
98 98 QString registerWidget::bitFieldName(int bitIndex)
99 99 {
100 100 if(bitIndex>=0 && bitIndex<32)
101 101 {
102 102 return p_fieldsEl->name(bitIndex);
103 103 }
104 104 return QString("Out of range");
105 105 }
106 106
107 107 QString registerWidget::bitFieldToHex(int bitIndex)
108 108 {
109 109 if(bitIndex>=0 && bitIndex<32)
110 110 {
111 111 return p_fieldsEl->valueHex(bitIndex);
112 112 }
113 113 return QString("Out of range");
114 114 }
115 115
116 116 QString registerWidget::bitFieldToDec(int bitIndex)
117 117 {
118 118 if(bitIndex>=0 && bitIndex<32)
119 119 {
120 120 return p_fieldsEl->valueDec(bitIndex);
121 121 }
122 122 return QString("Out of range");
123 123 }
124 124
125 125 QString registerWidget::bitFieldToBin(int bitIndex)
126 126 {
127 127 if(bitIndex>=0 && bitIndex<32)
128 128 {
129 129 // TODO do the convertion
130 130 return p_fieldsEl->valueDec(bitIndex);
131 131 }
132 132 return QString("Out of range");
133 133 }
134 134
135 void registerWidget::setValue(qint32 value)
135 void registerWidget::setValue(qint32 value, bool updated)
136 136 {
137 137 this->p_value = value;
138 138 this->p_fieldsEl->setValue(QString("%1").arg((uint)p_value,32,2).replace(" ","0"));
139 this->p_fieldsEl->setUpdated(updated);
139 140 emit this->repaint();
140 141 }
141 142
142 143 void registerWidget::blinkCursor()
143 144 {
144 145 p_fieldsEl->blinkCursor();
145 146 //repaint();
146 147 }
147 148
148 149 void registerWidget::moveCursorLeft(int count)
149 150 {
150 151 p_fieldsEl->moveCursorLeft(count);
151 152 p_fieldsEl->blinkCursor();
152 153 }
153 154
154 155 void registerWidget::moveCursorRight(int count)
155 156 {
156 157 p_fieldsEl->moveCursorRight(count);
157 158 p_fieldsEl->blinkCursor();
158 159 }
159 160
160 161 void registerWidget::enter(int index)
161 162 {
162 163 p_fieldsEl->enter(index);
163 164 }
164 165
165 166 void registerWidget::leave()
166 167 {
167 168 p_fieldsEl->leave();
168 169 }
169 170
170 171 void registerWidget::clear(int index)
171 172 {
172 p_value &= ~(1<<index);
173 this->p_fieldsEl->setValue(QString("%1").arg((uint)p_value,32,2).replace(" ","0"));
174 emit this->repaint();
173 if(!this->p_fieldsEl->readonly(index))
174 {
175 qint32 old = p_value;
176 p_value &= ~(1<<index);
177 if(old!=p_value)
178 p_fieldsEl->setUpdated(false);
179 this->p_fieldsEl->setValue(QString("%1").arg((uint)p_value,32,2).replace(" ","0"));
180 emit this->repaint();
181 }
175 182 }
176 183
177 184 void registerWidget::set(int index)
178 185 {
179 186 if(!this->p_fieldsEl->readonly(index))
180 187 {
188 qint32 old = p_value;
181 189 p_value |= 1<<index;
190 if(old!=p_value)
191 p_fieldsEl->setUpdated(false);
182 192 this->p_fieldsEl->setValue(QString("%1").arg((uint)p_value,32,2).replace(" ","0"));
183 193 emit this->repaint();
184 194 }
185 195 }
186 196
187 197 void registerWidget::updateBoundingRect()
188 198 {
189 199 p_boundingRect.setHeight(p_fieldsEl->boundingRect().height()+(p_yMargins*2));
190 200 p_boundingRect.setWidth(p_fieldsEl->boundingRect().width()+p_addressEl->boundingRect().width()+p_nameEl->boundingRect().width()+(p_xMargins*2));
191 201 }
192 202
193 203
194 204
195 205 bitfieldsElement::bitfieldsElement(const QString &value, QFont font, int xMargin, int yMargin)
196 206 :regWidgetElement(value,font,xMargin,yMargin)
197 207 {
198 208 this->attributesLUT.append(new bitFieldAttribute(false,"UNSUSED","UNSUSED"));
199 209 for(int i=0;i<32;i++)
200 210 {
201 211 attributesIndex[i] = 0;
202 212 }
203 213 p_startSelectionIndex = -1;
204 214 p_stopSelectionIndex = -1;
205 215 p_cursorIndex = -1;
206 216 p_dx=QFontMetrics(p_font).width("0")+4;
207 217 p_blinkTextBgColor = QColor(Qt::black);
208 218 p_blinkTextColor = QColor(Qt::white);
219 p_outdated = QColor(Qt::red);
209 220 p_cursorBlinkEnable = false;
221 p_changed = false;
210 222 updateBoundingRect();
211 223 }
212 224
213 225 QPoint bitfieldsElement::paint(QPainter *painter)
214 226 {
215 227 painter->fillRect(4,4,p_boundingRec.width(),p_boundingRec.height(),Qt::darkGray);
216 228 painter->fillRect(0,0,p_boundingRec.width(),p_boundingRec.height(),Qt::white);
217 229 painter->setFont(p_font);
218 230 int lastAttributeIndex = attributesIndex[31];
219 231 int xpos=p_xMargins;
220 232 for(int i=0;i<(32/4);i++)
221 233 {
222 234 for(int l = 0;l<4;l++)
223 235 {
224 236 if(p_cursorBlinkEnable == false || (31-(int)p_cursorIndex) != ((i*4)+l))
225 237 {
226 238 if((int)p_startSelectionIndex!=-1 && (int)p_stopSelectionIndex!=-1 && p_startSelectionIndex <=(uint)31-(uint)((i*4)+l) && p_stopSelectionIndex >=(uint)31-(uint)((i*4)+l))
227 239 {
228 240 QPen svg = painter->pen();
229 241 painter->setPen(p_blinkTextColor);
230 242 painter->fillRect(xpos-1,0,p_dx,p_boundingRec.height(),Qt::blue);
231 243 painter->drawText(xpos,QFontMetrics(p_font).ascent()+p_yMargins,p_valueStr.at((i*4)+l));
232 244 painter->setPen(svg);
233 245 }
234 246 else
235 247 {
236 248 if(attributesLUT[attributesIndex[31-((i*4)+l)]]->rw==false)
237 249 {
238 250 painter->fillRect(xpos-1,0,p_dx,p_boundingRec.height(),Qt::lightGray);
239 251 }
240 252 else
241 253 {
242 254 painter->fillRect(xpos-1,0,p_dx,p_boundingRec.height(),Qt::white);
243 255 }
244 256 painter->drawText(xpos,QFontMetrics(p_font).ascent()+p_yMargins,p_valueStr.at((i*4)+l));
245 257 }
246 258 }
247 259 else
248 260 {
249 261 QPen svg = painter->pen();
250 262 painter->setPen(p_blinkTextColor);
251 263 painter->fillRect(xpos-1,0,p_dx,p_boundingRec.height(),p_blinkTextBgColor);
252 264 painter->drawText(xpos,QFontMetrics(p_font).ascent()+p_yMargins,p_valueStr.at((i*4)+l));
253 265 painter->setPen(svg);
254 266 }
255 267 if(lastAttributeIndex!=attributesIndex[31-((i*4)+l)])
256 268 painter->drawLine(xpos-(p_xMargins/2),0,xpos-(p_xMargins/2),p_boundingRec.height());
257 269 lastAttributeIndex=attributesIndex[31-((i*4)+l)];
258 270 xpos+=p_dx;
259 271 }
260 272 if(i==3)
261 273 painter->drawLine(xpos+(p_xMargins/2),p_boundingRec.height()-6,xpos+(p_xMargins/2),p_boundingRec.height()+12);
262 274 else if(i<7)
263 275 painter->drawLine(xpos+(p_xMargins/2),p_boundingRec.height()-6,xpos+(p_xMargins/2),p_boundingRec.height()+6);
264 276 xpos+=p_xMargins;
265 277 }
278 QPen svg = painter->pen();
279 if(p_changed)
280 {
281 painter->setPen(p_outdated);
282 }
266 283 painter->drawRect(0,0,p_boundingRec.width(),p_boundingRec.height());
284 painter->setPen(svg);
267 285 return QPoint(p_boundingRec.width()+4+p_xMargins,0);
268 286 }
269 287
270 288 void bitfieldsElement::updateBoundingRect()
271 289 {
272 290 p_boundingRec.setHeight(QFontMetrics(p_font).boundingRect(p_valueStr).height()+(p_yMargins*2));
273 291 int width = (((4*(p_dx)) + p_xMargins) * 8) + (p_xMargins);
274 292 p_boundingRec.setWidth(width);
275 293 }
276 294
277 295 void bitfieldsElement::setValue(const QString &value)
278 296 {
279 297 p_valueStr = value;
280 298 updateBoundingRect();
281 299 }
282 300
283 301 void bitfieldsElement::blinkCursor()
284 302 {
285 303 p_cursorBlinkEnable = !p_cursorBlinkEnable;
286 304 }
287 305
288 306 void bitfieldsElement::moveCursorLeft(int count)
289 307 {
290 308 p_cursorIndex+=count;
291 309 if(31<p_cursorIndex)
292 310 {
293 311 p_cursorIndex = 31;
294 312 }
295 313 p_startSelectionIndex = p_cursorIndex;
296 314 p_stopSelectionIndex = p_cursorIndex;
297 315
298 316 }
299 317
300 318 void bitfieldsElement::moveCursorRight(int count)
301 319 {
302 320 p_cursorIndex -= count;
303 321 if(31<p_cursorIndex)
304 322 {
305 323 p_cursorIndex = 0;
306 324 }
307 325 p_startSelectionIndex = p_cursorIndex;
308 326 p_stopSelectionIndex = p_cursorIndex;
309 327 }
310 328
311 329 void bitfieldsElement::enter(int index)
312 330 {
313 331 p_cursorIndex = index;
314 332 p_cursorBlinkEnable = true;
315 333 p_startSelectionIndex = -1;
316 334 p_stopSelectionIndex = -1;
317 335 }
318 336
319 337 void bitfieldsElement::leave()
320 338 {
321 339 p_cursorBlinkEnable = false;
322 340 p_cursorIndex = -1;
323 341 p_startSelectionIndex = -1;
324 342 p_stopSelectionIndex = -1;
325 343 }
326 344
327 345 uint bitfieldsElement::cursorIndex()
328 346 {
329 347 return p_cursorIndex;
330 348 }
331 349
332 350 uint bitfieldsElement::cursorIndex(int xPos)
333 351 {
334 352 uint index=0;
335 353 xPos-=p_xMargins;
336 354 if(xPos<p_boundingRec.width())
337 355 {
338 356 index = 31 - ((4*xPos)/((4*p_dx)+p_xMargins));
339 357 }
340 358 return index;
341 359 }
342 360
343 361 void bitfieldsElement::setFont(QFont font)
344 362 {
345 363 p_font = font;
346 364 p_dx=QFontMetrics(p_font).width("0")+2;
347 365 updateBoundingRect();
348 366 }
349 367
350 368 void bitfieldsElement::updateSelection(int index)
351 369 {
352 370 if((int)p_cursorIndex!=-1)
353 371 {
354 372 if((int)p_startSelectionIndex!=-1 && (int)p_stopSelectionIndex!= -1)
355 373 {
356 374 if((uint)index>=p_stopSelectionIndex)
357 375 {
358 376 p_stopSelectionIndex = index;
359 377 p_cursorIndex = index;
360 378 }
361 379 else
362 380 {
363 381 p_startSelectionIndex = index;
364 382 p_cursorIndex = index;
365 383 }
366 384 }
367 385 else
368 386 {
369 387 if((uint)index>p_cursorIndex)
370 388 {
371 389 p_startSelectionIndex = p_cursorIndex;
372 390 p_stopSelectionIndex = index;
373 391 }
374 392 else
375 393 {
376 394 p_startSelectionIndex = index;
377 395 p_stopSelectionIndex = p_cursorIndex;
378 396 }
379 397 }
380 398 }
381 399 }
382 400
383 401
384 402
385 403
386 404
387 405
388 406
389 407
390 408
391 409
392 410
393 411
@@ -1,226 +1,235
1 1 #ifndef REGISTERWIDGET_H
2 2 #define REGISTERWIDGET_H
3 3 #include <QtWidgets>
4 4 #include <QWidget>
5 5 #include <QTimer>
6 6 #if defined(LPPMON_SDK_BUILD)
7 7 # define LPPMON_SDK_EXPORT Q_DECL_EXPORT
8 8 #else
9 9 # define LPPMON_SDK_EXPORT Q_DECL_IMPORT
10 10 #endif
11 11
12 12 class LPPMON_SDK_EXPORT regWidgetElement
13 13 {
14 14 public:
15 15 regWidgetElement(const QString& value,QFont font,int xMargin,int yMargin)
16 16 {
17 17 p_valueStr = value;
18 18 p_font = font;
19 19 p_xMargins = xMargin;
20 20 p_yMargins = yMargin;
21 21 updateBoundingRect();
22 22 }
23 23 void setValue(const QString& value)
24 24 {
25 25 p_valueStr = value;
26 26 updateBoundingRect();
27 27 }
28 28 void setFont(QFont font)
29 29 {
30 30 p_font = font;
31 31 updateBoundingRect();
32 32 }
33 33 QSize boundingRect(){return p_boundingRec;}
34 34 QString value(){return p_valueStr;}
35 35 const QChar at ( int position ) const{return p_valueStr.at(position);}
36 36 QFont font(){return p_font;}
37 37 int xMargin(){return p_xMargins;}
38 38 int yMargin(){return p_yMargins;}
39 39 QFontMetrics fontMetrics(){return QFontMetrics(p_font);}
40 40 QPoint paint(QPainter* painter)
41 41 {
42 42 painter->setFont(p_font);
43 43 painter->drawText(0,QFontMetrics(p_font).ascent()+p_yMargins,p_valueStr);
44 44 return QPoint(p_boundingRec.width(),0);
45 45 }
46 46 void updateBoundingRect()
47 47 {
48 48 p_boundingRec.setHeight(QFontMetrics(p_font).boundingRect(p_valueStr).height()+(p_yMargins*2));
49 49 p_boundingRec.setWidth(QFontMetrics(p_font).boundingRect(p_valueStr).width()+(p_xMargins*2));
50 50 }
51 51
52 52 protected:
53 53 QString p_valueStr;
54 54 QFont p_font;
55 55 QSize p_boundingRec;
56 56 int p_xMargins;
57 57 int p_yMargins;
58 58 };
59 59
60 60 class LPPMON_SDK_EXPORT bitfieldsElement: public regWidgetElement
61 61 {
62 62 class bitFieldAttribute
63 63 {
64 64 public:
65 65 bitFieldAttribute(bool rw,QString name,QString description)
66 66 {
67 67 this->rw = rw;
68 68 this->Name = name;
69 69 this->description = description;
70 70 }
71 71 bool rw;
72 72 QString description;
73 73 QString Name;
74 74 };
75 75 public:
76 76 bitfieldsElement(const QString& value,QFont font,int xMargin,int yMargin);
77 77 QPoint paint(QPainter* painter);
78 78
79 79 void updateBoundingRect();
80 80 void setValue(const QString& value);
81 81 void blinkCursor();
82 82 void moveCursorLeft(int count);
83 83 void moveCursorRight(int count);
84 84 void enter(int index);
85 85 void leave();
86 86 uint cursorIndex();
87 87 uint cursorIndex(int xPos);
88 88 void setFont(QFont font);
89 89 void updateSelection(int index);
90 90 int addAttribute(const QString& name,const QString& description,bool rw)
91 91 {
92 92 attributesLUT.append(new bitFieldAttribute(rw,name,description));
93 93 return attributesLUT.count()-1;
94 94 }
95 95
96 96 int setAttribute(int bitIndex,int attributeIndex)
97 97 {
98 98 if(bitIndex>=0 && bitIndex<32 && attributeIndex>=0 && attributeIndex<(attributesLUT.count()))
99 99 {
100 100 attributesIndex[bitIndex]=attributeIndex;
101 101 return 0;
102 102 }
103 103 return -1;
104 104 }
105 105
106 106 QString description(int bitIndex)
107 107 {
108 108 if(bitIndex>=0 && bitIndex<32)
109 109 return attributesLUT.at(attributesIndex[bitIndex])->description;
110 110 return QString("");
111 111 }
112 112 QString name(int bitIndex)
113 113 {
114 114 if(bitIndex>=0 && bitIndex<32)
115 115 return attributesLUT.at(attributesIndex[bitIndex])->Name;
116 116 return QString("");
117 117 }
118 118 bool readonly(int bitIndex)
119 119 {
120 120 if(bitIndex>=0 && bitIndex<32)
121 121 return !attributesLUT.at(attributesIndex[bitIndex])->rw;
122 122 return false;
123 123 }
124 124 QString valueHex(int index)
125 125 {
126 126 if(index>=0 && index<32)
127 127 {
128 128 return "0x" + QString::number(p_valueUint(index),16);
129 129 }
130 130 return QString("");
131 131 }
132 132 QString valueDec(int index)
133 133 {
134 134 if(index>=0 && index<32)
135 135 {
136 136 return QString::number(p_valueUint(index),10);
137 137 }
138 138 return QString("");
139 139 }
140 void setUpdated(bool updated=true)
141 {
142 this->p_changed = !updated;
143 }
140 144 private:
141 145 uint p_valueUint(int index)
142 146 {
143 147 uint value;
144 148 int attributeIndex = attributesIndex[index];
145 149 int startIndex = index;
146 150 int stopIndex=0;
147 151 while (startIndex>0)
148 152 {
149 153 if(attributesIndex[startIndex-1]==attributeIndex)
150 154 startIndex--;
151 155 else
152 156 break;
153 157 }
154 158 stopIndex = startIndex;
155 159 while (stopIndex<32)
156 160 {
157 161 if(attributesIndex[stopIndex+1]==attributeIndex)
158 162 stopIndex++;
159 163 else
160 164 break;
161 165 }
162 166 bool ok;
163 167 value = p_valueStr.toUInt(&ok,2);
164 168 value = (uint)0xFFFFFFFF & (value<<(31-stopIndex));
165 169 value = (uint)0xFFFFFFFF & (value>>(31-stopIndex+startIndex));
166 170 return value;
167 171 }
168 172 int attributesIndex[32];
169 173 uint p_cursorIndex;
170 174 uint p_startSelectionIndex;
171 175 uint p_stopSelectionIndex;
172 176 bool p_cursorBlinkEnable;
177 bool p_changed;
173 178 int p_dx;
174 179 QList<bitFieldAttribute*> attributesLUT;
175 QColor p_blinkTextColor,p_blinkTextBgColor;
180 QColor p_blinkTextColor,p_blinkTextBgColor,p_outdated;
176 181
177 182 };
178 183
179 184
180 185 class LPPMON_SDK_EXPORT registerWidget : public QObject
181 186 {
182 187 Q_OBJECT
183 188 public:
184 189 explicit registerWidget(const QString& name,qint32 address,QObject *parent = 0);
185 190 ~registerWidget();
186 191 int contains(const QPointF &point);
187 192 QPoint paint(QPainter* painter,QPoint offset);
188 193 QRect boundingRect();
189 194 uint cursorIndex();
190 195 uint cursorIndex(int xPos);
191 196 void updateSelection(int index);
192 197 qint32 address();
193 198 qint32 value();
194 199 void setBitFieldAttribute(uint startIndex,uint stopIndex,const QString& name,const QString& description,bool rw);
195 200 QString bitFieldDesc(int bitIndex);
196 201 QString bitFieldName(int bitIndex);
197 202 QString bitFieldToHex(int bitIndex);
198 203 QString bitFieldToDec(int bitIndex);
199 204 QString bitFieldToBin(int bitIndex);
205 void setUpdated(bool updated=true)
206 {
207 this->p_fieldsEl->setUpdated(updated);
208 }
200 209 signals:
201 210 void cursorUp(int pos);
202 211 void cursorDown(int pos);
203 212 void valueChanged(qint32 value);
204 213 void repaint();
205 214
206 215 public slots:
207 void setValue(qint32 value);
216 void setValue(qint32 value,bool updated=true);
208 217 void blinkCursor();
209 218 void moveCursorLeft(int count);
210 219 void moveCursorRight(int count);
211 220 void enter(int index=0);
212 221 void leave();
213 222 void clear(int index);
214 223 void set(int index);
215 224 private:
216 225 void updateBoundingRect();
217 226 qint32 p_address;
218 227 qint32 p_value;
219 228 QRect p_boundingRect;
220 229 int p_xMargins;
221 230 int p_yMargins;
222 231 regWidgetElement* p_addressEl,*p_nameEl;
223 232 bitfieldsElement* p_fieldsEl;
224 233 };
225 234
226 235 #endif // REGISTERWIDGET_H
General Comments 0
You need to be logged in to leave comments. Login now