diff --git a/src/SuggestionsPopup.cpp b/src/SuggestionsPopup.cpp
index 49495e71..cb569ddf 100644
--- a/src/SuggestionsPopup.cpp
+++ b/src/SuggestionsPopup.cpp
@@ -1,7 +1,5 @@
#include "Avatar.h"
#include "AvatarProvider.h"
-#include "Cache.h"
-#include "ChatPage.h"
#include "Config.h"
#include "DropShadow.h"
#include "SuggestionsPopup.hpp"
@@ -15,10 +13,9 @@
constexpr int PopupHMargin = 5;
constexpr int PopupItemMargin = 4;
-PopupItem::PopupItem(QWidget *parent, const QString &user_id)
+PopupItem::PopupItem(QWidget *parent)
: QWidget(parent)
, avatar_{new Avatar(this)}
- , user_id_{user_id}
, hovering_{false}
{
setMouseTracking(true);
@@ -27,11 +24,39 @@ PopupItem::PopupItem(QWidget *parent, const QString &user_id)
topLayout_ = new QHBoxLayout(this);
topLayout_->setContentsMargins(
PopupHMargin, PopupItemMargin, PopupHMargin, PopupItemMargin);
+}
+
+void
+PopupItem::paintEvent(QPaintEvent *)
+{
+ QStyleOption opt;
+ opt.init(this);
+ QPainter p(this);
+ style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
+
+ if (underMouse() || hovering_)
+ p.fillRect(rect(), hoverColor_);
+}
+void
+PopupItem::mousePressEvent(QMouseEvent *event)
+{
+ if (event->buttons() != Qt::RightButton)
+ // TODO: should be abstracted.
+ emit clicked(
+ Cache::displayName(ChatPage::instance()->currentRoom(), selectedText()));
+
+ QWidget::mousePressEvent(event);
+}
+
+UserItem::UserItem(QWidget *parent, const QString &user_id)
+ : PopupItem(parent)
+ , userId_{user_id}
+{
QFont font;
font.setPixelSize(conf::popup::font);
- auto displayName = Cache::displayName(ChatPage::instance()->currentRoom(), user_id);
+ auto displayName = Cache::displayName(ChatPage::instance()->currentRoom(), userId_);
avatar_->setSize(conf::popup::avatar);
avatar_->setLetter(utils::firstChar(displayName));
@@ -47,30 +72,29 @@ PopupItem::PopupItem(QWidget *parent, const QString &user_id)
topLayout_->addWidget(userName_, 1);
AvatarProvider::resolve(ChatPage::instance()->currentRoom(),
- user_id,
+ userId_,
this,
[this](const QImage &img) { avatar_->setImage(img); });
}
-void
-PopupItem::paintEvent(QPaintEvent *)
+RoomItem::RoomItem(QWidget *parent, const RoomSearchResult &res)
+ : PopupItem(parent)
+ , roomId_{QString::fromStdString(res.room_id)}
{
- QStyleOption opt;
- opt.init(this);
- QPainter p(this);
- style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
+ auto name = QFontMetrics(QFont()).elidedText(
+ QString::fromStdString(res.info.name), Qt::ElideRight, parentWidget()->width() - 10);
- if (underMouse() || hovering_)
- p.fillRect(rect(), hoverColor_);
-}
+ avatar_->setSize(conf::popup::avatar + 6);
+ avatar_->setLetter(utils::firstChar(name));
-void
-PopupItem::mousePressEvent(QMouseEvent *event)
-{
- if (event->buttons() != Qt::RightButton)
- emit clicked(Cache::displayName(ChatPage::instance()->currentRoom(), user_id_));
+ roomName_ = new QLabel(name, this);
+ roomName_->setMargin(0);
- QWidget::mousePressEvent(event);
+ topLayout_->addWidget(avatar_);
+ topLayout_->addWidget(roomName_, 1);
+
+ if (!res.img.isNull())
+ avatar_->setImage(res.img);
}
SuggestionsPopup::SuggestionsPopup(QWidget *parent)
@@ -85,24 +109,41 @@ SuggestionsPopup::SuggestionsPopup(QWidget *parent)
}
void
-SuggestionsPopup::addUsers(const QVector<SearchResult> &users)
+SuggestionsPopup::addRooms(const std::vector<RoomSearchResult> &rooms)
{
- // Remove all items from the layout.
- QLayoutItem *item;
- while ((item = layout_->takeAt(0)) != 0) {
- delete item->widget();
- delete item;
+ removeItems();
+
+ if (rooms.empty()) {
+ hide();
+ return;
+ }
+
+ for (const auto &r : rooms) {
+ auto room = new RoomItem(this, r);
+ layout_->addWidget(room);
+ connect(room, &RoomItem::clicked, this, &SuggestionsPopup::itemSelected);
}
+ resetSelection();
+ adjustSize();
+
+ resize(geometry().width(), 40 * rooms.size());
+}
+
+void
+SuggestionsPopup::addUsers(const QVector<SearchResult> &users)
+{
+ removeItems();
+
if (users.isEmpty()) {
hide();
return;
}
for (const auto &u : users) {
- auto user = new PopupItem(this, u.user_id);
+ auto user = new UserItem(this, u.user_id);
layout_->addWidget(user);
- connect(user, &PopupItem::clicked, this, &SuggestionsPopup::itemSelected);
+ connect(user, &UserItem::clicked, this, &SuggestionsPopup::itemSelected);
}
resetSelection();
@@ -161,19 +202,6 @@ SuggestionsPopup::setHovering(int pos)
}
void
-SuggestionsPopup::selectHoveredSuggestion()
-{
- const auto item = layout_->itemAt(selectedItem_);
- if (!item)
- return;
-
- const auto &widget = qobject_cast<PopupItem *>(item->widget());
- emit itemSelected(Cache::displayName(ChatPage::instance()->currentRoom(), widget->user()));
-
- resetSelection();
-}
-
-void
SuggestionsPopup::paintEvent(QPaintEvent *)
{
QStyleOption opt;
|