##// END OF EJS Templates
GUI almost finished, need work on data managing.
jeandet -
r4:bd9421acc1c2 default
parent child
Show More
@@ -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*8));
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 }
@@ -5,9 +5,10
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 QGroupBox
11 class peripheralWidget : public QWidget
11 {
12 {
12 Q_OBJECT
13 Q_OBJECT
13 public:
14 public:
@@ -16,11 +17,21 public:
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
@@ -2,21 +2,17
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, QObject *parent) :
6 QWidget(parent)
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),4,4);
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()
@@ -26,37 +22,90 registerWidget::~registerWidget()
26 delete p_nameEl;
22 delete p_nameEl;
27 }
23 }
28
24
29 void registerWidget::paintEvent(QPaintEvent *event)
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.translate(p_fieldsEl->paint(&painter));
35 painter->translate(p_addressEl->paint(painter));
35 p_nameEl->paint(&painter);
36 painter->translate(p_fieldsEl->paint(painter));
36 setMinimumSize(p_boundingRect);
37 p_nameEl->paint(painter);
37 updateGeometry();
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 p_fieldsEl->blinkCursor();
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()
@@ -77,6 +126,10 bitfieldsElement::bitfieldsElement(const
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)
@@ -84,17 +137,32 QPoint bitfieldsElement::paint(QPainter
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);
@@ -121,7 +189,7 void bitfieldsElement::setValue(const QS
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)
@@ -142,3 +210,37 void bitfieldsElement::moveCursorRight(i
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 }
@@ -74,43 +74,55 public:
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 QWidget
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,QWidget *parent = 0);
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 QSize p_boundingRect;
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 };
General Comments 0
You need to be logged in to leave comments. Login now