From 64209200833a3a983bb84f0544a0c40db51979dc Mon Sep 17 00:00:00 2001 From: wyf Date: Wed, 11 Dec 2019 16:57:28 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=8F=91=E9=80=81=E5=9B=BE=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/chatdialog.cpp | 201 ++++++++++++++++++++++++++++++++++++++++----- src/chatdialog.h | 25 ++++++ src/chatdialog.ui | 53 ++++++------ 3 files changed, 233 insertions(+), 46 deletions(-) diff --git a/src/chatdialog.cpp b/src/chatdialog.cpp index 79fe274..9aa165e 100644 --- a/src/chatdialog.cpp +++ b/src/chatdialog.cpp @@ -35,20 +35,6 @@ 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); @@ -56,6 +42,12 @@ ChatDialog::ChatDialog(QWidget *parent) : 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(); @@ -88,7 +80,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(); @@ -117,6 +109,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) { //设置用户名,显示在每次发消息的消息标题上 @@ -177,9 +203,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="<get(request); + } + else + { + insertInMessage(msg.body(), &time); + } } } @@ -230,9 +276,48 @@ 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(); + if(text!="") + { + qDebug()<<"Send Text is not Empty!"; + qDebug()<open(QIODevice::ReadOnly)) + { + qDebug()<<"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() @@ -282,3 +367,81 @@ void ChatDialog::closeChatDialog(ChatDialog *dialog) openedDialogs.erase(i); qDebug()<<" erase!"; } + +void ChatDialog::on_photoBtn_clicked() +{ + strFilePath = QFileDialog::getOpenFileName(this, QString::fromLocal8Bit("选择上传图片"), "./", tr("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() << "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() << "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:"<deleteLater(); +} + +void ChatDialog::LoadPicture(QNetworkReply *reply) +{ + qDebug() << "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(); + +} + diff --git a/src/chatdialog.h b/src/chatdialog.h index ffa34c6..1d5f605 100644 --- a/src/chatdialog.h +++ b/src/chatdialog.h @@ -7,6 +7,14 @@ #include #include #include +#include +#include +#include +#include +#include +#include +#include + namespace Ui { class ChatDialog; @@ -39,6 +47,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; @@ -55,16 +68,28 @@ class ChatDialog : public NoweBaseWindow QTextCursor save; int savepos; + QString strFilePath; + 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 diff --git a/src/chatdialog.ui b/src/chatdialog.ui index 4a25cd9..bdfa2f7 100644 --- a/src/chatdialog.ui +++ b/src/chatdialog.ui @@ -286,46 +286,43 @@ QScrollBar::add-page:vertical,QScrollBar::sub-page:vertical Qt::Horizontal - + - 10 - 14 - 25 - 25 + 50 + 20 + 111 + 20 - - :hover -{ - background-color: rgb(241, 241, 241); - padding: 3px; -} - - TextLabel + PushButton + + + + :/images/2.png:/images/2.png - + - 46 - 14 - 23 - 23 + 10 + 20 + 111 + 20 - - :hover -{ - background-color: rgb(241, 241, 241); - padding: 3px; -} - - TextLabel + PushButton + + + + :/images/1.png:/images/1.png + line + emojiBtn + photoBtn @@ -460,6 +457,8 @@ QScrollBar::add-page:vertical,QScrollBar::sub-page:vertical - + + + From 978e3ac92c789c6607217b976e6b49f72c7cd490 Mon Sep 17 00:00:00 2001 From: wyf Date: Wed, 11 Dec 2019 19:43:16 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=8F=91=E9=80=81=E5=9B=BE=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/chatdialog.cpp | 201 ++++++++++++++++++++++++++++++++++++++++----- src/chatdialog.h | 25 ++++++ src/chatdialog.ui | 53 ++++++------ 3 files changed, 233 insertions(+), 46 deletions(-) diff --git a/src/chatdialog.cpp b/src/chatdialog.cpp index 79fe274..9aa165e 100644 --- a/src/chatdialog.cpp +++ b/src/chatdialog.cpp @@ -35,20 +35,6 @@ 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); @@ -56,6 +42,12 @@ ChatDialog::ChatDialog(QWidget *parent) : 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(); @@ -88,7 +80,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(); @@ -117,6 +109,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) { //设置用户名,显示在每次发消息的消息标题上 @@ -177,9 +203,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="<get(request); + } + else + { + insertInMessage(msg.body(), &time); + } } } @@ -230,9 +276,48 @@ 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(); + if(text!="") + { + qDebug()<<"Send Text is not Empty!"; + qDebug()<open(QIODevice::ReadOnly)) + { + qDebug()<<"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() @@ -282,3 +367,81 @@ void ChatDialog::closeChatDialog(ChatDialog *dialog) openedDialogs.erase(i); qDebug()<<" erase!"; } + +void ChatDialog::on_photoBtn_clicked() +{ + strFilePath = QFileDialog::getOpenFileName(this, QString::fromLocal8Bit("选择上传图片"), "./", tr("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() << "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() << "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:"<deleteLater(); +} + +void ChatDialog::LoadPicture(QNetworkReply *reply) +{ + qDebug() << "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(); + +} + diff --git a/src/chatdialog.h b/src/chatdialog.h index ffa34c6..1d5f605 100644 --- a/src/chatdialog.h +++ b/src/chatdialog.h @@ -7,6 +7,14 @@ #include #include #include +#include +#include +#include +#include +#include +#include +#include + namespace Ui { class ChatDialog; @@ -39,6 +47,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; @@ -55,16 +68,28 @@ class ChatDialog : public NoweBaseWindow QTextCursor save; int savepos; + QString strFilePath; + 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 diff --git a/src/chatdialog.ui b/src/chatdialog.ui index 4a25cd9..bdfa2f7 100644 --- a/src/chatdialog.ui +++ b/src/chatdialog.ui @@ -286,46 +286,43 @@ QScrollBar::add-page:vertical,QScrollBar::sub-page:vertical Qt::Horizontal - + - 10 - 14 - 25 - 25 + 50 + 20 + 111 + 20 - - :hover -{ - background-color: rgb(241, 241, 241); - padding: 3px; -} - - TextLabel + PushButton + + + + :/images/2.png:/images/2.png - + - 46 - 14 - 23 - 23 + 10 + 20 + 111 + 20 - - :hover -{ - background-color: rgb(241, 241, 241); - padding: 3px; -} - - TextLabel + PushButton + + + + :/images/1.png:/images/1.png + line + emojiBtn + photoBtn @@ -460,6 +457,8 @@ QScrollBar::add-page:vertical,QScrollBar::sub-page:vertical - + + + From a51b9dc34605b5d5c92e93a8317610e8db5ae478 Mon Sep 17 00:00:00 2001 From: wyf Date: Thu, 12 Dec 2019 21:20:06 +0800 Subject: [PATCH 3/3] send pictures --- src/chatdialog.cpp | 34 +++++++++++++++++++--------------- src/chatdialog.h | 5 ----- src/chatdialog.ui | 3 +++ 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/chatdialog.cpp b/src/chatdialog.cpp index 9aa165e..37d3a6e 100644 --- a/src/chatdialog.cpp +++ b/src/chatdialog.cpp @@ -8,7 +8,11 @@ #include #include #include +#include +QString strFilePath; +int savepos; +QTextCursor save; QMap ChatDialog::openedDialogs; ChatDialog::ChatDialog(QWidget *parent) : @@ -49,6 +53,7 @@ ChatDialog::ChatDialog(QWidget *parent) : Nowe::myClient()->sendPacket(msg); }); + save=ui->messBox->textCursor(); savepos=ui->messBox->textCursor().position(); } @@ -277,13 +282,12 @@ void ChatDialog::on_sendBtn_clicked() { //点击发送按钮后发射信号,清空文本区 QString text=ui->contentBox->toPlainText(); - if(text!="") - { - qDebug()<<"Send Text is not Empty!"; - qDebug()<open(QIODevice::ReadOnly)) { - qDebug()<<"Open file failed!"; + qDebug()<::iterator i= openedDialogs.find(jid); openedDialogs.erase(i); - qDebug()<<" erase!"; + qDebug()<attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); - qDebug() << "statusCode:" << statusCode; + qDebug() <error() == QNetworkReply::NoError) { QByteArray allData = reply->readAll(); @@ -396,7 +400,7 @@ void ChatDialog::Generatelink(QNetworkReply* reply) QJsonDocument jsonDoc(QJsonDocument::fromJson(allData, &json_error)); if(json_error.error != QJsonParseError::NoError) { - qDebug() << "json error!"; + qDebug() <deleteLater(); @@ -430,7 +434,7 @@ void ChatDialog::Generatelink(QNetworkReply* reply) void ChatDialog::LoadPicture(QNetworkReply *reply) { - qDebug() << "reply :" << reply<< endl; + qDebug() << QString("reply :") << reply<< endl; if(reply->error() == QNetworkReply::NoError) { diff --git a/src/chatdialog.h b/src/chatdialog.h index 1d5f605..4eaea6c 100644 --- a/src/chatdialog.h +++ b/src/chatdialog.h @@ -5,9 +5,7 @@ #include #include #include -#include #include -#include #include #include #include @@ -65,10 +63,7 @@ class ChatDialog : public NoweBaseWindow QString receiver; QString timeFormat; QPixmap *avatar; - QTextCursor save; - int savepos; - QString strFilePath; QImage *image; QString picUrl; diff --git a/src/chatdialog.ui b/src/chatdialog.ui index bdfa2f7..c003cf8 100644 --- a/src/chatdialog.ui +++ b/src/chatdialog.ui @@ -433,6 +433,9 @@ QScrollBar::add-page:vertical,QScrollBar::sub-page:vertical 发送 + + Return +