Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

发送图片 #27

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
207 changes: 187 additions & 20 deletions src/chatdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
#include <NoweGlobal.h>
#include <QXmppUtils.h>
#include <QScrollBar>
#include <QFileDialog>

QString strFilePath;
int savepos;
QTextCursor save;
QMap<QString,ChatDialog *> ChatDialog::openedDialogs;

ChatDialog::ChatDialog(QWidget *parent) :
Expand All @@ -35,27 +39,20 @@ ChatDialog::ChatDialog(QWidget *parent) :
plainFormat.setFont(QFont("微软雅黑",10));
plainFormat.setForeground(Qt::black);

//设置按钮1,默认是表情按钮
QPixmap button1;
button1.load(":/images/1.png");
button1.scaled(20,20);
ui->button1->setPixmap(button1);
ui->button1->setScaledContents(true);

//设置按钮2,图片按钮
QPixmap button2;
button2.load(":/images/2.png");
button2.scaled(20,20);
ui->button2->setPixmap(button2);
ui->button2->setScaledContents(true);

connect(Nowe::myClient(), &QXmppClient::messageReceived, this, &ChatDialog::on_messageReceived);
connect(this, &ChatDialog::newMessage, [=](QString sender,QString receiver,QDateTime time,QString content) {
QXmppMessage msg(sender, receiver, content);
msg.setType(QXmppMessage::Chat);
msg.setStamp(time);
Nowe::myClient()->sendPacket(msg);
});
connect(this, &ChatDialog::newPicture, [=](QString sender,QString receiver,QDateTime time,QString content) {
QXmppMessage msg(sender, receiver, content);
msg.setType(QXmppMessage::Chat);
msg.setStamp(time);
Nowe::myClient()->sendPacket(msg);
});


save=ui->messBox->textCursor();
savepos=ui->messBox->textCursor().position();
Expand Down Expand Up @@ -88,7 +85,7 @@ void ChatDialog::insertOutMessage(QString msg)
cursor.setCharFormat(plainFormat);
ui->messBox->moveCursor(QTextCursor::End);
cursor.insertText(msg);
//cursor.insertText("\n");
cursor.insertText("\n");
save=ui->messBox->textCursor();
savepos=ui->messBox->textCursor().position();
scrollBarAdjust();
Expand Down Expand Up @@ -117,6 +114,40 @@ void ChatDialog::insertInMessage(QString msg,QDateTime *time)
scrollBarAdjust();
}

void ChatDialog::insertOutPicture(QImage *image)
{
ui->messBox->moveCursor(QTextCursor::End);
QTextCursor cursor=ui->messBox->textCursor();
ui->messBox->moveCursor(QTextCursor::End);
cursor.insertBlock(outMsgFormat);
ui->messBox->moveCursor(QTextCursor::End);
cursor.insertImage(*image);
cursor.insertText("\n");
save=ui->messBox->textCursor();
savepos=ui->messBox->textCursor().position();
scrollBarAdjust();
}

void ChatDialog::insertInPicture(QImage *image,QDateTime *time)
{
ui->messBox->moveCursor(QTextCursor::End);
QTextCursor cursor=ui->messBox->textCursor();
cursor.insertBlock(inMsgFormat);
cursor.setCharFormat(inMsgCharFormat);
cursor.insertText(receiver);
cursor.insertText(" ");
if(time==nullptr)
//如果没有传入时间,就用系统当前时间
cursor.insertText(QDateTime::currentDateTime().toString(timeFormat));
else
cursor.insertText(time->toString(timeFormat));
cursor.insertText("\n");

cursor.insertImage(*image);
cursor.insertText("\n");
scrollBarAdjust();
}

void ChatDialog::setUserName(QString usr)
{
//设置用户名,显示在每次发消息的消息标题上
Expand Down Expand Up @@ -177,9 +208,29 @@ void ChatDialog::setOutMsgFormat(QFont target,Qt::GlobalColor color)

void ChatDialog::on_messageReceived(const QXmppMessage &msg)
{
if (QXmppUtils::jidToBareJid(msg.from()) == bareJid) {
if (QXmppUtils::jidToBareJid(msg.from()) == bareJid)
{
auto time = msg.stamp();
insertInMessage(msg.body(), &time);

if(msg.body().mid(0,3)=="URL")
{
QString url_string=msg.body().mid(4,-1);
QUrl url(url_string);
qDebug()<<"QUrl="<<url;

QNetworkRequest request;
request.setUrl(url);

//网络载体
QNetworkAccessManager *networkAccessManager2=new QNetworkAccessManager(this);
//网络载体的响应接收信号,与响应接收槽绑定
connect(networkAccessManager2, SIGNAL(finished(QNetworkReply*)), this, SLOT(LoadPicture(QNetworkReply*)));
networkAccessManager2->get(request);
}
else
{
insertInMessage(msg.body(), &time);
}
}
}

Expand Down Expand Up @@ -230,9 +281,47 @@ void ChatDialog::scrollBarAdjust()
void ChatDialog::on_sendBtn_clicked()
{
//点击发送按钮后发射信号,清空文本区
insertOutMessage(ui->contentBox->toPlainText());
emit newMessage(sender,receiver,QDateTime::currentDateTime(),ui->contentBox->toPlainText());
QString text=ui->contentBox->toPlainText();

insertOutMessage(text);
if(!strFilePath.isEmpty())
text=QString("发送了一个图片 ")+text;
emit newMessage(sender,receiver,QDateTime::currentDateTime(),text);


if(!strFilePath.isEmpty())
{
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);

QHttpPart imagePart;
imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/png"));
imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"smfile\"; filename=\"image.png\""));

QFile *file = new QFile(strFilePath);
if(!file->open(QIODevice::ReadOnly))
{
qDebug()<<QString("Open file failed!");
}
else
{
imagePart.setBodyDevice(file);
multiPart->append(imagePart);
}

QNetworkRequest request;
request.setUrl(QUrl("https://sm.ms/api/upload"));
//网络载体
QNetworkAccessManager *networkAccessManager1=new QNetworkAccessManager(this);
//网络载体的响应接收信号,与响应接收槽绑定
connect(networkAccessManager1, SIGNAL(finished(QNetworkReply*)), this, SLOT(Generatelink(QNetworkReply*)));
networkAccessManager1->post(request,multiPart);
insertOutPicture(image);
}

