@@ -1,167 +1,177 | |||
|
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::mousePressEvent(QMouseEvent *event) |
|
35 | 35 | { |
|
36 | 36 | p_timer->stop(); |
|
37 | 37 | if(selectedReg!=-1) |
|
38 | 38 | { |
|
39 | 39 | registersWdgts.at(selectedReg)->leave(); |
|
40 | 40 | selectedReg = -1; |
|
41 | 41 | } |
|
42 | 42 | for(int i=0; i<registersWdgts.count();i++) |
|
43 | 43 | { |
|
44 | 44 | if(registersWdgts.at(i)->contains(event->pos())) |
|
45 | 45 | { |
|
46 | 46 | registersWdgts.at(i)->enter(registersWdgts.at(i)->cursorIndex(event->pos().x())); |
|
47 | 47 | selectedReg = i; |
|
48 | 48 | p_timer->start(); |
|
49 | 49 | } |
|
50 | 50 | } |
|
51 | 51 | repaint(); |
|
52 | 52 | } |
|
53 | 53 | |
|
54 | 54 | void peripheralWidget::mouseMoveEvent(QMouseEvent *event) |
|
55 | 55 | { |
|
56 | ||
|
56 | bool match=false; | |
|
57 | for(int i=0; i<registersWdgts.count();i++) | |
|
58 | { | |
|
59 | if(registersWdgts.at(i)->contains(event->pos())) | |
|
60 | { | |
|
61 | match = true; | |
|
62 | QToolTip::showText(event->globalPos(),registersWdgts.at(i)->bitFieldDesc(registersWdgts.at(i)->cursorIndex(event->pos().x())),(QWidget*)this); | |
|
63 | } | |
|
64 | } | |
|
65 | if(!match)QToolTip::hideText(); | |
|
57 | 66 | } |
|
58 | 67 | |
|
59 | 68 | void peripheralWidget::mouseReleaseEvent(QMouseEvent *event) |
|
60 | 69 | { |
|
61 | 70 | |
|
62 | 71 | } |
|
63 | 72 | |
|
64 | 73 | void peripheralWidget::keyPressEvent(QKeyEvent *event) |
|
65 | 74 | { |
|
66 | 75 | if(this->selectedReg!=-1){ |
|
67 | 76 | if(event->modifiers()==Qt::ControlModifier) |
|
68 | 77 | { |
|
69 | 78 | switch(event->key()) |
|
70 | 79 | { |
|
71 | 80 | case Qt::Key_Up: |
|
72 | 81 | registersWdgts.at(selectedReg)->set(registersWdgts.at(selectedReg)->cursorIndex()); |
|
73 | 82 | break; |
|
74 | 83 | case Qt::Key_Down: |
|
75 | 84 | registersWdgts.at(selectedReg)->clear(registersWdgts.at(selectedReg)->cursorIndex()); |
|
76 | 85 | break; |
|
77 | 86 | case Qt::Key_W: |
|
78 | 87 | emit writeRegSig(registersWdgts.at(selectedReg)->address(),registersWdgts.at(selectedReg)->value()); |
|
79 | 88 | break; |
|
80 | 89 | case Qt::Key_R: |
|
81 | 90 | qint32 value; |
|
82 | 91 | value = emit readRegSig(registersWdgts.at(selectedReg)->address()); |
|
92 | registersWdgts.at(selectedReg)->setValue(value); | |
|
83 | 93 | break; |
|
84 | 94 | default: |
|
85 | 95 | break; |
|
86 | 96 | } |
|
87 | 97 | } |
|
88 | 98 | else |
|
89 | 99 | { |
|
90 | 100 | switch(event->key()) |
|
91 | 101 | { |
|
92 | 102 | case Qt::Key_0: |
|
93 | 103 | registersWdgts.at(selectedReg)->clear(registersWdgts.at(selectedReg)->cursorIndex()); |
|
94 | 104 | registersWdgts.at(selectedReg)->moveCursorRight(1); |
|
95 | 105 | break; |
|
96 | 106 | case Qt::Key_1: |
|
97 | 107 | registersWdgts.at(selectedReg)->set(registersWdgts.at(selectedReg)->cursorIndex()); |
|
98 | 108 | registersWdgts.at(selectedReg)->moveCursorRight(1); |
|
99 | 109 | break; |
|
100 | 110 | case Qt::Key_Right: |
|
101 | 111 | registersWdgts.at(selectedReg)->moveCursorRight(1); |
|
102 | 112 | this->repaint(); |
|
103 | 113 | break; |
|
104 | 114 | case Qt::Key_Left: |
|
105 | 115 | registersWdgts.at(selectedReg)->moveCursorLeft(1); |
|
106 | 116 | this->repaint(); |
|
107 | 117 | break; |
|
108 | 118 | case Qt::Key_Up: |
|
109 | 119 | up(); |
|
110 | 120 | break; |
|
111 | 121 | case Qt::Key_Down: |
|
112 | 122 | down(); |
|
113 | 123 | break; |
|
114 | 124 | default: |
|
115 | 125 | break; |
|
116 | 126 | } |
|
117 | 127 | } |
|
118 | 128 | |
|
119 | 129 | } |
|
120 | 130 | } |
|
121 | 131 | |
|
122 | 132 | void peripheralWidget::paintEvent(QPaintEvent *event) |
|
123 | 133 | { |
|
124 | 134 | Q_UNUSED(event) |
|
125 | 135 | |
|
126 | 136 | QPainter painter(this); |
|
127 | 137 | QPoint offset=QPoint(0,0); |
|
128 | 138 | int nameWidth = fontMetrics().width(p_header); |
|
129 | 139 | if(registersWdgts.count()==0) |
|
130 | 140 | { |
|
131 | 141 | setMinimumSize(2*nameWidth+10,fontMetrics().height()+10); |
|
132 | 142 | } |
|
133 | 143 | painter.drawText((this->minimumWidth()/2)-nameWidth,4,fontMetrics().width(p_header),fontMetrics().height()+4,Qt::AlignCenter,p_header); |
|
134 | 144 | offset+=QPoint(0,fontMetrics().height()+8); |
|
135 | 145 | for(int i=0;i<registersWdgts.count();i++) |
|
136 | 146 | { |
|
137 | 147 | offset = registersWdgts.at(i)->paint(&painter,offset); |
|
138 | 148 | } |
|
139 | 149 | if(registersWdgts.count()>0) |
|
140 | 150 | { |
|
141 | 151 | setMinimumSize(registersWdgts.first()->boundingRect().width(),offset.y()); |
|
142 | 152 | } |
|
143 | 153 | updateGeometry(); |
|
144 | 154 | |
|
145 | 155 | } |
|
146 | 156 | |
|
147 | 157 | void peripheralWidget::up() |
|
148 | 158 | { |
|
149 | 159 | if(selectedReg!=-1 && selectedReg >0) |
|
150 | 160 | { |
|
151 | 161 | registersWdgts.at(selectedReg)->leave(); |
|
152 | 162 | selectedReg-=1; |
|
153 | 163 | registersWdgts.at(selectedReg)->enter(registersWdgts.at(selectedReg+1)->cursorIndex()); |
|
154 | 164 | repaint(); |
|
155 | 165 | } |
|
156 | 166 | } |
|
157 | 167 | |
|
158 | 168 | void peripheralWidget::down() |
|
159 | 169 | { |
|
160 | 170 | if(selectedReg!=-1 && selectedReg <(registersWdgts.count()-1)) |
|
161 | 171 | { |
|
162 | 172 | registersWdgts.at(selectedReg)->leave(); |
|
163 | 173 | selectedReg+=1; |
|
164 | 174 | registersWdgts.at(selectedReg)->enter(registersWdgts.at(selectedReg-1)->cursorIndex()); |
|
165 | 175 | repaint(); |
|
166 | 176 | } |
|
167 | 177 | } |
@@ -1,41 +1,49 | |||
|
1 | 1 | #ifndef PERIPHERALWIDGET_H |
|
2 | 2 | #define PERIPHERALWIDGET_H |
|
3 | 3 | |
|
4 | 4 | #include <QWidget> |
|
5 | 5 | #include <QGroupBox> |
|
6 | 6 | #include <QVBoxLayout> |
|
7 | 7 | #include <QList> |
|
8 | 8 | #include <QTimer> |
|
9 | 9 | #include "registerwidget.h" |
|
10 | ||
|
10 | /* | |
|
11 | * TODO ADD an outdated marker | |
|
12 | * Show outdated registers with a different color for example | |
|
13 | */ | |
|
11 | 14 | class peripheralWidget : public QWidget |
|
12 | 15 | { |
|
13 | 16 | Q_OBJECT |
|
14 | 17 | public: |
|
15 | 18 | explicit peripheralWidget(const QString& name,qint32 baseAddress, QWidget *parent = 0); |
|
16 | ||
|
19 | registerWidget* registerAt(int index) | |
|
20 | { | |
|
21 | if(index>=0 && index<registersWdgts.count()) | |
|
22 | return registersWdgts.at(index); | |
|
23 | return NULL; | |
|
24 | } | |
|
17 | 25 | signals: |
|
18 | 26 | void writeRegSig(qint32 address,qint32 value); |
|
19 | 27 | qint32 readRegSig(qint32 address); |
|
20 | 28 | public slots: |
|
21 | 29 | void blinkCursor(); |
|
22 | 30 | void addRegister(const QString& name,qint32 address); |
|
23 | 31 | protected: |
|
24 | 32 | void mousePressEvent(QMouseEvent *event); |
|
25 | 33 | void mouseMoveEvent(QMouseEvent *event); |
|
26 | 34 | void mouseReleaseEvent(QMouseEvent *event); |
|
27 | 35 | void keyPressEvent(QKeyEvent * event); |
|
28 | 36 | void paintEvent(QPaintEvent* event); |
|
29 | 37 | |
|
30 | 38 | private: |
|
31 | 39 | void up(); |
|
32 | 40 | void down(); |
|
33 | 41 | QString p_name; |
|
34 | 42 | QString p_header; |
|
35 | 43 | qint32 p_baseAddress; |
|
36 | 44 | QList<registerWidget*> registersWdgts; |
|
37 | 45 | int selectedReg; |
|
38 | 46 | QTimer* p_timer; |
|
39 | 47 | }; |
|
40 | 48 | |
|
41 | 49 | #endif // PERIPHERALWIDGET_H |
@@ -1,256 +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 | 10 | p_addressEl = new regWidgetElement(QString("0x%1").arg(p_address,8,16).replace(" ","0"),QFont("Utopia", 12),10,4); |
|
11 | 11 | p_fieldsEl = new bitfieldsElement(QString("%1").arg(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 | if(!this->p_fieldsEl->readonly(index)) | |
|
117 | { | |
|
116 | 118 | p_value |= 1<<index; |
|
117 | 119 | this->p_fieldsEl->setValue(QString("%1").arg((uint)p_value,32,2).replace(" ","0")); |
|
118 | 120 | emit this->repaint(); |
|
119 | 121 | } |
|
122 | } | |
|
120 | 123 | |
|
121 | 124 | void registerWidget::updateBoundingRect() |
|
122 | 125 | { |
|
123 | 126 | p_boundingRect.setHeight(p_fieldsEl->boundingRect().height()+(p_yMargins*2)); |
|
124 | 127 | p_boundingRect.setWidth(p_fieldsEl->boundingRect().width()+p_addressEl->boundingRect().width()+p_nameEl->boundingRect().width()+(p_xMargins*2)); |
|
125 | 128 | } |
|
126 | 129 | |
|
127 | 130 | |
|
128 | 131 | |
|
129 | 132 | bitfieldsElement::bitfieldsElement(const QString &value, QFont font, int xMargin, int yMargin) |
|
130 | 133 | :regWidgetElement(value,font,xMargin,yMargin) |
|
131 | 134 | { |
|
132 |
this->attributesLUT.append(new bitFieldAttribute( |
|
|
133 | this->attributesLUT.append(new bitFieldAttribute(false,"desc2")); | |
|
135 | this->attributesLUT.append(new bitFieldAttribute(false,"UNSUSED")); | |
|
134 | 136 | for(int i=0;i<32;i++) |
|
135 | 137 | { |
|
136 |
attributesIndex[i] = |
|
|
138 | attributesIndex[i] = 0; | |
|
137 | 139 | } |
|
138 | 140 | updateBoundingRect(); |
|
139 | 141 | p_blinkTextBgColor = QColor(Qt::black); |
|
140 | 142 | p_blinkTextColor = QColor(Qt::white); |
|
141 | 143 | p_cursorBlinkEnable = false; |
|
142 | 144 | p_dx=QFontMetrics(p_font).width("0")+2; |
|
143 | 145 | } |
|
144 | 146 | |
|
145 | 147 | QPoint bitfieldsElement::paint(QPainter *painter) |
|
146 | 148 | { |
|
147 | 149 | painter->fillRect(4,4,p_boundingRec.width(),p_boundingRec.height(),Qt::darkGray); |
|
148 | 150 | painter->fillRect(0,0,p_boundingRec.width(),p_boundingRec.height(),Qt::white); |
|
149 | 151 | painter->setFont(p_font); |
|
150 | 152 | int xpos=p_xMargins; |
|
151 | 153 | for(int i=0;i<(32/4);i++) |
|
152 | 154 | { |
|
153 | 155 | for(int l = 0;l<4;l++) |
|
154 | 156 | { |
|
155 | 157 | if(p_cursorBlinkEnable == false || (31-p_cursorIndex) != ((i*4)+l)) |
|
156 | 158 | { |
|
157 | if(attributesLUT[attributesIndex[(i*4)+l]]->rw==false) | |
|
159 | if(attributesLUT[attributesIndex[31-((i*4)+l)]]->rw==false) | |
|
158 | 160 | { |
|
159 | 161 | painter->fillRect(xpos-1,0,p_dx,p_boundingRec.height(),Qt::lightGray); |
|
160 | 162 | } |
|
161 | 163 | else |
|
162 | 164 | { |
|
163 | 165 | painter->fillRect(xpos-1,0,p_dx,p_boundingRec.height(),Qt::white); |
|
164 | 166 | } |
|
165 | 167 | painter->drawText(xpos,QFontMetrics(p_font).ascent()+p_yMargins,p_valueStr.at((i*4)+l)); |
|
166 | 168 | } |
|
167 | 169 | else |
|
168 | 170 | { |
|
169 | 171 | QPen svg = painter->pen(); |
|
170 | 172 | painter->setPen(p_blinkTextColor); |
|
171 | 173 | painter->fillRect(xpos-1,0,p_dx,p_boundingRec.height(),p_blinkTextBgColor); |
|
172 | 174 | painter->drawText(xpos,QFontMetrics(p_font).ascent()+p_yMargins,p_valueStr.at((i*4)+l)); |
|
173 | 175 | painter->setPen(svg); |
|
174 | 176 | } |
|
175 | 177 | xpos+=p_dx; |
|
176 | 178 | } |
|
177 | 179 | if(i==3) |
|
178 | 180 | painter->drawLine(xpos+(p_xMargins/2),p_boundingRec.height()-6,xpos+(p_xMargins/2),p_boundingRec.height()+6); |
|
179 | 181 | else if(i<7) |
|
180 | 182 | painter->drawLine(xpos+(p_xMargins/2),p_boundingRec.height()-2,xpos+(p_xMargins/2),p_boundingRec.height()+2); |
|
181 | 183 | xpos+=p_xMargins; |
|
182 | 184 | } |
|
183 | 185 | painter->drawRect(0,0,p_boundingRec.width(),p_boundingRec.height()); |
|
184 | 186 | return QPoint(p_boundingRec.width()+4+p_xMargins,0); |
|
185 | 187 | } |
|
186 | 188 | |
|
187 | 189 | void bitfieldsElement::updateBoundingRect() |
|
188 | 190 | { |
|
189 | 191 | p_boundingRec.setHeight(QFontMetrics(p_font).boundingRect(p_valueStr).height()+(p_yMargins*2)); |
|
190 | 192 | int width = (((4*(QFontMetrics(p_font).width("0") + 2)) + p_xMargins) * 8) + (p_xMargins); |
|
191 | 193 | p_boundingRec.setWidth(width); |
|
192 | 194 | } |
|
193 | 195 | |
|
194 | 196 | void bitfieldsElement::setValue(const QString &value) |
|
195 | 197 | { |
|
196 | 198 | p_valueStr = value; |
|
197 | 199 | updateBoundingRect(); |
|
198 | 200 | } |
|
199 | 201 | |
|
200 | 202 | void bitfieldsElement::blinkCursor() |
|
201 | 203 | { |
|
202 | 204 | p_cursorBlinkEnable = !p_cursorBlinkEnable; |
|
203 | 205 | } |
|
204 | 206 | |
|
205 | 207 | void bitfieldsElement::moveCursorLeft(int count) |
|
206 | 208 | { |
|
207 | 209 | p_cursorIndex+=count; |
|
208 | 210 | if(31<p_cursorIndex) |
|
209 | 211 | { |
|
210 | 212 | p_cursorIndex = 31; |
|
211 | 213 | } |
|
212 | 214 | |
|
213 | 215 | } |
|
214 | 216 | |
|
215 | 217 | void bitfieldsElement::moveCursorRight(int count) |
|
216 | 218 | { |
|
217 | 219 | p_cursorIndex -= count; |
|
218 | 220 | if(31<p_cursorIndex) |
|
219 | 221 | { |
|
220 | 222 | p_cursorIndex = 0; |
|
221 | 223 | } |
|
222 | 224 | } |
|
223 | 225 | |
|
224 | 226 | void bitfieldsElement::enter(int index) |
|
225 | 227 | { |
|
226 | 228 | p_cursorIndex = index; |
|
227 | 229 | p_cursorBlinkEnable = true; |
|
228 | 230 | } |
|
229 | 231 | |
|
230 | 232 | void bitfieldsElement::leave() |
|
231 | 233 | { |
|
232 | 234 | p_cursorBlinkEnable = false; |
|
233 | 235 | } |
|
234 | 236 | |
|
235 | 237 | uint bitfieldsElement::cursorIndex() |
|
236 | 238 | { |
|
237 | 239 | return p_cursorIndex; |
|
238 | 240 | } |
|
239 | 241 | |
|
240 | 242 | uint bitfieldsElement::cursorIndex(int xPos) |
|
241 | 243 | { |
|
242 | 244 | uint index=0; |
|
243 | 245 | xPos-=p_xMargins; |
|
244 | 246 | if(xPos<p_boundingRec.width()) |
|
245 | 247 | { |
|
246 | 248 | index = 31 - ((4*xPos)/((4*p_dx)+p_xMargins)); |
|
247 | 249 | } |
|
248 | 250 | return index; |
|
249 | 251 | } |
|
250 | 252 | |
|
251 | 253 | void bitfieldsElement::setFont(QFont font) |
|
252 | 254 | { |
|
253 | 255 | p_font = font; |
|
254 | 256 | p_dx=QFontMetrics(p_font).width("0")+2; |
|
255 | 257 | updateBoundingRect(); |
|
256 | 258 | } |
@@ -1,132 +1,167 | |||
|
1 | 1 | #ifndef REGISTERWIDGET_H |
|
2 | 2 | #define REGISTERWIDGET_H |
|
3 | 3 | #include <QtWidgets> |
|
4 | 4 | #include <QWidget> |
|
5 | 5 | #include <QTimer> |
|
6 | 6 | |
|
7 | 7 | class regWidgetElement |
|
8 | 8 | { |
|
9 | 9 | public: |
|
10 | 10 | regWidgetElement(const QString& value,QFont font,int xMargin,int yMargin) |
|
11 | 11 | { |
|
12 | 12 | p_valueStr = value; |
|
13 | 13 | p_font = font; |
|
14 | 14 | p_xMargins = xMargin; |
|
15 | 15 | p_yMargins = yMargin; |
|
16 | 16 | updateBoundingRect(); |
|
17 | 17 | } |
|
18 | 18 | void setValue(const QString& value) |
|
19 | 19 | { |
|
20 | 20 | p_valueStr = value; |
|
21 | 21 | updateBoundingRect(); |
|
22 | 22 | } |
|
23 | 23 | void setFont(QFont font) |
|
24 | 24 | { |
|
25 | 25 | p_font = font; |
|
26 | 26 | updateBoundingRect(); |
|
27 | 27 | } |
|
28 | 28 | QSize boundingRect(){return p_boundingRec;} |
|
29 | 29 | QString value(){return p_valueStr;} |
|
30 | 30 | const QChar at ( int position ) const{return p_valueStr.at(position);} |
|
31 | 31 | QFont font(){return p_font;} |
|
32 | 32 | int xMargin(){return p_xMargins;} |
|
33 | 33 | int yMargin(){return p_yMargins;} |
|
34 | 34 | QFontMetrics fontMetrics(){return QFontMetrics(p_font);} |
|
35 | 35 | QPoint paint(QPainter* painter) |
|
36 | 36 | { |
|
37 | 37 | painter->setFont(p_font); |
|
38 | 38 | painter->drawText(0,QFontMetrics(p_font).ascent()+p_yMargins,p_valueStr); |
|
39 | 39 | return QPoint(p_boundingRec.width(),0); |
|
40 | 40 | } |
|
41 | 41 | void updateBoundingRect() |
|
42 | 42 | { |
|
43 | 43 | p_boundingRec.setHeight(QFontMetrics(p_font).boundingRect(p_valueStr).height()+(p_yMargins*2)); |
|
44 | 44 | p_boundingRec.setWidth(QFontMetrics(p_font).boundingRect(p_valueStr).width()+(p_xMargins*2)); |
|
45 | 45 | } |
|
46 | 46 | |
|
47 | 47 | protected: |
|
48 | 48 | QString p_valueStr; |
|
49 | 49 | QFont p_font; |
|
50 | 50 | QSize p_boundingRec; |
|
51 | 51 | int p_xMargins; |
|
52 | 52 | int p_yMargins; |
|
53 | 53 | }; |
|
54 | 54 | |
|
55 | 55 | class bitfieldsElement: public regWidgetElement |
|
56 | 56 | { |
|
57 | 57 | class bitFieldAttribute |
|
58 | 58 | { |
|
59 | 59 | public: |
|
60 | 60 | bitFieldAttribute(bool rw,QString description) |
|
61 | 61 | { |
|
62 | 62 | this->rw = rw; |
|
63 | 63 | this->description = description; |
|
64 | 64 | } |
|
65 | 65 | bool rw; |
|
66 | 66 | QString description; |
|
67 | 67 | }; |
|
68 | 68 | public: |
|
69 | 69 | bitfieldsElement(const QString& value,QFont font,int xMargin,int yMargin); |
|
70 | 70 | QPoint paint(QPainter* painter); |
|
71 | 71 | |
|
72 | 72 | void updateBoundingRect(); |
|
73 | 73 | void setValue(const QString& value); |
|
74 | 74 | void blinkCursor(); |
|
75 | 75 | void moveCursorLeft(int count); |
|
76 | 76 | void moveCursorRight(int count); |
|
77 | 77 | void enter(int index); |
|
78 | 78 | void leave(); |
|
79 | 79 | uint cursorIndex(); |
|
80 | 80 | uint cursorIndex(int xPos); |
|
81 | 81 | void setFont(QFont font); |
|
82 | int addAttribute(const QString& description,bool rw) | |
|
83 | { | |
|
84 | attributesLUT.append(new bitFieldAttribute(rw,description)); | |
|
85 | return attributesLUT.count()-1; | |
|
86 | } | |
|
87 | int setAttribute(int bitIndex,int attributeIndex) | |
|
88 | { | |
|
89 | attributesIndex[bitIndex]=attributeIndex; | |
|
90 | } | |
|
91 | QString description(int bitIndex) | |
|
92 | { | |
|
93 | return attributesLUT.at(attributesIndex[bitIndex])->description; | |
|
94 | } | |
|
95 | bool readonly(int bitIndex) | |
|
96 | { | |
|
97 | if(bitIndex>=0 && bitIndex<32) | |
|
98 | return !attributesLUT.at(attributesIndex[bitIndex])->rw; | |
|
99 | return false; | |
|
100 | } | |
|
82 | 101 | private: |
|
83 | 102 | int attributesIndex[32]; |
|
84 | 103 | uint p_cursorIndex; |
|
85 | 104 | bool p_cursorBlinkEnable; |
|
86 | 105 | int p_dx; |
|
87 | 106 | QList<bitFieldAttribute*> attributesLUT; |
|
88 | 107 | QColor p_blinkTextColor,p_blinkTextBgColor; |
|
89 | 108 | |
|
90 | 109 | }; |
|
91 | 110 | |
|
92 | 111 | |
|
93 | 112 | class registerWidget : public QObject |
|
94 | 113 | { |
|
95 | 114 | Q_OBJECT |
|
96 | 115 | public: |
|
97 | 116 | explicit registerWidget(const QString& name,qint32 address,QObject *parent = 0); |
|
98 | 117 | ~registerWidget(); |
|
99 | 118 | int contains(const QPointF &point); |
|
100 | 119 | QPoint paint(QPainter* painter,QPoint offset); |
|
101 | 120 | QRect boundingRect(); |
|
102 | 121 | uint cursorIndex(); |
|
103 | 122 | uint cursorIndex(int xPos); |
|
104 | 123 | qint32 address(); |
|
105 | 124 | qint32 value(); |
|
125 | void setBitFieldAttribute(uint startIndex,uint stopIndex,const QString& description,bool rw) | |
|
126 | { | |
|
127 | if(startIndex<=stopIndex && stopIndex<32) | |
|
128 | { | |
|
129 | int index= p_fieldsEl->addAttribute(description,rw ); | |
|
130 | for(uint i=startIndex;i<=stopIndex;i++) | |
|
131 | { | |
|
132 | p_fieldsEl->setAttribute(i,index); | |
|
133 | } | |
|
134 | } | |
|
135 | } | |
|
136 | QString bitFieldDesc(int bitIndex) | |
|
137 | { | |
|
138 | return p_fieldsEl->description(bitIndex); | |
|
139 | } | |
|
140 | ||
|
106 | 141 | signals: |
|
107 | 142 | void cursorUp(int pos); |
|
108 | 143 | void cursorDown(int pos); |
|
109 | 144 | void valueChanged(qint32 value); |
|
110 | 145 | void repaint(); |
|
111 | 146 | |
|
112 | 147 | public slots: |
|
113 | 148 | void setValue(qint32 value); |
|
114 | 149 | void blinkCursor(); |
|
115 | 150 | void moveCursorLeft(int count); |
|
116 | 151 | void moveCursorRight(int count); |
|
117 | 152 | void enter(int index=0); |
|
118 | 153 | void leave(); |
|
119 | 154 | void clear(int index); |
|
120 | 155 | void set(int index); |
|
121 | 156 | private: |
|
122 | 157 | void updateBoundingRect(); |
|
123 | 158 | qint32 p_address; |
|
124 | 159 | qint32 p_value; |
|
125 | 160 | QRect p_boundingRect; |
|
126 | 161 | int p_xMargins; |
|
127 | 162 | int p_yMargins; |
|
128 | 163 | regWidgetElement* p_addressEl,*p_nameEl; |
|
129 | 164 | bitfieldsElement* p_fieldsEl; |
|
130 | 165 | }; |
|
131 | 166 | |
|
132 | 167 | #endif // REGISTERWIDGET_H |
@@ -1,28 +1,30 | |||
|
1 | 1 | #ifndef SOCREGSVIEWER_H |
|
2 | 2 | #define SOCREGSVIEWER_H |
|
3 | 3 | |
|
4 | 4 | #include <QWidget> |
|
5 | 5 | #include <QtWidgets> |
|
6 | 6 | #include "peripheralwidget.h" |
|
7 | 7 | |
|
8 | ||
|
9 | ||
|
8 | 10 | class socRegsViewer : public QScrollArea |
|
9 | 11 | { |
|
10 | 12 | Q_OBJECT |
|
11 | 13 | public: |
|
12 | 14 | explicit socRegsViewer(const QString& name,QWidget *parent = 0); |
|
13 | 15 | peripheralWidget* peripheral(int index); |
|
14 | 16 | |
|
15 | 17 | signals: |
|
16 | 18 | |
|
17 | 19 | public slots: |
|
18 | 20 | void addPeripheral(peripheralWidget* peripheral); |
|
19 | 21 | |
|
20 | 22 | private: |
|
21 | 23 | QWidget* p_scrollAreaWdgt; |
|
22 | 24 | QString p_name; |
|
23 | 25 | QGridLayout* p_layout,*p_scrollAreaWdgtLayout; |
|
24 | 26 | QLabel* p_nameLabel; |
|
25 | 27 | QList<peripheralWidget*> p_peripherals; |
|
26 | 28 | }; |
|
27 | 29 | |
|
28 | 30 | #endif // SOCREGSVIEWER_H |
General Comments 0
You need to be logged in to leave comments.
Login now