2021-10-06 21:12:31 +01:00
|
|
|
#include "BodyWidget.hpp"
|
|
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
namespace Bloom::Widgets::InsightTargetWidgets::Dip
|
|
|
|
|
{
|
|
|
|
|
BodyWidget::BodyWidget(QWidget* parent, std::size_t pinCount): QWidget(parent) {
|
|
|
|
|
this->setObjectName("target-body");
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
/*
|
|
|
|
|
* The DIP package widget looks awkward when the body height is fixed. For this reason, the body height and
|
|
|
|
|
* indicator sizes are proportional to the pin count.
|
|
|
|
|
*
|
|
|
|
|
* The proportionality constants used below were chosen because they look the nicest. No other reason.
|
|
|
|
|
*/
|
|
|
|
|
this->setFixedHeight(
|
|
|
|
|
std::min(
|
|
|
|
|
BodyWidget::MAXIMUM_BODY_HEIGHT,
|
|
|
|
|
std::max(
|
|
|
|
|
BodyWidget::MINIMUM_BODY_HEIGHT,
|
|
|
|
|
static_cast<int>(std::ceil(3.6 * static_cast<double>(pinCount)))
|
|
|
|
|
)
|
2021-12-01 02:33:41 +00:00
|
|
|
)
|
2022-02-05 15:32:08 +00:00
|
|
|
);
|
2021-12-01 02:33:41 +00:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
this->firstPinIndicatorDiameter = std::min(
|
|
|
|
|
BodyWidget::MAXIMUM_FIRST_PIN_INDICATOR_HEIGHT,
|
|
|
|
|
std::max(BodyWidget::MINIMUM_FIRST_PIN_INDICATOR_HEIGHT, static_cast<int>(std::floor(pinCount / 2)))
|
|
|
|
|
);
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
this->orientationIndicatorDiameter = this->firstPinIndicatorDiameter % 2 == 0 ?
|
|
|
|
|
this->firstPinIndicatorDiameter + 3
|
|
|
|
|
: this->firstPinIndicatorDiameter + 2;
|
|
|
|
|
}
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
void BodyWidget::paintEvent(QPaintEvent* event) {
|
|
|
|
|
auto painter = QPainter(this);
|
|
|
|
|
this->drawWidget(painter);
|
|
|
|
|
}
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
void BodyWidget::drawWidget(QPainter& painter) {
|
|
|
|
|
painter.setRenderHints(
|
|
|
|
|
QPainter::RenderHint::Antialiasing | QPainter::RenderHint::SmoothPixmapTransform,
|
|
|
|
|
true
|
|
|
|
|
);
|
|
|
|
|
const auto bodyHeight = this->height();
|
|
|
|
|
const auto bodyRect = QRectF(
|
|
|
|
|
0,
|
|
|
|
|
0,
|
|
|
|
|
this->width(),
|
|
|
|
|
bodyHeight
|
|
|
|
|
);
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
// The first pin indicator is just a circle positioned close to the first pin
|
|
|
|
|
const auto firstPinIndicatorRect = QRectF(
|
|
|
|
|
6,
|
|
|
|
|
(bodyHeight - this->firstPinIndicatorDiameter - 6),
|
|
|
|
|
this->firstPinIndicatorDiameter,
|
|
|
|
|
this->firstPinIndicatorDiameter
|
|
|
|
|
);
|
2021-12-01 02:33:41 +00:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
/*
|
|
|
|
|
* The orientation indicator is just a half-circle cut-out, positioned on the side of the DIP package
|
|
|
|
|
* closest to the first pin.
|
|
|
|
|
*/
|
|
|
|
|
const auto orientationIndicatorRect = QRectF(
|
|
|
|
|
-(this->orientationIndicatorDiameter / 2),
|
|
|
|
|
(bodyHeight / 2) - (this->orientationIndicatorDiameter / 2),
|
|
|
|
|
this->orientationIndicatorDiameter,
|
|
|
|
|
this->orientationIndicatorDiameter
|
|
|
|
|
);
|
2021-12-01 02:33:41 +00:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
static const auto backgroundColor = QColor(0x37, 0x38, 0x35);
|
|
|
|
|
auto targetBodyColor = this->getBodyColor();
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
if (!this->isEnabled()) {
|
|
|
|
|
targetBodyColor.setAlpha(this->getDisableAlphaLevel());
|
|
|
|
|
}
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
painter.setPen(Qt::PenStyle::NoPen);
|
|
|
|
|
painter.setBrush(targetBodyColor);
|
|
|
|
|
painter.drawRect(bodyRect);
|
|
|
|
|
painter.setBrush(backgroundColor);
|
|
|
|
|
painter.drawEllipse(firstPinIndicatorRect);
|
|
|
|
|
painter.drawEllipse(orientationIndicatorRect);
|
|
|
|
|
}
|
2021-04-04 21:04:12 +01:00
|
|
|
}
|