Commit 05ebb83b authored by Max-Wilhelm Bruker's avatar Max-Wilhelm Bruker
Browse files

improved banning; added [url] and [card] tags for chat

parent 4b84168b
......@@ -136,6 +136,7 @@ SOURCES += src/abstractcounter.cpp \
src/gamescene.cpp \
src/arrowitem.cpp \
src/arrowtarget.cpp \
src/tab.cpp \
src/tab_server.cpp \
src/tab_room.cpp \
src/tab_message.cpp \
......
......@@ -2,34 +2,41 @@
#include <QDateTime>
#include <QTextTable>
#include <QScrollBar>
#include <QMouseEvent>
#include <QDesktopServices>
#include "chatview.h"
ChatView::ChatView(const QString &_ownName, QWidget *parent)
: QTextEdit(parent), ownName(_ownName)
ChatView::ChatView(const QString &_ownName, bool _showTimestamps, QWidget *parent)
: QTextBrowser(parent), ownName(_ownName), showTimestamps(_showTimestamps)
{
setTextInteractionFlags(Qt::TextSelectableByMouse);
setReadOnly(true);
setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::LinksAccessibleByMouse);
setOpenLinks(false);
connect(this, SIGNAL(anchorClicked(const QUrl &)), this, SLOT(openLink(const QUrl &)));
}
void ChatView::appendMessage(QString sender, const QString &message)
void ChatView::appendMessage(QString sender, QString message)
{
QTextCursor cursor(document()->lastBlock());
cursor.movePosition(QTextCursor::End);
QTextBlockFormat blockFormat;
blockFormat.setBottomMargin(3);
blockFormat.setBottomMargin(2);
cursor.insertBlock(blockFormat);
QTextCharFormat timeFormat;
timeFormat.setForeground(Qt::black);
cursor.setCharFormat(timeFormat);
cursor.insertText(QDateTime::currentDateTime().toString("[hh:mm] "));
if (showTimestamps) {
QTextCharFormat timeFormat;
timeFormat.setForeground(Qt::black);
cursor.setCharFormat(timeFormat);
cursor.insertText(QDateTime::currentDateTime().toString("[hh:mm] "));
}
QTextCharFormat senderFormat;
if (sender == ownName) {
senderFormat.setFontWeight(QFont::Bold);
senderFormat.setForeground(Qt::red);
} else
senderFormat.setForeground(Qt::blue);
senderFormat.setForeground(QColor(0, 0, 254));
cursor.setCharFormat(senderFormat);
if (!sender.isEmpty())
sender.append(": ");
......@@ -39,7 +46,122 @@ void ChatView::appendMessage(QString sender, const QString &message)
if (sender.isEmpty())
messageFormat.setForeground(Qt::darkGreen);
cursor.setCharFormat(messageFormat);
cursor.insertText(message);
int from = 0, index = 0;
while ((index = message.indexOf('[', from)) != -1) {
cursor.insertText(message.left(index));
message = message.mid(index);
if (message.isEmpty())
break;
if (message.startsWith("[card]")) {
message = message.mid(6);
QTextCharFormat tempFormat = messageFormat;
tempFormat.setForeground(Qt::blue);
cursor.setCharFormat(tempFormat);
int closeTagIndex = message.indexOf("[/card]");
cursor.insertText(message.left(closeTagIndex));
cursor.setCharFormat(messageFormat);
if (closeTagIndex == -1)
message.clear();
else
message = message.mid(closeTagIndex + 7);
} else if (message.startsWith("[url]")) {
message = message.mid(5);
int closeTagIndex = message.indexOf("[/url]");
QString url = message.left(closeTagIndex);
if (!url.startsWith("http://"))
url.prepend("http://");
QTextCharFormat tempFormat = messageFormat;
tempFormat.setForeground(QColor(0, 0, 254));
tempFormat.setAnchor(true);
tempFormat.setAnchorHref(url);
cursor.setCharFormat(tempFormat);
cursor.insertText(url);
cursor.setCharFormat(messageFormat);
if (closeTagIndex == -1)
message.clear();
else
message = message.mid(closeTagIndex + 6);
} else
from = 1;
}
if (!message.isEmpty())
cursor.insertText(message);
verticalScrollBar()->setValue(verticalScrollBar()->maximum());
}
void ChatView::enterEvent(QEvent * /*event*/)
{
setMouseTracking(true);
}
void ChatView::leaveEvent(QEvent * /*event*/)
{
setMouseTracking(false);
}
QTextFragment ChatView::getFragmentUnderMouse(const QPoint &pos) const
{
QTextCursor cursor(cursorForPosition(pos));
QTextBlock block(cursor.block());
QTextBlock::iterator it;
for (it = block.begin(); !(it.atEnd()); ++it) {
QTextFragment frag = it.fragment();
if (frag.contains(cursor.position()))
return frag;
}
return QTextFragment();
}
QString ChatView::getCardNameUnderMouse(QTextFragment frag) const
{
if (frag.charFormat().foreground().color() == Qt::blue)
return frag.text();
return QString();
}
QString ChatView::getCardNameUnderMouse(const QPoint &pos) const
{
return getCardNameUnderMouse(getFragmentUnderMouse(pos));
}
void ChatView::mouseMoveEvent(QMouseEvent *event)
{
QTextFragment frag = getFragmentUnderMouse(event->pos());
QString cardName = getCardNameUnderMouse(frag);
if (!cardName.isEmpty()) {
viewport()->setCursor(Qt::PointingHandCursor);
emit cardNameHovered(cardName);
} else if (frag.charFormat().isAnchor())
viewport()->setCursor(Qt::PointingHandCursor);
else
viewport()->setCursor(Qt::IBeamCursor);
QTextBrowser::mouseMoveEvent(event);
}
void ChatView::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::MidButton) {
QString cardName = getCardNameUnderMouse(event->pos());
if (!cardName.isEmpty())
emit showCardInfoPopup(event->globalPos(), cardName);
}
QTextBrowser::mousePressEvent(event);
}
void ChatView::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::MidButton)
emit deleteCardInfoPopup();
QTextBrowser::mouseReleaseEvent(event);
}
void ChatView::openLink(const QUrl &link)
{
QDesktopServices::openUrl(link);
}
#ifndef CHATVIEW_H
#define CHATVIEW_H
#include <QTextEdit>
#include <QTextBrowser>
#include <QTextFragment>
class QTextTable;
class QMouseEvent;
class ChatView : public QTextEdit {
class ChatView : public QTextBrowser {
Q_OBJECT;
private:
QTextTable *table;
QString ownName;
bool showTimestamps;
QTextFragment getFragmentUnderMouse(const QPoint &pos) const;
QString getCardNameUnderMouse(QTextFragment frag) const;
QString getCardNameUnderMouse(const QPoint &pos) const;
private slots:
void openLink(const QUrl &link);
public:
ChatView(const QString &_ownName, QWidget *parent = 0);
void appendMessage(QString sender, const QString &message);
ChatView(const QString &_ownName, bool _showTimestamps, QWidget *parent = 0);
void appendMessage(QString sender, QString message);
protected:
void enterEvent(QEvent *event);
void leaveEvent(QEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
signals:
void cardNameHovered(QString cardName);
void showCardInfoPopup(QPoint pos, QString cardName);
void deleteCardInfoPopup();
};
#endif
\ No newline at end of file
......@@ -11,7 +11,7 @@ class LocalServer : public Server
public:
LocalServer(QObject *parent = 0);
~LocalServer();
AuthenticationResult checkUserPassword(const QString & /*user*/, const QString & /*password*/) { return UnknownUser; }
AuthenticationResult checkUserPassword(Server_ProtocolHandler * /*handler*/, const QString & /*user*/, const QString & /*password*/) { return UnknownUser; }
QString getLoginMessage() const { return QString(); }
bool getGameShouldPing() const { return false; }
int getMaxGameInactivityTime() const { return 9999999; }
......
This diff is collapsed.
#ifndef MESSAGELOGWIDGET_H
#define MESSAGELOGWIDGET_H
#include <QTextEdit>
#include "chatview.h"
#include <QAbstractSocket>
#include "translation.h"
#include "protocol_datastructures.h"
class Player;
class CardZone;
class QMouseEvent;
class QEvent;
class CardInfoWidget;
class GameEventContext;
class CardItem;
......@@ -24,16 +22,15 @@ struct LogMoveCard {
int newX;
};
class MessageLogWidget : public QTextEdit {
class MessageLogWidget : public ChatView {
Q_OBJECT
private:
enum MessageContext { MessageContext_None, MessageContext_MoveCard, MessageContext_Mulligan };
CardInfoWidget *infoWidget;
QString sanitizeHtml(QString dirty) const;
void myAppend(const QString &message);
bool isFemale(Player *player) const;
QPair<QString, QString> getFromStr(CardZone *zone, QString cardName, int position) const;
QString getCardNameUnderMouse(const QPoint &pos) const;
MessageContext currentContext;
QList<LogMoveCard> moveCardQueue;
......@@ -42,10 +39,6 @@ private:
Player *mulliganPlayer;
int mulliganNumber;
signals:
void cardNameHovered(QString cardName);
void showCardInfoPopup(QPoint pos, QString cardName);
void deleteCardInfoPopup();
public slots:
void logConnecting(QString hostname);
void logConnected();
......@@ -96,13 +89,7 @@ public slots:
void containerProcessingDone();
public:
void connectToPlayer(Player *player);
MessageLogWidget(QWidget *parent = 0);
protected:
void enterEvent(QEvent *event);
void leaveEvent(QEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
MessageLogWidget(const QString &_ownName, QWidget *parent = 0);
};
#endif
#include "tab.h"
#include "cardinfowidget.h"
#include <QDesktopWidget>
#include <QApplication>
Tab::Tab(TabSupervisor *_tabSupervisor, QWidget *parent)
: QWidget(parent), tabMenu(0), tabSupervisor(_tabSupervisor), contentsChanged(false), infoPopup(0)
{
}
void Tab::showCardInfoPopup(const QPoint &pos, const QString &cardName)
{
infoPopup = new CardInfoWidget(CardInfoWidget::ModePopUp, 0, Qt::Widget | Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint | Qt::WindowStaysOnTopHint);
infoPopup->setAttribute(Qt::WA_TransparentForMouseEvents);
infoPopup->setCard(cardName);
QRect screenRect = qApp->desktop()->screenGeometry(this);
infoPopup->move(
qMax(screenRect.left(), qMin(pos.x() - infoPopup->width() / 2, screenRect.left() + screenRect.width() - infoPopup->width())),
qMax(screenRect.top(), qMin(pos.y() - infoPopup->height() / 2, screenRect.top() + screenRect.height() - infoPopup->height()))
);
infoPopup->show();
}
void Tab::deleteCardInfoPopup()
{
if (infoPopup) {
infoPopup->deleteLater();
infoPopup = 0;
}
}
......@@ -5,6 +5,7 @@
class QMenu;
class TabSupervisor;
class CardInfoWidget;
class Tab : public QWidget {
Q_OBJECT
......@@ -13,11 +14,14 @@ signals:
protected:
QMenu *tabMenu;
TabSupervisor *tabSupervisor;
protected slots:
void showCardInfoPopup(const QPoint &pos, const QString &cardName);
void deleteCardInfoPopup();
private:
bool contentsChanged;
CardInfoWidget *infoPopup;
public:
Tab(TabSupervisor *_tabSupervisor, QWidget *parent = 0)
: QWidget(parent), tabMenu(0), tabSupervisor(_tabSupervisor), contentsChanged(false) { }
Tab(TabSupervisor *_tabSupervisor, QWidget *parent = 0);
QMenu *getTabMenu() const { return tabMenu; }
bool getContentsChanged() const { return contentsChanged; }
void setContentsChanged(bool _contentsChanged) { contentsChanged = _contentsChanged; }
......
......@@ -5,8 +5,6 @@
#include <QAction>
#include <QMessageBox>
#include <QFileDialog>
#include <QApplication>
#include <QDesktopWidget>
#include "tab_game.h"
#include "cardinfowidget.h"
#include "playerlistwidget.h"
......@@ -160,8 +158,8 @@ void DeckViewContainer::setDeck(DeckList *deck)
readyStartButton->setEnabled(true);
}
TabGame::TabGame(TabSupervisor *_tabSupervisor, QList<AbstractClient *> &_clients, int _gameId, const QString &_gameDescription, int _localPlayerId, bool _spectator, bool _spectatorsCanTalk, bool _spectatorsSeeEverything, bool _resuming)
: Tab(_tabSupervisor), clients(_clients), gameId(_gameId), gameDescription(_gameDescription), localPlayerId(_localPlayerId), spectator(_spectator), spectatorsCanTalk(_spectatorsCanTalk), spectatorsSeeEverything(_spectatorsSeeEverything), started(false), resuming(_resuming), currentPhase(-1), infoPopup(0)
TabGame::TabGame(TabSupervisor *_tabSupervisor, QList<AbstractClient *> &_clients, int _gameId, const QString &_gameDescription, int _localPlayerId, const QString &_userName, bool _spectator, bool _spectatorsCanTalk, bool _spectatorsSeeEverything, bool _resuming)
: Tab(_tabSupervisor), clients(_clients), gameId(_gameId), gameDescription(_gameDescription), localPlayerId(_localPlayerId), spectator(_spectator), spectatorsCanTalk(_spectatorsCanTalk), spectatorsSeeEverything(_spectatorsSeeEverything), started(false), resuming(_resuming), currentPhase(-1)
{
phasesToolbar = new PhasesToolbar;
phasesToolbar->hide();
......@@ -178,7 +176,7 @@ TabGame::TabGame(TabSupervisor *_tabSupervisor, QList<AbstractClient *> &_client
timeElapsedLabel = new QLabel;
timeElapsedLabel->setAlignment(Qt::AlignCenter);
messageLog = new MessageLogWidget;
messageLog = new MessageLogWidget(_userName);
connect(messageLog, SIGNAL(cardNameHovered(QString)), cardInfo, SLOT(setCard(QString)));
connect(messageLog, SIGNAL(showCardInfoPopup(QPoint, QString)), this, SLOT(showCardInfoPopup(QPoint, QString)));
connect(messageLog, SIGNAL(deleteCardInfoPopup()), this, SLOT(deleteCardInfoPopup()));
......@@ -779,24 +777,3 @@ Player *TabGame::getActiveLocalPlayer() const
return 0;
}
void TabGame::showCardInfoPopup(const QPoint &pos, const QString &cardName)
{
infoPopup = new CardInfoWidget(CardInfoWidget::ModePopUp, 0, Qt::Widget | Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint | Qt::WindowStaysOnTopHint);
infoPopup->setAttribute(Qt::WA_TransparentForMouseEvents);
infoPopup->setCard(cardName);
QRect screenRect = qApp->desktop()->screenGeometry(this);
infoPopup->move(
qMax(screenRect.left(), qMin(pos.x() - infoPopup->width() / 2, screenRect.left() + screenRect.width() - infoPopup->width())),
qMax(screenRect.top(), qMin(pos.y() - infoPopup->height() / 2, screenRect.top() + screenRect.height() - infoPopup->height()))
);
infoPopup->show();
}
void TabGame::deleteCardInfoPopup()
{
if (infoPopup) {
infoPopup->deleteLater();
infoPopup = 0;
}
}
......@@ -100,7 +100,6 @@ private:
int activePlayer;
QSplitter *splitter;
CardInfoWidget *infoPopup;
CardInfoWidget *cardInfo;
PlayerListWidget *playerListWidget;
QLabel *timeElapsedLabel;
......@@ -147,8 +146,6 @@ signals:
void openMessageDialog(const QString &userName, bool focus);
private slots:
void newCardAdded(AbstractCardItem *card);
void showCardInfoPopup(const QPoint &pos, const QString &cardName);
void deleteCardInfoPopup();
void actConcede();
void actLeaveGame();
......@@ -158,7 +155,7 @@ private slots:
void actNextPhase();
void actNextTurn();
public:
TabGame(TabSupervisor *_tabSupervisor, QList<AbstractClient *> &_clients, int _gameId, const QString &_gameDescription, int _localPlayerId, bool _spectator, bool _spectatorsCanTalk, bool _spectatorsSeeEverything, bool _resuming);
TabGame(TabSupervisor *_tabSupervisor, QList<AbstractClient *> &_clients, int _gameId, const QString &_gameDescription, int _localPlayerId, const QString &_userName, bool _spectator, bool _spectatorsCanTalk, bool _spectatorsSeeEverything, bool _resuming);
~TabGame();
void retranslateUi();
void closeRequest();
......
......@@ -11,7 +11,9 @@
TabMessage::TabMessage(TabSupervisor *_tabSupervisor, AbstractClient *_client, const QString &_ownName, const QString &_userName)
: Tab(_tabSupervisor), client(_client), userName(_userName), userOnline(true)
{
chatView = new ChatView(_ownName);
chatView = new ChatView(_ownName, true);
connect(chatView, SIGNAL(showCardInfoPopup(QPoint, QString)), this, SLOT(showCardInfoPopup(QPoint, QString)));
connect(chatView, SIGNAL(deleteCardInfoPopup()), this, SLOT(deleteCardInfoPopup()));
sayEdit = new QLineEdit;
connect(sayEdit, SIGNAL(returnPressed()), this, SLOT(sendMessage()));
......
......@@ -137,7 +137,9 @@ TabRoom::TabRoom(TabSupervisor *_tabSupervisor, AbstractClient *_client, const Q
userList = new UserList(tabSupervisor, client, UserList::RoomList);
connect(userList, SIGNAL(openMessageDialog(const QString &, bool)), this, SIGNAL(openMessageDialog(const QString &, bool)));
chatView = new ChatView(ownName);
chatView = new ChatView(ownName, true);
connect(chatView, SIGNAL(showCardInfoPopup(QPoint, QString)), this, SLOT(showCardInfoPopup(QPoint, QString)));
connect(chatView, SIGNAL(deleteCardInfoPopup()), this, SLOT(deleteCardInfoPopup()));
sayLabel = new QLabel;
sayEdit = new QLineEdit;
sayLabel->setBuddy(sayEdit);
......
......@@ -221,7 +221,7 @@ void TabSupervisor::addCloseButtonToTab(Tab *tab, int tabIndex)
void TabSupervisor::gameJoined(Event_GameJoined *event)
{
TabGame *tab = new TabGame(this, QList<AbstractClient *>() << client, event->getGameId(), event->getGameDescription(), event->getPlayerId(), event->getSpectator(), event->getSpectatorsCanTalk(), event->getSpectatorsSeeEverything(), event->getResuming());
TabGame *tab = new TabGame(this, QList<AbstractClient *>() << client, event->getGameId(), event->getGameDescription(), event->getPlayerId(), userName, event->getSpectator(), event->getSpectatorsCanTalk(), event->getSpectatorsSeeEverything(), event->getResuming());
connect(tab, SIGNAL(gameClosing(TabGame *)), this, SLOT(gameLeft(TabGame *)));
connect(tab, SIGNAL(openMessageDialog(const QString &, bool)), this, SLOT(addMessageTab(const QString &, bool)));
int tabIndex = myAddTab(tab);
......@@ -232,7 +232,7 @@ void TabSupervisor::gameJoined(Event_GameJoined *event)
void TabSupervisor::localGameJoined(Event_GameJoined *event)
{
TabGame *tab = new TabGame(this, localClients, event->getGameId(), event->getGameDescription(), event->getPlayerId(), event->getSpectator(), event->getSpectatorsCanTalk(), event->getSpectatorsSeeEverything(), event->getResuming());
TabGame *tab = new TabGame(this, localClients, event->getGameId(), event->getGameDescription(), event->getPlayerId(), QString(), event->getSpectator(), event->getSpectatorsCanTalk(), event->getSpectatorsSeeEverything(), event->getResuming());
connect(tab, SIGNAL(gameClosing(TabGame *)), this, SLOT(gameLeft(TabGame *)));
int tabIndex = myAddTab(tab);
addCloseButtonToTab(tab, tabIndex);
......
......@@ -10,6 +10,53 @@
#include <QMouseEvent>
#include <QMenu>
#include <QInputDialog>
#include <QLabel>
#include <QSpinBox>
#include <QPlainTextEdit>
#include <QPushButton>
#include <QHBoxLayout>
BanDialog::BanDialog(QWidget *parent)
: QDialog(parent)
{
QLabel *durationLabel = new QLabel(tr("Please enter the duration of the ban (in minutes).\nEnter 0 for an indefinite ban."));
durationEdit = new QSpinBox;
durationEdit->setMinimum(0);
durationEdit->setValue(5);
QLabel *reasonLabel = new QLabel(tr("Please enter the reason for the ban.\nThis is only saved for moderators and cannot be seen by the banned person."));
reasonEdit = new QPlainTextEdit;
QPushButton *okButton = new QPushButton(tr("&OK"));
okButton->setAutoDefault(true);
connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));
QPushButton *cancelButton = new QPushButton(tr("&Cancel"));
connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
QHBoxLayout *buttonLayout = new QHBoxLayout;
buttonLayout->addStretch();
buttonLayout->addWidget(okButton);
buttonLayout->addWidget(cancelButton);
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addWidget(durationLabel);
vbox->addWidget(durationEdit);
vbox->addWidget(reasonLabel);
vbox->addWidget(reasonEdit);
vbox->addLayout(buttonLayout);
setLayout(vbox);
setWindowTitle(tr("Ban user from server"));
}
int BanDialog::getMinutes() const
{
return durationEdit->value();
}
QString BanDialog::getReason() const
{
return reasonEdit->toPlainText();
}
UserListItemDelegate::UserListItemDelegate(QObject *const parent)
: QStyledItemDelegate(parent)
......@@ -215,10 +262,9 @@ void UserList::showContextMenu(const QPoint &pos, const QModelIndex &index)
else if (actionClicked == aRemoveFromIgnoreList)
client->sendCommand(new Command_RemoveFromList("ignore", userName));
else if (actionClicked == aBan) {
bool ok;
int minutes = QInputDialog::getInt(this, tr("Duration"), tr("Please enter the duration of the ban (in minutes).\nEnter 0 for an indefinite ban."), 0, 0, 2147483647, 10, &ok);
if (ok)
client->sendCommand(new Command_BanFromServer(userName, minutes));
BanDialog dlg(this);
if (dlg.exec())
client->sendCommand(new Command_BanFromServer(userName, dlg.getMinutes(), dlg.getReason()));
}
delete menu;
......
#ifndef USERLIST_H
#define USERLIST_H
#include <QDialog>
#include <QGroupBox>
#include <QTreeWidgetItem>
#include <QStyledItemDelegate>
......@@ -9,6 +10,19 @@ class QTreeWidget;
class ServerInfo_User;
class AbstractClient;
class TabSupervisor;
class QSpinBox;
class QPlainTextEdit;
class BanDialog : public QDialog {
Q_OBJECT
private:
QSpinBox *durationEdit;
QPlainTextEdit *reasonEdit;
public:
BanDialog(QWidget *parent = 0);
int getMinutes() const;
QString getReason() const;
};
class UserListItemDelegate : public QStyledItemDelegate {
public:
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment