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-04-09 20:41:20 +00:00
|
|
|
#include "app.h"
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2017-04-13 08:27:10 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2018-06-20 17:30:57 +00:00
|
|
|
#include "boxes/confirm_box.h"
|
2019-01-04 11:09:48 +00:00
|
|
|
#include "data/data_channel.h"
|
|
|
|
#include "data/data_chat.h"
|
|
|
|
#include "data/data_user.h"
|
2016-06-09 11:51:24 +00:00
|
|
|
#include "data/data_abstract_structure.h"
|
2018-01-14 16:02:25 +00:00
|
|
|
#include "data/data_media_types.h"
|
2018-01-04 10:22:53 +00:00
|
|
|
#include "data/data_session.h"
|
2018-12-18 10:45:06 +00:00
|
|
|
#include "data/data_document.h"
|
2018-01-13 12:45:11 +00:00
|
|
|
#include "history/history.h"
|
2016-09-27 13:37:18 +00:00
|
|
|
#include "history/history_location_manager.h"
|
2017-12-18 15:44:50 +00:00
|
|
|
#include "history/history_item_components.h"
|
2018-01-09 17:08:31 +00:00
|
|
|
#include "history/view/history_view_service_message.h"
|
2019-02-13 12:36:59 +00:00
|
|
|
#include "media/audio/media_audio.h"
|
2018-10-23 09:44:42 +00:00
|
|
|
#include "ui/image/image.h"
|
2016-07-08 13:56:53 +00:00
|
|
|
#include "inline_bots/inline_bot_layout_item.h"
|
2018-12-18 10:45:06 +00:00
|
|
|
#include "core/crash_reports.h"
|
|
|
|
#include "core/update_checker.h"
|
2019-01-21 13:42:21 +00:00
|
|
|
#include "core/sandbox.h"
|
|
|
|
#include "core/application.h"
|
2018-12-18 10:45:06 +00:00
|
|
|
#include "window/themes/window_theme.h"
|
|
|
|
#include "window/notifications_manager.h"
|
2019-06-06 11:20:21 +00:00
|
|
|
#include "window/window_controller.h"
|
2018-12-18 10:45:06 +00:00
|
|
|
#include "platform/platform_notifications_manager.h"
|
|
|
|
#include "storage/file_upload.h"
|
|
|
|
#include "storage/localstorage.h"
|
|
|
|
#include "storage/storage_facade.h"
|
|
|
|
#include "storage/storage_shared_media.h"
|
2017-02-23 10:59:19 +00:00
|
|
|
#include "mainwindow.h"
|
2014-05-30 08:53:19 +00:00
|
|
|
#include "mainwidget.h"
|
2016-04-12 21:31:28 +00:00
|
|
|
#include "apiwrap.h"
|
2015-07-21 14:55:23 +00:00
|
|
|
#include "numbers.h"
|
2016-05-24 16:13:07 +00:00
|
|
|
#include "observer_peer.h"
|
2019-07-24 11:45:24 +00:00
|
|
|
#include "main/main_session.h"
|
2020-01-05 16:14:08 +00:00
|
|
|
#include "styles/style_boxes.h"
|
2019-01-21 13:42:21 +00:00
|
|
|
#include "styles/style_overview.h"
|
2020-02-03 09:26:37 +00:00
|
|
|
#include "styles/style_media_view.h"
|
2019-01-21 13:42:21 +00:00
|
|
|
#include "styles/style_chat_helpers.h"
|
|
|
|
#include "styles/style_history.h"
|
2019-09-18 11:19:05 +00:00
|
|
|
#include "styles/style_layers.h"
|
2015-07-21 14:55:23 +00:00
|
|
|
|
2019-09-04 07:19:15 +00:00
|
|
|
#include <QtCore/QBuffer>
|
|
|
|
#include <QtGui/QFontDatabase>
|
|
|
|
|
2018-10-11 15:54:57 +00:00
|
|
|
#ifdef OS_MAC_OLD
|
|
|
|
#include <libexif/exif-data.h>
|
|
|
|
#endif // OS_MAC_OLD
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
namespace {
|
2016-03-02 18:34:42 +00:00
|
|
|
App::LaunchState _launchState = App::Launched;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2018-01-11 19:33:26 +00:00
|
|
|
HistoryView::Element *hoveredItem = nullptr,
|
2016-06-14 16:26:41 +00:00
|
|
|
*pressedItem = nullptr,
|
|
|
|
*hoveredLinkItem = nullptr,
|
|
|
|
*pressedLinkItem = nullptr,
|
|
|
|
*mousedItem = nullptr;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2015-10-03 10:09:09 +00:00
|
|
|
struct CornersPixmaps {
|
2017-07-13 14:42:46 +00:00
|
|
|
QPixmap p[4];
|
2015-10-03 10:09:09 +00:00
|
|
|
};
|
2017-07-13 14:42:46 +00:00
|
|
|
QVector<CornersPixmaps> corners;
|
2016-09-27 13:37:18 +00:00
|
|
|
using CornersMap = QMap<uint32, CornersPixmaps>;
|
2015-10-03 10:09:09 +00:00
|
|
|
CornersMap cornersMap;
|
2017-07-13 14:42:46 +00:00
|
|
|
QImage cornersMaskLarge[4], cornersMaskSmall[4];
|
2015-05-20 19:28:24 +00:00
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
int32 serviceImageCacheSize = 0;
|
|
|
|
|
2017-12-17 08:13:26 +00:00
|
|
|
} // namespace
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
namespace App {
|
|
|
|
|
|
|
|
QString formatPhone(QString phone) {
|
2015-07-21 14:55:23 +00:00
|
|
|
if (phone.isEmpty()) return QString();
|
2016-06-08 16:40:33 +00:00
|
|
|
if (phone.at(0) == '0') return phone;
|
|
|
|
|
2015-07-21 14:55:23 +00:00
|
|
|
QString number = phone;
|
|
|
|
for (const QChar *ch = phone.constData(), *e = ch + phone.size(); ch != e; ++ch) {
|
|
|
|
if (ch->unicode() < '0' || ch->unicode() > '9') {
|
|
|
|
number = phone.replace(QRegularExpression(qsl("[^\\d]")), QString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
QVector<int> groups = phoneNumberParse(number);
|
|
|
|
if (groups.isEmpty()) return '+' + number;
|
|
|
|
|
|
|
|
QString result;
|
|
|
|
result.reserve(number.size() + groups.size() + 1);
|
|
|
|
result.append('+');
|
|
|
|
int32 sum = 0;
|
|
|
|
for (int32 i = 0, l = groups.size(); i < l; ++i) {
|
|
|
|
result.append(number.midRef(sum, groups.at(i)));
|
|
|
|
sum += groups.at(i);
|
|
|
|
if (sum < number.size()) result.append(' ');
|
|
|
|
}
|
|
|
|
if (sum < number.size()) result.append(number.midRef(sum));
|
|
|
|
return result;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-12-23 13:21:01 +00:00
|
|
|
void prepareCorners(RoundCorners index, int32 radius, const QBrush &brush, const style::color *shadow = nullptr, QImage *cors = nullptr) {
|
2017-07-13 14:42:46 +00:00
|
|
|
Expects(::corners.size() > index);
|
2019-06-07 11:32:58 +00:00
|
|
|
|
2015-05-20 19:28:24 +00:00
|
|
|
int32 r = radius * cIntRetinaFactor(), s = st::msgShadow * cIntRetinaFactor();
|
|
|
|
QImage rect(r * 3, r * 3 + (shadow ? s : 0), QImage::Format_ARGB32_Premultiplied), localCors[4];
|
|
|
|
{
|
2016-12-03 12:10:35 +00:00
|
|
|
Painter p(&rect);
|
|
|
|
PainterHighQualityEnabler hq(p);
|
|
|
|
|
2015-05-20 19:28:24 +00:00
|
|
|
p.setCompositionMode(QPainter::CompositionMode_Source);
|
2016-10-31 12:29:26 +00:00
|
|
|
p.fillRect(QRect(0, 0, rect.width(), rect.height()), Qt::transparent);
|
2015-05-20 19:28:24 +00:00
|
|
|
p.setCompositionMode(QPainter::CompositionMode_SourceOver);
|
|
|
|
p.setPen(Qt::NoPen);
|
|
|
|
if (shadow) {
|
|
|
|
p.setBrush((*shadow)->b);
|
|
|
|
p.drawRoundedRect(0, s, r * 3, r * 3, r, r);
|
|
|
|
}
|
2016-12-23 13:21:01 +00:00
|
|
|
p.setBrush(brush);
|
2015-05-20 19:28:24 +00:00
|
|
|
p.drawRoundedRect(0, 0, r * 3, r * 3, r, r);
|
|
|
|
}
|
|
|
|
if (!cors) cors = localCors;
|
|
|
|
cors[0] = rect.copy(0, 0, r, r);
|
|
|
|
cors[1] = rect.copy(r * 2, 0, r, r);
|
|
|
|
cors[2] = rect.copy(0, r * 2, r, r + (shadow ? s : 0));
|
|
|
|
cors[3] = rect.copy(r * 2, r * 2, r, r + (shadow ? s : 0));
|
2016-07-07 12:35:14 +00:00
|
|
|
if (index != SmallMaskCorners && index != LargeMaskCorners) {
|
2015-10-03 10:09:09 +00:00
|
|
|
for (int i = 0; i < 4; ++i) {
|
2017-07-13 14:42:46 +00:00
|
|
|
::corners[index].p[i] = pixmapFromImageInPlace(std::move(cors[i]));
|
|
|
|
::corners[index].p[i].setDevicePixelRatio(cRetinaFactor());
|
2015-10-03 10:09:09 +00:00
|
|
|
}
|
2015-05-20 19:28:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-13 14:42:46 +00:00
|
|
|
void createMaskCorners() {
|
2015-05-20 19:28:24 +00:00
|
|
|
QImage mask[4];
|
2017-07-13 14:42:46 +00:00
|
|
|
prepareCorners(SmallMaskCorners, st::buttonRadius, QColor(255, 255, 255), nullptr, mask);
|
2015-05-20 19:28:24 +00:00
|
|
|
for (int i = 0; i < 4; ++i) {
|
2017-07-13 14:42:46 +00:00
|
|
|
::cornersMaskSmall[i] = mask[i].convertToFormat(QImage::Format_ARGB32_Premultiplied);
|
|
|
|
::cornersMaskSmall[i].setDevicePixelRatio(cRetinaFactor());
|
2016-07-07 12:35:14 +00:00
|
|
|
}
|
2017-12-25 19:26:08 +00:00
|
|
|
prepareCorners(LargeMaskCorners, st::historyMessageRadius, QColor(255, 255, 255), nullptr, mask);
|
2016-07-07 12:35:14 +00:00
|
|
|
for (int i = 0; i < 4; ++i) {
|
2017-07-13 14:42:46 +00:00
|
|
|
::cornersMaskLarge[i] = mask[i].convertToFormat(QImage::Format_ARGB32_Premultiplied);
|
|
|
|
::cornersMaskLarge[i].setDevicePixelRatio(cRetinaFactor());
|
2016-07-07 12:35:14 +00:00
|
|
|
}
|
2017-07-13 14:42:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void createPaletteCorners() {
|
2016-11-09 13:40:51 +00:00
|
|
|
prepareCorners(MenuCorners, st::buttonRadius, st::menuBg);
|
2016-12-13 17:07:56 +00:00
|
|
|
prepareCorners(BoxCorners, st::boxRadius, st::boxBg);
|
2016-12-21 15:05:58 +00:00
|
|
|
prepareCorners(BotKbOverCorners, st::dateRadius, st::msgBotKbOverBgAdd);
|
2016-07-08 10:06:41 +00:00
|
|
|
prepareCorners(StickerCorners, st::dateRadius, st::msgServiceBg);
|
2016-12-21 15:05:58 +00:00
|
|
|
prepareCorners(StickerSelectedCorners, st::dateRadius, st::msgServiceBgSelected);
|
2016-07-07 12:35:14 +00:00
|
|
|
prepareCorners(SelectedOverlaySmallCorners, st::buttonRadius, st::msgSelectOverlay);
|
2017-12-25 19:26:08 +00:00
|
|
|
prepareCorners(SelectedOverlayLargeCorners, st::historyMessageRadius, st::msgSelectOverlay);
|
2016-07-08 10:06:41 +00:00
|
|
|
prepareCorners(DateCorners, st::dateRadius, st::msgDateImgBg);
|
|
|
|
prepareCorners(DateSelectedCorners, st::dateRadius, st::msgDateImgBgSelected);
|
2019-03-14 20:00:24 +00:00
|
|
|
prepareCorners(OverviewVideoCorners, st::overviewVideoStatusRadius, st::msgDateImgBg);
|
|
|
|
prepareCorners(OverviewVideoSelectedCorners, st::overviewVideoStatusRadius, st::msgDateImgBgSelected);
|
2017-12-25 19:26:08 +00:00
|
|
|
prepareCorners(InShadowCorners, st::historyMessageRadius, st::msgInShadow);
|
|
|
|
prepareCorners(InSelectedShadowCorners, st::historyMessageRadius, st::msgInShadowSelected);
|
|
|
|
prepareCorners(ForwardCorners, st::historyMessageRadius, st::historyForwardChooseBg);
|
2016-12-21 15:05:58 +00:00
|
|
|
prepareCorners(MediaviewSaveCorners, st::mediaviewControllerRadius, st::mediaviewSaveMsgBg);
|
2016-07-07 12:35:14 +00:00
|
|
|
prepareCorners(EmojiHoverCorners, st::buttonRadius, st::emojiPanHover);
|
|
|
|
prepareCorners(StickerHoverCorners, st::buttonRadius, st::emojiPanHover);
|
|
|
|
prepareCorners(BotKeyboardCorners, st::buttonRadius, st::botKbBg);
|
|
|
|
prepareCorners(PhotoSelectOverlayCorners, st::buttonRadius, st::overviewPhotoSelectOverlay);
|
|
|
|
|
2016-12-21 15:05:58 +00:00
|
|
|
prepareCorners(Doc1Corners, st::buttonRadius, st::msgFile1Bg);
|
|
|
|
prepareCorners(Doc2Corners, st::buttonRadius, st::msgFile2Bg);
|
|
|
|
prepareCorners(Doc3Corners, st::buttonRadius, st::msgFile3Bg);
|
|
|
|
prepareCorners(Doc4Corners, st::buttonRadius, st::msgFile4Bg);
|
2015-05-20 19:28:24 +00:00
|
|
|
|
2017-12-25 19:26:08 +00:00
|
|
|
prepareCorners(MessageInCorners, st::historyMessageRadius, st::msgInBg, &st::msgInShadow);
|
|
|
|
prepareCorners(MessageInSelectedCorners, st::historyMessageRadius, st::msgInBgSelected, &st::msgInShadowSelected);
|
|
|
|
prepareCorners(MessageOutCorners, st::historyMessageRadius, st::msgOutBg, &st::msgOutShadow);
|
|
|
|
prepareCorners(MessageOutSelectedCorners, st::historyMessageRadius, st::msgOutBgSelected, &st::msgOutShadowSelected);
|
2020-01-05 16:14:08 +00:00
|
|
|
|
|
|
|
prepareCorners(SendFilesBoxAlbumGroupCorners, st::sendBoxAlbumGroupRadius, st::callFingerprintBg);
|
2016-12-20 13:03:51 +00:00
|
|
|
}
|
2016-11-03 10:33:57 +00:00
|
|
|
|
2017-07-13 14:42:46 +00:00
|
|
|
void createCorners() {
|
|
|
|
::corners.resize(RoundCornersCount);
|
|
|
|
createMaskCorners();
|
|
|
|
createPaletteCorners();
|
|
|
|
}
|
|
|
|
|
2016-12-20 13:03:51 +00:00
|
|
|
void clearCorners() {
|
2017-07-13 14:42:46 +00:00
|
|
|
::corners.clear();
|
2016-12-20 13:03:51 +00:00
|
|
|
::cornersMap.clear();
|
|
|
|
}
|
2016-11-03 10:33:57 +00:00
|
|
|
|
2016-12-20 13:03:51 +00:00
|
|
|
void initMedia() {
|
|
|
|
createCorners();
|
|
|
|
|
|
|
|
using Update = Window::Theme::BackgroundUpdate;
|
|
|
|
static auto subscription = Window::Theme::Background()->add_subscription([](const Update &update) {
|
2016-12-23 13:21:01 +00:00
|
|
|
if (update.paletteChanged()) {
|
2017-07-13 14:42:46 +00:00
|
|
|
createPaletteCorners();
|
2016-12-20 13:03:51 +00:00
|
|
|
|
|
|
|
if (App::main()) {
|
|
|
|
App::main()->updateScrollColors();
|
|
|
|
}
|
2018-01-09 17:08:31 +00:00
|
|
|
HistoryView::serviceColorsUpdated();
|
2016-12-20 13:03:51 +00:00
|
|
|
} else if (update.type == Update::Type::New) {
|
|
|
|
prepareCorners(StickerCorners, st::dateRadius, st::msgServiceBg);
|
2016-12-21 15:05:58 +00:00
|
|
|
prepareCorners(StickerSelectedCorners, st::dateRadius, st::msgServiceBgSelected);
|
2016-12-20 13:03:51 +00:00
|
|
|
|
|
|
|
if (App::main()) {
|
|
|
|
App::main()->updateScrollColors();
|
|
|
|
}
|
2018-01-09 17:08:31 +00:00
|
|
|
HistoryView::serviceColorsUpdated();
|
2016-11-03 10:33:57 +00:00
|
|
|
}
|
|
|
|
});
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2016-01-03 01:43:42 +00:00
|
|
|
|
2015-12-27 21:37:48 +00:00
|
|
|
void deinitMedia() {
|
2016-12-20 13:03:51 +00:00
|
|
|
clearCorners();
|
|
|
|
|
2016-06-09 11:51:24 +00:00
|
|
|
Data::clearGlobalStructures();
|
2016-04-09 20:41:20 +00:00
|
|
|
|
2018-10-11 15:54:57 +00:00
|
|
|
Images::ClearAll();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2018-01-11 19:33:26 +00:00
|
|
|
void hoveredItem(HistoryView::Element *item) {
|
2014-05-30 08:53:19 +00:00
|
|
|
::hoveredItem = item;
|
|
|
|
}
|
|
|
|
|
2018-01-11 19:33:26 +00:00
|
|
|
HistoryView::Element *hoveredItem() {
|
2014-05-30 08:53:19 +00:00
|
|
|
return ::hoveredItem;
|
|
|
|
}
|
|
|
|
|
2018-01-11 19:33:26 +00:00
|
|
|
void pressedItem(HistoryView::Element *item) {
|
2014-05-30 08:53:19 +00:00
|
|
|
::pressedItem = item;
|
|
|
|
}
|
|
|
|
|
2018-01-11 19:33:26 +00:00
|
|
|
HistoryView::Element *pressedItem() {
|
2014-05-30 08:53:19 +00:00
|
|
|
return ::pressedItem;
|
|
|
|
}
|
2016-01-03 01:43:42 +00:00
|
|
|
|
2018-01-11 19:33:26 +00:00
|
|
|
void hoveredLinkItem(HistoryView::Element *item) {
|
2014-05-30 08:53:19 +00:00
|
|
|
::hoveredLinkItem = item;
|
|
|
|
}
|
|
|
|
|
2018-01-11 19:33:26 +00:00
|
|
|
HistoryView::Element *hoveredLinkItem() {
|
2014-05-30 08:53:19 +00:00
|
|
|
return ::hoveredLinkItem;
|
|
|
|
}
|
|
|
|
|
2018-01-11 19:33:26 +00:00
|
|
|
void pressedLinkItem(HistoryView::Element *item) {
|
2014-05-30 08:53:19 +00:00
|
|
|
::pressedLinkItem = item;
|
|
|
|
}
|
|
|
|
|
2018-01-11 19:33:26 +00:00
|
|
|
HistoryView::Element *pressedLinkItem() {
|
2014-05-30 08:53:19 +00:00
|
|
|
return ::pressedLinkItem;
|
|
|
|
}
|
2016-01-03 01:43:42 +00:00
|
|
|
|
2018-01-11 19:33:26 +00:00
|
|
|
void mousedItem(HistoryView::Element *item) {
|
2014-05-30 08:53:19 +00:00
|
|
|
::mousedItem = item;
|
|
|
|
}
|
|
|
|
|
2018-01-11 19:33:26 +00:00
|
|
|
HistoryView::Element *mousedItem() {
|
2014-05-30 08:53:19 +00:00
|
|
|
return ::mousedItem;
|
|
|
|
}
|
|
|
|
|
2016-06-14 16:26:41 +00:00
|
|
|
void clearMousedItems() {
|
|
|
|
hoveredItem(nullptr);
|
|
|
|
pressedItem(nullptr);
|
|
|
|
hoveredLinkItem(nullptr);
|
|
|
|
pressedLinkItem(nullptr);
|
|
|
|
mousedItem(nullptr);
|
|
|
|
}
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
void quit() {
|
2018-06-20 17:30:57 +00:00
|
|
|
if (quitting()) {
|
|
|
|
return;
|
2019-07-24 14:00:30 +00:00
|
|
|
} else if (Core::IsAppLaunched()
|
|
|
|
&& Core::App().exportPreventsQuit()) {
|
2018-06-20 17:30:57 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-03-02 18:34:42 +00:00
|
|
|
setLaunchState(QuitRequested);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2018-12-04 12:10:00 +00:00
|
|
|
if (auto window = App::wnd()) {
|
2019-01-21 13:42:21 +00:00
|
|
|
if (!Core::Sandbox::Instance().isSavingSession()) {
|
2016-06-21 15:58:07 +00:00
|
|
|
window->hide();
|
|
|
|
}
|
2016-06-07 19:59:39 +00:00
|
|
|
}
|
2019-01-21 13:42:21 +00:00
|
|
|
Core::Application::QuitAttempt();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-03-02 18:34:42 +00:00
|
|
|
bool quitting() {
|
|
|
|
return _launchState != Launched;
|
|
|
|
}
|
|
|
|
|
|
|
|
LaunchState launchState() {
|
|
|
|
return _launchState;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-03-02 18:34:42 +00:00
|
|
|
void setLaunchState(LaunchState state) {
|
|
|
|
_launchState = state;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-10-28 12:44:28 +00:00
|
|
|
void restart() {
|
2018-07-11 22:14:44 +00:00
|
|
|
using namespace Core;
|
|
|
|
const auto updateReady = !UpdaterDisabled()
|
|
|
|
&& (UpdateChecker().state() == UpdateChecker::State::Ready);
|
2016-10-28 12:44:28 +00:00
|
|
|
if (updateReady) {
|
|
|
|
cSetRestartingUpdate(true);
|
|
|
|
} else {
|
|
|
|
cSetRestarting(true);
|
|
|
|
cSetRestartingToSettings(true);
|
|
|
|
}
|
|
|
|
App::quit();
|
|
|
|
}
|
|
|
|
|
2014-12-22 23:11:37 +00:00
|
|
|
QImage readImage(QByteArray data, QByteArray *format, bool opaque, bool *animated) {
|
2020-01-02 11:25:52 +00:00
|
|
|
QByteArray tmpFormat;
|
2014-05-30 08:53:19 +00:00
|
|
|
QImage result;
|
|
|
|
QBuffer buffer(&data);
|
2020-01-02 11:25:52 +00:00
|
|
|
if (!format) {
|
|
|
|
format = &tmpFormat;
|
|
|
|
}
|
2015-12-23 11:13:08 +00:00
|
|
|
{
|
|
|
|
QImageReader reader(&buffer, *format);
|
2016-08-30 05:24:16 +00:00
|
|
|
#ifndef OS_MAC_OLD
|
2015-12-23 11:13:08 +00:00
|
|
|
reader.setAutoTransform(true);
|
2016-08-30 05:24:16 +00:00
|
|
|
#endif // OS_MAC_OLD
|
2015-12-23 11:13:08 +00:00
|
|
|
if (animated) *animated = reader.supportsAnimation() && reader.imageCount() > 1;
|
|
|
|
QByteArray fmt = reader.format();
|
|
|
|
if (!fmt.isEmpty()) *format = fmt;
|
|
|
|
if (!reader.read(&result)) {
|
|
|
|
return QImage();
|
|
|
|
}
|
|
|
|
fmt = reader.format();
|
|
|
|
if (!fmt.isEmpty()) *format = fmt;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
buffer.seek(0);
|
2016-12-08 14:08:54 +00:00
|
|
|
auto fmt = QString::fromUtf8(*format).toLower();
|
2014-05-30 08:53:19 +00:00
|
|
|
if (fmt == "jpg" || fmt == "jpeg") {
|
2016-08-30 05:24:16 +00:00
|
|
|
#ifdef OS_MAC_OLD
|
2016-12-08 14:08:54 +00:00
|
|
|
if (auto exifData = exif_data_new_from_data((const uchar*)(data.constData()), data.size())) {
|
|
|
|
auto byteOrder = exif_data_get_byte_order(exifData);
|
|
|
|
if (auto exifEntry = exif_data_get_entry(exifData, EXIF_TAG_ORIENTATION)) {
|
|
|
|
auto orientationFix = [exifEntry, byteOrder] {
|
|
|
|
auto orientation = exif_get_short(exifEntry->data, byteOrder);
|
|
|
|
switch (orientation) {
|
|
|
|
case 2: return QTransform(-1, 0, 0, 1, 0, 0);
|
|
|
|
case 3: return QTransform(-1, 0, 0, -1, 0, 0);
|
|
|
|
case 4: return QTransform(1, 0, 0, -1, 0, 0);
|
|
|
|
case 5: return QTransform(0, -1, -1, 0, 0, 0);
|
|
|
|
case 6: return QTransform(0, 1, -1, 0, 0, 0);
|
|
|
|
case 7: return QTransform(0, 1, 1, 0, 0, 0);
|
|
|
|
case 8: return QTransform(0, -1, 1, 0, 0, 0);
|
|
|
|
}
|
|
|
|
return QTransform();
|
|
|
|
};
|
|
|
|
result = result.transformed(orientationFix());
|
2015-12-23 11:13:08 +00:00
|
|
|
}
|
|
|
|
exif_data_free(exifData);
|
|
|
|
}
|
2016-08-30 05:24:16 +00:00
|
|
|
#endif // OS_MAC_OLD
|
2016-11-28 15:45:07 +00:00
|
|
|
} else if (opaque) {
|
2017-02-21 13:45:56 +00:00
|
|
|
result = Images::prepareOpaque(std::move(result));
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-01-02 14:55:24 +00:00
|
|
|
QImage readImage(const QString &file, QByteArray *format, bool opaque, bool *animated, QByteArray *content) {
|
2014-05-30 08:53:19 +00:00
|
|
|
QFile f(file);
|
2016-11-28 15:45:07 +00:00
|
|
|
if (f.size() > kImageSizeLimit || !f.open(QIODevice::ReadOnly)) {
|
2014-12-22 23:11:37 +00:00
|
|
|
if (animated) *animated = false;
|
2014-05-30 08:53:19 +00:00
|
|
|
return QImage();
|
|
|
|
}
|
2016-12-08 14:08:54 +00:00
|
|
|
auto imageBytes = f.readAll();
|
|
|
|
auto result = readImage(imageBytes, format, opaque, animated);
|
|
|
|
if (content && !result.isNull()) {
|
|
|
|
*content = imageBytes;
|
|
|
|
}
|
2015-01-02 14:55:24 +00:00
|
|
|
return result;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-05-27 10:57:11 +00:00
|
|
|
QPixmap pixmapFromImageInPlace(QImage &&image) {
|
2017-02-21 13:45:56 +00:00
|
|
|
return QPixmap::fromImage(std::move(image), Qt::ColorOnly);
|
2016-05-27 10:57:11 +00:00
|
|
|
}
|
|
|
|
|
2017-12-21 16:15:37 +00:00
|
|
|
void rectWithCorners(Painter &p, QRect rect, const style::color &bg, RoundCorners index, RectParts corners) {
|
|
|
|
auto parts = RectPart::Top
|
|
|
|
| RectPart::NoTopBottom
|
|
|
|
| RectPart::Bottom
|
|
|
|
| corners;
|
|
|
|
roundRect(p, rect, bg, index, nullptr, parts);
|
|
|
|
if ((corners & RectPart::AllCorners) != RectPart::AllCorners) {
|
|
|
|
const auto size = ::corners[index].p[0].width() / cIntRetinaFactor();
|
|
|
|
if (!(corners & RectPart::TopLeft)) {
|
|
|
|
p.fillRect(rect.x(), rect.y(), size, size, bg);
|
|
|
|
}
|
|
|
|
if (!(corners & RectPart::TopRight)) {
|
|
|
|
p.fillRect(rect.x() + rect.width() - size, rect.y(), size, size, bg);
|
|
|
|
}
|
2017-12-18 17:50:25 +00:00
|
|
|
if (!(corners & RectPart::BottomLeft)) {
|
2017-12-21 16:15:37 +00:00
|
|
|
p.fillRect(rect.x(), rect.y() + rect.height() - size, size, size, bg);
|
|
|
|
}
|
|
|
|
if (!(corners & RectPart::BottomRight)) {
|
|
|
|
p.fillRect(rect.x() + rect.width() - size, rect.y() + rect.height() - size, size, size, bg);
|
2016-11-21 20:26:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-18 17:50:25 +00:00
|
|
|
void complexOverlayRect(Painter &p, QRect rect, ImageRoundRadius radius, RectParts corners) {
|
2017-04-02 11:30:03 +00:00
|
|
|
if (radius == ImageRoundRadius::Ellipse) {
|
|
|
|
PainterHighQualityEnabler hq(p);
|
|
|
|
p.setPen(Qt::NoPen);
|
|
|
|
p.setBrush(p.textPalette().selectOverlay);
|
|
|
|
p.drawEllipse(rect);
|
|
|
|
} else {
|
2017-12-13 18:10:48 +00:00
|
|
|
auto overlayCorners = (radius == ImageRoundRadius::Small)
|
|
|
|
? SelectedOverlaySmallCorners
|
|
|
|
: SelectedOverlayLargeCorners;
|
2017-12-21 16:15:37 +00:00
|
|
|
const auto bg = p.textPalette().selectOverlay;
|
|
|
|
rectWithCorners(p, rect, bg, overlayCorners, corners);
|
2016-11-21 20:26:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-18 17:50:25 +00:00
|
|
|
void complexLocationRect(Painter &p, QRect rect, ImageRoundRadius radius, RectParts corners) {
|
2017-12-21 16:15:37 +00:00
|
|
|
rectWithCorners(p, rect, st::msgInBg, MessageInCorners, corners);
|
2016-11-21 20:26:54 +00:00
|
|
|
}
|
|
|
|
|
2016-12-23 13:21:01 +00:00
|
|
|
void roundRect(Painter &p, int32 x, int32 y, int32 w, int32 h, style::color bg, const CornersPixmaps &corner, const style::color *shadow, RectParts parts) {
|
2017-07-13 14:42:46 +00:00
|
|
|
auto cornerWidth = corner.p[0].width() / cIntRetinaFactor();
|
|
|
|
auto cornerHeight = corner.p[0].height() / cIntRetinaFactor();
|
2016-11-12 15:02:19 +00:00
|
|
|
if (w < 2 * cornerWidth || h < 2 * cornerHeight) return;
|
|
|
|
if (w > 2 * cornerWidth) {
|
|
|
|
if (parts & RectPart::Top) {
|
|
|
|
p.fillRect(x + cornerWidth, y, w - 2 * cornerWidth, cornerHeight, bg);
|
|
|
|
}
|
|
|
|
if (parts & RectPart::Bottom) {
|
|
|
|
p.fillRect(x + cornerWidth, y + h - cornerHeight, w - 2 * cornerWidth, cornerHeight, bg);
|
|
|
|
if (shadow) {
|
|
|
|
p.fillRect(x + cornerWidth, y + h, w - 2 * cornerWidth, st::msgShadow, *shadow);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (h > 2 * cornerHeight) {
|
2017-08-31 16:28:58 +00:00
|
|
|
if ((parts & RectPart::NoTopBottom) == RectPart::NoTopBottom) {
|
2016-11-12 15:02:19 +00:00
|
|
|
p.fillRect(x, y + cornerHeight, w, h - 2 * cornerHeight, bg);
|
|
|
|
} else {
|
|
|
|
if (parts & RectPart::Left) {
|
|
|
|
p.fillRect(x, y + cornerHeight, cornerWidth, h - 2 * cornerHeight, bg);
|
|
|
|
}
|
|
|
|
if ((parts & RectPart::Center) && w > 2 * cornerWidth) {
|
|
|
|
p.fillRect(x + cornerWidth, y + cornerHeight, w - 2 * cornerWidth, h - 2 * cornerHeight, bg);
|
|
|
|
}
|
|
|
|
if (parts & RectPart::Right) {
|
|
|
|
p.fillRect(x + w - cornerWidth, y + cornerHeight, cornerWidth, h - 2 * cornerHeight, bg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (parts & RectPart::TopLeft) {
|
2017-07-13 14:42:46 +00:00
|
|
|
p.drawPixmap(x, y, corner.p[0]);
|
2016-11-12 15:02:19 +00:00
|
|
|
}
|
|
|
|
if (parts & RectPart::TopRight) {
|
2017-07-13 14:42:46 +00:00
|
|
|
p.drawPixmap(x + w - cornerWidth, y, corner.p[1]);
|
2016-11-12 15:02:19 +00:00
|
|
|
}
|
|
|
|
if (parts & RectPart::BottomLeft) {
|
2017-07-13 14:42:46 +00:00
|
|
|
p.drawPixmap(x, y + h - cornerHeight, corner.p[2]);
|
2015-05-20 19:28:24 +00:00
|
|
|
}
|
2016-11-12 15:02:19 +00:00
|
|
|
if (parts & RectPart::BottomRight) {
|
2017-07-13 14:42:46 +00:00
|
|
|
p.drawPixmap(x + w - cornerWidth, y + h - cornerHeight, corner.p[3]);
|
2015-05-20 19:28:24 +00:00
|
|
|
}
|
2015-10-03 10:09:09 +00:00
|
|
|
}
|
|
|
|
|
2016-12-23 13:21:01 +00:00
|
|
|
void roundRect(Painter &p, int32 x, int32 y, int32 w, int32 h, style::color bg, RoundCorners index, const style::color *shadow, RectParts parts) {
|
2016-11-12 15:02:19 +00:00
|
|
|
roundRect(p, x, y, w, h, bg, ::corners[index], shadow, parts);
|
2015-05-20 19:28:24 +00:00
|
|
|
}
|
|
|
|
|
2016-12-23 13:21:01 +00:00
|
|
|
void roundShadow(Painter &p, int32 x, int32 y, int32 w, int32 h, style::color shadow, RoundCorners index, RectParts parts) {
|
2016-11-12 15:02:19 +00:00
|
|
|
auto &corner = ::corners[index];
|
2017-07-13 14:42:46 +00:00
|
|
|
auto cornerWidth = corner.p[0].width() / cIntRetinaFactor();
|
|
|
|
auto cornerHeight = corner.p[0].height() / cIntRetinaFactor();
|
2016-11-12 15:02:19 +00:00
|
|
|
if (parts & RectPart::Bottom) {
|
|
|
|
p.fillRect(x + cornerWidth, y + h, w - 2 * cornerWidth, st::msgShadow, shadow);
|
|
|
|
}
|
|
|
|
if (parts & RectPart::BottomLeft) {
|
|
|
|
p.fillRect(x, y + h - cornerHeight, cornerWidth, st::msgShadow, shadow);
|
2017-07-13 14:42:46 +00:00
|
|
|
p.drawPixmap(x, y + h - cornerHeight + st::msgShadow, corner.p[2]);
|
2016-11-12 15:02:19 +00:00
|
|
|
}
|
|
|
|
if (parts & RectPart::BottomRight) {
|
|
|
|
p.fillRect(x + w - cornerWidth, y + h - cornerHeight, cornerWidth, st::msgShadow, shadow);
|
2017-07-13 14:42:46 +00:00
|
|
|
p.drawPixmap(x + w - cornerWidth, y + h - cornerHeight + st::msgShadow, corner.p[3]);
|
2016-11-12 15:02:19 +00:00
|
|
|
}
|
2015-10-03 10:09:09 +00:00
|
|
|
}
|
|
|
|
|
2016-12-23 13:21:01 +00:00
|
|
|
void roundRect(Painter &p, int32 x, int32 y, int32 w, int32 h, style::color bg, ImageRoundRadius radius, RectParts parts) {
|
2016-11-12 15:02:19 +00:00
|
|
|
auto colorKey = ((uint32(bg->c.alpha()) & 0xFF) << 24) | ((uint32(bg->c.red()) & 0xFF) << 16) | ((uint32(bg->c.green()) & 0xFF) << 8) | ((uint32(bg->c.blue()) & 0xFF) << 24);
|
|
|
|
auto i = cornersMap.find(colorKey);
|
2015-10-03 10:09:09 +00:00
|
|
|
if (i == cornersMap.cend()) {
|
|
|
|
QImage images[4];
|
2016-07-07 12:35:14 +00:00
|
|
|
switch (radius) {
|
2016-07-13 17:34:57 +00:00
|
|
|
case ImageRoundRadius::Small: prepareCorners(SmallMaskCorners, st::buttonRadius, bg, nullptr, images); break;
|
2017-12-25 19:26:08 +00:00
|
|
|
case ImageRoundRadius::Large: prepareCorners(LargeMaskCorners, st::historyMessageRadius, bg, nullptr, images); break;
|
2016-07-19 10:54:43 +00:00
|
|
|
default: p.fillRect(x, y, w, h, bg); return;
|
2016-07-07 12:35:14 +00:00
|
|
|
}
|
2015-10-03 10:09:09 +00:00
|
|
|
|
|
|
|
CornersPixmaps pixmaps;
|
|
|
|
for (int j = 0; j < 4; ++j) {
|
2017-07-13 14:42:46 +00:00
|
|
|
pixmaps.p[j] = pixmapFromImageInPlace(std::move(images[j]));
|
|
|
|
pixmaps.p[j].setDevicePixelRatio(cRetinaFactor());
|
2015-10-03 10:09:09 +00:00
|
|
|
}
|
|
|
|
i = cornersMap.insert(colorKey, pixmaps);
|
|
|
|
}
|
2016-11-12 15:02:19 +00:00
|
|
|
roundRect(p, x, y, w, h, bg, i.value(), nullptr, parts);
|
2015-05-22 11:14:52 +00:00
|
|
|
}
|
|
|
|
|
2015-11-20 13:34:37 +00:00
|
|
|
}
|