diff --git a/cockatrice/CMakeLists.txt b/cockatrice/CMakeLists.txt index 4012795fd..c90a00d7e 100644 --- a/cockatrice/CMakeLists.txt +++ b/cockatrice/CMakeLists.txt @@ -26,6 +26,7 @@ SET(cockatrice_SOURCES src/handzone.cpp src/handcounter.cpp src/carddatabase.cpp + src/keysignals.cpp src/gameview.cpp src/gameselector.cpp src/decklistmodel.cpp @@ -110,6 +111,7 @@ SET(cockatrice_HEADERS src/handzone.h src/handcounter.h src/carddatabase.h + src/keysignals.h src/gameview.h src/gameselector.h src/decklistmodel.h diff --git a/cockatrice/src/decklistmodel.cpp b/cockatrice/src/decklistmodel.cpp index 6abbba86a..68062ead1 100644 --- a/cockatrice/src/decklistmodel.cpp +++ b/cockatrice/src/decklistmodel.cpp @@ -169,8 +169,7 @@ Qt::ItemFlags DeckListModel::flags(const QModelIndex &index) const return 0; Qt::ItemFlags result = Qt::ItemIsEnabled; - if (getNode(index)) - result |= Qt::ItemIsSelectable; + result |= Qt::ItemIsSelectable; return result; } @@ -236,6 +235,38 @@ InnerDecklistNode *DeckListModel::createNodeIfNeeded(const QString &name, InnerD return newNode; } +DecklistModelCardNode *DeckListModel::findCardNode(const QString &cardName, const QString &zoneName) const +{ + InnerDecklistNode *zoneNode, *typeNode; + CardInfo *info; + QString cardType; + + zoneNode = dynamic_cast(root->findChild(zoneName)); + if(!zoneNode) + return NULL; + + info = db->getCard(cardName); + if(!info) + return NULL; + + cardType = info->getMainCardType(); + typeNode = dynamic_cast(zoneNode->findChild(cardType)); + if(!typeNode) + return NULL; + + return dynamic_cast(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) { InnerDecklistNode *zoneNode = createNodeIfNeeded(zoneName, root); diff --git a/cockatrice/src/decklistmodel.h b/cockatrice/src/decklistmodel.h index 4d81d0073..43c8e2a31 100644 --- a/cockatrice/src/decklistmodel.h +++ b/cockatrice/src/decklistmodel.h @@ -45,6 +45,8 @@ public: Qt::ItemFlags flags(const QModelIndex &index) const; bool setData(const QModelIndex &index, const QVariant &value, int role); 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); void sort(int column, Qt::SortOrder order = Qt::AscendingOrder); void cleanList(); @@ -56,6 +58,7 @@ private: InnerDecklistNode *root; InnerDecklistNode *createNodeIfNeeded(const QString &name, InnerDecklistNode *parent); QModelIndex nodeToIndex(AbstractDecklistNode *node) const; + DecklistModelCardNode *findCardNode(const QString &cardName, const QString &zoneName) const; void emitRecursiveUpdates(const QModelIndex &index); void sortHelper(InnerDecklistNode *node, Qt::SortOrder order); diff --git a/cockatrice/src/filterbuilder.cpp b/cockatrice/src/filterbuilder.cpp index 3b5206636..2e0bc73db 100644 --- a/cockatrice/src/filterbuilder.cpp +++ b/cockatrice/src/filterbuilder.cpp @@ -41,7 +41,8 @@ FilterBuilder::FilterBuilder(QWidget *parent) layout->setAlignment(Qt::AlignTop); 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())); fltr = NULL; } @@ -62,7 +63,7 @@ static int comboCurrentIntData(const QComboBox *combo) return combo->itemData(combo->currentIndex()).toInt(); } -void FilterBuilder::add_released() +void FilterBuilder::emit_add() { QString txt; diff --git a/cockatrice/src/filterbuilder.h b/cockatrice/src/filterbuilder.h index c126c82bb..1d0565f38 100644 --- a/cockatrice/src/filterbuilder.h +++ b/cockatrice/src/filterbuilder.h @@ -28,7 +28,7 @@ signals: public slots: private slots: - void add_released(); + void emit_add(); protected: }; diff --git a/cockatrice/src/keysignals.cpp b/cockatrice/src/keysignals.cpp new file mode 100644 index 000000000..1994e3874 --- /dev/null +++ b/cockatrice/src/keysignals.cpp @@ -0,0 +1,49 @@ +#include "keysignals.h" +#include + +bool KeySignals::eventFilter(QObject * /*object*/, QEvent *event) { + QKeyEvent *kevent; + + if(event->type() != QEvent::KeyPress) + return false; + + kevent = static_cast(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; +} diff --git a/cockatrice/src/keysignals.h b/cockatrice/src/keysignals.h new file mode 100644 index 000000000..006e0ce3e --- /dev/null +++ b/cockatrice/src/keysignals.h @@ -0,0 +1,30 @@ +#include +#include + +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; } +}; diff --git a/cockatrice/src/tab_deck_editor.cpp b/cockatrice/src/tab_deck_editor.cpp index 381791978..8454cd25c 100644 --- a/cockatrice/src/tab_deck_editor.cpp +++ b/cockatrice/src/tab_deck_editor.cpp @@ -54,8 +54,15 @@ TabDeckEditor::TabDeckEditor(TabSupervisor *_tabSupervisor, QWidget *parent) searchLabel = new QLabel(); searchEdit = new SearchLineEdit; searchLabel->setBuddy(searchEdit); + searchKeySignals.filterDelete(false); + searchEdit->installEventFilter(&searchKeySignals); 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; deckEditToolBar->setOrientation(Qt::Horizontal); @@ -71,6 +78,7 @@ TabDeckEditor::TabDeckEditor(TabSupervisor *_tabSupervisor, QWidget *parent) databaseDisplayModel->setSourceModel(databaseModel); databaseDisplayModel->setFilterKeyColumn(0); databaseDisplayModel->sort(0, Qt::AscendingOrder); + databaseView = new QTreeView(); databaseView->setModel(databaseDisplayModel); databaseView->setUniformRowHeights(true); @@ -81,6 +89,14 @@ TabDeckEditor::TabDeckEditor(TabSupervisor *_tabSupervisor, QWidget *parent) databaseView->resizeColumnToContents(0); 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())); + 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); QVBoxLayout *leftFrame = new QVBoxLayout; @@ -121,7 +137,13 @@ TabDeckEditor::TabDeckEditor(TabSupervisor *_tabSupervisor, QWidget *parent) deckView->setModel(deckModel); deckView->setUniformRowHeights(true); 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(&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(); nameEdit = new QLineEdit; @@ -286,9 +308,8 @@ void TabDeckEditor::retranslateUi() aClose->setShortcut(tr("Ctrl+Q")); aAddCard->setText(tr("Add card to &maindeck")); - aAddCard->setShortcuts(QList() << QKeySequence(tr("Return")) << QKeySequence(tr("Enter"))); aAddCardToSideboard->setText(tr("Add card to &sideboard")); - aAddCardToSideboard->setShortcuts(QList() << QKeySequence(tr("Ctrl+Return")) << QKeySequence(tr("Ctrl+Enter"))); + aRemoveCard->setText(tr("&Remove row")); aRemoveCard->setShortcut(tr("Del")); aIncrement->setText(tr("&Increment number")); @@ -517,18 +538,27 @@ void TabDeckEditor::recursiveExpand(const QModelIndex &index) deckView->expand(index); } -void TabDeckEditor::addCardHelper(QString zoneName) +CardInfo *TabDeckEditor::currentCardInfo() const { const QModelIndex currentIndex = databaseView->selectionModel()->currentIndex(); if (!currentIndex.isValid()) - return; + return NULL; 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()) zoneName = "tokens"; - - QModelIndex newCardIndex = deckModel->addCard(cardName, zoneName); + + QModelIndex newCardIndex = deckModel->addCard(info->getName(), zoneName); recursiveExpand(newCardIndex); deckView->setCurrentIndex(newCardIndex); @@ -554,31 +584,56 @@ void TabDeckEditor::actRemoveCard() setModified(true); } +void TabDeckEditor::offsetCountAtIndex(const QModelIndex &idx, int offset) +{ + if (!idx.isValid() || offset == 0) + return; + + const QModelIndex numberIndex = idx.sibling(idx.row(), 0); + const int count = deckModel->data(numberIndex, Qt::EditRole).toInt(); + deckView->setCurrentIndex(numberIndex); + if ((count + offset) <= 0) + deckModel->removeRow(idx.row(), idx.parent()); + else + deckModel->setData(numberIndex, count + offset, Qt::EditRole); + 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 ¤tIndex = deckView->selectionModel()->currentIndex(); - if (!currentIndex.isValid()) - return; - const QModelIndex numberIndex = currentIndex.sibling(currentIndex.row(), 0); - const int count = deckModel->data(numberIndex, Qt::EditRole).toInt(); - deckView->setCurrentIndex(numberIndex); - deckModel->setData(numberIndex, count + 1, Qt::EditRole); - setModified(true); + offsetCountAtIndex(currentIndex, 1); } void TabDeckEditor::actDecrement() { const QModelIndex ¤tIndex = deckView->selectionModel()->currentIndex(); - if (!currentIndex.isValid()) - 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); + offsetCountAtIndex(currentIndex, -1); } void TabDeckEditor::actUpdatePrices() diff --git a/cockatrice/src/tab_deck_editor.h b/cockatrice/src/tab_deck_editor.h index 0821e6054..8dde3e583 100644 --- a/cockatrice/src/tab_deck_editor.h +++ b/cockatrice/src/tab_deck_editor.h @@ -4,6 +4,7 @@ #include "tab.h" #include #include +#include "keysignals.h" class CardDatabaseModel; class CardDatabaseDisplayModel; @@ -16,6 +17,7 @@ class QLabel; class DeckLoader; class Response; class FilterTreeModel; +class CardInfo; class SearchLineEdit : public QLineEdit { private: @@ -56,6 +58,9 @@ private slots: void actRemoveCard(); void actIncrement(); void actDecrement(); + void actDecrementCard(); + void actDecrementCardFromSideboard(); + void actUpdatePrices(); void finishedUpdatingPrices(); @@ -63,7 +68,10 @@ private slots: void filterViewCustomContextMenu(const QPoint &point); void filterRemove(QAction *action); private: + CardInfo *currentCardInfo() const; void addCardHelper(QString zoneName); + void offsetCountAtIndex(const QModelIndex &idx, int offset); + void decrementCardHelper(QString zoneName); void recursiveExpand(const QModelIndex &index); bool confirmClose(); @@ -71,10 +79,13 @@ private: CardDatabaseDisplayModel *databaseDisplayModel; DeckListModel *deckModel; QTreeView *databaseView; + KeySignals dbViewKeySignals; QTreeView *deckView; + KeySignals deckViewKeySignals; CardFrame *cardInfo; QLabel *searchLabel; SearchLineEdit *searchEdit; + KeySignals searchKeySignals; QLabel *nameLabel; QLineEdit *nameEdit; QLabel *commentsLabel;