CorePlugin: Remove foreach / Q_FOREACH usage mostly in dialogs

Task-number: QTCREATORBUG-27464
Change-Id: Iad47e9cb0b2fa7590dba2d26d0a33a83d2909bb7
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
Artem Sokolovskii
2022-05-02 17:25:11 +02:00
parent 33a385c4d6
commit f4e943bda3
15 changed files with 73 additions and 57 deletions

View File

@@ -160,8 +160,8 @@ void DesignMode::currentEditorChanged(IEditor *editor)
if (editor) {
const QString mimeType = editor->document()->mimeType();
if (!mimeType.isEmpty()) {
foreach (DesignEditorInfo *editorInfo, d->m_editors) {
foreach (const QString &mime, editorInfo->mimeTypes) {
for (const DesignEditorInfo *editorInfo : qAsConst(d->m_editors)) {
for (const QString &mime : editorInfo->mimeTypes) {
if (mime == mimeType) {
d->m_stackWidget->setCurrentIndex(editorInfo->widgetIndex);
setActiveContext(editorInfo->context);
@@ -169,10 +169,10 @@ void DesignMode::currentEditorChanged(IEditor *editor)
setEnabled(true);
break;
}
} // foreach mime
}
if (mimeEditorAvailable)
break;
} // foreach editorInfo
}
}
}
if (d->m_currentEditor)

View File

