tdesktop/Telegram/SourceFiles/history/history_media_grouped.cpp

464 lines
13 KiB
C++
Raw Normal View History

2017-12-13 18:10:48 +00:00
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
2017-12-13 18:10:48 +00:00
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
2017-12-13 18:10:48 +00:00
*/
#include "history/history_media_grouped.h"
#include "history/history_item_components.h"
2017-12-13 18:10:48 +00:00
#include "history/history_media_types.h"
#include "history/history_message.h"
#include "history/view/history_view_message.h"
2017-12-13 18:10:48 +00:00
#include "storage/storage_shared_media.h"
#include "lang/lang_keys.h"
#include "ui/grouped_layout.h"
#include "ui/text_options.h"
2017-12-13 18:10:48 +00:00
#include "styles/style_history.h"
HistoryGroupedMedia::Element::Element(not_null<HistoryItem*> item)
: item(item) {
}
HistoryGroupedMedia::HistoryGroupedMedia(
not_null<HistoryItem*> parent,
const std::vector<not_null<HistoryItem*>> &others)
: HistoryMedia(parent)
, _caption(st::minPhotoSize - st::msgPadding.left() - st::msgPadding.right()) {
2017-12-13 18:10:48 +00:00
const auto result = applyGroup(others);
Ensures(result);
}
2017-12-19 16:57:42 +00:00
std::unique_ptr<HistoryMedia> HistoryGroupedMedia::clone(
not_null<HistoryItem*> newParent,
not_null<HistoryItem*> realParent) const {
return main()->clone(newParent, realParent);
}
2017-12-13 18:10:48 +00:00
void HistoryGroupedMedia::initDimensions() {
if (_caption.hasSkipBlock()) {
_caption.setSkipBlock(
_parent->skipBlockWidth(),
_parent->skipBlockHeight());
}
2017-12-13 18:10:48 +00:00
std::vector<QSize> sizes;
sizes.reserve(_elements.size());
for (const auto &element : _elements) {
const auto &media = element.content;
media->initDimensions();
sizes.push_back(media->sizeForGrouping());
}
2017-12-19 16:57:42 +00:00
const auto layout = Ui::LayoutMediaGroup(
2017-12-13 18:10:48 +00:00
sizes,
st::historyGroupWidthMax,
st::historyGroupWidthMin,
st::historyGroupSkip);
Assert(layout.size() == _elements.size());
_maxw = _minh = 0;
for (auto i = 0, count = int(layout.size()); i != count; ++i) {
const auto &item = layout[i];
accumulate_max(_maxw, item.geometry.x() + item.geometry.width());
accumulate_max(_minh, item.geometry.y() + item.geometry.height());
_elements[i].initialGeometry = item.geometry;
_elements[i].sides = item.sides;
}
if (!_caption.isEmpty()) {
auto captionw = _maxw - st::msgPadding.left() - st::msgPadding.right();
_minh += st::mediaCaptionSkip + _caption.countHeight(captionw);
if (isBubbleBottom()) {
_minh += st::msgPadding.bottom();
}
}
2017-12-13 18:10:48 +00:00
}
int HistoryGroupedMedia::resizeGetHeight(int width) {
_width = std::min(width, _maxw);
2017-12-13 18:10:48 +00:00
_height = 0;
if (_width < st::historyGroupWidthMin) {
return _height;
}
const auto initialSpacing = st::historyGroupSkip;
const auto factor = _width / float64(_maxw);
2017-12-13 18:10:48 +00:00
const auto scale = [&](int value) {
return int(std::round(value * factor));
};
const auto spacing = scale(initialSpacing);
for (auto &element : _elements) {
const auto sides = element.sides;
const auto initialGeometry = element.initialGeometry;
const auto needRightSkip = !(sides & RectPart::Right);
const auto needBottomSkip = !(sides & RectPart::Bottom);
const auto initialLeft = initialGeometry.x();
const auto initialTop = initialGeometry.y();
const auto initialRight = initialLeft
+ initialGeometry.width()
+ (needRightSkip ? initialSpacing : 0);
const auto initialBottom = initialTop
+ initialGeometry.height()
+ (needBottomSkip ? initialSpacing : 0);
const auto left = scale(initialLeft);
const auto top = scale(initialTop);
const auto width = scale(initialRight)
- left
- (needRightSkip ? spacing : 0);
const auto height = scale(initialBottom)
- top
- (needBottomSkip ? spacing : 0);
element.geometry = QRect(left, top, width, height);
accumulate_max(_height, top + height);
}
if (!_caption.isEmpty()) {
const auto captionw = _width - st::msgPadding.left() - st::msgPadding.right();
_height += st::mediaCaptionSkip + _caption.countHeight(captionw);
if (isBubbleBottom()) {
_height += st::msgPadding.bottom();
}
}
2017-12-13 18:10:48 +00:00
return _height;
}
void HistoryGroupedMedia::refreshParentId(
not_null<HistoryItem*> realParent) {
for (const auto &element : _elements) {
element.content->refreshParentId(element.item);
}
}
2017-12-13 18:10:48 +00:00
void HistoryGroupedMedia::draw(
Painter &p,
const QRect &clip,
TextSelection selection,
TimeMs ms) const {
for (auto i = 0, count = int(_elements.size()); i != count; ++i) {
const auto &element = _elements[i];
const auto elementSelection = (selection == FullSelection)
? FullSelection
: IsGroupItemSelection(selection, i)
? FullSelection
: TextSelection();
2017-12-19 16:57:42 +00:00
auto corners = Ui::GetCornersFromSides(element.sides);
2017-12-13 18:10:48 +00:00
if (!isBubbleTop()) {
corners &= ~(RectPart::TopLeft | RectPart::TopRight);
}
if (!isBubbleBottom() || !_caption.isEmpty()) {
corners &= ~(RectPart::BottomLeft | RectPart::BottomRight);
}
element.content->drawGrouped(
p,
clip,
elementSelection,
2017-12-13 18:10:48 +00:00
ms,
element.geometry,
corners,
&element.cacheKey,
&element.cache);
}
// date
const auto selected = (selection == FullSelection);
if (!_caption.isEmpty()) {
const auto captionw = _width - st::msgPadding.left() - st::msgPadding.right();
const auto outbg = _parent->hasOutLayout();
const auto captiony = _height
- (isBubbleBottom() ? st::msgPadding.bottom() : 0)
- _caption.countHeight(captionw);
p.setPen(outbg ? (selected ? st::historyTextOutFgSelected : st::historyTextOutFg) : (selected ? st::historyTextInFgSelected : st::historyTextInFg));
_caption.draw(p, st::msgPadding.left(), captiony, captionw, style::al_left, 0, -1, selection);
} else if (_parent->getMedia() == this) {
auto fullRight = _width;
auto fullBottom = _height;
if (needInfoDisplay()) {
_parent->drawInfo(p, fullRight, fullBottom, _width, selected, InfoDisplayOverImage);
}
if (!_parent->hasBubble() && _parent->displayRightAction()) {
auto fastShareLeft = (fullRight + st::historyFastShareLeft);
auto fastShareTop = (fullBottom - st::historyFastShareBottom - st::historyFastShareSize);
_parent->drawRightAction(p, fastShareLeft, fastShareTop, _width);
}
}
2017-12-13 18:10:48 +00:00
}
2017-12-16 07:20:04 +00:00
HistoryTextState HistoryGroupedMedia::getElementState(
2017-12-13 18:10:48 +00:00
QPoint point,
HistoryStateRequest request) const {
for (const auto &element : _elements) {
if (element.geometry.contains(point)) {
auto result = element.content->getStateGrouped(
2017-12-13 18:10:48 +00:00
element.geometry,
point,
request);
result.itemId = element.item->fullId();
return result;
}
}
2017-12-16 07:20:04 +00:00
return HistoryTextState(_parent);
}
HistoryTextState HistoryGroupedMedia::getState(
QPoint point,
HistoryStateRequest request) const {
auto result = getElementState(point, request);
if (!result.link && !_caption.isEmpty()) {
const auto captionw = _width - st::msgPadding.left() - st::msgPadding.right();
const auto captiony = _height
- (isBubbleBottom() ? st::msgPadding.bottom() : 0)
- _caption.countHeight(captionw);
if (QRect(st::msgPadding.left(), captiony, captionw, _height - captiony).contains(point)) {
return HistoryTextState(_parent, _caption.getState(
point - QPoint(st::msgPadding.left(), captiony),
captionw,
request.forText()));
}
2017-12-16 07:20:04 +00:00
} else if (_parent->getMedia() == this) {
auto fullRight = _width;
auto fullBottom = _height;
if (_parent->pointInTime(fullRight, fullBottom, point, InfoDisplayOverImage)) {
result.cursor = HistoryInDateCursorState;
}
if (!_parent->hasBubble() && _parent->displayRightAction()) {
auto fastShareLeft = (fullRight + st::historyFastShareLeft);
auto fastShareTop = (fullBottom - st::historyFastShareBottom - st::historyFastShareSize);
if (QRect(fastShareLeft, fastShareTop, st::historyFastShareSize, st::historyFastShareSize).contains(point)) {
result.link = _parent->rightActionLink();
}
2017-12-13 18:10:48 +00:00
}
}
2017-12-16 07:20:04 +00:00
return result;
2017-12-13 18:10:48 +00:00
}
bool HistoryGroupedMedia::toggleSelectionByHandlerClick(
const ClickHandlerPtr &p) const {
for (const auto &element : _elements) {
if (element.content->toggleSelectionByHandlerClick(p)) {
return true;
}
}
return false;
}
bool HistoryGroupedMedia::dragItemByHandler(const ClickHandlerPtr &p) const {
for (const auto &element : _elements) {
if (element.content->dragItemByHandler(p)) {
return true;
}
}
return false;
}
TextSelection HistoryGroupedMedia::adjustSelection(
TextSelection selection,
TextSelectType type) const {
return _caption.adjustSelection(selection, type);
}
QString HistoryGroupedMedia::notificationText() const {
return WithCaptionNotificationText(lang(lng_in_dlg_photo), _caption);
}
QString HistoryGroupedMedia::inDialogsText() const {
return WithCaptionDialogsText(lang(lng_in_dlg_album), _caption);
}
2017-12-13 18:10:48 +00:00
TextWithEntities HistoryGroupedMedia::selectedText(
TextSelection selection) const {
if (!IsSubGroupSelection(selection)) {
return WithCaptionSelectedText(
lang(lng_in_dlg_album),
_caption,
selection);
} else if (IsGroupItemSelection(selection, int(_elements.size()) - 1)) {
return main()->selectedText(FullSelection);
}
return TextWithEntities();
2017-12-13 18:10:48 +00:00
}
void HistoryGroupedMedia::clickHandlerActiveChanged(
const ClickHandlerPtr &p,
bool active) {
for (const auto &element : _elements) {
element.content->clickHandlerActiveChanged(p, active);
}
}
void HistoryGroupedMedia::clickHandlerPressedChanged(
const ClickHandlerPtr &p,
bool pressed) {
for (const auto &element : _elements) {
element.content->clickHandlerPressedChanged(p, pressed);
if (pressed && element.content->dragItemByHandler(p)) {
// #TODO pressedLinkItem
//App::pressedLinkItem(element.item);
}
2017-12-13 18:10:48 +00:00
}
}
void HistoryGroupedMedia::attachToParent() {
for (const auto &element : _elements) {
element.content->attachToParent();
}
}
void HistoryGroupedMedia::detachFromParent() {
for (const auto &element : _elements) {
if (element.content) {
element.content->detachFromParent();
}
}
}
std::unique_ptr<HistoryMedia> HistoryGroupedMedia::takeLastFromGroup() {
return std::move(_elements.back().content);
}
bool HistoryGroupedMedia::applyGroup(
const std::vector<not_null<HistoryItem*>> &others) {
if (others.empty()) {
return false;
}
const auto pushElement = [&](not_null<HistoryItem*> item) {
const auto media = item->getMedia();
Assert(media != nullptr && media->canBeGrouped());
_elements.push_back(Element(item));
_elements.back().content = item->getMedia()->clone(_parent, item);
2017-12-13 18:10:48 +00:00
};
if (_elements.empty()) {
pushElement(_parent);
} else if (validateGroupElements(others)) {
return true;
}
// We're updating other elements, so we just need to preserve the main.
auto mainElement = std::move(_elements.back());
_elements.erase(_elements.begin(), _elements.end());
_elements.reserve(others.size() + 1);
for (const auto item : others) {
pushElement(item);
}
_elements.push_back(std::move(mainElement));
_parent->setPendingInitDimensions();
return true;
}
bool HistoryGroupedMedia::validateGroupElements(
const std::vector<not_null<HistoryItem*>> &others) const {
if (_elements.size() != others.size() + 1) {
return false;
}
for (auto i = 0, count = int(others.size()); i != count; ++i) {
if (_elements[i].item != others[i]) {
return false;
}
}
return true;
}
not_null<HistoryMedia*> HistoryGroupedMedia::main() const {
Expects(!_elements.empty());
return _elements.back().content.get();
}
bool HistoryGroupedMedia::hasReplyPreview() const {
return main()->hasReplyPreview();
}
ImagePtr HistoryGroupedMedia::replyPreview() {
return main()->replyPreview();
}
TextWithEntities HistoryGroupedMedia::getCaption() const {
return main()->getCaption();
}
2017-12-13 18:10:48 +00:00
Storage::SharedMediaTypesMask HistoryGroupedMedia::sharedMediaTypes() const {
return main()->sharedMediaTypes();
}
2017-12-19 16:57:42 +00:00
void HistoryGroupedMedia::updateSentMedia(const MTPMessageMedia &media) {
return main()->updateSentMedia(media);
}
bool HistoryGroupedMedia::needReSetInlineResultMedia(
const MTPMessageMedia &media) {
return main()->needReSetInlineResultMedia(media);
}
PhotoData *HistoryGroupedMedia::getPhoto() const {
return main()->getPhoto();
}
DocumentData *HistoryGroupedMedia::getDocument() const {
return main()->getDocument();
}
HistoryMessageEdited *HistoryGroupedMedia::displayedEditBadge() const {
if (!_caption.isEmpty()) {
return _elements.front().item->Get<HistoryMessageEdited>();
}
return nullptr;
}
2017-12-13 18:10:48 +00:00
void HistoryGroupedMedia::updateNeedBubbleState() {
const auto getItemCaption = [](const Element &element) {
if (const auto media = element.item->getMedia()) {
return media->getCaption();
}
return element.content->getCaption();
};
const auto captionText = [&] {
auto result = getItemCaption(_elements.front());
if (result.text.isEmpty()) {
return result;
}
for (auto i = 1, count = int(_elements.size()); i != count; ++i) {
if (!getItemCaption(_elements[i]).text.isEmpty()) {
return TextWithEntities();
2017-12-13 18:10:48 +00:00
}
}
return result;
2017-12-13 18:10:48 +00:00
}();
_caption.setText(
st::messageTextStyle,
captionText.text + _parent->skipBlock(),
Ui::ItemTextNoMonoOptions(_parent));
2017-12-13 18:10:48 +00:00
_needBubble = computeNeedBubble();
}
bool HistoryGroupedMedia::needsBubble() const {
return _needBubble;
}
2017-12-25 14:16:44 +00:00
bool HistoryGroupedMedia::canEditCaption() const {
return main()->canEditCaption();
}
2017-12-13 18:10:48 +00:00
bool HistoryGroupedMedia::computeNeedBubble() const {
if (!_caption.isEmpty()) {
return true;
}
2017-12-16 13:09:53 +00:00
if (const auto message = _parent->toHistoryMessage()) {
if (message->viaBot()
|| message->Has<HistoryMessageReply>()
|| message->displayForwardedFrom()
|| message->displayFromName()) {
return true;
2017-12-13 18:10:48 +00:00
}
}
return false;
}
bool HistoryGroupedMedia::needInfoDisplay() const {
return (_parent->id < 0 || _parent->isUnderCursor());
}