2016-10-20 19:48:35 +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.
|
2016-10-20 19:48:35 +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
|
2016-10-20 19:48:35 +00:00
|
|
|
*/
|
|
|
|
#include "ui/widgets/multi_select.h"
|
|
|
|
|
|
|
|
#include "styles/style_widgets.h"
|
2016-11-11 13:46:04 +00:00
|
|
|
#include "ui/widgets/buttons.h"
|
2016-11-15 11:56:49 +00:00
|
|
|
#include "ui/widgets/input_fields.h"
|
2016-11-16 10:44:06 +00:00
|
|
|
#include "ui/widgets/scroll_area.h"
|
2016-11-21 17:46:29 +00:00
|
|
|
#include "ui/effects/cross_animation.h"
|
2020-10-10 09:15:37 +00:00
|
|
|
#include "ui/text/text_options.h"
|
2021-05-07 14:33:53 +00:00
|
|
|
#include "ui/ui_utility.h"
|
2017-04-13 08:27:10 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2016-10-20 19:48:35 +00:00
|
|
|
|
|
|
|
namespace Ui {
|
2016-10-21 12:28:26 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr int kWideScale = 3;
|
|
|
|
|
2022-03-20 07:20:43 +00:00
|
|
|
class Item {
|
2022-03-20 07:12:03 +00:00
|
|
|
public:
|
2022-03-20 07:20:43 +00:00
|
|
|
Item(
|
|
|
|
const style::MultiSelectItem &st,
|
|
|
|
uint64 id,
|
|
|
|
const QString &text,
|
|
|
|
style::color color,
|
|
|
|
MultiSelect::PaintRoundImage &&paintRoundImage);
|
|
|
|
|
|
|
|
uint64 id() const {
|
|
|
|
return _id;
|
|
|
|
}
|
|
|
|
int getWidth() const {
|
|
|
|
return _width;
|
|
|
|
}
|
|
|
|
QRect rect() const {
|
|
|
|
return QRect(_x, _y, _width, _st.height);
|
|
|
|
}
|
|
|
|
bool isOverDelete() const {
|
|
|
|
return _overDelete;
|
|
|
|
}
|
|
|
|
void setActive(bool active) {
|
|
|
|
_active = active;
|
|
|
|
}
|
|
|
|
void setPosition(int x, int y, int outerWidth, int maxVisiblePadding);
|
|
|
|
QRect paintArea(int outerWidth) const;
|
2022-03-20 07:12:03 +00:00
|
|
|
|
2022-03-20 07:20:43 +00:00
|
|
|
void setUpdateCallback(Fn<void()> updateCallback) {
|
|
|
|
_updateCallback = updateCallback;
|
|
|
|
}
|
|
|
|
void setText(const QString &text);
|
|
|
|
void paint(Painter &p, int outerWidth);
|
2022-03-20 07:12:03 +00:00
|
|
|
|
2022-03-20 07:20:43 +00:00
|
|
|
void mouseMoveEvent(QPoint point);
|
|
|
|
void leaveEvent();
|
2022-03-20 07:12:03 +00:00
|
|
|
|
2022-03-20 07:20:43 +00:00
|
|
|
void showAnimated() {
|
|
|
|
setVisibleAnimated(true);
|
|
|
|
}
|
|
|
|
void hideAnimated() {
|
|
|
|
setVisibleAnimated(false);
|
|
|
|
}
|
|
|
|
bool hideFinished() const {
|
|
|
|
return (_hiding && !_visibility.animating());
|
|
|
|
}
|
2022-03-20 07:12:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
private:
|
2022-03-20 07:20:43 +00:00
|
|
|
void setOver(bool over);
|
|
|
|
void paintOnce(Painter &p, int x, int y, int outerWidth);
|
|
|
|
void paintDeleteButton(
|
|
|
|
Painter &p,
|
|
|
|
int x,
|
|
|
|
int y,
|
|
|
|
int outerWidth,
|
|
|
|
float64 overOpacity);
|
|
|
|
bool paintCached(Painter &p, int x, int y, int outerWidth);
|
|
|
|
void prepareCache();
|
|
|
|
void setVisibleAnimated(bool visible);
|
|
|
|
|
|
|
|
const style::MultiSelectItem &_st;
|
|
|
|
|
|
|
|
uint64 _id;
|
|
|
|
struct SlideAnimation {
|
|
|
|
SlideAnimation(
|
|
|
|
Fn<void()> updateCallback,
|
|
|
|
int fromX,
|
|
|
|
int toX,
|
|
|
|
int y,
|
|
|
|
float64 duration)
|
|
|
|
: fromX(fromX)
|
|
|
|
, toX(toX)
|
|
|
|
, y(y) {
|
|
|
|
x.start(updateCallback, fromX, toX, duration);
|
|
|
|
}
|
|
|
|
Ui::Animations::Simple x;
|
|
|
|
int fromX, toX;
|
|
|
|
int y;
|
2022-03-20 07:12:03 +00:00
|
|
|
};
|
2022-03-20 07:20:43 +00:00
|
|
|
std::vector<SlideAnimation> _copies;
|
|
|
|
int _x = -1;
|
|
|
|
int _y = -1;
|
|
|
|
int _width = 0;
|
|
|
|
Text::String _text;
|
|
|
|
style::color _color;
|
|
|
|
bool _over = false;
|
|
|
|
QPixmap _cache;
|
|
|
|
Ui::Animations::Simple _visibility;
|
|
|
|
Ui::Animations::Simple _overOpacity;
|
2022-03-20 07:12:03 +00:00
|
|
|
bool _overDelete = false;
|
2022-03-20 07:20:43 +00:00
|
|
|
bool _active = false;
|
|
|
|
MultiSelect::PaintRoundImage _paintRoundImage;
|
|
|
|
Fn<void()> _updateCallback;
|
|
|
|
bool _hiding = false;
|
2022-03-20 07:12:03 +00:00
|
|
|
|
|
|
|
};
|
2016-10-21 12:28:26 +00:00
|
|
|
|
2022-03-20 07:20:43 +00:00
|
|
|
Item::Item(
|
|
|
|
const style::MultiSelectItem &st,
|
|
|
|
uint64 id,
|
|
|
|
const QString &text,
|
|
|
|
style::color color,
|
|
|
|
MultiSelect::PaintRoundImage &&paintRoundImage)
|
2016-10-21 12:28:26 +00:00
|
|
|
: _st(st)
|
|
|
|
, _id(id)
|
|
|
|
, _color(color)
|
2017-02-21 13:45:56 +00:00
|
|
|
, _paintRoundImage(std::move(paintRoundImage)) {
|
2016-10-21 12:28:26 +00:00
|
|
|
setText(text);
|
|
|
|
}
|
|
|
|
|
2022-03-20 07:20:43 +00:00
|
|
|
void Item::setText(const QString &text) {
|
2017-12-28 13:06:06 +00:00
|
|
|
_text.setText(_st.style, text, NameTextOptions());
|
2022-03-20 07:20:43 +00:00
|
|
|
_width = _st.height
|
|
|
|
+ _st.padding.left()
|
|
|
|
+ _text.maxWidth()
|
|
|
|
+ _st.padding.right();
|
2016-10-21 12:28:26 +00:00
|
|
|
accumulate_min(_width, _st.maxWidth);
|
|
|
|
}
|
|
|
|
|
2022-03-20 07:20:43 +00:00
|
|
|
void Item::paint(Painter &p, int outerWidth) {
|
2019-04-02 09:13:30 +00:00
|
|
|
if (!_cache.isNull() && !_visibility.animating()) {
|
2016-10-21 12:28:26 +00:00
|
|
|
if (_hiding) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
_cache = QPixmap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_copies.empty()) {
|
2019-04-02 09:13:30 +00:00
|
|
|
paintOnce(p, _x, _y, outerWidth);
|
2016-10-21 12:28:26 +00:00
|
|
|
} else {
|
|
|
|
for (auto i = _copies.begin(), e = _copies.end(); i != e;) {
|
2019-04-02 09:13:30 +00:00
|
|
|
auto x = qRound(i->x.value(_x));
|
2016-10-21 12:28:26 +00:00
|
|
|
auto y = i->y;
|
|
|
|
auto animating = i->x.animating();
|
|
|
|
if (animating || (y == _y)) {
|
2019-04-02 09:13:30 +00:00
|
|
|
paintOnce(p, x, y, outerWidth);
|
2016-10-21 12:28:26 +00:00
|
|
|
}
|
|
|
|
if (animating) {
|
|
|
|
++i;
|
|
|
|
} else {
|
|
|
|
i = _copies.erase(i);
|
|
|
|
e = _copies.end();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-20 07:20:43 +00:00
|
|
|
void Item::paintOnce(Painter &p, int x, int y, int outerWidth) {
|
2016-10-21 12:28:26 +00:00
|
|
|
if (!_cache.isNull()) {
|
|
|
|
paintCached(p, x, y, outerWidth);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto radius = _st.height / 2;
|
2022-03-20 07:20:43 +00:00
|
|
|
auto inner = style::rtlrect(
|
|
|
|
x + radius,
|
|
|
|
y,
|
|
|
|
_width - radius,
|
|
|
|
_st.height,
|
|
|
|
outerWidth);
|
2016-10-21 12:28:26 +00:00
|
|
|
|
|
|
|
auto clipEnabled = p.hasClipping();
|
|
|
|
auto clip = clipEnabled ? p.clipRegion() : QRegion();
|
|
|
|
p.setClipRect(inner);
|
|
|
|
|
|
|
|
p.setPen(Qt::NoPen);
|
|
|
|
p.setBrush(_active ? _st.textActiveBg : _st.textBg);
|
2016-12-03 12:10:35 +00:00
|
|
|
{
|
|
|
|
PainterHighQualityEnabler hq(p);
|
2022-03-20 07:20:43 +00:00
|
|
|
p.drawRoundedRect(
|
|
|
|
style::rtlrect(x, y, _width, _st.height, outerWidth),
|
|
|
|
radius,
|
|
|
|
radius);
|
2016-12-03 12:10:35 +00:00
|
|
|
}
|
2016-10-21 12:28:26 +00:00
|
|
|
|
|
|
|
if (clipEnabled) {
|
|
|
|
p.setClipRegion(clip);
|
|
|
|
} else {
|
|
|
|
p.setClipping(false);
|
|
|
|
}
|
|
|
|
|
2019-04-02 09:13:30 +00:00
|
|
|
auto overOpacity = _overOpacity.value(_over ? 1. : 0.);
|
2016-10-21 12:28:26 +00:00
|
|
|
if (overOpacity < 1.) {
|
|
|
|
_paintRoundImage(p, x, y, outerWidth, _st.height);
|
|
|
|
}
|
|
|
|
if (overOpacity > 0.) {
|
|
|
|
paintDeleteButton(p, x, y, outerWidth, overOpacity);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto textLeft = _st.height + _st.padding.left();
|
|
|
|
auto textWidth = _width - textLeft - _st.padding.right();
|
|
|
|
p.setPen(_active ? _st.textActiveFg : _st.textFg);
|
2022-03-20 07:20:43 +00:00
|
|
|
_text.drawLeftElided(
|
|
|
|
p,
|
|
|
|
x + textLeft,
|
|
|
|
y + _st.padding.top(),
|
|
|
|
textWidth,
|
|
|
|
outerWidth);
|
2016-10-21 12:28:26 +00:00
|
|
|
}
|
|
|
|
|
2022-03-20 07:20:43 +00:00
|
|
|
void Item::paintDeleteButton(
|
|
|
|
Painter &p,
|
|
|
|
int x,
|
|
|
|
int y,
|
|
|
|
int outerWidth,
|
|
|
|
float64 overOpacity) {
|
2016-10-21 12:28:26 +00:00
|
|
|
p.setOpacity(overOpacity);
|
2016-11-21 17:46:29 +00:00
|
|
|
|
2016-10-21 12:28:26 +00:00
|
|
|
p.setPen(Qt::NoPen);
|
|
|
|
p.setBrush(_color);
|
2016-12-03 12:10:35 +00:00
|
|
|
{
|
|
|
|
PainterHighQualityEnabler hq(p);
|
2022-03-20 07:20:43 +00:00
|
|
|
p.drawEllipse(
|
|
|
|
style::rtlrect(x, y, _st.height, _st.height, outerWidth));
|
2016-12-03 12:10:35 +00:00
|
|
|
}
|
2016-10-21 12:28:26 +00:00
|
|
|
|
2022-03-20 07:20:43 +00:00
|
|
|
CrossAnimation::paint(
|
|
|
|
p,
|
|
|
|
_st.deleteCross,
|
|
|
|
_st.deleteFg,
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
outerWidth,
|
|
|
|
overOpacity);
|
2016-10-21 12:28:26 +00:00
|
|
|
|
|
|
|
p.setOpacity(1.);
|
|
|
|
}
|
|
|
|
|
2022-03-20 07:20:43 +00:00
|
|
|
bool Item::paintCached(Painter &p, int x, int y, int outerWidth) {
|
2016-12-03 12:10:35 +00:00
|
|
|
PainterHighQualityEnabler hq(p);
|
|
|
|
|
2019-04-02 09:13:30 +00:00
|
|
|
auto opacity = _visibility.value(_hiding ? 0. : 1.);
|
2016-10-21 12:28:26 +00:00
|
|
|
auto height = opacity * _cache.height() / _cache.devicePixelRatio();
|
|
|
|
auto width = opacity * _cache.width() / _cache.devicePixelRatio();
|
|
|
|
|
|
|
|
p.setOpacity(opacity);
|
2022-03-20 07:20:43 +00:00
|
|
|
p.drawPixmap(
|
|
|
|
style::rtlrect(
|
|
|
|
x + (_width - width) / 2.,
|
|
|
|
y + (_st.height - height) / 2.,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
outerWidth),
|
|
|
|
_cache);
|
2016-10-21 12:28:26 +00:00
|
|
|
p.setOpacity(1.);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-03-20 07:20:43 +00:00
|
|
|
void Item::mouseMoveEvent(QPoint point) {
|
|
|
|
if (!_cache.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
2016-10-21 12:28:26 +00:00
|
|
|
_overDelete = QRect(0, 0, _st.height, _st.height).contains(point);
|
|
|
|
setOver(true);
|
|
|
|
}
|
|
|
|
|
2022-03-20 07:20:43 +00:00
|
|
|
void Item::leaveEvent() {
|
2016-10-21 12:28:26 +00:00
|
|
|
_overDelete = false;
|
|
|
|
setOver(false);
|
|
|
|
}
|
|
|
|
|
2022-03-20 07:20:43 +00:00
|
|
|
void Item::setPosition(int x, int y, int outerWidth, int maxVisiblePadding) {
|
2016-10-21 12:28:26 +00:00
|
|
|
if (_x >= 0 && _y >= 0 && (_x != x || _y != y)) {
|
|
|
|
// Make an animation if it is not the first setPosition().
|
|
|
|
auto found = false;
|
|
|
|
auto leftHidden = -_width - maxVisiblePadding;
|
|
|
|
auto rightHidden = outerWidth + maxVisiblePadding;
|
|
|
|
for (auto i = _copies.begin(), e = _copies.end(); i != e;) {
|
|
|
|
if (i->x.animating()) {
|
|
|
|
if (i->y == y) {
|
|
|
|
i->x.start(_updateCallback, i->toX, x, _st.duration);
|
|
|
|
found = true;
|
|
|
|
} else {
|
2022-03-20 07:20:43 +00:00
|
|
|
i->x.start(
|
|
|
|
_updateCallback,
|
|
|
|
i->fromX,
|
|
|
|
(i->toX > i->fromX) ? rightHidden : leftHidden,
|
|
|
|
_st.duration);
|
2016-10-21 12:28:26 +00:00
|
|
|
}
|
|
|
|
++i;
|
|
|
|
} else {
|
|
|
|
i = _copies.erase(i);
|
|
|
|
e = _copies.end();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_copies.empty()) {
|
|
|
|
if (_y == y) {
|
2022-03-20 07:20:43 +00:00
|
|
|
auto copy = SlideAnimation(
|
|
|
|
_updateCallback,
|
|
|
|
_x,
|
|
|
|
x,
|
|
|
|
_y,
|
|
|
|
_st.duration);
|
2017-02-21 13:45:56 +00:00
|
|
|
_copies.push_back(std::move(copy));
|
2016-10-21 12:28:26 +00:00
|
|
|
} else {
|
2022-03-20 07:20:43 +00:00
|
|
|
auto copyHiding = SlideAnimation(
|
|
|
|
_updateCallback,
|
|
|
|
_x,
|
|
|
|
(y > _y) ? rightHidden : leftHidden,
|
|
|
|
_y,
|
|
|
|
_st.duration);
|
2017-02-21 13:45:56 +00:00
|
|
|
_copies.push_back(std::move(copyHiding));
|
2022-03-20 07:20:43 +00:00
|
|
|
auto copyShowing = SlideAnimation(
|
|
|
|
_updateCallback,
|
|
|
|
(y > _y) ? leftHidden : rightHidden,
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
_st.duration);
|
2017-02-21 13:45:56 +00:00
|
|
|
_copies.push_back(std::move(copyShowing));
|
2016-10-21 12:28:26 +00:00
|
|
|
}
|
|
|
|
} else if (!found) {
|
2022-03-20 07:20:43 +00:00
|
|
|
auto copy = SlideAnimation(
|
|
|
|
_updateCallback,
|
|
|
|
(y > _y) ? leftHidden : rightHidden,
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
_st.duration);
|
2017-02-21 13:45:56 +00:00
|
|
|
_copies.push_back(std::move(copy));
|
2016-10-21 12:28:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_x = x;
|
|
|
|
_y = y;
|
|
|
|
}
|
|
|
|
|
2022-03-20 07:20:43 +00:00
|
|
|
QRect Item::paintArea(int outerWidth) const {
|
2016-10-21 12:28:26 +00:00
|
|
|
if (_copies.empty()) {
|
|
|
|
return rect();
|
|
|
|
}
|
|
|
|
auto yMin = 0, yMax = 0;
|
2021-09-03 11:09:57 +00:00
|
|
|
for (const auto © : _copies) {
|
2016-10-21 12:28:26 +00:00
|
|
|
accumulate_max(yMax, copy.y);
|
|
|
|
if (yMin) {
|
|
|
|
accumulate_min(yMin, copy.y);
|
|
|
|
} else {
|
|
|
|
yMin = copy.y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QRect(0, yMin, outerWidth, yMax - yMin + _st.height);
|
|
|
|
}
|
|
|
|
|
2022-03-20 07:20:43 +00:00
|
|
|
void Item::prepareCache() {
|
2016-10-21 12:28:26 +00:00
|
|
|
if (!_cache.isNull()) return;
|
|
|
|
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(!_visibility.animating());
|
2016-10-21 12:28:26 +00:00
|
|
|
auto cacheWidth = _width * kWideScale * cIntRetinaFactor();
|
|
|
|
auto cacheHeight = _st.height * kWideScale * cIntRetinaFactor();
|
2022-03-20 07:20:43 +00:00
|
|
|
auto data = QImage(
|
|
|
|
cacheWidth,
|
|
|
|
cacheHeight,
|
|
|
|
QImage::Format_ARGB32_Premultiplied);
|
2016-10-21 12:28:26 +00:00
|
|
|
data.fill(Qt::transparent);
|
|
|
|
data.setDevicePixelRatio(cRetinaFactor());
|
|
|
|
{
|
|
|
|
Painter p(&data);
|
2022-03-20 07:20:43 +00:00
|
|
|
paintOnce(
|
|
|
|
p,
|
|
|
|
_width * (kWideScale - 1) / 2,
|
|
|
|
_st.height * (kWideScale - 1) / 2,
|
|
|
|
cacheWidth);
|
2016-10-21 12:28:26 +00:00
|
|
|
}
|
2021-05-07 14:33:53 +00:00
|
|
|
_cache = Ui::PixmapFromImage(std::move(data));
|
2016-10-21 12:28:26 +00:00
|
|
|
}
|
|
|
|
|
2022-03-20 07:20:43 +00:00
|
|
|
void Item::setVisibleAnimated(bool visible) {
|
2016-10-21 12:28:26 +00:00
|
|
|
_hiding = !visible;
|
|
|
|
prepareCache();
|
|
|
|
auto from = visible ? 0. : 1.;
|
|
|
|
auto to = visible ? 1. : 0.;
|
2016-11-10 18:10:31 +00:00
|
|
|
auto transition = visible ? anim::bumpy(1.0625) : anim::linear;
|
2016-10-21 12:28:26 +00:00
|
|
|
_visibility.start(_updateCallback, from, to, _st.duration, transition);
|
|
|
|
}
|
|
|
|
|
2022-03-20 07:20:43 +00:00
|
|
|
void Item::setOver(bool over) {
|
2016-10-21 12:28:26 +00:00
|
|
|
if (over != _over) {
|
|
|
|
_over = over;
|
2022-03-20 07:20:43 +00:00
|
|
|
_overOpacity.start(
|
|
|
|
_updateCallback,
|
|
|
|
_over ? 0. : 1.,
|
|
|
|
_over ? 1. : 0.,
|
|
|
|
_st.duration);
|
2016-10-21 12:28:26 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-20 19:48:35 +00:00
|
|
|
|
2022-03-20 07:20:43 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
class MultiSelect::Inner : public TWidget {
|
|
|
|
public:
|
|
|
|
using ScrollCallback = Fn<void(int activeTop, int activeBottom)>;
|
|
|
|
Inner(
|
|
|
|
QWidget *parent,
|
|
|
|
const style::MultiSelect &st,
|
|
|
|
rpl::producer<QString> placeholder,
|
|
|
|
ScrollCallback callback);
|
|
|
|
|
|
|
|
QString getQuery() const;
|
|
|
|
bool setInnerFocus();
|
|
|
|
void clearQuery();
|
|
|
|
|
|
|
|
void setQueryChangedCallback(Fn<void(const QString &query)> callback);
|
|
|
|
void setSubmittedCallback(Fn<void(Qt::KeyboardModifiers)> callback);
|
|
|
|
void setCancelledCallback(Fn<void()> callback);
|
|
|
|
|
|
|
|
void addItemInBunch(std::unique_ptr<Item> item);
|
|
|
|
void finishItemsBunch(AddItemWay way);
|
|
|
|
void setItemText(uint64 itemId, const QString &text);
|
|
|
|
|
|
|
|
void setItemRemovedCallback(Fn<void(uint64 itemId)> callback);
|
|
|
|
void removeItem(uint64 itemId);
|
|
|
|
|
|
|
|
int getItemsCount() const;
|
|
|
|
QVector<uint64> getItems() const;
|
|
|
|
bool hasItem(uint64 itemId) const;
|
|
|
|
|
|
|
|
void setResizedCallback(Fn<void(int heightDelta)> callback);
|
|
|
|
|
|
|
|
~Inner();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
int resizeGetHeight(int newWidth) override;
|
|
|
|
|
|
|
|
void paintEvent(QPaintEvent *e) override;
|
|
|
|
void leaveEventHook(QEvent *e) override;
|
|
|
|
void mouseMoveEvent(QMouseEvent *e) override;
|
|
|
|
void mousePressEvent(QMouseEvent *e) override;
|
|
|
|
void keyPressEvent(QKeyEvent *e) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
void submitted(Qt::KeyboardModifiers modifiers);
|
|
|
|
void cancelled();
|
|
|
|
void queryChanged();
|
|
|
|
void fieldFocused();
|
|
|
|
void computeItemsGeometry(int newWidth);
|
|
|
|
void updateItemsGeometry();
|
|
|
|
void updateFieldGeometry();
|
|
|
|
void updateHasAnyItems(bool hasAnyItems);
|
|
|
|
void updateSelection(QPoint mousePosition);
|
|
|
|
void clearSelection() {
|
|
|
|
updateSelection(QPoint(-1, -1));
|
|
|
|
}
|
|
|
|
void updateCursor();
|
|
|
|
void updateHeightStep();
|
|
|
|
void finishHeightAnimation();
|
|
|
|
enum class ChangeActiveWay {
|
|
|
|
Default,
|
|
|
|
SkipSetFocus,
|
|
|
|
};
|
|
|
|
void setActiveItem(
|
|
|
|
int active,
|
|
|
|
ChangeActiveWay skipSetFocus = ChangeActiveWay::Default);
|
|
|
|
void setActiveItemPrevious();
|
|
|
|
void setActiveItemNext();
|
|
|
|
|
|
|
|
QMargins itemPaintMargins() const;
|
|
|
|
|
|
|
|
const style::MultiSelect &_st;
|
|
|
|
Ui::Animations::Simple _iconOpacity;
|
|
|
|
|
|
|
|
ScrollCallback _scrollCallback;
|
|
|
|
|
|
|
|
std::set<uint64> _idsMap;
|
|
|
|
std::vector<std::unique_ptr<Item>> _items;
|
|
|
|
std::set<std::unique_ptr<Item>> _removingItems;
|
|
|
|
|
|
|
|
int _selected = -1;
|
|
|
|
int _active = -1;
|
|
|
|
bool _overDelete = false;
|
|
|
|
|
|
|
|
int _fieldLeft = 0;
|
|
|
|
int _fieldTop = 0;
|
|
|
|
int _fieldWidth = 0;
|
|
|
|
object_ptr<Ui::InputField> _field;
|
|
|
|
object_ptr<Ui::CrossButton> _cancel;
|
|
|
|
|
|
|
|
int _newHeight = 0;
|
|
|
|
Ui::Animations::Simple _height;
|
|
|
|
|
|
|
|
Fn<void(const QString &query)> _queryChangedCallback;
|
|
|
|
Fn<void(Qt::KeyboardModifiers)> _submittedCallback;
|
|
|
|
Fn<void()> _cancelledCallback;
|
|
|
|
Fn<void(uint64 itemId)> _itemRemovedCallback;
|
|
|
|
Fn<void(int heightDelta)> _resizedCallback;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-09-13 16:57:44 +00:00
|
|
|
MultiSelect::MultiSelect(
|
|
|
|
QWidget *parent,
|
|
|
|
const style::MultiSelect &st,
|
2019-06-18 14:07:45 +00:00
|
|
|
rpl::producer<QString> placeholder)
|
2017-09-13 16:57:44 +00:00
|
|
|
: RpWidget(parent)
|
2016-10-20 19:48:35 +00:00
|
|
|
, _st(st)
|
2016-12-13 17:07:56 +00:00
|
|
|
, _scroll(this, _st.scroll) {
|
2019-06-18 14:07:45 +00:00
|
|
|
const auto scrollCallback = [=](int activeTop, int activeBottom) {
|
2016-12-13 17:07:56 +00:00
|
|
|
scrollTo(activeTop, activeBottom);
|
2019-06-18 14:07:45 +00:00
|
|
|
};
|
|
|
|
_inner = _scroll->setOwnedWidget(object_ptr<Inner>(
|
|
|
|
this,
|
|
|
|
st,
|
|
|
|
std::move(placeholder),
|
|
|
|
scrollCallback));
|
2016-10-21 12:28:26 +00:00
|
|
|
_scroll->installEventFilter(this);
|
|
|
|
_inner->setResizedCallback([this](int innerHeightDelta) {
|
|
|
|
auto newHeight = resizeGetHeight(width());
|
|
|
|
if (innerHeightDelta > 0) {
|
|
|
|
_scroll->scrollToY(_scroll->scrollTop() + innerHeightDelta);
|
|
|
|
}
|
|
|
|
if (newHeight != height()) {
|
|
|
|
resize(width(), newHeight);
|
|
|
|
if (_resizedCallback) {
|
|
|
|
_resizedCallback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
_inner->setQueryChangedCallback([this](const QString &query) {
|
|
|
|
_scroll->scrollToY(_scroll->scrollTopMax());
|
|
|
|
if (_queryChangedCallback) {
|
|
|
|
_queryChangedCallback(query);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-10-20 19:48:35 +00:00
|
|
|
setAttribute(Qt::WA_OpaquePaintEvent);
|
2016-12-02 19:16:35 +00:00
|
|
|
auto defaultWidth = _st.item.maxWidth + _st.fieldMinWidth + _st.fieldCancelSkip;
|
|
|
|
resizeToWidth(_st.padding.left() + defaultWidth + _st.padding.right());
|
2016-10-20 19:48:35 +00:00
|
|
|
}
|
|
|
|
|
2016-10-21 12:28:26 +00:00
|
|
|
bool MultiSelect::eventFilter(QObject *o, QEvent *e) {
|
|
|
|
if (o == _scroll && e->type() == QEvent::KeyPress) {
|
|
|
|
e->ignore();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultiSelect::scrollTo(int activeTop, int activeBottom) {
|
|
|
|
auto scrollTop = _scroll->scrollTop();
|
|
|
|
auto scrollHeight = _scroll->height();
|
|
|
|
auto scrollBottom = scrollTop + scrollHeight;
|
|
|
|
if (scrollTop > activeTop) {
|
|
|
|
_scroll->scrollToY(activeTop);
|
|
|
|
} else if (scrollBottom < activeBottom) {
|
|
|
|
_scroll->scrollToY(activeBottom - scrollHeight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-04 15:35:11 +00:00
|
|
|
void MultiSelect::setQueryChangedCallback(Fn<void(const QString &query)> callback) {
|
2017-02-21 13:45:56 +00:00
|
|
|
_queryChangedCallback = std::move(callback);
|
2016-10-20 19:48:35 +00:00
|
|
|
}
|
|
|
|
|
2018-06-04 15:35:11 +00:00
|
|
|
void MultiSelect::setSubmittedCallback(Fn<void(Qt::KeyboardModifiers)> callback) {
|
2017-02-21 13:45:56 +00:00
|
|
|
_inner->setSubmittedCallback(std::move(callback));
|
2016-10-20 19:48:35 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 11:23:54 +00:00
|
|
|
void MultiSelect::setCancelledCallback(Fn<void()> callback) {
|
|
|
|
_inner->setCancelledCallback(std::move(callback));
|
|
|
|
}
|
|
|
|
|
2018-06-04 15:35:11 +00:00
|
|
|
void MultiSelect::setResizedCallback(Fn<void()> callback) {
|
2017-02-21 13:45:56 +00:00
|
|
|
_resizedCallback = std::move(callback);
|
2016-10-21 12:28:26 +00:00
|
|
|
}
|
|
|
|
|
2016-10-20 19:48:35 +00:00
|
|
|
void MultiSelect::setInnerFocus() {
|
|
|
|
if (_inner->setInnerFocus()) {
|
|
|
|
_scroll->scrollToY(_scroll->scrollTopMax());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-21 12:28:26 +00:00
|
|
|
void MultiSelect::clearQuery() {
|
|
|
|
_inner->clearQuery();
|
2016-10-20 19:48:35 +00:00
|
|
|
}
|
|
|
|
|
2016-10-21 12:28:26 +00:00
|
|
|
QString MultiSelect::getQuery() const {
|
|
|
|
return _inner->getQuery();
|
2016-10-20 19:48:35 +00:00
|
|
|
}
|
|
|
|
|
2017-02-26 11:32:13 +00:00
|
|
|
void MultiSelect::addItem(uint64 itemId, const QString &text, style::color color, PaintRoundImage paintRoundImage, AddItemWay way) {
|
2017-03-17 12:05:50 +00:00
|
|
|
addItemInBunch(itemId, text, color, std::move(paintRoundImage));
|
|
|
|
_inner->finishItemsBunch(way);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultiSelect::addItemInBunch(uint64 itemId, const QString &text, style::color color, PaintRoundImage paintRoundImage) {
|
2017-08-01 15:55:51 +00:00
|
|
|
_inner->addItemInBunch(std::make_unique<Item>(_st.item, itemId, text, color, std::move(paintRoundImage)));
|
2017-03-17 12:05:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MultiSelect::finishItemsBunch() {
|
|
|
|
_inner->finishItemsBunch(AddItemWay::SkipAnimation);
|
2016-10-20 19:48:35 +00:00
|
|
|
}
|
|
|
|
|
2018-06-04 15:35:11 +00:00
|
|
|
void MultiSelect::setItemRemovedCallback(Fn<void(uint64 itemId)> callback) {
|
2017-02-21 13:45:56 +00:00
|
|
|
_inner->setItemRemovedCallback(std::move(callback));
|
2016-10-20 19:48:35 +00:00
|
|
|
}
|
|
|
|
|
2016-10-21 12:28:26 +00:00
|
|
|
void MultiSelect::removeItem(uint64 itemId) {
|
|
|
|
_inner->removeItem(itemId);
|
2016-10-20 19:48:35 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 12:05:50 +00:00
|
|
|
int MultiSelect::getItemsCount() const {
|
|
|
|
return _inner->getItemsCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVector<uint64> MultiSelect::getItems() const {
|
|
|
|
return _inner->getItems();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MultiSelect::hasItem(uint64 itemId) const {
|
|
|
|
return _inner->hasItem(itemId);
|
|
|
|
}
|
|
|
|
|
2016-10-21 12:28:26 +00:00
|
|
|
int MultiSelect::resizeGetHeight(int newWidth) {
|
|
|
|
if (newWidth != _inner->width()) {
|
|
|
|
_inner->resizeToWidth(newWidth);
|
|
|
|
}
|
|
|
|
auto newHeight = qMin(_inner->height(), _st.maxHeight);
|
|
|
|
_scroll->setGeometryToLeft(0, 0, newWidth, newHeight);
|
|
|
|
return newHeight;
|
2016-10-20 19:48:35 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 14:07:45 +00:00
|
|
|
MultiSelect::Inner::Inner(
|
|
|
|
QWidget *parent,
|
|
|
|
const style::MultiSelect &st,
|
|
|
|
rpl::producer<QString> placeholder,
|
|
|
|
ScrollCallback callback)
|
|
|
|
: TWidget(parent)
|
2016-10-20 19:48:35 +00:00
|
|
|
, _st(st)
|
2017-02-21 13:45:56 +00:00
|
|
|
, _scrollCallback(std::move(callback))
|
2017-05-30 15:21:05 +00:00
|
|
|
, _field(this, _st.field, std::move(placeholder))
|
2016-10-21 12:28:26 +00:00
|
|
|
, _cancel(this, _st.fieldCancel) {
|
|
|
|
_field->customUpDown(true);
|
2018-05-31 12:20:28 +00:00
|
|
|
connect(_field, &Ui::InputField::focused, [=] { fieldFocused(); });
|
|
|
|
connect(_field, &Ui::InputField::changed, [=] { queryChanged(); });
|
|
|
|
connect(_field, &Ui::InputField::submitted, this, &Inner::submitted);
|
2018-10-30 11:23:54 +00:00
|
|
|
connect(_field, &Ui::InputField::cancelled, this, &Inner::cancelled);
|
2018-05-31 12:20:28 +00:00
|
|
|
_cancel->setClickedCallback([=] {
|
2016-10-21 12:28:26 +00:00
|
|
|
clearQuery();
|
|
|
|
_field->setFocus();
|
2016-10-20 19:48:35 +00:00
|
|
|
});
|
2016-10-21 12:28:26 +00:00
|
|
|
setMouseTracking(true);
|
2016-10-20 19:48:35 +00:00
|
|
|
}
|
|
|
|
|
2018-05-31 12:20:28 +00:00
|
|
|
void MultiSelect::Inner::queryChanged() {
|
2016-10-20 19:48:35 +00:00
|
|
|
auto query = getQuery();
|
2017-11-27 11:43:57 +00:00
|
|
|
_cancel->toggle(!query.isEmpty(), anim::type::normal);
|
2016-10-21 12:28:26 +00:00
|
|
|
updateFieldGeometry();
|
2016-10-20 19:48:35 +00:00
|
|
|
if (_queryChangedCallback) {
|
|
|
|
_queryChangedCallback(query);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-21 12:28:26 +00:00
|
|
|
QString MultiSelect::Inner::getQuery() const {
|
|
|
|
return _field->getLastText().trimmed();
|
|
|
|
}
|
|
|
|
|
2016-10-20 19:48:35 +00:00
|
|
|
bool MultiSelect::Inner::setInnerFocus() {
|
2016-10-21 12:28:26 +00:00
|
|
|
if (_active >= 0) {
|
|
|
|
setFocus();
|
|
|
|
} else if (!_field->hasFocus()) {
|
2016-12-20 13:03:51 +00:00
|
|
|
_field->setFocusFast();
|
2016-10-20 19:48:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-21 12:28:26 +00:00
|
|
|
void MultiSelect::Inner::clearQuery() {
|
|
|
|
_field->setText(QString());
|
2016-10-20 19:48:35 +00:00
|
|
|
}
|
|
|
|
|
2018-06-04 15:35:11 +00:00
|
|
|
void MultiSelect::Inner::setQueryChangedCallback(Fn<void(const QString &query)> callback) {
|
2017-02-21 13:45:56 +00:00
|
|
|
_queryChangedCallback = std::move(callback);
|
2016-10-20 19:48:35 +00:00
|
|
|
}
|
|
|
|
|
2018-05-31 12:20:28 +00:00
|
|
|
void MultiSelect::Inner::setSubmittedCallback(
|
2018-06-04 15:35:11 +00:00
|
|
|
Fn<void(Qt::KeyboardModifiers)> callback) {
|
2017-02-21 13:45:56 +00:00
|
|
|
_submittedCallback = std::move(callback);
|
2016-10-20 19:48:35 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 11:23:54 +00:00
|
|
|
void MultiSelect::Inner::setCancelledCallback(Fn<void()> callback) {
|
|
|
|
_cancelledCallback = std::move(callback);
|
|
|
|
}
|
|
|
|
|
2016-10-21 12:28:26 +00:00
|
|
|
void MultiSelect::Inner::updateFieldGeometry() {
|
|
|
|
auto fieldFinalWidth = _fieldWidth;
|
2017-11-27 11:43:57 +00:00
|
|
|
if (_cancel->toggled()) {
|
2016-10-21 12:28:26 +00:00
|
|
|
fieldFinalWidth -= _st.fieldCancelSkip;
|
|
|
|
}
|
|
|
|
_field->resizeToWidth(fieldFinalWidth);
|
|
|
|
_field->moveToLeft(_st.padding.left() + _fieldLeft, _st.padding.top() + _fieldTop);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultiSelect::Inner::updateHasAnyItems(bool hasAnyItems) {
|
|
|
|
_field->setPlaceholderHidden(hasAnyItems);
|
|
|
|
updateCursor();
|
|
|
|
_iconOpacity.start([this] {
|
|
|
|
rtlupdate(_st.padding.left(), _st.padding.top(), _st.fieldIcon.width(), _st.fieldIcon.height());
|
|
|
|
}, hasAnyItems ? 1. : 0., hasAnyItems ? 0. : 1., _st.item.duration);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultiSelect::Inner::updateCursor() {
|
|
|
|
setCursor(_items.empty() ? style::cur_text : (_overDelete ? style::cur_pointer : style::cur_default));
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultiSelect::Inner::setActiveItem(int active, ChangeActiveWay skipSetFocus) {
|
|
|
|
if (_active == active) return;
|
|
|
|
|
|
|
|
if (_active >= 0) {
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(_active < _items.size());
|
2016-10-21 12:28:26 +00:00
|
|
|
_items[_active]->setActive(false);
|
|
|
|
}
|
|
|
|
_active = active;
|
|
|
|
if (_active >= 0) {
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(_active < _items.size());
|
2016-10-21 12:28:26 +00:00
|
|
|
_items[_active]->setActive(true);
|
|
|
|
}
|
|
|
|
if (skipSetFocus != ChangeActiveWay::SkipSetFocus) {
|
|
|
|
setInnerFocus();
|
|
|
|
}
|
|
|
|
if (_scrollCallback) {
|
|
|
|
auto rect = (_active >= 0) ? _items[_active]->rect() : _field->geometry().translated(-_st.padding.left(), -_st.padding.top());
|
|
|
|
_scrollCallback(rect.y(), rect.y() + rect.height() + _st.padding.top() + _st.padding.bottom());
|
|
|
|
}
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultiSelect::Inner::setActiveItemPrevious() {
|
|
|
|
if (_active > 0) {
|
|
|
|
setActiveItem(_active - 1);
|
|
|
|
} else if (_active < 0 && !_items.empty()) {
|
|
|
|
setActiveItem(_items.size() - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultiSelect::Inner::setActiveItemNext() {
|
|
|
|
if (_active >= 0 && _active + 1 < _items.size()) {
|
|
|
|
setActiveItem(_active + 1);
|
|
|
|
} else {
|
|
|
|
setActiveItem(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-20 19:48:35 +00:00
|
|
|
int MultiSelect::Inner::resizeGetHeight(int newWidth) {
|
2016-10-21 12:28:26 +00:00
|
|
|
computeItemsGeometry(newWidth);
|
|
|
|
updateFieldGeometry();
|
|
|
|
|
|
|
|
auto cancelLeft = _fieldLeft + _fieldWidth + _st.padding.right() - _cancel->width();
|
|
|
|
auto cancelTop = _fieldTop - _st.padding.top();
|
|
|
|
_cancel->moveToLeft(_st.padding.left() + cancelLeft, _st.padding.top() + cancelTop);
|
|
|
|
|
|
|
|
return _field->y() + _field->height() + _st.padding.bottom();
|
2016-10-20 19:48:35 +00:00
|
|
|
}
|
|
|
|
|
2016-10-21 12:28:26 +00:00
|
|
|
void MultiSelect::Inner::paintEvent(QPaintEvent *e) {
|
|
|
|
Painter p(this);
|
|
|
|
|
|
|
|
auto paintRect = e->rect();
|
2016-10-31 12:29:26 +00:00
|
|
|
p.fillRect(paintRect, _st.bg);
|
2016-10-21 12:28:26 +00:00
|
|
|
|
|
|
|
auto offset = QPoint(rtl() ? _st.padding.right() : _st.padding.left(), _st.padding.top());
|
|
|
|
p.translate(offset);
|
|
|
|
paintRect.translate(-offset);
|
|
|
|
|
|
|
|
auto outerWidth = width() - _st.padding.left() - _st.padding.right();
|
2019-04-02 09:13:30 +00:00
|
|
|
auto iconOpacity = _iconOpacity.value(_items.empty() ? 1. : 0.);
|
2016-10-21 12:28:26 +00:00
|
|
|
if (iconOpacity > 0.) {
|
|
|
|
p.setOpacity(iconOpacity);
|
|
|
|
_st.fieldIcon.paint(p, 0, 0, outerWidth);
|
|
|
|
p.setOpacity(1.);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto checkRect = myrtlrect(paintRect);
|
|
|
|
auto paintMargins = itemPaintMargins();
|
|
|
|
for (auto i = _removingItems.begin(), e = _removingItems.end(); i != e;) {
|
2017-03-17 12:05:50 +00:00
|
|
|
auto &item = *i;
|
2016-10-21 12:28:26 +00:00
|
|
|
auto itemRect = item->paintArea(outerWidth);
|
|
|
|
itemRect = itemRect.marginsAdded(paintMargins);
|
|
|
|
if (checkRect.intersects(itemRect)) {
|
2019-04-02 09:13:30 +00:00
|
|
|
item->paint(p, outerWidth);
|
2016-10-21 12:28:26 +00:00
|
|
|
}
|
|
|
|
if (item->hideFinished()) {
|
|
|
|
i = _removingItems.erase(i);
|
|
|
|
e = _removingItems.end();
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
2019-04-02 09:13:30 +00:00
|
|
|
for (const auto &item : _items) {
|
2016-10-21 12:28:26 +00:00
|
|
|
auto itemRect = item->paintArea(outerWidth);
|
|
|
|
itemRect = itemRect.marginsAdded(paintMargins);
|
|
|
|
if (checkRect.y() + checkRect.height() <= itemRect.y()) {
|
|
|
|
break;
|
|
|
|
} else if (checkRect.intersects(itemRect)) {
|
2019-04-02 09:13:30 +00:00
|
|
|
item->paint(p, outerWidth);
|
2016-10-21 12:28:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QMargins MultiSelect::Inner::itemPaintMargins() const {
|
|
|
|
return {
|
|
|
|
qMax(_st.itemSkip, _st.padding.left()),
|
|
|
|
_st.itemSkip,
|
|
|
|
qMax(_st.itemSkip, _st.padding.right()),
|
|
|
|
_st.itemSkip,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-02-11 11:24:37 +00:00
|
|
|
void MultiSelect::Inner::leaveEventHook(QEvent *e) {
|
2016-10-21 12:28:26 +00:00
|
|
|
clearSelection();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultiSelect::Inner::mouseMoveEvent(QMouseEvent *e) {
|
|
|
|
updateSelection(e->pos());
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultiSelect::Inner::keyPressEvent(QKeyEvent *e) {
|
|
|
|
if (_active >= 0) {
|
2017-03-17 12:05:50 +00:00
|
|
|
Expects(_active < _items.size());
|
2016-10-21 12:28:26 +00:00
|
|
|
if (e->key() == Qt::Key_Delete || e->key() == Qt::Key_Backspace) {
|
|
|
|
auto itemId = _items[_active]->id();
|
|
|
|
setActiveItemNext();
|
|
|
|
removeItem(itemId);
|
|
|
|
} else if (e->key() == Qt::Key_Left) {
|
|
|
|
setActiveItemPrevious();
|
|
|
|
} else if (e->key() == Qt::Key_Right) {
|
|
|
|
setActiveItemNext();
|
|
|
|
} else if (e->key() == Qt::Key_Escape) {
|
|
|
|
setActiveItem(-1);
|
|
|
|
} else {
|
|
|
|
e->ignore();
|
|
|
|
}
|
|
|
|
} else if (e->key() == Qt::Key_Left || e->key() == Qt::Key_Backspace) {
|
|
|
|
setActiveItemPrevious();
|
|
|
|
} else {
|
|
|
|
e->ignore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-31 12:20:28 +00:00
|
|
|
void MultiSelect::Inner::submitted(Qt::KeyboardModifiers modifiers) {
|
|
|
|
if (_submittedCallback) {
|
|
|
|
_submittedCallback(modifiers);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-30 11:23:54 +00:00
|
|
|
void MultiSelect::Inner::cancelled() {
|
|
|
|
if (_cancelledCallback) {
|
|
|
|
_cancelledCallback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-31 12:20:28 +00:00
|
|
|
void MultiSelect::Inner::fieldFocused() {
|
2016-10-21 12:28:26 +00:00
|
|
|
setActiveItem(-1, ChangeActiveWay::SkipSetFocus);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultiSelect::Inner::updateSelection(QPoint mousePosition) {
|
|
|
|
auto point = myrtlpoint(mousePosition) - QPoint(_st.padding.left(), _st.padding.right());
|
|
|
|
auto selected = -1;
|
2017-03-17 12:05:50 +00:00
|
|
|
for (auto i = 0, count = int(_items.size()); i != count; ++i) {
|
2016-10-21 12:28:26 +00:00
|
|
|
auto itemRect = _items[i]->rect();
|
|
|
|
if (itemRect.y() > point.y()) {
|
|
|
|
break;
|
|
|
|
} else if (itemRect.contains(point)) {
|
|
|
|
point -= itemRect.topLeft();
|
|
|
|
selected = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_selected != selected) {
|
|
|
|
if (_selected >= 0) {
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(_selected < _items.size());
|
2016-10-21 12:28:26 +00:00
|
|
|
_items[_selected]->leaveEvent();
|
|
|
|
}
|
|
|
|
_selected = selected;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
auto overDelete = false;
|
|
|
|
if (_selected >= 0) {
|
|
|
|
_items[_selected]->mouseMoveEvent(point);
|
|
|
|
overDelete = _items[_selected]->isOverDelete();
|
|
|
|
}
|
|
|
|
if (_overDelete != overDelete) {
|
|
|
|
_overDelete = overDelete;
|
|
|
|
updateCursor();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultiSelect::Inner::mousePressEvent(QMouseEvent *e) {
|
|
|
|
if (_overDelete) {
|
2017-03-17 12:05:50 +00:00
|
|
|
Expects(_selected >= 0);
|
|
|
|
Expects(_selected < _items.size());
|
2016-10-21 12:28:26 +00:00
|
|
|
removeItem(_items[_selected]->id());
|
|
|
|
} else if (_selected >= 0) {
|
|
|
|
setActiveItem(_selected);
|
|
|
|
} else {
|
|
|
|
setInnerFocus();
|
|
|
|
}
|
2016-10-20 19:48:35 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 12:05:50 +00:00
|
|
|
void MultiSelect::Inner::addItemInBunch(std::unique_ptr<Item> item) {
|
2016-10-21 12:28:26 +00:00
|
|
|
auto wasEmpty = _items.empty();
|
|
|
|
item->setUpdateCallback([this, item = item.get()] {
|
|
|
|
auto itemRect = item->paintArea(width() - _st.padding.left() - _st.padding.top());
|
|
|
|
itemRect = itemRect.translated(_st.padding.left(), _st.padding.top());
|
|
|
|
itemRect = itemRect.marginsAdded(itemPaintMargins());
|
|
|
|
rtlupdate(itemRect);
|
|
|
|
});
|
2017-03-17 12:05:50 +00:00
|
|
|
_idsMap.insert(item->id());
|
|
|
|
_items.push_back(std::move(item));
|
2016-10-21 12:28:26 +00:00
|
|
|
if (wasEmpty) {
|
|
|
|
updateHasAnyItems(true);
|
|
|
|
}
|
2017-03-17 12:05:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MultiSelect::Inner::finishItemsBunch(AddItemWay way) {
|
|
|
|
updateItemsGeometry();
|
2016-10-22 13:03:20 +00:00
|
|
|
if (way != AddItemWay::SkipAnimation) {
|
|
|
|
_items.back()->showAnimated();
|
|
|
|
} else {
|
2017-09-30 19:20:40 +00:00
|
|
|
_field->finishAnimating();
|
2016-10-22 13:03:20 +00:00
|
|
|
finishHeightAnimation();
|
|
|
|
}
|
2016-10-20 19:48:35 +00:00
|
|
|
}
|
|
|
|
|
2016-10-21 12:28:26 +00:00
|
|
|
void MultiSelect::Inner::computeItemsGeometry(int newWidth) {
|
|
|
|
newWidth -= _st.padding.left() + _st.padding.right();
|
|
|
|
|
|
|
|
auto itemLeft = 0;
|
|
|
|
auto itemTop = 0;
|
|
|
|
auto widthLeft = newWidth;
|
|
|
|
auto maxVisiblePadding = qMax(_st.padding.left(), _st.padding.right());
|
2021-09-03 11:09:57 +00:00
|
|
|
for (const auto &item : _items) {
|
2016-10-21 12:28:26 +00:00
|
|
|
auto itemWidth = item->getWidth();
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(itemWidth <= newWidth);
|
2016-10-21 12:28:26 +00:00
|
|
|
if (itemWidth > widthLeft) {
|
|
|
|
itemLeft = 0;
|
|
|
|
itemTop += _st.item.height + _st.itemSkip;
|
|
|
|
widthLeft = newWidth;
|
2016-10-20 19:48:35 +00:00
|
|
|
}
|
2016-10-21 12:28:26 +00:00
|
|
|
item->setPosition(itemLeft, itemTop, newWidth, maxVisiblePadding);
|
|
|
|
itemLeft += itemWidth + _st.itemSkip;
|
|
|
|
widthLeft -= itemWidth + _st.itemSkip;
|
2016-10-20 19:48:35 +00:00
|
|
|
}
|
2016-10-21 12:28:26 +00:00
|
|
|
|
|
|
|
auto fieldMinWidth = _st.fieldMinWidth + _st.fieldCancelSkip;
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(fieldMinWidth <= newWidth);
|
2016-10-21 12:28:26 +00:00
|
|
|
if (fieldMinWidth > widthLeft) {
|
|
|
|
_fieldLeft = 0;
|
|
|
|
_fieldTop = itemTop + _st.item.height + _st.itemSkip;
|
|
|
|
} else {
|
|
|
|
_fieldLeft = itemLeft + (_items.empty() ? _st.fieldIconSkip : 0);
|
|
|
|
_fieldTop = itemTop;
|
2016-10-20 19:48:35 +00:00
|
|
|
}
|
2016-10-21 12:28:26 +00:00
|
|
|
_fieldWidth = newWidth - _fieldLeft;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultiSelect::Inner::updateItemsGeometry() {
|
|
|
|
auto newHeight = resizeGetHeight(width());
|
|
|
|
if (newHeight == _newHeight) return;
|
|
|
|
|
|
|
|
_newHeight = newHeight;
|
2016-10-22 13:03:20 +00:00
|
|
|
_height.start([this] { updateHeightStep(); }, height(), _newHeight, _st.item.duration);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultiSelect::Inner::updateHeightStep() {
|
2019-04-02 09:13:30 +00:00
|
|
|
auto newHeight = qRound(_height.value(_newHeight));
|
2016-10-22 13:03:20 +00:00
|
|
|
if (auto heightDelta = newHeight - height()) {
|
|
|
|
resize(width(), newHeight);
|
|
|
|
if (_resizedCallback) {
|
|
|
|
_resizedCallback(heightDelta);
|
2016-10-21 12:28:26 +00:00
|
|
|
}
|
2016-10-22 13:03:20 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultiSelect::Inner::finishHeightAnimation() {
|
2019-04-02 09:13:30 +00:00
|
|
|
_height.stop();
|
2016-10-22 13:03:20 +00:00
|
|
|
updateHeightStep();
|
2016-10-20 19:48:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MultiSelect::Inner::setItemText(uint64 itemId, const QString &text) {
|
2021-09-03 11:09:57 +00:00
|
|
|
for (const auto &item : _items) {
|
2016-10-20 19:48:35 +00:00
|
|
|
if (item->id() == itemId) {
|
|
|
|
item->setText(text);
|
2016-10-21 12:28:26 +00:00
|
|
|
updateItemsGeometry();
|
2016-10-20 19:48:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-04 15:35:11 +00:00
|
|
|
void MultiSelect::Inner::setItemRemovedCallback(Fn<void(uint64 itemId)> callback) {
|
2017-02-21 13:45:56 +00:00
|
|
|
_itemRemovedCallback = std::move(callback);
|
2016-10-20 19:48:35 +00:00
|
|
|
}
|
|
|
|
|
2018-06-04 15:35:11 +00:00
|
|
|
void MultiSelect::Inner::setResizedCallback(Fn<void(int heightDelta)> callback) {
|
2017-02-21 13:45:56 +00:00
|
|
|
_resizedCallback = std::move(callback);
|
2016-10-21 12:28:26 +00:00
|
|
|
}
|
|
|
|
|
2016-10-20 19:48:35 +00:00
|
|
|
void MultiSelect::Inner::removeItem(uint64 itemId) {
|
2017-03-17 12:05:50 +00:00
|
|
|
auto found = false;
|
|
|
|
for (auto i = 0, count = int(_items.size()); i != count; ++i) {
|
|
|
|
auto &item = _items[i];
|
2016-10-20 19:48:35 +00:00
|
|
|
if (item->id() == itemId) {
|
2017-03-17 12:05:50 +00:00
|
|
|
found = true;
|
2016-10-21 12:28:26 +00:00
|
|
|
clearSelection();
|
2017-03-17 12:05:50 +00:00
|
|
|
|
|
|
|
item->hideAnimated();
|
|
|
|
_idsMap.erase(item->id());
|
2021-07-13 17:51:07 +00:00
|
|
|
_removingItems.insert(std::move(item));
|
2017-03-17 12:05:50 +00:00
|
|
|
_items.erase(_items.begin() + i);
|
|
|
|
|
2016-10-21 12:28:26 +00:00
|
|
|
if (_active == i) {
|
|
|
|
_active = -1;
|
|
|
|
} else if (_active > i) {
|
|
|
|
--_active;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateItemsGeometry();
|
|
|
|
if (_items.empty()) {
|
|
|
|
updateHasAnyItems(false);
|
|
|
|
}
|
|
|
|
auto point = QCursor::pos();
|
|
|
|
if (auto parent = parentWidget()) {
|
|
|
|
if (parent->rect().contains(parent->mapFromGlobal(point))) {
|
|
|
|
updateSelection(mapFromGlobal(point));
|
|
|
|
}
|
|
|
|
}
|
2016-10-20 19:48:35 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-03-17 12:05:50 +00:00
|
|
|
if (found && _itemRemovedCallback) {
|
2016-10-20 19:48:35 +00:00
|
|
|
_itemRemovedCallback(itemId);
|
|
|
|
}
|
2016-10-21 12:28:26 +00:00
|
|
|
setInnerFocus();
|
2016-10-20 19:48:35 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 12:05:50 +00:00
|
|
|
int MultiSelect::Inner::getItemsCount() const {
|
|
|
|
return _items.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVector<uint64> MultiSelect::Inner::getItems() const {
|
|
|
|
auto result = QVector<uint64>();
|
|
|
|
result.reserve(_items.size());
|
2021-09-03 11:09:57 +00:00
|
|
|
for (const auto &item : _items) {
|
2017-03-17 12:05:50 +00:00
|
|
|
result.push_back(item->id());
|
2016-10-21 12:28:26 +00:00
|
|
|
}
|
2017-03-17 12:05:50 +00:00
|
|
|
return result;
|
2016-10-20 19:48:35 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 12:05:50 +00:00
|
|
|
bool MultiSelect::Inner::hasItem(uint64 itemId) const {
|
|
|
|
return _idsMap.find(itemId) != _idsMap.cend();
|
|
|
|
}
|
|
|
|
|
2017-03-18 21:29:43 +00:00
|
|
|
MultiSelect::Inner::~Inner() = default;
|
2017-03-17 12:05:50 +00:00
|
|
|
|
2016-10-20 19:48:35 +00:00
|
|
|
} // namespace Ui
|