diff --git a/qt/new_game.cpp b/qt/new_game.cpp index cbe432a..96571c8 100644 --- a/qt/new_game.cpp +++ b/qt/new_game.cpp @@ -66,6 +66,7 @@ NewGame::NewGame(QWidget *iParent) QObject::connect(addToFavAction, SIGNAL(triggered()), this, SLOT(addSelectedToFav())); m_helper->addPopupAction(addToFavAction); + m_helper->setUpDown(buttonUp, buttonDown); // Initialize the model of the default players QList fav = PlayersTableHelper::getFavPlayers(); @@ -109,10 +110,6 @@ NewGame::NewGame(QWidget *iParent) QObject::connect(buttonAddFav, SIGNAL(clicked()), this, SLOT(addFavoritePlayers())); - QObject::connect(buttonUp, SIGNAL(clicked()), - m_helper, SLOT(moveSelectionUp())); - QObject::connect(buttonDown, SIGNAL(clicked()), - m_helper, SLOT(moveSelectionDown())); } diff --git a/qt/players_table_helper.cpp b/qt/players_table_helper.cpp index c722a27..7446611 100644 --- a/qt/players_table_helper.cpp +++ b/qt/players_table_helper.cpp @@ -71,7 +71,8 @@ PlayersTableHelper::PlayersTableHelper(QObject *parent, bool showDefaultColumn) : QObject(parent), m_tablePlayers(tablePlayers), m_buttonAdd(addButton), m_buttonRemove(removeButton), - m_showDefaultColumn(showDefaultColumn) + m_showDefaultColumn(showDefaultColumn), + m_buttonUp(0), m_buttonDown(0) { // Initialize the table headers tablePlayers->setColumnCount(m_showDefaultColumn ? 4 : 3); @@ -101,11 +102,11 @@ PlayersTableHelper::PlayersTableHelper(QObject *parent, } if (m_buttonRemove) { - QObject::connect(tablePlayers, SIGNAL(itemSelectionChanged()), - this, SLOT(enableRemoveButton())); QObject::connect(m_buttonRemove, SIGNAL(clicked()), this, SLOT(removeSelectedRows())); } + QObject::connect(tablePlayers, SIGNAL(itemSelectionChanged()), + this, SLOT(enableSelDepButtons())); PlayersTypeDelegate *typeDelegate = new PlayersTypeDelegate(this); m_tablePlayers->setItemDelegateForColumn(1, typeDelegate); @@ -119,6 +120,21 @@ PlayersTableHelper::PlayersTableHelper(QObject *parent, } +void PlayersTableHelper::setUpDown(QPushButton *iButtonUp, QPushButton *iButtonDown) +{ + ASSERT(m_buttonUp == 0 && m_buttonDown == 0, + "The up and down buttons can only be associated once"); + m_buttonUp = iButtonUp; + m_buttonDown = iButtonDown; + QObject::connect(m_buttonUp, SIGNAL(clicked()), + this, SLOT(moveSelectionUp())); + QObject::connect(m_buttonDown, SIGNAL(clicked()), + this, SLOT(moveSelectionDown())); + // Set a correct initial state + enableSelDepButtons(); +} + + void PlayersTableHelper::populateMenu(QMenu &iMenu, const QPoint &iPoint) { const QModelIndex &index = m_tablePlayers->indexAt(iPoint); @@ -156,9 +172,15 @@ void PlayersTableHelper::addPopupRemoveAction() } -void PlayersTableHelper::enableRemoveButton() +void PlayersTableHelper::enableSelDepButtons() { - m_buttonRemove->setEnabled(!m_tablePlayers->selectedItems().isEmpty()); + bool hasSelection = !m_tablePlayers->selectedItems().isEmpty(); + if (m_buttonRemove) + m_buttonRemove->setEnabled(hasSelection); + if (m_buttonUp) + m_buttonUp->setEnabled(hasSelection); + if (m_buttonDown) + m_buttonDown->setEnabled(hasSelection); } diff --git a/qt/players_table_helper.h b/qt/players_table_helper.h index 821c338..fe22c11 100644 --- a/qt/players_table_helper.h +++ b/qt/players_table_helper.h @@ -63,6 +63,9 @@ public: QPushButton *removeButton = 0, bool showDefaultColumn = false); + /// Associate up/down buttons + void setUpDown(QPushButton *iButtonUp, QPushButton *iButtonDown); + QList getPlayers(bool onlySelected) const; void addPlayers(const QList &iList); void addPlayer(const PlayerDef &iPlayer, @@ -77,19 +80,18 @@ public: void addPopupAction(QAction *iAction); void addPopupRemoveAction(); -public slots: - void moveSelectionUp(); - void moveSelectionDown(); - signals: void rowCountChanged(); private slots: void populateMenu(QMenu &, const QPoint &); - void enableRemoveButton(); + /// Enable selection-dependent buttons + void enableSelDepButtons(); void removeSelectedRows(); void addRow(); void addRow(const PlayerDef &iDef); + void moveSelectionUp(); + void moveSelectionDown(); private: QTableWidget *m_tablePlayers; @@ -97,6 +99,9 @@ private: QPushButton *m_buttonRemove; QList m_popupActions; bool m_showDefaultColumn; + // Up/down buttons (optional) + QPushButton *m_buttonUp; + QPushButton *m_buttonDown; /// Return a "normalized" player def, i.e. with correct values PlayerDef normalize(const PlayerDef &iDef) const;