6 #include <ros/package.h> 9 static int g_icon_size = 128;
10 static int g_icon_alpha = 180;
12 void startOnMainThreadAsync(
const std::function<
void()> &fun) {
14 QObject::connect(&o, &QObject::destroyed, QCoreApplication::instance(),
15 [fun](QObject *o) { fun(); });
18 void startOnObjectThreadAsync(QObject *
object,
19 const std::function<
void()> &fun) {
21 QObject::connect(&o, &QObject::destroyed,
object,
22 [fun](QObject *o) { fun(); });
25 QIcon _createTextIcon(
const QString &str) {
29 pixmap.fill(QColor(255, 255, 255, 0));
31 font.setPixelSize(h * 0.8);
35 p.setPen(QColor(0, 0, 0, g_icon_alpha));
36 p.drawText(QRect(0, 0, w, h), str,
37 QTextOption(Qt::AlignHCenter | Qt::AlignVCenter));
40 pixmap = pixmap.copy(QGraphicsPixmapItem(pixmap)
44 .marginsAdded(QMargins(m, m, m, m)));
50 QMap<QString, QChar> glyph_map;
51 IconLoader(
const std::string &font_path,
const std::string &map_path) {
56 int id = QFontDatabase::addApplicationFont(path.c_str());
58 LOG_ERROR(
"failed to load font " << path);
61 QString family = QFontDatabase::applicationFontFamilies(
id).at(0);
63 LOG_DEBUG(
"font loaded " << family.toStdString() <<
" " <<
id <<
" " 68 QFile file(map_path.c_str());
69 file.open(QFile::ReadOnly);
70 QMap<QString, QChar> map;
71 for (
auto &line : QString(file.readAll()).split(
"\n")) {
72 auto tokens = line.trimmed().split(
" ");
73 if (tokens.size() == 2) {
74 map[tokens[0]] = QChar(tokens[1].toInt(
nullptr, 16));
80 QIcon load(
const QString &str,
double margin) {
84 pixmap.fill(QColor(255, 255, 255, 0));
87 LOG_DEBUG(
"icon glyph " << str.toStdString() <<
" " 88 << glyph_map[str].unicode());
90 font.setPixelSize(h * 0.5);
97 p.setPen(QColor(0, 0, 0, g_icon_alpha));
98 p.drawText(QRect(0, 0, w, h), Qt::AlignHCenter | Qt::AlignVCenter,
103 QGraphicsPixmapItem(pixmap).opaqueArea().boundingRect().toRect();
105 std::round(std::sqrt(rect.width() * rect.height()) * margin);
106 pixmap = pixmap.copy(
107 rect.marginsAdded(QMargins(margins, margins, margins, margins)));
109 auto rect = pixmap.rect();
110 int margins = std::round(std::sqrt(rect.width() * rect.height()) *
112 pixmap = pixmap.copy(
113 rect.marginsRemoved(QMargins(margins, margins, margins, margins)));
116 return QIcon(pixmap);
120 QIcon _createMaterialIcon(
const QString &str,
double padding) {
122 ros::package::getPath(ROS_PACKAGE_NAME) +
123 "/3rdparty/material-design-icons/MaterialIcons-Regular.ttf",
124 ros::package::getPath(ROS_PACKAGE_NAME) +
125 "/3rdparty/material-design-icons/codepoints.txt");
126 return loader.load(str, padding);
129 QIcon _create_FA_R_Icon(
const QString &str,
double padding) {
130 static IconLoader loader(ros::package::getPath(ROS_PACKAGE_NAME) +
131 "/3rdparty/fontawesome/fa-regular-400.ttf",
132 ros::package::getPath(ROS_PACKAGE_NAME) +
133 "/3rdparty/fontawesome/codepoints.txt");
134 return loader.load(str, padding);
137 QIcon _create_FA_S_Icon(
const QString &str,
double padding) {
138 static IconLoader loader(ros::package::getPath(ROS_PACKAGE_NAME) +
139 "/3rdparty/fontawesome/fa-solid-900.ttf",
140 ros::package::getPath(ROS_PACKAGE_NAME) +
141 "/3rdparty/fontawesome/codepoints.txt");
142 return loader.load(str, padding);
145 void FlatButton::init() {
147 setFocusPolicy(Qt::TabFocus);
148 setAutoDefault(
false);
151 FlatButton::~FlatButton() {}
153 void FlatButton::paintEvent(QPaintEvent *event) {
154 QStylePainter p(
this);
155 QStyleOptionButton option;
156 initStyleOption(&option);
157 p.drawControl(QStyle::CE_PushButton, option);
160 QSize FlatButton::minimumSizeHint()
const {
161 return QAbstractButton::minimumSizeHint();
164 QSize FlatButton::sizeHint()
const {
165 int width = QFontMetrics(font()).width(text()) +
166 style()->pixelMetric(QStyle::PM_ButtonMargin) * 2;
167 if (!icon().isNull() || menu() !=
nullptr) {
168 width += style()->pixelMetric(QStyle::PM_ButtonMargin);
169 width += style()->pixelMetric(QStyle::PM_ButtonIconSize);
171 return QSize(width, QPushButton::sizeHint().height());