Skip to content

Commit

Permalink
(Qt) Prevent some unneeded string heap allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
LibretroAdmin committed Feb 11, 2025
1 parent 0f5a990 commit 6c7522f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 45 deletions.
30 changes: 16 additions & 14 deletions ui/drivers/qt/gridview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,14 @@ QModelIndex GridView::moveCursor(QAbstractItemView::CursorAction cursorAction, Q
QModelIndex index = currentIndex();
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);
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));
QRect rect = viewportRectForRow(index.row()).toRect();
Expand Down Expand Up @@ -435,24 +437,24 @@ QString GridView::getLayout() const
switch (m_viewMode)
{
case Simple:
return "simple";
return QStringLiteral("simple");
case Anchored:
return "anchored";
return QStringLiteral("anchored");
case Centered:
default:
break;
}

return "centered";
return QStringLiteral("centered");
}

void GridView::setLayout(QString layout)
{
if (layout == "anchored")
if (layout == QLatin1String("anchored"))
m_viewMode = Anchored;
else if (layout == "centered")
else if (layout == QLatin1String("centered"))
m_viewMode = Centered;
else if (layout == "fixed")
else if (layout == QLatin1String("fixed"))
m_viewMode = Simple;
}

Expand Down Expand Up @@ -487,23 +489,23 @@ QString GridItem::getThumbnailVerticalAlign() const
switch (thumbnailVerticalAlignmentFlag)
{
case Qt::AlignTop:
return "top";
return QStringLiteral("top");
case Qt::AlignVCenter:
return "center";
return QStringLiteral("center");
case Qt::AlignBottom:
default:
break;
}

return "bottom";
return QStringLiteral("bottom");
}

