namespace pv {
const QSizeF Signal::LabelPadding(4, 0);
+const int Signal::LabelHitPadding = 2;
Signal::Signal(QString name) :
_name(name)
return _name;
}
-QRectF Signal::get_label_rect(QPainter &p, const QRect &rect)
-{
- const QSizeF text_size = p.boundingRect(
- QRectF(0, 0, rect.width(), 0), 0, _name).size();
-
- const float nominal_offset = get_nominal_offset(rect);
- const QSizeF label_size(
- text_size.width() + LabelPadding.width() * 2,
- text_size.height() + LabelPadding.height() * 2);
- const float label_arrow_length = label_size.height() / 2;
- return QRectF(
- rect.right() - label_arrow_length - label_size.width(),
- nominal_offset - label_size.height() / 2,
- label_size.width(), label_size.height());
-}
-
-void Signal::paint_label(QPainter &p, const QRect &rect)
+void Signal::paint_label(QPainter &p, const QRect &rect, bool hover)
{
p.setBrush(get_colour());
};
p.setPen(Qt::transparent);
- p.setBrush(colour);
+ p.setBrush(hover ? colour.lighter() : colour);
p.drawPolygon(points, countof(points));
p.setPen(colour.lighter());
p.drawText(label_rect, Qt::AlignCenter | Qt::AlignVCenter, _name);
}
+bool Signal::pt_in_label_rect(QPainter &p,
+ const QRect &rect, const QPoint &point)
+{
+ const QRectF label = get_label_rect(p, rect);
+ return QRectF(
+ QPointF(label.left() - LabelHitPadding,
+ label.top() - LabelHitPadding),
+ QPointF(rect.right(),
+ label.bottom() + LabelHitPadding)
+ ).contains(point);
+}
+
+QRectF Signal::get_label_rect(QPainter &p, const QRect &rect)
+{
+ const QSizeF text_size = p.boundingRect(
+ QRectF(0, 0, rect.width(), 0), 0, _name).size();
+
+ const float nominal_offset = get_nominal_offset(rect);
+ const QSizeF label_size(
+ text_size.width() + LabelPadding.width() * 2,
+ text_size.height() + LabelPadding.height() * 2);
+ const float label_arrow_length = label_size.height() / 2;
+ return QRectF(
+ rect.right() - label_arrow_length - label_size.width(),
+ nominal_offset - label_size.height() / 2,
+ label_size.width(), label_size.height());
+}
+
} // namespace pv
{
private:
static const QSizeF LabelPadding;
+ static const int LabelHitPadding;
protected:
Signal(QString name);
virtual void paint(QPainter &p, const QRect &rect, double scale,
double offset) = 0;
+
+ /**
+ * Paints the signal label into a QGLWidget.
+ * @param p the QPainter to paint into.
+ * @param rect the rectangular area to draw the label into.
+ * @param hover true if the label is being hovered over by the mouse.
+ */
+ virtual void paint_label(QPainter &p, const QRect &rect,
+ bool hover);
+
+ /**
+ * Determines if a point is in the header label rect.
+ * @param p the QPainter to paint into.
+ * @param rect the rectangular area to draw the label into.
+ * @param point the point to test.
+ */
+ bool pt_in_label_rect(QPainter &p, const QRect &rect,
+ const QPoint &point);
+
+private:
/**
* Computes the outline rectangle of a label.
* @param p the QPainter to lay out text with.
* @return Returns the rectangle of the signal label.
*/
virtual QRectF get_label_rect(QPainter &p, const QRect &rect);
-
- /**
- * Paints the signal label into a QGLWidget.
- * @param p the QPainter to paint into.
- * @param rect the rectangular area to draw the label into.
- */
- virtual void paint_label(QPainter &p, const QRect &rect);
protected:
-
/**
* Get the colour of the logic signal
*/
#include <boost/foreach.hpp>
+#include <QMouseEvent>
#include <QPainter>
#include <QRect>
QWidget(&parent),
_view(parent)
{
+ setMouseTracking(true);
}
void Header::paintEvent(QPaintEvent *event)
{
assert(s);
- const QRect label_rect(0, offset, w, View::SignalHeight);
- s->paint_label(painter, label_rect);
+ const QRect signal_heading_rect(
+ 0, offset, w, View::SignalHeight);
+
+ s->paint_label(painter, signal_heading_rect,
+ s->pt_in_label_rect(painter,
+ signal_heading_rect, _mouse_point));
offset += View::SignalHeight;
}
painter.end();
}
+void Header::mouseMoveEvent(QMouseEvent *event)
+{
+ assert(event);
+ _mouse_point = event->pos();
+ update();
+}
+
+void Header::leaveEvent(QEvent *event)
+{
+ _mouse_point = QPoint(-1, -1);
+ update();
+}
+
} // namespace view
} // namespace pv