summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Rolf Eike Beer <eike@sf-mail.de>2019-04-01 22:15:19 +0200
committerGravatar Dirk Hohndel <dirk@hohndel.org>2019-04-12 12:59:17 +0300
commitc4c8094e32ad78dee558a80584470172f48c45b1 (patch)
tree4fede2acf0f1a3cee2182d96b1b3efa33e4fd8ff
parent2b9ca488fd18dc9d65d42dc5900e120a07e5b3f6 (diff)
downloadsubsurface-c4c8094e32ad78dee558a80584470172f48c45b1.tar.gz
get rid of some foreach and Q_FOREACH constructs
See https://www.kdab.com/goodbye-q_foreach/ This is reduced to the places where the container is const or can be made const without the need to always introduce an extra variable. Sadly qAsConst (Qt 5.7) and std::as_const (C++17) are not available in all supported setups. Also do some minor cleanups along the way. Signed-off-by: Rolf Eike Beer <eike@sf-mail.de>
-rw-r--r--CodingStyle.md12
-rw-r--r--core/btdiscovery.cpp4
-rw-r--r--core/checkcloudconnection.cpp4
-rw-r--r--core/checkcloudconnection.h2
-rw-r--r--core/cloudstorage.cpp4
-rw-r--r--core/cloudstorage.h2
-rw-r--r--core/qt-ble.cpp8
-rw-r--r--core/qt-ble.h2
-rw-r--r--core/qthelper.cpp6
-rw-r--r--desktop-widgets/locationinformation.cpp4
-rw-r--r--desktop-widgets/printer.cpp4
-rw-r--r--desktop-widgets/tab-widgets/TabDiveStatistics.cpp6
-rw-r--r--desktop-widgets/tab-widgets/maintab.cpp9
-rw-r--r--desktop-widgets/tagwidget.cpp3
-rw-r--r--desktop-widgets/templatelayout.cpp16
-rw-r--r--mobile-widgets/qmlmanager.cpp6
-rw-r--r--profile-widget/divetooltipitem.cpp5
-rw-r--r--profile-widget/profilewidget2.cpp4
-rw-r--r--qt-models/divelistmodel.cpp4
-rw-r--r--qt-models/divelistmodel.h2
-rw-r--r--subsurface-helper.cpp4
21 files changed, 62 insertions, 49 deletions
diff --git a/CodingStyle.md b/CodingStyle.md
index cb27c4b1d..9be434470 100644
--- a/CodingStyle.md
+++ b/CodingStyle.md
@@ -156,6 +156,16 @@ other editors that implement this coding style, please add them here.
```
QLowEnergyService *service = qobject_cast<QLowEnergyService*>(sender());
```
+ - If the variable is a container that is only assigned to a local variable to
+ be able to use it in a range-based for loop
+ ```
+ const auto l = device.serviceUuids();
+ for (QBluetoothUuid id: serviceUuids) {
+ ```
+ The variable has also to be const to avoid that Qt containers will do a
+ deep copy when the range bases for loop will call the begin() method
+ internally.
+
* text strings
The default language of subsurface is US English so please use US English
spelling and terminology.
@@ -173,7 +183,7 @@ other editors that implement this coding style, please add them here.
This works by default in classes (indirectly) derived from QObject. Each
string to be translated is associated with a context, which corresponds
to the class name. Classes that are not derived from QObject can generate
- the tr() functions by using the `Q_DECLARE_FUNCTIONS` macro:
+ the tr() functions by using the `Q_DECLARE_TR_FUNCTIONS` macro:
```
#include <QCoreApplication>
diff --git a/core/btdiscovery.cpp b/core/btdiscovery.cpp
index 3ef0273fe..7e75994da 100644
--- a/core/btdiscovery.cpp
+++ b/core/btdiscovery.cpp
@@ -198,8 +198,8 @@ void BTDiscovery::btDeviceDiscovered(const QBluetoothDeviceInfo &device)
this_d.name = device.name();
btPairedDevices.append(this_d);
- QList<QBluetoothUuid> serviceUuids = device.serviceUuids();
- foreach (QBluetoothUuid id, serviceUuids) {
+ const auto serviceUuids = device.serviceUuids();
+ for (QBluetoothUuid id: serviceUuids) {
addBtUuid(id);
qDebug() << id.toByteArray();
}
diff --git a/core/checkcloudconnection.cpp b/core/checkcloudconnection.cpp
index ffe2094f6..b912160c1 100644
--- a/core/checkcloudconnection.cpp
+++ b/core/checkcloudconnection.cpp
@@ -76,11 +76,11 @@ bool CheckCloudConnection::checkServer()
return false;
}
-void CheckCloudConnection::sslErrors(QList<QSslError> errorList)
+void CheckCloudConnection::sslErrors(const QList<QSslError> &errorList)
{
if (verbose) {
qDebug() << "Received error response trying to set up https connection with cloud storage backend:";
- Q_FOREACH (QSslError err, errorList) {
+ for (QSslError err: errorList) {
qDebug() << err.errorString();
}
}
diff --git a/core/checkcloudconnection.h b/core/checkcloudconnection.h
index 69f8a2964..312a1e78c 100644
--- a/core/checkcloudconnection.h
+++ b/core/checkcloudconnection.h
@@ -15,7 +15,7 @@ private:
QNetworkReply *reply;
private
slots:
- void sslErrors(QList<QSslError> errorList);
+ void sslErrors(const QList<QSslError> &errorList);
};
#endif // CHECKCLOUDCONNECTION_H
diff --git a/core/cloudstorage.cpp b/core/cloudstorage.cpp
index 39d9a4d66..77d9153e9 100644
--- a/core/cloudstorage.cpp
+++ b/core/cloudstorage.cpp
@@ -81,11 +81,11 @@ void CloudStorageAuthenticate::uploadError(QNetworkReply::NetworkError)
qDebug() << "Received error response from cloud storage backend:" << reply->errorString();
}
-void CloudStorageAuthenticate::sslErrors(QList<QSslError> errorList)
+void CloudStorageAuthenticate::sslErrors(const QList<QSslError> &errorList)
{
if (verbose) {
qDebug() << "Received error response trying to set up https connection with cloud storage backend:";
- Q_FOREACH (QSslError err, errorList) {
+ for (QSslError err: errorList) {
qDebug() << err.errorString();
}
}
diff --git a/core/cloudstorage.h b/core/cloudstorage.h
index 681b2b290..b19b3292c 100644
--- a/core/cloudstorage.h
+++ b/core/cloudstorage.h
@@ -16,7 +16,7 @@ signals:
private
slots:
void uploadError(QNetworkReply::NetworkError error);
- void sslErrors(QList<QSslError> errorList);
+ void sslErrors(const QList<QSslError> &errorList);
void uploadFinished();
private:
QNetworkReply *reply;
diff --git a/core/qt-ble.cpp b/core/qt-ble.cpp
index 9626042b5..9ee5def51 100644
--- a/core/qt-ble.cpp
+++ b/core/qt-ble.cpp
@@ -341,7 +341,7 @@ dc_status_t BLEObject::setHwCredit(unsigned int c)
return DC_STATUS_SUCCESS;
}
-dc_status_t BLEObject::setupHwTerminalIo(QList<QLowEnergyCharacteristic> allC)
+dc_status_t BLEObject::setupHwTerminalIo(const QList<QLowEnergyCharacteristic> &allC)
{ /* This initalizes the Terminal I/O client as described in
* http://www.telit.com/fileadmin/user_upload/products/Downloads/sr-rf/BlueMod/TIO_Implementation_Guide_r04.pdf
* Referenced section numbers below are from that document.
@@ -479,16 +479,16 @@ dc_status_t qt_ble_open(void **io, dc_context_t *, const char *devaddr, dc_user_
return r;
}
} else {
- foreach (const QLowEnergyCharacteristic &c, list) {
+ for (const QLowEnergyCharacteristic &c: list) {
if (!is_read_characteristic(c))
continue;
qDebug() << "Using read characteristic" << c.uuid();
- QList<QLowEnergyDescriptor> l = c.descriptors();
+ const QList<QLowEnergyDescriptor> l = c.descriptors();
QLowEnergyDescriptor d = l.first();
- foreach (const QLowEnergyDescriptor &tmp, l) {
+ for (const QLowEnergyDescriptor &tmp: l) {
if (tmp.type() == QBluetoothUuid::ClientCharacteristicConfiguration) {
d = tmp;
break;
diff --git a/core/qt-ble.h b/core/qt-ble.h
index 65e23964a..2b011163f 100644
--- a/core/qt-ble.h
+++ b/core/qt-ble.h
@@ -35,7 +35,7 @@ public slots:
void characteristcStateChanged(const QLowEnergyCharacteristic &c, const QByteArray &value);
void characteristicWritten(const QLowEnergyCharacteristic &c, const QByteArray &value);
void writeCompleted(const QLowEnergyDescriptor &d, const QByteArray &value);
- dc_status_t setupHwTerminalIo(QList<QLowEnergyCharacteristic>);
+ dc_status_t setupHwTerminalIo(const QList<QLowEnergyCharacteristic> &allC);
dc_status_t setHwCredit(unsigned int c);
private:
QVector<QLowEnergyService *> services;
diff --git a/core/qthelper.cpp b/core/qthelper.cpp
index 1a85d35a7..3738e56f2 100644
--- a/core/qthelper.cpp
+++ b/core/qthelper.cpp
@@ -1144,7 +1144,7 @@ QStringList imageExtensionFilters()
QStringList videoExtensionFilters()
{
QStringList filters;
- foreach (const QString &format, videoExtensionsList)
+ for (const QString &format: videoExtensionsList)
filters.append("*" + format);
return filters;
}
@@ -1506,9 +1506,9 @@ int parse_seabear_header(const char *filename, char **params, int pnr)
parseLine = f.readLine().trimmed();
- QStringList currColumns = parseLine.split(';');
+ const QStringList currColumns = parseLine.split(';');
unsigned short index = 0;
- Q_FOREACH (QString columnText, currColumns) {
+ for (const QString &columnText: currColumns) {
if (columnText == "Time") {
params[pnr++] = strdup("timeField");
params[pnr++] = intdup(index++);
diff --git a/desktop-widgets/locationinformation.cpp b/desktop-widgets/locationinformation.cpp
index af3fa513f..24f45058a 100644
--- a/desktop-widgets/locationinformation.cpp
+++ b/desktop-widgets/locationinformation.cpp
@@ -75,11 +75,11 @@ void LocationInformationWidget::mergeSelectedDiveSites()
QMessageBox::Ok, QMessageBox::Cancel) != QMessageBox::Ok)
return;
- QModelIndexList selection = ui.diveSiteListView->selectionModel()->selectedIndexes();
+ const QModelIndexList selection = ui.diveSiteListView->selectionModel()->selectedIndexes();
// std::vector guarantees contiguous storage and can therefore be passed to C-code
std::vector<struct dive_site *> selected_dive_sites;
selected_dive_sites.reserve(selection.count());
- Q_FOREACH (const QModelIndex &idx, selection) {
+ for (const QModelIndex &idx: selection) {
dive_site *ds = idx.data(LocationInformationModel::DIVESITE_ROLE).value<dive_site *>();
if (ds)
selected_dive_sites.push_back(ds);
diff --git a/desktop-widgets/printer.cpp b/desktop-widgets/printer.cpp
index fa0cd1010..4b7123adb 100644
--- a/desktop-widgets/printer.cpp
+++ b/desktop-widgets/printer.cpp
@@ -77,8 +77,8 @@ void Printer::flowRender()
// get all references to dontbreak divs
int start = 0, end = 0;
int fullPageResolution = webView->page()->mainFrame()->contentsSize().height();
- QWebElementCollection dontbreak = webView->page()->mainFrame()->findAllElements(".dontbreak");
- foreach (QWebElement dontbreakElement, dontbreak) {
+ const QWebElementCollection dontbreak = webView->page()->mainFrame()->findAllElements(".dontbreak");
+ for (QWebElement dontbreakElement: dontbreak) {
if ((dontbreakElement.geometry().y() + dontbreakElement.geometry().height()) - start < pageSize.height()) {
// One more element can be placed
end = dontbreakElement.geometry().y() + dontbreakElement.geometry().height();
diff --git a/desktop-widgets/tab-widgets/TabDiveStatistics.cpp b/desktop-widgets/tab-widgets/TabDiveStatistics.cpp
index 1b3fe9522..dacb01721 100644
--- a/desktop-widgets/tab-widgets/TabDiveStatistics.cpp
+++ b/desktop-widgets/tab-widgets/TabDiveStatistics.cpp
@@ -21,9 +21,9 @@ TabDiveStatistics::TabDiveStatistics(QWidget *parent) : TabBase(parent), ui(new
ui->timeLimits->overrideMinToolTipText(tr("Shortest dive"));
ui->timeLimits->overrideAvgToolTipText(tr("Average length of all selected dives"));
- Q_FOREACH (QObject *obj, children()) {
- if (QLabel *label = qobject_cast<QLabel *>(obj))
- label->setAlignment(Qt::AlignHCenter);
+ const auto l = findChildren<QLabel *>(QString(), Qt::FindDirectChildrenOnly);
+ for (QLabel *label: l) {
+ label->setAlignment(Qt::AlignHCenter);
}
}
diff --git a/desktop-widgets/tab-widgets/maintab.cpp b/desktop-widgets/tab-widgets/maintab.cpp
index 98b8904c2..c934cdb16 100644
--- a/desktop-widgets/tab-widgets/maintab.cpp
+++ b/desktop-widgets/tab-widgets/maintab.cpp
@@ -1271,14 +1271,13 @@ void MainTab::saveTaggedStrings(const QVector<dive *> &selectedDives)
int MainTab::diffTaggedStrings(QString currentString, QString displayedString, QStringList &addedList, QStringList &removedList)
{
- QStringList displayedList, currentList;
- currentList = currentString.split(',', QString::SkipEmptyParts);
- displayedList = displayedString.split(',', QString::SkipEmptyParts);
- Q_FOREACH ( const QString tag, currentList) {
+ const QStringList currentList = currentString.split(',', QString::SkipEmptyParts);
+ const QStringList displayedList = displayedString.split(',', QString::SkipEmptyParts);
+ for (const QString &tag: currentList) {
if (!displayedList.contains(tag, Qt::CaseInsensitive))
removedList << tag.trimmed();
}
- Q_FOREACH (const QString tag, displayedList) {
+ for (const QString &tag: displayedList) {
if (!currentList.contains(tag, Qt::CaseInsensitive))
addedList << tag.trimmed();
}
diff --git a/desktop-widgets/tagwidget.cpp b/desktop-widgets/tagwidget.cpp
index c8d017aad..6673b23ac 100644
--- a/desktop-widgets/tagwidget.cpp
+++ b/desktop-widgets/tagwidget.cpp
@@ -70,7 +70,8 @@ void TagWidget::highlight()
{
removeAllBlocks();
int lastPos = 0;
- Q_FOREACH (const QString& s, text().split(QChar(','), QString::SkipEmptyParts)) {
+ const auto l = text().split(QChar(','), QString::SkipEmptyParts);
+ for (const QString &s: l) {
QString trimmed = s.trimmed();
if (trimmed.isEmpty())
continue;
diff --git a/desktop-widgets/templatelayout.cpp b/desktop-widgets/templatelayout.cpp
index f451250d6..b28c78741 100644
--- a/desktop-widgets/templatelayout.cpp
+++ b/desktop-widgets/templatelayout.cpp
@@ -19,20 +19,20 @@ int getTotalWork(print_options *printOptions)
void find_all_templates()
{
- const QString ext(".html");
+ const QLatin1String ext(".html");
grantlee_templates.clear();
grantlee_statistics_templates.clear();
QDir dir(getPrintingTemplatePathUser());
- QStringList list = dir.entryList(QDir::Files | QDir::NoDotAndDotDot);
- foreach (const QString& filename, list) {
+ const QStringList list = dir.entryList(QDir::Files | QDir::NoDotAndDotDot);
+ for (const QString &filename: list) {
if (filename.at(filename.size() - 1) != '~' && filename.endsWith(ext))
grantlee_templates.append(filename);
}
// find statistics templates
dir.setPath(getPrintingTemplatePathUser() + QDir::separator() + "statistics");
- list = dir.entryList(QDir::Files | QDir::NoDotAndDotDot);
- foreach (const QString& filename, list) {
+ const QStringList stat = dir.entryList(QDir::Files | QDir::NoDotAndDotDot);
+ for (const QString &filename: stat) {
if (filename.at(filename.size() - 1) != '~' && filename.endsWith(ext))
grantlee_statistics_templates.append(filename);
}
@@ -66,12 +66,14 @@ void copy_bundled_templates(QString src, QString dst, QStringList *templateBacku
QDir dir(src);
if (!dir.exists())
return;
- foreach (QString d, dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) {
+ const auto dirs = dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
+ for (const QString &d: dirs) {
QString dst_path = dst + QDir::separator() + d;
dir.mkpath(dst_path);
copy_bundled_templates(src + QDir::separator() + d, dst_path, templateBackupList);
}
- foreach (QString f, dir.entryList(QDir::Files)) {
+ const auto files = dir.entryList(QDir::Files);
+ for (const QString &f: files) {
QFile fileSrc(src + QDir::separator() + f);
QFile fileDest(dst + QDir::separator() + f);
if (fileDest.exists()) {
diff --git a/mobile-widgets/qmlmanager.cpp b/mobile-widgets/qmlmanager.cpp
index 1e2b6e132..196554654 100644
--- a/mobile-widgets/qmlmanager.cpp
+++ b/mobile-widgets/qmlmanager.cpp
@@ -599,7 +599,7 @@ void QMLManager::handleSslErrors(const QList<QSslError> &errors)
{
auto *reply = qobject_cast<QNetworkReply *>(sender());
setStartPageText(RED_FONT + tr("Cannot open cloud storage: Error creating https connection") + END_FONT);
- Q_FOREACH (QSslError e, errors) {
+ for (QSslError e: errors) {
appendTextToLog(e.errorString());
}
reply->abort();
@@ -1761,8 +1761,8 @@ void QMLManager::setStatusbarColor(QColor)
void QMLManager::retrieveBluetoothName()
{
QString name = DC_devName();
- QList<BTDiscovery::btVendorProduct> btDCs = BTDiscovery::instance()->getBtDcs();
- foreach (BTDiscovery::btVendorProduct btDC, btDCs) {
+ const QList<BTDiscovery::btVendorProduct> btDCs = BTDiscovery::instance()->getBtDcs();
+ for (BTDiscovery::btVendorProduct btDC: btDCs) {
qDebug() << "compare" <<name << btDC.btpdi.address;
if (name.contains(btDC.btpdi.address))
DC_setDevBluetoothName(btDC.btpdi.name);
diff --git a/profile-widget/divetooltipitem.cpp b/profile-widget/divetooltipitem.cpp
index 0d3e6728c..f88d3b769 100644
--- a/profile-widget/divetooltipitem.cpp
+++ b/profile-widget/divetooltipitem.cpp
@@ -272,8 +272,9 @@ void ToolTipItem::refresh(const QPointF &pos)
}
entryToolTip.first->setPixmap(tissues);
- Q_FOREACH (QGraphicsItem *item, scene()->items(pos, Qt::IntersectsItemBoundingRect
- ,Qt::DescendingOrder, scene()->views().first()->transform())) {
+ const auto l = scene()->items(pos, Qt::IntersectsItemBoundingRect, Qt::DescendingOrder,
+ scene()->views().first()->transform());
+ for (QGraphicsItem *item: l) {
if (!item->toolTip().isEmpty())
addToolTip(item->toolTip());
}
diff --git a/profile-widget/profilewidget2.cpp b/profile-widget/profilewidget2.cpp
index 157ab9fcd..236afb850 100644
--- a/profile-widget/profilewidget2.cpp
+++ b/profile-widget/profilewidget2.cpp
@@ -1101,9 +1101,9 @@ bool ProfileWidget2::eventFilter(QObject *object, QEvent *event)
#endif
template <typename T>
-static void hideAll(T &container)
+static void hideAll(const T &container)
{
- Q_FOREACH (auto *item, container)
+ for (auto *item: container)
item->setVisible(false);
}
diff --git a/qt-models/divelistmodel.cpp b/qt-models/divelistmodel.cpp
index 6edb69705..4d66c5406 100644
--- a/qt-models/divelistmodel.cpp
+++ b/qt-models/divelistmodel.cpp
@@ -144,12 +144,12 @@ DiveListModel::DiveListModel(QObject *parent) : QAbstractListModel(parent)
m_instance = this;
}
-void DiveListModel::addDive(QList<dive *>listOfDives)
+void DiveListModel::addDive(const QList<dive *> &listOfDives)
{
if (listOfDives.isEmpty())
return;
beginInsertRows(QModelIndex(), rowCount(), rowCount() + listOfDives.count() - 1);
- foreach (dive *d, listOfDives) {
+ for (dive *d: listOfDives) {
m_dives.append(new DiveObjectHelper(d));
}
endInsertRows();
diff --git a/qt-models/divelistmodel.h b/qt-models/divelistmodel.h
index 50b45caa9..37d3513bf 100644
--- a/qt-models/divelistmodel.h
+++ b/qt-models/divelistmodel.h
@@ -44,7 +44,7 @@ public:
static DiveListModel *instance();
DiveListModel(QObject *parent = 0);
- void addDive(QList<dive *> listOfDives);
+ void addDive(const QList<dive *> &listOfDives);
void addAllDives();
void insertDive(int i, DiveObjectHelper *newDive);
void removeDive(int i);
diff --git a/subsurface-helper.cpp b/subsurface-helper.cpp
index 786c2ab03..291a3e3bf 100644
--- a/subsurface-helper.cpp
+++ b/subsurface-helper.cpp
@@ -76,8 +76,8 @@ void run_ui()
// same directory the executable was started from <bundle>/Contents/MacOS/
// To work around this we need to manually copy the components at install time
// to Contents/Frameworks/qml and make sure that we add the correct import path
- QStringList importPathList = engine.importPathList();
- Q_FOREACH (QString importPath, importPathList) {
+ const QStringList importPathList = engine.importPathList();
+ for (QString importPath: importPathList) {
if (importPath.contains("MacOS"))
engine.addImportPath(importPath.replace("MacOS", "Frameworks"));
}