forked from blue/squawk
removed Order, resolved a crash on several files being uploaded simultaniuosly
This commit is contained in:
parent
0a530bfa93
commit
be466fbad1
12 changed files with 122 additions and 304 deletions
|
@ -18,6 +18,8 @@
|
|||
|
||||
#include "badge.h"
|
||||
|
||||
#include "shared/utils.h"
|
||||
|
||||
Badge::Badge(const QString& p_id, const QString& p_text, const QIcon& icon, QWidget* parent):
|
||||
QFrame(parent),
|
||||
id(p_id),
|
||||
|
@ -48,8 +50,7 @@ Badge::Badge(QWidget* parent):
|
|||
layout->addWidget(closeButton);
|
||||
}
|
||||
|
||||
void Badge::setIcon(const QIcon& icon)
|
||||
{
|
||||
void Badge::setIcon(const QIcon& icon) {
|
||||
if (image == nullptr) {
|
||||
image = new QLabel();
|
||||
image->setPixmap(icon.pixmap(25, 25));
|
||||
|
@ -59,8 +60,7 @@ void Badge::setIcon(const QIcon& icon)
|
|||
}
|
||||
}
|
||||
|
||||
void Badge::setText(const QString& p_text)
|
||||
{
|
||||
void Badge::setText(const QString& p_text) {
|
||||
if (text == nullptr) {
|
||||
text = new QLabel(p_text);
|
||||
int index = 0;
|
||||
|
@ -73,42 +73,30 @@ void Badge::setText(const QString& p_text)
|
|||
}
|
||||
}
|
||||
|
||||
Badge::~Badge()
|
||||
{
|
||||
if (image != nullptr) {
|
||||
Badge::~Badge() {
|
||||
if (image != nullptr)
|
||||
delete image;
|
||||
}
|
||||
if (text != nullptr) {
|
||||
|
||||
if (text != nullptr)
|
||||
delete text;
|
||||
}
|
||||
|
||||
delete closeButton;
|
||||
}
|
||||
|
||||
bool Badge::Comparator::operator()(const Badge* a, const Badge* b) const
|
||||
{
|
||||
return a->id < b->id;
|
||||
}
|
||||
|
||||
bool Badge::Comparator::operator()(const Badge& a, const Badge& b) const
|
||||
{
|
||||
return a.id < b.id;
|
||||
}
|
||||
|
||||
void Badge::createMandatoryComponents()
|
||||
{
|
||||
void Badge::createMandatoryComponents() {
|
||||
setBackgroundRole(QPalette::Base);
|
||||
//setAutoFillBackground(true);
|
||||
setFrameStyle(QFrame::StyledPanel);
|
||||
setFrameShadow(QFrame::Raised);
|
||||
|
||||
QIcon tabCloseIcon = QIcon::fromTheme("tab-close");
|
||||
if (tabCloseIcon.isNull()) {
|
||||
if (tabCloseIcon.isNull())
|
||||
tabCloseIcon.addFile(QString::fromUtf8(":/images/fallback/dark/big/edit-none.svg"), QSize(), QIcon::Normal, QIcon::Off);
|
||||
}
|
||||
|
||||
closeButton->setIcon(tabCloseIcon);
|
||||
|
||||
closeButton->setMaximumHeight(25);
|
||||
closeButton->setMaximumWidth(25);
|
||||
layout->setContentsMargins(2, 2, 2, 2);
|
||||
connect(closeButton, &QPushButton::clicked, this, &Badge::close);
|
||||
connect(closeButton, &QPushButton::clicked, this, &Badge::closeClicked);
|
||||
}
|
||||
|
|
|
@ -16,8 +16,7 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef BADGE_H
|
||||
#define BADGE_H
|
||||
#pragma once
|
||||
|
||||
#include <QFrame>
|
||||
#include <QLabel>
|
||||
|
@ -25,13 +24,7 @@
|
|||
#include <QIcon>
|
||||
#include <QPushButton>
|
||||
|
||||
#include "shared/utils.h"
|
||||
|
||||
/**
|
||||
* @todo write docs
|
||||
*/
|
||||
class Badge : public QFrame
|
||||
{
|
||||
class Badge : public QFrame {
|
||||
Q_OBJECT
|
||||
public:
|
||||
Badge(const QString& id, const QString& text, const QIcon& icon, QWidget* parent = nullptr);
|
||||
|
@ -45,7 +38,7 @@ public:
|
|||
void setIcon(const QIcon& icon);
|
||||
|
||||
signals:
|
||||
void close();
|
||||
void closeClicked();
|
||||
|
||||
private:
|
||||
QLabel* image;
|
||||
|
@ -55,12 +48,5 @@ private:
|
|||
|
||||
private:
|
||||
void createMandatoryComponents();
|
||||
|
||||
public:
|
||||
struct Comparator {
|
||||
bool operator()(const Badge& a, const Badge& b) const;
|
||||
bool operator()(const Badge* a, const Badge* b) const;
|
||||
};
|
||||
};
|
||||
|
||||
#endif // BADGE_H
|
||||
};
|
||||
|
|
|
@ -16,8 +16,7 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef FLOWLAYOUT_H
|
||||
#define FLOWLAYOUT_H
|
||||
#pragma once
|
||||
|
||||
#include <QLayout>
|
||||
#include <QWidget>
|
||||
|
@ -26,8 +25,7 @@
|
|||
/**
|
||||
* @todo write docs
|
||||
*/
|
||||
class FlowLayout : public QLayout
|
||||
{
|
||||
class FlowLayout : public QLayout {
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit FlowLayout(QWidget *parent, int margin = -1, int hSpacing = -1, int vSpacing = -1);
|
||||
|
@ -55,5 +53,3 @@ private:
|
|||
int m_hSpace;
|
||||
int m_vSpace;
|
||||
};
|
||||
|
||||
#endif // FLOWLAYOUT_H
|
||||
|
|
|
@ -33,6 +33,12 @@
|
|||
#include <QBitmap>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <algorithm>
|
||||
|
||||
#include "shared/icons.h"
|
||||
#include "shared/utils.h"
|
||||
#include "shared/pathcheck.h"
|
||||
#include "shared/defines.h"
|
||||
|
||||
constexpr QSize avatarSize(50, 50);
|
||||
|
||||
|
@ -61,58 +67,9 @@ Conversation::Conversation(bool muc, Models::Account* acc, Models::Element* el,
|
|||
currentAction(CurrentAction::none),
|
||||
currentMessageId()
|
||||
{
|
||||
m_ui->setupUi(this);
|
||||
|
||||
shadow.setFrames(true, false, true, false);
|
||||
|
||||
feed->setItemDelegate(delegate);
|
||||
feed->setFrameShape(QFrame::NoFrame);
|
||||
feed->setContextMenuPolicy(Qt::CustomContextMenu);
|
||||
feed->setModel(el->feed);
|
||||
el->feed->incrementObservers();
|
||||
m_ui->widget->layout()->addWidget(feed);
|
||||
|
||||
connect(el->feed, &Models::MessageFeed::newMessage, this, &Conversation::onFeedMessage);
|
||||
connect(feed, &FeedView::resized, this, &Conversation::positionShadow);
|
||||
connect(feed, &FeedView::customContextMenuRequested, this, &Conversation::onFeedContext);
|
||||
|
||||
connect(acc, &Models::Account::childChanged, this, &Conversation::onAccountChanged);
|
||||
|
||||
filesLayout = new FlowLayout(m_ui->filesPanel, 0);
|
||||
m_ui->filesPanel->setLayout(filesLayout);
|
||||
|
||||
statusIcon = m_ui->statusIcon;
|
||||
statusLabel = m_ui->statusLabel;
|
||||
|
||||
connect(&ker, &KeyEnterReceiver::enterPressed, this, qOverload<>(&Conversation::initiateMessageSending));
|
||||
connect(&ker, &KeyEnterReceiver::imagePasted, this, &Conversation::onImagePasted);
|
||||
connect(m_ui->sendButton, &QPushButton::clicked, this, qOverload<>(&Conversation::initiateMessageSending));
|
||||
connect(m_ui->attachButton, &QPushButton::clicked, this, &Conversation::onAttach);
|
||||
connect(m_ui->clearButton, &QPushButton::clicked, this, &Conversation::clear);
|
||||
connect(m_ui->messageEditor->document()->documentLayout(), &QAbstractTextDocumentLayout::documentSizeChanged,
|
||||
this, &Conversation::onTextEditDocSizeChanged);
|
||||
connect(m_ui->encryptionButton, &QPushButton::clicked, this, &Conversation::onEncryptionButtonClicked);
|
||||
|
||||
m_ui->messageEditor->installEventFilter(&ker);
|
||||
|
||||
connect(m_ui->messageEditor, &QTextEdit::customContextMenuRequested, this, &Conversation::onMessageEditorContext);
|
||||
connect(pasteImageAction, &QAction::triggered, this, &Conversation::onImagePasted);
|
||||
|
||||
connect(m_ui->currentActionBadge, &Badge::close, this, &Conversation::clear);
|
||||
m_ui->currentActionBadge->setVisible(false);
|
||||
|
||||
m_ui->encryptionButton->setVisible(false);
|
||||
|
||||
//line->setAutoFillBackground(false);
|
||||
//if (testAttribute(Qt::WA_TranslucentBackground)) {
|
||||
//m_ui->scrollArea->setAutoFillBackground(false);
|
||||
//} else {
|
||||
//m_ui->scrollArea->setBackgroundRole(QPalette::Base);
|
||||
//}
|
||||
|
||||
//line->setMyAvatarPath(acc->getAvatarPath());
|
||||
//line->setMyName(acc->getName());
|
||||
|
||||
createUI();
|
||||
createFeed();
|
||||
subscribeEvents();
|
||||
initializeOverlay();
|
||||
}
|
||||
|
||||
|
@ -123,6 +80,52 @@ Conversation::~Conversation() {
|
|||
delete m_ui;
|
||||
}
|
||||
|
||||
void Conversation::createFeed() {
|
||||
feed->setItemDelegate(delegate);
|
||||
feed->setFrameShape(QFrame::NoFrame);
|
||||
feed->setContextMenuPolicy(Qt::CustomContextMenu);
|
||||
feed->setModel(element->feed);
|
||||
element->feed->incrementObservers();
|
||||
m_ui->widget->layout()->addWidget(feed);
|
||||
|
||||
connect(element->feed, &Models::MessageFeed::newMessage, this, &Conversation::onFeedMessage);
|
||||
connect(feed, &FeedView::resized, this, &Conversation::positionShadow);
|
||||
connect(feed, &FeedView::customContextMenuRequested, this, &Conversation::onFeedContext);
|
||||
}
|
||||
|
||||
void Conversation::createUI() {
|
||||
m_ui->setupUi(this);
|
||||
statusIcon = m_ui->statusIcon;
|
||||
statusLabel = m_ui->statusLabel;
|
||||
|
||||
filesLayout = new FlowLayout(m_ui->filesPanel, 0);
|
||||
m_ui->filesPanel->setLayout(filesLayout);
|
||||
|
||||
m_ui->currentActionBadge->setVisible(false);
|
||||
m_ui->encryptionButton->setVisible(false);
|
||||
|
||||
shadow.setFrames(true, false, true, false);
|
||||
}
|
||||
|
||||
void Conversation::subscribeEvents() {
|
||||
connect(account, &Models::Account::childChanged, this, &Conversation::onAccountChanged);
|
||||
connect(&ker, &KeyEnterReceiver::enterPressed, this, qOverload<>(&Conversation::initiateMessageSending));
|
||||
connect(&ker, &KeyEnterReceiver::imagePasted, this, &Conversation::onImagePasted);
|
||||
connect(m_ui->sendButton, &QPushButton::clicked, this, qOverload<>(&Conversation::initiateMessageSending));
|
||||
connect(m_ui->attachButton, &QPushButton::clicked, this, &Conversation::onAttach);
|
||||
connect(m_ui->clearButton, &QPushButton::clicked, this, &Conversation::clear);
|
||||
connect(m_ui->messageEditor->document()->documentLayout(), &QAbstractTextDocumentLayout::documentSizeChanged,
|
||||
this, &Conversation::onTextEditDocSizeChanged);
|
||||
connect(m_ui->encryptionButton, &QPushButton::clicked, this, &Conversation::onEncryptionButtonClicked);
|
||||
|
||||
m_ui->messageEditor->installEventFilter(&ker);
|
||||
|
||||
connect(m_ui->messageEditor, &QTextEdit::customContextMenuRequested, this, &Conversation::onMessageEditorContext);
|
||||
connect(pasteImageAction, &QAction::triggered, this, &Conversation::onImagePasted);
|
||||
|
||||
connect(m_ui->currentActionBadge, &Badge::closeClicked, this, &Conversation::clear);
|
||||
}
|
||||
|
||||
void Conversation::onAccountChanged(Models::Item* item, int row, int col) {
|
||||
SHARED_UNUSED(row);
|
||||
if (item == account) {
|
||||
|
@ -228,7 +231,7 @@ void Conversation::initiateMessageSending() {
|
|||
initiateMessageSending(msg);
|
||||
}
|
||||
if (filesToAttach.size() > 0) {
|
||||
for (Badge* badge : filesToAttach) {
|
||||
for (const Badge* badge : filesToAttach) {
|
||||
Shared::Message msg = createMessage();
|
||||
msg.setAttachPath(badge->id);
|
||||
element->feed->registerUpload(msg.getId());
|
||||
|
@ -291,6 +294,14 @@ Models::Roster::ElId Conversation::getId() const {
|
|||
}
|
||||
|
||||
void Conversation::addAttachedFile(const QString& path) {
|
||||
std::vector<Badge*>::const_iterator itr = std::find_if(
|
||||
filesToAttach.begin(),
|
||||
filesToAttach.end(),
|
||||
[&path] (const Badge* badge) {return badge->id == path;}
|
||||
);
|
||||
if (itr != filesToAttach.end())
|
||||
return;
|
||||
|
||||
QMimeDatabase db;
|
||||
QMimeType type = db.mimeTypeForFile(path);
|
||||
QFileInfo info(path);
|
||||
|
@ -300,25 +311,23 @@ void Conversation::addAttachedFile(const QString& path) {
|
|||
fileIcon.addFile(QString::fromUtf8(":/images/fallback/dark/big/mail-attachment.svg"), QSize(), QIcon::Normal, QIcon::Off);
|
||||
|
||||
Badge* badge = new Badge(path, info.fileName(), fileIcon);
|
||||
filesToAttach.push_back(badge);
|
||||
|
||||
connect(badge, &Badge::close, this, &Conversation::onBadgeClose);
|
||||
try {
|
||||
filesToAttach.push_back(badge);
|
||||
filesLayout->addWidget(badge);
|
||||
if (filesLayout->count() == 1) {
|
||||
filesLayout->setContentsMargins(3, 3, 3, 3);
|
||||
}
|
||||
} catch (const W::Order<Badge*, Badge::Comparator>::Duplicates& e) {
|
||||
delete badge;
|
||||
} catch (...) {
|
||||
throw;
|
||||
}
|
||||
connect(badge, &Badge::closeClicked, this, &Conversation::onBadgeClose);
|
||||
filesLayout->addWidget(badge);
|
||||
if (filesLayout->count() == 1)
|
||||
filesLayout->setContentsMargins(3, 3, 3, 3);
|
||||
}
|
||||
|
||||
void Conversation::removeAttachedFile(Badge* badge) {
|
||||
W::Order<Badge*, Badge::Comparator>::const_iterator itr = filesToAttach.find(badge);
|
||||
void Conversation::removeAttachedFile(const QString& id) {
|
||||
std::vector<Badge*>::const_iterator itr = std::find_if(
|
||||
filesToAttach.begin(),
|
||||
filesToAttach.end(),
|
||||
[&id] (const Badge* badge) {return badge->id == id;}
|
||||
);
|
||||
if (itr != filesToAttach.end()) {
|
||||
filesToAttach.erase(badge);
|
||||
Badge* badge = *itr;
|
||||
filesToAttach.erase(itr);
|
||||
if (filesLayout->count() == 1)
|
||||
filesLayout->setContentsMargins(0, 0, 0, 0);
|
||||
|
||||
|
@ -328,7 +337,7 @@ void Conversation::removeAttachedFile(Badge* badge) {
|
|||
|
||||
void Conversation::onBadgeClose() {
|
||||
Badge* badge = static_cast<Badge*>(sender());
|
||||
removeAttachedFile(badge);
|
||||
removeAttachedFile(badge->id);
|
||||
}
|
||||
|
||||
void Conversation::clearAttachedFiles() {
|
||||
|
@ -351,12 +360,10 @@ void Conversation::onEncryptionButtonClicked() {}
|
|||
|
||||
void Conversation::setAvatar(const QString& path) {
|
||||
QPixmap pixmap;
|
||||
if (path.size() == 0) {
|
||||
if (path.size() == 0)
|
||||
pixmap = Shared::icon("user", true).pixmap(avatarSize);
|
||||
} else {
|
||||
else
|
||||
pixmap = QPixmap(path).scaled(avatarSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
|
||||
}
|
||||
|
||||
|
||||
QPixmap result(avatarSize);
|
||||
result.fill(Qt::transparent);
|
||||
|
@ -379,8 +386,7 @@ void Conversation::setFeedFrames(bool top, bool right, bool bottom, bool left) {
|
|||
shadow.setFrames(top, right, bottom, left);
|
||||
}
|
||||
|
||||
void Conversation::dragEnterEvent(QDragEnterEvent* event)
|
||||
{
|
||||
void Conversation::dragEnterEvent(QDragEnterEvent* event) {
|
||||
bool accept = false;
|
||||
if (event->mimeData()->hasUrls()) {
|
||||
QList<QUrl> list = event->mimeData()->urls();
|
||||
|
@ -542,9 +548,8 @@ void Conversation::onMessageEditRequested(const QString& id) {
|
|||
currentAction = CurrentAction::edit;
|
||||
m_ui->messageEditor->setText(msg.getBody());
|
||||
QString path = msg.getAttachPath();
|
||||
if (path.size() > 0) {
|
||||
if (path.size() > 0)
|
||||
addAttachedFile(path);
|
||||
}
|
||||
|
||||
} catch (const Models::MessageFeed::NotFound& e) {
|
||||
qDebug() << "The message requested to be edited was not found" << e.getMessage().c_str();
|
||||
|
|
|
@ -16,8 +16,7 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef CONVERSATION_H
|
||||
#define CONVERSATION_H
|
||||
#pragma once
|
||||
|
||||
#include <QWidget>
|
||||
#include <QObject>
|
||||
|
@ -30,12 +29,9 @@
|
|||
#include <QAction>
|
||||
#include <QDesktopServices>
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "shared/message.h"
|
||||
#include "shared/order.h"
|
||||
#include "shared/icons.h"
|
||||
#include "shared/utils.h"
|
||||
#include "shared/pathcheck.h"
|
||||
#include "shared/defines.h"
|
||||
|
||||
#include "ui/models/account.h"
|
||||
#include "ui/models/roster.h"
|
||||
|
@ -96,7 +92,7 @@ protected:
|
|||
virtual Shared::Message createMessage() const;
|
||||
void setStatus(const QString& status);
|
||||
void addAttachedFile(const QString& path);
|
||||
void removeAttachedFile(Badge* badge);
|
||||
void removeAttachedFile(const QString& id);
|
||||
void clearAttachedFiles();
|
||||
void dragEnterEvent(QDragEnterEvent* event) override;
|
||||
void dragLeaveEvent(QDragLeaveEvent* event) override;
|
||||
|
@ -104,6 +100,10 @@ protected:
|
|||
void initializeOverlay();
|
||||
virtual void onMessage(const Shared::Message& msg);
|
||||
virtual void showEvent(QShowEvent * event) override;
|
||||
|
||||
void createFeed();
|
||||
void createUI();
|
||||
void subscribeEvents();
|
||||
|
||||
protected slots:
|
||||
void initiateMessageSending();
|
||||
|
@ -142,7 +142,7 @@ protected:
|
|||
QLabel* statusLabel;
|
||||
FlowLayout* filesLayout;
|
||||
QWidget* overlay;
|
||||
W::Order<Badge*, Badge::Comparator> filesToAttach;
|
||||
std::vector<Badge*> filesToAttach;
|
||||
FeedView* feed;
|
||||
MessageDelegate* delegate;
|
||||
bool manualSliderChange;
|
||||
|
@ -161,5 +161,3 @@ private:
|
|||
static QPixmap* avatarPixmap;
|
||||
static QPainter* avatarPainter;
|
||||
};
|
||||
|
||||
#endif // CONVERSATION_H
|
||||
|
|
|
@ -18,6 +18,8 @@
|
|||
|
||||
#include "room.h"
|
||||
|
||||
#include "shared/defines.h"
|
||||
|
||||
Room::Room(Models::Account* acc, Models::Room* p_room, QWidget* parent):
|
||||
Conversation(true, acc, p_room, p_room->getJid(), "", parent),
|
||||
room(p_room)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue