close buttons for tabs
This commit is contained in:
parent
9933b219a9
commit
0ea8375a2f
9 changed files with 116 additions and 10 deletions
|
@ -23,6 +23,7 @@ public:
|
||||||
void setContentsChanged(bool _contentsChanged) { contentsChanged = _contentsChanged; }
|
void setContentsChanged(bool _contentsChanged) { contentsChanged = _contentsChanged; }
|
||||||
virtual QString getTabText() const = 0;
|
virtual QString getTabText() const = 0;
|
||||||
virtual void retranslateUi() = 0;
|
virtual void retranslateUi() = 0;
|
||||||
|
virtual void closeRequest() { }
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -310,6 +310,11 @@ void TabGame::retranslateUi()
|
||||||
scene->retranslateUi();
|
scene->retranslateUi();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TabGame::closeRequest()
|
||||||
|
{
|
||||||
|
actLeaveGame();
|
||||||
|
}
|
||||||
|
|
||||||
void TabGame::actConcede()
|
void TabGame::actConcede()
|
||||||
{
|
{
|
||||||
if (QMessageBox::question(this, tr("Concede"), tr("Are you sure you want to concede this game?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No) != QMessageBox::Yes)
|
if (QMessageBox::question(this, tr("Concede"), tr("Are you sure you want to concede this game?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No) != QMessageBox::Yes)
|
||||||
|
@ -320,8 +325,9 @@ void TabGame::actConcede()
|
||||||
|
|
||||||
void TabGame::actLeaveGame()
|
void TabGame::actLeaveGame()
|
||||||
{
|
{
|
||||||
if (QMessageBox::question(this, tr("Leave game"), tr("Are you sure you want to leave this game?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No) != QMessageBox::Yes)
|
if (!spectator)
|
||||||
return;
|
if (QMessageBox::question(this, tr("Leave game"), tr("Are you sure you want to leave this game?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No) != QMessageBox::Yes)
|
||||||
|
return;
|
||||||
|
|
||||||
sendGameCommand(new Command_LeaveGame);
|
sendGameCommand(new Command_LeaveGame);
|
||||||
deleteLater();
|
deleteLater();
|
||||||
|
|
|
@ -161,6 +161,7 @@ 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, bool _spectator, bool _spectatorsCanTalk, bool _spectatorsSeeEverything, bool _resuming);
|
||||||
~TabGame();
|
~TabGame();
|
||||||
void retranslateUi();
|
void retranslateUi();
|
||||||
|
void closeRequest();
|
||||||
const QMap<int, Player *> &getPlayers() const { return players; }
|
const QMap<int, Player *> &getPlayers() const { return players; }
|
||||||
CardItem *getCard(int playerId, const QString &zoneName, int cardId) const;
|
CardItem *getCard(int playerId, const QString &zoneName, int cardId) const;
|
||||||
int getGameId() const { return gameId; }
|
int getGameId() const { return gameId; }
|
||||||
|
|
|
@ -40,6 +40,11 @@ void TabMessage::retranslateUi()
|
||||||
aLeave->setText(tr("&Leave"));
|
aLeave->setText(tr("&Leave"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TabMessage::closeRequest()
|
||||||
|
{
|
||||||
|
actLeave();
|
||||||
|
}
|
||||||
|
|
||||||
void TabMessage::sendMessage()
|
void TabMessage::sendMessage()
|
||||||
{
|
{
|
||||||
if (sayEdit->text().isEmpty() || !userOnline)
|
if (sayEdit->text().isEmpty() || !userOnline)
|
||||||
|
|
|
@ -30,6 +30,7 @@ public:
|
||||||
TabMessage(TabSupervisor *_tabSupervisor, AbstractClient *_client, const QString &_ownName, const QString &_userName);
|
TabMessage(TabSupervisor *_tabSupervisor, AbstractClient *_client, const QString &_ownName, const QString &_userName);
|
||||||
~TabMessage();
|
~TabMessage();
|
||||||
void retranslateUi();
|
void retranslateUi();
|
||||||
|
void closeRequest();
|
||||||
QString getUserName() const { return userName; }
|
QString getUserName() const { return userName; }
|
||||||
QString getTabText() const { return tr("Talking to %1").arg(userName); }
|
QString getTabText() const { return tr("Talking to %1").arg(userName); }
|
||||||
|
|
||||||
|
|
|
@ -193,6 +193,11 @@ void TabRoom::retranslateUi()
|
||||||
aLeaveRoom->setText(tr("&Leave room"));
|
aLeaveRoom->setText(tr("&Leave room"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TabRoom::closeRequest()
|
||||||
|
{
|
||||||
|
actLeaveRoom();
|
||||||
|
}
|
||||||
|
|
||||||
QString TabRoom::sanitizeHtml(QString dirty) const
|
QString TabRoom::sanitizeHtml(QString dirty) const
|
||||||
{
|
{
|
||||||
return dirty
|
return dirty
|
||||||
|
|
|
@ -84,6 +84,7 @@ public:
|
||||||
TabRoom(TabSupervisor *_tabSupervisor, AbstractClient *_client, const QString &_ownName, ServerInfo_Room *info);
|
TabRoom(TabSupervisor *_tabSupervisor, AbstractClient *_client, const QString &_ownName, ServerInfo_Room *info);
|
||||||
~TabRoom();
|
~TabRoom();
|
||||||
void retranslateUi();
|
void retranslateUi();
|
||||||
|
void closeRequest();
|
||||||
void processRoomEvent(RoomEvent *event);
|
void processRoomEvent(RoomEvent *event);
|
||||||
int getRoomId() const { return roomId; }
|
int getRoomId() const { return roomId; }
|
||||||
const QMap<int, QString> &getGameTypes() const { return gameTypes; }
|
const QMap<int, QString> &getGameTypes() const { return gameTypes; }
|
||||||
|
|
|
@ -11,8 +11,60 @@
|
||||||
#include "protocol_items.h"
|
#include "protocol_items.h"
|
||||||
#include "pixmapgenerator.h"
|
#include "pixmapgenerator.h"
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
#include <QPainter>
|
||||||
|
|
||||||
TabSupervisor:: TabSupervisor(QWidget *parent)
|
CloseButton::CloseButton(QWidget *parent)
|
||||||
|
: QAbstractButton(parent)
|
||||||
|
{
|
||||||
|
setFocusPolicy(Qt::NoFocus);
|
||||||
|
setCursor(Qt::ArrowCursor);
|
||||||
|
resize(sizeHint());
|
||||||
|
}
|
||||||
|
|
||||||
|
QSize CloseButton::sizeHint() const
|
||||||
|
{
|
||||||
|
ensurePolished();
|
||||||
|
int width = style()->pixelMetric(QStyle::PM_TabCloseIndicatorWidth, 0, this);
|
||||||
|
int height = style()->pixelMetric(QStyle::PM_TabCloseIndicatorHeight, 0, this);
|
||||||
|
return QSize(width, height);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CloseButton::enterEvent(QEvent *event)
|
||||||
|
{
|
||||||
|
update();
|
||||||
|
QAbstractButton::enterEvent(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CloseButton::leaveEvent(QEvent *event)
|
||||||
|
{
|
||||||
|
update();
|
||||||
|
QAbstractButton::leaveEvent(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CloseButton::paintEvent(QPaintEvent * /*event*/)
|
||||||
|
{
|
||||||
|
QPainter p(this);
|
||||||
|
QStyleOption opt;
|
||||||
|
opt.init(this);
|
||||||
|
opt.state |= QStyle::State_AutoRaise;
|
||||||
|
if (isEnabled() && underMouse() && !isChecked() && !isDown())
|
||||||
|
opt.state |= QStyle::State_Raised;
|
||||||
|
if (isChecked())
|
||||||
|
opt.state |= QStyle::State_On;
|
||||||
|
if (isDown())
|
||||||
|
opt.state |= QStyle::State_Sunken;
|
||||||
|
|
||||||
|
if (const QTabBar *tb = qobject_cast<const QTabBar *>(parent())) {
|
||||||
|
int index = tb->currentIndex();
|
||||||
|
QTabBar::ButtonPosition position = (QTabBar::ButtonPosition) style()->styleHint(QStyle::SH_TabBar_CloseButtonPosition, 0, tb);
|
||||||
|
if (tb->tabButton(index, position) == this)
|
||||||
|
opt.state |= QStyle::State_Selected;
|
||||||
|
}
|
||||||
|
|
||||||
|
style()->drawPrimitive(QStyle::PE_IndicatorTabClose, &opt, &p, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
TabSupervisor::TabSupervisor(QWidget *parent)
|
||||||
: QTabWidget(parent), client(0), tabServer(0), tabDeckStorage(0), tabAdmin(0)
|
: QTabWidget(parent), client(0), tabServer(0), tabDeckStorage(0), tabAdmin(0)
|
||||||
{
|
{
|
||||||
tabChangedIcon = new QIcon(":/resources/icon_tab_changed.svg");
|
tabChangedIcon = new QIcon(":/resources/icon_tab_changed.svg");
|
||||||
|
@ -47,10 +99,10 @@ void TabSupervisor::retranslateUi()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TabSupervisor::myAddTab(Tab *tab)
|
int TabSupervisor::myAddTab(Tab *tab)
|
||||||
{
|
{
|
||||||
connect(tab, SIGNAL(userEvent()), this, SLOT(tabUserEvent()));
|
connect(tab, SIGNAL(userEvent()), this, SLOT(tabUserEvent()));
|
||||||
addTab(tab, tab->getTabText());
|
return addTab(tab, tab->getTabText());
|
||||||
}
|
}
|
||||||
|
|
||||||
void TabSupervisor::start(AbstractClient *_client, ServerInfo_User *userInfo)
|
void TabSupervisor::start(AbstractClient *_client, ServerInfo_User *userInfo)
|
||||||
|
@ -152,12 +204,28 @@ void TabSupervisor::updatePingTime(int value, int max)
|
||||||
setTabIcon(0, QIcon(PingPixmapGenerator::generatePixmap(15, value, max)));
|
setTabIcon(0, QIcon(PingPixmapGenerator::generatePixmap(15, value, max)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TabSupervisor::closeButtonPressed()
|
||||||
|
{
|
||||||
|
Tab *tab = static_cast<Tab *>(static_cast<CloseButton *>(sender())->property("tab").value<QObject *>());
|
||||||
|
tab->closeRequest();
|
||||||
|
}
|
||||||
|
|
||||||
|
void TabSupervisor::addCloseButtonToTab(Tab *tab, int tabIndex)
|
||||||
|
{
|
||||||
|
QTabBar::ButtonPosition closeSide = (QTabBar::ButtonPosition) tabBar()->style()->styleHint(QStyle::SH_TabBar_CloseButtonPosition, 0, tabBar());
|
||||||
|
CloseButton *closeButton = new CloseButton;
|
||||||
|
connect(closeButton, SIGNAL(clicked()), this, SLOT(closeButtonPressed()));
|
||||||
|
closeButton->setProperty("tab", qVariantFromValue((QObject *) tab));
|
||||||
|
tabBar()->setTabButton(tabIndex, closeSide, closeButton);
|
||||||
|
}
|
||||||
|
|
||||||
void TabSupervisor::gameJoined(Event_GameJoined *event)
|
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(), event->getSpectator(), event->getSpectatorsCanTalk(), event->getSpectatorsSeeEverything(), event->getResuming());
|
||||||
connect(tab, SIGNAL(gameClosing(TabGame *)), this, SLOT(gameLeft(TabGame *)));
|
connect(tab, SIGNAL(gameClosing(TabGame *)), this, SLOT(gameLeft(TabGame *)));
|
||||||
connect(tab, SIGNAL(openMessageDialog(const QString &, bool)), this, SLOT(addMessageTab(const QString &, bool)));
|
connect(tab, SIGNAL(openMessageDialog(const QString &, bool)), this, SLOT(addMessageTab(const QString &, bool)));
|
||||||
myAddTab(tab);
|
int tabIndex = myAddTab(tab);
|
||||||
|
addCloseButtonToTab(tab, tabIndex);
|
||||||
gameTabs.insert(event->getGameId(), tab);
|
gameTabs.insert(event->getGameId(), tab);
|
||||||
setCurrentWidget(tab);
|
setCurrentWidget(tab);
|
||||||
}
|
}
|
||||||
|
@ -166,7 +234,8 @@ 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(), event->getSpectator(), event->getSpectatorsCanTalk(), event->getSpectatorsSeeEverything(), event->getResuming());
|
||||||
connect(tab, SIGNAL(gameClosing(TabGame *)), this, SLOT(gameLeft(TabGame *)));
|
connect(tab, SIGNAL(gameClosing(TabGame *)), this, SLOT(gameLeft(TabGame *)));
|
||||||
myAddTab(tab);
|
int tabIndex = myAddTab(tab);
|
||||||
|
addCloseButtonToTab(tab, tabIndex);
|
||||||
gameTabs.insert(event->getGameId(), tab);
|
gameTabs.insert(event->getGameId(), tab);
|
||||||
setCurrentWidget(tab);
|
setCurrentWidget(tab);
|
||||||
|
|
||||||
|
@ -192,7 +261,8 @@ void TabSupervisor::addRoomTab(ServerInfo_Room *info, bool setCurrent)
|
||||||
TabRoom *tab = new TabRoom(this, client, userName, info);
|
TabRoom *tab = new TabRoom(this, client, userName, info);
|
||||||
connect(tab, SIGNAL(roomClosing(TabRoom *)), this, SLOT(roomLeft(TabRoom *)));
|
connect(tab, SIGNAL(roomClosing(TabRoom *)), this, SLOT(roomLeft(TabRoom *)));
|
||||||
connect(tab, SIGNAL(openMessageDialog(const QString &, bool)), this, SLOT(addMessageTab(const QString &, bool)));
|
connect(tab, SIGNAL(openMessageDialog(const QString &, bool)), this, SLOT(addMessageTab(const QString &, bool)));
|
||||||
myAddTab(tab);
|
int tabIndex = myAddTab(tab);
|
||||||
|
addCloseButtonToTab(tab, tabIndex);
|
||||||
roomTabs.insert(info->getRoomId(), tab);
|
roomTabs.insert(info->getRoomId(), tab);
|
||||||
if (setCurrent)
|
if (setCurrent)
|
||||||
setCurrentWidget(tab);
|
setCurrentWidget(tab);
|
||||||
|
@ -213,7 +283,8 @@ TabMessage *TabSupervisor::addMessageTab(const QString &receiverName, bool focus
|
||||||
|
|
||||||
TabMessage *tab = new TabMessage(this, client, userName, receiverName);
|
TabMessage *tab = new TabMessage(this, client, userName, receiverName);
|
||||||
connect(tab, SIGNAL(talkClosing(TabMessage *)), this, SLOT(talkLeft(TabMessage *)));
|
connect(tab, SIGNAL(talkClosing(TabMessage *)), this, SLOT(talkLeft(TabMessage *)));
|
||||||
myAddTab(tab);
|
int tabIndex = myAddTab(tab);
|
||||||
|
addCloseButtonToTab(tab, tabIndex);
|
||||||
messageTabs.insert(receiverName, tab);
|
messageTabs.insert(receiverName, tab);
|
||||||
if (focus)
|
if (focus)
|
||||||
setCurrentWidget(tab);
|
setCurrentWidget(tab);
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
#include <QTabWidget>
|
#include <QTabWidget>
|
||||||
#include <QMap>
|
#include <QMap>
|
||||||
|
#include <QAbstractButton>
|
||||||
|
|
||||||
class QMenu;
|
class QMenu;
|
||||||
class AbstractClient;
|
class AbstractClient;
|
||||||
|
@ -21,6 +22,18 @@ class Event_Message;
|
||||||
class ServerInfo_Room;
|
class ServerInfo_Room;
|
||||||
class ServerInfo_User;
|
class ServerInfo_User;
|
||||||
|
|
||||||
|
class CloseButton : public QAbstractButton {
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
CloseButton(QWidget *parent = 0);
|
||||||
|
QSize sizeHint() const;
|
||||||
|
inline QSize minimumSizeHint() const { return sizeHint(); }
|
||||||
|
protected:
|
||||||
|
void enterEvent(QEvent *event);
|
||||||
|
void leaveEvent(QEvent *event);
|
||||||
|
void paintEvent(QPaintEvent *event);
|
||||||
|
};
|
||||||
|
|
||||||
class TabSupervisor : public QTabWidget {
|
class TabSupervisor : public QTabWidget {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
private:
|
private:
|
||||||
|
@ -36,7 +49,8 @@ private:
|
||||||
QMap<int, TabRoom *> roomTabs;
|
QMap<int, TabRoom *> roomTabs;
|
||||||
QMap<int, TabGame *> gameTabs;
|
QMap<int, TabGame *> gameTabs;
|
||||||
QMap<QString, TabMessage *> messageTabs;
|
QMap<QString, TabMessage *> messageTabs;
|
||||||
void myAddTab(Tab *tab);
|
int myAddTab(Tab *tab);
|
||||||
|
void addCloseButtonToTab(Tab *tab, int tabIndex);
|
||||||
public:
|
public:
|
||||||
TabSupervisor(QWidget *parent = 0);
|
TabSupervisor(QWidget *parent = 0);
|
||||||
~TabSupervisor();
|
~TabSupervisor();
|
||||||
|
@ -52,6 +66,7 @@ signals:
|
||||||
void setMenu(QMenu *menu);
|
void setMenu(QMenu *menu);
|
||||||
void localGameEnded();
|
void localGameEnded();
|
||||||
private slots:
|
private slots:
|
||||||
|
void closeButtonPressed();
|
||||||
void updateCurrent(int index);
|
void updateCurrent(int index);
|
||||||
void updatePingTime(int value, int max);
|
void updatePingTime(int value, int max);
|
||||||
void gameJoined(Event_GameJoined *event);
|
void gameJoined(Event_GameJoined *event);
|
||||||
|
|
Loading…
Reference in a new issue