2018-03-25 00:16:15 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <QHBoxLayout>
|
|
|
|
#include <QLabel>
|
|
|
|
#include <QPoint>
|
|
|
|
#include <QWidget>
|
|
|
|
|
2018-04-27 01:57:46 +03:00
|
|
|
#include "Avatar.h"
|
|
|
|
#include "AvatarProvider.h"
|
|
|
|
#include "Cache.h"
|
|
|
|
#include "ChatPage.h"
|
|
|
|
|
2018-03-25 00:16:15 +03:00
|
|
|
class Avatar;
|
2018-04-21 16:34:50 +03:00
|
|
|
struct SearchResult;
|
2018-03-25 00:16:15 +03:00
|
|
|
|
|
|
|
class PopupItem : public QWidget
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
Q_PROPERTY(QColor hoverColor READ hoverColor WRITE setHoverColor)
|
2018-04-10 11:47:23 +03:00
|
|
|
Q_PROPERTY(bool hovering READ hovering WRITE setHovering)
|
2018-03-25 00:16:15 +03:00
|
|
|
|
|
|
|
public:
|
2018-04-27 01:57:46 +03:00
|
|
|
PopupItem(QWidget *parent);
|
2018-03-25 00:16:15 +03:00
|
|
|
|
2018-04-27 01:57:46 +03:00
|
|
|
QString selectedText() const { return QString(); }
|
2018-03-25 00:16:15 +03:00
|
|
|
QColor hoverColor() const { return hoverColor_; }
|
|
|
|
void setHoverColor(QColor &color) { hoverColor_ = color; }
|
|
|
|
|
2018-04-10 11:47:23 +03:00
|
|
|
bool hovering() const { return hovering_; }
|
|
|
|
void setHovering(const bool hover) { hovering_ = hover; };
|
|
|
|
|
2018-03-25 00:16:15 +03:00
|
|
|
protected:
|
|
|
|
void paintEvent(QPaintEvent *event) override;
|
|
|
|
|
|
|
|
signals:
|
2018-04-27 01:57:46 +03:00
|
|
|
void clicked(const QString &text);
|
2018-03-25 00:16:15 +03:00
|
|
|
|
2018-04-27 01:57:46 +03:00
|
|
|
protected:
|
2018-03-25 00:16:15 +03:00
|
|
|
QHBoxLayout *topLayout_;
|
|
|
|
Avatar *avatar_;
|
|
|
|
QColor hoverColor_;
|
2018-04-10 11:47:23 +03:00
|
|
|
|
2018-04-27 14:04:13 +03:00
|
|
|
//! Set if the item is currently being
|
|
|
|
//! hovered during tab completion (cycling).
|
2018-04-10 11:47:23 +03:00
|
|
|
bool hovering_;
|
2018-03-25 00:16:15 +03:00
|
|
|
};
|
|
|
|
|
2018-04-27 01:57:46 +03:00
|
|
|
class UserItem : public PopupItem
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
UserItem(QWidget *parent, const QString &user_id);
|
|
|
|
QString selectedText() const { return userId_; }
|
2018-05-03 17:29:02 +03:00
|
|
|
void updateItem(const QString &user_id);
|
2018-04-27 01:57:46 +03:00
|
|
|
|
2018-04-27 14:04:13 +03:00
|
|
|
protected:
|
|
|
|
void mousePressEvent(QMouseEvent *event) override;
|
|
|
|
|
2018-04-27 01:57:46 +03:00
|
|
|
private:
|
2018-05-03 17:29:02 +03:00
|
|
|
void resolveAvatar(const QString &user_id);
|
|
|
|
|
2018-04-27 01:57:46 +03:00
|
|
|
QLabel *userName_;
|
|
|
|
QString userId_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class RoomItem : public PopupItem
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
RoomItem(QWidget *parent, const RoomSearchResult &res);
|
|
|
|
QString selectedText() const { return roomId_; }
|
2018-05-03 17:29:02 +03:00
|
|
|
void updateItem(const RoomSearchResult &res);
|
2018-04-27 01:57:46 +03:00
|
|
|
|
2018-04-27 14:04:13 +03:00
|
|
|
protected:
|
|
|
|
void mousePressEvent(QMouseEvent *event) override;
|
|
|
|
|
2018-04-27 01:57:46 +03:00
|
|
|
private:
|
|
|
|
QLabel *roomName_;
|
|
|
|
QString roomId_;
|
|
|
|
RoomSearchResult info_;
|
|
|
|
};
|
|
|
|
|
2018-03-25 00:16:15 +03:00
|
|
|
class SuggestionsPopup : public QWidget
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit SuggestionsPopup(QWidget *parent = nullptr);
|
|
|
|
|
2018-04-27 01:57:46 +03:00
|
|
|
template<class Item>
|
|
|
|
void selectHoveredSuggestion()
|
|
|
|
{
|
|
|
|
const auto item = layout_->itemAt(selectedItem_);
|
|
|
|
if (!item)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const auto &widget = qobject_cast<Item *>(item->widget());
|
|
|
|
emit itemSelected(
|
|
|
|
Cache::displayName(ChatPage::instance()->currentRoom(), widget->selectedText()));
|
|
|
|
|
|
|
|
resetSelection();
|
|
|
|
}
|
|
|
|
|
2018-03-25 00:16:15 +03:00
|
|
|
public slots:
|
|
|
|
void addUsers(const QVector<SearchResult> &users);
|
2018-04-27 01:57:46 +03:00
|
|
|
void addRooms(const std::vector<RoomSearchResult> &rooms);
|
|
|
|
|
2018-04-14 14:12:36 +03:00
|
|
|
//! Move to the next available suggestion item.
|
|
|
|
void selectNextSuggestion();
|
|
|
|
//! Move to the previous available suggestion item.
|
|
|
|
void selectPreviousSuggestion();
|
|
|
|
//! Remove hovering from all items.
|
|
|
|
void resetHovering();
|
|
|
|
//! Set hovering to the item in the given layout position.
|
|
|
|
void setHovering(int pos);
|
2018-03-25 00:16:15 +03:00
|
|
|
|
2018-04-24 16:03:50 +03:00
|
|
|
protected:
|
|
|
|
void paintEvent(QPaintEvent *event) override;
|
|
|
|
|
2018-03-25 00:16:15 +03:00
|
|
|
signals:
|
|
|
|
void itemSelected(const QString &user);
|
|
|
|
|
|
|
|
private:
|
2018-04-14 14:12:36 +03:00
|
|
|
void hoverSelection();
|
|
|
|
void resetSelection() { selectedItem_ = -1; }
|
|
|
|
void selectFirstItem() { selectedItem_ = 0; }
|
|
|
|
void selectLastItem() { selectedItem_ = layout_->count() - 1; }
|
2018-05-03 17:29:02 +03:00
|
|
|
void removeLayoutItemsAfter(size_t startingPos)
|
2018-04-27 01:57:46 +03:00
|
|
|
{
|
2018-05-03 17:29:02 +03:00
|
|
|
size_t posToRemove = layout_->count() - 1;
|
|
|
|
|
2018-04-27 01:57:46 +03:00
|
|
|
QLayoutItem *item;
|
2018-05-03 17:29:02 +03:00
|
|
|
while (startingPos <= posToRemove && (item = layout_->takeAt(posToRemove)) != 0) {
|
2018-04-27 01:57:46 +03:00
|
|
|
delete item->widget();
|
|
|
|
delete item;
|
2018-05-03 17:29:02 +03:00
|
|
|
|
|
|
|
posToRemove = layout_->count() - 1;
|
2018-04-27 01:57:46 +03:00
|
|
|
}
|
|
|
|
}
|
2018-04-14 14:12:36 +03:00
|
|
|
|
2018-03-25 00:16:15 +03:00
|
|
|
QVBoxLayout *layout_;
|
2018-04-10 11:47:23 +03:00
|
|
|
|
|
|
|
//! Counter for tab completion (cycling).
|
2018-04-14 14:12:36 +03:00
|
|
|
int selectedItem_ = -1;
|
2018-03-25 00:16:15 +03:00
|
|
|
};
|