BatteryMonitor/custom/delegate.h

176 lines
5.9 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef DELEGATE_H
#define DELEGATE_H
/**
* @projectName Delegate-自定义委托集合
* @author m晴朗
* @email mengdadae@qq.com
* 1.支持复选框、进度条、数值、组合框、密码框、日期控件、图标
* 2.可以直接将代码加入到自己的项目中,零耦合
* 3.扩展性强,整体框架已经搭好了,可以根据自己的需求,自行加入其他控件
**/
#include <QStyledItemDelegate>
#include <QAbstractItemModel>
class QComboBox;
class QProgressBar;
class QTableView;
class QPushButton;
class QLineEdit;
class QCheckBox;
class Delegate : public QStyledItemDelegate
{
Q_OBJECT
public:
Delegate(QObject *parent = nullptr);
protected:
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
void setEditorData(QWidget *editor, const QModelIndex &index) const override;
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) override;
public:
/**
* 设置需要委托的类型
* @param delegateType 设置的委托类型
* @return
**/
void setDelegateType(const QString &delegateType);
/**
* 设置要委托的列号主要是paint绘制的委托需要这个参数
* @param delegateColumnID 需要委托的列号
* @return
**/
void setDelegateColumn(const int delegateColumnID);
/*QSpinBox设置相关参数函数*/
void setSboxMaxValue(const int max);
void setSboxMinValue(const int min);
void setSboxPrefixStr(const QString &prefix);
void setSboxSuffixStr(const QString &suffix);
void setSboxSingleStep(const int SingleStep);
void setSboxInitValue(const int initValue);
void setSboxStepType(QAbstractSpinBox::StepType st);
/*QComboBox相关参数设置*/
void setCboxItems(const QStringList &items);
void setCboxEditable(bool editable);
/*QProgressBar设置相关参数函数*/
void setPgBarRange(const int min,const int max);
void setPgBarTextVisible(bool isTextVisible);
void setPgBarAlignment(Qt::Alignment pgbarAlignment);
void setInvertedAppearance(bool invertedAppearance);
/*QCheckBox*/
void setCheckBoxText(const QString &checkBoxChecked, const QString &checkBoxUnchecked);
/*QPixmap设置相关参数函数*/
void setImgOk(const QPixmap &imgOk);
void setImgNo(const QPixmap &imgNo);
void setImgSize(int width,int heigth);
void setJudgmentColumn(int column);
void setJudgmentValue(const QString &value);
void setJudgmentCondition(const QString &condition);
/*QLineEdit相关参数函数*/
void setDelegatePwd(bool showDelegatePwd);
/*不使用委托实现的控件*/
void setComboBox(QAbstractItemView *view,QAbstractItemModel *model);
private:
/**
* 初始化各个委托的相关参数
* @param
* @return
**/
void init();
void checkData(const QString &data,bool *selse)const;
/**
* 重新绘制委托控件,在paint函数中调用
* @param 和paint函数参数保持一致
* @return
**/
void drawProgressBar(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
void drawCheckBox(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
void drawPushButton(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
void drawPixmap(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
void drawLineEdit(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
signals:
void valueChanged(QString value);
/**
* 按下按钮触发
* @param
* @return
**/
void buttonClicked(int btnIndex, const QModelIndex &index);
private slots:
/**
* 微调框等值改变的时候触发
* @param
* @return
**/
void on_valueChanged(int value);
void on_valueChanged(double value);
void on_valueChanged(QString value);
private:
QString delegateType;/*委托类型*/
int delegateColumnID;/*需要委托的列*/
QTableView *view;/*在哪个表格*/
QPoint mousePoint;/*鼠标按下处坐标*/
bool mousePressed;/*鼠标是否按下*/
/*QSpinBox相关参数*/
int sboxMaxValue;/*微调框的最大值*/
int sboxMinValue;/*微调框的最小值*/
QString sboxPrefixStr;/*微调框前缀*/
QString sboxSuffixStr;/*微调框后缀*/
int sboxSingleStep;/*微调框步长*/
int sboxInitValue;/*微调框初始值*/
QAbstractSpinBox::StepType sboxStepType;/*微调框步长类型*/
QStringList cboxItems;/*下组合框项目内容*/
bool cboxEditable;/*组合框是否可以编辑*/
/*QProgressBar相关参数*/
int pgbarMax;
int pgbarMin;
Qt::Alignment pgbarAlignment;
bool isTextVisible;
bool invertedAppearance;
/*QCheckBox参数*/
QString checkedText; //存储复选框当前文字
QString checkBoxChecked; //复选框选中对应的文字
QString checkBoxUnchecked; //复选框选中对应的文字
/*QPixmap相关参数*/
QPixmap imgOk;/*表示正确的图标*/
QPixmap imgNo;/*表示错误的图标*/
int imgWidth;/*图片宽度*/
int imgHeigth;/*图片高度*/
int judgmentColumn;/*需要判断的列*/
QString judgmentValue;/*阀值*/
QString judgmentCondition;/*判断条件*/
/*QLineEdit相关参数函数*/
bool delegatePwd; //是否密码显示
/*需要paint重新绘制的委托*/
QProgressBar *pgBar;
QPushButton *button;
QCheckBox *checkBox; //复选框
};
#endif // DELEGATE_H