| 1 |
/* |
| 2 |
* Copyright (C) 2020 ~ 2021 Uniontech Software Technology Co., Ltd. |
| 3 |
* |
| 4 |
* Author: ZhangYong <[email protected]> |
| 5 |
* |
| 6 |
* Maintainer: ZhangYong <[email protected]> |
| 7 |
* |
| 8 |
* This program is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* This program is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU General Public License |
| 19 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
#ifndef ACCESSIBLE_FUNCTIONS_H |
| 22 |
#define ACCESSIBLE_FUNCTIONS_H |
| 23 |
|
| 24 |
// 为了方便使用,把相关定义独立出来,如有需要,直接包含这个头文件,然后使用SET_*的宏去设置,USE_*宏开启即可 |
| 25 |
/* 宏参数说明 |
| 26 |
* classname:类名,例如DLineEdit |
| 27 |
* accessiblename:accessible唯一标识,需保证唯一性[getAccessibleName函数处理],优先使用QObject::setAccessibleName值 |
| 28 |
* accessibletype:即QAccessible::Role,表示标识控件的类型 |
| 29 |
* classobj:QObject指针 |
| 30 |
* accessdescription:accessible描述内容,考虑到暂时用不到,目前都默认为空,有需要可自行设计接口 |
| 31 |
* |
| 32 |
* 部分创建宏说明 |
| 33 |
* FUNC_CREATE:创建构造函数 |
| 34 |
* FUNC_PRESS:创建Press接口 |
| 35 |
* FUNC_SHOWMENU:创建右键菜单接口 |
| 36 |
* FUNC_PRESS_SHOWMENU:上两者的综合 |
| 37 |
* FUNC_RECT:实现rect接口 |
| 38 |
* FUNC_TEXT:实现text接口 |
| 39 |
* USE_ACCESSIBLE:对传入的类型设置其accessible功能 |
| 40 |
* USE_ACCESSIBLE_BY_OBJECTNAME:同上,[指定objectname]---适用同一个类,但objectname不同的情况 |
| 41 |
* |
| 42 |
* 设置为指定类型的Accessible控件宏 |
| 43 |
* SET_BUTTON_ACCESSIBLE_PRESS_SHOWMENU:button类型,添加press和showmenu功能 |
| 44 |
* SET_BUTTON_ACCESSIBLE_SHOWMENU:button类型,添加showmenu功能 |
| 45 |
* SET_BUTTON_ACCESSIBLE:button类型,添加press功能 |
| 46 |
* SET_LABEL_ACCESSIBLE:label类型,用于标签控件 |
| 47 |
* SET_FORM_ACCESSIBLE:form类型,用于widget控件 |
| 48 |
* SET_SLIDER_ACCESSIBLE:slider类型,用于滑块控件 |
| 49 |
* SET_SEPARATOR_ACCESSIBLE:separator类型,用于分隔符控件 |
| 50 |
*/ |
| 51 |
#include <QAccessible> |
| 52 |
#include <QAccessibleWidget> |
| 53 |
#include <QEvent> |
| 54 |
#include <QMap> |
| 55 |
#include <QString> |
| 56 |
#include <QWidget> |
| 57 |
#include <QObject> |
| 58 |
#include <QMetaEnum> |
| 59 |
#include <QMouseEvent> |
| 60 |
#include <QApplication> |
| 61 |
|
| 62 |
#define SEPARATOR "_" |
| 63 |
|
| 64 |
inline QString getObjPrefix(QAccessible::Role r) |
| 65 |
{ |
| 66 |
// 按照类型添加固定前缀 |
| 67 |
QMetaEnum metaEnum = QMetaEnum::fromType<QAccessible::Role>(); |
| 68 |
QByteArray prefix = metaEnum.valueToKeys(r); |
| 69 |
switch (r) { |
| 70 |
case QAccessible::Button: prefix = "Btn"; break; |
| 71 |
case QAccessible::StaticText: prefix = "Label"; break; |
| 72 |
default: break; |
| 73 |
} |
| 74 |
return QString::fromLatin1(prefix); |
| 75 |
} |
| 76 |
|
| 77 |
inline QString getIntelAccessibleName(QWidget *w, QAccessible::Role r, QString fallback) |
| 78 |
{ |
| 79 |
// 避免重复生成 |
| 80 |
static QMap< QObject *, QString > objnameMap; |
| 81 |
if (!objnameMap[w].isEmpty()) |
| 82 |
return objnameMap[w]; |
| 83 |
|
| 84 |
static QMap< QAccessible::Role, QList< QString > > accessibleMap; |
| 85 |
QString oldAccessName = w->accessibleName(); |
| 86 |
oldAccessName.replace(SEPARATOR, ""); |
| 87 |
|
| 88 |
// 再加上标识 |
| 89 |
QString accessibleName = "";//getObjPrefix(r) + SEPARATOR; |
| 90 |
accessibleName += oldAccessName.isEmpty() ? fallback : oldAccessName; |
| 91 |
// 检查名称是否唯一 |
| 92 |
if (accessibleMap[r].contains(accessibleName)) { |
| 93 |
if (objnameMap.key(accessibleName)) { |
| 94 |
objnameMap.remove(objnameMap.key(accessibleName)); |
| 95 |
objnameMap.insert(w, accessibleName); |
| 96 |
return accessibleName; |
| 97 |
} |
| 98 |
// 获取编号,然后+1 |
| 99 |
int pos = accessibleName.indexOf(SEPARATOR); |
| 100 |
int id = accessibleName.mid(pos + 1).toInt(); |
| 101 |
|
| 102 |
QString newAccessibleName; |
| 103 |
do { |
| 104 |
// 一直找到一个不重复的名字 |
| 105 |
newAccessibleName = accessibleName + SEPARATOR + QString::number(++id); |
| 106 |
} while (accessibleMap[r].contains(newAccessibleName)); |
| 107 |
|
| 108 |
accessibleMap[r].append(newAccessibleName); |
| 109 |
objnameMap.insert(w, newAccessibleName); |
| 110 |
|
| 111 |
return newAccessibleName; |
| 112 |
} else { |
| 113 |
accessibleMap[r].append(accessibleName); |
| 114 |
objnameMap.insert(w, accessibleName); |
| 115 |
|
| 116 |
return accessibleName; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
inline QString getAccessibleName(QWidget *w, QAccessible::Role r, QString fallback) |
| 121 |
{ |
| 122 |
QString accessibleName = getIntelAccessibleName(w, r, fallback); |
| 123 |
if (accessibleName.isEmpty()) { |
| 124 |
return getObjPrefix(r) + SEPARATOR; |
| 125 |
} |
| 126 |
|
| 127 |
return accessibleName; |
| 128 |
} |
| 129 |
|
| 130 |
#define FUNC_CREATE(classname,accessibletype,accessdescription) Accessible##classname(classname *w) \ |
| 131 |
: QAccessibleWidget(w,accessibletype,#classname)\ |
| 132 |
, m_w(w)\ |
| 133 |
, m_description(accessdescription)\ |
| 134 |
{}\ |
| 135 |
private:\ |
| 136 |
classname *m_w;\ |
| 137 |
QString m_description;\ |
| 138 |
|
| 139 |
#define FUNC_PRESS(classobj) QStringList actionNames() const override{\ |
| 140 |
if(!classobj->isEnabled())\ |
| 141 |
return QStringList();\ |
| 142 |
return QStringList() << pressAction();}\ |
| 143 |
void doAction(const QString &actionName) override{\ |
| 144 |
if(actionName == pressAction())\ |
| 145 |
{\ |
| 146 |
QPointF localPos = classobj->geometry().center();\ |
| 147 |
QMouseEvent event(QEvent::MouseButtonPress,localPos,Qt::LeftButton,Qt::LeftButton,Qt::NoModifier);\ |
| 148 |
QMouseEvent event2(QEvent::MouseButtonRelease,localPos,Qt::LeftButton,Qt::LeftButton,Qt::NoModifier);\ |
| 149 |
qApp->sendEvent(classobj,&event);\ |
| 150 |
qApp->sendEvent(classobj,&event2);\ |
| 151 |
}\ |
| 152 |
}\ |
| 153 |
|
| 154 |
#define FUNC_SHOWMENU(classobj) QStringList actionNames() const override{\ |
| 155 |
if(!classobj->isEnabled())\ |
| 156 |
return QStringList();\ |
| 157 |
return QStringList() << showMenuAction();}\ |
| 158 |
void doAction(const QString &actionName) override{\ |
| 159 |
if(actionName == showMenuAction())\ |
| 160 |
{\ |
| 161 |
QPointF localPos = classobj->geometry().center();\ |
| 162 |
QMouseEvent event(QEvent::MouseButtonPress,localPos,Qt::RightButton,Qt::RightButton,Qt::NoModifier);\ |
| 163 |
QMouseEvent event2(QEvent::MouseButtonRelease,localPos,Qt::RightButton,Qt::RightButton,Qt::NoModifier);\ |
| 164 |
qApp->sendEvent(classobj,&event);\ |
| 165 |
qApp->sendEvent(classobj,&event2);\ |
| 166 |
}\ |
| 167 |
}\ |
| 168 |
|
| 169 |
#define FUNC_PRESS_SHOWMENU(classobj) QStringList actionNames() const override{\ |
| 170 |
if(!classobj->isEnabled())\ |
| 171 |
return QStringList();\ |
| 172 |
return QStringList() << pressAction() << showMenuAction();}\ |
| 173 |
void doAction(const QString &actionName) override{\ |
| 174 |
if(actionName == pressAction())\ |
| 175 |
{\ |
| 176 |
QPointF localPos = classobj->geometry().center();\ |
| 177 |
QMouseEvent event(QEvent::MouseButtonPress,localPos,Qt::LeftButton,Qt::LeftButton,Qt::NoModifier);\ |
| 178 |
QMouseEvent event2(QEvent::MouseButtonRelease,localPos,Qt::LeftButton,Qt::LeftButton,Qt::NoModifier);\ |
| 179 |
qApp->sendEvent(classobj,&event);\ |
| 180 |
qApp->sendEvent(classobj,&event2);\ |
| 181 |
}\ |
| 182 |
else if(actionName == showMenuAction())\ |
| 183 |
{\ |
| 184 |
QPointF localPos = classobj->geometry().center();\ |
| 185 |
QMouseEvent event(QEvent::MouseButtonPress,localPos,Qt::RightButton,Qt::RightButton,Qt::NoModifier);\ |
| 186 |
QMouseEvent event2(QEvent::MouseButtonRelease,localPos,Qt::RightButton,Qt::RightButton,Qt::NoModifier);\ |
| 187 |
qApp->sendEvent(classobj,&event);\ |
| 188 |
qApp->sendEvent(classobj,&event2);\ |
| 189 |
}\ |
| 190 |
}\ |
| 191 |
|
| 192 |
#define FUNC_RECT(classobj) QRect rect() const override{\ |
| 193 |
if (!classobj->isVisible())\ |
| 194 |
return QRect();\ |
| 195 |
return classobj->geometry();\ |
| 196 |
}\ |
| 197 |
|
| 198 |
#define FUNC_TEXT(classname,accessiblename) QString Accessible##classname::text(QAccessible::Text t) const{\ |
| 199 |
switch (t) {\ |
| 200 |
case QAccessible::Name:\ |
| 201 |
return getAccessibleName(m_w, this->role(), accessiblename);\ |
| 202 |
case QAccessible::Description:\ |
| 203 |
return m_description;\ |
| 204 |
default:\ |
| 205 |
return QString();\ |
| 206 |
}\ |
| 207 |
}\ |
| 208 |
|
| 209 |
#define USE_ACCESSIBLE(classnamestring,classname) if (classnamestring == QLatin1String(#classname) && object && object->isWidgetType())\ |
| 210 |
{\ |
| 211 |
interface = new Accessible##classname(static_cast<classname *>(object));\ |
| 212 |
}\ |
| 213 |
|
| 214 |
// [指定objectname]---适用同一个类,但objectname不同的情况 |
| 215 |
#define USE_ACCESSIBLE_BY_OBJECTNAME(classnamestring,classname,objectname) if (classnamestring == QLatin1String(#classname) && object && (object->objectName() == objectname) && object->isWidgetType())\ |
| 216 |
{\ |
| 217 |
interface = new Accessible##classname(static_cast<classname *>(object));\ |
| 218 |
}\ |
| 219 |
|
| 220 |
#define SET_BUTTON_ACCESSIBLE_PRESS_DESCRIPTION(classname,accessiblename,accessdescription) class Accessible##classname : public QAccessibleWidget\ |
| 221 |
{\ |
| 222 |
public:\ |
| 223 |
FUNC_CREATE(classname,QAccessible::Button,accessdescription)\ |
| 224 |
QString text(QAccessible::Text t) const override;\ |
| 225 |
FUNC_PRESS(m_w)\ |
| 226 |
};\ |
| 227 |
|
| 228 |
#define SET_BUTTON_ACCESSIBLE_SHOWMENU_DESCRIPTION(classname,accessiblename,accessdescription) class Accessible##classname : public QAccessibleWidget\ |
| 229 |
{\ |
| 230 |
public:\ |
| 231 |
FUNC_CREATE(classname,QAccessible::Button,accessdescription)\ |
| 232 |
QString text(QAccessible::Text t) const override;\ |
| 233 |
FUNC_SHOWMENU(m_w)\ |
| 234 |
};\ |
| 235 |
|
| 236 |
#define SET_BUTTON_ACCESSIBLE_PRESS_SHOEMENU_DESCRIPTION(classname,accessiblename,accessdescription) class Accessible##classname : public QAccessibleWidget\ |
| 237 |
{\ |
| 238 |
public:\ |
| 239 |
FUNC_CREATE(classname,QAccessible::Button,accessdescription)\ |
| 240 |
QString text(QAccessible::Text t) const override;\ |
| 241 |
FUNC_PRESS_SHOWMENU(m_w)\ |
| 242 |
};\ |
| 243 |
|
| 244 |
#define SET_LABEL_ACCESSIBLE_WITH_DESCRIPTION(classname,aaccessibletype,accessiblename,accessdescription) class Accessible##classname : public QAccessibleWidget\ |
| 245 |
{\ |
| 246 |
public:\ |
| 247 |
FUNC_CREATE(classname,aaccessibletype,accessdescription)\ |
| 248 |
QString text(QAccessible::Text t) const override;\ |
| 249 |
FUNC_RECT(m_w)\ |
| 250 |
};\ |
| 251 |
|
| 252 |
// /*******************************************简化使用*******************************************/ |
| 253 |
#define SET_BUTTON_ACCESSIBLE_PRESS_SHOWMENU(classname,accessiblename) SET_BUTTON_ACCESSIBLE_PRESS_SHOEMENU_DESCRIPTION(classname,accessiblename,"")\ |
| 254 |
FUNC_TEXT(classname,accessiblename) |
| 255 |
|
| 256 |
#define SET_BUTTON_ACCESSIBLE_SHOWMENU(classname,accessiblename) SET_BUTTON_ACCESSIBLE_SHOWMENU_DESCRIPTION(classname,accessiblename,"")\ |
| 257 |
FUNC_TEXT(classname,accessiblename) |
| 258 |
|
| 259 |
#define SET_BUTTON_ACCESSIBLE(classname,accessiblename) SET_BUTTON_ACCESSIBLE_PRESS_DESCRIPTION(classname,accessiblename,"")\ |
| 260 |
FUNC_TEXT(classname,accessiblename) |
| 261 |
|
| 262 |
#define SET_LABEL_ACCESSIBLE(classname,accessiblename) SET_LABEL_ACCESSIBLE_WITH_DESCRIPTION(classname,QAccessible::StaticText,accessiblename,"")\ |
| 263 |
FUNC_TEXT(classname,accessiblename) |
| 264 |
|
| 265 |
#define SET_FORM_ACCESSIBLE(classname,accessiblename) SET_LABEL_ACCESSIBLE_WITH_DESCRIPTION(classname,QAccessible::Form,accessiblename,"");\ |
| 266 |
FUNC_TEXT(classname,accessiblename) |
| 267 |
|
| 268 |
#define SET_SLIDER_ACCESSIBLE(classname,accessiblename) SET_LABEL_ACCESSIBLE_WITH_DESCRIPTION(classname,QAccessible::Slider,accessiblename,"")\ |
| 269 |
FUNC_TEXT(classname,accessiblename) |
| 270 |
|
| 271 |
#define SET_SEPARATOR_ACCESSIBLE(classname,accessiblename) SET_LABEL_ACCESSIBLE_WITH_DESCRIPTION(classname,QAccessible::Separator,accessiblename,"")\ |
| 272 |
FUNC_TEXT(classname,accessiblename) |
| 273 |
/************************************************************************************************/ |
| 274 |
|
| 275 |
#endif // ACCESSIBLE_FUNCTIONS_H |