Commit 60f99818 authored by Max-Wilhelm Bruker's avatar Max-Wilhelm Bruker
Browse files

fixed DlgEditTokens crash when removing a token

parent 089c9371
...@@ -16,6 +16,7 @@ public: ...@@ -16,6 +16,7 @@ public:
int columnCount(const QModelIndex &parent = QModelIndex()) const; int columnCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role) const; QVariant data(const QModelIndex &index, int role) const;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
CardDatabase *getDatabase() const { return db; }
CardInfo *getCard(int index) const { return cardList[index]; } CardInfo *getCard(int index) const { return cardList[index]; }
private: private:
QList<CardInfo *> cardList; QList<CardInfo *> cardList;
......
#include "dlg_edit_tokens.h" #include "dlg_edit_tokens.h"
#include "carddatabasemodel.h" #include "carddatabasemodel.h"
#include "main.h"
#include <QDialogButtonBox> #include <QDialogButtonBox>
#include <QVBoxLayout> #include <QVBoxLayout>
#include <QHBoxLayout> #include <QHBoxLayout>
...@@ -16,8 +15,8 @@ ...@@ -16,8 +15,8 @@
#include <QInputDialog> #include <QInputDialog>
#include <QMessageBox> #include <QMessageBox>
DlgEditTokens::DlgEditTokens(QWidget *parent) DlgEditTokens::DlgEditTokens(CardDatabaseModel *_cardDatabaseModel, QWidget *parent)
: QDialog(parent), currentCard(0) : QDialog(parent), currentCard(0), cardDatabaseModel(_cardDatabaseModel)
{ {
nameLabel = new QLabel(tr("&Name:")); nameLabel = new QLabel(tr("&Name:"));
nameEdit = new QLineEdit; nameEdit = new QLineEdit;
...@@ -59,7 +58,6 @@ DlgEditTokens::DlgEditTokens(QWidget *parent) ...@@ -59,7 +58,6 @@ DlgEditTokens::DlgEditTokens(QWidget *parent)
QGroupBox *tokenDataGroupBox = new QGroupBox(tr("Token data")); QGroupBox *tokenDataGroupBox = new QGroupBox(tr("Token data"));
tokenDataGroupBox->setLayout(grid); tokenDataGroupBox->setLayout(grid);
cardDatabaseModel = new CardDatabaseModel(db, this);
cardDatabaseDisplayModel = new CardDatabaseDisplayModel(this); cardDatabaseDisplayModel = new CardDatabaseDisplayModel(this);
cardDatabaseDisplayModel->setSourceModel(cardDatabaseModel); cardDatabaseDisplayModel->setSourceModel(cardDatabaseModel);
cardDatabaseDisplayModel->setIsToken(CardDatabaseDisplayModel::ShowTrue); cardDatabaseDisplayModel->setIsToken(CardDatabaseDisplayModel::ShowTrue);
...@@ -113,7 +111,7 @@ DlgEditTokens::DlgEditTokens(QWidget *parent) ...@@ -113,7 +111,7 @@ DlgEditTokens::DlgEditTokens(QWidget *parent)
void DlgEditTokens::tokenSelectionChanged(const QModelIndex &current, const QModelIndex &previous) void DlgEditTokens::tokenSelectionChanged(const QModelIndex &current, const QModelIndex &previous)
{ {
const QModelIndex realIndex = cardDatabaseDisplayModel->mapToSource(current); const QModelIndex realIndex = cardDatabaseDisplayModel->mapToSource(current);
CardInfo *cardInfo = current.row() >= 0 ? cardDatabaseModel->getCard(realIndex.row()) : db->getCard(); CardInfo *cardInfo = current.row() >= 0 ? cardDatabaseModel->getCard(realIndex.row()) : cardDatabaseModel->getDatabase()->getCard();
if (!cardInfo->getName().isEmpty()) if (!cardInfo->getName().isEmpty())
currentCard = cardInfo; currentCard = cardInfo;
else else
...@@ -132,7 +130,7 @@ void DlgEditTokens::actAddToken() ...@@ -132,7 +130,7 @@ void DlgEditTokens::actAddToken()
bool askAgain; bool askAgain;
do { do {
name = QInputDialog::getText(this, tr("Add token"), tr("Please enter the name of the token:")); name = QInputDialog::getText(this, tr("Add token"), tr("Please enter the name of the token:"));
if (!name.isEmpty() && db->getCard(name, false)) { if (!name.isEmpty() && cardDatabaseModel->getDatabase()->getCard(name, false)) {
QMessageBox::critical(this, tr("Error"), tr("The chosen name conflicts with an existing card or token.")); QMessageBox::critical(this, tr("Error"), tr("The chosen name conflicts with an existing card or token."));
askAgain = true; askAgain = true;
} else } else
...@@ -142,18 +140,19 @@ void DlgEditTokens::actAddToken() ...@@ -142,18 +140,19 @@ void DlgEditTokens::actAddToken()
if (name.isEmpty()) if (name.isEmpty())
return; return;
CardInfo *card = new CardInfo(db, name, true); CardInfo *card = new CardInfo(cardDatabaseModel->getDatabase(), name, true);
card->addToSet(db->getSet("TK")); card->addToSet(cardDatabaseModel->getDatabase()->getSet("TK"));
card->setCardType("Token"); card->setCardType("Token");
db->addCard(card); cardDatabaseModel->getDatabase()->addCard(card);
} }
void DlgEditTokens::actRemoveToken() void DlgEditTokens::actRemoveToken()
{ {
if (currentCard) { if (currentCard) {
db->removeCard(currentCard); CardInfo *cardToRemove = currentCard; // the currentCard property gets modified during db->removeCard()
delete currentCard;
currentCard = 0; currentCard = 0;
cardDatabaseModel->getDatabase()->removeCard(cardToRemove);
delete cardToRemove;
} }
} }
......
...@@ -32,7 +32,7 @@ private: ...@@ -32,7 +32,7 @@ private:
QLineEdit *nameEdit, *ptEdit, *annotationEdit; QLineEdit *nameEdit, *ptEdit, *annotationEdit;
QTreeView *chooseTokenView; QTreeView *chooseTokenView;
public: public:
DlgEditTokens(QWidget *parent = 0); DlgEditTokens(CardDatabaseModel *_cardDatabaseModel, QWidget *parent = 0);
}; };
#endif #endif
...@@ -492,7 +492,7 @@ void TabDeckEditor::actEditSets() ...@@ -492,7 +492,7 @@ void TabDeckEditor::actEditSets()
void TabDeckEditor::actEditTokens() void TabDeckEditor::actEditTokens()
{ {
DlgEditTokens dlg; DlgEditTokens dlg(databaseModel);
dlg.exec(); dlg.exec();
db->saveToFile(settingsCache->getTokenDatabasePath(), true); db->saveToFile(settingsCache->getTokenDatabasePath(), true);
} }
......
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