From add4695fbdc898652dbc9b3f4dcace390642cb33 Mon Sep 17 00:00:00 2001 From: doitux Date: Fri, 12 Oct 2007 21:16:02 +0000 Subject: [PATCH] start chat-implementing in startnetworkgamedialog with only chattools ;) --- src/gui/qt/chattools/chattools.cpp | 26 ++++++++++- src/gui/qt/chattools/chattools.h | 9 +++- src/gui/qt/startnetworkgamedialog.ui | 2 +- .../startnetworkgamedialogimpl.cpp | 45 ++++++++++++++++--- .../startnetworkgamedialogimpl.h | 10 ++++- 5 files changed, 80 insertions(+), 12 deletions(-) diff --git a/src/gui/qt/chattools/chattools.cpp b/src/gui/qt/chattools/chattools.cpp index 396640aa..afc680e6 100644 --- a/src/gui/qt/chattools/chattools.cpp +++ b/src/gui/qt/chattools/chattools.cpp @@ -23,7 +23,7 @@ using namespace std; -ChatTools::ChatTools(QLineEdit* l, QTreeWidget *t) : nickAutoCompletitionCounter(0), myLineEdit(l), myNickTreeWidget(t), myNickStringList(NULL) +ChatTools::ChatTools(QLineEdit* l, QTreeWidget *t, QTextBrowser *b) : nickAutoCompletitionCounter(0), myLineEdit(l), myNickTreeWidget(t), myNickStringList(NULL), myTextBrowser(b) { } @@ -33,6 +33,30 @@ ChatTools::~ChatTools() } +void ChatTools::sendMessage() { + + fillChatLinesHistory(myLineEdit->text()); +// myW->getSession().sendChatMessage(myW->lineEdit_ChatInput->text().toUtf8().constData()); + myLineEdit->setText(""); +} + +void ChatTools::receiveMessage(QString playerName, QString message) { + + if(myTextBrowser) + myTextBrowser->append(playerName + ": " + message); +} + +void ChatTools::clearChat() { + + if(myTextBrowser) + myTextBrowser->clear(); +} + +void ChatTools::checkInputLength(QString string) { + + if(string.toUtf8().length() > 120) myLineEdit->setMaxLength(string.length()); +} + void ChatTools::fillChatLinesHistory(QString fillString) { chatLinesHistory << fillString; diff --git a/src/gui/qt/chattools/chattools.h b/src/gui/qt/chattools/chattools.h index 96c55cb8..ce93f615 100644 --- a/src/gui/qt/chattools/chattools.h +++ b/src/gui/qt/chattools/chattools.h @@ -29,12 +29,17 @@ class ChatTools : public QObject Q_OBJECT public: - ChatTools(QLineEdit* l, QTreeWidget *t = NULL); + ChatTools(QLineEdit* l, QTreeWidget *t = NULL, QTextBrowser *b = NULL); ~ChatTools(); public slots: + void sendMessage(); + void receiveMessage(QString playerName, QString message); + void clearChat(); + void checkInputLength(QString string); + void fillChatLinesHistory(QString fillString); void showChatHistoryIndex(int index); int getChatLinesHistorySize() { return chatLinesHistory.size(); } @@ -53,6 +58,8 @@ private: QLineEdit* myLineEdit; QTreeWidget *myNickTreeWidget; QStringList myNickStringList; + QTextBrowser* myTextBrowser; + // friend class GuiWrapper; }; diff --git a/src/gui/qt/startnetworkgamedialog.ui b/src/gui/qt/startnetworkgamedialog.ui index 5f7cbadd..8ce0cb05 100644 --- a/src/gui/qt/startnetworkgamedialog.ui +++ b/src/gui/qt/startnetworkgamedialog.ui @@ -85,7 +85,7 @@ - Lobby-Chat + Chat diff --git a/src/gui/qt/startnetworkgamedialog/startnetworkgamedialogimpl.cpp b/src/gui/qt/startnetworkgamedialog/startnetworkgamedialogimpl.cpp index 1bbda9d1..dd1b630b 100644 --- a/src/gui/qt/startnetworkgamedialog/startnetworkgamedialogimpl.cpp +++ b/src/gui/qt/startnetworkgamedialog/startnetworkgamedialogimpl.cpp @@ -20,19 +20,25 @@ #include "startnetworkgamedialogimpl.h" #include "session.h" #include "configfile.h" +#include "chattools.h" #include startNetworkGameDialogImpl::startNetworkGameDialogImpl(QWidget *parent, ConfigFile *config) - : QDialog(parent), myW(NULL), myPlayerId(0), isAdmin(false), myConfig(config), mySession(NULL) + : QDialog(parent), myW(NULL), keyUpDownChatCounter(0), myPlayerId(0), isAdmin(false), myConfig(config), mySession(NULL), myChat(NULL) { setupUi(this); + myChat = new ChatTools(lineEdit_ChatInput, treeWidget, textBrowser_ChatDisplay); + + lineEdit_ChatInput->installEventFilter(this); + connect( pushButton_cancel, SIGNAL( clicked() ), this, SLOT( cancel() ) ); connect( pushButton_startGame, SIGNAL( clicked() ), this, SLOT( startGame() ) ); connect( pushButton_Kick, SIGNAL( clicked() ), this, SLOT( kickPlayer() ) ); connect( treeWidget, SIGNAL( currentItemChanged ( QTreeWidgetItem*, QTreeWidgetItem*) ), this, SLOT( playerSelected(QTreeWidgetItem*, QTreeWidgetItem*) ) ); - connect( lineEdit_ChatInput, SIGNAL( returnPressed () ), this, SLOT( sendChatMessage() ) ); - + connect( lineEdit_ChatInput, SIGNAL( returnPressed () ), myChat, SLOT( sendMessage() ) ); + connect( lineEdit_ChatInput, SIGNAL( textChanged (QString) ), myChat, SLOT( checkInputLength(QString) ) ); + connect( lineEdit_ChatInput, SIGNAL( textEdited (QString) ), myChat, SLOT( setChatTextEdited() ) ); clearDialog(); } @@ -175,6 +181,7 @@ void startNetworkGameDialogImpl::clearDialog() pushButton_Kick->setEnabled(false); pushButton_startGame->setEnabled(false); treeWidget->clear(); + myChat->clearChat(); checkBox_fillUpWithComputerOpponents->hide(); myPlayerId = 0; @@ -187,9 +194,33 @@ void startNetworkGameDialogImpl::setSession(Session *session) void startNetworkGameDialogImpl::keyPressEvent ( QKeyEvent * event ) { - - if (event->key() == 16777220) { pushButton_startGame->click(); } //ENTER - + if (event->key() == Qt::Key_Up && lineEdit_ChatInput->hasFocus()) { + if((keyUpDownChatCounter + 1) <= myChat->getChatLinesHistorySize()) { keyUpDownChatCounter++; } +// std::cout << "Up keyUpDownChatCounter: " << keyUpDownChatCounter << "\n"; + myChat->showChatHistoryIndex(keyUpDownChatCounter); + } + else if(event->key() == Qt::Key_Down && lineEdit_ChatInput->hasFocus()) { + if((keyUpDownChatCounter - 1) >= 0) { keyUpDownChatCounter--; } +// std::cout << "Down keyUpDownChatCounter: " << keyUpDownChatCounter << "\n"; + myChat->showChatHistoryIndex(keyUpDownChatCounter); + } + else { keyUpDownChatCounter = 0; } } -void startNetworkGameDialogImpl::sendChatMessage() { /*myChat->sendMessage();*/ } +bool startNetworkGameDialogImpl::eventFilter(QObject *obj, QEvent *event) +{ + QKeyEvent *keyEvent = static_cast(event); + + if (obj == lineEdit_ChatInput && lineEdit_ChatInput->text() != "" && event->type() == QEvent::KeyPress && keyEvent->key() == Qt::Key_Tab) + { + myChat->nickAutoCompletition(); + return true; + } else { + // pass the event on to the parent class + return QDialog::eventFilter(obj, event); + } +} + +void startNetworkGameDialogImpl::receiveChatMsg(QString playerName, QString message) { + myChat->receiveMessage(playerName, message); +} diff --git a/src/gui/qt/startnetworkgamedialog/startnetworkgamedialogimpl.h b/src/gui/qt/startnetworkgamedialog/startnetworkgamedialogimpl.h index 372bb8a5..6a854005 100644 --- a/src/gui/qt/startnetworkgamedialog/startnetworkgamedialogimpl.h +++ b/src/gui/qt/startnetworkgamedialog/startnetworkgamedialogimpl.h @@ -29,6 +29,7 @@ class Session; class ConfigFile; +class ChatTools; class startNetworkGameDialogImpl: public QDialog, public Ui::startNetworkGameDialog { Q_OBJECT @@ -56,23 +57,28 @@ public slots: void checkPlayerQuantity(); void clearDialog(); + void receiveChatMsg(QString playerName, QString message); + void keyPressEvent ( QKeyEvent*); + bool eventFilter(QObject *obj, QEvent *event); + void setMaxPlayerNumber ( int theValue ) { maxPlayerNumber = theValue; label_maxPlayerNumber->setText(QString::number(theValue,10)); } int getMaxPlayerNumber() const { return maxPlayerNumber; } void exec(); - void sendChatMessage(); - private: mainWindowImpl* myW; int maxPlayerNumber; + int keyUpDownChatCounter; unsigned myPlayerId; bool isAdmin; ConfigFile *myConfig; Session *mySession; + ChatTools *myChat; + }; #endif