-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
331 additions
and
229 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
#include "ChatBox.h" | ||
#include "ui_ChatBox.h" | ||
|
||
ChatMsg::ChatMsg(){} | ||
ChatMsg::ChatMsg(string message, int msgLife):life(msgLife),msg(message){ | ||
} | ||
|
||
ChatBox::ChatBox(QWidget *parent) :QDialog(parent,Qt::FramelessWindowHint),ui(new Ui::ChatBox){ | ||
ui->setupUi(this); | ||
|
||
this->setAttribute(Qt::WA_TranslucentBackground); | ||
//Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint|Qt::SubWindow | ||
|
||
//资源读取 | ||
chatBoxPic[0] = QPixmap(GetFileDir("Pic\\chat0.png")); //top | ||
chatBoxPic[1] = QPixmap(GetFileDir("Pic\\chat1.png")); | ||
chatBoxPic[2] = QPixmap(GetFileDir("Pic\\chat2.png")); | ||
|
||
QTimer *timer = new QTimer(this); | ||
connect(timer, SIGNAL(timeout()), this, SLOT(timerUpDate())); | ||
timer->start(1000);//1s | ||
|
||
lines = 0; | ||
cancover = true; | ||
|
||
this->setMinimumWidth(219); | ||
this->setMaximumWidth(219); | ||
} | ||
|
||
int ChatBox::GetStrWidth(const string& str){ | ||
return this->fontMetrics().width(QString().fromStdString(str)); | ||
} | ||
|
||
void ChatBox::Say(string &text, int time){ | ||
//简单显示 | ||
if (time == 0 && (boxLife <= 0 || cancover)){ | ||
boxText = text; | ||
boxLife = 3; | ||
cancover = true; | ||
UpdateChatBoxDis(); | ||
this->update(); | ||
}else{ | ||
//复杂显示,加入消息队列 | ||
msgQueue.push(ChatMsg(text,time)); | ||
} | ||
} | ||
|
||
void ChatBox::UpdateChatBoxDis(){ | ||
|
||
lines = 0; | ||
bool nextline = true; | ||
|
||
//vector<string> strList; | ||
|
||
strList.clear(); | ||
|
||
/* | ||
for(size_t i = 0;i < cstrList.size();++i){ | ||
delete [] cstrList[i]; | ||
} | ||
*/ | ||
|
||
//static string punc = " !@#$%^&*()-_+={}[]|:;'<>?,./\""; | ||
size_t slen = boxText.size(); | ||
|
||
//a chinese equal two english | ||
for(size_t i = 0;i < slen;){ | ||
if(boxText[i] == '\n'){ | ||
nextline = true; | ||
i ++; | ||
continue; | ||
} | ||
if(nextline){ | ||
nextline = false; | ||
lines ++; | ||
strList.push_back(""); | ||
} | ||
|
||
bool zh = boxText[i] < 0; | ||
|
||
strList[lines - 1] += boxText[i++]; | ||
|
||
if(zh){ | ||
//UTF - 8 可变长字符 | ||
while((boxText[i]&0xC0) == 0x80) | ||
strList[lines - 1] += boxText[i++]; | ||
} | ||
if(GetStrWidth(strList[lines - 1]) >= widthPerLine){ | ||
nextline = true; | ||
} | ||
} | ||
|
||
//cstrList.clear(); | ||
/* | ||
for(size_t i = 0;i < strList.size();++i){ | ||
const char *cs = strList[i].c_str(); | ||
size_t cl = strList[i].size();//注意,strlen("ab") = 2,还要加上\0 | ||
char *newstr = new char[cl + 1]; | ||
memcpy(newstr,cs,cl); | ||
newstr[cl] = '\0'; | ||
cstrList.push_back(newstr); | ||
} | ||
*/ | ||
|
||
boxH = 13 + font_size * lines - (-4)+31;//(lines - 1) * font_size + 70;//(lines - 1) * font_size; | ||
this->setMinimumHeight(boxH); | ||
this->setMaximumHeight(boxH); | ||
} | ||
|
||
void ChatBox::timerUpDate(){ | ||
if(boxLife <= 0){ | ||
boxLife = 0; | ||
this->hide(); | ||
//update(); | ||
}else{ | ||
boxLife --; | ||
} | ||
UpdateChatBox(); | ||
} | ||
void ChatBox::UpdateChatBox(){ | ||
if (cancover || boxLife <= 0){ | ||
if (!msgQueue.empty()){ | ||
const ChatMsg &cm = msgQueue.front(); | ||
boxText = cm.msg; | ||
boxLife = cm.life; | ||
cancover = false; | ||
msgQueue.pop(); | ||
UpdateChatBoxDis(); | ||
this->update(); | ||
} | ||
} | ||
} | ||
|
||
void ChatBox::Update(){ | ||
if(boxLife > 0){ | ||
this->show(); | ||
this->update(); | ||
} | ||
} | ||
|
||
void ChatBox::paintEvent(QPaintEvent *){ | ||
if (boxLife > 0){ | ||
QRect pg = this->parentWidget()->geometry(); | ||
this->move(pg.x()-20,pg.y() - boxH); | ||
|
||
QPainter painter; | ||
painter.begin(this); | ||
|
||
//这里要进行优化,只在设定信息时更新行距 | ||
//17,13,31 | ||
painter.setPen(Qt::black); | ||
painter.drawPixmap(0,0, 219, 17, chatBoxPic[0]); | ||
painter.drawPixmap(0, 17 + font_size * lines , 219, 31, chatBoxPic[2]); | ||
for(int i=0;i < lines;i++){ | ||
painter.drawPixmap(0, 17 + font_size * i , 219, font_size, chatBoxPic[1]); | ||
painter.drawText(16, 12 + font_size * i, 200, font_size, Qt::AlignBottom, strList[i].c_str()); | ||
} | ||
} | ||
} | ||
|
||
ChatBox::~ChatBox(){ | ||
delete ui; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#ifndef CHATBOX_H | ||
#define CHATBOX_H | ||
|
||
#include "Defines.h" | ||
|
||
struct ChatMsg{ | ||
int life; | ||
string msg; | ||
ChatMsg(); | ||
ChatMsg(string message,int msgLife); | ||
}; | ||
|
||
namespace Ui { | ||
class ChatBox; | ||
} | ||
|
||
class ChatBox : public QDialog | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
explicit ChatBox(QWidget *parent = 0); | ||
~ChatBox(); | ||
public: | ||
void Say(string &text,int time); | ||
void Update(); | ||
private: | ||
void UpdateChatBox(); | ||
void UpdateChatBoxDis(); | ||
int GetStrWidth(const string& str); | ||
void paintEvent(QPaintEvent *); | ||
private slots: | ||
void timerUpDate(); | ||
private: | ||
Ui::ChatBox *ui; | ||
QPixmap chatBoxPic[3]; | ||
|
||
//聊天窗口 | ||
int boxLife; | ||
string boxText; | ||
bool cancover;//可覆盖 | ||
queue<ChatMsg> msgQueue;//消息队列 | ||
const int widthPerLine = 172; | ||
const int font_size = 28; | ||
int boxH; | ||
int lines; | ||
//vector<char*> cstrList; | ||
vector<string> strList; | ||
}; | ||
|
||
#endif // CHATBOX_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ui version="4.0"> | ||
<class>ChatBox</class> | ||
<widget class="QDialog" name="ChatBox"> | ||
<property name="geometry"> | ||
<rect> | ||
<x>0</x> | ||
<y>0</y> | ||
<width>219</width> | ||
<height>39</height> | ||
</rect> | ||
</property> | ||
<property name="windowTitle"> | ||
<string>Dialog</string> | ||
</property> | ||
</widget> | ||
<resources/> | ||
<connections/> | ||
</ui> |
Oops, something went wrong.