Debian Codegraph @

src/album/accessibledefine.h

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