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
|
|
|
*/
|
2017-06-21 10:49:10 +00:00
|
|
|
#include "twidget.h"
|
|
|
|
|
2016-04-12 21:31:28 +00:00
|
|
|
#include "mainwindow.h"
|
2019-01-21 13:42:21 +00:00
|
|
|
#include "core/application.h"
|
2014-06-14 19:32:11 +00:00
|
|
|
|
2016-01-30 16:31:10 +00:00
|
|
|
namespace Fonts {
|
2017-04-30 16:03:12 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
bool ValidateFont(const QString &familyName, int flags = 0) {
|
|
|
|
QFont checkFont(familyName);
|
|
|
|
checkFont.setPixelSize(13);
|
|
|
|
checkFont.setBold(flags & style::internal::FontBold);
|
|
|
|
checkFont.setItalic(flags & style::internal::FontItalic);
|
|
|
|
checkFont.setUnderline(flags & style::internal::FontUnderline);
|
|
|
|
checkFont.setStyleStrategy(QFont::PreferQuality);
|
|
|
|
auto realFamily = QFontInfo(checkFont).family();
|
|
|
|
if (realFamily.trimmed().compare(familyName, Qt::CaseInsensitive)) {
|
2019-01-21 10:22:18 +00:00
|
|
|
LOG(("Font Error: could not resolve '%1' font, got '%2'.").arg(familyName).arg(realFamily));
|
2017-04-30 16:03:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto metrics = QFontMetrics(checkFont);
|
|
|
|
if (!metrics.height()) {
|
|
|
|
LOG(("Font Error: got a zero height in '%1'.").arg(familyName));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LoadCustomFont(const QString &filePath, const QString &familyName, int flags = 0) {
|
|
|
|
auto regularId = QFontDatabase::addApplicationFont(filePath);
|
|
|
|
if (regularId < 0) {
|
|
|
|
LOG(("Font Error: could not add '%1'.").arg(filePath));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto found = [&familyName, regularId] {
|
|
|
|
for (auto &family : QFontDatabase::applicationFontFamilies(regularId)) {
|
|
|
|
if (!family.trimmed().compare(familyName, Qt::CaseInsensitive)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
if (!found()) {
|
|
|
|
LOG(("Font Error: could not locate '%1' font in '%2'.").arg(familyName).arg(filePath));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ValidateFont(familyName, flags);
|
|
|
|
}
|
2016-01-30 16:31:10 +00:00
|
|
|
|
2017-03-16 09:53:13 +00:00
|
|
|
bool Started = false;
|
2017-04-30 16:03:12 +00:00
|
|
|
QString OpenSansOverride;
|
|
|
|
QString OpenSansSemiboldOverride;
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void Start() {
|
|
|
|
if (Started) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Started = true;
|
|
|
|
|
|
|
|
auto regular = LoadCustomFont(qsl(":/gui/fonts/OpenSans-Regular.ttf"), qsl("Open Sans"));
|
|
|
|
auto bold = LoadCustomFont(qsl(":/gui/fonts/OpenSans-Bold.ttf"), qsl("Open Sans"), style::internal::FontBold);
|
|
|
|
auto semibold = LoadCustomFont(qsl(":/gui/fonts/OpenSans-Semibold.ttf"), qsl("Open Sans Semibold"));
|
|
|
|
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
// Attempt to workaround a strange font bug with Open Sans Semibold not loading.
|
|
|
|
// See https://github.com/telegramdesktop/tdesktop/issues/3276 for details.
|
|
|
|
// Crash happens on "options.maxh / _t->_st->font->height" with "division by zero".
|
|
|
|
// In that place "_t->_st->font" is "semiboldFont" is "font(13 "Open Sans Semibold").
|
2017-05-01 09:25:57 +00:00
|
|
|
if (!regular || !bold) {
|
|
|
|
if (ValidateFont(qsl("Segoe UI")) && ValidateFont(qsl("Segoe UI"), style::internal::FontBold)) {
|
|
|
|
OpenSansOverride = qsl("Segoe UI");
|
|
|
|
LOG(("Fonts Info: Using Segoe UI instead of Open Sans."));
|
2017-04-30 16:03:12 +00:00
|
|
|
}
|
2017-05-01 09:25:57 +00:00
|
|
|
}
|
|
|
|
if (!semibold) {
|
|
|
|
if (ValidateFont(qsl("Segoe UI Semibold"))) {
|
|
|
|
OpenSansSemiboldOverride = qsl("Segoe UI Semibold");
|
|
|
|
LOG(("Fonts Info: Using Segoe UI Semibold instead of Open Sans Semibold."));
|
2017-04-30 16:03:12 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-12 08:54:27 +00:00
|
|
|
// Disable default fallbacks to Segoe UI, see:
|
|
|
|
// https://github.com/telegramdesktop/tdesktop/issues/5368
|
|
|
|
//
|
|
|
|
//QFont::insertSubstitution(qsl("Open Sans"), qsl("Segoe UI"));
|
|
|
|
//QFont::insertSubstitution(qsl("Open Sans Semibold"), qsl("Segoe UI Semibold"));
|
2018-10-26 07:01:00 +00:00
|
|
|
#elif defined Q_OS_MAC // Q_OS_WIN
|
|
|
|
auto list = QStringList();
|
|
|
|
list.append(qsl(".SF NS Text"));
|
|
|
|
list.append(qsl("Helvetica Neue"));
|
|
|
|
list.append(qsl("Lucida Grande"));
|
|
|
|
QFont::insertSubstitutions(qsl("Open Sans"), list);
|
|
|
|
QFont::insertSubstitutions(qsl("Open Sans Semibold"), list);
|
|
|
|
#endif // Q_OS_WIN || Q_OS_MAC
|
2017-04-30 16:03:12 +00:00
|
|
|
}
|
2017-03-16 09:53:13 +00:00
|
|
|
|
2017-04-30 16:03:12 +00:00
|
|
|
QString GetOverride(const QString &familyName) {
|
|
|
|
if (familyName == qstr("Open Sans")) {
|
|
|
|
return OpenSansOverride.isEmpty() ? familyName : OpenSansOverride;
|
|
|
|
} else if (familyName == qstr("Open Sans Semibold")) {
|
|
|
|
return OpenSansSemiboldOverride.isEmpty() ? familyName : OpenSansSemiboldOverride;
|
2016-01-30 16:31:10 +00:00
|
|
|
}
|
2017-04-30 16:03:12 +00:00
|
|
|
return familyName;
|
2016-01-30 16:31:10 +00:00
|
|
|
}
|
|
|
|
|
2017-03-16 09:53:13 +00:00
|
|
|
} // Fonts
|
|
|
|
|
2017-12-26 12:41:48 +00:00
|
|
|
namespace Ui {
|
2014-06-14 19:32:11 +00:00
|
|
|
namespace {
|
2017-03-16 09:53:13 +00:00
|
|
|
|
2017-09-11 08:55:36 +00: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 16:55:16 +00:00
|
|
|
WidgetCreator::Create(target);
|
|
|
|
} else if (!cIsSnowLeopard()) {
|
|
|
|
WidgetCreator::Create(target);
|
2015-04-02 10:33:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-24 13:05:15 +00:00
|
|
|
void SendPendingEventsRecursive(QWidget *target, bool parentHiddenFlag) {
|
2017-09-11 08:55:36 +00: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());
|
|
|
|
QApplication::sendEvent(target, &e);
|
|
|
|
}
|
|
|
|
if (target->testAttribute(Qt::WA_PendingResizeEvent)) {
|
|
|
|
target->setAttribute(Qt::WA_PendingResizeEvent, false);
|
|
|
|
QResizeEvent e(target->size(), QSize());
|
|
|
|
QApplication::sendEvent(target, &e);
|
|
|
|
}
|
2017-11-24 13:05:15 +00:00
|
|
|
|
|
|
|
auto removeVisibleFlag = [&] {
|
|
|
|
return parentHiddenFlag
|
|
|
|
|| target->testAttribute(Qt::WA_WState_Hidden);
|
|
|
|
};
|
|
|
|
|
2017-09-11 08:55:36 +00: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 13:05:15 +00:00
|
|
|
SendPendingEventsRecursive(widget, removeVisibleFlag());
|
2017-09-11 08:55:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-24 13:05:15 +00:00
|
|
|
|
|
|
|
if (removeVisibleFlag()) {
|
2017-09-11 08:55:36 +00:00
|
|
|
target->setAttribute(Qt::WA_WState_Visible, false);
|
|
|
|
}
|
2016-02-27 19:39:51 +00:00
|
|
|
}
|
|
|
|
|
2017-09-11 08:55:36 +00:00
|
|
|
} // namespace
|
|
|
|
|
2017-12-26 12:41:48 +00:00
|
|
|
void SendPendingMoveResizeEvents(not_null<QWidget*> target) {
|
2017-09-11 08:55:36 +00:00
|
|
|
CreateWidgetStateRecursive(target);
|
2017-11-24 13:05:15 +00:00
|
|
|
SendPendingEventsRecursive(target, !target->isVisible());
|
2015-07-17 19:17:37 +00:00
|
|
|
}
|
|
|
|
|
2018-04-06 16:23:09 +00:00
|
|
|
QPixmap GrabWidget(not_null<QWidget*> target, QRect rect, QColor bg) {
|
2017-12-26 12:41:48 +00:00
|
|
|
SendPendingMoveResizeEvents(target);
|
|
|
|
if (rect.isNull()) {
|
|
|
|
rect = target->rect();
|
|
|
|
}
|
2015-10-17 14:52:26 +00:00
|
|
|
|
2017-12-26 12:41:48 +00:00
|
|
|
auto result = QPixmap(rect.size() * cIntRetinaFactor());
|
|
|
|
result.setDevicePixelRatio(cRetinaFactor());
|
2016-12-13 17:07:56 +00:00
|
|
|
if (!target->testAttribute(Qt::WA_OpaquePaintEvent)) {
|
|
|
|
result.fill(bg);
|
|
|
|
}
|
2017-12-26 12:41:48 +00:00
|
|
|
target->render(
|
|
|
|
&result,
|
|
|
|
QPoint(0, 0),
|
|
|
|
rect,
|
|
|
|
QWidget::DrawChildren | QWidget::IgnoreMask);
|
2017-02-21 14:37:53 +00:00
|
|
|
return result;
|
2016-11-24 19:28:23 +00:00
|
|
|
}
|
|
|
|
|
2018-04-06 16:23:09 +00:00
|
|
|
QImage GrabWidgetToImage(not_null<QWidget*> target, QRect rect, QColor bg) {
|
2017-12-26 12:41:48 +00:00
|
|
|
Ui::SendPendingMoveResizeEvents(target);
|
|
|
|
if (rect.isNull()) {
|
|
|
|
rect = target->rect();
|
|
|
|
}
|
2016-11-24 19:28:23 +00:00
|
|
|
|
2017-12-26 12:41:48 +00:00
|
|
|
auto result = QImage(
|
|
|
|
rect.size() * cIntRetinaFactor(),
|
|
|
|
QImage::Format_ARGB32_Premultiplied);
|
2016-11-24 19:28:23 +00:00
|
|
|
result.setDevicePixelRatio(cRetinaFactor());
|
|
|
|
if (!target->testAttribute(Qt::WA_OpaquePaintEvent)) {
|
2016-12-13 17:07:56 +00:00
|
|
|
result.fill(bg);
|
2016-11-24 19:28:23 +00:00
|
|
|
}
|
2017-12-26 12:41:48 +00:00
|
|
|
target->render(
|
|
|
|
&result,
|
|
|
|
QPoint(0, 0),
|
|
|
|
rect,
|
|
|
|
QWidget::DrawChildren | QWidget::IgnoreMask);
|
2017-02-21 14:37:53 +00:00
|
|
|
return result;
|
2014-06-14 19:32:11 +00:00
|
|
|
}
|
2016-05-19 12:03:51 +00:00
|
|
|
|
2018-06-26 21:33:30 +00:00
|
|
|
void ForceFullRepaint(not_null<QWidget*> widget) {
|
|
|
|
const auto refresher = std::make_unique<QWidget>(widget);
|
|
|
|
refresher->setGeometry(widget->rect());
|
|
|
|
refresher->show();
|
|
|
|
}
|
|
|
|
|
2018-12-12 08:59:51 +00:00
|
|
|
void PostponeCall(FnMut<void()> &&callable) {
|
2019-01-18 11:26:43 +00:00
|
|
|
Core::App().postponeCall(std::move(callable));
|
2018-12-12 08:59:51 +00:00
|
|
|
}
|
|
|
|
|
2017-12-26 12:41:48 +00:00
|
|
|
} // namespace Ui
|
|
|
|
|
2016-06-15 17:13:46 +00:00
|
|
|
void sendSynteticMouseEvent(QWidget *widget, QEvent::Type type, Qt::MouseButton button, const QPoint &globalPoint) {
|
2016-08-27 17:52:05 +00:00
|
|
|
if (auto windowHandle = widget->window()->windowHandle()) {
|
|
|
|
auto localPoint = windowHandle->mapFromGlobal(globalPoint);
|
2016-08-31 20:06:12 +00:00
|
|
|
QMouseEvent ev(type
|
|
|
|
, localPoint
|
|
|
|
, localPoint
|
|
|
|
, globalPoint
|
|
|
|
, button
|
|
|
|
, QGuiApplication::mouseButtons() | button
|
|
|
|
, QGuiApplication::keyboardModifiers()
|
2016-08-30 05:24:16 +00:00
|
|
|
#ifndef OS_MAC_OLD
|
2016-08-31 20:06:12 +00:00
|
|
|
, Qt::MouseEventSynthesizedByApplication
|
2016-08-30 05:24:16 +00:00
|
|
|
#endif // OS_MAC_OLD
|
2016-08-31 20:06:12 +00:00
|
|
|
);
|
2016-08-27 17:52:05 +00:00
|
|
|
ev.setTimestamp(getms());
|
|
|
|
QGuiApplication::sendEvent(windowHandle, &ev);
|
|
|
|
}
|
2016-06-15 17:13:46 +00:00
|
|
|
}
|