Support tooltips with an icon.

This commit is contained in:
John Preston 2020-04-30 14:50:34 +04:00
parent 7849f0561b
commit 66e44506dc
3 changed files with 22 additions and 5 deletions

View file

@ -62,12 +62,17 @@ void Widget::updateGeometry() {
width, width,
parentWidget()->width() - _st->margin.left() - _st->margin.right()); parentWidget()->width() - _st->margin.left() - _st->margin.right());
_textWidth = widthWithoutPadding(width); _textWidth = widthWithoutPadding(width);
const auto textHeight = _multiline _textHeight = _multiline
? qMin(_text.countHeight(_textWidth), _maxTextHeight) ? qMin(_text.countHeight(_textWidth), _maxTextHeight)
: _text.minHeight(); : _text.minHeight();
const auto height = _st->padding.top() const auto minHeight = _st->icon.empty()
+ textHeight ? 0
: (_st->icon.height() + 2 * _st->iconPosition.y());
const auto normalHeight = _st->padding.top()
+ _textHeight
+ _st->padding.bottom(); + _st->padding.bottom();
const auto height = std::max(minHeight, normalHeight);
_textTop = _st->padding.top() + ((height - normalHeight) / 2);
const auto rect = QRect(0, 0, width, height); const auto rect = QRect(0, 0, width, height);
const auto outer = parentWidget()->size(); const auto outer = parentWidget()->size();
const auto full = QPoint(outer.width(), outer.height()); const auto full = QPoint(outer.width(), outer.height());
@ -123,6 +128,14 @@ void Widget::paintEvent(QPaintEvent *e) {
_roundRect.paint(p, rect()); _roundRect.paint(p, rect());
} }
if (!_st->icon.empty()) {
_st->icon.paint(
p,
_st->iconPosition.x(),
_st->iconPosition.y(),
width());
}
p.setTextPalette(_st->palette); p.setTextPalette(_st->palette);
const auto lines = _maxTextHeight / _st->style.font->height; const auto lines = _maxTextHeight / _st->style.font->height;
@ -130,7 +143,7 @@ void Widget::paintEvent(QPaintEvent *e) {
_text.drawElided( _text.drawElided(
p, p,
_st->padding.left(), _st->padding.left(),
_st->padding.top(), _textTop,
_textWidth + 1, _textWidth + 1,
lines); lines);
} }
@ -151,7 +164,7 @@ void Widget::mouseMoveEvent(QMouseEvent *e) {
return; return;
} }
const auto point = e->pos() const auto point = e->pos()
- QPoint(_st->padding.left(), _st->padding.top()); - QPoint(_st->padding.left(), _textTop);
const auto lines = _maxTextHeight / _st->style.font->height; const auto lines = _maxTextHeight / _st->style.font->height;
const auto state = _text.getStateElided(point, _textWidth + 1); const auto state = _text.getStateElided(point, _textWidth + 1);
const auto was = ClickHandler::getActive(); const auto was = ClickHandler::getActive();

View file

@ -47,6 +47,8 @@ private:
int _maxTextWidth = 0; int _maxTextWidth = 0;
int _maxTextHeight = 0; int _maxTextHeight = 0;
int _textWidth = 0; int _textWidth = 0;
int _textHeight = 0;
int _textTop = 0;
Text::String _text; Text::String _text;
ClickHandlerFilter _clickHandlerFilter; ClickHandlerFilter _clickHandlerFilter;

View file

@ -570,6 +570,8 @@ SideBarButton {
Toast { Toast {
style: TextStyle; style: TextStyle;
icon: icon;
iconPosition: point;
palette: TextPalette; palette: TextPalette;
padding: margins; padding: margins;
margin: margins; margin: margins;