Commit 2b7ea0c9 authored by sylvanbasilisk's avatar sylvanbasilisk
Browse files

refactored deck editor UI key shortcuts

The global shortcuts were removed in favor of key events
which are only triggered when a widget has focus. This is
necessary to allow different widgets to react differently
to the 'enter' key. The current key configuration is:
search edit focused:
  * left/right: decrease/increase card count for selected database card
  * enter: add selected card to deck
  * ctrl-enter: add selected card to deck sideboard
  * ctrl-left/right: decrease/increase card count in sideboard for selected card

database focused: the same as with search edit.

deckview focused:
  * left/right: decrease/increase card count for selected deckview card
  * enter: increase count for selected deckview card
  * delete/backspace: delete selected card from deck
parent f6e9676c
...@@ -26,6 +26,7 @@ SET(cockatrice_SOURCES ...@@ -26,6 +26,7 @@ SET(cockatrice_SOURCES
src/handzone.cpp src/handzone.cpp
src/handcounter.cpp src/handcounter.cpp
src/carddatabase.cpp src/carddatabase.cpp
src/keysignals.cpp
src/gameview.cpp src/gameview.cpp
src/gameselector.cpp src/gameselector.cpp
src/decklistmodel.cpp src/decklistmodel.cpp
...@@ -110,6 +111,7 @@ SET(cockatrice_HEADERS ...@@ -110,6 +111,7 @@ SET(cockatrice_HEADERS
src/handzone.h src/handzone.h
src/handcounter.h src/handcounter.h
src/carddatabase.h src/carddatabase.h
src/keysignals.h
src/gameview.h src/gameview.h
src/gameselector.h src/gameselector.h
src/decklistmodel.h src/decklistmodel.h
......
...@@ -169,8 +169,7 @@ Qt::ItemFlags DeckListModel::flags(const QModelIndex &index) const ...@@ -169,8 +169,7 @@ Qt::ItemFlags DeckListModel::flags(const QModelIndex &index) const
return 0; return 0;
Qt::ItemFlags result = Qt::ItemIsEnabled; Qt::ItemFlags result = Qt::ItemIsEnabled;
if (getNode<DecklistModelCardNode *>(index)) result |= Qt::ItemIsSelectable;
result |= Qt::ItemIsSelectable;
return result; return result;
} }
...@@ -236,6 +235,38 @@ InnerDecklistNode *DeckListModel::createNodeIfNeeded(const QString &name, InnerD ...@@ -236,6 +235,38 @@ InnerDecklistNode *DeckListModel::createNodeIfNeeded(const QString &name, InnerD
return newNode; return newNode;
} }
DecklistModelCardNode *DeckListModel::findCardNode(const QString &cardName, const QString &zoneName) const
{
InnerDecklistNode *zoneNode, *typeNode;
CardInfo *info;
QString cardType;
zoneNode = dynamic_cast<InnerDecklistNode *>(root->findChild(zoneName));
if(!zoneNode)
return NULL;
info = db->getCard(cardName);
if(!info)
return NULL;
cardType = info->getMainCardType();
typeNode = dynamic_cast<InnerDecklistNode *>(zoneNode->findChild(cardType));
if(!typeNode)
return NULL;
return dynamic_cast<DecklistModelCardNode *>(typeNode->findChild(cardName));
}
QModelIndex DeckListModel::findCard(const QString &cardName, const QString &zoneName) const
{
DecklistModelCardNode *cardNode;
cardNode = findCardNode(cardName, zoneName);
if(!cardNode)
return QModelIndex();
return nodeToIndex(cardNode);
}
QModelIndex DeckListModel::addCard(const QString &cardName, const QString &zoneName) QModelIndex DeckListModel::addCard(const QString &cardName, const QString &zoneName)
{ {
InnerDecklistNode *zoneNode = createNodeIfNeeded(zoneName, root); InnerDecklistNode *zoneNode = createNodeIfNeeded(zoneName, root);
......
...@@ -45,6 +45,8 @@ public: ...@@ -45,6 +45,8 @@ public:
Qt::ItemFlags flags(const QModelIndex &index) const; Qt::ItemFlags flags(const QModelIndex &index) const;
bool setData(const QModelIndex &index, const QVariant &value, int role); bool setData(const QModelIndex &index, const QVariant &value, int role);
bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()); bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
QModelIndex findCard(const QString &cardName, const QString &zoneName) const;
QModelIndex addCard(const QString &cardName, const QString &zoneName); QModelIndex addCard(const QString &cardName, const QString &zoneName);
void sort(int column, Qt::SortOrder order = Qt::AscendingOrder); void sort(int column, Qt::SortOrder order = Qt::AscendingOrder);
void cleanList(); void cleanList();
...@@ -56,6 +58,7 @@ private: ...@@ -56,6 +58,7 @@ private:
InnerDecklistNode *root; InnerDecklistNode *root;
InnerDecklistNode *createNodeIfNeeded(const QString &name, InnerDecklistNode *parent); InnerDecklistNode *createNodeIfNeeded(const QString &name, InnerDecklistNode *parent);
QModelIndex nodeToIndex(AbstractDecklistNode *node) const; QModelIndex nodeToIndex(AbstractDecklistNode *node) const;
DecklistModelCardNode *findCardNode(const QString &cardName, const QString &zoneName) const;
void emitRecursiveUpdates(const QModelIndex &index); void emitRecursiveUpdates(const QModelIndex &index);
void sortHelper(InnerDecklistNode *node, Qt::SortOrder order); void sortHelper(InnerDecklistNode *node, Qt::SortOrder order);
......
...@@ -41,7 +41,8 @@ FilterBuilder::FilterBuilder(QWidget *parent) ...@@ -41,7 +41,8 @@ FilterBuilder::FilterBuilder(QWidget *parent)
layout->setAlignment(Qt::AlignTop); layout->setAlignment(Qt::AlignTop);
setLayout(layout); setLayout(layout);
connect(ok, SIGNAL(released()), this, SLOT(add_released())); connect(edit, SIGNAL(returnPressed()), this, SLOT(emit_add()));
connect(ok, SIGNAL(released()), this, SLOT(emit_add()));
connect(filterCombo, SIGNAL(currentIndexChanged(int)), edit, SLOT(clear())); connect(filterCombo, SIGNAL(currentIndexChanged(int)), edit, SLOT(clear()));
fltr = NULL; fltr = NULL;
} }
...@@ -62,7 +63,7 @@ static int comboCurrentIntData(const QComboBox *combo) ...@@ -62,7 +63,7 @@ static int comboCurrentIntData(const QComboBox *combo)
return combo->itemData(combo->currentIndex()).toInt(); return combo->itemData(combo->currentIndex()).toInt();
} }
void FilterBuilder::add_released() void FilterBuilder::emit_add()
{ {
QString txt; QString txt;
......
...@@ -28,7 +28,7 @@ signals: ...@@ -28,7 +28,7 @@ signals:
public slots: public slots:
private slots: private slots:
void add_released(); void emit_add();
protected: protected:
}; };
......
#include "keysignals.h"
#include <QKeyEvent>
bool KeySignals::eventFilter(QObject * /*object*/, QEvent *event) {
QKeyEvent *kevent;
if(event->type() != QEvent::KeyPress)
return false;
kevent = static_cast<QKeyEvent *>(event);
switch(kevent->key()) {
case Qt::Key_Return:
case Qt::Key_Enter:
if(kevent->modifiers() & Qt::ControlModifier)
emit onCtrlEnter();
else
emit onEnter();
break;
case Qt::Key_Right:
if(kevent->modifiers() & Qt::ControlModifier)
emit onCtrlRight();
else
emit onRight();
if(!filterLROn)
return false;
break;
case Qt::Key_Left:
if(kevent->modifiers() & Qt::ControlModifier)
emit onCtrlLeft();
else
emit onLeft();
if(!filterLROn)
return false;
break;
case Qt::Key_Delete:
case Qt::Key_Backspace:
emit onDelete();
if(!filterDeleteOn)
return false;
break;
default:
return false;
}
return true;
}
#include <QObject>
#include <QEvent>
class KeySignals : public QObject {
Q_OBJECT
private:
bool filterDeleteOn;
bool filterLROn;
signals:
void onEnter();
void onCtrlEnter();
void onLeft();
void onCtrlLeft();
void onRight();
void onCtrlRight();
void onDelete();
protected:
virtual bool eventFilter(QObject *, QEvent *event);
public:
KeySignals()
: filterDeleteOn(true)
, filterLROn(true)
{}
void filterDelete(bool on) { filterDeleteOn = on; }
void filterLeftRight(bool on) { filterLROn = on; }
};
...@@ -54,8 +54,15 @@ TabDeckEditor::TabDeckEditor(TabSupervisor *_tabSupervisor, QWidget *parent) ...@@ -54,8 +54,15 @@ TabDeckEditor::TabDeckEditor(TabSupervisor *_tabSupervisor, QWidget *parent)
searchLabel = new QLabel(); searchLabel = new QLabel();
searchEdit = new SearchLineEdit; searchEdit = new SearchLineEdit;
searchLabel->setBuddy(searchEdit); searchLabel->setBuddy(searchEdit);
searchKeySignals.filterDelete(false);
searchEdit->installEventFilter(&searchKeySignals);
connect(searchEdit, SIGNAL(textChanged(const QString &)), this, SLOT(updateSearch(const QString &))); connect(searchEdit, SIGNAL(textChanged(const QString &)), this, SLOT(updateSearch(const QString &)));
connect(searchEdit, SIGNAL(returnPressed()), this, SLOT(actAddCard())); connect(&searchKeySignals, SIGNAL(onEnter()), this, SLOT(actAddCard()));
connect(&searchKeySignals, SIGNAL(onRight()), this, SLOT(actAddCard()));
connect(&searchKeySignals, SIGNAL(onCtrlRight()), this, SLOT(actAddCardToSideboard()));
connect(&searchKeySignals, SIGNAL(onLeft()), this, SLOT(actDecrementCard()));
connect(&searchKeySignals, SIGNAL(onCtrlLeft()), this, SLOT(actDecrementCardFromSideboard()));
connect(&searchKeySignals, SIGNAL(onCtrlEnter()), this, SLOT(actAddCardToSideboard()));
QToolBar *deckEditToolBar = new QToolBar; QToolBar *deckEditToolBar = new QToolBar;
deckEditToolBar->setOrientation(Qt::Horizontal); deckEditToolBar->setOrientation(Qt::Horizontal);
...@@ -71,6 +78,7 @@ TabDeckEditor::TabDeckEditor(TabSupervisor *_tabSupervisor, QWidget *parent) ...@@ -71,6 +78,7 @@ TabDeckEditor::TabDeckEditor(TabSupervisor *_tabSupervisor, QWidget *parent)
databaseDisplayModel->setSourceModel(databaseModel); databaseDisplayModel->setSourceModel(databaseModel);
databaseDisplayModel->setFilterKeyColumn(0); databaseDisplayModel->setFilterKeyColumn(0);
databaseDisplayModel->sort(0, Qt::AscendingOrder); databaseDisplayModel->sort(0, Qt::AscendingOrder);
databaseView = new QTreeView(); databaseView = new QTreeView();
databaseView->setModel(databaseDisplayModel); databaseView->setModel(databaseDisplayModel);
databaseView->setUniformRowHeights(true); databaseView->setUniformRowHeights(true);
...@@ -81,6 +89,14 @@ TabDeckEditor::TabDeckEditor(TabSupervisor *_tabSupervisor, QWidget *parent) ...@@ -81,6 +89,14 @@ TabDeckEditor::TabDeckEditor(TabSupervisor *_tabSupervisor, QWidget *parent)
databaseView->resizeColumnToContents(0); databaseView->resizeColumnToContents(0);
connect(databaseView->selectionModel(), SIGNAL(currentRowChanged(const QModelIndex &, const QModelIndex &)), this, SLOT(updateCardInfoLeft(const QModelIndex &, const QModelIndex &))); connect(databaseView->selectionModel(), SIGNAL(currentRowChanged(const QModelIndex &, const QModelIndex &)), this, SLOT(updateCardInfoLeft(const QModelIndex &, const QModelIndex &)));
connect(databaseView, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(actAddCard())); connect(databaseView, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(actAddCard()));
databaseView->installEventFilter(&dbViewKeySignals);
connect(&dbViewKeySignals, SIGNAL(onEnter()), this, SLOT(actAddCard()));
connect(&dbViewKeySignals, SIGNAL(onRight()), this, SLOT(actAddCard()));
connect(&dbViewKeySignals, SIGNAL(onCtrlRight()), this, SLOT(actAddCardToSideboard()));
connect(&dbViewKeySignals, SIGNAL(onLeft()), this, SLOT(actDecrementCard()));
connect(&dbViewKeySignals, SIGNAL(onCtrlLeft()), this, SLOT(actDecrementCardFromSideboard()));
connect(&dbViewKeySignals, SIGNAL(onCtrlEnter()), this, SLOT(actAddCardToSideboard()));
searchEdit->setTreeView(databaseView); searchEdit->setTreeView(databaseView);
QVBoxLayout *leftFrame = new QVBoxLayout; QVBoxLayout *leftFrame = new QVBoxLayout;
...@@ -121,7 +137,13 @@ TabDeckEditor::TabDeckEditor(TabSupervisor *_tabSupervisor, QWidget *parent) ...@@ -121,7 +137,13 @@ TabDeckEditor::TabDeckEditor(TabSupervisor *_tabSupervisor, QWidget *parent)
deckView->setModel(deckModel); deckView->setModel(deckModel);
deckView->setUniformRowHeights(true); deckView->setUniformRowHeights(true);
deckView->header()->setResizeMode(QHeaderView::ResizeToContents); deckView->header()->setResizeMode(QHeaderView::ResizeToContents);
deckViewKeySignals.filterLeftRight(false);
deckView->installEventFilter(&deckViewKeySignals);
connect(deckView->selectionModel(), SIGNAL(currentRowChanged(const QModelIndex &, const QModelIndex &)), this, SLOT(updateCardInfoRight(const QModelIndex &, const QModelIndex &))); connect(deckView->selectionModel(), SIGNAL(currentRowChanged(const QModelIndex &, const QModelIndex &)), this, SLOT(updateCardInfoRight(const QModelIndex &, const QModelIndex &)));
connect(&deckViewKeySignals, SIGNAL(onEnter()), this, SLOT(actIncrement()));
connect(&deckViewKeySignals, SIGNAL(onRight()), this, SLOT(actIncrement()));
connect(&deckViewKeySignals, SIGNAL(onLeft()), this, SLOT(actDecrement()));
connect(&deckViewKeySignals, SIGNAL(onDelete()), this, SLOT(actRemoveCard()));
nameLabel = new QLabel(); nameLabel = new QLabel();
nameEdit = new QLineEdit; nameEdit = new QLineEdit;
...@@ -286,9 +308,8 @@ void TabDeckEditor::retranslateUi() ...@@ -286,9 +308,8 @@ void TabDeckEditor::retranslateUi()
aClose->setShortcut(tr("Ctrl+Q")); aClose->setShortcut(tr("Ctrl+Q"));
aAddCard->setText(tr("Add card to &maindeck")); aAddCard->setText(tr("Add card to &maindeck"));
aAddCard->setShortcuts(QList<QKeySequence>() << QKeySequence(tr("Return")) << QKeySequence(tr("Enter")));
aAddCardToSideboard->setText(tr("Add card to &sideboard")); aAddCardToSideboard->setText(tr("Add card to &sideboard"));
aAddCardToSideboard->setShortcuts(QList<QKeySequence>() << QKeySequence(tr("Ctrl+Return")) << QKeySequence(tr("Ctrl+Enter")));
aRemoveCard->setText(tr("&Remove row")); aRemoveCard->setText(tr("&Remove row"));
aRemoveCard->setShortcut(tr("Del")); aRemoveCard->setShortcut(tr("Del"));
aIncrement->setText(tr("&Increment number")); aIncrement->setText(tr("&Increment number"));
...@@ -517,18 +538,27 @@ void TabDeckEditor::recursiveExpand(const QModelIndex &index) ...@@ -517,18 +538,27 @@ void TabDeckEditor::recursiveExpand(const QModelIndex &index)
deckView->expand(index); deckView->expand(index);
} }
void TabDeckEditor::addCardHelper(QString zoneName) CardInfo *TabDeckEditor::currentCardInfo() const
{ {
const QModelIndex currentIndex = databaseView->selectionModel()->currentIndex(); const QModelIndex currentIndex = databaseView->selectionModel()->currentIndex();
if (!currentIndex.isValid()) if (!currentIndex.isValid())
return; return NULL;
const QString cardName = currentIndex.sibling(currentIndex.row(), 0).data().toString(); const QString cardName = currentIndex.sibling(currentIndex.row(), 0).data().toString();
CardInfo *info = db->getCard(cardName); return db->getCard(cardName);
}
void TabDeckEditor::addCardHelper(QString zoneName)
{
const CardInfo *info;
info = currentCardInfo();
if(!info)
return;
if (info->getIsToken()) if (info->getIsToken())
zoneName = "tokens"; zoneName = "tokens";
QModelIndex newCardIndex = deckModel->addCard(cardName, zoneName); QModelIndex newCardIndex = deckModel->addCard(info->getName(), zoneName);
recursiveExpand(newCardIndex); recursiveExpand(newCardIndex);
deckView->setCurrentIndex(newCardIndex); deckView->setCurrentIndex(newCardIndex);
...@@ -554,31 +584,56 @@ void TabDeckEditor::actRemoveCard() ...@@ -554,31 +584,56 @@ void TabDeckEditor::actRemoveCard()
setModified(true); setModified(true);
} }
void TabDeckEditor::actIncrement() void TabDeckEditor::offsetCountAtIndex(const QModelIndex &idx, int offset)
{ {
const QModelIndex &currentIndex = deckView->selectionModel()->currentIndex(); if (!idx.isValid() || offset == 0)
if (!currentIndex.isValid())
return; return;
const QModelIndex numberIndex = currentIndex.sibling(currentIndex.row(), 0);
const QModelIndex numberIndex = idx.sibling(idx.row(), 0);
const int count = deckModel->data(numberIndex, Qt::EditRole).toInt(); const int count = deckModel->data(numberIndex, Qt::EditRole).toInt();
deckView->setCurrentIndex(numberIndex); deckView->setCurrentIndex(numberIndex);
deckModel->setData(numberIndex, count + 1, Qt::EditRole); if ((count + offset) <= 0)
deckModel->removeRow(idx.row(), idx.parent());
else
deckModel->setData(numberIndex, count + offset, Qt::EditRole);
setModified(true); setModified(true);
} }
void TabDeckEditor::decrementCardHelper(QString zoneName)
{
const CardInfo *info;
QModelIndex idx;
info = currentCardInfo();
if(!info)
return;
if (info->getIsToken())
zoneName = "tokens";
idx = deckModel->findCard(info->getName(), zoneName);
offsetCountAtIndex(idx, -1);
}
void TabDeckEditor::actDecrementCard()
{
decrementCardHelper("main");
}
void TabDeckEditor::actDecrementCardFromSideboard()
{
decrementCardHelper("side");
}
void TabDeckEditor::actIncrement()
{
const QModelIndex &currentIndex = deckView->selectionModel()->currentIndex();
offsetCountAtIndex(currentIndex, 1);
}
void TabDeckEditor::actDecrement() void TabDeckEditor::actDecrement()
{ {
const QModelIndex &currentIndex = deckView->selectionModel()->currentIndex(); const QModelIndex &currentIndex = deckView->selectionModel()->currentIndex();
if (!currentIndex.isValid()) offsetCountAtIndex(currentIndex, -1);
return;
const QModelIndex numberIndex = currentIndex.sibling(currentIndex.row(), 0);
const int count = deckModel->data(numberIndex, Qt::EditRole).toInt();
deckView->setCurrentIndex(numberIndex);
if (count == 1)
deckModel->removeRow(currentIndex.row(), currentIndex.parent());
else
deckModel->setData(numberIndex, count - 1, Qt::EditRole);
setModified(true);
} }
void TabDeckEditor::actUpdatePrices() void TabDeckEditor::actUpdatePrices()
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "tab.h" #include "tab.h"
#include <QAbstractItemModel> #include <QAbstractItemModel>
#include <QLineEdit> #include <QLineEdit>
#include "keysignals.h"
class CardDatabaseModel; class CardDatabaseModel;
class CardDatabaseDisplayModel; class CardDatabaseDisplayModel;
...@@ -16,6 +17,7 @@ class QLabel; ...@@ -16,6 +17,7 @@ class QLabel;
class DeckLoader; class DeckLoader;
class Response; class Response;
class FilterTreeModel; class FilterTreeModel;
class CardInfo;
class SearchLineEdit : public QLineEdit { class SearchLineEdit : public QLineEdit {
private: private:
...@@ -56,6 +58,9 @@ private slots: ...@@ -56,6 +58,9 @@ private slots:
void actRemoveCard(); void actRemoveCard();
void actIncrement(); void actIncrement();
void actDecrement(); void actDecrement();
void actDecrementCard();
void actDecrementCardFromSideboard();
void actUpdatePrices(); void actUpdatePrices();
void finishedUpdatingPrices(); void finishedUpdatingPrices();
...@@ -63,7 +68,10 @@ private slots: ...@@ -63,7 +68,10 @@ private slots:
void filterViewCustomContextMenu(const QPoint &point); void filterViewCustomContextMenu(const QPoint &point);
void filterRemove(QAction *action); void filterRemove(QAction *action);
private: private:
CardInfo *currentCardInfo() const;
void addCardHelper(QString zoneName); void addCardHelper(QString zoneName);
void offsetCountAtIndex(const QModelIndex &idx, int offset);
void decrementCardHelper(QString zoneName);
void recursiveExpand(const QModelIndex &index); void recursiveExpand(const QModelIndex &index);
bool confirmClose(); bool confirmClose();
...@@ -71,10 +79,13 @@ private: ...@@ -71,10 +79,13 @@ private:
CardDatabaseDisplayModel *databaseDisplayModel; CardDatabaseDisplayModel *databaseDisplayModel;
DeckListModel *deckModel; DeckListModel *deckModel;
QTreeView *databaseView; QTreeView *databaseView;
KeySignals dbViewKeySignals;
QTreeView *deckView; QTreeView *deckView;
KeySignals deckViewKeySignals;
CardFrame *cardInfo; CardFrame *cardInfo;
QLabel *searchLabel; QLabel *searchLabel;
SearchLineEdit *searchEdit; SearchLineEdit *searchEdit;
KeySignals searchKeySignals;
QLabel *nameLabel; QLabel *nameLabel;
QLineEdit *nameEdit; QLineEdit *nameEdit;
QLabel *commentsLabel; QLabel *commentsLabel;
......
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