2014-05-30 08:53:19 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2014-05-30 08:53:19 +00:00
|
|
|
*/
|
2016-11-16 10:44:06 +00:00
|
|
|
#include "ui/widgets/labels.h"
|
2016-10-01 12:34:23 +00:00
|
|
|
|
2016-10-26 16:43:13 +00:00
|
|
|
#include "ui/widgets/popup_menu.h"
|
2016-05-26 15:31:20 +00:00
|
|
|
#include "mainwindow.h"
|
2017-04-13 08:27:10 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-11-16 10:44:06 +00:00
|
|
|
namespace Ui {
|
2014-05-30 08:53:19 +00:00
|
|
|
namespace {
|
2016-11-16 10:44:06 +00:00
|
|
|
|
|
|
|
TextParseOptions _labelOptions = {
|
|
|
|
TextParseMultiline, // flags
|
|
|
|
0, // maxw
|
|
|
|
0, // maxh
|
|
|
|
Qt::LayoutDirectionAuto, // dir
|
|
|
|
};
|
2016-12-31 15:19:22 +00:00
|
|
|
|
2016-11-16 10:44:06 +00:00
|
|
|
TextParseOptions _labelMarkedOptions = {
|
2017-07-06 13:44:11 +00:00
|
|
|
TextParseMultiline | TextParseRichText | TextParseLinks | TextParseHashtags | TextParseMentions | TextParseBotCommands | TextParseMarkdown, // flags
|
2016-11-16 10:44:06 +00:00
|
|
|
0, // maxw
|
|
|
|
0, // maxh
|
|
|
|
Qt::LayoutDirectionAuto, // dir
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2016-12-23 13:21:01 +00:00
|
|
|
CrossFadeAnimation::CrossFadeAnimation(style::color bg) : _bg(bg) {
|
2016-11-24 19:28:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CrossFadeAnimation::addLine(Part was, Part now) {
|
2017-02-21 13:45:56 +00:00
|
|
|
_lines.push_back(Line(std::move(was), std::move(now)));
|
2016-11-24 19:28:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CrossFadeAnimation::paintFrame(Painter &p, float64 positionReady, float64 alphaWas, float64 alphaNow) {
|
|
|
|
if (_lines.isEmpty()) return;
|
|
|
|
|
|
|
|
for_const (auto &line, _lines) {
|
|
|
|
paintLine(p, line, positionReady, alphaWas, alphaNow);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CrossFadeAnimation::paintLine(Painter &p, const Line &line, float64 positionReady, float64 alphaWas, float64 alphaNow) {
|
|
|
|
auto &snapshotWas = line.was.snapshot;
|
|
|
|
auto &snapshotNow = line.now.snapshot;
|
2017-04-15 17:37:43 +00:00
|
|
|
if (snapshotWas.isNull() && snapshotNow.isNull()) {
|
|
|
|
// This can happen if both labels have an empty line or if one
|
|
|
|
// label has an empty line where the second one already ended.
|
|
|
|
// In this case lineWidth is zero and snapshot is null.
|
|
|
|
return;
|
|
|
|
}
|
2016-11-24 19:28:23 +00:00
|
|
|
|
|
|
|
auto positionWas = line.was.position;
|
|
|
|
auto positionNow = line.now.position;
|
|
|
|
auto left = anim::interpolate(positionWas.x(), positionNow.x(), positionReady);
|
|
|
|
auto topDelta = (snapshotNow.height() / cIntRetinaFactor()) - (snapshotWas.height() / cIntRetinaFactor());
|
|
|
|
auto widthDelta = (snapshotNow.width() / cIntRetinaFactor()) - (snapshotWas.width() / cIntRetinaFactor());
|
|
|
|
auto topWas = anim::interpolate(positionWas.y(), positionNow.y() + topDelta, positionReady);
|
|
|
|
auto topNow = topWas - topDelta;
|
|
|
|
|
|
|
|
p.setOpacity(alphaWas);
|
|
|
|
if (!snapshotWas.isNull()) {
|
|
|
|
p.drawPixmap(left, topWas, snapshotWas);
|
|
|
|
if (topDelta > 0) {
|
|
|
|
p.fillRect(left, topWas - topDelta, snapshotWas.width() / cIntRetinaFactor(), topDelta, _bg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (widthDelta > 0) {
|
|
|
|
p.fillRect(left + (snapshotWas.width() / cIntRetinaFactor()), topNow, widthDelta, snapshotNow.height() / cIntRetinaFactor(), _bg);
|
|
|
|
}
|
|
|
|
|
|
|
|
p.setOpacity(alphaNow);
|
|
|
|
if (!snapshotNow.isNull()) {
|
|
|
|
p.drawPixmap(left, topNow, snapshotNow);
|
|
|
|
if (topDelta < 0) {
|
|
|
|
p.fillRect(left, topNow + topDelta, snapshotNow.width() / cIntRetinaFactor(), -topDelta, _bg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (widthDelta < 0) {
|
|
|
|
p.fillRect(left + (snapshotNow.width() / cIntRetinaFactor()), topWas, -widthDelta, snapshotWas.height() / cIntRetinaFactor(), _bg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-15 18:56:17 +00:00
|
|
|
LabelSimple::LabelSimple(
|
|
|
|
QWidget *parent,
|
|
|
|
const style::LabelSimple &st,
|
|
|
|
const QString &value)
|
|
|
|
: RpWidget(parent)
|
2016-11-16 10:44:06 +00:00
|
|
|
, _st(st) {
|
|
|
|
setText(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LabelSimple::setText(const QString &value, bool *outTextChanged) {
|
|
|
|
if (_fullText == value) {
|
|
|
|
if (outTextChanged) *outTextChanged = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_fullText = value;
|
|
|
|
_fullTextWidth = _st.font->width(_fullText);
|
|
|
|
if (!_st.maxWidth || _fullTextWidth <= _st.maxWidth) {
|
|
|
|
_text = _fullText;
|
|
|
|
_textWidth = _fullTextWidth;
|
|
|
|
} else {
|
|
|
|
auto newText = _st.font->elided(_fullText, _st.maxWidth);
|
|
|
|
if (newText == _text) {
|
|
|
|
if (outTextChanged) *outTextChanged = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_text = newText;
|
|
|
|
_textWidth = _st.font->width(_text);
|
|
|
|
}
|
|
|
|
resize(_textWidth, _st.font->height);
|
|
|
|
update();
|
|
|
|
if (outTextChanged) *outTextChanged = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LabelSimple::paintEvent(QPaintEvent *e) {
|
|
|
|
Painter p(this);
|
|
|
|
|
|
|
|
p.setFont(_st.font);
|
|
|
|
p.setPen(_st.textFg);
|
|
|
|
p.drawTextLeft(0, 0, width(), _text, _textWidth);
|
|
|
|
}
|
|
|
|
|
2017-09-13 16:57:44 +00:00
|
|
|
FlatLabel::FlatLabel(QWidget *parent, const style::FlatLabel &st)
|
|
|
|
: RpWidget(parent)
|
2017-11-10 15:45:10 +00:00
|
|
|
, _text(st.minWidth ? st.minWidth : QFIXED_MAX)
|
2016-05-31 19:27:11 +00:00
|
|
|
, _st(st)
|
|
|
|
, _contextCopyText(lang(lng_context_copy_text)) {
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2017-09-13 16:57:44 +00:00
|
|
|
FlatLabel::FlatLabel(
|
|
|
|
QWidget *parent,
|
|
|
|
const QString &text,
|
|
|
|
InitType initType,
|
|
|
|
const style::FlatLabel &st)
|
|
|
|
: RpWidget(parent)
|
2017-11-10 15:45:10 +00:00
|
|
|
, _text(st.minWidth ? st.minWidth : QFIXED_MAX)
|
2016-05-31 19:27:11 +00:00
|
|
|
, _st(st)
|
|
|
|
, _contextCopyText(lang(lng_context_copy_text)) {
|
|
|
|
if (initType == InitType::Rich) {
|
|
|
|
setRichText(text);
|
|
|
|
} else {
|
|
|
|
setText(text);
|
|
|
|
}
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2017-09-13 16:57:44 +00:00
|
|
|
FlatLabel::FlatLabel(
|
|
|
|
QWidget *parent,
|
|
|
|
rpl::producer<QString> &&text,
|
|
|
|
const style::FlatLabel &st)
|
|
|
|
: RpWidget(parent)
|
2017-11-10 15:45:10 +00:00
|
|
|
, _text(st.minWidth ? st.minWidth : QFIXED_MAX)
|
2017-09-13 16:57:44 +00:00
|
|
|
, _st(st)
|
|
|
|
, _contextCopyText(lang(lng_context_copy_text)) {
|
2017-09-20 18:40:23 +00:00
|
|
|
textUpdated();
|
2017-12-22 07:05:20 +00:00
|
|
|
std::move(
|
|
|
|
text
|
|
|
|
) | rpl::start_with_next([this](const QString &value) {
|
|
|
|
setText(value);
|
|
|
|
}, lifetime());
|
2017-09-13 16:57:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FlatLabel::FlatLabel(
|
|
|
|
QWidget *parent,
|
|
|
|
rpl::producer<TextWithEntities> &&text,
|
|
|
|
const style::FlatLabel &st)
|
|
|
|
: RpWidget(parent)
|
2017-11-10 15:45:10 +00:00
|
|
|
, _text(st.minWidth ? st.minWidth : QFIXED_MAX)
|
2017-09-13 16:57:44 +00:00
|
|
|
, _st(st)
|
|
|
|
, _contextCopyText(lang(lng_context_copy_text)) {
|
2017-09-20 18:40:23 +00:00
|
|
|
textUpdated();
|
2017-12-22 07:05:20 +00:00
|
|
|
std::move(
|
|
|
|
text
|
|
|
|
) | rpl::start_with_next([this](const TextWithEntities &value) {
|
|
|
|
setMarkedText(value);
|
|
|
|
}, lifetime());
|
2017-09-13 16:57:44 +00:00
|
|
|
}
|
|
|
|
|
2016-05-31 19:27:11 +00:00
|
|
|
void FlatLabel::init() {
|
2016-05-26 15:31:20 +00:00
|
|
|
_trippleClickTimer.setSingleShot(true);
|
|
|
|
|
|
|
|
_touchSelectTimer.setSingleShot(true);
|
|
|
|
connect(&_touchSelectTimer, SIGNAL(timeout()), this, SLOT(onTouchSelect()));
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2017-01-02 17:11:49 +00:00
|
|
|
void FlatLabel::textUpdated() {
|
2016-05-26 15:31:20 +00:00
|
|
|
refreshSize();
|
|
|
|
setMouseTracking(_selectable || _text.hasLinks());
|
2016-09-23 16:04:26 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FlatLabel::setText(const QString &text) {
|
2017-01-02 17:11:49 +00:00
|
|
|
_text.setText(_st.style, text, _labelOptions);
|
|
|
|
textUpdated();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FlatLabel::setRichText(const QString &text) {
|
2017-01-02 17:11:49 +00:00
|
|
|
_text.setRichText(_st.style, text, _labelOptions);
|
|
|
|
textUpdated();
|
2016-05-26 15:31:20 +00:00
|
|
|
}
|
|
|
|
|
2016-05-31 19:27:11 +00:00
|
|
|
void FlatLabel::setMarkedText(const TextWithEntities &textWithEntities) {
|
2017-01-02 17:11:49 +00:00
|
|
|
_text.setMarkedText(_st.style, textWithEntities, _labelMarkedOptions);
|
|
|
|
textUpdated();
|
2016-05-31 19:27:11 +00:00
|
|
|
}
|
|
|
|
|
2016-05-26 15:31:20 +00:00
|
|
|
void FlatLabel::setSelectable(bool selectable) {
|
|
|
|
_selectable = selectable;
|
|
|
|
setMouseTracking(_selectable || _text.hasLinks());
|
|
|
|
}
|
|
|
|
|
2016-05-31 19:27:11 +00:00
|
|
|
void FlatLabel::setDoubleClickSelectsParagraph(bool doubleClickSelectsParagraph) {
|
|
|
|
_doubleClickSelectsParagraph = doubleClickSelectsParagraph;
|
|
|
|
}
|
|
|
|
|
2016-05-26 15:31:20 +00:00
|
|
|
void FlatLabel::setContextCopyText(const QString ©Text) {
|
|
|
|
_contextCopyText = copyText;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-05-31 19:27:11 +00:00
|
|
|
void FlatLabel::setExpandLinksMode(ExpandLinksMode mode) {
|
|
|
|
_contextExpandLinksMode = mode;
|
|
|
|
}
|
|
|
|
|
2016-06-01 16:40:51 +00:00
|
|
|
void FlatLabel::setBreakEverywhere(bool breakEverywhere) {
|
|
|
|
_breakEverywhere = breakEverywhere;
|
|
|
|
}
|
|
|
|
|
2016-08-22 17:16:21 +00:00
|
|
|
int FlatLabel::resizeGetHeight(int newWidth) {
|
|
|
|
_allowedWidth = newWidth;
|
|
|
|
int textWidth = countTextWidth();
|
|
|
|
int textHeight = countTextHeight(textWidth);
|
2017-09-20 19:44:22 +00:00
|
|
|
return textHeight;
|
2016-05-26 15:31:20 +00:00
|
|
|
}
|
|
|
|
|
2016-05-31 19:27:11 +00:00
|
|
|
int FlatLabel::naturalWidth() const {
|
|
|
|
return _text.maxWidth();
|
|
|
|
}
|
|
|
|
|
2017-09-20 19:44:22 +00:00
|
|
|
QMargins FlatLabel::getMargins() const {
|
|
|
|
return _st.margin;
|
|
|
|
}
|
|
|
|
|
2016-05-26 15:31:20 +00:00
|
|
|
int FlatLabel::countTextWidth() const {
|
2017-09-20 19:44:22 +00:00
|
|
|
return _allowedWidth
|
|
|
|
? _allowedWidth
|
2017-11-10 15:45:10 +00:00
|
|
|
: (_st.minWidth ? _st.minWidth : _text.maxWidth());
|
2016-05-26 15:31:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int FlatLabel::countTextHeight(int textWidth) {
|
|
|
|
_fullTextHeight = _text.countHeight(textWidth);
|
|
|
|
return _st.maxHeight ? qMin(_fullTextHeight, _st.maxHeight) : _fullTextHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FlatLabel::refreshSize() {
|
|
|
|
int textWidth = countTextWidth();
|
|
|
|
int textHeight = countTextHeight(textWidth);
|
|
|
|
int fullWidth = _st.margin.left() + textWidth + _st.margin.right();
|
|
|
|
int fullHeight = _st.margin.top() + textHeight + _st.margin.bottom();
|
|
|
|
resize(fullWidth, fullHeight);
|
2016-03-10 10:15:21 +00:00
|
|
|
}
|
|
|
|
|
2016-03-29 17:17:00 +00:00
|
|
|
void FlatLabel::setLink(uint16 lnkIndex, const ClickHandlerPtr &lnk) {
|
2014-05-30 08:53:19 +00:00
|
|
|
_text.setLink(lnkIndex, lnk);
|
|
|
|
}
|
|
|
|
|
2018-06-22 19:46:20 +00:00
|
|
|
void FlatLabel::setClickHandlerFilter(ClickHandlerFilter &&filter) {
|
|
|
|
_clickHandlerFilter = std::move(filter);
|
2016-05-31 19:27:11 +00:00
|
|
|
}
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
void FlatLabel::mouseMoveEvent(QMouseEvent *e) {
|
|
|
|
_lastMousePos = e->globalPos();
|
2016-05-26 15:31:20 +00:00
|
|
|
dragActionUpdate();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FlatLabel::mousePressEvent(QMouseEvent *e) {
|
2016-05-26 15:31:20 +00:00
|
|
|
if (_contextMenu) {
|
|
|
|
e->accept();
|
|
|
|
return; // ignore mouse press, that was hiding context menu
|
|
|
|
}
|
|
|
|
dragActionStart(e->globalPos(), e->button());
|
|
|
|
}
|
|
|
|
|
|
|
|
Text::StateResult FlatLabel::dragActionStart(const QPoint &p, Qt::MouseButton button) {
|
|
|
|
_lastMousePos = p;
|
|
|
|
auto state = dragActionUpdate();
|
|
|
|
|
|
|
|
if (button != Qt::LeftButton) return state;
|
|
|
|
|
2016-03-29 17:17:00 +00:00
|
|
|
ClickHandler::pressed();
|
2016-05-26 15:31:20 +00:00
|
|
|
_dragAction = NoDrag;
|
2017-06-21 21:38:31 +00:00
|
|
|
_dragWasInactive = App::wnd()->wasInactivePress();
|
|
|
|
if (_dragWasInactive) App::wnd()->setInactivePress(false);
|
2016-05-26 15:31:20 +00:00
|
|
|
|
|
|
|
if (ClickHandler::getPressed()) {
|
2016-05-26 16:05:39 +00:00
|
|
|
_dragStartPosition = mapFromGlobal(_lastMousePos);
|
2016-05-26 15:31:20 +00:00
|
|
|
_dragAction = PrepareDrag;
|
|
|
|
}
|
|
|
|
if (!_selectable || _dragAction != NoDrag) {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_trippleClickTimer.isActive() && (_lastMousePos - _trippleClickPoint).manhattanLength() < QApplication::startDragDistance()) {
|
|
|
|
if (state.uponSymbol) {
|
|
|
|
_selection = { state.symbol, state.symbol };
|
|
|
|
_savedSelection = { 0, 0 };
|
|
|
|
_dragSymbol = state.symbol;
|
|
|
|
_dragAction = Selecting;
|
|
|
|
_selectionType = TextSelectType::Paragraphs;
|
|
|
|
updateHover(state);
|
|
|
|
_trippleClickTimer.start(QApplication::doubleClickInterval());
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_selectionType != TextSelectType::Paragraphs) {
|
|
|
|
_dragSymbol = state.symbol;
|
|
|
|
bool uponSelected = state.uponSymbol;
|
|
|
|
if (uponSelected) {
|
|
|
|
if (_dragSymbol < _selection.from || _dragSymbol >= _selection.to) {
|
|
|
|
uponSelected = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (uponSelected) {
|
2016-05-26 16:05:39 +00:00
|
|
|
_dragStartPosition = mapFromGlobal(_lastMousePos);
|
2016-05-26 15:31:20 +00:00
|
|
|
_dragAction = PrepareDrag; // start text drag
|
|
|
|
} else if (!_dragWasInactive) {
|
|
|
|
if (state.afterSymbol) ++_dragSymbol;
|
|
|
|
_selection = { _dragSymbol, _dragSymbol };
|
|
|
|
_savedSelection = { 0, 0 };
|
|
|
|
_dragAction = Selecting;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
Text::StateResult FlatLabel::dragActionFinish(const QPoint &p, Qt::MouseButton button) {
|
|
|
|
_lastMousePos = p;
|
|
|
|
auto state = dragActionUpdate();
|
|
|
|
|
2017-12-18 09:07:18 +00:00
|
|
|
auto activated = ClickHandler::unpressed();
|
2016-05-26 15:31:20 +00:00
|
|
|
if (_dragAction == Dragging) {
|
2017-12-18 09:07:18 +00:00
|
|
|
activated = nullptr;
|
2016-05-26 15:31:20 +00:00
|
|
|
} else if (_dragAction == PrepareDrag) {
|
|
|
|
_selection = { 0, 0 };
|
|
|
|
_savedSelection = { 0, 0 };
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
_dragAction = NoDrag;
|
|
|
|
_selectionType = TextSelectType::Letters;
|
|
|
|
|
|
|
|
if (activated) {
|
2018-06-22 19:46:20 +00:00
|
|
|
if (!_clickHandlerFilter
|
|
|
|
|| _clickHandlerFilter(activated, button)) {
|
2016-05-31 19:27:11 +00:00
|
|
|
App::activateClickHandler(activated, button);
|
|
|
|
}
|
2016-05-26 15:31:20 +00:00
|
|
|
}
|
2016-06-14 16:26:41 +00:00
|
|
|
|
|
|
|
#if defined Q_OS_LINUX32 || defined Q_OS_LINUX64
|
|
|
|
if (!_selection.empty()) {
|
|
|
|
QApplication::clipboard()->setText(_text.originalText(_selection, _contextExpandLinksMode), QClipboard::Selection);
|
|
|
|
}
|
|
|
|
#endif // Q_OS_LINUX32 || Q_OS_LINUX64
|
|
|
|
|
2016-05-26 15:31:20 +00:00
|
|
|
return state;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FlatLabel::mouseReleaseEvent(QMouseEvent *e) {
|
2016-05-26 15:31:20 +00:00
|
|
|
dragActionFinish(e->globalPos(), e->button());
|
|
|
|
if (!rect().contains(e->pos())) {
|
|
|
|
leaveEvent(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FlatLabel::mouseDoubleClickEvent(QMouseEvent *e) {
|
|
|
|
auto state = dragActionStart(e->globalPos(), e->button());
|
|
|
|
if (((_dragAction == Selecting) || (_dragAction == NoDrag)) && _selectionType == TextSelectType::Letters) {
|
|
|
|
if (state.uponSymbol) {
|
|
|
|
_dragSymbol = state.symbol;
|
2016-05-31 19:27:11 +00:00
|
|
|
_selectionType = _doubleClickSelectsParagraph ? TextSelectType::Paragraphs : TextSelectType::Words;
|
2016-05-26 15:31:20 +00:00
|
|
|
if (_dragAction == NoDrag) {
|
|
|
|
_dragAction = Selecting;
|
|
|
|
_selection = { state.symbol, state.symbol };
|
|
|
|
_savedSelection = { 0, 0 };
|
|
|
|
}
|
|
|
|
mouseMoveEvent(e);
|
|
|
|
|
|
|
|
_trippleClickPoint = e->globalPos();
|
|
|
|
_trippleClickTimer.start(QApplication::doubleClickInterval());
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-11 11:24:37 +00:00
|
|
|
void FlatLabel::enterEventHook(QEvent *e) {
|
2016-03-30 08:03:29 +00:00
|
|
|
_lastMousePos = QCursor::pos();
|
2016-05-26 15:31:20 +00:00
|
|
|
dragActionUpdate();
|
2016-03-30 08:03:29 +00:00
|
|
|
}
|
|
|
|
|
2017-02-11 11:24:37 +00:00
|
|
|
void FlatLabel::leaveEventHook(QEvent *e) {
|
2016-03-29 17:17:00 +00:00
|
|
|
ClickHandler::clearActive(this);
|
|
|
|
}
|
|
|
|
|
2016-05-26 15:31:20 +00:00
|
|
|
void FlatLabel::focusOutEvent(QFocusEvent *e) {
|
|
|
|
if (!_selection.empty()) {
|
2016-05-31 19:27:11 +00:00
|
|
|
if (_contextMenu) {
|
|
|
|
_savedSelection = _selection;
|
|
|
|
}
|
2016-05-26 15:31:20 +00:00
|
|
|
_selection = { 0, 0 };
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FlatLabel::focusInEvent(QFocusEvent *e) {
|
|
|
|
if (!_savedSelection.empty()) {
|
|
|
|
_selection = _savedSelection;
|
|
|
|
_savedSelection = { 0, 0 };
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FlatLabel::keyPressEvent(QKeyEvent *e) {
|
|
|
|
e->ignore();
|
|
|
|
if (e->key() == Qt::Key_Copy || (e->key() == Qt::Key_C && e->modifiers().testFlag(Qt::ControlModifier))) {
|
|
|
|
if (!_selection.empty()) {
|
|
|
|
onCopySelectedText();
|
|
|
|
e->accept();
|
|
|
|
}
|
2016-06-28 18:27:23 +00:00
|
|
|
#ifdef Q_OS_MAC
|
|
|
|
} else if (e->key() == Qt::Key_E && e->modifiers().testFlag(Qt::ControlModifier)) {
|
|
|
|
auto selection = _selection.empty() ? (_contextMenu ? _savedSelection : _selection) : _selection;
|
|
|
|
if (!selection.empty()) {
|
|
|
|
QApplication::clipboard()->setText(_text.originalText(selection, _contextExpandLinksMode), QClipboard::FindBuffer);
|
|
|
|
}
|
|
|
|
#endif // Q_OS_MAC
|
2016-05-26 15:31:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FlatLabel::contextMenuEvent(QContextMenuEvent *e) {
|
2016-06-03 21:46:45 +00:00
|
|
|
if (!_selectable) return;
|
|
|
|
|
2016-05-26 15:31:20 +00:00
|
|
|
showContextMenu(e, ContextMenuReason::FromEvent);
|
|
|
|
}
|
|
|
|
|
2017-09-20 19:44:22 +00:00
|
|
|
bool FlatLabel::eventHook(QEvent *e) {
|
2016-05-26 15:31:20 +00:00
|
|
|
if (e->type() == QEvent::TouchBegin || e->type() == QEvent::TouchUpdate || e->type() == QEvent::TouchEnd || e->type() == QEvent::TouchCancel) {
|
|
|
|
QTouchEvent *ev = static_cast<QTouchEvent*>(e);
|
|
|
|
if (ev->device()->type() == QTouchDevice::TouchScreen) {
|
|
|
|
touchEvent(ev);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2017-09-20 19:44:22 +00:00
|
|
|
return RpWidget::eventHook(e);
|
2016-05-26 15:31:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FlatLabel::touchEvent(QTouchEvent *e) {
|
|
|
|
const Qt::TouchPointStates &states(e->touchPointStates());
|
|
|
|
if (e->type() == QEvent::TouchCancel) { // cancel
|
|
|
|
if (!_touchInProgress) return;
|
|
|
|
_touchInProgress = false;
|
|
|
|
_touchSelectTimer.stop();
|
|
|
|
_touchSelect = false;
|
|
|
|
_dragAction = NoDrag;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!e->touchPoints().isEmpty()) {
|
|
|
|
_touchPrevPos = _touchPos;
|
|
|
|
_touchPos = e->touchPoints().cbegin()->screenPos().toPoint();
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (e->type()) {
|
2017-11-30 18:18:39 +00:00
|
|
|
case QEvent::TouchBegin: {
|
|
|
|
if (_contextMenu) {
|
|
|
|
e->accept();
|
|
|
|
return; // ignore mouse press, that was hiding context menu
|
|
|
|
}
|
|
|
|
if (_touchInProgress) return;
|
|
|
|
if (e->touchPoints().isEmpty()) return;
|
|
|
|
|
|
|
|
_touchInProgress = true;
|
|
|
|
_touchSelectTimer.start(QApplication::startDragTime());
|
|
|
|
_touchSelect = false;
|
|
|
|
_touchStart = _touchPrevPos = _touchPos;
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case QEvent::TouchUpdate: {
|
|
|
|
if (!_touchInProgress) return;
|
|
|
|
if (_touchSelect) {
|
|
|
|
_lastMousePos = _touchPos;
|
|
|
|
dragActionUpdate();
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case QEvent::TouchEnd: {
|
|
|
|
if (!_touchInProgress) return;
|
|
|
|
_touchInProgress = false;
|
|
|
|
auto weak = make_weak(this);
|
|
|
|
if (_touchSelect) {
|
|
|
|
dragActionFinish(_touchPos, Qt::RightButton);
|
|
|
|
QContextMenuEvent contextMenu(QContextMenuEvent::Mouse, mapFromGlobal(_touchPos), _touchPos);
|
|
|
|
showContextMenu(&contextMenu, ContextMenuReason::FromTouch);
|
|
|
|
} else { // one short tap -- like mouse click
|
|
|
|
dragActionStart(_touchPos, Qt::LeftButton);
|
|
|
|
dragActionFinish(_touchPos, Qt::LeftButton);
|
|
|
|
}
|
|
|
|
if (weak) {
|
|
|
|
_touchSelectTimer.stop();
|
|
|
|
_touchSelect = false;
|
|
|
|
}
|
|
|
|
} break;
|
2016-05-26 15:31:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FlatLabel::showContextMenu(QContextMenuEvent *e, ContextMenuReason reason) {
|
|
|
|
if (_contextMenu) {
|
|
|
|
_contextMenu->deleteLater();
|
|
|
|
_contextMenu = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e->reason() == QContextMenuEvent::Mouse) {
|
|
|
|
_lastMousePos = e->globalPos();
|
|
|
|
} else {
|
|
|
|
_lastMousePos = QCursor::pos();
|
|
|
|
}
|
|
|
|
auto state = dragActionUpdate();
|
|
|
|
|
|
|
|
bool hasSelection = !_selection.empty();
|
|
|
|
bool uponSelection = state.uponSymbol && (state.symbol >= _selection.from) && (state.symbol < _selection.to);
|
|
|
|
bool fullSelection = _text.isFullSelection(_selection);
|
|
|
|
if (reason == ContextMenuReason::FromTouch && hasSelection && !uponSelection) {
|
|
|
|
uponSelection = hasSelection;
|
|
|
|
}
|
|
|
|
|
2018-07-19 14:27:09 +00:00
|
|
|
_contextMenu = new Ui::PopupMenu(this);
|
2016-05-26 15:31:20 +00:00
|
|
|
|
2016-05-31 19:27:11 +00:00
|
|
|
if (fullSelection && !_contextCopyText.isEmpty()) {
|
2018-01-25 10:10:52 +00:00
|
|
|
_contextMenu->addAction(_contextCopyText, this, SLOT(onCopyContextText()));
|
2016-05-31 19:27:11 +00:00
|
|
|
} else if (uponSelection && !fullSelection) {
|
2018-01-25 10:10:52 +00:00
|
|
|
_contextMenu->addAction(lang(lng_context_copy_selected), this, SLOT(onCopySelectedText()));
|
2016-05-31 19:27:11 +00:00
|
|
|
} else if (!hasSelection && !_contextCopyText.isEmpty()) {
|
2018-01-25 10:10:52 +00:00
|
|
|
_contextMenu->addAction(_contextCopyText, this, SLOT(onCopyContextText()));
|
2016-05-26 15:31:20 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 10:10:52 +00:00
|
|
|
if (const auto link = ClickHandler::getActive()) {
|
|
|
|
const auto actionText = link->copyToClipboardContextItemText();
|
|
|
|
if (!actionText.isEmpty()) {
|
|
|
|
_contextMenu->addAction(
|
|
|
|
actionText,
|
|
|
|
[text = link->copyToClipboardText()] {
|
|
|
|
QApplication::clipboard()->setText(text);
|
|
|
|
});
|
|
|
|
}
|
2016-05-26 15:31:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_contextMenu->actions().isEmpty()) {
|
|
|
|
delete _contextMenu;
|
|
|
|
_contextMenu = nullptr;
|
|
|
|
} else {
|
|
|
|
connect(_contextMenu, SIGNAL(destroyed(QObject*)), this, SLOT(onContextMenuDestroy(QObject*)));
|
|
|
|
_contextMenu->popup(e->globalPos());
|
|
|
|
e->accept();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FlatLabel::onCopySelectedText() {
|
|
|
|
auto selection = _selection.empty() ? (_contextMenu ? _savedSelection : _selection) : _selection;
|
|
|
|
if (!selection.empty()) {
|
2016-05-31 19:27:11 +00:00
|
|
|
QApplication::clipboard()->setText(_text.originalText(selection, _contextExpandLinksMode));
|
2016-05-26 15:31:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FlatLabel::onCopyContextText() {
|
2016-05-31 19:27:11 +00:00
|
|
|
QApplication::clipboard()->setText(_text.originalText({ 0, 0xFFFF }, _contextExpandLinksMode));
|
2016-05-26 15:31:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FlatLabel::onTouchSelect() {
|
|
|
|
_touchSelect = true;
|
|
|
|
dragActionStart(_touchPos, Qt::LeftButton);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FlatLabel::onContextMenuDestroy(QObject *obj) {
|
|
|
|
if (obj == _contextMenu) {
|
|
|
|
_contextMenu = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-26 16:05:39 +00:00
|
|
|
void FlatLabel::onExecuteDrag() {
|
|
|
|
if (_dragAction != Dragging) return;
|
|
|
|
|
|
|
|
auto state = getTextState(_dragStartPosition);
|
|
|
|
bool uponSelected = state.uponSymbol && _selection.from <= state.symbol;
|
|
|
|
if (uponSelected) {
|
|
|
|
if (_dragSymbol < _selection.from || _dragSymbol >= _selection.to) {
|
|
|
|
uponSelected = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ClickHandlerPtr pressedHandler = ClickHandler::getPressed();
|
|
|
|
QString selectedText;
|
|
|
|
if (uponSelected) {
|
|
|
|
selectedText = _text.originalText(_selection, ExpandLinksAll);
|
|
|
|
} else if (pressedHandler) {
|
|
|
|
selectedText = pressedHandler->dragText();
|
|
|
|
}
|
|
|
|
if (!selectedText.isEmpty()) {
|
|
|
|
auto mimeData = new QMimeData();
|
|
|
|
mimeData->setText(selectedText);
|
|
|
|
auto drag = new QDrag(App::wnd());
|
|
|
|
drag->setMimeData(mimeData);
|
2016-06-21 17:02:45 +00:00
|
|
|
drag->exec(Qt::CopyAction);
|
2016-06-01 16:40:51 +00:00
|
|
|
|
|
|
|
// We don't receive mouseReleaseEvent when drag is finished.
|
|
|
|
ClickHandler::unpressed();
|
2016-05-26 16:05:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-29 17:17:00 +00:00
|
|
|
void FlatLabel::clickHandlerActiveChanged(const ClickHandlerPtr &action, bool active) {
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FlatLabel::clickHandlerPressedChanged(const ClickHandlerPtr &action, bool active) {
|
|
|
|
update();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2017-12-26 12:41:48 +00:00
|
|
|
std::unique_ptr<CrossFadeAnimation> FlatLabel::CrossFade(
|
|
|
|
not_null<FlatLabel*> from,
|
|
|
|
not_null<FlatLabel*> to,
|
|
|
|
style::color bg,
|
|
|
|
QPoint fromPosition,
|
|
|
|
QPoint toPosition) {
|
2017-02-21 13:45:56 +00:00
|
|
|
auto result = std::make_unique<CrossFadeAnimation>(bg);
|
2016-11-24 19:28:23 +00:00
|
|
|
|
|
|
|
struct Data {
|
|
|
|
QImage full;
|
|
|
|
QVector<int> lineWidths;
|
|
|
|
int lineHeight = 0;
|
|
|
|
int lineAddTop = 0;
|
|
|
|
};
|
2017-12-26 12:41:48 +00:00
|
|
|
auto prepareData = [&bg](not_null<FlatLabel*> label) {
|
2016-11-24 19:28:23 +00:00
|
|
|
auto result = Data();
|
2017-12-26 12:41:48 +00:00
|
|
|
result.full = GrabWidgetToImage(label, QRect(), bg->c);
|
2016-11-24 19:28:23 +00:00
|
|
|
auto textWidth = label->width() - label->_st.margin.left() - label->_st.margin.right();
|
|
|
|
label->_text.countLineWidths(textWidth, &result.lineWidths);
|
2016-12-23 13:21:01 +00:00
|
|
|
result.lineHeight = label->_st.style.font->height;
|
|
|
|
auto addedHeight = (label->_st.style.lineHeight - result.lineHeight);
|
2016-11-24 19:28:23 +00:00
|
|
|
if (addedHeight > 0) {
|
|
|
|
result.lineAddTop = addedHeight / 2;
|
|
|
|
result.lineHeight += addedHeight;
|
|
|
|
}
|
2017-02-21 14:37:53 +00:00
|
|
|
return result;
|
2016-11-24 19:28:23 +00:00
|
|
|
};
|
|
|
|
auto was = prepareData(from);
|
|
|
|
auto now = prepareData(to);
|
|
|
|
|
|
|
|
auto maxLines = qMax(was.lineWidths.size(), now.lineWidths.size());
|
|
|
|
auto fillDataTill = [maxLines](Data &data) {
|
|
|
|
for (auto i = data.lineWidths.size(); i != maxLines; ++i) {
|
|
|
|
data.lineWidths.push_back(-1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
fillDataTill(was);
|
|
|
|
fillDataTill(now);
|
|
|
|
auto preparePart = [](FlatLabel *label, QPoint position, Data &data, int index, Data &other) {
|
|
|
|
auto result = CrossFadeAnimation::Part();
|
|
|
|
auto lineWidth = data.lineWidths[index];
|
|
|
|
if (lineWidth < 0) {
|
|
|
|
lineWidth = other.lineWidths[index];
|
|
|
|
}
|
|
|
|
auto fullWidth = data.full.width() / cIntRetinaFactor();
|
|
|
|
auto top = index * data.lineHeight + data.lineAddTop;
|
|
|
|
auto left = 0;
|
|
|
|
if (label->_st.align & Qt::AlignHCenter) {
|
|
|
|
left += (fullWidth - lineWidth) / 2;
|
|
|
|
} else if (label->_st.align & Qt::AlignRight) {
|
|
|
|
left += (fullWidth - lineWidth);
|
|
|
|
}
|
2016-12-23 13:21:01 +00:00
|
|
|
auto snapshotRect = data.full.rect().intersected(QRect(left * cIntRetinaFactor(), top * cIntRetinaFactor(), lineWidth * cIntRetinaFactor(), label->_st.style.font->height * cIntRetinaFactor()));
|
2016-11-24 19:28:23 +00:00
|
|
|
if (!snapshotRect.isEmpty()) {
|
|
|
|
result.snapshot = App::pixmapFromImageInPlace(data.full.copy(snapshotRect));
|
|
|
|
result.snapshot.setDevicePixelRatio(cRetinaFactor());
|
|
|
|
}
|
|
|
|
auto positionBase = position + label->pos();
|
|
|
|
result.position = positionBase + QPoint(label->_st.margin.left() + left, label->_st.margin.top() + top);
|
2017-02-21 14:37:53 +00:00
|
|
|
return result;
|
2016-11-24 19:28:23 +00:00
|
|
|
};
|
|
|
|
for (int i = 0; i != maxLines; ++i) {
|
|
|
|
result->addLine(preparePart(from, fromPosition, was, i, now), preparePart(to, toPosition, now, i, was));
|
|
|
|
}
|
|
|
|
|
2017-02-21 14:37:53 +00:00
|
|
|
return result;
|
2016-11-24 19:28:23 +00:00
|
|
|
}
|
|
|
|
|
2016-05-26 15:31:20 +00:00
|
|
|
Text::StateResult FlatLabel::dragActionUpdate() {
|
2016-05-26 16:05:39 +00:00
|
|
|
auto m = mapFromGlobal(_lastMousePos);
|
2016-05-26 15:31:20 +00:00
|
|
|
auto state = getTextState(m);
|
|
|
|
updateHover(state);
|
2016-05-26 16:05:39 +00:00
|
|
|
|
|
|
|
if (_dragAction == PrepareDrag && (m - _dragStartPosition).manhattanLength() >= QApplication::startDragDistance()) {
|
|
|
|
_dragAction = Dragging;
|
|
|
|
QTimer::singleShot(1, this, SLOT(onExecuteDrag()));
|
|
|
|
}
|
|
|
|
|
2016-05-26 15:31:20 +00:00
|
|
|
return state;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-05-26 15:31:20 +00:00
|
|
|
void FlatLabel::updateHover(const Text::StateResult &state) {
|
|
|
|
bool lnkChanged = ClickHandler::setActive(state.link, this);
|
2016-03-29 17:17:00 +00:00
|
|
|
|
2016-05-26 15:31:20 +00:00
|
|
|
if (!_selectable) {
|
|
|
|
refreshCursor(state.uponSymbol);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Qt::CursorShape cur = style::cur_default;
|
|
|
|
if (_dragAction == NoDrag) {
|
|
|
|
if (state.link) {
|
|
|
|
cur = style::cur_pointer;
|
|
|
|
} else if (state.uponSymbol) {
|
|
|
|
cur = style::cur_text;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (_dragAction == Selecting) {
|
|
|
|
uint16 second = state.symbol;
|
|
|
|
if (state.afterSymbol && _selectionType == TextSelectType::Letters) {
|
|
|
|
++second;
|
|
|
|
}
|
|
|
|
auto selection = _text.adjustSelection({ qMin(second, _dragSymbol), qMax(second, _dragSymbol) }, _selectionType);
|
|
|
|
if (_selection != selection) {
|
|
|
|
_selection = selection;
|
|
|
|
_savedSelection = { 0, 0 };
|
|
|
|
setFocus();
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
} else if (_dragAction == Dragging) {
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ClickHandler::getPressed()) {
|
|
|
|
cur = style::cur_pointer;
|
|
|
|
} else if (_dragAction == Selecting) {
|
|
|
|
cur = style::cur_text;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_dragAction == Selecting) {
|
2016-11-16 10:44:06 +00:00
|
|
|
// checkSelectingScroll();
|
2016-05-26 15:31:20 +00:00
|
|
|
} else {
|
2016-11-16 10:44:06 +00:00
|
|
|
// noSelectingScroll();
|
2016-05-26 15:31:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_dragAction == NoDrag && (lnkChanged || cur != _cursor)) {
|
|
|
|
setCursor(_cursor = cur);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FlatLabel::refreshCursor(bool uponSymbol) {
|
|
|
|
if (_dragAction != NoDrag) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
bool needTextCursor = _selectable && uponSymbol;
|
|
|
|
style::cursor newCursor = needTextCursor ? style::cur_text : style::cur_default;
|
|
|
|
if (ClickHandler::getActive()) {
|
|
|
|
newCursor = style::cur_pointer;
|
|
|
|
}
|
|
|
|
if (newCursor != _cursor) {
|
|
|
|
_cursor = newCursor;
|
|
|
|
setCursor(_cursor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Text::StateResult FlatLabel::getTextState(const QPoint &m) const {
|
|
|
|
Text::StateRequestElided request;
|
2016-04-13 18:29:32 +00:00
|
|
|
request.align = _st.align;
|
2016-05-26 15:31:20 +00:00
|
|
|
if (_selectable) {
|
|
|
|
request.flags |= Text::StateRequest::Flag::LookupSymbol;
|
|
|
|
}
|
|
|
|
int textWidth = width() - _st.margin.left() - _st.margin.right();
|
|
|
|
|
|
|
|
Text::StateResult state;
|
2016-06-01 16:40:51 +00:00
|
|
|
bool heightExceeded = _st.maxHeight && (_st.maxHeight < _fullTextHeight || textWidth < _text.maxWidth());
|
|
|
|
bool renderElided = _breakEverywhere || heightExceeded;
|
|
|
|
if (renderElided) {
|
2016-12-23 13:21:01 +00:00
|
|
|
auto lineHeight = qMax(_st.style.lineHeight, _st.style.font->height);
|
2016-06-01 16:40:51 +00:00
|
|
|
auto lines = _st.maxHeight ? qMax(_st.maxHeight / lineHeight, 1) : ((height() / lineHeight) + 2);
|
|
|
|
request.lines = lines;
|
|
|
|
if (_breakEverywhere) {
|
|
|
|
request.flags |= Text::StateRequest::Flag::BreakEverywhere;
|
|
|
|
}
|
2017-06-21 21:38:31 +00:00
|
|
|
state = _text.getStateElided(m - QPoint(_st.margin.left(), _st.margin.top()), textWidth, request);
|
2016-05-26 15:31:20 +00:00
|
|
|
} else {
|
2017-06-21 21:38:31 +00:00
|
|
|
state = _text.getState(m - QPoint(_st.margin.left(), _st.margin.top()), textWidth, request);
|
2016-05-26 15:31:20 +00:00
|
|
|
}
|
2016-03-29 17:17:00 +00:00
|
|
|
|
2016-05-26 15:31:20 +00:00
|
|
|
return state;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FlatLabel::setOpacity(float64 o) {
|
|
|
|
_opacity = o;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2018-12-27 09:37:11 +00:00
|
|
|
void FlatLabel::setTextColorOverride(std::optional<QColor> color) {
|
|
|
|
_textColorOverride = color;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
void FlatLabel::paintEvent(QPaintEvent *e) {
|
2016-05-26 15:31:20 +00:00
|
|
|
Painter p(this);
|
2014-05-30 08:53:19 +00:00
|
|
|
p.setOpacity(_opacity);
|
2018-12-27 09:37:11 +00:00
|
|
|
if (_textColorOverride) {
|
|
|
|
p.setPen(*_textColorOverride);
|
|
|
|
} else {
|
|
|
|
p.setPen(_st.textFg);
|
|
|
|
}
|
2016-12-23 13:21:01 +00:00
|
|
|
p.setTextPalette(_st.palette);
|
2016-05-26 15:31:20 +00:00
|
|
|
int textWidth = width() - _st.margin.left() - _st.margin.right();
|
|
|
|
auto selection = _selection.empty() ? (_contextMenu ? _savedSelection : _selection) : _selection;
|
2016-06-01 16:40:51 +00:00
|
|
|
bool heightExceeded = _st.maxHeight && (_st.maxHeight < _fullTextHeight || textWidth < _text.maxWidth());
|
|
|
|
bool renderElided = _breakEverywhere || heightExceeded;
|
|
|
|
if (renderElided) {
|
2016-12-23 13:21:01 +00:00
|
|
|
auto lineHeight = qMax(_st.style.lineHeight, _st.style.font->height);
|
2016-06-01 16:40:51 +00:00
|
|
|
auto lines = _st.maxHeight ? qMax(_st.maxHeight / lineHeight, 1) : ((height() / lineHeight) + 2);
|
|
|
|
_text.drawElided(p, _st.margin.left(), _st.margin.top(), textWidth, lines, _st.align, e->rect().y(), e->rect().bottom(), 0, _breakEverywhere, selection);
|
2016-05-26 15:31:20 +00:00
|
|
|
} else {
|
|
|
|
_text.draw(p, _st.margin.left(), _st.margin.top(), textWidth, _st.align, e->rect().y(), e->rect().bottom(), selection);
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2016-11-16 10:44:06 +00:00
|
|
|
|
2018-05-06 21:29:53 +00:00
|
|
|
int DividerLabel::naturalWidth() const {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DividerLabel::resizeEvent(QResizeEvent *e) {
|
|
|
|
_background->lower();
|
|
|
|
_background->setGeometry(rect());
|
|
|
|
return PaddingWrap::resizeEvent(e);
|
|
|
|
}
|
|
|
|
|
2016-11-16 10:44:06 +00:00
|
|
|
} // namespace Ui
|