2014-05-30 12:53:19 +04:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 13:23:14 +03:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2014-05-30 12:53:19 +04:00
|
|
|
|
2018-01-03 13:23:14 +03:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2014-05-30 12:53:19 +04:00
|
|
|
*/
|
2017-06-21 13:49:10 +03:00
|
|
|
#include "twidget.h"
|
|
|
|
|
2016-04-13 00:31:28 +03:00
|
|
|
#include "mainwindow.h"
|
2019-01-21 17:42:21 +04:00
|
|
|
#include "core/application.h"
|
2019-06-03 17:41:23 +03:00
|
|
|
#include "platform/platform_info.h"
|
2014-06-14 23:32:11 +04:00
|
|
|
|
2019-09-04 10:19:15 +03:00
|
|
|
#include <QtGui/QWindow>
|
|
|
|
#include <QtGui/QGuiApplication>
|
2017-03-16 12:53:13 +03:00
|
|
|
|
2017-12-26 15:41:48 +03:00
|
|
|
namespace Ui {
|
2014-06-14 23:32:11 +04:00
|
|
|
namespace {
|
2017-03-16 12:53:13 +03:00
|
|
|
|
2017-09-11 11:55:36 +03:00
|
|
|
class WidgetCreator : public QWidget {
|
|
|
|
public:
|
|
|
|
static void Create(not_null<QWidget*> widget) {
|
|
|
|
volatile auto unknown = widget.get();
|
|
|
|
static_cast<WidgetCreator*>(unknown)->create();
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
void CreateWidgetStateRecursive(not_null<QWidget*> target) {
|
|
|
|
if (!target->testAttribute(Qt::WA_WState_Created)) {
|
|
|
|
if (!target->isWindow()) {
|
|
|
|
CreateWidgetStateRecursive(target->parentWidget());
|
2017-12-01 20:55:16 +04:00
|
|
|
WidgetCreator::Create(target);
|
2019-06-03 17:41:23 +03:00
|
|
|
} else if (!Platform::IsMac() || Platform::IsMac10_7OrGreater()) {
|
2017-12-01 20:55:16 +04:00
|
|
|
WidgetCreator::Create(target);
|
2015-04-02 13:33:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-24 17:05:15 +04:00
|
|
|
void SendPendingEventsRecursive(QWidget *target, bool parentHiddenFlag) {
|
2017-09-11 11:55:36 +03:00
|
|
|
auto wasVisible = target->isVisible();
|
|
|
|
if (!wasVisible) {
|
|
|
|
target->setAttribute(Qt::WA_WState_Visible, true);
|
|
|
|
}
|
|
|
|
if (target->testAttribute(Qt::WA_PendingMoveEvent)) {
|
|
|
|
target->setAttribute(Qt::WA_PendingMoveEvent, false);
|
|
|
|
QMoveEvent e(target->pos(), QPoint());
|
2019-09-04 10:19:15 +03:00
|
|
|
QCoreApplication::sendEvent(target, &e);
|
2017-09-11 11:55:36 +03:00
|
|
|
}
|
|
|
|
if (target->testAttribute(Qt::WA_PendingResizeEvent)) {
|
|
|
|
target->setAttribute(Qt::WA_PendingResizeEvent, false);
|
|
|
|
QResizeEvent e(target->size(), QSize());
|
2019-09-04 10:19:15 +03:00
|
|
|
QCoreApplication::sendEvent(target, &e);
|
2017-09-11 11:55:36 +03:00
|
|
|
}
|
2017-11-24 17:05:15 +04:00
|
|
|
|
|
|
|
auto removeVisibleFlag = [&] {
|
|
|
|
return parentHiddenFlag
|
|
|
|
|| target->testAttribute(Qt::WA_WState_Hidden);
|
|
|
|
};
|
|
|
|
|
2017-09-11 11:55:36 +03:00
|
|
|
auto &children = target->children();
|
|
|
|
for (auto i = 0; i < children.size(); ++i) {
|
|
|
|
auto child = children[i];
|
|
|
|
if (child->isWidgetType()) {
|
|
|
|
auto widget = static_cast<QWidget*>(child);
|
|
|
|
if (!widget->isWindow()) {
|
|
|
|
if (!widget->testAttribute(Qt::WA_WState_Created)) {
|
|
|
|
WidgetCreator::Create(widget);
|
|
|
|
}
|
2017-11-24 17:05:15 +04:00
|
|
|
SendPendingEventsRecursive(widget, removeVisibleFlag());
|
2017-09-11 11:55:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-24 17:05:15 +04:00
|
|
|
|
|
|
|
if (removeVisibleFlag()) {
|
2017-09-11 11:55:36 +03:00
|
|
|
target->setAttribute(Qt::WA_WState_Visible, false);
|
|
|
|
}
|
2016-02-27 22:39:51 +03:00
|
|
|
}
|
|
|
|
|
2017-09-11 11:55:36 +03:00
|
|
|
} // namespace
|
|
|
|
|
2017-12-26 15:41:48 +03:00
|
|
|
void SendPendingMoveResizeEvents(not_null<QWidget*> target) {
|
2017-09-11 11:55:36 +03:00
|
|
|
CreateWidgetStateRecursive(target);
|
2017-11-24 17:05:15 +04:00
|
|
|
SendPendingEventsRecursive(target, !target->isVisible());
|
2015-07-17 22:17:37 +03:00
|
|
|
}
|
|
|
|
|
2019-04-26 13:43:57 +04:00
|
|
|
void MarkDirtyOpaqueChildrenRecursive(not_null<QWidget*> target) {
|
|
|
|
target->resize(target->size()); // Calls setDirtyOpaqueRegion().
|
|
|
|
for (const auto child : target->children()) {
|
|
|
|
if (const auto widget = qobject_cast<QWidget*>(child)) {
|
|
|
|
MarkDirtyOpaqueChildrenRecursive(widget);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-06 20:23:09 +04:00
|
|
|
QPixmap GrabWidget(not_null<QWidget*> target, QRect rect, QColor bg) {
|
2017-12-26 15:41:48 +03:00
|
|
|
SendPendingMoveResizeEvents(target);
|
|
|
|
if (rect.isNull()) {
|
|
|
|
rect = target->rect();
|
|
|
|
}
|
2015-10-17 16:52:26 +02:00
|
|
|
|
2017-12-26 15:41:48 +03:00
|
|
|
auto result = QPixmap(rect.size() * cIntRetinaFactor());
|
|
|
|
result.setDevicePixelRatio(cRetinaFactor());
|
2016-12-13 20:07:56 +03:00
|
|
|
if (!target->testAttribute(Qt::WA_OpaquePaintEvent)) {
|
|
|
|
result.fill(bg);
|
|
|
|
}
|
2019-04-26 13:43:57 +04:00
|
|
|
{
|
|
|
|
QPainter p(&result);
|
|
|
|
RenderWidget(p, target, QPoint(), rect);
|
|
|
|
}
|
2017-02-21 17:37:53 +03:00
|
|
|
return result;
|
2016-11-24 22:28:23 +03:00
|
|
|
}
|
|
|
|
|
2018-04-06 20:23:09 +04:00
|
|
|
QImage GrabWidgetToImage(not_null<QWidget*> target, QRect rect, QColor bg) {
|
2019-04-26 13:43:57 +04:00
|
|
|
SendPendingMoveResizeEvents(target);
|
2017-12-26 15:41:48 +03:00
|
|
|
if (rect.isNull()) {
|
|
|
|
rect = target->rect();
|
|
|
|
}
|
2016-11-24 22:28:23 +03:00
|
|
|
|
2017-12-26 15:41:48 +03:00
|
|
|
auto result = QImage(
|
|
|
|
rect.size() * cIntRetinaFactor(),
|
|
|
|
QImage::Format_ARGB32_Premultiplied);
|
2016-11-24 22:28:23 +03:00
|
|
|
result.setDevicePixelRatio(cRetinaFactor());
|
|
|
|
if (!target->testAttribute(Qt::WA_OpaquePaintEvent)) {
|
2016-12-13 20:07:56 +03:00
|
|
|
result.fill(bg);
|
2016-11-24 22:28:23 +03:00
|
|
|
}
|
2019-04-26 13:43:57 +04:00
|
|
|
{
|
|
|
|
QPainter p(&result);
|
|
|
|
RenderWidget(p, target, QPoint(), rect);
|
|
|
|
}
|
2017-02-21 17:37:53 +03:00
|
|
|
return result;
|
2014-06-14 23:32:11 +04:00
|
|
|
}
|
2016-05-19 15:03:51 +03:00
|
|
|
|
2019-04-26 13:43:57 +04:00
|
|
|
void RenderWidget(
|
|
|
|
QPainter &painter,
|
|
|
|
not_null<QWidget*> source,
|
|
|
|
const QPoint &targetOffset,
|
|
|
|
const QRegion &sourceRegion,
|
|
|
|
QWidget::RenderFlags renderFlags) {
|
|
|
|
const auto visible = source->isVisible();
|
|
|
|
source->render(&painter, targetOffset, sourceRegion, renderFlags);
|
|
|
|
if (!visible) {
|
|
|
|
MarkDirtyOpaqueChildrenRecursive(source);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-26 22:33:30 +01:00
|
|
|
void ForceFullRepaint(not_null<QWidget*> widget) {
|
|
|
|
const auto refresher = std::make_unique<QWidget>(widget);
|
|
|
|
refresher->setGeometry(widget->rect());
|
|
|
|
refresher->show();
|
|
|
|
}
|
|
|
|
|
2018-12-12 12:59:51 +04:00
|
|
|
void PostponeCall(FnMut<void()> &&callable) {
|
2019-01-18 15:26:43 +04:00
|
|
|
Core::App().postponeCall(std::move(callable));
|
2018-12-12 12:59:51 +04:00
|
|
|
}
|
|
|
|
|
2017-12-26 15:41:48 +03:00
|
|
|
} // namespace Ui
|
|
|
|
|
2016-06-15 20:13:46 +03:00
|
|
|
void sendSynteticMouseEvent(QWidget *widget, QEvent::Type type, Qt::MouseButton button, const QPoint &globalPoint) {
|
2016-08-27 11:52:05 -06:00
|
|
|
if (auto windowHandle = widget->window()->windowHandle()) {
|
|
|
|
auto localPoint = windowHandle->mapFromGlobal(globalPoint);
|
2016-08-31 16:06:12 -04:00
|
|
|
QMouseEvent ev(type
|
|
|
|
, localPoint
|
|
|
|
, localPoint
|
|
|
|
, globalPoint
|
|
|
|
, button
|
|
|
|
, QGuiApplication::mouseButtons() | button
|
|
|
|
, QGuiApplication::keyboardModifiers()
|
2016-08-29 23:24:16 -06:00
|
|
|
#ifndef OS_MAC_OLD
|
2016-08-31 16:06:12 -04:00
|
|
|
, Qt::MouseEventSynthesizedByApplication
|
2016-08-29 23:24:16 -06:00
|
|
|
#endif // OS_MAC_OLD
|
2016-08-31 16:06:12 -04:00
|
|
|
);
|
2019-02-19 10:57:53 +04:00
|
|
|
ev.setTimestamp(crl::now());
|
2016-08-27 11:52:05 -06:00
|
|
|
QGuiApplication::sendEvent(windowHandle, &ev);
|
|
|
|
}
|
2016-06-15 20:13:46 +03:00
|
|
|
}
|