Skip to content

Commit

Permalink
Support uploading attachments
Browse files Browse the repository at this point in the history
Closes #209.
  • Loading branch information
KitsuneRal committed Dec 28, 2018
1 parent ab44a15 commit d118620
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 12 deletions.
64 changes: 56 additions & 8 deletions client/chatroomwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QLabel>
#include <QtWidgets/QToolButton>
#include <QtWidgets/QAction>
#include <QtWidgets/QFileDialog>
#include <QtWidgets/QApplication>

Expand Down Expand Up @@ -107,6 +109,32 @@ ChatRoomWidget::ChatRoomWidget(QWidget* parent)
m_hudCaption = new QLabel();
m_hudCaption->setWordWrap(true);

auto attachButton = new QToolButton();
attachButton->setAutoRaise(true);
m_attachAction = new QAction(QIcon::fromTheme("mail-attachment"),
tr("Attach"), attachButton);
m_attachAction->setCheckable(true);
m_attachAction->setDisabled(true);
connect(m_attachAction, &QAction::triggered, this, [this] (bool checked) {
if (checked)
attachedFileName =
QFileDialog::getOpenFileName(this, tr("Attach file"));
else
attachedFileName.clear();

if (!attachedFileName.isEmpty())
{
m_chatEdit->setPlaceholderText(
tr("Add a message to the file or just push Enter"));
emit showStatusMessage(tr("Attaching ") + attachedFileName);
} else {
m_attachAction->setChecked(false);
m_chatEdit->setPlaceholderText(DefaultPlaceholderText);
emit showStatusMessage(tr("Attaching cancelled"), 3000);
}
});
attachButton->setDefaultAction(m_attachAction);

m_chatEdit = new ChatEdit(this);
m_chatEdit->setPlaceholderText(DefaultPlaceholderText);
m_chatEdit->setAcceptRichText(false);
Expand All @@ -122,16 +150,21 @@ ChatRoomWidget::ChatRoomWidget(QWidget* parent)
this, &ChatRoomWidget::typingChanged);

auto layout = new QVBoxLayout();

auto headerLayout = new QHBoxLayout;
headerLayout->addWidget(m_roomAvatar);
headerLayout->addWidget(m_topicLabel, 1);
layout->addLayout(headerLayout);

{
auto headerLayout = new QHBoxLayout;
headerLayout->addWidget(m_roomAvatar);
headerLayout->addWidget(m_topicLabel, 1);
layout->addLayout(headerLayout);
}
layout->addWidget(topicSeparator);
layout->addWidget(qmlContainer);
layout->addWidget(m_hudCaption);
layout->addWidget(m_chatEdit);
{
auto inputLayout = new QHBoxLayout;
inputLayout->addWidget(attachButton);
inputLayout->addWidget(m_chatEdit);
layout->addLayout(inputLayout);
}
setLayout(layout);
}

Expand Down Expand Up @@ -159,10 +192,13 @@ void ChatRoomWidget::setRoom(QuaternionRoom* room)
readMarkerOnScreen = false;
maybeReadTimer.stop();
indicesOnScreen.clear();
attachedFileName.clear();
m_attachAction->setChecked(false);
m_chatEdit->cancelCompletion();

m_currentRoom = room;
m_timelineWidget->rootContext()->setContextProperty("room", room);
m_attachAction->setEnabled(m_currentRoom != nullptr);
if( m_currentRoom )
{
using namespace QMatrixClient;
Expand Down Expand Up @@ -294,7 +330,19 @@ QVector<QString> lazySplitRef(const QString& s, QChar sep, int maxParts)

QString ChatRoomWidget::doSendInput()
{
QString text = m_chatEdit->toPlainText();
auto text = m_chatEdit->toPlainText();
if (!attachedFileName.isEmpty())
{
Q_ASSERT(m_currentRoom != nullptr);
auto txnId = m_currentRoom->postFile(text,
QUrl::fromLocalFile(attachedFileName));

attachedFileName.clear();
m_attachAction->setChecked(false);
m_chatEdit->setPlaceholderText(DefaultPlaceholderText);
return {};
}

if ( text.isEmpty() )
return tr("There's nothing to send");

Expand Down
14 changes: 10 additions & 4 deletions client/chatroomwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class QQuickView;
class QQuickWidget;
#endif
class QLabel;
class QAction;
class QTextDocument;

class ChatRoomWidget: public QWidget
Expand Down Expand Up @@ -79,27 +80,32 @@ class ChatRoomWidget: public QWidget
void setHudCaption(QString newCaption);

private:
// Data
MessageEventModel* m_messageModel;
QuaternionRoom* m_currentRoom;
ImageProvider* m_imageProvider;

#ifdef DISABLE_QQUICKWIDGET
using timelineWidget_t = QQuickView;
#else
using timelineWidget_t = QQuickWidget;
#endif
// Controls
QLabel* m_roomAvatar;
QLabel* m_topicLabel;
timelineWidget_t* m_timelineWidget;
ImageProvider* m_imageProvider;
ChatEdit* m_chatEdit;
QLabel* m_hudCaption; //< For typing and completion notifications
QLabel* m_topicLabel;
QLabel* m_roomAvatar;
QAction* m_attachAction;
ChatEdit* m_chatEdit;

// Supplementary/cache data members
using timeline_index_t = QMatrixClient::TimelineItem::index_t;
QVector<timeline_index_t> indicesOnScreen;
timeline_index_t indexToMaybeRead;
QBasicTimer maybeReadTimer;
bool readMarkerOnScreen;
QMap<QuaternionRoom*, QVector<QTextDocument*>> roomHistories;
QString attachedFileName;

void reStartShownTimer();
QString doSendInput();
Expand Down

0 comments on commit d118620

Please sign in to comment.