##// END OF EJS Templates
WIP
jeandet -
r1:aca341872bcb default
parent child
Show More
@@ -0,0 +1,48
1 #include "registerwidget.h"
2 #include <QPaintEvent>
3 #include <QPainter>
4
5 registerWidget::registerWidget(const QString &name, qint32 address, QWidget *parent) :
6 QWidget(parent)
7 {
8 p_address = address;
9 p_value = 0;
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);
12 p_nameEl = new regWidgetElement(name,QFont("Utopia", 12,QFont::Bold),4,4);
13 p_xMargins = 4;
14 p_yMargins = 4;
15 updateBoundingRect();
16 }
17
18 registerWidget::~registerWidget()
19 {
20 delete p_addressEl;
21 delete p_fieldsEl;
22 delete p_nameEl;
23 }
24
25 void registerWidget::paintEvent(QPaintEvent *event)
26 {
27 Q_UNUSED(event)
28 QPainter painter(this);
29 painter.translate(p_addressEl->paint(&painter));
30 painter.translate(p_fieldsEl->paint(&painter));
31 p_nameEl->paint(&painter);
32 setMinimumSize(p_boundingRect);
33 updateGeometry();
34 }
35
36 void registerWidget::setValue(qint32 value)
37 {
38 this->p_value = value;
39 this->p_fieldsEl->setValue(QString("%1").arg(p_value,32,2).replace(" ","0"));
40 this->repaint();
41 }
42
43 void registerWidget::updateBoundingRect()
44 {
45 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));
47 }
48
@@ -0,0 +1,137
1 #ifndef REGISTERWIDGET_H
2 #define REGISTERWIDGET_H
3 #include <QtWidgets>
4 #include <QWidget>
5
6 class regWidgetElement
7 {
8 public:
9 regWidgetElement(const QString& value,QFont font,int xMargin,int yMargin)
10 {
11 p_valueStr = value;
12 p_font = font;
13 p_xMargins = xMargin;
14 p_yMargins = yMargin;
15 updateBoundingRect();
16 }
17 void setValue(const QString& value)
18 {
19 p_valueStr = value;
20 updateBoundingRect();
21 }
22 void setFont(QFont font)
23 {
24 p_font = font;
25 updateBoundingRect();
26 }
27 QSize boundingRect(){return p_boundingRec;}
28 QString value(){return p_valueStr;}
29 const QChar at ( int position ) const{return p_valueStr.at(position);}
30 QFont font(){return p_font;}
31 int xMargin(){return p_xMargins;}
32 int yMargin(){return p_yMargins;}
33 QFontMetrics fontMetrics(){return QFontMetrics(p_font);}
34 QPoint paint(QPainter* painter)
35 {
36 painter->setFont(p_font);
37 painter->drawText(0,QFontMetrics(p_font).ascent()+p_yMargins,p_valueStr);
38 return QPoint(p_boundingRec.width(),0);
39 }
40 void updateBoundingRect()
41 {
42 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 }
45
46 protected:
47 QString p_valueStr;
48 QFont p_font;
49 QSize p_boundingRec;
50 int p_xMargins;
51 int p_yMargins;
52 };
53
54 class bitfieldsElement: public regWidgetElement
55 {
56 struct bitAttribute
57 {
58 bool rw;
59 int descIndex;
60 };
61 public:
62 bitfieldsElement(const QString& value,QFont font,int xMargin,int yMargin)
63 :regWidgetElement(value,font,xMargin,yMargin)
64 {
65 for(int i=0;i<32;i++)
66 {
67 attributes[i].rw = false;
68 }
69 updateBoundingRect();
70 }
71 QPoint paint(QPainter* painter)
72 {
73 painter->fillRect(4,4,p_boundingRec.width(),p_boundingRec.height(),Qt::darkGray);
74 painter->fillRect(0,0,p_boundingRec.width(),p_boundingRec.height(),Qt::white);
75 painter->setFont(p_font);
76 int xpos=p_xMargins,dx=QFontMetrics(p_font).width("0")+2;
77 for(int i=0;i<(32/4);i++)
78 {
79 for(int l = 0;l<4;l++)
80 {
81 if(attributes[(i*4)+l].rw==false)
82 painter->fillRect(xpos-1,0,dx,p_boundingRec.height(),Qt::lightGray);
83 else
84 painter->fillRect(xpos-1,0,dx,p_boundingRec.height(),Qt::white);
85 painter->drawText(xpos,QFontMetrics(p_font).ascent()+p_yMargins,p_valueStr.at((i*4)+l));
86 xpos+=dx;
87 }
88 if(i==3)
89 painter->drawLine(xpos+(p_xMargins/2),p_boundingRec.height()-6,xpos+(p_xMargins/2),p_boundingRec.height()+6);
90 else if(i<7)
91 painter->drawLine(xpos+(p_xMargins/2),p_boundingRec.height()-2,xpos+(p_xMargins/2),p_boundingRec.height()+2);
92 xpos+=p_xMargins;
93 }
94 painter->drawRect(0,0,p_boundingRec.width(),p_boundingRec.height());
95 return QPoint(p_boundingRec.width()+4+p_xMargins,0);
96 }
97
98 void updateBoundingRect()
99 {
100 p_boundingRec.setHeight(QFontMetrics(p_font).boundingRect(p_valueStr).height()+(p_yMargins*2));
101 int width = (((4*(QFontMetrics(p_font).width("0") + 2)) + p_xMargins) * 8) + (p_xMargins);
102 p_boundingRec.setWidth(width);
103 }
104 private:
105 struct bitAttribute attributes[32];
106 };
107
108
109 class registerWidget : public QWidget
110 {
111 Q_OBJECT
112 public:
113 explicit registerWidget(const QString& name,qint32 address,QWidget *parent = 0);
114 ~registerWidget();
115
116 signals:
117 void cursorUp(int pos);
118 void cursorDown(int pos);
119
120 protected:
121 void paintEvent(QPaintEvent* event);
122
123 public slots:
124 void setValue(qint32 value);
125
126 private:
127 void updateBoundingRect();
128 qint32 p_address;
129 qint32 p_value;
130 QSize p_boundingRect;
131 int p_xMargins;
132 int p_yMargins;
133 regWidgetElement* p_addressEl,*p_nameEl;
134 bitfieldsElement* p_fieldsEl;
135 };
136
137 #endif // REGISTERWIDGET_H
@@ -14,7 +14,9 TEMPLATE = app
14
14
15 SOURCES += main.cpp\
15 SOURCES += main.cpp\
16 mainwindow.cpp \
16 mainwindow.cpp \
17 peripheralwidget.cpp
17 peripheralwidget.cpp \
18 registerwidget.cpp
18
19
19 HEADERS += mainwindow.h \
20 HEADERS += mainwindow.h \
20 peripheralwidget.h
21 peripheralwidget.h \
22 registerwidget.h
@@ -3,6 +3,7
3 MainWindow::MainWindow(QWidget *parent)
3 MainWindow::MainWindow(QWidget *parent)
4 : QMainWindow(parent)
4 : QMainWindow(parent)
5 {
5 {
6 this->setCentralWidget(new peripheralWidget("UART1",this));
6 }
7 }
7
8
8 MainWindow::~MainWindow()
9 MainWindow::~MainWindow()
@@ -2,6 +2,7
2 #define MAINWINDOW_H
2 #define MAINWINDOW_H
3
3
4 #include <QMainWindow>
4 #include <QMainWindow>
5 #include "peripheralwidget.h"
5
6
6 class MainWindow : public QMainWindow
7 class MainWindow : public QMainWindow
7 {
8 {
@@ -1,6 +1,14
1 #include "peripheralwidget.h"
1 #include "peripheralwidget.h"
2
2
3 peripheralWidget::peripheralWidget(QWidget *parent) :
3 peripheralWidget::peripheralWidget(const QString &name, QWidget *parent) :
4 QGroupBox(parent)
4 QGroupBox(name,parent)
5 {
5 {
6 mainLayout = new QVBoxLayout(this);
7 for(int i=0;i<10;i++)
8 {
9 registersWdgts.append(new registerWidget(QString("REG%1").arg(i),i*8));
10 registersWdgts.at(i)->setValue(-1);
11 mainLayout->addWidget(registersWdgts.at(i));
12 }
13 setLayout(mainLayout);
6 }
14 }
@@ -4,17 +4,23
4 #include <QWidget>
4 #include <QWidget>
5 #include <QGroupBox>
5 #include <QGroupBox>
6 #include <QVBoxLayout>
6 #include <QVBoxLayout>
7 #include <QList>
8 #include "registerwidget.h"
7
9
8 class peripheralWidget : public QGroupBox
10 class peripheralWidget : public QGroupBox
9 {
11 {
10 Q_OBJECT
12 Q_OBJECT
11 public:
13 public:
12 explicit peripheralWidget(QWidget *parent = 0);
14 explicit peripheralWidget(const QString& name, QWidget *parent = 0);
13
15
14 signals:
16 signals:
15
17
16 public slots:
18 public slots:
17
19
20 private:
21 QVBoxLayout* mainLayout;
22 QList<registerWidget*> registersWdgts;
23
18 };
24 };
19
25
20 #endif // PERIPHERALWIDGET_H
26 #endif // PERIPHERALWIDGET_H
General Comments 0
You need to be logged in to leave comments. Login now