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