Deal with recent Qt methods deprecation (#3801)

* Deal with recent Qt methods deprecation

 * Use std::sort, std::less instead of qSort/qLess
 * Use QFontMetrics::horizontalAdvance instead of ::width
 * Use qApp->primaryScreen() instead of QDesktopWidget
 * use lambas instead of QSignalMapper
 * Use QTreeWidgetItem::setForeground instead of ::setTextColor
 * Use QDir::setPath instead of operator=(QString)
 * Use QList::swapItemsAt instead of ::swap

* fix error
This commit is contained in:
ctrlaltca 2019-08-28 02:06:54 +02:00 committed by Zach H
parent f54165025e
commit b6df5a4ac3
17 changed files with 71 additions and 40 deletions

View file

@ -16,7 +16,12 @@ void AbstractGraphicsItem::paintNumberEllipse(int number,
font.setWeight(QFont::Bold); font.setWeight(QFont::Bold);
QFontMetrics fm(font); QFontMetrics fm(font);
double w = fm.width(numStr) * 1.3; double w = 1.3 *
#if (QT_VERSION >= QT_VERSION_CHECK(5, 11, 0))
fm.horizontalAdvance(numStr);
#else
fm.width(numStr);
#endif
double h = fm.height() * 1.3; double h = fm.height() * 1.3;
if (w < h) if (w < h)
w = h; w = h;

View file

@ -11,6 +11,7 @@
#include <QDir> #include <QDir>
#include <QFile> #include <QFile>
#include <QMessageBox> #include <QMessageBox>
#include <algorithm>
#include <utility> #include <utility>
const char *CardDatabase::TOKENS_SETNAME = "TK"; const char *CardDatabase::TOKENS_SETNAME = "TK";
@ -105,7 +106,7 @@ public:
void SetList::sortByKey() void SetList::sortByKey()
{ {
qSort(begin(), end(), KeyCompareFunctor()); std::sort(begin(), end(), KeyCompareFunctor());
} }
int SetList::getEnabledSetsNum() int SetList::getEnabledSetsNum()

View file

@ -5,7 +5,12 @@
#include "cardinfowidget.h" #include "cardinfowidget.h"
#include "carditem.h" #include "carditem.h"
#include "main.h" #include "main.h"
#include <QApplication>
#if (QT_VERSION >= QT_VERSION_CHECK(5, 13, 0))
#include <QScreen>
#else
#include <QDesktopWidget> #include <QDesktopWidget>
#endif
#include <QVBoxLayout> #include <QVBoxLayout>
CardInfoWidget::CardInfoWidget(const QString &cardName, QWidget *parent, Qt::WindowFlags flags) CardInfoWidget::CardInfoWidget(const QString &cardName, QWidget *parent, Qt::WindowFlags flags)
@ -27,8 +32,13 @@ CardInfoWidget::CardInfoWidget(const QString &cardName, QWidget *parent, Qt::Win
setLayout(layout); setLayout(layout);
setFrameStyle(QFrame::Panel | QFrame::Raised); setFrameStyle(QFrame::Panel | QFrame::Raised);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 13, 0))
int pixmapHeight = qApp->primaryScreen()->geometry().height() / 3;
#else
QDesktopWidget desktopWidget; QDesktopWidget desktopWidget;
int pixmapHeight = desktopWidget.screenGeometry().height() / 3; int pixmapHeight = desktopWidget.screenGeometry().height() / 3;
#endif
int pixmapWidth = static_cast<int>(pixmapHeight / aspectRatio); int pixmapWidth = static_cast<int>(pixmapHeight / aspectRatio);
pic->setFixedWidth(pixmapWidth); pic->setFixedWidth(pixmapWidth);
pic->setFixedHeight(pixmapHeight); pic->setFixedHeight(pixmapHeight);

View file

@ -2,6 +2,8 @@
#include "carddatabase.h" #include "carddatabase.h"
#include "carditem.h" #include "carditem.h"
#include <algorithm>
CardList::CardList(bool _contentsKnown) : QList<CardItem *>(), contentsKnown(_contentsKnown) CardList::CardList(bool _contentsKnown) : QList<CardItem *>(), contentsKnown(_contentsKnown)
{ {
} }
@ -56,5 +58,5 @@ public:
void CardList::sort(int flags) void CardList::sort(int flags)
{ {
compareFunctor cf(flags); compareFunctor cf(flags);
qSort(begin(), end(), cf); std::sort(begin(), end(), cf);
} }

View file

@ -7,6 +7,7 @@
#include <QApplication> #include <QApplication>
#include <QGraphicsSceneMouseEvent> #include <QGraphicsSceneMouseEvent>
#include <QMouseEvent> #include <QMouseEvent>
#include <algorithm>
#include <math.h> #include <math.h>
DeckViewCardDragItem::DeckViewCardDragItem(DeckViewCard *_item, DeckViewCardDragItem::DeckViewCardDragItem(DeckViewCard *_item,
@ -283,7 +284,7 @@ void DeckViewCardContainer::rearrangeItems(const QList<QPair<int, int>> &rowsAnd
QList<QString> cardTypeList = cardsByType.uniqueKeys(); QList<QString> cardTypeList = cardsByType.uniqueKeys();
QList<DeckViewCard *> row = cardsByType.values(cardTypeList[i]); QList<DeckViewCard *> row = cardsByType.values(cardTypeList[i]);
qSort(row.begin(), row.end(), DeckViewCardContainer::sortCardsByName); std::sort(row.begin(), row.end(), DeckViewCardContainer::sortCardsByName);
for (int j = 0; j < row.size(); ++j) { for (int j = 0; j < row.size(); ++j) {
DeckViewCard *card = row[j]; DeckViewCard *card = row[j];
card->setPos(x + (j % tempCols) * CARD_WIDTH, yUntilNow + (j / tempCols) * CARD_HEIGHT); card->setPos(x + (j % tempCols) * CARD_WIDTH, yUntilNow + (j / tempCols) * CARD_HEIGHT);

View file

@ -26,6 +26,7 @@
#include <QMessageBox> #include <QMessageBox>
#include <QPushButton> #include <QPushButton>
#include <QRadioButton> #include <QRadioButton>
#include <QScreen>
#include <QSlider> #include <QSlider>
#include <QSpinBox> #include <QSpinBox>
#include <QStackedWidget> #include <QStackedWidget>
@ -1129,7 +1130,11 @@ void ShortcutSettingsPage::retranslateUi()
DlgSettings::DlgSettings(QWidget *parent) : QDialog(parent) DlgSettings::DlgSettings(QWidget *parent) : QDialog(parent)
{ {
#if (QT_VERSION >= QT_VERSION_CHECK(5, 13, 0))
QRect rec = qApp->primaryScreen()->availableGeometry();
#else
QRect rec = QApplication::desktop()->availableGeometry(); QRect rec = QApplication::desktop()->availableGeometry();
#endif
this->setMinimumSize(rec.width() / 2, rec.height() - 100); this->setMinimumSize(rec.width() / 2, rec.height() - 100);
this->setBaseSize(rec.width(), rec.height()); this->setBaseSize(rec.width(), rec.height());

View file

@ -20,6 +20,7 @@
#include <QSvgRenderer> #include <QSvgRenderer>
#include <QThread> #include <QThread>
#include <QUrl> #include <QUrl>
#include <algorithm>
#include <utility> #include <utility>
// never cache more than 300 cards at once for a single deck // never cache more than 300 cards at once for a single deck
@ -36,7 +37,7 @@ PictureToLoad::PictureToLoad(CardInfoPtr _card) : card(std::move(_card))
if (sortedSets.empty()) { if (sortedSets.empty()) {
sortedSets << CardSet::newInstance("", "", "", QDate()); sortedSets << CardSet::newInstance("", "", "", QDate());
} }
qSort(sortedSets.begin(), sortedSets.end(), SetDownloadPriorityComparator()); std::sort(sortedSets.begin(), sortedSets.end(), SetDownloadPriorityComparator());
// The first time called, nextSet will also populate the Urls for the first set. // The first time called, nextSet will also populate the Urls for the first set.
nextSet(); nextSet();
} }

View file

@ -26,7 +26,6 @@
#include <QPainter> #include <QPainter>
#include <QRegularExpression> #include <QRegularExpression>
#include <QRegularExpressionMatch> #include <QRegularExpressionMatch>
#include <QSignalMapper>
#include "pb/command_attach_card.pb.h" #include "pb/command_attach_card.pb.h"
#include "pb/command_change_zone_properties.pb.h" #include "pb/command_change_zone_properties.pb.h"
@ -3128,13 +3127,11 @@ void Player::addRelatedCardView(const CardItem *card, QMenu *cardMenu)
cardMenu->addSeparator(); cardMenu->addSeparator();
auto viewRelatedCards = new QMenu(tr("View related cards")); auto viewRelatedCards = new QMenu(tr("View related cards"));
cardMenu->addMenu(viewRelatedCards); cardMenu->addMenu(viewRelatedCards);
auto *signalMapper = new QSignalMapper(this);
for (const CardRelation *relatedCard : relatedCards) { for (const CardRelation *relatedCard : relatedCards) {
QAction *viewCard = viewRelatedCards->addAction(relatedCard->getName()); QString relatedCardName = relatedCard->getName();
connect(viewCard, SIGNAL(triggered()), signalMapper, SLOT(map())); QAction *viewCard = viewRelatedCards->addAction(relatedCardName);
signalMapper->setMapping(viewCard, relatedCard->getName()); connect(viewCard, &QAction::triggered, game, [this, relatedCardName] { game->viewCardInfo(relatedCardName); });
} }
connect(signalMapper, SIGNAL(mapped(const QString &)), game, SLOT(viewCardInfo(const QString &)));
} }
void Player::addRelatedCardActions(const CardItem *card, QMenu *cardMenu) void Player::addRelatedCardActions(const CardItem *card, QMenu *cardMenu)

View file

@ -176,10 +176,10 @@ void PlayerListWidget::setActivePlayer(int playerId)
QTreeWidgetItem *twi = i.value(); QTreeWidgetItem *twi = i.value();
if (i.key() == playerId) { if (i.key() == playerId) {
twi->setBackground(4, QColor(150, 255, 150)); twi->setBackground(4, QColor(150, 255, 150));
twi->setTextColor(4, QColor(0, 0, 0)); twi->setForeground(4, QColor(0, 0, 0));
} else { } else {
twi->setBackground(4, palette().base().color()); twi->setBackground(4, palette().base().color());
twi->setTextColor(4, palette().text().color()); twi->setForeground(4, palette().text().color());
} }
} }
} }

