1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-10-06 23:03:22 +02:00
GPXSee/src/GUI/stylecombobox.cpp

58 lines
1.3 KiB
C++
Raw Normal View History

2016-12-06 01:48:26 +01:00
#include <QPen>
#include <QPainter>
#include <QResizeEvent>
#include "stylecombobox.h"
2016-12-06 21:01:06 +01:00
2016-12-07 21:38:36 +01:00
#define MIN_LINE_LENGTH 60
2016-12-06 21:01:06 +01:00
#define LINE_WIDTH_RATIO 7
2016-12-06 01:48:26 +01:00
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(*a))
static Qt::PenStyle styles[] = {Qt::SolidLine, Qt::DashLine, Qt::DotLine,
Qt::DashDotLine, Qt::DashDotDotLine};
QIcon StyleComboBox::icon(Qt::PenStyle style)
2016-12-06 01:48:26 +01:00
{
QPixmap pm(iconSize());
pm.fill(Qt::transparent);
2016-12-06 01:48:26 +01:00
QBrush brush(QPalette().brush(QPalette::Active, QPalette::WindowText));
QPen pen(brush, pm.height() / LINE_WIDTH_RATIO, style);
2016-12-06 01:48:26 +01:00
QPainter painter(&pm);
painter.setPen(pen);
painter.drawLine(0, pm.height() / 2, pm.width(), pm.height() / 2);
2016-12-06 01:48:26 +01:00
return QIcon(pm);
}
2016-12-06 01:48:26 +01:00
StyleComboBox::StyleComboBox(QWidget *parent) : QComboBox(parent)
{
QSize is = iconSize();
setIconSize(QSize(MIN_LINE_LENGTH, is.height()));
2016-12-06 01:48:26 +01:00
for (size_t i = 0; i < ARRAY_SIZE(styles); i++)
addItem(icon(styles[i]), QString(), QVariant((int)styles[i]));
2016-12-06 01:48:26 +01:00
}
void StyleComboBox::setValue(Qt::PenStyle value)
{
for (int i = 0; i < count(); i++) {
if ((Qt::PenStyle) itemData(i).toInt() == value) {
setCurrentIndex(i);
return;
}
}
}
void StyleComboBox::changeEvent(QEvent *e)
{
if (e->type() == QEvent::PaletteChange)
for (int i = 0; i < count(); i++)
setItemIcon(i, icon(styles[i]));
QComboBox::changeEvent(e);
}