servatrice/cockatrice/src/setsmodel.cpp
ZeldaZach b02adccf87 Support Qt6, Min Qt5.8, Fix Win32, Fix Servatrice
Add lock around deleting arrows for commanding cards

Add support for Qt6 w/ Backwards Qt5

Handle Qt5/6 cross compilation better

Last cleanups

caps matter

Fix serv

Prevent crash on 6.3.0 Linux & bump to 5.8 min

Prevent out of bounds indexing

Delete shutdown timer if it exists

Fixup ticket comments, remove unneeded guards

Try to add support for missing OSes

Update .ci/release_template.md

Update PR based on comments

Update XML name after done and remove Hirsute

Address local game crash

Address comments from PR (again)
Tests don't work on mac, will see if a problem on other OSes

make soundengine more consistent across qt versions

disable tests on distros that are covered by others

Fix Oracle Crash due to bad memory access

Update Oracle to use new Qt6 way of adding translations

Add support for Qt5/Qt6 compiling of Cockatrice

Remove unneeded calls to QtMath/cmath/math.h

Update how we handle bitwise comparisons for enums with Tray Icon

Change header guards to not duplicate function

Leave comment & Fix Path for GHA Qt

Update common/server.h

Update cockatrice/src/window_main.cpp

Rollback change on cmake module path for NSIS

check docker image requirements

add size limit to ccache

put variables in quotes

properly set build type on mac

avoid names used in cmake

fix up cmake module path

cmake 3.10 does not recognize prepend

Support Tests in FindQtRuntime

set ccache size on non debug builds as well

immediately return when removing non existing client

handle incTxBytes with a signal instead

don't set common link libraries in cockatrice/CMakeLists.txt

add comments

set macos qt version to 6

Try upgrading XCode versions to latest they can be supported on

Ensure Qt gets linked

add tmate so i can see what's going on

Qt6 points two directories further down than Qt5 with regard to the top lib path, so we need to account for this

Establish Plugins directory for Qt6

Establish TLS plugins for Qt6 services

Minor change for release channel network manager

Let windows build in parallel cores

Wrong symbols

Qt6 patch up for signal

add missing qt6 package on deb builds

boolean expressions are hard

negative indexes should go to the end

Intentionally fail cache

move size checks to individual zone types

Hardcode libs needed for building on Windows, as the regex was annoying

Update wording

use the --parallel option in all builds

clean up the .ci scripts some more

tweak fedora build

add os parameter to compile.sh

I don't really like this but it seems the easiest way
I'd prefer if these types of quirks would live in the main configuration
file, the yml

fixup yml

readd appended cache key to vcpkg step

fix windows 32 quirk

the json hash is already added to the key as well

remove os parameter and clean up ci files

set name_build.sh to output relative paths

set backwards compatible version of xcode and qt on mac

set QTDIR for mac builds on qt5

has no effect for qt6

export BUILD_DIR to name_build.sh

merge mac build steps

merge homebrew steps, set package suffix

link qt5

remove brew link

set qtdir to qt5 only

compile.sh vars need to be empty not 0

fix sets manager search bar on qt 5.12/15

fix oracle subprocess errors being ignored on qt 5

clean up translation loading

move en@source translation file so it will not get included in packages
NOTE: this needs to be done at transifex as well!

Use generator platform over osname

Short circuit if not Win defined
2022-05-06 17:31:08 -04:00

296 lines
8 KiB
C++

