mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-04-27 07:48:01 -07:00
Rework the way sets selection/importing works; fix #539 (rebased)
This commit is contained in:
parent
e69ca60164
commit
881cea27f4
13 changed files with 369 additions and 162 deletions
|
|
@ -4,6 +4,11 @@ SetsModel::SetsModel(CardDatabase *_db, QObject *parent)
|
|||
: QAbstractTableModel(parent), sets(_db->getSetList())
|
||||
{
|
||||
sets.sortByKey();
|
||||
foreach(CardSet *set, sets)
|
||||
{
|
||||
if(set->getEnabled())
|
||||
enabledSets.insert(set);
|
||||
}
|
||||
}
|
||||
|
||||
SetsModel::~SetsModel()
|
||||
|
|
@ -20,12 +25,20 @@ int SetsModel::rowCount(const QModelIndex &parent) const
|
|||
|
||||
QVariant SetsModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
if (!index.isValid() || (index.column() >= NUM_COLS) || (index.row() >= rowCount()) || (role != Qt::DisplayRole))
|
||||
if (!index.isValid() || (index.column() >= NUM_COLS) || (index.row() >= rowCount()))
|
||||
return QVariant();
|
||||
|
||||
CardSet *set = sets[index.row()];
|
||||
|
||||
if ( role == Qt::CheckStateRole && index.column() == EnabledCol )
|
||||
return static_cast< int >( enabledSets.contains(set) ? Qt::Checked : Qt::Unchecked );
|
||||
|
||||
if (role != Qt::DisplayRole)
|
||||
return QVariant();
|
||||
|
||||
switch (index.column()) {
|
||||
case SortKeyCol: return QString("%1").arg(set->getSortKey(), 4, 10, QChar('0'));
|
||||
case SortKeyCol: return QString("%1").arg(set->getSortKey(), 8, 10, QChar('0'));
|
||||
case IsKnownCol: return set->getIsKnown();
|
||||
case SetTypeCol: return set->getSetType();
|
||||
case ShortNameCol: return set->getShortName();
|
||||
case LongNameCol: return set->getLongName();
|
||||
|
|
@ -34,12 +47,24 @@ QVariant SetsModel::data(const QModelIndex &index, int role) const
|
|||
}
|
||||
}
|
||||
|
||||
bool SetsModel::setData(const QModelIndex & index, const QVariant & value, int role)
|
||||
{
|
||||
if (role == Qt::CheckStateRole && index.column () == EnabledCol)
|
||||
{
|
||||
toggleRow(index.row(), value == Qt::Checked);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
QVariant SetsModel::headerData(int section, Qt::Orientation orientation, int role) const
|
||||
{
|
||||
if ((role != Qt::DisplayRole) || (orientation != Qt::Horizontal))
|
||||
return QVariant();
|
||||
switch (section) {
|
||||
case SortKeyCol: return tr("Key");
|
||||
case IsKnownCol: return tr("Is known");
|
||||
case EnabledCol: return tr("Enabled");
|
||||
case SetTypeCol: return tr("Set type");
|
||||
case ShortNameCol: return tr("Set code");
|
||||
case LongNameCol: return tr("Long name");
|
||||
|
|
@ -50,8 +75,17 @@ QVariant SetsModel::headerData(int section, Qt::Orientation orientation, int rol
|
|||
|
||||
Qt::ItemFlags SetsModel::flags(const QModelIndex &index) const
|
||||
{
|
||||
Qt::ItemFlags result = QAbstractTableModel::flags(index);
|
||||
return result | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
|
||||
if (!index.isValid())
|
||||
return 0;
|
||||
|
||||
Qt::ItemFlags flags = QAbstractTableModel::flags(index) |
|
||||
Qt::ItemIsDragEnabled |
|
||||
Qt::ItemIsDropEnabled;
|
||||
|
||||
if ( index.column() == EnabledCol)
|
||||
flags |= Qt::ItemIsUserCheckable;
|
||||
|
||||
return flags;
|
||||
}
|
||||
|
||||
Qt::DropActions SetsModel::supportedDropActions() const
|
||||
|
|
@ -86,6 +120,30 @@ bool SetsModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int r
|
|||
return true;
|
||||
}
|
||||
|
||||
void SetsModel::toggleRow(int row, bool enable)
|
||||
{
|
||||
CardSet *temp = sets.at(row);
|
||||
|
||||
if(enable)
|
||||
enabledSets.insert(temp);
|
||||
else
|
||||
enabledSets.remove(temp);
|
||||
|
||||
emit dataChanged(index(row, EnabledCol), index(row, EnabledCol));
|
||||
}
|
||||
|
||||
void SetsModel::toggleAll(bool enable)
|
||||
{
|
||||
enabledSets.clear();
|
||||
if(enable)
|
||||
{
|
||||
foreach(CardSet *set, sets)
|
||||
enabledSets.insert(set);
|
||||
}
|
||||
|
||||
emit dataChanged(index(0, 0), index(rowCount() - 1, columnCount() - 1));
|
||||
}
|
||||
|
||||
void SetsModel::swapRows(int oldRow, int newRow)
|
||||
{
|
||||
beginRemoveRows(QModelIndex(), oldRow, oldRow);
|
||||
|
|
@ -124,19 +182,35 @@ void SetsModel::sort(int column, Qt::SortOrder order)
|
|||
emit dataChanged(index(0, 0), index(numRows - 1, columnCount() - 1));
|
||||
}
|
||||
|
||||
void SetsModel::save()
|
||||
void SetsModel::save(CardDatabase *db)
|
||||
{
|
||||
// order
|
||||
for (int i = 0; i < sets.size(); i++)
|
||||
sets[i]->setSortKey(i);
|
||||
sets[i]->setSortKey(i+1);
|
||||
|
||||
// enabled sets
|
||||
foreach(CardSet *set, sets)
|
||||
set->setEnabled(enabledSets.contains(set));
|
||||
|
||||
sets.sortByKey();
|
||||
|
||||
db->emitCardListChanged();
|
||||
}
|
||||
|
||||
void SetsModel::restore(CardDatabase *db)
|
||||
{
|
||||
// order
|
||||
sets = db->getSetList();
|
||||
sets.sortByKey();
|
||||
|
||||
// enabled sets
|
||||
enabledSets.clear();
|
||||
foreach(CardSet *set, sets)
|
||||
{
|
||||
if(set->getEnabled())
|
||||
enabledSets.insert(set);
|
||||
}
|
||||
|
||||
emit dataChanged(index(0, 0), index(rowCount() - 1, columnCount() - 1));
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue