2016-10-20 19:48:35 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop version of Telegram messaging app, see https://telegram.org
|
|
|
|
|
|
|
|
Telegram Desktop is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
In addition, as a special exception, the copyright holders give permission
|
|
|
|
to link the code of portions of this program with the OpenSSL library.
|
|
|
|
|
|
|
|
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
|
|
|
Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "styles/style_widgets.h"
|
|
|
|
|
|
|
|
namespace Ui {
|
|
|
|
|
2016-11-15 11:56:49 +00:00
|
|
|
class InputField;
|
2016-11-21 17:46:29 +00:00
|
|
|
class CrossButton;
|
2016-11-16 10:44:06 +00:00
|
|
|
class ScrollArea;
|
2016-10-20 19:48:35 +00:00
|
|
|
|
|
|
|
class MultiSelect : public TWidget {
|
|
|
|
public:
|
|
|
|
MultiSelect(QWidget *parent, const style::MultiSelect &st, const QString &placeholder = QString());
|
|
|
|
|
|
|
|
QString getQuery() const;
|
|
|
|
void setInnerFocus();
|
2016-10-21 12:28:26 +00:00
|
|
|
void clearQuery();
|
2016-10-20 19:48:35 +00:00
|
|
|
|
2016-11-20 12:54:07 +00:00
|
|
|
void setQueryChangedCallback(base::lambda<void(const QString &query)> &&callback);
|
|
|
|
void setSubmittedCallback(base::lambda<void(bool ctrlShiftEnter)> &&callback);
|
|
|
|
void setResizedCallback(base::lambda<void()> &&callback);
|
2016-10-20 19:48:35 +00:00
|
|
|
|
2016-10-22 13:03:20 +00:00
|
|
|
enum class AddItemWay {
|
|
|
|
Default,
|
|
|
|
SkipAnimation,
|
|
|
|
};
|
2016-11-20 12:54:07 +00:00
|
|
|
using PaintRoundImage = base::lambda<void(Painter &p, int x, int y, int outerWidth, int size)>;
|
2016-12-23 13:21:01 +00:00
|
|
|
void addItem(uint64 itemId, const QString &text, style::color color, PaintRoundImage &&paintRoundImage, AddItemWay way = AddItemWay::Default);
|
2016-10-20 19:48:35 +00:00
|
|
|
void setItemText(uint64 itemId, const QString &text);
|
|
|
|
|
2016-11-20 12:54:07 +00:00
|
|
|
void setItemRemovedCallback(base::lambda<void(uint64 itemId)> &&callback);
|
2016-10-21 12:28:26 +00:00
|
|
|
void removeItem(uint64 itemId);
|
2016-10-20 19:48:35 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
int resizeGetHeight(int newWidth) override;
|
2016-10-21 12:28:26 +00:00
|
|
|
bool eventFilter(QObject *o, QEvent *e) override;
|
2016-10-20 19:48:35 +00:00
|
|
|
|
|
|
|
private:
|
2016-10-21 12:28:26 +00:00
|
|
|
void scrollTo(int activeTop, int activeBottom);
|
2016-10-20 19:48:35 +00:00
|
|
|
|
|
|
|
const style::MultiSelect &_st;
|
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
object_ptr<Ui::ScrollArea> _scroll;
|
2016-10-20 19:48:35 +00:00
|
|
|
|
2016-10-21 12:28:26 +00:00
|
|
|
class Inner;
|
2016-12-13 17:07:56 +00:00
|
|
|
QPointer<Inner> _inner;
|
2016-10-20 19:48:35 +00:00
|
|
|
|
2016-11-20 12:54:07 +00:00
|
|
|
base::lambda<void()> _resizedCallback;
|
|
|
|
base::lambda<void(const QString &query)> _queryChangedCallback;
|
2016-10-20 19:48:35 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
// This class is hold in header because it requires Qt preprocessing.
|
2016-10-26 16:43:13 +00:00
|
|
|
class MultiSelect::Inner : public TWidget {
|
2016-10-20 19:48:35 +00:00
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2016-11-20 12:54:07 +00:00
|
|
|
using ScrollCallback = base::lambda<void(int activeTop, int activeBottom)>;
|
|
|
|
Inner(QWidget *parent, const style::MultiSelect &st, const QString &placeholder, ScrollCallback &&callback);
|
2016-10-20 19:48:35 +00:00
|
|
|
|
|
|
|
QString getQuery() const;
|
|
|
|
bool setInnerFocus();
|
2016-10-21 12:28:26 +00:00
|
|
|
void clearQuery();
|
2016-10-20 19:48:35 +00:00
|
|
|
|
2016-11-20 12:54:07 +00:00
|
|
|
void setQueryChangedCallback(base::lambda<void(const QString &query)> &&callback);
|
|
|
|
void setSubmittedCallback(base::lambda<void(bool ctrlShiftEnter)> &&callback);
|
2016-10-20 19:48:35 +00:00
|
|
|
|
2016-10-21 12:28:26 +00:00
|
|
|
class Item;
|
2016-10-22 13:03:20 +00:00
|
|
|
void addItem(std_::unique_ptr<Item> item, AddItemWay way);
|
2016-10-20 19:48:35 +00:00
|
|
|
void setItemText(uint64 itemId, const QString &text);
|
|
|
|
|
2016-11-20 12:54:07 +00:00
|
|
|
void setItemRemovedCallback(base::lambda<void(uint64 itemId)> &&callback);
|
2016-10-21 12:28:26 +00:00
|
|
|
void removeItem(uint64 itemId);
|
|
|
|
|
2016-11-20 12:54:07 +00:00
|
|
|
void setResizedCallback(base::lambda<void(int heightDelta)> &&callback);
|
2016-10-20 19:48:35 +00:00
|
|
|
|
|
|
|
~Inner();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
int resizeGetHeight(int newWidth) override;
|
|
|
|
|
2016-10-21 12:28:26 +00:00
|
|
|
void paintEvent(QPaintEvent *e) override;
|
|
|
|
void leaveEvent(QEvent *e) override;
|
|
|
|
void mouseMoveEvent(QMouseEvent *e) override;
|
|
|
|
void mousePressEvent(QMouseEvent *e) override;
|
|
|
|
void keyPressEvent(QKeyEvent *e) override;
|
2016-10-20 19:48:35 +00:00
|
|
|
|
|
|
|
private slots:
|
|
|
|
void onQueryChanged();
|
|
|
|
void onSubmitted(bool ctrlShiftEnter) {
|
|
|
|
if (_submittedCallback) {
|
|
|
|
_submittedCallback(ctrlShiftEnter);
|
|
|
|
}
|
|
|
|
}
|
2016-10-21 12:28:26 +00:00
|
|
|
void onFieldFocused();
|
2016-10-20 19:48:35 +00:00
|
|
|
|
|
|
|
private:
|
2016-10-21 12:28:26 +00:00
|
|
|
void computeItemsGeometry(int newWidth);
|
|
|
|
void updateItemsGeometry();
|
|
|
|
void updateFieldGeometry();
|
|
|
|
void updateHasAnyItems(bool hasAnyItems);
|
|
|
|
void updateSelection(QPoint mousePosition);
|
|
|
|
void clearSelection() {
|
|
|
|
updateSelection(QPoint(-1, -1));
|
|
|
|
}
|
|
|
|
void updateCursor();
|
2016-10-22 13:03:20 +00:00
|
|
|
void updateHeightStep();
|
|
|
|
void finishHeightAnimation();
|
2016-10-21 12:28:26 +00:00
|
|
|
enum class ChangeActiveWay {
|
|
|
|
Default,
|
|
|
|
SkipSetFocus,
|
|
|
|
};
|
|
|
|
void setActiveItem(int active, ChangeActiveWay skipSetFocus = ChangeActiveWay::Default);
|
|
|
|
void setActiveItemPrevious();
|
|
|
|
void setActiveItemNext();
|
|
|
|
|
|
|
|
QMargins itemPaintMargins() const;
|
2016-10-20 19:48:35 +00:00
|
|
|
|
|
|
|
const style::MultiSelect &_st;
|
2016-12-07 13:32:25 +00:00
|
|
|
Animation _iconOpacity;
|
2016-10-20 19:48:35 +00:00
|
|
|
|
2016-10-21 12:28:26 +00:00
|
|
|
ScrollCallback _scrollCallback;
|
2016-10-20 19:48:35 +00:00
|
|
|
|
|
|
|
using Items = QList<Item*>;
|
|
|
|
Items _items;
|
2016-10-21 12:28:26 +00:00
|
|
|
using RemovingItems = OrderedSet<Item*>;
|
|
|
|
RemovingItems _removingItems;
|
|
|
|
|
|
|
|
int _selected = -1;
|
|
|
|
int _active = -1;
|
|
|
|
bool _overDelete = false;
|
2016-10-20 19:48:35 +00:00
|
|
|
|
2016-10-21 12:28:26 +00:00
|
|
|
int _fieldLeft = 0;
|
|
|
|
int _fieldTop = 0;
|
|
|
|
int _fieldWidth = 0;
|
2016-12-13 17:07:56 +00:00
|
|
|
object_ptr<Ui::InputField> _field;
|
|
|
|
object_ptr<Ui::CrossButton> _cancel;
|
2016-10-20 19:48:35 +00:00
|
|
|
|
2016-10-21 12:28:26 +00:00
|
|
|
int _newHeight = 0;
|
2016-12-07 13:32:25 +00:00
|
|
|
Animation _height;
|
2016-10-21 12:28:26 +00:00
|
|
|
|
2016-11-20 12:54:07 +00:00
|
|
|
base::lambda<void(const QString &query)> _queryChangedCallback;
|
|
|
|
base::lambda<void(bool ctrlShiftEnter)> _submittedCallback;
|
|
|
|
base::lambda<void(uint64 itemId)> _itemRemovedCallback;
|
|
|
|
base::lambda<void(int heightDelta)> _resizedCallback;
|
2016-10-20 19:48:35 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Ui
|