#include "setsmodel.h"
#include <QSortFilterProxyModel>
SetsModel::SetsModel(CardDatabase *_db, QObject *parent) : QAbstractTableModel(parent), sets(_db->getSetList())
{
sets.sortByKey();
for (const CardSetPtr &set : sets) {
if (set->getEnabled())
enabledSets.insert(set);
}
}
SetsModel::~SetsModel() = default;
int SetsModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
else
return sets.size();
}
QVariant SetsModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || (index.column() >= NUM_COLS) || (index.row() >= rowCount()))
return QVariant();
CardSetPtr set = sets[index.row()];
if (index.column() == EnabledCol) {
switch (role) {
case SortRole:
return enabledSets.contains(set) ? "1" : "0";
case Qt::CheckStateRole:
return static_cast<int>(enabledSets.contains(set) ? Qt::Checked : Qt::Unchecked);
case Qt::DisplayRole:
default:
return QVariant();
}
}
if (role != Qt::DisplayRole && role != SortRole)
return QVariant();
switch (index.column()) {
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();
case ReleaseDateCol:
return set->getReleaseDate().toString(Qt::ISODate);
default:
return QVariant();
}
}
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 QString("Key"); /* no tr() for translations needed, column just used for sorting --> hidden */
case IsKnownCol:
return QString(
"Is known"); /* no tr() for translations needed, column is just used for sorting --> hidden */
case EnabledCol:
return tr("Enabled");
case SetTypeCol:
return tr("Set type");
case ShortNameCol:
return tr("Set code");
case LongNameCol:
return tr("Long name");
case ReleaseDateCol:
return tr("Release date");
default:
return QVariant();
}
}
Qt::ItemFlags SetsModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::NoItemFlags;
Qt::ItemFlags flags = QAbstractTableModel::flags(index) | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
if (index.column() == EnabledCol)
flags |= Qt::ItemIsUserCheckable;
return flags;
}
Qt::DropActions SetsModel::supportedDropActions() const
{
return Qt::MoveAction;
}
QMimeData *SetsModel::mimeData(const QModelIndexList &indexes) const
{
if (indexes.isEmpty())
return 0;
SetsMimeData *result = new SetsMimeData(indexes[0].row());
return qobject_cast<QMimeData *>(result);
}
bool SetsModel::dropMimeData(const QMimeData *data,
Qt::DropAction action,
int row,
int /*column*/,
const QModelIndex &parent)
{
if (action != Qt::MoveAction)
return false;
if (row == -1) {
if (!parent.isValid())
return false;
row = parent.row();
}
int oldRow = qobject_cast<const SetsMimeData *>(data)->getOldRow();
if (oldRow < row)
row--;
swapRows(oldRow, row);
return true;
}
void SetsModel::toggleRow(int row, bool enable)
{
CardSetPtr temp = sets.at(row);
if (enable)
enabledSets.insert(temp);
else
enabledSets.remove(temp);
emit dataChanged(index(row, EnabledCol), index(row, EnabledCol));
}
void SetsModel::toggleRow(int row)
{
CardSetPtr tmp = sets.at(row);
if (tmp == nullptr)
return;
if (enabledSets.contains(tmp))
enabledSets.remove(tmp);
else
enabledSets.insert(tmp);
emit dataChanged(index(row, EnabledCol), index(row, EnabledCol));
}
void SetsModel::toggleAll(bool enabled)
{
enabledSets.clear();
if (enabled)
for (CardSetPtr 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);
CardSetPtr temp = sets.takeAt(oldRow);
endRemoveRows();
beginInsertRows(QModelIndex(), newRow, newRow);
sets.insert(newRow, temp);
endInsertRows();
emit dataChanged(index(0, 0), index(rowCount() - 1, columnCount() - 1));
}
void SetsModel::sort(int column, Qt::SortOrder order)
{
QMultiMap<QString, CardSetPtr> setMap;
int numRows = rowCount();
int row;
for (row = 0; row < numRows; ++row)
setMap.insert(index(row, column).data(SetsModel::SortRole).toString(), sets.at(row));
QList<CardSetPtr> tmp = setMap.values();
sets.clear();
if (order == Qt::AscendingOrder) {
for (row = 0; row < tmp.size(); row++) {
sets.append(tmp.at(row));
}
} else {
for (row = tmp.size() - 1; row >= 0; row--) {
sets.append(tmp.at(row));
}
}
emit dataChanged(index(0, 0), index(numRows - 1, columnCount() - 1));
}
void SetsModel::save(CardDatabase *db)
{
// order
for (int i = 0; i < sets.size(); i++)
sets[i]->setSortKey(static_cast<unsigned int>(i + 1));
// enabled sets
for (const CardSetPtr &set : sets)
set->setEnabled(enabledSets.contains(set));
sets.sortByKey();
db->notifyEnabledSetsChanged();
}
void SetsModel::restore(CardDatabase *db)
{
// order
sets = db->getSetList();
sets.sortByKey();
// enabled sets
enabledSets.clear();
for (const CardSetPtr &set : sets) {
if (set->getEnabled())
enabledSets.insert(set);
}
emit dataChanged(index(0, 0), index(rowCount() - 1, columnCount() - 1));
}
QStringList SetsModel::mimeTypes() const
{
return QStringList() << "application/x-cockatricecardset";
}
SetsDisplayModel::SetsDisplayModel(QObject *parent) : QSortFilterProxyModel(parent)
{
setFilterCaseSensitivity(Qt::CaseInsensitive);
setSortCaseSensitivity(Qt::CaseInsensitive);
}
void SetsDisplayModel::fetchMore(const QModelIndex &index)
{
int itemsToFetch = sourceModel()->rowCount(index);
beginInsertRows(QModelIndex(), 0, itemsToFetch - 1);
endInsertRows();
}
bool SetsDisplayModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{
auto typeIndex = sourceModel()->index(sourceRow, SetsModel::SetTypeCol, sourceParent);
auto nameIndex = sourceModel()->index(sourceRow, SetsModel::LongNameCol, sourceParent);
auto shortNameIndex = sourceModel()->index(sourceRow, SetsModel::ShortNameCol, sourceParent);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0))
const auto filter = filterRegularExpression();
#else
const auto filter = filterRegExp();
#endif
return (sourceModel()->data(typeIndex).toString().contains(filter) ||
sourceModel()->data(nameIndex).toString().contains(filter) ||
sourceModel()->data(shortNameIndex).toString().contains(filter));
}
bool SetsDisplayModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
{
QString leftString = sourceModel()->data(left, SetsModel::SortRole).toString();
QString rightString = sourceModel()->data(right, SetsModel::SortRole).toString();
return QString::localeAwareCompare(leftString, rightString) < 0;
}