mirror of
https://github.com/KDE/konsole.git
synced 2025-12-23 23:38:08 -05:00
Avoid detaching Qt containers in range-based for loops
Using temporaries or non-const containers directly in range-based loops can cause containers to detach, leading to unnecessary copies. This commit ensures all range-based loops iterate over named, const-qualified containers to prevent detachment and improve performance.
This commit is contained in:
committed by
Christoph Cullmann
parent
e68789ba0c
commit
0ce309e66b
@@ -91,7 +91,8 @@ void KeyBindingEditor::filterRows(const QString &text)
|
||||
|
||||
QList<int> matchedRows;
|
||||
|
||||
for (QTableWidgetItem *matchedItem : _ui->keyBindingTable->findItems(text, Qt::MatchContains)) {
|
||||
const auto items = _ui->keyBindingTable->findItems(text, Qt::MatchContains);
|
||||
for (QTableWidgetItem *matchedItem : items) {
|
||||
matchedRows.append(matchedItem->row());
|
||||
}
|
||||
|
||||
|
||||
@@ -878,7 +878,8 @@ void MainWindow::showSettingsDialog(const bool showProfilePage)
|
||||
|
||||
if (confDialog != nullptr) {
|
||||
if (showProfilePage) {
|
||||
for (auto page : confDialog->findChildren<KPageWidgetItem *>()) {
|
||||
const auto items = confDialog->findChildren<KPageWidgetItem *>();
|
||||
for (const auto page : items) {
|
||||
if (page->name().contains(profilePageName)) {
|
||||
confDialog->setCurrentPage(page);
|
||||
break;
|
||||
|
||||
@@ -203,7 +203,8 @@ QString Part::currentProfileName() const
|
||||
bool Part::setCurrentProfile(const QString &profileName)
|
||||
{
|
||||
Profile::Ptr profile;
|
||||
for (auto p : ProfileManager::instance()->allProfiles()) {
|
||||
const auto profiles = ProfileManager::instance()->allProfiles();
|
||||
for (const auto& p : profiles) {
|
||||
if (p->name() == profileName) {
|
||||
profile = p;
|
||||
break;
|
||||
|
||||
@@ -692,8 +692,8 @@ void ViewManager::focusAnotherTerminal(ViewSplitter *toplevelSplitter)
|
||||
|
||||
if (tabTterminalDisplays.count() > 1) {
|
||||
// Give focus to the last used terminal in this tab
|
||||
for (auto *historyItem : _terminalDisplayHistory) {
|
||||
for (auto *terminalDisplay : tabTterminalDisplays) {
|
||||
for (const auto *historyItem : std::as_const(_terminalDisplayHistory)) {
|
||||
for (auto *terminalDisplay : std::as_const(tabTterminalDisplays)) {
|
||||
if (terminalDisplay == historyItem) {
|
||||
terminalDisplay->setFocus(Qt::OtherFocusReason);
|
||||
return;
|
||||
@@ -1126,10 +1126,10 @@ QList<ViewProperties *> ViewManager::viewProperties() const
|
||||
return {};
|
||||
}
|
||||
|
||||
auto terminalContainers = _viewContainer->findChildren<TerminalDisplay *>();
|
||||
const auto terminalContainers = _viewContainer->findChildren<TerminalDisplay *>();
|
||||
list.reserve(terminalContainers.size());
|
||||
|
||||
for (auto terminalDisplay : _viewContainer->findChildren<TerminalDisplay *>()) {
|
||||
for (auto terminalDisplay : terminalContainers) {
|
||||
list.append(terminalDisplay->sessionController());
|
||||
}
|
||||
|
||||
@@ -1371,7 +1371,7 @@ QStringList ViewManager::sessionList()
|
||||
QStringList ids;
|
||||
|
||||
for (int i = 0; i < _viewContainer->count(); i++) {
|
||||
auto terminaldisplayList = _viewContainer->widget(i)->findChildren<TerminalDisplay *>();
|
||||
const auto terminaldisplayList = _viewContainer->widget(i)->findChildren<TerminalDisplay *>();
|
||||
for (auto *terminaldisplay : terminaldisplayList) {
|
||||
ids.append(QString::number(terminaldisplay->sessionController()->session()->sessionId()));
|
||||
}
|
||||
@@ -1499,21 +1499,22 @@ QStringList ViewManager::viewHierarchy()
|
||||
|
||||
QList<double> ViewManager::getSplitProportions(int splitterId)
|
||||
{
|
||||
auto splitter = _viewContainer->findSplitter(splitterId);
|
||||
const auto *splitter = _viewContainer->findSplitter(splitterId);
|
||||
if (splitter == nullptr)
|
||||
return QList<double>();
|
||||
|
||||
const QList<int> sizes = splitter->sizes();
|
||||
int totalSize = 0;
|
||||
QList<double> percentages;
|
||||
|
||||
for (auto size : splitter->sizes()) {
|
||||
for (const auto& size : sizes) {
|
||||
totalSize += size;
|
||||
}
|
||||
|
||||
QList<double> percentages;
|
||||
if (totalSize == 0)
|
||||
return QList<double>();
|
||||
return percentages;
|
||||
|
||||
for (auto size : splitter->sizes()) {
|
||||
for (auto size : sizes) {
|
||||
percentages.append((size / static_cast<double>(totalSize)) * 100);
|
||||
}
|
||||
|
||||
@@ -1599,7 +1600,7 @@ bool ViewManager::createSplitWithExisting(int targetSplitterId, QStringList widg
|
||||
ViewSplitter *createdSplitter = new ViewSplitter();
|
||||
createdSplitter->setOrientation(horizontalSplit ? Qt::Horizontal : Qt::Vertical);
|
||||
|
||||
for (auto widget : linearLayout) {
|
||||
for (auto widget : std::as_const(linearLayout)) {
|
||||
if (auto s = qobject_cast<ViewSplitter *>(widget))
|
||||
createdSplitter->addSplitter(s);
|
||||
else
|
||||
@@ -1644,7 +1645,8 @@ bool ViewManager::resizeSplits(int splitterId, QList<double> percentages)
|
||||
int sum = 0;
|
||||
QList<int> newSizes;
|
||||
|
||||
for (int size : splitter->sizes()) {
|
||||
const auto sizes = splitter->sizes();
|
||||
for (int size : sizes) {
|
||||
sum += size;
|
||||
}
|
||||
|
||||
|
||||
@@ -1275,7 +1275,7 @@ void Vt102Emulation::processSessionAttributeRequest(const int tokenSize, const u
|
||||
int closeSignal = -1;
|
||||
QString applicationName;
|
||||
|
||||
for (QString param: params) {
|
||||
for (QString param: std::as_const(params)) {
|
||||
if (param.startsWith(QString::fromLatin1("i="))) {
|
||||
// Notification ID:
|
||||
QString payloadValue = param.mid(2);
|
||||
@@ -1303,7 +1303,7 @@ void Vt102Emulation::processSessionAttributeRequest(const int tokenSize, const u
|
||||
// Parse a comma-separated of actions with optional "-" (eg. "-focus,report"):
|
||||
QString payloadValue = param.mid(2);
|
||||
QStringList tokens = payloadValue.split(u',');
|
||||
for (QString token : tokens) {
|
||||
for (QString token : std::as_const(tokens)) {
|
||||
int tokenValue = NotificationActionNone;
|
||||
bool removed = false;
|
||||
if (token.startsWith(QString::fromLatin1("-"))) {
|
||||
@@ -1598,7 +1598,7 @@ void Vt102Emulation::processSessionAttributeRequest(const int tokenSize, const u
|
||||
int scaledWidth = 0;
|
||||
int scaledHeight = 0;
|
||||
bool moveCursor = true;
|
||||
for (const auto &p : params) {
|
||||
for (const auto &p : std::as_const(params)) {
|
||||
int eq = p.indexOf(QLatin1Char('='));
|
||||
if (eq > 0) {
|
||||
QString var = p.mid(0, eq);
|
||||
@@ -2666,7 +2666,7 @@ void Vt102Emulation::emulateUpDown(int up, KeyboardTranslator::Entry entry, QByt
|
||||
lineProperties.removeLast();
|
||||
}
|
||||
int num = up > 0 ? cuX - targetCol : targetCol - cuX;
|
||||
for (LineProperty i : lineProperties) {
|
||||
for (LineProperty i : std::as_const(lineProperties)) {
|
||||
num += i.length + 1 - i.flags.f.wrapped;
|
||||
}
|
||||
for (int i = 0; i < num; i++) {
|
||||
|
||||
@@ -38,7 +38,8 @@ QList<std::shared_ptr<const ColorScheme>> ColorSchemeManager::allColorSchemes()
|
||||
int failed = 0;
|
||||
|
||||
QList<std::shared_ptr<const ColorScheme>> ret;
|
||||
for (const QString &name : listColorSchemes()) {
|
||||
const auto schemes = listColorSchemes();
|
||||
for (const QString &name : schemes) {
|
||||
std::shared_ptr<const ColorScheme> scheme = findColorScheme(colorSchemeNameFromPath(name));
|
||||
if (!scheme) {
|
||||
failed++;
|
||||
|
||||
@@ -45,21 +45,21 @@ void FilterChain::removeFilter(Filter *filter)
|
||||
|
||||
void FilterChain::reset()
|
||||
{
|
||||
for (auto *filter : _filters) {
|
||||
for (auto *filter : std::as_const(_filters)) {
|
||||
filter->reset();
|
||||
}
|
||||
}
|
||||
|
||||
void FilterChain::setBuffer(const QString *buffer, const QList<int> *linePositions)
|
||||
{
|
||||
for (auto *filter : _filters) {
|
||||
for (auto *filter : std::as_const(_filters)) {
|
||||
filter->setBuffer(buffer, linePositions);
|
||||
}
|
||||
}
|
||||
|
||||
void FilterChain::process()
|
||||
{
|
||||
for (auto *filter : _filters) {
|
||||
for (auto *filter : std::as_const(_filters)) {
|
||||
filter->process();
|
||||
}
|
||||
}
|
||||
@@ -92,7 +92,8 @@ QList<QSharedPointer<HotSpot>> FilterChain::hotSpots() const
|
||||
QRegion FilterChain::hotSpotRegion() const
|
||||
{
|
||||
QRegion region;
|
||||
for (const auto &hotSpot : hotSpots()) {
|
||||
const auto _hotSpots = hotSpots();
|
||||
for (const auto &hotSpot : _hotSpots) {
|
||||
QRect r;
|
||||
r.setLeft(hotSpot->startColumn());
|
||||
r.setTop(hotSpot->startLine());
|
||||
@@ -131,7 +132,8 @@ int FilterChain::count(HotSpot::Type type) const
|
||||
QList<QSharedPointer<HotSpot>> FilterChain::filterBy(HotSpot::Type type) const
|
||||
{
|
||||
QList<QSharedPointer<HotSpot>> hotspots;
|
||||
for (const auto &spot : hotSpots()) {
|
||||
const auto _hotSpots = hotSpots();
|
||||
for (const auto &spot : _hotSpots) {
|
||||
if (spot->type() == type) {
|
||||
hotspots.append(spot);
|
||||
}
|
||||
|
||||
@@ -22,10 +22,12 @@ QuickCommandsModel::~QuickCommandsModel() noexcept
|
||||
void QuickCommandsModel::load()
|
||||
{
|
||||
auto config = KConfig(QStringLiteral("konsolequickcommandsconfig"), KConfig::OpenFlag::SimpleConfig);
|
||||
for (const QString &groupName : config.groupList()) {
|
||||
const auto groupList = config.groupList();
|
||||
for (const QString &groupName : groupList) {
|
||||
KConfigGroup group = config.group(groupName);
|
||||
addTopLevelItem(groupName);
|
||||
for (const QString &commandGroup : group.groupList()) {
|
||||
const auto groupGroupList = group.groupList();
|
||||
for (const QString &commandGroup : groupGroupList) {
|
||||
QuickCommandData data;
|
||||
KConfigGroup element = group.group(commandGroup);
|
||||
data.name = element.readEntry("name");
|
||||
@@ -39,7 +41,8 @@ void QuickCommandsModel::load()
|
||||
void QuickCommandsModel::save()
|
||||
{
|
||||
auto config = KConfig(QStringLiteral("konsolequickcommandsconfig"), KConfig::OpenFlag::SimpleConfig);
|
||||
for (const QString &groupName : config.groupList()) {
|
||||
const auto groupList = config.groupList();
|
||||
for (const QString &groupName : groupList) {
|
||||
config.deleteGroup(groupName);
|
||||
}
|
||||
for (int i = 0, end = invisibleRootItem()->rowCount(); i < end; i++) {
|
||||
|
||||
@@ -249,14 +249,17 @@ void SSHManagerModel::triggerProfileChange(const QString &sshHost)
|
||||
void SSHManagerModel::load()
|
||||
{
|
||||
auto config = KConfig(QStringLiteral("konsolesshconfig"), KConfig::OpenFlag::SimpleConfig);
|
||||
for (const QString &groupName : config.groupList()) {
|
||||
|
||||
const auto groupList = config.groupList();
|
||||
for (const QString &groupName : groupList) {
|
||||
KConfigGroup group = config.group(groupName);
|
||||
if (groupName == QStringLiteral("Global plugin config")) {
|
||||
manageProfile = group.readEntry<bool>("manageProfile", false);
|
||||
continue;
|
||||
}
|
||||
addTopLevelItem(groupName);
|
||||
for (const QString &sessionName : group.groupList()) {
|
||||
const auto groupGroupList = group.groupList();
|
||||
for (const QString &sessionName : groupGroupList) {
|
||||
SSHConfigurationData data;
|
||||
KConfigGroup sessionGroup = group.group(sessionName);
|
||||
data.host = sessionGroup.readEntry("hostname");
|
||||
@@ -275,7 +278,8 @@ void SSHManagerModel::load()
|
||||
void SSHManagerModel::save()
|
||||
{
|
||||
auto config = KConfig(QStringLiteral("konsolesshconfig"), KConfig::OpenFlag::SimpleConfig);
|
||||
for (const QString &groupName : config.groupList()) {
|
||||
const auto groupList = config.groupList();
|
||||
for (const QString &groupName : groupList) {
|
||||
config.deleteGroup(groupName);
|
||||
}
|
||||
|
||||
|
||||
@@ -47,7 +47,7 @@ void PluginManager::loadAllPlugins()
|
||||
return false;
|
||||
}
|
||||
});
|
||||
for (const auto &metaData : pluginMetaData) {
|
||||
for (const auto &metaData : std::as_const(pluginMetaData)) {
|
||||
const KPluginFactory::Result result = KPluginFactory::instantiatePlugin<IKonsolePlugin>(metaData);
|
||||
if (!result) {
|
||||
continue;
|
||||
|
||||
@@ -38,7 +38,8 @@ ProfileList::ProfileList(bool addShortcuts, QObject *parent)
|
||||
|
||||
connect(_group, &QActionGroup::triggered, this, &ProfileList::triggered);
|
||||
|
||||
for (const auto &profile : ProfileManager::instance()->allProfiles()) {
|
||||
const auto profiles = ProfileManager::instance()->allProfiles();
|
||||
for (const auto &profile : profiles) {
|
||||
addShortcutAction(profile);
|
||||
}
|
||||
|
||||
|
||||
@@ -1859,7 +1859,8 @@ QList<int> Session::copyingSessions()
|
||||
if (auto copyToGroup = c->copyToGroup()) {
|
||||
QList<int> sessionIds;
|
||||
|
||||
for (auto session : copyToGroup->sessions()) {
|
||||
const auto sessions = copyToGroup->sessions();
|
||||
for (auto session : sessions) {
|
||||
sessionIds.append(session->sessionId());
|
||||
}
|
||||
|
||||
|
||||
@@ -2100,7 +2100,7 @@ void SessionController::showDisplayContextMenu(const QPoint &position)
|
||||
// Open Folder With - with different folders on each.
|
||||
// Change the text of the second one, that points to the
|
||||
// current folder.
|
||||
for (auto *action : newActions) {
|
||||
for (auto *action : std::as_const(newActions)) {
|
||||
if (action->objectName() == QStringLiteral("openWith_submenu")) {
|
||||
action->setText(i18n("Open Current Folder With"));
|
||||
}
|
||||
@@ -2130,7 +2130,7 @@ void SessionController::showDisplayContextMenu(const QPoint &position)
|
||||
if (!popup.isNull()) {
|
||||
delete contentSeparator;
|
||||
// Remove the 'Open with' actions from it.
|
||||
for (auto *act : toRemove) {
|
||||
for (auto *act : std::as_const(toRemove)) {
|
||||
popup->removeAction(act);
|
||||
}
|
||||
|
||||
|
||||
@@ -276,7 +276,8 @@ void SessionManager::applyProfile(Session *session, const Profile::Ptr &profile,
|
||||
const bool shouldEnableUrlExtractor = profile->allowEscapedLinks();
|
||||
const bool enableReflowLines = profile->property<bool>(Profile::ReflowLines);
|
||||
const bool ignoreWcWidth = profile->property<bool>(Profile::IgnoreWcWidth);
|
||||
for (TerminalDisplay *view : session->views()) {
|
||||
const auto views = session->views();
|
||||
for (TerminalDisplay *view : views) {
|
||||
view->screenWindow()->screen()->setReflowLines(enableReflowLines);
|
||||
view->screenWindow()->screen()->setIgnoreWcWidth(ignoreWcWidth);
|
||||
view->screenWindow()->screen()->setEnableUrlExtractor(shouldEnableUrlExtractor);
|
||||
|
||||
@@ -281,7 +281,7 @@ void TerminalScrollBar::paintEvent(QPaintEvent *event)
|
||||
p.setPen(Qt::NoPen);
|
||||
|
||||
p.setBrush(searchLineColor);
|
||||
for (int y: _searchLines) {
|
||||
for (int y: std::as_const(_searchLines)) {
|
||||
y = (y * height()) / _terminalLines;
|
||||
p.drawRoundedRect(2, y, width() - 4, stripeHeight, cornerRadius, cornerRadius);
|
||||
}
|
||||
@@ -331,7 +331,7 @@ void TerminalScrollBar::mouseMoveEvent(QMouseEvent *event) {
|
||||
int posY = event->pos().y();
|
||||
bool showToolTip = false;
|
||||
|
||||
for (int y: _searchLines) {
|
||||
for (int y: std::as_const(_searchLines)) {
|
||||
int x = y;
|
||||
y = (y * height()) / _terminalLines;
|
||||
if (abs(y - posY) <= 3) {
|
||||
@@ -372,7 +372,7 @@ void TerminalScrollBar::removeMarker(int clickedYCoord)
|
||||
|
||||
bool TerminalScrollBar::markerExists(int clickedYCoord)
|
||||
{
|
||||
for (Marker *marker : _markers) {
|
||||
for (Marker *marker : std::as_const(_markers)) {
|
||||
double markerTop = marker->geometry.top();
|
||||
double markerHeight = marker->geometry.height();
|
||||
|
||||
@@ -409,7 +409,7 @@ void TerminalScrollBar::registerMarker(TerminalScrollBar::Marker *marker)
|
||||
|
||||
void TerminalScrollBar::regenerateMarkersGeometry()
|
||||
{
|
||||
for (Marker *marker : _markers) {
|
||||
for (Marker *marker : std::as_const(_markers)) {
|
||||
generateMarkerGeometry(marker->position, *marker);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1332,8 +1332,9 @@ void EditProfileDialog::gotNewColorSchemes(const QList<KNSCore::Entry> &changedE
|
||||
int failures = 0;
|
||||
for (auto &entry : std::as_const(changedEntries)) {
|
||||
switch (entry.status()) {
|
||||
case KNSCore::Entry::Installed:
|
||||
for (const QString &file : entry.installedFiles()) {
|
||||
case KNSCore::Entry::Installed: {
|
||||
const auto installedFiles = entry.installedFiles();
|
||||
for (const QString &file : installedFiles) {
|
||||
if (ColorSchemeManager::instance()->loadColorScheme(file)) {
|
||||
continue;
|
||||
}
|
||||
@@ -1346,8 +1347,10 @@ void EditProfileDialog::gotNewColorSchemes(const QList<KNSCore::Entry> &changedE
|
||||
QTimer::singleShot(8000, _appearanceUi->colorSchemeMessageWidget, &KMessageWidget::animatedHide);
|
||||
}
|
||||
break;
|
||||
case KNSCore::Entry::Deleted:
|
||||
for (const auto &file : entry.uninstalledFiles()) {
|
||||
}
|
||||
case KNSCore::Entry::Deleted: {
|
||||
const auto uninstalledFiles = entry.uninstalledFiles();
|
||||
for (const auto &file : uninstalledFiles) {
|
||||
if (ColorSchemeManager::instance()->unloadColorScheme(file)) {
|
||||
continue;
|
||||
}
|
||||
@@ -1356,6 +1359,7 @@ void EditProfileDialog::gotNewColorSchemes(const QList<KNSCore::Entry> &changedE
|
||||
// it either wasn't loaded or was invalid to begin with.
|
||||
}
|
||||
break;
|
||||
}
|
||||
case KNSCore::Entry::Invalid:
|
||||
case KNSCore::Entry::Installing:
|
||||
case KNSCore::Entry::Downloadable:
|
||||
|
||||
@@ -513,7 +513,8 @@ void TabbedViewContainer::openTabContextMenu(const QPoint &point)
|
||||
|
||||
// TODO: add a countChanged signal so we can remove this for.
|
||||
// Detaching in mac causes crashes.
|
||||
for (auto action : _contextPopupMenu->actions()) {
|
||||
const auto actions = _contextPopupMenu->actions();
|
||||
for (auto action : actions) {
|
||||
if (action->objectName() == QStringLiteral("tab-detach")) {
|
||||
action->setEnabled(count() > 1);
|
||||
}
|
||||
@@ -705,7 +706,8 @@ void TabbedViewContainer::closeTerminalTab(int idx)
|
||||
{
|
||||
Q_EMIT removeColor(idx);
|
||||
// TODO: This for should probably go to the ViewSplitter
|
||||
for (auto terminal : viewSplitterAt(idx)->findChildren<TerminalDisplay *>()) {
|
||||
const auto viewSplitters = viewSplitterAt(idx)->findChildren<TerminalDisplay *>();
|
||||
for (auto terminal : viewSplitters) {
|
||||
terminal->sessionController()->closeSession();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -223,7 +223,7 @@ void ViewSplitter::childEvent(QChildEvent *event)
|
||||
}
|
||||
}
|
||||
|
||||
auto terminals = getToplevelSplitter()->findChildren<TerminalDisplay *>();
|
||||
const auto terminals = getToplevelSplitter()->findChildren<TerminalDisplay *>();
|
||||
for (auto terminal : terminals) {
|
||||
terminal->headerBar()->applyVisibilitySettings();
|
||||
}
|
||||
@@ -433,7 +433,8 @@ ViewSplitter *ViewSplitter::getToplevelSplitter()
|
||||
|
||||
ViewSplitter *ViewSplitter::getChildSplitter(int id)
|
||||
{
|
||||
for (auto childSplitter : findChildren<ViewSplitter *>()) {
|
||||
const auto viewSplitters = findChildren<ViewSplitter *>();
|
||||
for (auto childSplitter : viewSplitters) {
|
||||
if (childSplitter->id() == id)
|
||||
return childSplitter;
|
||||
}
|
||||
@@ -616,13 +617,14 @@ void Konsole::ViewSplitterHandle::mousePressEvent(QMouseEvent *ev)
|
||||
QList<ViewSplitter *> splitters = topLevelSplitter->findChildren<ViewSplitter *>();
|
||||
splitters.append(topLevelSplitter);
|
||||
|
||||
for (auto splitter : splitters) {
|
||||
for (auto splitter : std::as_const(splitters)) {
|
||||
if (splitter->orientation() != orientation()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
int delta = 0;
|
||||
for (auto point : splitter->sizes()) {
|
||||
const auto sizes = splitter->sizes();
|
||||
for (auto point : sizes) {
|
||||
delta += point;
|
||||
QPoint thisPoint = orientation() == Qt::Horizontal ? QPoint(delta, 0) : QPoint(0, delta);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user