void GridItem::setThumbnailVerticalAlign(const QString valign)
{
if (valign == "top")
if (valign == QLatin1String("top"))
thumbnailVerticalAlignmentFlag = Qt::AlignTop;
else if (valign == "center")
else if (valign == QLatin1String("center"))
thumbnailVerticalAlignmentFlag = Qt::AlignVCenter;
else if (valign == "bottom")
else if (valign == QLatin1String("bottom"))
thumbnailVerticalAlignmentFlag = Qt::AlignBottom;
}
9 changes: 5 additions & 4 deletions ui/drivers/qt/qt_downloads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ void MainWindow::onThumbnailPackDownloadNetworkSslErrors(const QList<QSslError>
{
const QSslError &error = errors.at(i);
QString string =
QString("Ignoring SSL error code ")
QStringLiteral("Ignoring SSL error code ")
+ QString::number(error.error())
+ ": "
+ error.errorString();
Expand Down Expand Up @@ -389,7 +389,7 @@ void MainWindow::onThumbnailDownloadNetworkSslErrors(
{
const QSslError &error = errors.at(i);
QString string =
QString("Ignoring SSL error code ")
QStringLiteral("Ignoring SSL error code ")
+ QString::number(error.error())
+ ": "
+ error.errorString();
Expand Down Expand Up @@ -675,7 +675,7 @@ void MainWindow::onPlaylistThumbnailDownloadNetworkSslErrors(const QList<QSslErr
{
const QSslError &error = errors.at(i);
QString string =
QString("Ignoring SSL error code ")
QStringLiteral("Ignoring SSL error code ")
+ QString::number(error.error())
+ ": "
+ error.errorString();
Expand Down Expand Up @@ -923,7 +923,8 @@ void MainWindow::downloadPlaylistThumbnails(QString playlistPath)
QHash<QString, QString> hash2;
QHash<QString, QString> hash3;
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["label_noext"] = itemHash.value("label_noext");
Expand Down
24 changes: 15 additions & 9 deletions ui/drivers/qt/qt_widgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1127,12 +1127,14 @@ void FileDropWidget::paintEvent(QPaintEvent *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();
emit enterPressed();
}
else if (event->key() == Qt::Key_Delete)
else if (key == Qt::Key_Delete)
{
event->accept();
emit deletePressed();
Expand All @@ -1149,7 +1151,8 @@ void FileDropWidget::dragEnterEvent(QDragEnterEvent *event)
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)
{
event->acceptProposedAction();
Expand All @@ -1161,9 +1164,9 @@ void FileDropWidget::dropEvent(QDropEvent *event)

if (data->hasUrls())
{
QList<QUrl> urls = data->urls();
QStringList files;
int i;
QStringList files;
QList<QUrl> urls = data->urls();

for (i = 0; i < urls.count(); i++)
{
Expand Down Expand Up @@ -1196,7 +1199,9 @@ void MainWindow::onFileDropWidgetContextMenuRequested(const QPoint &pos)

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());
}

Expand Down Expand Up @@ -1270,8 +1275,7 @@ void MainWindow::onFileDropWidgetContextMenuRequested(const QPoint &pos)
addFilesToPlaylist(QStringList());
else if (selectedAction == addFolderAction.data())
{
QString dirPath = QFileDialog::getExistingDirectory(
this,
QString dirPath = QFileDialog::getExistingDirectory(this,
msg_hash_to_str(MENU_ENUM_LABEL_VALUE_QT_SELECT_FOLDER),
QString(), QFileDialog::ShowDirsOnly);

Expand Down Expand Up @@ -1312,7 +1316,9 @@ void MainWindow::onFileDropWidgetContextMenuRequested(const QPoint &pos)

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;
}
}
Expand Down
36 changes: 18 additions & 18 deletions ui/drivers/ui_qt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2103,13 +2103,12 @@ void MainWindow::onLaunchWithComboBoxIndexChanged(int)

MainWindow::Theme MainWindow::getThemeFromString(QString themeString)
{
if (themeString == "default")
if (themeString == QLatin1String("default"))
return THEME_SYSTEM_DEFAULT;
else if (themeString == "dark")
else if (themeString == QLatin1String("dark"))
return THEME_DARK;
else if (themeString == "custom")
else if (themeString == QLatin1String("custom"))
return THEME_CUSTOM;

return THEME_SYSTEM_DEFAULT;
}

Expand Down Expand Up @@ -3048,7 +3047,8 @@ void MainWindow::setCoreActions()
{
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;
break;
Expand Down Expand Up @@ -3877,13 +3877,13 @@ QString MainWindow::getCurrentThumbnailTypeString()

ThumbnailType MainWindow::getThumbnailTypeFromString(QString thumbnailType)
{
if (thumbnailType == "boxart")
if (thumbnailType == QLatin1String("boxart"))
return THUMBNAIL_TYPE_BOXART;
else if (thumbnailType == "screenshot")
else if (thumbnailType == QLatin1String("screenshot"))
return THUMBNAIL_TYPE_SCREENSHOT;
else if (thumbnailType == "title")
else if (thumbnailType == QLatin1String("title"))
return THUMBNAIL_TYPE_TITLE_SCREEN;
else if (thumbnailType == "logo")
else if (thumbnailType == QLatin1String("logo"))
return THUMBNAIL_TYPE_LOGO;

return THUMBNAIL_TYPE_BOXART;
Expand Down Expand Up @@ -3925,7 +3925,7 @@ void MainWindow::onContributorsClicked()
dialog->layout()->addWidget(buttonBox);

textEdit->setReadOnly(true);
textEdit->setHtml(QString("<pre>") + retroarch_contributors_list + "</pre>");
textEdit->setHtml(QStringLiteral("<pre>") + retroarch_contributors_list + "</pre>");

dialog->resize(480, 640);
dialog->exec();
Expand All @@ -3935,7 +3935,7 @@ void MainWindow::showAbout()
{
QScopedPointer<QDialog> dialog(new QDialog());
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.retroarch.com/\">www.retroarch.com</a>"
#ifdef HAVE_GIT_VERSION
Expand Down Expand Up @@ -4870,9 +4870,9 @@ static void* ui_companion_qt_init(void)
{
QString viewType = qsettings->value("view_type", "list").toString();

if (viewType == "list")
if (viewType == QLatin1String("list"))
mainwindow->setCurrentViewType(MainWindow::VIEW_TYPE_LIST);
else if (viewType == "icons")
else if (viewType == QLatin1String("icons"))
mainwindow->setCurrentViewType(MainWindow::VIEW_TYPE_ICONS);
else
mainwindow->setCurrentViewType(MainWindow::VIEW_TYPE_LIST);
Expand All @@ -4884,13 +4884,13 @@ static void* ui_companion_qt_init(void)
{
QString thumbnailType = qsettings->value("icon_view_thumbnail_type", "boxart").toString();

if (thumbnailType == "boxart")
if (thumbnailType == QLatin1String("boxart"))
mainwindow->setCurrentThumbnailType(THUMBNAIL_TYPE_BOXART);
else if (thumbnailType == "screenshot")
else if (thumbnailType == QLatin1String("screenshot"))
mainwindow->setCurrentThumbnailType(THUMBNAIL_TYPE_SCREENSHOT);
else if (thumbnailType == "title")
else if (thumbnailType == QLatin1String("title"))
mainwindow->setCurrentThumbnailType(THUMBNAIL_TYPE_TITLE_SCREEN);
else if (thumbnailType == "logo")
else if (thumbnailType == QLatin1String("logo"))
mainwindow->setCurrentThumbnailType(THUMBNAIL_TYPE_LOGO);
else
mainwindow->setCurrentThumbnailType(THUMBNAIL_TYPE_BOXART);
Expand Down Expand Up @@ -5113,7 +5113,7 @@ void LoadCoreTableWidget::keyPressEvent(QKeyEvent *event)
{
int key = event->key();
if ( key == Qt::Key_Return
|| key == Qt::Key_Enter)
|| key == Qt::Key_Enter)
{
event->accept();
emit enterPressed();
Expand Down

0 comments on commit 6c7522f

Please sign in to comment.