| 1 |
/* |
| 2 |
* libkysdk-qtwidgets's Library |
| 3 |
* |
| 4 |
* Copyright (C) 2024, KylinSoft Co., Ltd. |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 3 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this library. If not, see <https://www.gnu.org/licenses/>. |
| 18 |
* |
| 19 |
* Authors: Zhen Sun <[email protected]> |
| 20 |
* |
| 21 |
*/ |
| 22 |
|
| 23 |
#ifndef ACCESSIBLEFORMAT_H |
| 24 |
#define ACCESSIBLEFORMAT_H |
| 25 |
|
| 26 |
#include <QApplication> |
| 27 |
#include <QFileInfo> |
| 28 |
#include <QRegularExpression> |
| 29 |
#include <QString> |
| 30 |
#include <QWidget> |
| 31 |
#include <type_traits> |
| 32 |
|
| 33 |
/** |
| 34 |
* @brief 设置控件属性 |
| 35 |
* @since 2.5.2.0 |
| 36 |
*/ |
| 37 |
|
| 38 |
namespace kdk |
| 39 |
{ |
| 40 |
|
| 41 |
template<typename T> |
| 42 |
QString combineAccessibleName(T *obj, QString widgetName, const QString &pluginName, const QString &extend = QString()) |
| 43 |
{ |
| 44 |
if (!obj) |
| 45 |
return QString(); |
| 46 |
if (!std::is_base_of<QObject, T>::value) |
| 47 |
return QString(); |
| 48 |
QFileInfo cmdInfo(QApplication::arguments().at(0)); |
| 49 |
QString accessibleName = cmdInfo.fileName(); |
| 50 |
accessibleName += "_"; |
| 51 |
if (!pluginName.isEmpty()) { |
| 52 |
accessibleName += pluginName; |
| 53 |
accessibleName += "_"; |
| 54 |
} |
| 55 |
accessibleName += obj->metaObject()->className(); |
| 56 |
accessibleName += "_"; |
| 57 |
accessibleName += widgetName.remove(QRegularExpression("[&*]")); |
| 58 |
if (!extend.isEmpty()) { |
| 59 |
accessibleName += "_"; |
| 60 |
accessibleName += extend; |
| 61 |
} |
| 62 |
return accessibleName; |
| 63 |
} |
| 64 |
|
| 65 |
template<typename T> |
| 66 |
QString combineAccessibleDescription(T *obj, const QString &widgetName) |
| 67 |
{ |
| 68 |
if (!obj) |
| 69 |
return QString(); |
| 70 |
if (!std::is_base_of<QObject, T>::value) |
| 71 |
return QString(); |
| 72 |
QFileInfo cmdInfo(QApplication::arguments().at(0)); |
| 73 |
return QString("[%1] is [%2] type in process:[%3]") |
| 74 |
.arg(widgetName) |
| 75 |
.arg(obj->metaObject()->className()) |
| 76 |
.arg(cmdInfo.fileName()); |
| 77 |
} |
| 78 |
|
| 79 |
template<typename T> |
| 80 |
class AccessInfoHelper |
| 81 |
{ |
| 82 |
public: |
| 83 |
AccessInfoHelper(T *widget) |
| 84 |
: m_widget(widget) |
| 85 |
{ |
| 86 |
} |
| 87 |
|
| 88 |
void setObjectInfo(const QString &widgetName, const QString &pluginName, const QString &extend = QString()) |
| 89 |
{ |
| 90 |
if (m_widget->objectName().isEmpty()) |
| 91 |
m_widget->setObjectName(combineAccessibleName<T>(m_widget, widgetName, pluginName, extend)); |
| 92 |
} |
| 93 |
|
| 94 |
void setAllAttribute(const QString &widgetName, const QString &pluginName = QString(), const QString &extend = QString(), const QString &description = QString()) |
| 95 |
{ |
| 96 |
if (!m_widget) |
| 97 |
return; |
| 98 |
setObjectInfo(widgetName, pluginName, extend); |
| 99 |
if (std::is_base_of<QWidget, T>::value) { |
| 100 |
m_widget->setAccessibleName(combineAccessibleName<T>(m_widget, widgetName, pluginName, extend)); |
| 101 |
if (!description.isEmpty()) { |
| 102 |
m_widget->setAccessibleDescription(description); |
| 103 |
} else { |
| 104 |
m_widget->setAccessibleDescription(combineAccessibleDescription<T>(m_widget, widgetName)); |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
private: |
| 110 |
T *m_widget; |
| 111 |
}; |
| 112 |
|
| 113 |
template<typename T> |
| 114 |
AccessInfoHelper<T> getHandle(T *widget) |
| 115 |
{ |
| 116 |
return AccessInfoHelper<T>(widget); |
| 117 |
} |
| 118 |
|
| 119 |
#define KDK_ALL_INFO_FORMAT(widget, description) getHandle(widget).setAllAttribute(#widget, "", "", description) |
| 120 |
#define KDK_EXTEND_ALL_INFO_FORMAT(widget, pluginName, extend, description) getHandle(widget).setAllAttribute(#widget, pluginName, extend, description) |
| 121 |
|
| 122 |
#define KDK_OBJ_INFO_FORMAT(obj) getHandle(obj).setObjectInfo(#obj, "") |
| 123 |
#define KDK_EXTEND_OBJ_INFO_FORMAT(obj, pluginName, extend) getHandle(obj).setObjectInfo(#obj, pluginName, extend) |
| 124 |
|
| 125 |
} |
| 126 |
#endif // ACCESSIBLEFORMAT_H |