通用悬浮按钮工具栏这个功能经过了好几个版本的迭代,一开始设计的时候是写在视频控件widget窗体中,当时功能简单就放一排按钮在顶部悬浮widget中就好,随着用户需求的变化,用户需要自定义悬浮条的要求越发强烈,而且部分用户还希望悬浮条的位置能够指定,比如可以在顶部、底部、左侧、右侧位置。为了满足各种需求,特意将通用悬浮按钮工具栏单独成类BannerWidget,将所有悬浮条参数放到结构体BannerPara中,可以设置按钮的间距、边距、背景透明度、背景颜色、文本颜色、按下颜色、悬浮条位置等,每个按钮都对应有图标代码、名称标识、提示信息。这些信息都可以动态设置并立即应用,在最外层的视频控件窗体就提供了设置接口。
//悬浮条位置
enum BannerPosition {
BannerPosition_Top = 0, //顶部
BannerPosition_Bottom = 1, //底部
BannerPosition_Left = 2, //左侧
BannerPosition_Right = 3 //右侧
};
//悬浮条参数
#include
struct BannerPara {
QMargins margin; //边距
int spacing; //间距
int bgAlpha; //背景透明度
QColor bgColor; //背景颜色
QColor textColor; //文本颜色
QColor pressColor; //按下颜色
BannerPosition position; //悬浮条位置
QList<int> icons; //按钮图标代码集合
QList<QString> names; //按钮名称标识集合
QList<QString> tips; //按钮提示信息集合
BannerPara() {
margin = QMargins(5, 0, 3, 0);
spacing = 2;
bgAlpha = 200;
bgColor = "#333333";
textColor = "#FFFFFF";
pressColor = "#5EC7D9";
position = BannerPosition_Top;
//采用iconfont图形字体
icons = QList<int>() << 0xea1b << 0xeb15 << 0xe674 << 0xea36 << 0xe74c;
//为了避免和其他控件重名建议前面加上前缀用来区分
names = QList<QString>() << "banner_btnRecord" << "banner_btnSnap" << "banner_btnSound" << "banner_btnAlarm" << "banner_btnClose";
tips = QList<QString>() << "录制" << "抓图" << "声音" << "警情" << "关闭";
}
};
void BannerWidget::btnClicked()
{
QPushButton *btn = (QPushButton *)sender();
QString objName = btn->objectName();
emit btnClicked(objName);
//根据需要切换图标以及标识
if (objName.endsWith("btnRecord")) {
btn->setText((QChar)0xea1c);
btn->setObjectName(objName.replace("Record", "Stop"));
} else if (objName.endsWith("btnStop")) {
btn->setText((QChar)0xea1b);
btn->setObjectName(objName.replace("Stop", "Record"));
} else if (objName.endsWith("btnSound")) {
btn->setText((QChar)0xe667);
btn->setObjectName(objName.replace("Sound", "Muted"));
} else if (objName.endsWith("btnMuted")) {
btn->setText((QChar)0xe674);
btn->setObjectName(objName.replace("Muted", "Sound"));
}
}
void BannerWidget::receivePlayFinsh()
{
this->changeStatus("Stop", "Record", 0xea1b);
this->changeStatus("Muted", "Sound", 0xe674);
}
void BannerWidget::receiveMuted(bool muted)
{
if (muted) {
this->changeStatus("Sound", "Muted", 0xe667);
} else {
this->changeStatus("Muted", "Sound", 0xe674);
}
}
void BannerWidget::recorderStateChanged(const RecorderState &state, const QString &file)
{
if (state == RecorderState_Recording) {
this->isRecord = true;
this->changeStatus("Record", "Stop", 0xea1c);
} else if (state == RecorderState_Stopped) {
this->isRecord = false;
this->changeStatus("Stop", "Record", 0xea1b);
}
this->showInfo(text);
}
void BannerWidget::setBannerPara(const BannerPara &bannerPara)
{
this->bannerPara = bannerPara;
}
void BannerWidget::initButton()
{
//检查数量是否一致
int iconCount = bannerPara.icons.size();
int nameCount = bannerPara.names.size();
if (iconCount == 0 || iconCount != nameCount) {
return;
}
//清空之前的按钮对象
qDeleteAll(btns);
btns.clear();
//如果之前存在布局则删除布局(居然只能用delete而不是deleteLater)
if (this->layout()) {
delete this->layout();
}
//识别当前用哪个布局
bool vertical = (bannerPara.position == BannerPosition_Left || bannerPara.position == BannerPosition_Right);
//实例化布局
QBoxLayout *layout = 0;
if (vertical) {
layout = new QVBoxLayout;
//插入弹簧并设置布局的边距间距
layout->addStretch();
} else {
layout = new QHBoxLayout;
//插入标签放置各种信息
layout->addWidget(label);
}
layout->setContentsMargins(bannerPara.margin);
layout->setSpacing(bannerPara.spacing);
this->setLayout(layout);
//有多种办法来设置图片,qt内置的图标+自定义的图标+图形字体
//既可以设置图标形式,也可以直接图形字体设置文本
#if 0
QList<QIcon> icons;
icons << QApplication::style()->standardIcon(QStyle::SP_ComputerIcon);
icons << QApplication::style()->standardIcon(QStyle::SP_FileIcon);
icons << QApplication::style()->standardIcon(QStyle::SP_DirIcon);
icons << QApplication::style()->standardIcon(QStyle::SP_DialogOkButton);
icons << QApplication::style()->standardIcon(QStyle::SP_DialogCancelButton);
#endif
//根据位置设置布局以及添加按钮(如果不需要按钮则只需要加一行 iconCount = 0)
for (int i = 0; i < iconCount; ++i) {
QPushButton *btn = new QPushButton;
//绑定按钮单击事件,用来发出信号通知
connect(btn, SIGNAL(clicked(bool)), this, SLOT(btnClicked()));
//设置标识,用来区别按钮
btn->setObjectName(bannerPara.names.at(i));
//设置提示文字信息
btn->setToolTip(bannerPara.tips.at(i));
if (vertical) {
//设置固定高度
btn->setFixedHeight(20);
//设置拉伸策略使得填充
btn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
} else {
//设置固定宽度
btn->setFixedWidth(20);
//设置拉伸策略使得填充
btn->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
}
//设置焦点策略为无焦点,避免单击后焦点跑到按钮上
btn->setFocusPolicy(Qt::NoFocus);
#if 0
//设置图标大小和图标
btn->setIconSize(QSize(16, 16));
btn->setIcon(icons.at(i));
#else
btn->setFont(iconFont);
btn->setText((QChar)bannerPara.icons.at(i));
#endif
//将按钮加到布局中
layout->addWidget(btn);
btns << btn;
}
}
void BannerWidget::initStyle()
{
//应用样式表
QStringList list;
QColor bgColor = bannerPara.bgColor;
QString rgba = QString("rgba(%1,%2,%3,%4)").arg(bgColor.red()).arg(bgColor.green()).arg(bgColor.blue()).arg(bannerPara.bgAlpha);
list << QString("#bannerWidget{background:%1;border:none;}").arg(rgba);
list << QString("QLabel{margin:0px;padding:0px;}");
list << QString("QPushButton,QLabel{color:%1;}").arg(bannerPara.textColor.name());
list << QString("QPushButton:pressed{color:%1;}").arg(bannerPara.pressColor.name());
list << QString("QPushButton{border:none;padding:0px;background:rgba(0,0,0,0);}");
this->setStyleSheet(list.join(""));
}
void BannerWidget::showInfo(const QString &text)
{
this->text = text;
if (isRecord) {
label->setText(text + " 录制中...");
} else {
label->setText(text);
}
}
void BannerWidget::changeStatus(const QString &objNameSrc, const QString &objNameDst, int icon)
{
foreach (QPushButton *btn, btns) {
QString objName = btn->objectName();
if (objName.endsWith(objNameSrc)) {
btn->setObjectName(objName.replace(objNameSrc, objNameDst));
btn->setText((QChar)icon);
break;
}
}
}