@@ -673,7 +673,8 @@ static QString findUnusedId(const QString &proposal, const QMap<QString, QList<E
++number;
found = false;
for (auto it = tools.cbegin(), end = tools.cend(); it != end; ++it) {
foreach (ExternalTool *tool, it.value()) {
const QList<ExternalTool *> tools = it.value();
for (const ExternalTool *tool : tools) {
if (tool->id() == result) {
found = true;
break;
@@ -695,7 +696,8 @@ void ExternalToolConfig::apply()
QMap<QString, QList<ExternalTool *> > resultMap;
for (auto it = newToolsMap.cbegin(), end = newToolsMap.cend(); it != end; ++it) {
QList<ExternalTool *> items;
foreach (ExternalTool *tool, it.value()) {
const QList<ExternalTool *> tools = it.value();
for (ExternalTool *tool : tools) {
ExternalTool *toolToAdd = nullptr;
if (ExternalTool *originalTool = originalTools.take(tool->id())) {
// check if it has different category and is custom tool
@@ -752,7 +754,7 @@ void ExternalToolConfig::apply()
resultMap.insert(it.key(), items);
}
// Remove tools that have been deleted from the settings (and are no preset)
foreach (ExternalTool *tool, originalTools) {
for (const ExternalTool *tool : qAsConst(originalTools)) {
QTC_ASSERT(!tool->preset(), continue);
// TODO error handling
tool->fileName().removeFile();

View File

@@ -270,18 +270,18 @@ bool IOptionsPage::matches(const QRegularExpression &regexp) const
if (!widget)
return false;
// find common subwidgets
foreach (const QLabel *label, widget->findChildren<QLabel *>())
for (const QLabel *label : widget->findChildren<QLabel *>())
m_keywords << Utils::stripAccelerator(label->text());
foreach (const QCheckBox *checkbox, widget->findChildren<QCheckBox *>())
for (const QCheckBox *checkbox : widget->findChildren<QCheckBox *>())
m_keywords << Utils::stripAccelerator(checkbox->text());
foreach (const QPushButton *pushButton, widget->findChildren<QPushButton *>())
for (const QPushButton *pushButton : widget->findChildren<QPushButton *>())
m_keywords << Utils::stripAccelerator(pushButton->text());
foreach (const QGroupBox *groupBox, widget->findChildren<QGroupBox *>())
for (const QGroupBox *groupBox : widget->findChildren<QGroupBox *>())
m_keywords << Utils::stripAccelerator(groupBox->title());
m_keywordsInitialized = true;
}
foreach (const QString &keyword, m_keywords)
for (const QString &keyword : qAsConst(m_keywords))
if (keyword.contains(regexp))
return true;
return false;

View File

@@ -401,7 +401,8 @@ void NewDialogWidget::currentItemChanged(const QModelIndex &index)
if (const IWizardFactory *wizard = factoryOfItem(cat)) {
QString desciption = wizard->description();
QStringList displayNamesForSupportedPlatforms;
foreach (Id platform, wizard->supportedPlatforms())
const QSet<Id> platforms = wizard->supportedPlatforms();
for (const Id platform : platforms)
displayNamesForSupportedPlatforms << IWizardFactory::displayNameForPlatform(platform);
Utils::sort(displayNamesForSupportedPlatforms);
if (!Qt::mightBeRichText(desciption))

View File

@@ -58,7 +58,7 @@ void OpenWithDialog::setOkButtonEnabled(bool v)
void OpenWithDialog::setEditors(const QStringList &editors)
{
foreach (const QString &e, editors)
for (const QString &e : editors)
editorListWidget->addItem(e);
}

View File

@@ -82,7 +82,7 @@ void PromptOverwriteDialog::setFiles(const QStringList &l)
{
// Format checkable list excluding common path
const QString nativeCommonPath = QDir::toNativeSeparators(Utils::commonPath(l));
foreach (const QString &fileName, l) {
for (const QString &fileName : l) {
const QString nativeFileName = QDir::toNativeSeparators(fileName);
const int length = nativeFileName.size() - nativeCommonPath.size() - 1;
QStandardItem *item = new QStandardItem(nativeFileName.right(length));

View File

@@ -124,7 +124,7 @@ ReadOnlyFilesDialogPrivate::ReadOnlyFilesDialogPrivate(ReadOnlyFilesDialog *pare
ReadOnlyFilesDialogPrivate::~ReadOnlyFilesDialogPrivate()
{
foreach (const ButtonGroupForFile &groupForFile, buttonGroups)
for (const ButtonGroupForFile &groupForFile : qAsConst(buttonGroups))
delete groupForFile.group;
}
@@ -371,8 +371,9 @@ void ReadOnlyFilesDialogPrivate::setAll(int index)
type = SaveAs;
// Check for every file if the selected operation is available and change it to the operation.
foreach (ReadOnlyFilesDialogPrivate::ButtonGroupForFile groupForFile, buttonGroups) {
auto radioButton = qobject_cast<QRadioButton*> (groupForFile.group->button(type));
for (const ReadOnlyFilesDialogPrivate::ButtonGroupForFile &groupForFile :
qAsConst(buttonGroups)) {
auto radioButton = qobject_cast<QRadioButton *>(groupForFile.group->button(type));
if (radioButton)
radioButton->setChecked(true);
}
@@ -386,7 +387,8 @@ void ReadOnlyFilesDialogPrivate::setAll(int index)
void ReadOnlyFilesDialogPrivate::updateSelectAll()
{
int selectedOperation = -1;
foreach (ReadOnlyFilesDialogPrivate::ButtonGroupForFile groupForFile, buttonGroups) {
for (const ReadOnlyFilesDialogPrivate::ButtonGroupForFile &groupForFile :
qAsConst(buttonGroups)) {
if (selectedOperation == -1) {
selectedOperation = groupForFile.group->checkedId();
} else if (selectedOperation != groupForFile.group->checkedId()) {

View File

@@ -65,7 +65,7 @@ SaveItemsDialog::SaveItemsDialog(QWidget *parent,
m_ui.saveBeforeBuildCheckBox->setVisible(false);
foreach (IDocument *document, items) {
for (IDocument *document : qAsConst(items)) {
QString visibleName;
QString directory;
Utils::FilePath filePath = document->filePath();
@@ -135,7 +135,7 @@ void SaveItemsDialog::adjustButtonWidths()
possibleTexts << tr("Save Selected");
int maxTextWidth = 0;
QPushButton *saveButton = m_ui.buttonBox->button(QDialogButtonBox::Save);
foreach (const QString &text, possibleTexts) {
for (const QString &text : qAsConst(possibleTexts)) {
saveButton->setText(text);
int hint = saveButton->sizeHint().width();
if (hint > maxTextWidth)
@@ -154,7 +154,8 @@ void SaveItemsDialog::adjustButtonWidths()
void SaveItemsDialog::collectItemsToSave()
{
m_itemsToSave.clear();
foreach (QTreeWidgetItem *item, m_ui.treeWidget->selectedItems()) {
const QList<QTreeWidgetItem *> items = m_ui.treeWidget->selectedItems();
for (const QTreeWidgetItem *item : items) {
m_itemsToSave.append(item->data(0, Qt::UserRole).value<IDocument*>());
}
accept();
@@ -163,7 +164,8 @@ void SaveItemsDialog::collectItemsToSave()
void SaveItemsDialog::collectFilesToDiff()
{
m_filesToDiff.clear();
foreach (QTreeWidgetItem *item, m_ui.treeWidget->selectedItems()) {
const QList<QTreeWidgetItem *> items = m_ui.treeWidget->selectedItems();
for (const QTreeWidgetItem *item : items) {
if (auto doc = item->data(0, Qt::UserRole).value<IDocument*>())
m_filesToDiff.append(doc->filePath().toString());
}

View File

@@ -170,7 +170,7 @@ void CategoryModel::setPages(const QList<IOptionsPage*> &pages,
m_pageIds.clear();
// Put the pages in categories
foreach (IOptionsPage *page, pages) {
for (IOptionsPage *page : pages) {
QTC_ASSERT(!m_pageIds.contains(page->id()),
qWarning("duplicate options page id '%s'", qPrintable(page->id().toString())));
m_pageIds.insert(page->id());
@@ -190,7 +190,7 @@ void CategoryModel::setPages(const QList<IOptionsPage*> &pages,
category->pages.append(page);
}
foreach (IOptionsPageProvider *provider, providers) {
for (IOptionsPageProvider *provider : providers) {
const Id categoryId = provider->category();
Category *category = findCategoryById(categoryId);
if (!category) {
@@ -217,11 +217,11 @@ void CategoryModel::ensurePages(Category *category)
{
if (!category->providerPagesCreated) {
QList<IOptionsPage *> createdPages;
foreach (const IOptionsPageProvider *provider, category->providers)
for (const IOptionsPageProvider *provider : qAsConst(category->providers))
createdPages += provider->pages();
// check for duplicate ids
foreach (IOptionsPage *page, createdPages) {
for (const IOptionsPage *page : qAsConst(createdPages)) {
QTC_ASSERT(!m_pageIds.contains(page->id()),
qWarning("duplicate options page id '%s'", qPrintable(page->id().toString())));
}
@@ -702,9 +702,9 @@ void SettingsDialog::accept()
m_finished = true;
disconnectTabWidgets();
m_applied = true;
foreach (IOptionsPage *page, m_visitedPages)
for (IOptionsPage *page : qAsConst(m_visitedPages))
page->apply();
foreach (IOptionsPage *page, m_pages)
for (IOptionsPage *page : qAsConst(m_pages))
page->finish();
done(QDialog::Accepted);
}
@@ -715,14 +715,14 @@ void SettingsDialog::reject()
return;
m_finished = true;
disconnectTabWidgets();
foreach (IOptionsPage *page, m_pages)
for (IOptionsPage *page : qAsConst(m_pages))
page->finish();
done(QDialog::Rejected);
}
void SettingsDialog::apply()
{
foreach (IOptionsPage *page, m_visitedPages)
for (IOptionsPage *page : qAsConst(m_visitedPages))
page->apply();
m_applied = true;
}

View File

@@ -332,7 +332,7 @@ QWidget *ShortcutSettings::widget()
void ShortcutSettingsWidget::apply()
{
foreach (ShortcutItem *item, m_scitems)
for (const ShortcutItem *item : qAsConst(m_scitems))
item->m_cmd->setKeySequences(item->m_keys);
}
@@ -520,7 +520,7 @@ void ShortcutSettingsWidget::importAction()
void ShortcutSettingsWidget::defaultAction()
{
foreach (ShortcutItem *item, m_scitems) {
for (ShortcutItem *item : qAsConst(m_scitems)) {
item->m_keys = item->m_cmd->defaultKeySequences();
item->m_item->setText(2, keySequencesToNativeString(item->m_keys));
setModified(item->m_item, false);
@@ -557,7 +557,8 @@ void ShortcutSettingsWidget::initialize()
clear();
QMap<QString, QTreeWidgetItem *> sections;
foreach (Command *c, ActionManager::commands()) {
const QList<Command *> commands = ActionManager::commands();
for (Command *c : commands) {
if (c->hasAttribute(Command::CA_NonConfigurable))
continue;
if (c->action() && c->action()->isSeparator())

View File

@@ -370,7 +370,7 @@ void DocumentManager::addDocuments(const QList<IDocument *> &documents, bool add
if (!addWatcher) {
// We keep those in a separate list
foreach (IDocument *document, documents) {
for (IDocument *document : documents) {
if (document && !d->m_documentsWithoutWatch.contains(document)) {
connect(document, &QObject::destroyed,
m_instance, &DocumentManager::documentDestroyed);
@@ -406,7 +406,8 @@ static void removeFileInfo(IDocument *document)
QTC_ASSERT(isMainThread(), return);
if (!d->m_documentsWithWatch.contains(document))
return;
foreach (const FilePath &filePath, d->m_documentsWithWatch.value(document)) {
const FilePaths filePaths = d->m_documentsWithWatch.value(document);
for (const FilePath &filePath : filePaths) {
if (!d->m_states.contains(filePath))
continue;
qCDebug(log) << "removing document (" << filePath << ")";
@@ -599,7 +600,7 @@ QList<IDocument *> DocumentManager::modifiedDocuments()
modified << document;
}
foreach (IDocument *document, d->m_documentsWithoutWatch) {
for (IDocument *document : qAsConst(d->m_documentsWithoutWatch)) {
if (document->isModified())
modified << document;
}
@@ -665,7 +666,7 @@ static bool saveModifiedFilesHelper(const QList<IDocument *> &documents,
QHash<IDocument *, QString> modifiedDocumentsMap;
QList<IDocument *> modifiedDocuments;
foreach (IDocument *document, documents) {
for (IDocument *document : documents) {
if (document && document->isModified() && !document->isTemporary()) {
QString name = document->filePath().toString();
if (name.isEmpty())
@@ -709,7 +710,7 @@ static bool saveModifiedFilesHelper(const QList<IDocument *> &documents,
}
// Check for files without write permissions.
QList<IDocument *> roDocuments;
foreach (IDocument *document, documentsToSave) {
for (IDocument *document : qAsConst(documentsToSave)) {
if (document->isFileReadOnly())
roDocuments << document;
}
@@ -726,7 +727,7 @@ static bool saveModifiedFilesHelper(const QList<IDocument *> &documents,
return false;
}
}
foreach (IDocument *document, documentsToSave) {
for (IDocument *document : qAsConst(documentsToSave)) {
if (!EditorManagerPrivate::saveDocument(document)) {
if (cancelled)
*cancelled = true;
@@ -1114,7 +1115,7 @@ void DocumentManager::checkForReload()
QMap<FilePath, FileStateItem> currentStates;
QMap<FilePath, IDocument::ChangeType> changeTypes;
QSet<IDocument *> changedIDocuments;
foreach (const FilePath &filePath, d->m_changedFiles) {
for (const FilePath &filePath : qAsConst(d->m_changedFiles)) {
const FilePath fileKey = filePathKey(filePath, KeepLinks);
qCDebug(log) << "handling file change for" << filePath << "(" << fileKey << ")";
IDocument::ChangeType type = IDocument::TypeContents;
@@ -1130,7 +1131,8 @@ void DocumentManager::checkForReload()
}
currentStates.insert(fileKey, state);
changeTypes.insert(fileKey, type);
foreach (IDocument *document, d->m_states.value(fileKey).lastUpdatedState.keys())
QList<IDocument *> documents = d->m_states.value(fileKey).lastUpdatedState.keys();
for (IDocument *document : documents)
changedIDocuments.insert(document);
}
@@ -1144,7 +1146,7 @@ void DocumentManager::checkForReload()
// if the resolved names are different when unexpectFileChange is called
// we would end up with never-unexpected file names
QSet<FilePath> expectedFileKeys;
foreach (const FilePath &filePath, d->m_expectedFileNames) {
for (const FilePath &filePath : qAsConst(d->m_expectedFileNames)) {
const FilePath cleanAbsFilePath = filePathKey(filePath, KeepLinks);
expectedFileKeys.insert(filePathKey(filePath, KeepLinks));
const FilePath resolvedCleanAbsFilePath = cleanAbsFilePath.canonicalPath();
@@ -1155,14 +1157,15 @@ void DocumentManager::checkForReload()
// handle the IDocuments
QStringList errorStrings;
QStringList filesToDiff;
foreach (IDocument *document, changedIDocuments) {
for (IDocument *document : qAsConst(changedIDocuments)) {
IDocument::ChangeTrigger trigger = IDocument::TriggerInternal;
optional<IDocument::ChangeType> type;
bool changed = false;
// find out the type & behavior from the two possible files
// behavior is internal if all changes are expected (and none removed)
// type is "max" of both types (remove > contents > permissions)
foreach (const FilePath &fileKey, d->m_documentsWithWatch.value(document)) {
const FilePaths files = d->m_documentsWithWatch.value(document);
for (const FilePath &fileKey : files) {
// was the file reported?
if (!currentStates.contains(fileKey))
continue;
@@ -1379,7 +1382,7 @@ void DocumentManager::saveSettings()
{
QVariantList recentFiles;
QStringList recentEditorIds;
foreach (const RecentFile &file, d->m_recentFiles) {
for (const RecentFile &file : qAsConst(d->m_recentFiles)) {
recentFiles.append(file.first.toVariant());
recentEditorIds.append(file.second.toString());
}

View File

@@ -90,7 +90,7 @@ SideBarWidget::SideBarWidget(SideBar *sideBar, const QString &id)
Utils::sort(titleList);
QString t = id;
if (!titleList.isEmpty()) {
foreach (const QString &itemTitle, titleList)
for (const QString &itemTitle : qAsConst(titleList))
m_comboBox->addItem(itemTitle, m_sideBar->idForTitle(itemTitle));
m_comboBox->setCurrentIndex(0);
@@ -139,7 +139,8 @@ void SideBarWidget::setCurrentItem(const QString &id)
m_currentItem->widget()->show();
// Add buttons and remember their actions for later removal
foreach (QToolButton *b, m_currentItem->createToolBarWidgets())
QList<QToolButton *> buttons = m_currentItem->createToolBarWidgets();
for (QToolButton *b : buttons)
m_addedToolBarActions.append(m_toolbar->insertWidget(m_splitAction, b));
}
@@ -153,7 +154,7 @@ void SideBarWidget::updateAvailableItems()
titleList.append(currentTitle);
Utils::sort(titleList);
foreach (const QString &itemTitle, titleList)
for (const QString &itemTitle : qAsConst(titleList))
m_comboBox->addItem(itemTitle, m_sideBar->idForTitle(itemTitle));
int idx = m_comboBox->findText(currentTitle);

View File

@@ -164,7 +164,8 @@ void StatusBarManager::restoreSettings()
leftSplitWidth = m_splitter->widget(0)->sizeHint().width();
}
int sum = 0;
foreach (int w, m_splitter->sizes())
const QList<int> sizes = m_splitter->sizes();
for (const int w : sizes)
sum += w;
m_splitter->setSizes(QList<int>() << leftSplitWidth << (sum - leftSplitWidth));
}

View File

@@ -201,7 +201,7 @@ static void addThemesFromPath(const QString &path, QList<ThemeEntry> *themes)
themeDir.setNameFilters({extension});
themeDir.setFilter(QDir::Files);
const QStringList themeList = themeDir.entryList();
foreach (const QString &fileName, themeList) {
for (const QString &fileName : qAsConst(themeList)) {
QString id = QFileInfo(fileName).completeBaseName();
themes->append(ThemeEntry(Id::fromString(id), themeDir.absoluteFilePath(fileName)));
}

View File

@@ -101,7 +101,8 @@ public:
QTC_ASSERT(QDir::fromNativeSeparators(dir) == dir, return);
const QString dirSlash = dir + QLatin1Char('/');
foreach (const QString &key, m_cachedMatches.keys()) {
const QList<QString> keys = m_cachedMatches.keys();
for (const QString &key : keys) {
if (key == dir || key.startsWith(dirSlash))
m_cachedMatches.remove(key);
}
@@ -172,7 +173,8 @@ VcsManager *VcsManager::instance()
void VcsManager::extensionsInitialized()
{
// Change signal connections
foreach (IVersionControl *versionControl, versionControls()) {
const QList<IVersionControl *> versionControlList = versionControls();
for (const IVersionControl *versionControl : versionControlList) {
connect(versionControl, &IVersionControl::filesChanged, DocumentManager::instance(),
[](const QStringList fileNames) {
DocumentManager::notifyFilesChangedInternally(
@@ -240,7 +242,8 @@ IVersionControl* VcsManager::findVersionControlForDirectory(const FilePath &inpu
// Nothing: ask the IVersionControls directly.
StringVersionControlPairs allThatCanManage;
foreach (IVersionControl * versionControl, versionControls()) {
const QList<IVersionControl *> versionControlList = versionControls();
for (IVersionControl *versionControl : versionControlList) {
FilePath topLevel;
if (versionControl->managesDirectory(FilePath::fromString(directory), &topLevel))
allThatCanManage.push_back(StringVersionControlPair(topLevel.toString(), versionControl));
@@ -465,9 +468,9 @@ void VcsManager::emitRepositoryChanged(const FilePath &repository)
void VcsManager::clearVersionControlCache()
{
QStringList repoList = d->m_cachedMatches.keys();
const QStringList repoList = d->m_cachedMatches.keys();
d->clearCache();
foreach (const QString &repo, repoList)
for (const QString &repo : repoList)
emit m_instance->repositoryChanged(FilePath::fromString(repo));
}
@@ -589,7 +592,7 @@ void CorePlugin::testVcsManager()
// From VCSes:
int expectedCount = 0;
foreach (const QString &result, results) {
for (const QString &result : qAsConst(results)) {
// qDebug() << "Expecting:" << result;
QStringList split = result.split(QLatin1Char(':'));