View file

@ -102,7 +102,7 @@ QStringMap &SoundEngine::getAvailableThemes()
// load themes from user profile dir // load themes from user profile dir
dir = settingsCache->getDataPath() + "/sounds"; dir.setPath(settingsCache->getDataPath() + "/sounds");
foreach (QString themeName, dir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot, QDir::Name)) { foreach (QString themeName, dir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot, QDir::Name)) {
if (!availableThemes.contains(themeName)) if (!availableThemes.contains(themeName))
@ -110,14 +110,15 @@ QStringMap &SoundEngine::getAvailableThemes()
} }
// load themes from cockatrice system dir // load themes from cockatrice system dir
dir = qApp->applicationDirPath() + dir.setPath(qApp->applicationDirPath() +
#ifdef Q_OS_MAC #ifdef Q_OS_MAC
"/../Resources/sounds"; "/../Resources/sounds"
#elif defined(Q_OS_WIN) #elif defined(Q_OS_WIN)
"/sounds"; "/sounds"
#else // linux #else // linux
"/../share/cockatrice/sounds"; "/../share/cockatrice/sounds"
#endif #endif
);
foreach (QString themeName, dir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot, QDir::Name)) { foreach (QString themeName, dir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot, QDir::Name)) {
if (!availableThemes.contains(themeName)) if (!availableThemes.contains(themeName))

View file

@ -35,7 +35,6 @@
#include <QProcessEnvironment> #include <QProcessEnvironment>
#include <QPushButton> #include <QPushButton>
#include <QRegularExpression> #include <QRegularExpression>
#include <QSignalMapper>
#include <QSplitter> #include <QSplitter>
#include <QTextBrowser> #include <QTextBrowser>
#include <QTextEdit> #include <QTextEdit>
@ -471,14 +470,12 @@ void TabDeckEditor::databaseCustomMenu(QPoint point)
if (relatedCards.isEmpty()) { if (relatedCards.isEmpty()) {
relatedMenu->setDisabled(true); relatedMenu->setDisabled(true);
} else { } else {
auto *signalMapper = new QSignalMapper(this);
for (const CardRelation *rel : relatedCards) { for (const CardRelation *rel : relatedCards) {
QAction *relatedCard; QString relatedCardName = rel->getName();
relatedCard = relatedMenu->addAction(rel->getName()); QAction *relatedCard = relatedMenu->addAction(relatedCardName);
connect(relatedCard, SIGNAL(triggered()), signalMapper, SLOT(map())); connect(relatedCard, &QAction::triggered, cardInfo,
signalMapper->setMapping(relatedCard, rel->getName()); [this, relatedCardName] { cardInfo->setCard(relatedCardName); });
} }
connect(signalMapper, SIGNAL(mapped(const QString &)), cardInfo, SLOT(setCard(const QString &)));
} }
menu.exec(databaseView->mapToGlobal(point)); menu.exec(databaseView->mapToGlobal(point));
} }
@ -820,7 +817,6 @@ bool TabDeckEditor::actSaveDeckAs()
QFileDialog dialog(this, tr("Save deck")); QFileDialog dialog(this, tr("Save deck"));
dialog.setDirectory(settingsCache->getDeckPath()); dialog.setDirectory(settingsCache->getDeckPath());
dialog.setAcceptMode(QFileDialog::AcceptSave); dialog.setAcceptMode(QFileDialog::AcceptSave);
dialog.setConfirmOverwrite(true);
dialog.setDefaultSuffix("cod"); dialog.setDefaultSuffix("cod");
dialog.setNameFilters(DeckLoader::fileNameFilters); dialog.setNameFilters(DeckLoader::fileNameFilters);
dialog.selectFile(deckModel->getDeckList()->getName().trimmed() + ".cod"); dialog.selectFile(deckModel->getDeckList()->getName().trimmed() + ".cod");

