##// END OF EJS Templates
sync
Jeandet Alexis -
r3:321fbc64206b default
parent child
Show More
@@ -1,48 +1,144
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, QWidget *parent) :
5 registerWidget::registerWidget(const QString &name, qint32 address, QWidget *parent) :
6 QWidget(parent)
6 QWidget(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),4,4);
10 p_addressEl = new regWidgetElement(QString("0x%1").arg(p_address,8,16).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);
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;
15 updateBoundingRect();
18 updateBoundingRect();
19 connect(p_timer,SIGNAL(timeout()),this,SLOT(blinkCursor()));
16 }
20 }
17
21
18 registerWidget::~registerWidget()
22 registerWidget::~registerWidget()
19 {
23 {
20 delete p_addressEl;
24 delete p_addressEl;
21 delete p_fieldsEl;
25 delete p_fieldsEl;
22 delete p_nameEl;
26 delete p_nameEl;
23 }
27 }
24
28
25 void registerWidget::paintEvent(QPaintEvent *event)
29 void registerWidget::paintEvent(QPaintEvent *event)
26 {
30 {
27 Q_UNUSED(event)
31 Q_UNUSED(event)
28 QPainter painter(this);
32 QPainter painter(this);
29 painter.translate(p_addressEl->paint(&painter));
33 painter.translate(p_addressEl->paint(&painter));
30 painter.translate(p_fieldsEl->paint(&painter));
34 painter.translate(p_fieldsEl->paint(&painter));
31 p_nameEl->paint(&painter);
35 p_nameEl->paint(&painter);
32 setMinimumSize(p_boundingRect);
36 setMinimumSize(p_boundingRect);
33 updateGeometry();
37 updateGeometry();
34 }
38 }
35
39
36 void registerWidget::setValue(qint32 value)
40 void registerWidget::setValue(qint32 value)
37 {
41 {
38 this->p_value = value;
42 this->p_value = value;
39 this->p_fieldsEl->setValue(QString("%1").arg(p_value,32,2).replace(" ","0"));
43 this->p_fieldsEl->setValue(QString("%1").arg(p_value,32,2).replace(" ","0"));
40 this->repaint();
44 this->repaint();
41 }
45 }
42
46
47 void registerWidget::blinkCursor()
48 {
49 p_fieldsEl->blinkCursor();
50 }
51
52 void registerWidget::moveCursorLeft(int count)
53 {
54 p_fieldsEl->moveCursorLeft(count);
55 }
56
57 void registerWidget::moveCursorRight(int count)
58 {
59 p_fieldsEl->moveCursorRight(count);
60 }
61
43 void registerWidget::updateBoundingRect()
62 void registerWidget::updateBoundingRect()
44 {
63 {
45 p_boundingRect.setHeight(p_fieldsEl->boundingRect().height()+(p_yMargins*2));
64 p_boundingRect.setHeight(p_fieldsEl->boundingRect().height()+(p_yMargins*2));
46 p_boundingRect.setWidth(p_fieldsEl->boundingRect().width()+p_addressEl->boundingRect().width()+p_nameEl->boundingRect().width()+(p_xMargins*2));
65 p_boundingRect.setWidth(p_fieldsEl->boundingRect().width()+p_addressEl->boundingRect().width()+p_nameEl->boundingRect().width()+(p_xMargins*2));
47 }
66 }
48
67
68
69
70 bitfieldsElement::bitfieldsElement(const QString &value, QFont font, int xMargin, int yMargin)
71 :regWidgetElement(value,font,xMargin,yMargin)
72 {
73 this->attributesLUT.append(new bitFieldAttribute(true,"desc1"));
74 this->attributesLUT.append(new bitFieldAttribute(false,"desc2"));
75 for(int i=0;i<32;i++)
76 {
77 attributesIndex[i] = i&1;
78 }
79 updateBoundingRect();
80 }
81
82 QPoint bitfieldsElement::paint(QPainter *painter)
83 {
84 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);
86 painter->setFont(p_font);
87 int xpos=p_xMargins,dx=QFontMetrics(p_font).width("0")+2;
88 for(int i=0;i<(32/4);i++)
89 {
90 for(int l = 0;l<4;l++)
91 {
92 if(attributesLUT[attributesIndex[(i*4)+l]]->rw==false)
93 painter->fillRect(xpos-1,0,dx,p_boundingRec.height(),Qt::lightGray);
94 else
95 painter->fillRect(xpos-1,0,dx,p_boundingRec.height(),Qt::white);
96 painter->drawText(xpos,QFontMetrics(p_font).ascent()+p_yMargins,p_valueStr.at((i*4)+l));
97 xpos+=dx;
98 }
99 if(i==3)
100 painter->drawLine(xpos+(p_xMargins/2),p_boundingRec.height()-6,xpos+(p_xMargins/2),p_boundingRec.height()+6);
101 else if(i<7)
102 painter->drawLine(xpos+(p_xMargins/2),p_boundingRec.height()-2,xpos+(p_xMargins/2),p_boundingRec.height()+2);
103 xpos+=p_xMargins;
104 }
105 painter->drawRect(0,0,p_boundingRec.width(),p_boundingRec.height());
106 return QPoint(p_boundingRec.width()+4+p_xMargins,0);
107 }
108
109 void bitfieldsElement::updateBoundingRect()
110 {
111 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);
113 p_boundingRec.setWidth(width);
114 }
115
116 void bitfieldsElement::setValue(const QString &value)
117 {
118 p_valueStr = value;
119 updateBoundingRect();
120 }
121
122 void bitfieldsElement::blinkCursor()
123 {
124
125 }
126
127 void bitfieldsElement::moveCursorLeft(int count)
128 {
129 p_cursorIndex+=count;
130 if(31<p_cursorIndex)
131 {
132 p_cursorIndex = 31;
133 }
134
135 }
136
137 void bitfieldsElement::moveCursorRight(int count)
138 {
139 p_cursorIndex -= count;
140 if(31<p_cursorIndex)
141 {
142 p_cursorIndex = 0;
143 }
144 }
@@ -1,151 +1,118
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
6
6 class regWidgetElement
7 class regWidgetElement
7 {
8 {
8 public:
9 public:
9 regWidgetElement(const QString& value,QFont font,int xMargin,int yMargin)
10 regWidgetElement(const QString& value,QFont font,int xMargin,int yMargin)
10 {
11 {
11 p_valueStr = value;
12 p_valueStr = value;
12 p_font = font;
13 p_font = font;
13 p_xMargins = xMargin;
14 p_xMargins = xMargin;
14 p_yMargins = yMargin;
15 p_yMargins = yMargin;
15 updateBoundingRect();
16 updateBoundingRect();
16 }
17 }
17 void setValue(const QString& value)
18 void setValue(const QString& value)
18 {
19 {
19 p_valueStr = value;
20 p_valueStr = value;
20 updateBoundingRect();
21 updateBoundingRect();
21 }
22 }
22 void setFont(QFont font)
23 void setFont(QFont font)
23 {
24 {
24 p_font = font;
25 p_font = font;
25 updateBoundingRect();
26 updateBoundingRect();
26 }
27 }
27 QSize boundingRect(){return p_boundingRec;}
28 QSize boundingRect(){return p_boundingRec;}
28 QString value(){return p_valueStr;}
29 QString value(){return p_valueStr;}
29 const QChar at ( int position ) const{return p_valueStr.at(position);}
30 const QChar at ( int position ) const{return p_valueStr.at(position);}
30 QFont font(){return p_font;}
31 QFont font(){return p_font;}
31 int xMargin(){return p_xMargins;}
32 int xMargin(){return p_xMargins;}
32 int yMargin(){return p_yMargins;}
33 int yMargin(){return p_yMargins;}
33 QFontMetrics fontMetrics(){return QFontMetrics(p_font);}
34 QFontMetrics fontMetrics(){return QFontMetrics(p_font);}
34 QPoint paint(QPainter* painter)
35 QPoint paint(QPainter* painter)
35 {
36 {
36 painter->setFont(p_font);
37 painter->setFont(p_font);
37 painter->drawText(0,QFontMetrics(p_font).ascent()+p_yMargins,p_valueStr);
38 painter->drawText(0,QFontMetrics(p_font).ascent()+p_yMargins,p_valueStr);
38 return QPoint(p_boundingRec.width(),0);
39 return QPoint(p_boundingRec.width(),0);
39 }
40 }
40 void updateBoundingRect()
41 void updateBoundingRect()
41 {
42 {
42 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));
43 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));
44 }
45 }
45
46
46 protected:
47 protected:
47 QString p_valueStr;
48 QString p_valueStr;
48 QFont p_font;
49 QFont p_font;
49 QSize p_boundingRec;
50 QSize p_boundingRec;
50 int p_xMargins;
51 int p_xMargins;
51 int p_yMargins;
52 int p_yMargins;
52 };
53 };
53
54
54 class bitfieldsElement: public regWidgetElement
55 class bitfieldsElement: public regWidgetElement
55 {
56 {
56 class bitFieldAttribute
57 class bitFieldAttribute
57 {
58 {
58 public:
59 public:
59 bitFieldAttribute(bool rw,QString description)
60 bitFieldAttribute(bool rw,QString description)
60 {
61 {
61 this->rw = rw;
62 this->rw = rw;
62 this->description = description;
63 this->description = description;
63 }
64 }
64 bool rw;
65 bool rw;
65 QString description;
66 QString description;
66 };
67 };
67 public:
68 public:
68 bitfieldsElement(const QString& value,QFont font,int xMargin,int yMargin)
69 bitfieldsElement(const QString& value,QFont font,int xMargin,int yMargin);
69 :regWidgetElement(value,font,xMargin,yMargin)
70 QPoint paint(QPainter* painter);
70 {
71 this->attributesLUT.append(new bitFieldAttribute(true,"desc1"));
72 this->attributesLUT.append(new bitFieldAttribute(false,"desc2"));
73 for(int i=0;i<32;i++)
74 {
75 attributesIndex[i] = i&1;
76 }
77 updateBoundingRect();
78 }
79 QPoint paint(QPainter* painter)
80 {
81 painter->fillRect(4,4,p_boundingRec.width(),p_boundingRec.height(),Qt::darkGray);
82 painter->fillRect(0,0,p_boundingRec.width(),p_boundingRec.height(),Qt::white);
83 painter->setFont(p_font);
84 int xpos=p_xMargins,dx=QFontMetrics(p_font).width("0")+2;
85 for(int i=0;i<(32/4);i++)
86 {
87 for(int l = 0;l<4;l++)
88 {
89 if(attributesLUT[attributesIndex[(i*4)+l]]->rw==false)
90 painter->fillRect(xpos-1,0,dx,p_boundingRec.height(),Qt::lightGray);
91 else
92 painter->fillRect(xpos-1,0,dx,p_boundingRec.height(),Qt::white);
93 painter->drawText(xpos,QFontMetrics(p_font).ascent()+p_yMargins,p_valueStr.at((i*4)+l));
94 xpos+=dx;
95 }
96 if(i==3)
97 painter->drawLine(xpos+(p_xMargins/2),p_boundingRec.height()-6,xpos+(p_xMargins/2),p_boundingRec.height()+6);
98 else if(i<7)
99 painter->drawLine(xpos+(p_xMargins/2),p_boundingRec.height()-2,xpos+(p_xMargins/2),p_boundingRec.height()+2);
100 xpos+=p_xMargins;
101 }
102 painter->drawRect(0,0,p_boundingRec.width(),p_boundingRec.height());
103 return QPoint(p_boundingRec.width()+4+p_xMargins,0);
104 }
105
71
106 void updateBoundingRect()
72 void updateBoundingRect();
107 {
73 void setValue(const QString& value);
108 p_boundingRec.setHeight(QFontMetrics(p_font).boundingRect(p_valueStr).height()+(p_yMargins*2));
74 void blinkCursor();
109 int width = (((4*(QFontMetrics(p_font).width("0") + 2)) + p_xMargins) * 8) + (p_xMargins);
75 void moveCursorLeft(int count);
110 p_boundingRec.setWidth(width);
76 void moveCursorRight(int count);
111 }
112 void setValue(const QString& value)
113 {
114 p_valueStr = value;
115 updateBoundingRect();
116 }
117 private:
77 private:
118 int attributesIndex[32];
78 int attributesIndex[32];
79 uint p_cursorIndex;
119 QList<bitFieldAttribute*> attributesLUT;
80 QList<bitFieldAttribute*> attributesLUT;
120 };
81 };
121
82
122
83
123 class registerWidget : public QWidget
84 class registerWidget : public QWidget
124 {
85 {
125 Q_OBJECT
86 Q_OBJECT
126 public:
87 public:
127 explicit registerWidget(const QString& name,qint32 address,QWidget *parent = 0);
88 explicit registerWidget(const QString& name,qint32 address,QWidget *parent = 0);
128 ~registerWidget();
89 ~registerWidget();
129
90
130 signals:
91 signals:
131 void cursorUp(int pos);
92 void cursorUp(int pos);
132 void cursorDown(int pos);
93 void cursorDown(int pos);
94 void valueChanged(qint32 value);
133
95
134 protected:
96 protected:
135 void paintEvent(QPaintEvent* event);
97 void paintEvent(QPaintEvent* event);
136
98
137 public slots:
99 public slots:
138 void setValue(qint32 value);
100 void setValue(qint32 value);
101 void blinkCursor();
102 void moveCursorLeft(int count);
103 void moveCursorRight(int count);
139
104
140 private:
105 private:
106 QTimer* p_timer;
141 void updateBoundingRect();
107 void updateBoundingRect();
142 qint32 p_address;
108 qint32 p_address;
143 qint32 p_value;
109 qint32 p_value;
144 QSize p_boundingRect;
110 QSize p_boundingRect;
145 int p_xMargins;
111 int p_xMargins;
146 int p_yMargins;
112 int p_yMargins;
113 uint p_cursorIndex;
147 regWidgetElement* p_addressEl,*p_nameEl;
114 regWidgetElement* p_addressEl,*p_nameEl;
148 bitfieldsElement* p_fieldsEl;
115 bitfieldsElement* p_fieldsEl;
149 };
116 };
150
117
151 #endif // REGISTERWIDGET_H
118 #endif // REGISTERWIDGET_H
General Comments 0
You need to be logged in to leave comments. Login now