Replace 'foreach' by range-based for loop from C++11 · GyPapi/sqlitebrowser@3bd2dc3 · GitHub
Skip to content

Commit 3bd2dc3

Browse files
committed
Replace 'foreach' by range-based for loop from C++11
1 parent ac70855 commit 3bd2dc3

20 files changed

Lines changed: 73 additions & 72 deletions

‎src/Application.cpp‎

Lines changed: 1 addition & 1 deletion

‎src/DbStructureModel.cpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ QMimeData* DbStructureModel::mimeData(const QModelIndexList& indices) const
192192
{
193193
// Loop through selected indices
194194
QByteArray d;
195-
foreach(QModelIndex index, indices)
195+
for(const QModelIndex& index : indices)
196196
{
197197
// Only export data for valid indices and only for the SQL column, i.e. only once per row
198198
if(index.isValid() && index.column() == ColumnSQL)
@@ -306,11 +306,11 @@ void DbStructureModel::buildTree(QTreeWidgetItem* parent, const QString& schema)
306306
if((*it)->type() == sqlb::Object::Types::Table)
307307
{
308308
sqlb::FieldVector pk = (*it).dynamicCast<sqlb::Table>()->primaryKey();
309-
foreach(sqlb::FieldPtr pk_col, pk)
309+
for(const sqlb::FieldPtr& pk_col : pk)
310310
pk_columns.push_back(pk_col->name());
311311

312312
}
313-
foreach(const sqlb::FieldInfo& field, fieldList)
313+
for(const sqlb::FieldInfo& field : fieldList)
314314
{
315315
QTreeWidgetItem *fldItem = new QTreeWidgetItem(item);
316316
fldItem->setText(ColumnName, field.name);

‎src/EditDialog.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ QString EditDialog::humanReadableSize(double byteCount) const
525525
QStringList units;
526526
units << "" << "Ki" << "Mi" << "Gi" << "Ti" << "Pi" << "Ei" << "Zi";
527527

528-
foreach(const QString& unit, units)
528+
for(const QString& unit : units)
529529
{
530530
if(fabs(byteCount) < 1024.0)
531531
{

‎src/EditTableDialog.cpp‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ void EditTableDialog::populateFields()
105105
ui->treeWidget->clear();
106106
sqlb::FieldVector fields = m_table.fields();
107107
sqlb::FieldVector pk = m_table.primaryKey();
108-
foreach(sqlb::FieldPtr f, fields)
108+
for(const sqlb::FieldPtr& f : fields)
109109
{
110110
QTreeWidgetItem *tbitem = new QTreeWidgetItem(ui->treeWidget);
111111
tbitem->setFlags(tbitem->flags() | Qt::ItemIsEditable);
@@ -211,7 +211,7 @@ void EditTableDialog::checkInput()
211211

212212
// update fk's that refer to table itself recursively
213213
sqlb::FieldVector fields = m_table.fields();
214-
foreach(sqlb::FieldPtr f, fields) {
214+
for(const sqlb::FieldPtr& f : fields) {
215215
QSharedPointer<sqlb::ForeignKeyClause> fk = m_table.constraint({f}, sqlb::Constraint::ForeignKeyConstraintType).dynamicCast<sqlb::ForeignKeyClause>();
216216
if(!fk.isNull()) {
217217
if (oldTableName == fk->table()) {
@@ -300,10 +300,10 @@ void EditTableDialog::itemChanged(QTreeWidgetItem *item, int column)
300300
if(!m_bNewTable)
301301
{
302302
sqlb::FieldVector pk = m_table.primaryKey();
303-
foreach(const sqlb::ObjectPtr& fkobj, pdb.schemata[curTable.schema()].values("table"))
303+
for(const sqlb::ObjectPtr& fkobj : pdb.schemata[curTable.schema()].values("table"))
304304
{
305305
QList<sqlb::ConstraintPtr> fks = fkobj.dynamicCast<sqlb::Table>()->constraints(sqlb::FieldVector(), sqlb::Constraint::ForeignKeyConstraintType);
306-
foreach(sqlb::ConstraintPtr fkptr, fks)
306+
for(const sqlb::ConstraintPtr& fkptr : fks)
307307
{
308308
QSharedPointer<sqlb::ForeignKeyClause> fk = fkptr.dynamicCast<sqlb::ForeignKeyClause>();
309309
if(fk->table() == m_table.name())

‎src/ExportDataDialog.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ void ExportDataDialog::accept()
324324
return;
325325
}
326326

327-
foreach(QListWidgetItem* item, selectedItems)
327+
for(const QListWidgetItem* item : selectedItems)
328328
filenames << QDir(exportfolder).filePath(item->text() + default_file_extension);
329329
}
330330

‎src/ExportSqlDialog.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ void ExportSqlDialog::accept()
105105
settings.endGroup();
106106

107107
QStringList tables;
108-
foreach (const QListWidgetItem * item, ui->listTables->selectedItems())
108+
for(const QListWidgetItem* item : ui->listTables->selectedItems())
109109
tables.push_back(item->text());
110110

111111
// Check what to export. The indices here depend on the order of the items in the combobox in the ui file

‎src/ExtendedTableWidget.cpp‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ ExtendedTableWidget::ExtendedTableWidget(QWidget* parent) :
109109
useAsFilter();
110110
});
111111
connect(nullAction, &QAction::triggered, [&]() {
112-
foreach(const QModelIndex& index, selectedIndexes())
112+
for(const QModelIndex& index : selectedIndexes())
113113
model()->setData(index, QVariant());
114114
});
115115
connect(copyAction, &QAction::triggered, [&]() {
@@ -172,11 +172,13 @@ void ExtendedTableWidget::copy()
172172

173173
// If any of the cells contain binary data - we use inner buffer
174174
bool containsBinary = false;
175-
foreach (const QModelIndex& index, indices)
175+
for(const QModelIndex& index : indices)
176+
{
176177
if (m->isBinary(index)) {
177178
containsBinary = true;
178179
break;
179180
}
181+
}
180182

181183
if (containsBinary) {
182184
qApp->clipboard()->clear();
@@ -198,7 +200,7 @@ void ExtendedTableWidget::copy()
198200
QString result;
199201
int currentRow = 0;
200202

201-
foreach(const QModelIndex& index, indices) {
203+
for(const QModelIndex& index : indices) {
202204
if (first == index) { /* first index */ }
203205
else if (index.row() != currentRow)
204206
result.append("\r\n");
@@ -259,9 +261,9 @@ void ExtendedTableWidget::paste()
259261

260262
int row = firstRow;
261263

262-
foreach(const QByteArrayList& lst, m_buffer) {
264+
for(const QByteArrayList& lst : m_buffer) {
263265
int column = firstColumn;
264-
foreach(const QByteArray& ba, lst) {
266+
for(const QByteArray& ba : lst) {
265267
m->setData(m->index(row, column), ba);
266268

267269
column++;
@@ -315,10 +317,10 @@ void ExtendedTableWidget::paste()
315317
int lastColumn = qMin(firstColumn + clipboardColumns - 1, m->columnCount() - 1);
316318

317319
int row = firstRow;
318-
foreach(const QStringList& clipboardRow, clipboardTable)
320+
for(const QStringList& clipboardRow : clipboardTable)
319321
{
320322
int column = firstColumn;
321-
foreach(const QString& cell, clipboardRow)
323+
for(const QString& cell : clipboardRow)
322324
{
323325
if (cell.isEmpty())
324326
m->setData(m->index(row, column), QVariant());
@@ -383,11 +385,11 @@ void ExtendedTableWidget::keyPressEvent(QKeyEvent* event)
383385
if(event->modifiers().testFlag(Qt::AltModifier))
384386
{
385387
// When pressing Alt+Delete set the value to NULL
386-
foreach(const QModelIndex& index, selectedIndexes())
388+
for(const QModelIndex& index : selectedIndexes())
387389
model()->setData(index, QVariant());
388390
} else {
389391
// When pressing Delete only set the value to empty string
390-
foreach(const QModelIndex& index, selectedIndexes())
392+
for(const QModelIndex& index : selectedIndexes())
391393
model()->setData(index, "");
392394
}
393395
} else if(event->modifiers().testFlag(Qt::ControlModifier) && (event->key() == Qt::Key_PageUp || event->key() == Qt::Key_PageDown)) {
@@ -442,7 +444,7 @@ int ExtendedTableWidget::numVisibleRows()
442444
QSet<int> ExtendedTableWidget::selectedCols()
443445
{
444446
QSet<int> selectedCols;
445-
foreach(const QModelIndex & idx, selectedIndexes())
447+
for(const QModelIndex & idx : selectedIndexes())
446448
selectedCols.insert(idx.column());
447449
return selectedCols;
448450
}

‎src/FilterTableHeader.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ void FilterTableHeader::inputChanged(const QString& new_value)
8888

8989
void FilterTableHeader::clearFilters()
9090
{
91-
foreach (FilterLineEdit* filterLineEdit, filterWidgets)
91+
for(FilterLineEdit* filterLineEdit : filterWidgets)
9292
filterLineEdit->clear();
9393
}
9494

‎src/ImportCsvDialog.cpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ ImportCsvDialog::ImportCsvDialog(const QStringList &filenames, DBBrowserDB* db,
4040

4141
// Create a list of all available encodings and create an auto completion list from them
4242
QStringList encodingList;
43-
foreach(QString enc, QTextCodec::availableCodecs())
43+
for(const QString& enc : QTextCodec::availableCodecs())
4444
encodingList.push_back(enc);
4545
encodingCompleter = new QCompleter(encodingList, this);
4646
encodingCompleter->setCaseSensitivity(Qt::CaseInsensitive);
@@ -237,7 +237,7 @@ void ImportCsvDialog::updatePreview()
237237

238238
// Set horizontal header data
239239
QStringList horizontalHeader;
240-
foreach(const sqlb::FieldPtr& field, fieldList)
240+
for(const sqlb::FieldPtr& field : fieldList)
241241
horizontalHeader.push_back(field->name());
242242
ui->tablePreview->setHorizontalHeaderLabels(horizontalHeader);
243243

@@ -557,7 +557,7 @@ bool ImportCsvDialog::importCsv(const QString& fileName, const QString& name)
557557
sqlb::TablePtr tbl = pdb->getObjectByName(sqlb::ObjectIdentifier("main", tableName)).dynamicCast<sqlb::Table>();
558558
if(tbl)
559559
{
560-
foreach(const sqlb::FieldPtr& f, tbl->fields())
560+
for(const sqlb::FieldPtr& f : tbl->fields())
561561
{
562562
if(f->isInteger() && f->notnull()) // If this is an integer column but NULL isn't allowed, insert 0
563563
nullValues << "0";

‎src/MainWindow.cpp‎

Lines changed: 6 additions & 6 deletions

0 commit comments

Comments
 (0)