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