View file

@ -35,7 +35,7 @@ QStringMap &ThemeManager::getAvailableThemes()
availableThemes.clear(); availableThemes.clear();
// load themes from user profile dir // load themes from user profile dir
dir = settingsCache->getDataPath() + "/themes"; dir.setPath(settingsCache->getDataPath() + "/themes");
foreach (QString themeName, dir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot, QDir::Name)) { foreach (QString themeName, dir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot, QDir::Name)) {
if (!availableThemes.contains(themeName)) if (!availableThemes.contains(themeName))
@ -43,14 +43,15 @@ QStringMap &ThemeManager::getAvailableThemes()
} }
// load themes from cockatrice system dir // load themes from cockatrice system dir
dir = qApp->applicationDirPath() + dir.setPath(qApp->applicationDirPath() +
#ifdef Q_OS_MAC #ifdef Q_OS_MAC
"/../Resources/themes"; "/../Resources/themes"
#elif defined(Q_OS_WIN) #elif defined(Q_OS_WIN)
"/themes"; "/themes"
#else // linux #else // linux
"/../share/cockatrice/themes"; "/../share/cockatrice/themes"
#endif #endif
);
foreach (QString themeName, dir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot, QDir::Name)) { foreach (QString themeName, dir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot, QDir::Name)) {
if (!availableThemes.contains(themeName)) if (!availableThemes.contains(themeName))

View file

@ -17,6 +17,8 @@
#include <QToolBar> #include <QToolBar>
#include <QTreeView> #include <QTreeView>
#include <algorithm>
#define SORT_RESET -1 #define SORT_RESET -1
WndSets::WndSets(QWidget *parent) : QMainWindow(parent) WndSets::WndSets(QWidget *parent) : QMainWindow(parent)
@ -338,7 +340,7 @@ void WndSets::actDisableSome()
void WndSets::actUp() void WndSets::actUp()
{ {
QModelIndexList rows = view->selectionModel()->selectedRows(); QModelIndexList rows = view->selectionModel()->selectedRows();
qSort(rows.begin(), rows.end(), qLess<QModelIndex>()); std::sort(rows.begin(), rows.end(), std::less<QModelIndex>());
QSet<int> newRows; QSet<int> newRows;
if (rows.empty()) if (rows.empty())
@ -360,7 +362,8 @@ void WndSets::actUp()
void WndSets::actDown() void WndSets::actDown()
{ {
QModelIndexList rows = view->selectionModel()->selectedRows(); QModelIndexList rows = view->selectionModel()->selectedRows();
qSort(rows.begin(), rows.end(), qGreater<QModelIndex>()); // QModelIndex only implements operator<, so we can't use std::greater
std::sort(rows.begin(), rows.end(), [](const QModelIndex &a, const QModelIndex &b) { return !(b < a); });
QSet<int> newRows; QSet<int> newRows;
if (rows.empty()) if (rows.empty())
@ -382,7 +385,7 @@ void WndSets::actDown()
void WndSets::actTop() void WndSets::actTop()
{ {
QModelIndexList rows = view->selectionModel()->selectedRows(); QModelIndexList rows = view->selectionModel()->selectedRows();
qSort(rows.begin(), rows.end(), qLess<QModelIndex>()); std::sort(rows.begin(), rows.end(), std::less<QModelIndex>());
QSet<int> newRows; QSet<int> newRows;
int newRow = 0; int newRow = 0;
@ -407,7 +410,8 @@ void WndSets::actTop()
void WndSets::actBottom() void WndSets::actBottom()
{ {
QModelIndexList rows = view->selectionModel()->selectedRows(); QModelIndexList rows = view->selectionModel()->selectedRows();
qSort(rows.begin(), rows.end(), qGreater<QModelIndex>()); // QModelIndex only implements operator<, so we can't use std::greater
std::sort(rows.begin(), rows.end(), [](const QModelIndex &a, const QModelIndex &b) { return !(b < a); });
QSet<int> newRows; QSet<int> newRows;
int newRow = model->rowCount() - 1; int newRow = model->rowCount() - 1;

View file

@ -4,6 +4,7 @@
#include <QFile> #include <QFile>
#include <QRegularExpression> #include <QRegularExpression>
#include <QTextStream> #include <QTextStream>
#include <algorithm>
#if QT_VERSION < 0x050600 #if QT_VERSION < 0x050600
// qHash on QRegularExpression was added in 5.6, FIX IT // qHash on QRegularExpression was added in 5.6, FIX IT
@ -316,7 +317,7 @@ QVector<QPair<int, int>> InnerDecklistNode::sort(Qt::SortOrder order)
// Sort temporary list // Sort temporary list
compareFunctor cmp(order); compareFunctor cmp(order);
qSort(tempList.begin(), tempList.end(), cmp); std::sort(tempList.begin(), tempList.end(), cmp);
// Map old indexes to new indexes and // Map old indexes to new indexes and
// copy temporary list to the current one // copy temporary list to the current one

View file

@ -59,7 +59,11 @@ void Server_CardZone::shuffle(int start, int end)
for (int i = end; i > start; i--) { for (int i = end; i > start; i--) {
int j = rng->rand(start, i); int j = rng->rand(start, i);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 13, 0))
cards.swapItemsAt(j, i);
#else
cards.swap(j, i); cards.swap(j, i);
#endif
} }
playersWithWritePermission.clear(); playersWithWritePermission.clear();
} }

View file

@ -84,6 +84,7 @@
#include "pb/context_undo_draw.pb.h" #include "pb/context_undo_draw.pb.h"
#include <QDebug> #include <QDebug>
#include <algorithm>
Server_Player::Server_Player(Server_Game *_game, Server_Player::Server_Player(Server_Game *_game,
int _playerId, int _playerId,
@ -409,7 +410,7 @@ Response::ResponseCode Server_Player::moveCard(GameEventStorage &ges,
// 0 performs no sorting // 0 performs no sorting
// 1 reverses the sorting // 1 reverses the sorting
MoveCardCompareFunctor cmp(0); MoveCardCompareFunctor cmp(0);
qSort(cardsToMove.begin(), cardsToMove.end(), cmp); std::sort(cardsToMove.begin(), cardsToMove.end(), cmp);
bool secondHalf = false; bool secondHalf = false;
int xIndex = -1; int xIndex = -1;

View file

@ -3,6 +3,7 @@
#include <QDebug> #include <QDebug>
#include <QtWidgets> #include <QtWidgets>
#include <algorithm>
#include <climits> #include <climits>
#include "qt-json/json.h" #include "qt-json/json.h"
@ -70,7 +71,7 @@ bool OracleImporter::readSetsFromByteArray(const QByteArray &data)
newSetList.append(SetToDownload(shortName, longName, setCards, setType, releaseDate)); newSetList.append(SetToDownload(shortName, longName, setCards, setType, releaseDate));
} }
qSort(newSetList); std::sort(newSetList.begin(), newSetList.end());
if (newSetList.isEmpty()) { if (newSetList.isEmpty()) {
return false; return false;
@ -337,8 +338,8 @@ int OracleImporter::importCardsFromSet(CardSetPtr currentSet, const QList<QVaria
// get all parts for this specific card // get all parts for this specific card
QList<SplitCardPart> splitCardParts = splitCards.values(nameSplit); QList<SplitCardPart> splitCardParts = splitCards.values(nameSplit);
// sort them by index (aka position) // sort them by index (aka position)
qSort(splitCardParts.begin(), splitCardParts.end(), std::sort(splitCardParts.begin(), splitCardParts.end(),
[](const SplitCardPart &a, const SplitCardPart &b) -> bool { return a.getIndex() < b.getIndex(); }); [](const SplitCardPart &a, const SplitCardPart &b) -> bool { return a.getIndex() < b.getIndex(); });
text = QString(""); text = QString("");
isToken = false; isToken = false;