//clear
ui->contentBox->clear();
picUrl="";
strFilePath="";
}

void ChatDialog::on_cancleBtn_clicked()
Expand Down Expand Up @@ -280,5 +369,83 @@ void ChatDialog::closeChatDialog(ChatDialog *dialog)
QString jid=openedDialogs.key(dialog);
QMap<QString,ChatDialog *>::iterator i= openedDialogs.find(jid);
openedDialogs.erase(i);
qDebug()<<" erase!";
qDebug()<<QString(" erase!");
}

void ChatDialog::on_photoBtn_clicked()
{
strFilePath = QFileDialog::getOpenFileName(this, QString::fromLocal8Bit("选择上传图片"), "./", QString("Image files(*.bmp *.jpg *.pbm *.pgm *.png *.ppm *.xbm *.xpm *.jpeg);;All files (*.*)"));
if (strFilePath.isEmpty())
{
return;
}
image=new QImage();
image->load(strFilePath);
image->scaled(40,30,Qt::IgnoreAspectRatio, Qt::SmoothTransformation);

ui->contentBox->moveCursor(QTextCursor::End);
QTextCursor cursor=ui->contentBox->textCursor();
cursor.insertImage(*image);

}

void ChatDialog::Generatelink(QNetworkReply* reply)
{
int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
qDebug() <<QString( "statusCode:" )<< statusCode;
if(reply->error() == QNetworkReply::NoError)
{
QByteArray allData = reply->readAll();
QJsonParseError json_error;
QJsonDocument jsonDoc(QJsonDocument::fromJson(allData, &json_error));
if(json_error.error != QJsonParseError::NoError)
{
qDebug() <<QString( "json error!");
return;
}
QJsonObject rootObj = jsonDoc.object();

QString content;
if(rootObj.contains("data"))
{
QJsonObject subObj = rootObj.value("data").toObject();
picUrl=subObj["url"].toString();
content.append("URL=");
content.append(picUrl);
emit newPicture(sender,receiver,QDateTime::currentDateTime(),content);
qDebug()<<"answer:"<<picUrl;
}
else
{
picUrl=rootObj["images"].toString();
content.append("URL=");
content.append(picUrl);
emit newPicture(sender,receiver,QDateTime::currentDateTime(),content);
qDebug()<<QString("answer:")<<picUrl;
}
}
else
{
qDebug() << QString("NetworkReply Error!");
}

reply->deleteLater();
}

void ChatDialog::LoadPicture(QNetworkReply *reply)
{
qDebug() << QString("reply :") << reply<< endl;

if(reply->error() == QNetworkReply::NoError)
{
QByteArray data_bytes = reply->readAll();

QImage *image_in=new QImage();
image_in->loadFromData(data_bytes);
image_in->scaled(40,30,Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
insertInPicture(image_in,nullptr);
}
reply->deleteLater();

}

26 changes: 23 additions & 3 deletions src/chatdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@
#include <QDialog>
#include <QTextDocument>
#include <QTextFrame>
#include <QDateTime>
#include <QXmppMessage.h>
#include <QNetworkReply>
#include <QHttpMultiPart>
#include <QJsonObject>
#include <QJsonParseError>
#include <QNetworkAccessManager>
#include <QKeyEvent>


namespace Ui {
class ChatDialog;
Expand Down Expand Up @@ -39,6 +45,11 @@ class ChatDialog : public NoweBaseWindow
void setBareJid(const QString &value);
void scrollBarAdjust();

void insertOutPicture(QImage *image);
void insertInPicture(QImage *image,QDateTime *time);



private:
Ui::ChatDialog *ui;
QTextDocument *document;
Expand All @@ -52,19 +63,28 @@ class ChatDialog : public NoweBaseWindow
QString receiver;
QString timeFormat;
QPixmap *avatar;
QTextCursor save;
int savepos;

QImage *image;
QString picUrl;



public slots:
void on_messageReceived(const QXmppMessage &msg);
void windowclosed() override;

signals:
void newMessage(QString sender,QString receiver,QDateTime time,QString content);
void newPicture(QString sender,QString receiver,QDateTime time,QString content);

private slots:
void on_cancleBtn_clicked();
void on_sendBtn_clicked();
void on_messBox_cursorPositionChanged();

void on_photoBtn_clicked();
void Generatelink(QNetworkReply* reply);
void LoadPicture(QNetworkReply *reply);
};

#endif // CHATDIALOG_H
Loading