From c83bb19a68a65a59e149586d809c442168ba35aa Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Sat, 23 Nov 2019 18:41:57 +0200 Subject: [PATCH] Port away from deprecated foreach, use for-range instead, first take The code compiles and konsole loads and seems to work as before. --- src/Application.cpp | 15 ++++++++------- src/ColorSchemeManager.cpp | 4 ++-- src/CopyInputDialog.cpp | 4 ++-- src/EditProfileDialog.cpp | 9 +++++---- src/Emulation.cpp | 4 ++-- src/ExtendedCharTable.cpp | 9 +++++---- src/Filter.cpp | 4 ++-- src/KeyBindingEditor.cpp | 6 +++--- src/KeyboardTranslatorManager.cpp | 6 +++--- src/Session.cpp | 14 +++++++------- src/SessionManager.cpp | 14 +++++++------- src/TerminalDisplay.cpp | 8 +++++--- src/main.cpp | 9 ++++----- 13 files changed, 55 insertions(+), 51 deletions(-) diff --git a/src/Application.cpp b/src/Application.cpp index fd44ce352..1d4aec98a 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -57,7 +57,7 @@ Application::Application(QSharedPointer parser, void Application::populateCommandLineParser(QCommandLineParser *parser) { - auto options = QVector { + const auto options = QVector { { { QStringLiteral("profile") }, i18nc("@info:shell", "Name of profile to use for new Konsole instance"), QStringLiteral("name") @@ -118,7 +118,7 @@ void Application::populateCommandLineParser(QCommandLineParser *parser) QStringLiteral("cmd") } }; - foreach(const auto& option, options) { + for (const auto &option : options) { parser->addOption(option); } @@ -187,7 +187,8 @@ void Application::detachTerminals(ViewSplitter *splitter,const QHashviewManager(); - foreach(TerminalDisplay* terminal, splitter->findChildren()) { + const QList displays = splitter->findChildren(); + for (TerminalDisplay* terminal : displays) { manager->attachView(terminal, sessionsMap[terminal]); } manager->activeContainer()->addSplitter(splitter); @@ -472,9 +473,9 @@ bool Application::processHelpArgs() void Application::listAvailableProfiles() { - QStringList paths = ProfileManager::instance()->availableProfilePaths(); + const QStringList paths = ProfileManager::instance()->availableProfilePaths(); - foreach (const QString &path, paths) { + for (const QString &path : paths) { QFileInfo info(path); printf("%s\n", info.completeBaseName().toLocal8Bit().constData()); } @@ -485,7 +486,7 @@ void Application::listProfilePropertyInfo() Profile::Ptr tempProfile = ProfileManager::instance()->defaultProfile(); const QStringList names = tempProfile->propertiesInfoList(); - foreach (const QString &name, names) { + for (const QString &name : names) { printf("%s\n", name.toLocal8Bit().constData()); } } @@ -505,7 +506,7 @@ Profile::Ptr Application::processProfileChangeArgs(Profile::Ptr baseProfile) // temporary changes to profile options specified on the command line const QStringList profileProperties = m_parser->values(QStringLiteral("p")); - foreach (const QString &value, profileProperties) { + for (const QString &value : profileProperties) { ProfileCommandParser parser; QHashIterator iter(parser.parse(value)); diff --git a/src/ColorSchemeManager.cpp b/src/ColorSchemeManager.cpp index 0b030234a..bed07128e 100644 --- a/src/ColorSchemeManager.cpp +++ b/src/ColorSchemeManager.cpp @@ -58,8 +58,8 @@ void ColorSchemeManager::loadAllColorSchemes() int success = 0; int failed = 0; - QStringList nativeColorSchemes = listColorSchemes(); - foreach (const QString &colorScheme, nativeColorSchemes) { + const QStringList nativeColorSchemes = listColorSchemes(); + for (const QString &colorScheme : nativeColorSchemes) { if (loadColorScheme(colorScheme)) { success++; } else { diff --git a/src/CopyInputDialog.cpp b/src/CopyInputDialog.cpp index 4913cc339..89669be6c 100644 --- a/src/CopyInputDialog.cpp +++ b/src/CopyInputDialog.cpp @@ -121,10 +121,10 @@ void CopyInputDialog::setSelectionChecked(bool checked) QAbstractItemModel *model = _ui->sessionList->model(); int rows = model->rowCount(); - QModelIndexList selected = _ui->sessionList->selectionModel()->selectedIndexes(); + const QModelIndexList selected = _ui->sessionList->selectionModel()->selectedIndexes(); if (selected.count() > 1) { - foreach (const QModelIndex &index, selected) { + for (const QModelIndex &index : selected) { setRowChecked(index.row(), checked); } } else { diff --git a/src/EditProfileDialog.cpp b/src/EditProfileDialog.cpp index 06674bd29..a653dc21a 100644 --- a/src/EditProfileDialog.cpp +++ b/src/EditProfileDialog.cpp @@ -307,7 +307,7 @@ bool EditProfileDialog::isValidProfileName() const QList existingProfiles = ProfileManager::instance()->allProfiles(); QStringList otherExistingProfileNames; - foreach(auto existingProfile, existingProfiles) { + for (const Profile::Ptr &existingProfile : existingProfiles) { if (existingProfile->name() != _profile->name()) { otherExistingProfileNames.append(existingProfile->name()); } @@ -866,9 +866,9 @@ void EditProfileDialog::updateColorSchemeList(const QString &selectedColorScheme QStandardItem *selectedItem = nullptr; - QList schemeList = ColorSchemeManager::instance()->allColorSchemes(); + const QList schemeList = ColorSchemeManager::instance()->allColorSchemes(); - foreach (const ColorScheme *scheme, schemeList) { + for (const ColorScheme *scheme : schemeList) { QStandardItem *item = new QStandardItem(scheme->description()); item->setData(QVariant::fromValue(scheme), Qt::UserRole + 1); item->setData(QVariant::fromValue(_profile->font()), Qt::UserRole + 2); @@ -1325,7 +1325,8 @@ void EditProfileDialog::enableIfNonEmptySelection(QWidget *widget, QItemSelectio void EditProfileDialog::updateTransparencyWarning() { // zero or one indexes can be selected - foreach (const QModelIndex &index, _appearanceUi->colorSchemeList->selectionModel()->selectedIndexes()) { + const QModelIndexList selected = _appearanceUi->colorSchemeList->selectionModel()->selectedIndexes(); + for (const QModelIndex &index : selected) { bool needTransparency = index.data(Qt::UserRole + 1).value()->opacity() < 1.0; if (!needTransparency) { diff --git a/src/Emulation.cpp b/src/Emulation.cpp index fab70f25d..612ea3504 100644 --- a/src/Emulation.cpp +++ b/src/Emulation.cpp @@ -109,7 +109,7 @@ void Emulation::checkSelectedText() Emulation::~Emulation() { - foreach (ScreenWindow *window, _windows) { + for (ScreenWindow *window : qAsConst(_windows)) { delete window; } @@ -124,7 +124,7 @@ void Emulation::setScreen(int index) _currentScreen = _screen[index & 1]; if (_currentScreen != oldScreen) { // tell all windows onto this emulation to switch to the newly active screen - foreach (ScreenWindow *window, _windows) { + for (ScreenWindow *window : qAsConst(_windows)) { window->setScreen(_currentScreen); } diff --git a/src/ExtendedCharTable.cpp b/src/ExtendedCharTable.cpp index 181eca601..bfac6b2f0 100644 --- a/src/ExtendedCharTable.cpp +++ b/src/ExtendedCharTable.cpp @@ -74,10 +74,11 @@ uint ExtendedCharTable::createExtendedChar(const uint *unicodePoints, ushort len // All the hashes are full, go to all Screens and try to free any // This is slow but should happen very rarely QSet usedExtendedChars; - const SessionManager *sm = SessionManager::instance(); - foreach (const Session *s, sm->sessions()) { - foreach (const TerminalDisplay *td, s->views()) { - usedExtendedChars += td->screenWindow()->screen()->usedExtendedChars(); + const QList sessionsList = SessionManager::instance()->sessions(); + for (const Session *s : sessionsList) { + const QList displayList = s->views(); + for (const TerminalDisplay *display : displayList) { + usedExtendedChars += display->screenWindow()->screen()->usedExtendedChars(); } } diff --git a/src/Filter.cpp b/src/Filter.cpp index f889534f0..fb52b6dc9 100644 --- a/src/Filter.cpp +++ b/src/Filter.cpp @@ -254,9 +254,9 @@ QList Filter::hotSpots() const Filter::HotSpot *Filter::hotSpotAt(int line, int column) const { - QList hotspots = _hotspots.values(line); + const QList hotspots = _hotspots.values(line); - foreach (HotSpot *spot, hotspots) { + for (HotSpot *spot : hotspots) { if (spot->startLine() == line && spot->startColumn() > column) { continue; } diff --git a/src/KeyBindingEditor.cpp b/src/KeyBindingEditor.cpp index db885556c..4b8774e9f 100644 --- a/src/KeyBindingEditor.cpp +++ b/src/KeyBindingEditor.cpp @@ -123,8 +123,8 @@ void KeyBindingEditor::filterRows(const QString &text) void KeyBindingEditor::removeSelectedEntry() { QList uniqueList; - - foreach (QTableWidgetItem *item, _ui->keyBindingTable->selectedItems()) { + const QList selectedItems = _ui->keyBindingTable->selectedItems(); + for (QTableWidgetItem *item : selectedItems) { if (item->column() == 1) { //Select item at the first column item = _ui->keyBindingTable->item(item->row(), 0); } @@ -134,7 +134,7 @@ void KeyBindingEditor::removeSelectedEntry() } } - foreach (QTableWidgetItem *item, uniqueList) { + for (QTableWidgetItem *item : qAsConst(uniqueList)) { // get the first item in the row which has the entry KeyboardTranslator::Entry existing = item->data(Qt::UserRole). diff --git a/src/KeyboardTranslatorManager.cpp b/src/KeyboardTranslatorManager.cpp index ac70f44ed..0dad8799b 100644 --- a/src/KeyboardTranslatorManager.cpp +++ b/src/KeyboardTranslatorManager.cpp @@ -112,7 +112,7 @@ void KeyboardTranslatorManager::findTranslators() // add the name of each translator to the list and associated // the name with a null pointer to indicate that the translator // has not yet been loaded from disk - foreach (const QString &translatorPath, list) { + for (const QString &translatorPath : qAsConst(list)) { QString name = QFileInfo(translatorPath).completeBaseName(); if (!_translators.contains(name)) { @@ -163,8 +163,8 @@ bool KeyboardTranslatorManager::saveTranslator(const KeyboardTranslator *transla { KeyboardTranslatorWriter writer(&destination); writer.writeHeader(translator->description()); - - foreach (const KeyboardTranslator::Entry &entry, translator->entries()) { + const QList entriesList = translator->entries(); + for (const KeyboardTranslator::Entry &entry : entriesList) { writer.writeEntry(entry); } } diff --git a/src/Session.cpp b/src/Session.cpp index c3f851023..e4d6a7164 100644 --- a/src/Session.cpp +++ b/src/Session.cpp @@ -623,7 +623,7 @@ void Session::silenceTimerDone() } bool hasFocus = false; - foreach(TerminalDisplay *display, _views) { + for (const TerminalDisplay *display : qAsConst(_views)) { if (display->hasFocus()) { hasFocus = true; break; @@ -646,14 +646,14 @@ void Session::updateFlowControlState(bool suspended) { if (suspended) { if (flowControlEnabled()) { - foreach(TerminalDisplay * display, _views) { + for (TerminalDisplay *display : qAsConst(_views)) { if (display->flowControlWarningEnabled()) { display->outputSuspended(true); } } } } else { - foreach(TerminalDisplay * display, _views) { + for (TerminalDisplay *display : qAsConst(_views)) { display->outputSuspended(false); } } @@ -695,7 +695,7 @@ void Session::activityStateSet(int state) } else if (state == NOTIFYACTIVITY) { // Don't notify if the terminal is active bool hasFocus = false; - foreach(TerminalDisplay *display, _views) { + for (const TerminalDisplay *display : qAsConst(_views)) { if (display->hasFocus()) { hasFocus = true; break; @@ -746,7 +746,7 @@ void Session::updateTerminalSize() const int VIEW_COLUMNS_THRESHOLD = 2; //select largest number of lines and columns that will fit in all visible views - foreach(TerminalDisplay* view, _views) { + for (TerminalDisplay *view : qAsConst(_views)) { if (!view->isHidden() && view->lines() >= VIEW_LINES_THRESHOLD && view->columns() >= VIEW_COLUMNS_THRESHOLD) { @@ -1604,7 +1604,7 @@ QString Session::profile() void Session::setProfile(const QString &profileName) { const QList profiles = ProfileManager::instance()->allProfiles(); - foreach (const Profile::Ptr &profile, profiles) { + for (const Profile::Ptr &profile : profiles) { if (profile->name() == profileName) { SessionManager::instance()->setSessionProfile(this, profile); } @@ -1785,7 +1785,7 @@ void SessionGroup::forwardData(const QByteArray& data) _inForwardData = true; const QList sessionsKeys = _sessions.keys(); - foreach(Session* other, sessionsKeys) { + for (Session *other : sessionsKeys) { if (!_sessions[other]) { other->emulation()->sendString(data); } diff --git a/src/SessionManager.cpp b/src/SessionManager.cpp index b82d629c4..db89ceb67 100644 --- a/src/SessionManager.cpp +++ b/src/SessionManager.cpp @@ -61,7 +61,7 @@ SessionManager::~SessionManager() <<"session(s) still alive"; // ensure that the Session doesn't later try to call back and do things to the // SessionManager - foreach (Session *session, _sessions) { + for (Session *session : qAsConst(_sessions)) { disconnect(session, nullptr, this, nullptr); } } @@ -81,7 +81,7 @@ bool SessionManager::isClosingAllSessions() const void SessionManager::closeAllSessions() { _isClosingAllSessions = true; - foreach (Session *session, _sessions) { + for (Session *session : qAsConst(_sessions)) { session->close(); } _sessions.clear(); @@ -142,7 +142,7 @@ void SessionManager::sessionTerminated(Session *session) void SessionManager::applyProfile(const Profile::Ptr &profile, bool modifiedPropertiesOnly) { - foreach (Session *session, _sessions) { + for (Session *session : qAsConst(_sessions)) { if (_sessionProfiles[session] == profile) { applyProfile(session, profile, modifiedPropertiesOnly); } @@ -292,8 +292,8 @@ void SessionManager::sessionProfileCommandReceived(const QString &text) // store the font for each view if zoom was applied so that they can // be restored after applying the new profile QHash zoomFontSizes; - - foreach (TerminalDisplay *view, session->views()) { + const QList views = session->views(); + for (TerminalDisplay *view : views) { const QFont &viewCurFont = view->getVTFont(); if (viewCurFont != _sessionProfiles[session]->font()) { zoomFontSizes.insert(view, viewCurFont); @@ -337,7 +337,7 @@ void SessionManager::saveSessions(KConfig *config) int n = 1; _restoreMapping.clear(); - foreach (Session *session, _sessions) { + for (Session *session : qAsConst(_sessions)) { QString name = QLatin1String("Session") + QString::number(n); KConfigGroup group(config, name); @@ -379,7 +379,7 @@ void SessionManager::restoreSessions(KConfig *config) Session *SessionManager::idToSession(int id) { - foreach (Session *session, _sessions) { + for (Session *session : qAsConst(_sessions)) { if (session->sessionId() == id) { return session; } diff --git a/src/TerminalDisplay.cpp b/src/TerminalDisplay.cpp index 446bfe769..dca9c2368 100644 --- a/src/TerminalDisplay.cpp +++ b/src/TerminalDisplay.cpp @@ -1292,7 +1292,9 @@ void TerminalDisplay::paintEvent(QPaintEvent* pe) // Determine which characters should be repainted (1 region unit = 1 character) QRegion dirtyImageRegion; - foreach(const QRect & rect, (pe->region() & contentsRect())) { + const QRegion region = pe->region() & contentsRect(); + + for (const QRect &rect : region) { dirtyImageRegion += widgetToImage(rect); drawBackground(paint, rect, getBackgroundColor(), true /* use opacity setting */); } @@ -1301,7 +1303,7 @@ void TerminalDisplay::paintEvent(QPaintEvent* pe) // set https://bugreports.qt.io/browse/QTBUG-66036 paint.setRenderHint(QPainter::TextAntialiasing, _antialiasText); - foreach(const QRect & rect, dirtyImageRegion) { + for (const QRect &rect : qAsConst(dirtyImageRegion)) { drawContents(paint, rect); } drawCurrentResultRect(paint); @@ -1310,7 +1312,7 @@ void TerminalDisplay::paintEvent(QPaintEvent* pe) const bool drawDimmed = _dimWhenInactive && !hasFocus(); const QColor dimColor(0, 0, 0, 128); - foreach(const QRect & rect, (pe->region() & contentsRect())) { + for (const QRect &rect : region) { if (drawDimmed) { paint.fillRect(rect, dimColor); } diff --git a/src/main.cpp b/src/main.cpp index b477109fd..151cdb4d5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -196,9 +196,8 @@ extern "C" int Q_DECL_EXPORT kdemain(int argc, char *argv[]) if (!targetDir.exists()) { QDir().mkpath(targetBasePath); } - QStringList fileNames = sourceDir.entryList( - QDir::Files | QDir::NoDotAndDotDot | QDir::NoSymLinks); - foreach (const QString &fileName, fileNames) { + const QStringList fileNames = sourceDir.entryList(QDir::Files | QDir::NoDotAndDotDot | QDir::NoSymLinks); + for (const QString &fileName : fileNames) { targetFilePath = targetBasePath + fileName; if (!QFile::exists(targetFilePath)) { QFile::copy(sourceBasePath + fileName, targetFilePath); @@ -259,7 +258,7 @@ bool shouldUseNewProcess(int argc, char *argv[]) qtProblematicOptions << QStringLiteral("--display") << QStringLiteral("--visual"); #endif - foreach (const QString &option, qtProblematicOptions) { + for (const QString &option : qAsConst(qtProblematicOptions)) { if (arguments.contains(option)) { return true; } @@ -273,7 +272,7 @@ bool shouldUseNewProcess(int argc, char *argv[]) kdeProblematicOptions << QStringLiteral("--waitforwm"); #endif - foreach (const QString &option, kdeProblematicOptions) { + for (const QString &option : qAsConst(kdeProblematicOptions)) { if (arguments.contains(option)) { return true; }