close buttons for tabs

This commit is contained in:
Max-Wilhelm Bruker 2011-04-17 17:23:44 +02:00
parent 9933b219a9
commit 0ea8375a2f
9 changed files with 116 additions and 10 deletions

View file

@ -23,6 +23,7 @@ public:
void setContentsChanged(bool _contentsChanged) { contentsChanged = _contentsChanged; }
virtual QString getTabText() const = 0;
virtual void retranslateUi() = 0;
virtual void closeRequest() { }
};
#endif

View file

@ -310,6 +310,11 @@ void TabGame::retranslateUi()
scene->retranslateUi();
}
void TabGame::closeRequest()
{
actLeaveGame();
}
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)
@ -320,8 +325,9 @@ void TabGame::actConcede()
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)
return;
if (!spectator)
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);
deleteLater();

View file

@ -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();
void retranslateUi();
void closeRequest();
const QMap<int, Player *> &getPlayers() const { return players; }
CardItem *getCard(int playerId, const QString &zoneName, int cardId) const;
int getGameId() const { return gameId; }

View file

@ -40,6 +40,11 @@ void TabMessage::retranslateUi()
aLeave->setText(tr("&Leave"));
}
void TabMessage::closeRequest()
{
actLeave();
}
void TabMessage::sendMessage()
{
if (sayEdit->text().isEmpty() || !userOnline)

View file

@ -30,6 +30,7 @@ public:
TabMessage(TabSupervisor *_tabSupervisor, AbstractClient *_client, const QString &_ownName, const QString &_userName);
~TabMessage();
void retranslateUi();
void closeRequest();
QString getUserName() const { return userName; }
QString getTabText() const { return tr("Talking to %1").arg(userName); }

View file

@ -193,6 +193,11 @@ void TabRoom::retranslateUi()
aLeaveRoom->setText(tr("&Leave room"));
}
void TabRoom::closeRequest()
{
actLeaveRoom();
}
QString TabRoom::sanitizeHtml(QString dirty) const
{
return dirty

View file

@ -84,6 +84,7 @@ public:
TabRoom(TabSupervisor *_tabSupervisor, AbstractClient *_client, const QString &_ownName, ServerInfo_Room *info);
~TabRoom();
void retranslateUi();
void closeRequest();
void processRoomEvent(RoomEvent *event);
int getRoomId() const { return roomId; }
const QMap<int, QString> &getGameTypes() const { return gameTypes; }

View file

@ -11,8 +11,60 @@
#include "protocol_items.h"
#include "pixmapgenerator.h"
#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)
{
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()));
addTab(tab, tab->getTabText());
return addTab(tab, tab->getTabText());
}
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)));
}
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)
{
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(openMessageDialog(const QString &, bool)), this, SLOT(addMessageTab(const QString &, bool)));
myAddTab(tab);
int tabIndex = myAddTab(tab);
addCloseButtonToTab(tab, tabIndex);
gameTabs.insert(event->getGameId(), 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());
connect(tab, SIGNAL(gameClosing(TabGame *)), this, SLOT(gameLeft(TabGame *)));
myAddTab(tab);
int tabIndex = myAddTab(tab);
addCloseButtonToTab(tab, tabIndex);
gameTabs.insert(event->getGameId(), tab);
setCurrentWidget(tab);
@ -192,7 +261,8 @@ void TabSupervisor::addRoomTab(ServerInfo_Room *info, bool setCurrent)
TabRoom *tab = new TabRoom(this, client, userName, info);
connect(tab, SIGNAL(roomClosing(TabRoom *)), this, SLOT(roomLeft(TabRoom *)));
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);
if (setCurrent)
setCurrentWidget(tab);
@ -213,7 +283,8 @@ TabMessage *TabSupervisor::addMessageTab(const QString &receiverName, bool focus
TabMessage *tab = new TabMessage(this, client, userName, receiverName);
connect(tab, SIGNAL(talkClosing(TabMessage *)), this, SLOT(talkLeft(TabMessage *)));
myAddTab(tab);
int tabIndex = myAddTab(tab);
addCloseButtonToTab(tab, tabIndex);
messageTabs.insert(receiverName, tab);
if (focus)
setCurrentWidget(tab);

View file

@ -3,6 +3,7 @@
#include <QTabWidget>
#include <QMap>
#include <QAbstractButton>
class QMenu;
class AbstractClient;
@ -21,6 +22,18 @@ class Event_Message;
class ServerInfo_Room;
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 {
Q_OBJECT
private:
@ -36,7 +49,8 @@ private:
QMap<int, TabRoom *> roomTabs;
QMap<int, TabGame *> gameTabs;
QMap<QString, TabMessage *> messageTabs;
void myAddTab(Tab *tab);
int myAddTab(Tab *tab);
void addCloseButtonToTab(Tab *tab, int tabIndex);
public:
TabSupervisor(QWidget *parent = 0);
~TabSupervisor();
@ -52,6 +66,7 @@ signals:
void setMenu(QMenu *menu);
void localGameEnded();
private slots:
void closeButtonPressed();
void updateCurrent(int index);
void updatePingTime(int value, int max);
void gameJoined(Event_GameJoined *event);