registerwidget.h
167 lines
| 4.4 KiB
| text/x-c
|
CLexer
/ src / registerwidget.h
Jeandet Alexis
|
r5 | #ifndef REGISTERWIDGET_H | ||
#define REGISTERWIDGET_H | ||||
#include <QtWidgets> | ||||
#include <QWidget> | ||||
#include <QTimer> | ||||
class regWidgetElement | ||||
{ | ||||
public: | ||||
regWidgetElement(const QString& value,QFont font,int xMargin,int yMargin) | ||||
{ | ||||
p_valueStr = value; | ||||
p_font = font; | ||||
p_xMargins = xMargin; | ||||
p_yMargins = yMargin; | ||||
updateBoundingRect(); | ||||
} | ||||
void setValue(const QString& value) | ||||
{ | ||||
p_valueStr = value; | ||||
updateBoundingRect(); | ||||
} | ||||
void setFont(QFont font) | ||||
{ | ||||
p_font = font; | ||||
updateBoundingRect(); | ||||
} | ||||
QSize boundingRect(){return p_boundingRec;} | ||||
QString value(){return p_valueStr;} | ||||
const QChar at ( int position ) const{return p_valueStr.at(position);} | ||||
QFont font(){return p_font;} | ||||
int xMargin(){return p_xMargins;} | ||||
int yMargin(){return p_yMargins;} | ||||
QFontMetrics fontMetrics(){return QFontMetrics(p_font);} | ||||
QPoint paint(QPainter* painter) | ||||
{ | ||||
painter->setFont(p_font); | ||||
painter->drawText(0,QFontMetrics(p_font).ascent()+p_yMargins,p_valueStr); | ||||
return QPoint(p_boundingRec.width(),0); | ||||
} | ||||
void updateBoundingRect() | ||||
{ | ||||
p_boundingRec.setHeight(QFontMetrics(p_font).boundingRect(p_valueStr).height()+(p_yMargins*2)); | ||||
p_boundingRec.setWidth(QFontMetrics(p_font).boundingRect(p_valueStr).width()+(p_xMargins*2)); | ||||
} | ||||
protected: | ||||
QString p_valueStr; | ||||
QFont p_font; | ||||
QSize p_boundingRec; | ||||
int p_xMargins; | ||||
int p_yMargins; | ||||
}; | ||||
class bitfieldsElement: public regWidgetElement | ||||
{ | ||||
class bitFieldAttribute | ||||
{ | ||||
public: | ||||
bitFieldAttribute(bool rw,QString description) | ||||
{ | ||||
this->rw = rw; | ||||
this->description = description; | ||||
} | ||||
bool rw; | ||||
QString description; | ||||
}; | ||||
public: | ||||
bitfieldsElement(const QString& value,QFont font,int xMargin,int yMargin); | ||||
QPoint paint(QPainter* painter); | ||||
void updateBoundingRect(); | ||||
void setValue(const QString& value); | ||||
void blinkCursor(); | ||||
void moveCursorLeft(int count); | ||||
void moveCursorRight(int count); | ||||
void enter(int index); | ||||
void leave(); | ||||
uint cursorIndex(); | ||||
uint cursorIndex(int xPos); | ||||
void setFont(QFont font); | ||||
Jeandet Alexis
|
r7 | int addAttribute(const QString& description,bool rw) | ||
{ | ||||
attributesLUT.append(new bitFieldAttribute(rw,description)); | ||||
return attributesLUT.count()-1; | ||||
} | ||||
int setAttribute(int bitIndex,int attributeIndex) | ||||
{ | ||||
attributesIndex[bitIndex]=attributeIndex; | ||||
} | ||||
QString description(int bitIndex) | ||||
{ | ||||
return attributesLUT.at(attributesIndex[bitIndex])->description; | ||||
} | ||||
bool readonly(int bitIndex) | ||||
{ | ||||
if(bitIndex>=0 && bitIndex<32) | ||||
return !attributesLUT.at(attributesIndex[bitIndex])->rw; | ||||
return false; | ||||
} | ||||
Jeandet Alexis
|
r5 | private: | ||
int attributesIndex[32]; | ||||
uint p_cursorIndex; | ||||
bool p_cursorBlinkEnable; | ||||
int p_dx; | ||||
QList<bitFieldAttribute*> attributesLUT; | ||||
QColor p_blinkTextColor,p_blinkTextBgColor; | ||||
}; | ||||
class registerWidget : public QObject | ||||
{ | ||||
Q_OBJECT | ||||
public: | ||||
explicit registerWidget(const QString& name,qint32 address,QObject *parent = 0); | ||||
~registerWidget(); | ||||
int contains(const QPointF &point); | ||||
QPoint paint(QPainter* painter,QPoint offset); | ||||
QRect boundingRect(); | ||||
uint cursorIndex(); | ||||
uint cursorIndex(int xPos); | ||||
qint32 address(); | ||||
qint32 value(); | ||||
Jeandet Alexis
|
r7 | void setBitFieldAttribute(uint startIndex,uint stopIndex,const QString& description,bool rw) | ||
{ | ||||
if(startIndex<=stopIndex && stopIndex<32) | ||||
{ | ||||
int index= p_fieldsEl->addAttribute(description,rw ); | ||||
for(uint i=startIndex;i<=stopIndex;i++) | ||||
{ | ||||
p_fieldsEl->setAttribute(i,index); | ||||
} | ||||
} | ||||
} | ||||
QString bitFieldDesc(int bitIndex) | ||||
{ | ||||
return p_fieldsEl->description(bitIndex); | ||||
} | ||||
Jeandet Alexis
|
r5 | signals: | ||
void cursorUp(int pos); | ||||
void cursorDown(int pos); | ||||
void valueChanged(qint32 value); | ||||
void repaint(); | ||||
public slots: | ||||
void setValue(qint32 value); | ||||
void blinkCursor(); | ||||
void moveCursorLeft(int count); | ||||
void moveCursorRight(int count); | ||||
void enter(int index=0); | ||||
void leave(); | ||||
void clear(int index); | ||||
void set(int index); | ||||
private: | ||||
void updateBoundingRect(); | ||||
qint32 p_address; | ||||
qint32 p_value; | ||||
QRect p_boundingRect; | ||||
int p_xMargins; | ||||
int p_yMargins; | ||||
regWidgetElement* p_addressEl,*p_nameEl; | ||||
bitfieldsElement* p_fieldsEl; | ||||
}; | ||||
#endif // REGISTERWIDGET_H | ||||