(Qt) Prevent some unneeded string heap allocations

This commit is contained in:
LibretroAdmin 2025-02-11 10:58:48 +01:00
parent 0f5a990141
commit 6c7522fef8
4 changed files with 54 additions and 45 deletions

View File

@ -279,12 +279,14 @@ QModelIndex GridView::moveCursor(QAbstractItemView::CursorAction cursorAction, Q
QModelIndex index = currentIndex(); QModelIndex index = currentIndex();
if (index.isValid()) if (index.isValid())
{ {
if ((cursorAction == MoveLeft && index.row() > 0) || (cursorAction == MoveRight && index.row() + 1 < model()->rowCount())) if ( (cursorAction == MoveLeft && index.row() > 0)
|| (cursorAction == MoveRight && index.row() + 1 < model()->rowCount()))
{ {
const int offset = (cursorAction == MoveLeft ? -1 : 1); const int offset = (cursorAction == MoveLeft ? -1 : 1);
index = model()->index(index.row() + offset, index.column(), index.parent()); index = model()->index(index.row() + offset, index.column(), index.parent());
} }
else if ((cursorAction == MoveUp && index.row() > 0) || (cursorAction == MoveDown && index.row() + 1 < model()->rowCount())) else if ((cursorAction == MoveUp && index.row() > 0)
|| (cursorAction == MoveDown && index.row() + 1 < model()->rowCount()))
{ {
const int offset = ((m_size + m_spacing) * (cursorAction == MoveUp ? -1 : 1)); const int offset = ((m_size + m_spacing) * (cursorAction == MoveUp ? -1 : 1));
QRect rect = viewportRectForRow(index.row()).toRect(); QRect rect = viewportRectForRow(index.row()).toRect();
@ -435,24 +437,24 @@ QString GridView::getLayout() const
switch (m_viewMode) switch (m_viewMode)
{ {
case Simple: case Simple:
return "simple"; return QStringLiteral("simple");
case Anchored: case Anchored:
return "anchored"; return QStringLiteral("anchored");
case Centered: case Centered:
default: default:
break; break;
} }
return "centered"; return QStringLiteral("centered");
} }
void GridView::setLayout(QString layout) void GridView::setLayout(QString layout)
{ {
if (layout == "anchored") if (layout == QLatin1String("anchored"))
m_viewMode = Anchored; m_viewMode = Anchored;
else if (layout == "centered") else if (layout == QLatin1String("centered"))
m_viewMode = Centered; m_viewMode = Centered;
else if (layout == "fixed") else if (layout == QLatin1String("fixed"))
m_viewMode = Simple; m_viewMode = Simple;
} }
@ -487,23 +489,23 @@ QString GridItem::getThumbnailVerticalAlign() const
switch (thumbnailVerticalAlignmentFlag) switch (thumbnailVerticalAlignmentFlag)
{ {
case Qt::AlignTop: case Qt::AlignTop:
return "top"; return QStringLiteral("top");
case Qt::AlignVCenter: case Qt::AlignVCenter:
return "center"; return QStringLiteral("center");
case Qt::AlignBottom: case Qt::AlignBottom:
default: default:
break; break;
} }
return "bottom"; return QStringLiteral("bottom");
} }
void GridItem::setThumbnailVerticalAlign(const QString valign) void GridItem::setThumbnailVerticalAlign(const QString valign)
{ {
if (valign == "top") if (valign == QLatin1String("top"))
thumbnailVerticalAlignmentFlag = Qt::AlignTop; thumbnailVerticalAlignmentFlag = Qt::AlignTop;
else if (valign == "center") else if (valign == QLatin1String("center"))
thumbnailVerticalAlignmentFlag = Qt::AlignVCenter; thumbnailVerticalAlignmentFlag = Qt::AlignVCenter;
else if (valign == "bottom") else if (valign == QLatin1String("bottom"))
thumbnailVerticalAlignmentFlag = Qt::AlignBottom; thumbnailVerticalAlignmentFlag = Qt::AlignBottom;
} }

View File

@ -97,7 +97,7 @@ void MainWindow::onThumbnailPackDownloadNetworkSslErrors(const QList<QSslError>
{ {
const QSslError &error = errors.at(i); const QSslError &error = errors.at(i);
QString string = QString string =
QString("Ignoring SSL error code ") QStringLiteral("Ignoring SSL error code ")
+ QString::number(error.error()) + QString::number(error.error())
+ ": " + ": "
+ error.errorString(); + error.errorString();
@ -389,7 +389,7 @@ void MainWindow::onThumbnailDownloadNetworkSslErrors(
{ {
const QSslError &error = errors.at(i); const QSslError &error = errors.at(i);
QString string = QString string =
QString("Ignoring SSL error code ") QStringLiteral("Ignoring SSL error code ")
+ QString::number(error.error()) + QString::number(error.error())
+ ": " + ": "
+ error.errorString(); + error.errorString();
@ -675,7 +675,7 @@ void MainWindow::onPlaylistThumbnailDownloadNetworkSslErrors(const QList<QSslErr
{ {
const QSslError &error = errors.at(i); const QSslError &error = errors.at(i);
QString string = QString string =
QString("Ignoring SSL error code ") QStringLiteral("Ignoring SSL error code ")
+ QString::number(error.error()) + QString::number(error.error())
+ ": " + ": "
+ error.errorString(); + error.errorString();
@ -923,7 +923,8 @@ void MainWindow::downloadPlaylistThumbnails(QString playlistPath)
QHash<QString, QString> hash2; QHash<QString, QString> hash2;
QHash<QString, QString> hash3; QHash<QString, QString> hash3;
QHash<QString, QString> hash4; QHash<QString, QString> hash4;
const QHash<QString, QString> &itemHash = m_playlistModel->index(i, 0).data(PlaylistModel::HASH).value< QHash<QString, QString> >(); const QHash<QString, QString> &itemHash =
m_playlistModel->index(i, 0).data(PlaylistModel::HASH).value< QHash<QString, QString> >();
hash["db_name"] = itemHash.value("db_name"); hash["db_name"] = itemHash.value("db_name");
hash["label_noext"] = itemHash.value("label_noext"); hash["label_noext"] = itemHash.value("label_noext");

View File

@ -1127,12 +1127,14 @@ void FileDropWidget::paintEvent(QPaintEvent *event)
void FileDropWidget::keyPressEvent(QKeyEvent *event) void FileDropWidget::keyPressEvent(QKeyEvent *event)
{ {
if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) int key = event->key();
if ( key == Qt::Key_Return
|| key == Qt::Key_Enter)
{ {
event->accept(); event->accept();
emit enterPressed(); emit enterPressed();
} }
else if (event->key() == Qt::Key_Delete) else if (key == Qt::Key_Delete)
{ {
event->accept(); event->accept();
emit deletePressed(); emit deletePressed();
@ -1149,7 +1151,8 @@ void FileDropWidget::dragEnterEvent(QDragEnterEvent *event)
event->acceptProposedAction(); event->acceptProposedAction();
} }
/* Workaround for QTBUG-72844. Without it, you can't drop on this if you first drag over another widget that doesn't accept drops. */ /* Workaround for QTBUG-72844. Without it, you can't drop on this if you
* first drag over another widget that doesn't accept drops. */
void FileDropWidget::dragMoveEvent(QDragMoveEvent *event) void FileDropWidget::dragMoveEvent(QDragMoveEvent *event)
{ {
event->acceptProposedAction(); event->acceptProposedAction();
@ -1161,9 +1164,9 @@ void FileDropWidget::dropEvent(QDropEvent *event)
if (data->hasUrls()) if (data->hasUrls())
{ {
QList<QUrl> urls = data->urls();
QStringList files;
int i; int i;
QStringList files;
QList<QUrl> urls = data->urls();
for (i = 0; i < urls.count(); i++) for (i = 0; i < urls.count(); i++)
{ {
@ -1196,7 +1199,9 @@ void MainWindow::onFileDropWidgetContextMenuRequested(const QPoint &pos)
if (!specialPlaylist) if (!specialPlaylist)
{ {
downloadThumbnailAction.reset(new QAction(QString(msg_hash_to_str(MENU_ENUM_LABEL_VALUE_QT_DOWNLOAD_THUMBNAIL)), this)); downloadThumbnailAction.reset(new QAction(
QString(msg_hash_to_str(
MENU_ENUM_LABEL_VALUE_QT_DOWNLOAD_THUMBNAIL)), this));
menu->addAction(downloadThumbnailAction.data()); menu->addAction(downloadThumbnailAction.data());
} }
@ -1270,8 +1275,7 @@ void MainWindow::onFileDropWidgetContextMenuRequested(const QPoint &pos)
addFilesToPlaylist(QStringList()); addFilesToPlaylist(QStringList());
else if (selectedAction == addFolderAction.data()) else if (selectedAction == addFolderAction.data())
{ {
QString dirPath = QFileDialog::getExistingDirectory( QString dirPath = QFileDialog::getExistingDirectory(this,
this,
msg_hash_to_str(MENU_ENUM_LABEL_VALUE_QT_SELECT_FOLDER), msg_hash_to_str(MENU_ENUM_LABEL_VALUE_QT_SELECT_FOLDER),
QString(), QFileDialog::ShowDirsOnly); QString(), QFileDialog::ShowDirsOnly);
@ -1312,7 +1316,9 @@ void MainWindow::onFileDropWidgetContextMenuRequested(const QPoint &pos)
if (!updateCurrentPlaylistEntry(contentHash)) if (!updateCurrentPlaylistEntry(contentHash))
{ {
showMessageBox(msg_hash_to_str(MENU_ENUM_LABEL_VALUE_QT_COULD_NOT_UPDATE_PLAYLIST_ENTRY), MainWindow::MSGBOX_TYPE_ERROR, Qt::ApplicationModal, false); showMessageBox(msg_hash_to_str(
MENU_ENUM_LABEL_VALUE_QT_COULD_NOT_UPDATE_PLAYLIST_ENTRY),
MainWindow::MSGBOX_TYPE_ERROR, Qt::ApplicationModal, false);
return; return;
} }
} }

View File

@ -2103,13 +2103,12 @@ void MainWindow::onLaunchWithComboBoxIndexChanged(int)
MainWindow::Theme MainWindow::getThemeFromString(QString themeString) MainWindow::Theme MainWindow::getThemeFromString(QString themeString)
{ {
if (themeString == "default") if (themeString == QLatin1String("default"))
return THEME_SYSTEM_DEFAULT; return THEME_SYSTEM_DEFAULT;
else if (themeString == "dark") else if (themeString == QLatin1String("dark"))
return THEME_DARK; return THEME_DARK;
else if (themeString == "custom") else if (themeString == QLatin1String("custom"))
return THEME_CUSTOM; return THEME_CUSTOM;
return THEME_SYSTEM_DEFAULT; return THEME_SYSTEM_DEFAULT;
} }
@ -3048,7 +3047,8 @@ void MainWindow::setCoreActions()
{ {
QVariantMap map = m_launchWithComboBox->itemData(i, Qt::UserRole).toMap(); QVariantMap map = m_launchWithComboBox->itemData(i, Qt::UserRole).toMap();
if (map.value("core_path").toString() == hash["core_path"] || map.value("core_name").toString() == coreName) if ( map.value("core_path").toString() == hash["core_path"]
|| map.value("core_name").toString() == coreName)
{ {
found_existing = true; found_existing = true;
break; break;
@ -3877,13 +3877,13 @@ QString MainWindow::getCurrentThumbnailTypeString()
ThumbnailType MainWindow::getThumbnailTypeFromString(QString thumbnailType) ThumbnailType MainWindow::getThumbnailTypeFromString(QString thumbnailType)
{ {
if (thumbnailType == "boxart") if (thumbnailType == QLatin1String("boxart"))
return THUMBNAIL_TYPE_BOXART; return THUMBNAIL_TYPE_BOXART;
else if (thumbnailType == "screenshot") else if (thumbnailType == QLatin1String("screenshot"))
return THUMBNAIL_TYPE_SCREENSHOT; return THUMBNAIL_TYPE_SCREENSHOT;
else if (thumbnailType == "title") else if (thumbnailType == QLatin1String("title"))
return THUMBNAIL_TYPE_TITLE_SCREEN; return THUMBNAIL_TYPE_TITLE_SCREEN;
else if (thumbnailType == "logo") else if (thumbnailType == QLatin1String("logo"))
return THUMBNAIL_TYPE_LOGO; return THUMBNAIL_TYPE_LOGO;
return THUMBNAIL_TYPE_BOXART; return THUMBNAIL_TYPE_BOXART;
@ -3925,7 +3925,7 @@ void MainWindow::onContributorsClicked()
dialog->layout()->addWidget(buttonBox); dialog->layout()->addWidget(buttonBox);
textEdit->setReadOnly(true); textEdit->setReadOnly(true);
textEdit->setHtml(QString("<pre>") + retroarch_contributors_list + "</pre>"); textEdit->setHtml(QStringLiteral("<pre>") + retroarch_contributors_list + "</pre>");
dialog->resize(480, 640); dialog->resize(480, 640);
dialog->exec(); dialog->exec();
@ -3935,7 +3935,7 @@ void MainWindow::showAbout()
{ {
QScopedPointer<QDialog> dialog(new QDialog()); QScopedPointer<QDialog> dialog(new QDialog());
QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok); QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok);
QString text = QString("RetroArch ") + PACKAGE_VERSION + QString text = QStringLiteral("RetroArch ") + PACKAGE_VERSION +
"<br><br>" + "<a href=\"https://www.libretro.com/\">www.libretro.com</a>" "<br><br>" + "<a href=\"https://www.libretro.com/\">www.libretro.com</a>"
"<br><br>" + "<a href=\"https://www.retroarch.com/\">www.retroarch.com</a>" "<br><br>" + "<a href=\"https://www.retroarch.com/\">www.retroarch.com</a>"
#ifdef HAVE_GIT_VERSION #ifdef HAVE_GIT_VERSION
@ -4870,9 +4870,9 @@ static void* ui_companion_qt_init(void)
{ {
QString viewType = qsettings->value("view_type", "list").toString(); QString viewType = qsettings->value("view_type", "list").toString();
if (viewType == "list") if (viewType == QLatin1String("list"))
mainwindow->setCurrentViewType(MainWindow::VIEW_TYPE_LIST); mainwindow->setCurrentViewType(MainWindow::VIEW_TYPE_LIST);
else if (viewType == "icons") else if (viewType == QLatin1String("icons"))
mainwindow->setCurrentViewType(MainWindow::VIEW_TYPE_ICONS); mainwindow->setCurrentViewType(MainWindow::VIEW_TYPE_ICONS);
else else
mainwindow->setCurrentViewType(MainWindow::VIEW_TYPE_LIST); mainwindow->setCurrentViewType(MainWindow::VIEW_TYPE_LIST);
@ -4884,13 +4884,13 @@ static void* ui_companion_qt_init(void)
{ {
QString thumbnailType = qsettings->value("icon_view_thumbnail_type", "boxart").toString(); QString thumbnailType = qsettings->value("icon_view_thumbnail_type", "boxart").toString();
if (thumbnailType == "boxart") if (thumbnailType == QLatin1String("boxart"))
mainwindow->setCurrentThumbnailType(THUMBNAIL_TYPE_BOXART); mainwindow->setCurrentThumbnailType(THUMBNAIL_TYPE_BOXART);
else if (thumbnailType == "screenshot") else if (thumbnailType == QLatin1String("screenshot"))
mainwindow->setCurrentThumbnailType(THUMBNAIL_TYPE_SCREENSHOT); mainwindow->setCurrentThumbnailType(THUMBNAIL_TYPE_SCREENSHOT);
else if (thumbnailType == "title") else if (thumbnailType == QLatin1String("title"))
mainwindow->setCurrentThumbnailType(THUMBNAIL_TYPE_TITLE_SCREEN); mainwindow->setCurrentThumbnailType(THUMBNAIL_TYPE_TITLE_SCREEN);
else if (thumbnailType == "logo") else if (thumbnailType == QLatin1String("logo"))
mainwindow->setCurrentThumbnailType(THUMBNAIL_TYPE_LOGO); mainwindow->setCurrentThumbnailType(THUMBNAIL_TYPE_LOGO);
else else
mainwindow->setCurrentThumbnailType(THUMBNAIL_TYPE_BOXART); mainwindow->setCurrentThumbnailType(THUMBNAIL_TYPE_BOXART);
@ -5113,7 +5113,7 @@ void LoadCoreTableWidget::keyPressEvent(QKeyEvent *event)
{ {
int key = event->key(); int key = event->key();
if ( key == Qt::Key_Return if ( key == Qt::Key_Return
|| key == Qt::Key_Enter) || key == Qt::Key_Enter)
{ {
event->accept(); event->accept();
emit enterPressed(); emit enterPressed();