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
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2017-08-31 16:28:58 +00:00
|
|
|
#include "base/flags.h"
|
|
|
|
|
2016-01-30 16:31:10 +00:00
|
|
|
namespace Fonts {
|
2017-04-30 16:03:12 +00:00
|
|
|
|
|
|
|
void Start();
|
|
|
|
QString GetOverride(const QString &familyName);
|
|
|
|
|
|
|
|
} // namespace
|
2016-01-30 16:31:10 +00:00
|
|
|
|
2017-12-26 12:41:48 +00:00
|
|
|
class TWidget;
|
|
|
|
|
2017-09-11 08:55:36 +00:00
|
|
|
template <typename Object>
|
|
|
|
class object_ptr;
|
|
|
|
|
|
|
|
namespace Ui {
|
|
|
|
|
|
|
|
inline bool InFocusChain(not_null<const QWidget*> widget) {
|
|
|
|
if (auto top = widget->window()) {
|
|
|
|
if (auto focused = top->focusWidget()) {
|
|
|
|
return !widget->isHidden()
|
|
|
|
&& (focused == widget
|
|
|
|
|| widget->isAncestorOf(focused));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ChildWidget>
|
|
|
|
inline ChildWidget *AttachParentChild(
|
|
|
|
not_null<QWidget*> parent,
|
|
|
|
const object_ptr<ChildWidget> &child) {
|
|
|
|
if (auto raw = child.data()) {
|
|
|
|
raw->setParent(parent);
|
|
|
|
raw->show();
|
|
|
|
return raw;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-12-26 12:41:48 +00:00
|
|
|
void SendPendingMoveResizeEvents(not_null<QWidget*> target);
|
|
|
|
|
|
|
|
QPixmap GrabWidget(
|
2018-04-06 16:23:09 +00:00
|
|
|
not_null<QWidget*> target,
|
2017-12-26 12:41:48 +00:00
|
|
|
QRect rect = QRect(),
|
|
|
|
QColor bg = QColor(255, 255, 255, 0));
|
|
|
|
QImage GrabWidgetToImage(
|
2018-04-06 16:23:09 +00:00
|
|
|
not_null<QWidget*> target,
|
2017-12-26 12:41:48 +00:00
|
|
|
QRect rect = QRect(),
|
|
|
|
QColor bg = QColor(255, 255, 255, 0));
|
|
|
|
|
2017-09-11 08:55:36 +00:00
|
|
|
} // namespace Ui
|
|
|
|
|
2017-05-24 12:07:58 +00:00
|
|
|
enum class RectPart {
|
2017-09-03 18:36:06 +00:00
|
|
|
None = 0,
|
2017-05-24 12:07:58 +00:00
|
|
|
|
2017-09-03 18:36:06 +00:00
|
|
|
TopLeft = (1 << 0),
|
|
|
|
Top = (1 << 1),
|
|
|
|
TopRight = (1 << 2),
|
|
|
|
Left = (1 << 3),
|
|
|
|
Center = (1 << 4),
|
|
|
|
Right = (1 << 5),
|
|
|
|
BottomLeft = (1 << 6),
|
|
|
|
Bottom = (1 << 7),
|
2017-05-24 12:07:58 +00:00
|
|
|
BottomRight = (1 << 8),
|
|
|
|
|
2017-09-03 18:36:06 +00:00
|
|
|
FullTop = TopLeft | Top | TopRight,
|
2017-05-24 12:07:58 +00:00
|
|
|
NoTopBottom = Left | Center | Right,
|
2017-09-03 18:36:06 +00:00
|
|
|
FullBottom = BottomLeft | Bottom | BottomRight,
|
|
|
|
NoTop = NoTopBottom | FullBottom,
|
|
|
|
NoBottom = FullTop | NoTopBottom,
|
2017-05-24 12:07:58 +00:00
|
|
|
|
2017-09-03 18:36:06 +00:00
|
|
|
FullLeft = TopLeft | Left | BottomLeft,
|
2017-05-24 12:07:58 +00:00
|
|
|
NoLeftRight = Top | Center | Bottom,
|
2017-09-03 18:36:06 +00:00
|
|
|
FullRight = TopRight | Right | BottomRight,
|
|
|
|
NoLeft = NoLeftRight | FullRight,
|
|
|
|
NoRight = FullLeft | NoLeftRight,
|
2017-05-24 12:07:58 +00:00
|
|
|
|
2017-12-18 17:50:25 +00:00
|
|
|
AllCorners = TopLeft | TopRight | BottomLeft | BottomRight,
|
|
|
|
AllSides = Top | Bottom | Left | Right,
|
2017-05-24 12:07:58 +00:00
|
|
|
|
2017-09-03 18:36:06 +00:00
|
|
|
Full = FullTop | NoTop,
|
2017-05-24 12:07:58 +00:00
|
|
|
};
|
2017-08-31 16:28:58 +00:00
|
|
|
using RectParts = base::flags<RectPart>;
|
|
|
|
inline constexpr auto is_flag_type(RectPart) { return true; };
|
2017-05-24 12:07:58 +00:00
|
|
|
|
|
|
|
inline bool IsTopCorner(RectPart corner) {
|
|
|
|
return (corner == RectPart::TopLeft) || (corner == RectPart::TopRight);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool IsBottomCorner(RectPart corner) {
|
|
|
|
return (corner == RectPart::BottomLeft) || (corner == RectPart::BottomRight);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool IsLeftCorner(RectPart corner) {
|
|
|
|
return (corner == RectPart::TopLeft) || (corner == RectPart::BottomLeft);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool IsRightCorner(RectPart corner) {
|
|
|
|
return (corner == RectPart::TopRight) || (corner == RectPart::BottomRight);
|
|
|
|
}
|
|
|
|
|
2015-04-02 10:33:19 +00:00
|
|
|
class Painter : public QPainter {
|
|
|
|
public:
|
|
|
|
explicit Painter(QPaintDevice *device) : QPainter(device) {
|
|
|
|
}
|
|
|
|
|
2015-04-11 10:04:10 +00:00
|
|
|
void drawTextLeft(int x, int y, int outerw, const QString &text, int textWidth = -1) {
|
2015-04-02 10:33:19 +00:00
|
|
|
QFontMetrics m(fontMetrics());
|
|
|
|
if (rtl() && textWidth < 0) textWidth = m.width(text);
|
2015-04-11 10:04:10 +00:00
|
|
|
drawText(rtl() ? (outerw - x - textWidth) : x, y + m.ascent(), text);
|
2015-04-02 10:33:19 +00:00
|
|
|
}
|
2015-04-11 10:04:10 +00:00
|
|
|
void drawTextRight(int x, int y, int outerw, const QString &text, int textWidth = -1) {
|
2015-04-02 10:33:19 +00:00
|
|
|
QFontMetrics m(fontMetrics());
|
|
|
|
if (!rtl() && textWidth < 0) textWidth = m.width(text);
|
2015-04-11 10:04:10 +00:00
|
|
|
drawText(rtl() ? x : (outerw - x - textWidth), y + m.ascent(), text);
|
|
|
|
}
|
|
|
|
void drawPixmapLeft(int x, int y, int outerw, const QPixmap &pix, const QRect &from) {
|
|
|
|
drawPixmap(QPoint(rtl() ? (outerw - x - (from.width() / pix.devicePixelRatio())) : x, y), pix, from);
|
|
|
|
}
|
2015-04-19 10:29:19 +00:00
|
|
|
void drawPixmapLeft(const QPoint &p, int outerw, const QPixmap &pix, const QRect &from) {
|
|
|
|
return drawPixmapLeft(p.x(), p.y(), outerw, pix, from);
|
|
|
|
}
|
2015-06-05 15:02:20 +00:00
|
|
|
void drawPixmapLeft(int x, int y, int w, int h, int outerw, const QPixmap &pix, const QRect &from) {
|
|
|
|
drawPixmap(QRect(rtl() ? (outerw - x - w) : x, y, w, h), pix, from);
|
|
|
|
}
|
|
|
|
void drawPixmapLeft(const QRect &r, int outerw, const QPixmap &pix, const QRect &from) {
|
|
|
|
return drawPixmapLeft(r.x(), r.y(), r.width(), r.height(), outerw, pix, from);
|
|
|
|
}
|
2015-05-08 14:44:07 +00:00
|
|
|
void drawPixmapLeft(int x, int y, int outerw, const QPixmap &pix) {
|
|
|
|
drawPixmap(QPoint(rtl() ? (outerw - x - (pix.width() / pix.devicePixelRatio())) : x, y), pix);
|
|
|
|
}
|
|
|
|
void drawPixmapLeft(const QPoint &p, int outerw, const QPixmap &pix) {
|
|
|
|
return drawPixmapLeft(p.x(), p.y(), outerw, pix);
|
|
|
|
}
|
2015-04-11 10:04:10 +00:00
|
|
|
void drawPixmapRight(int x, int y, int outerw, const QPixmap &pix, const QRect &from) {
|
|
|
|
drawPixmap(QPoint(rtl() ? x : (outerw - x - (from.width() / pix.devicePixelRatio())), y), pix, from);
|
|
|
|
}
|
2015-04-19 10:29:19 +00:00
|
|
|
void drawPixmapRight(const QPoint &p, int outerw, const QPixmap &pix, const QRect &from) {
|
|
|
|
return drawPixmapRight(p.x(), p.y(), outerw, pix, from);
|
|
|
|
}
|
2015-06-05 15:02:20 +00:00
|
|
|
void drawPixmapRight(int x, int y, int w, int h, int outerw, const QPixmap &pix, const QRect &from) {
|
|
|
|
drawPixmap(QRect(rtl() ? x : (outerw - x - w), y, w, h), pix, from);
|
|
|
|
}
|
|
|
|
void drawPixmapRight(const QRect &r, int outerw, const QPixmap &pix, const QRect &from) {
|
|
|
|
return drawPixmapRight(r.x(), r.y(), r.width(), r.height(), outerw, pix, from);
|
|
|
|
}
|
2015-05-08 14:44:07 +00:00
|
|
|
void drawPixmapRight(int x, int y, int outerw, const QPixmap &pix) {
|
|
|
|
drawPixmap(QPoint(rtl() ? x : (outerw - x - (pix.width() / pix.devicePixelRatio())), y), pix);
|
|
|
|
}
|
|
|
|
void drawPixmapRight(const QPoint &p, int outerw, const QPixmap &pix) {
|
|
|
|
return drawPixmapRight(p.x(), p.y(), outerw, pix);
|
|
|
|
}
|
2016-12-03 12:10:35 +00:00
|
|
|
|
2016-12-23 13:21:01 +00:00
|
|
|
void setTextPalette(const style::TextPalette &palette) {
|
|
|
|
_textPalette = &palette;
|
|
|
|
}
|
|
|
|
void restoreTextPalette() {
|
|
|
|
_textPalette = nullptr;
|
|
|
|
}
|
|
|
|
const style::TextPalette &textPalette() const {
|
|
|
|
return _textPalette ? *_textPalette : st::defaultTextPalette;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const style::TextPalette *_textPalette = nullptr;
|
|
|
|
|
2016-12-03 12:10:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class PainterHighQualityEnabler {
|
|
|
|
public:
|
|
|
|
PainterHighQualityEnabler(Painter &p) : _painter(p) {
|
2016-12-04 19:40:41 +00:00
|
|
|
static constexpr QPainter::RenderHint Hints[] = {
|
|
|
|
QPainter::Antialiasing,
|
|
|
|
QPainter::SmoothPixmapTransform,
|
|
|
|
QPainter::TextAntialiasing,
|
|
|
|
QPainter::HighQualityAntialiasing
|
|
|
|
};
|
|
|
|
|
2016-12-03 12:10:35 +00:00
|
|
|
auto hints = _painter.renderHints();
|
|
|
|
for_const (auto hint, Hints) {
|
|
|
|
if (!(hints & hint)) {
|
|
|
|
_hints |= hint;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_hints) {
|
|
|
|
_painter.setRenderHints(_hints);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PainterHighQualityEnabler(const PainterHighQualityEnabler &other) = delete;
|
|
|
|
PainterHighQualityEnabler &operator=(const PainterHighQualityEnabler &other) = delete;
|
|
|
|
~PainterHighQualityEnabler() {
|
|
|
|
if (_hints) {
|
|
|
|
_painter.setRenderHints(_hints, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Painter &_painter;
|
|
|
|
QPainter::RenderHints _hints = 0;
|
|
|
|
|
2015-04-02 10:33:19 +00:00
|
|
|
};
|
|
|
|
|
2017-02-11 11:24:37 +00:00
|
|
|
template <typename Base>
|
|
|
|
class TWidgetHelper : public Base {
|
|
|
|
public:
|
|
|
|
using Base::Base;
|
|
|
|
|
|
|
|
virtual QMargins getMargins() const {
|
|
|
|
return QMargins();
|
|
|
|
}
|
|
|
|
|
|
|
|
void moveToLeft(int x, int y, int outerw = 0) {
|
|
|
|
auto margins = getMargins();
|
|
|
|
x -= margins.left();
|
|
|
|
y -= margins.top();
|
2017-02-12 20:41:29 +00:00
|
|
|
Base::move(rtl() ? ((outerw > 0 ? outerw : Base::parentWidget()->width()) - x - Base::width()) : x, y);
|
2017-02-11 11:24:37 +00:00
|
|
|
}
|
|
|
|
void moveToRight(int x, int y, int outerw = 0) {
|
|
|
|
auto margins = getMargins();
|
|
|
|
x -= margins.right();
|
|
|
|
y -= margins.top();
|
2017-02-12 20:41:29 +00:00
|
|
|
Base::move(rtl() ? x : ((outerw > 0 ? outerw : Base::parentWidget()->width()) - x - Base::width()), y);
|
2017-02-11 11:24:37 +00:00
|
|
|
}
|
|
|
|
void setGeometryToLeft(int x, int y, int w, int h, int outerw = 0) {
|
|
|
|
auto margins = getMargins();
|
|
|
|
x -= margins.left();
|
|
|
|
y -= margins.top();
|
|
|
|
w -= margins.left() - margins.right();
|
|
|
|
h -= margins.top() - margins.bottom();
|
2017-02-12 20:41:29 +00:00
|
|
|
Base::setGeometry(rtl() ? ((outerw > 0 ? outerw : Base::parentWidget()->width()) - x - w) : x, y, w, h);
|
2017-02-11 11:24:37 +00:00
|
|
|
}
|
|
|
|
void setGeometryToRight(int x, int y, int w, int h, int outerw = 0) {
|
|
|
|
auto margins = getMargins();
|
|
|
|
x -= margins.right();
|
|
|
|
y -= margins.top();
|
|
|
|
w -= margins.left() - margins.right();
|
|
|
|
h -= margins.top() - margins.bottom();
|
2017-02-12 20:41:29 +00:00
|
|
|
Base::setGeometry(rtl() ? x : ((outerw > 0 ? outerw : Base::parentWidget()->width()) - x - w), y, w, h);
|
2017-02-11 11:24:37 +00:00
|
|
|
}
|
|
|
|
QPoint myrtlpoint(int x, int y) const {
|
2017-02-12 20:41:29 +00:00
|
|
|
return rtlpoint(x, y, Base::width());
|
2017-02-11 11:24:37 +00:00
|
|
|
}
|
2017-05-22 15:25:49 +00:00
|
|
|
QPoint myrtlpoint(const QPoint point) const {
|
|
|
|
return rtlpoint(point, Base::width());
|
2017-02-11 11:24:37 +00:00
|
|
|
}
|
|
|
|
QRect myrtlrect(int x, int y, int w, int h) const {
|
2017-02-12 20:41:29 +00:00
|
|
|
return rtlrect(x, y, w, h, Base::width());
|
2017-02-11 11:24:37 +00:00
|
|
|
}
|
2017-05-22 15:25:49 +00:00
|
|
|
QRect myrtlrect(const QRect &rect) const {
|
|
|
|
return rtlrect(rect, Base::width());
|
2017-02-11 11:24:37 +00:00
|
|
|
}
|
2017-05-22 15:25:49 +00:00
|
|
|
void rtlupdate(const QRect &rect) {
|
|
|
|
Base::update(myrtlrect(rect));
|
2017-02-11 11:24:37 +00:00
|
|
|
}
|
|
|
|
void rtlupdate(int x, int y, int w, int h) {
|
2017-02-12 20:41:29 +00:00
|
|
|
Base::update(myrtlrect(x, y, w, h));
|
2017-02-11 11:24:37 +00:00
|
|
|
}
|
|
|
|
|
2017-05-22 15:25:49 +00:00
|
|
|
QPoint mapFromGlobal(const QPoint &point) const {
|
|
|
|
return Base::mapFromGlobal(point);
|
|
|
|
}
|
|
|
|
QPoint mapToGlobal(const QPoint &point) const {
|
|
|
|
return Base::mapToGlobal(point);
|
|
|
|
}
|
|
|
|
QRect mapFromGlobal(const QRect &rect) const {
|
|
|
|
return QRect(mapFromGlobal(rect.topLeft()), rect.size());
|
|
|
|
}
|
2017-09-13 16:57:44 +00:00
|
|
|
QRect mapToGlobal(const QRect &rect) const {
|
2017-05-22 15:25:49 +00:00
|
|
|
return QRect(mapToGlobal(rect.topLeft()), rect.size());
|
|
|
|
}
|
|
|
|
|
2017-02-11 11:24:37 +00:00
|
|
|
protected:
|
|
|
|
void enterEvent(QEvent *e) final override {
|
|
|
|
if (auto parent = tparent()) {
|
|
|
|
parent->leaveToChildEvent(e, this);
|
|
|
|
}
|
|
|
|
return enterEventHook(e);
|
|
|
|
}
|
|
|
|
virtual void enterEventHook(QEvent *e) {
|
|
|
|
return Base::enterEvent(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
void leaveEvent(QEvent *e) final override {
|
|
|
|
if (auto parent = tparent()) {
|
|
|
|
parent->enterFromChildEvent(e, this);
|
|
|
|
}
|
|
|
|
return leaveEventHook(e);
|
|
|
|
}
|
|
|
|
virtual void leaveEventHook(QEvent *e) {
|
|
|
|
return Base::leaveEvent(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
// e - from enterEvent() of child TWidget
|
|
|
|
virtual void leaveToChildEvent(QEvent *e, QWidget *child) {
|
|
|
|
}
|
|
|
|
|
|
|
|
// e - from leaveEvent() of child TWidget
|
|
|
|
virtual void enterFromChildEvent(QEvent *e, QWidget *child) {
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
TWidget *tparent() {
|
2017-02-12 20:41:29 +00:00
|
|
|
return qobject_cast<TWidget*>(Base::parentWidget());
|
2017-02-11 11:24:37 +00:00
|
|
|
}
|
|
|
|
const TWidget *tparent() const {
|
2017-02-12 20:41:29 +00:00
|
|
|
return qobject_cast<const TWidget*>(Base::parentWidget());
|
2017-02-11 11:24:37 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 23:17:06 +00:00
|
|
|
template <typename OtherBase>
|
|
|
|
friend class TWidgetHelper;
|
|
|
|
|
2017-02-11 11:24:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class TWidget : public TWidgetHelper<QWidget> {
|
2014-05-30 08:53:19 +00:00
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2017-02-11 11:24:37 +00:00
|
|
|
TWidget(QWidget *parent = nullptr) : TWidgetHelper<QWidget>(parent) {
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2014-09-29 02:47:30 +00:00
|
|
|
|
2017-09-11 08:55:36 +00:00
|
|
|
bool inFocusChain() const {
|
|
|
|
return Ui::InFocusChain(this);
|
|
|
|
}
|
2016-02-27 19:39:51 +00:00
|
|
|
|
2016-05-14 16:57:06 +00:00
|
|
|
void hideChildren() {
|
|
|
|
for (auto child : children()) {
|
2017-09-11 08:55:36 +00:00
|
|
|
if (child->isWidgetType()) {
|
|
|
|
static_cast<QWidget*>(child)->hide();
|
2016-05-14 16:57:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void showChildren() {
|
|
|
|
for (auto child : children()) {
|
2017-09-11 08:55:36 +00:00
|
|
|
if (child->isWidgetType()) {
|
|
|
|
static_cast<QWidget*>(child)->show();
|
2016-05-14 16:57:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-22 17:16:21 +00:00
|
|
|
// Get the size of the widget as it should be.
|
|
|
|
// Negative return value means no default width.
|
|
|
|
virtual int naturalWidth() const {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Count new height for width=newWidth and resize to it.
|
|
|
|
void resizeToWidth(int newWidth) {
|
2016-12-02 19:16:35 +00:00
|
|
|
auto margins = getMargins();
|
|
|
|
auto fullWidth = margins.left() + newWidth + margins.right();
|
|
|
|
auto fullHeight = margins.top() + resizeGetHeight(newWidth) + margins.bottom();
|
|
|
|
auto newSize = QSize(fullWidth, fullHeight);
|
2016-08-22 17:16:21 +00:00
|
|
|
if (newSize != size()) {
|
|
|
|
resize(newSize);
|
|
|
|
update();
|
|
|
|
}
|
2016-06-17 18:18:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-16 15:15:07 +00:00
|
|
|
// Resize to minimum of natural width and available width.
|
|
|
|
void resizeToNaturalWidth(int newWidth) {
|
|
|
|
auto maxWidth = naturalWidth();
|
|
|
|
resizeToWidth((maxWidth >= 0) ? qMin(newWidth, maxWidth) : newWidth);
|
|
|
|
}
|
|
|
|
|
2016-12-02 19:16:35 +00:00
|
|
|
QRect rectNoMargins() const {
|
|
|
|
return rect().marginsRemoved(getMargins());
|
|
|
|
}
|
|
|
|
|
|
|
|
int widthNoMargins() const {
|
|
|
|
return rectNoMargins().width();
|
|
|
|
}
|
|
|
|
|
|
|
|
int heightNoMargins() const {
|
|
|
|
return rectNoMargins().height();
|
|
|
|
}
|
|
|
|
|
|
|
|
int bottomNoMargins() const {
|
|
|
|
auto rectWithoutMargins = rectNoMargins();
|
|
|
|
return y() + rectWithoutMargins.y() + rectWithoutMargins.height();
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize sizeNoMargins() const {
|
|
|
|
return rectNoMargins().size();
|
|
|
|
}
|
|
|
|
|
2016-10-26 16:43:13 +00:00
|
|
|
// Updates the area that is visible inside the scroll container.
|
2017-09-13 16:57:44 +00:00
|
|
|
void setVisibleTopBottom(int visibleTop, int visibleBottom) {
|
|
|
|
auto max = height();
|
|
|
|
visibleTopBottomUpdated(
|
|
|
|
snap(visibleTop, 0, max),
|
|
|
|
snap(visibleBottom, 0, max));
|
2016-10-26 16:43:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
signals:
|
|
|
|
// Child widget is responsible for emitting this signal.
|
|
|
|
void heightUpdated();
|
|
|
|
|
2016-06-15 17:13:46 +00:00
|
|
|
protected:
|
2017-09-13 16:57:44 +00:00
|
|
|
void setChildVisibleTopBottom(
|
|
|
|
TWidget *child,
|
|
|
|
int visibleTop,
|
|
|
|
int visibleBottom) {
|
|
|
|
if (child) {
|
|
|
|
auto top = child->y();
|
|
|
|
child->setVisibleTopBottom(
|
|
|
|
visibleTop - top,
|
|
|
|
visibleBottom - top);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-22 17:16:21 +00:00
|
|
|
// Resizes content and counts natural widget height for the desired width.
|
|
|
|
virtual int resizeGetHeight(int newWidth) {
|
2017-09-21 19:21:33 +00:00
|
|
|
return heightNoMargins();
|
2016-08-22 17:16:21 +00:00
|
|
|
}
|
|
|
|
|
2017-09-13 16:57:44 +00:00
|
|
|
virtual void visibleTopBottomUpdated(
|
|
|
|
int visibleTop,
|
|
|
|
int visibleBottom) {
|
|
|
|
}
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
};
|
2014-06-14 19:32:11 +00:00
|
|
|
|
2016-11-28 15:45:07 +00:00
|
|
|
template <typename Widget>
|
2017-11-30 18:04:13 +00:00
|
|
|
QPointer<Widget> make_weak(Widget *object) {
|
2016-12-23 13:21:01 +00:00
|
|
|
return QPointer<Widget>(object);
|
|
|
|
}
|
2016-11-28 15:45:07 +00:00
|
|
|
|
2016-12-23 13:21:01 +00:00
|
|
|
template <typename Widget>
|
2017-11-30 18:04:13 +00:00
|
|
|
QPointer<const Widget> make_weak(const Widget *object) {
|
2016-12-23 13:21:01 +00:00
|
|
|
return QPointer<const Widget>(object);
|
|
|
|
}
|
2016-11-28 15:45:07 +00:00
|
|
|
|
2018-01-28 15:08:34 +00:00
|
|
|
template <typename Widget>
|
|
|
|
QPointer<Widget> make_weak(not_null<Widget*> object) {
|
|
|
|
return QPointer<Widget>(object.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Widget>
|
|
|
|
QPointer<const Widget> make_weak(not_null<const Widget*> object) {
|
|
|
|
return QPointer<const Widget>(object.get());
|
|
|
|
}
|
|
|
|
|
2017-04-06 16:49:42 +00:00
|
|
|
class SingleQueuedInvokation : public QObject {
|
2016-03-10 10:15:21 +00:00
|
|
|
public:
|
2017-04-06 16:49:42 +00:00
|
|
|
SingleQueuedInvokation(base::lambda<void()> callback) : _callback(callback) {
|
2016-03-10 10:15:21 +00:00
|
|
|
}
|
|
|
|
void call() {
|
2017-04-06 16:49:42 +00:00
|
|
|
if (_pending.testAndSetAcquire(0, 1)) {
|
|
|
|
InvokeQueued(this, [this] {
|
|
|
|
if (_pending.testAndSetRelease(1, 0)) {
|
|
|
|
_callback();
|
|
|
|
}
|
|
|
|
});
|
2016-08-18 19:27:43 +00:00
|
|
|
}
|
2016-03-10 10:15:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-04-06 16:49:42 +00:00
|
|
|
base::lambda<void()> _callback;
|
2016-07-05 17:44:02 +00:00
|
|
|
QAtomicInt _pending = { 0 };
|
2016-03-10 10:15:21 +00:00
|
|
|
|
|
|
|
};
|
2016-04-12 21:31:28 +00:00
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
// Smart pointer for QObject*, has move semantics, destroys object if it doesn't have a parent.
|
|
|
|
template <typename Object>
|
|
|
|
class object_ptr {
|
2016-04-12 21:31:28 +00:00
|
|
|
public:
|
2017-11-27 11:43:57 +00:00
|
|
|
object_ptr(std::nullptr_t) noexcept {
|
2016-04-12 21:31:28 +00:00
|
|
|
}
|
2016-04-14 13:03:03 +00:00
|
|
|
|
2016-04-12 21:31:28 +00:00
|
|
|
// No default constructor, but constructors with at least
|
|
|
|
// one argument are simply make functions.
|
|
|
|
template <typename Parent, typename... Args>
|
2017-11-27 11:43:57 +00:00
|
|
|
explicit object_ptr(Parent &&parent, Args&&... args)
|
|
|
|
: _object(new Object(std::forward<Parent>(parent), std::forward<Args>(args)...)) {
|
|
|
|
}
|
|
|
|
static object_ptr<Object> fromRaw(Object *value) noexcept {
|
|
|
|
object_ptr<Object> result = { nullptr };
|
|
|
|
result._object = value;
|
|
|
|
return result;
|
2016-04-12 21:31:28 +00:00
|
|
|
}
|
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
object_ptr(const object_ptr &other) = delete;
|
|
|
|
object_ptr &operator=(const object_ptr &other) = delete;
|
2017-11-27 11:43:57 +00:00
|
|
|
object_ptr(object_ptr &&other) noexcept : _object(base::take(other._object)) {
|
2016-12-13 17:07:56 +00:00
|
|
|
}
|
2017-11-27 11:43:57 +00:00
|
|
|
object_ptr &operator=(object_ptr &&other) noexcept {
|
2017-02-21 13:45:56 +00:00
|
|
|
auto temp = std::move(other);
|
2016-12-13 17:07:56 +00:00
|
|
|
destroy();
|
2017-02-21 13:45:56 +00:00
|
|
|
std::swap(_object, temp._object);
|
2016-12-13 17:07:56 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2016-04-12 21:31:28 +00:00
|
|
|
|
2017-11-27 11:43:57 +00:00
|
|
|
template <
|
|
|
|
typename OtherObject,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
std::is_base_of_v<Object, OtherObject>>>
|
|
|
|
object_ptr(object_ptr<OtherObject> &&other) noexcept
|
|
|
|
: _object(base::take(other._object)) {
|
2016-12-13 17:07:56 +00:00
|
|
|
}
|
|
|
|
|
2017-11-27 11:43:57 +00:00
|
|
|
template <
|
|
|
|
typename OtherObject,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
std::is_base_of_v<Object, OtherObject>>>
|
|
|
|
object_ptr &operator=(object_ptr<OtherObject> &&other) noexcept {
|
2016-12-13 17:07:56 +00:00
|
|
|
_object = base::take(other._object);
|
2016-04-12 21:31:28 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2016-12-13 17:07:56 +00:00
|
|
|
|
2017-11-27 11:43:57 +00:00
|
|
|
object_ptr &operator=(std::nullptr_t) noexcept {
|
2016-12-13 17:07:56 +00:00
|
|
|
_object = nullptr;
|
2016-04-12 21:31:28 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
// So we can pass this pointer to methods like connect().
|
2017-11-27 11:43:57 +00:00
|
|
|
Object *data() const noexcept {
|
2017-07-22 08:35:18 +00:00
|
|
|
return static_cast<Object*>(_object.data());
|
2016-04-12 21:31:28 +00:00
|
|
|
}
|
2017-11-27 11:43:57 +00:00
|
|
|
operator Object*() const noexcept {
|
2016-12-13 17:07:56 +00:00
|
|
|
return data();
|
2016-04-12 21:31:28 +00:00
|
|
|
}
|
|
|
|
|
2017-11-27 11:43:57 +00:00
|
|
|
explicit operator bool() const noexcept {
|
2016-12-13 17:07:56 +00:00
|
|
|
return _object != nullptr;
|
2016-04-12 21:31:28 +00:00
|
|
|
}
|
2016-12-13 17:07:56 +00:00
|
|
|
|
2017-11-27 11:43:57 +00:00
|
|
|
Object *operator->() const noexcept {
|
2016-12-13 17:07:56 +00:00
|
|
|
return data();
|
|
|
|
}
|
2017-11-27 11:43:57 +00:00
|
|
|
Object &operator*() const noexcept {
|
2016-12-13 17:07:56 +00:00
|
|
|
return *data();
|
2016-08-28 20:23:32 +00:00
|
|
|
}
|
2016-04-12 21:31:28 +00:00
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
// Use that instead "= new Object(parent, ...)"
|
2016-09-21 11:44:20 +00:00
|
|
|
template <typename Parent, typename... Args>
|
2017-09-30 18:26:45 +00:00
|
|
|
Object *create(Parent &&parent, Args&&... args) {
|
2016-12-13 17:07:56 +00:00
|
|
|
destroy();
|
2017-11-27 11:43:57 +00:00
|
|
|
_object = new Object(
|
|
|
|
std::forward<Parent>(parent),
|
|
|
|
std::forward<Args>(args)...);
|
2017-09-30 18:26:45 +00:00
|
|
|
return data();
|
2016-09-21 11:44:20 +00:00
|
|
|
}
|
2017-11-27 11:43:57 +00:00
|
|
|
void destroy() noexcept {
|
2016-11-04 11:14:47 +00:00
|
|
|
delete base::take(_object);
|
2016-05-31 19:27:11 +00:00
|
|
|
}
|
|
|
|
void destroyDelayed() {
|
2016-11-04 11:14:47 +00:00
|
|
|
if (_object) {
|
2016-12-13 17:07:56 +00:00
|
|
|
if (auto widget = base::up_cast<QWidget*>(data())) {
|
2016-11-04 11:14:47 +00:00
|
|
|
widget->hide();
|
|
|
|
}
|
2016-12-13 17:07:56 +00:00
|
|
|
base::take(_object)->deleteLater();
|
2016-05-31 19:27:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-27 11:43:57 +00:00
|
|
|
~object_ptr() noexcept {
|
2016-12-13 17:07:56 +00:00
|
|
|
if (auto pointer = _object) {
|
|
|
|
if (!pointer->parent()) {
|
|
|
|
destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ResultType, typename SourceType>
|
2017-11-27 11:43:57 +00:00
|
|
|
friend object_ptr<ResultType> static_object_cast(
|
|
|
|
object_ptr<SourceType> source);
|
2016-12-13 17:07:56 +00:00
|
|
|
|
2016-04-12 21:31:28 +00:00
|
|
|
private:
|
2016-12-13 17:07:56 +00:00
|
|
|
template <typename OtherObject>
|
|
|
|
friend class object_ptr;
|
|
|
|
|
2017-07-22 08:35:18 +00:00
|
|
|
QPointer<QObject> _object;
|
2016-04-12 21:31:28 +00:00
|
|
|
|
|
|
|
};
|
2016-06-15 17:13:46 +00:00
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
template <typename ResultType, typename SourceType>
|
2017-11-27 11:43:57 +00:00
|
|
|
inline object_ptr<ResultType> static_object_cast(
|
|
|
|
object_ptr<SourceType> source) {
|
2016-12-13 17:07:56 +00:00
|
|
|
auto result = object_ptr<ResultType>(nullptr);
|
2017-11-27 11:43:57 +00:00
|
|
|
result._object = static_cast<ResultType*>(
|
|
|
|
base::take(source._object).data());
|
2017-02-21 13:45:56 +00:00
|
|
|
return std::move(result);
|
2016-12-13 17:07:56 +00:00
|
|
|
}
|
2016-11-04 11:14:47 +00:00
|
|
|
|
2017-11-27 11:43:57 +00:00
|
|
|
void sendSynteticMouseEvent(
|
|
|
|
QWidget *widget,
|
|
|
|
QEvent::Type type,
|
|
|
|
Qt::MouseButton button,
|
|
|
|
const QPoint &globalPoint);
|
2016-06-15 17:13:46 +00:00
|
|
|
|
2017-11-27 11:43:57 +00:00
|
|
|
inline void sendSynteticMouseEvent(
|
|
|
|
QWidget *widget,
|
|
|
|
QEvent::Type type,
|
|
|
|
Qt::MouseButton button) {
|
2016-06-15 17:13:46 +00:00
|
|
|
return sendSynteticMouseEvent(widget, type, button, QCursor::pos());
|
|
|
|
}
|