From 85934dee77526d5214c354646ececab3fb4a406a Mon Sep 17 00:00:00 2001 From: Martin Kleusberg Date: Tue, 18 Oct 2016 20:07:48 +0200 Subject: [PATCH] Simplify code This simplifies some of the super long iterator declarations by using the auto keyword from C++11. --- src/CreateIndexDialog.cpp | 4 ++-- src/DbStructureModel.cpp | 4 ++-- src/ExportSqlDialog.cpp | 4 ++-- src/ImportCsvDialog.cpp | 6 +++--- src/MainWindow.cpp | 17 ++++++++--------- src/Settings.cpp | 2 +- src/SqlUiLexer.cpp | 2 +- src/sqlitedb.cpp | 11 +++++------ src/sqlitetypes.cpp | 2 +- 9 files changed, 25 insertions(+), 27 deletions(-) diff --git a/src/CreateIndexDialog.cpp b/src/CreateIndexDialog.cpp index 92fd2129..755531e8 100644 --- a/src/CreateIndexDialog.cpp +++ b/src/CreateIndexDialog.cpp @@ -16,9 +16,9 @@ CreateIndexDialog::CreateIndexDialog(DBBrowserDB* db, QWidget* parent) // Get list of tables, sort it alphabetically and fill the combobox QMultiMap dbobjs; QList tables = pdb->objMap.values("table"); - for(QList::ConstIterator it=tables.begin();it!=tables.end();++it) + for(auto it=tables.constBegin();it!=tables.constEnd();++it) dbobjs.insert((*it).getname(), (*it)); - for(QMultiMap::ConstIterator it=dbobjs.begin(); it != dbobjs.end(); ++it) + for(auto it=dbobjs.constBegin();it!=dbobjs.constEnd();++it) ui->comboTableName->addItem(QIcon(QString(":icons/table")), (*it).getname()); QHeaderView *tableHeaderView = ui->tableIndexColumns->horizontalHeader(); diff --git a/src/DbStructureModel.cpp b/src/DbStructureModel.cpp index d1e8e84a..9aac8b9a 100644 --- a/src/DbStructureModel.cpp +++ b/src/DbStructureModel.cpp @@ -171,11 +171,11 @@ void DbStructureModel::reloadData() // Get all database objects and sort them by their name QMultiMap dbobjs; - for(objectMap::ConstIterator it=m_db.objMap.begin(); it != m_db.objMap.end(); ++it) + for(auto it=m_db.objMap.constBegin(); it != m_db.objMap.constEnd(); ++it) dbobjs.insert((*it).getname(), (*it)); // Add the actual table objects - for(QMultiMap::ConstIterator it=dbobjs.begin(); it != dbobjs.end(); ++it) + for(auto it=dbobjs.constBegin();it!=dbobjs.constEnd();++it) { // Object node QTreeWidgetItem* item = addNode(typeToParentItem.value((*it).gettype()), *it); diff --git a/src/ExportSqlDialog.cpp b/src/ExportSqlDialog.cpp index d983a39c..bba5c471 100644 --- a/src/ExportSqlDialog.cpp +++ b/src/ExportSqlDialog.cpp @@ -29,8 +29,8 @@ ExportSqlDialog::ExportSqlDialog(DBBrowserDB* db, QWidget* parent, const QString // Get list of tables to export objectMap objects = pdb->getBrowsableObjects(); - for(objectMap::ConstIterator i=objects.begin();i!=objects.end();++i) { - ui->listTables->addItem(new QListWidgetItem(QIcon(QString(":icons/%1").arg(i.value().gettype())), i.value().getname())); + for(auto it=objects.constBegin();it!=objects.constEnd();++it) { + ui->listTables->addItem(new QListWidgetItem(QIcon(QString(":icons/%1").arg(it.value().gettype())), it.value().getname())); } // Sort list of tables and select the table specified in the diff --git a/src/ImportCsvDialog.cpp b/src/ImportCsvDialog.cpp index 3caf326f..1afb997c 100644 --- a/src/ImportCsvDialog.cpp +++ b/src/ImportCsvDialog.cpp @@ -184,11 +184,11 @@ void ImportCsvDialog::accept() // Are we importing into an existing table? bool importToExistingTable = false; objectMap objects = pdb->getBrowsableObjects(); - for(objectMap::ConstIterator i=objects.begin();i!=objects.end();++i) + for(auto it=objects.constBegin();it!=objects.constEnd();++it) { - if(i.value().gettype() == "table" && i.value().getname() == ui->editName->text()) + if(it.value().gettype() == "table" && it.value().getname() == ui->editName->text()) { - if((size_t)i.value().table.fields().size() != csv.columns()) + if((size_t)it.value().table.fields().size() != csv.columns()) { QMessageBox::warning(this, QApplication::applicationName(), tr("There is already a table of that name and an import into an existing table is only possible if the number of columns match.")); diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 34f4841c..f714033d 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -365,7 +365,7 @@ void MainWindow::populateStructure() // Update table and column names for syntax highlighting objectMap tab = db.getBrowsableObjects(); SqlUiLexer::TablesAndColumnsMap tablesToColumnsMap; - for(objectMap::ConstIterator it=tab.begin(); it!=tab.end(); ++it) + for(auto it=tab.constBegin();it!=tab.constEnd();++it) { QString objectname = it.value().getname(); @@ -422,8 +422,7 @@ void MainWindow::populateTable() connect(ui->dataTable->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), this, SLOT(dataTableSelectionChanged(QModelIndex))); // Search stored table settings for this table - QMap::ConstIterator tableIt; - tableIt = browseTableSettings.constFind(tablename); + auto tableIt = browseTableSettings.constFind(tablename); bool storedDataFound = tableIt != browseTableSettings.constEnd(); // Set new table @@ -460,7 +459,7 @@ void MainWindow::populateTable() showRowidColumn(tableIt.value().showRowid); // Column widths - for(QMap::ConstIterator widthIt=tableIt.value().columnWidths.constBegin();widthIt!=tableIt.value().columnWidths.constEnd();++widthIt) + for(auto widthIt=tableIt.value().columnWidths.constBegin();widthIt!=tableIt.value().columnWidths.constEnd();++widthIt) ui->dataTable->setColumnWidth(widthIt.key(), widthIt.value()); // Sorting @@ -469,7 +468,7 @@ void MainWindow::populateTable() // Filters FilterTableHeader* filterHeader = qobject_cast(ui->dataTable->horizontalHeader()); - for(QMap::ConstIterator filterIt=tableIt.value().filterValues.constBegin();filterIt!=tableIt.value().filterValues.constEnd();++filterIt) + for(auto filterIt=tableIt.value().filterValues.constBegin();filterIt!=tableIt.value().filterValues.constEnd();++filterIt) filterHeader->setFilter(filterIt.key(), filterIt.value()); // Encoding @@ -1833,7 +1832,7 @@ void MainWindow::updatePlot(SqliteTableModel *model, bool update, bool keepOrRes sItemX = browseTableSettings[ui->comboBrowseTable->currentText()].plotXAxis; QMap axesY = browseTableSettings[ui->comboBrowseTable->currentText()].plotYAxes; - QMap::ConstIterator it = axesY.constBegin(); + auto it = axesY.constBegin(); while(it != axesY.constEnd()) { mapItemsY.insert(it.key(), it.value().colour); @@ -2508,7 +2507,7 @@ void MainWindow::on_comboLineType_currentIndexChanged(int index) // Save settings for this table QMap& graphs = browseTableSettings[ui->comboBrowseTable->currentText()].plotYAxes; - QMap::Iterator it = graphs.begin(); + auto it = graphs.begin(); while(it != graphs.end()) { it.value().lineStyle = lineStyle; @@ -2533,7 +2532,7 @@ void MainWindow::on_comboPointShape_currentIndexChanged(int index) // Save settings for this table QMap& graphs = browseTableSettings[ui->comboBrowseTable->currentText()].plotYAxes; - QMap::Iterator it = graphs.begin(); + auto it = graphs.begin(); while(it != graphs.end()) { it.value().pointShape = shape; @@ -2691,7 +2690,7 @@ void MainWindow::browseDataSetTableEncoding(bool forAllTables) { defaultBrowseTableEncoding = encoding; - for(QMap::Iterator it=browseTableSettings.begin();it!=browseTableSettings.end();++it) + for(auto it=browseTableSettings.begin();it!=browseTableSettings.end();++it) it.value().encoding = encoding; } } diff --git a/src/Settings.cpp b/src/Settings.cpp index e9b29cfc..b4a4f12d 100644 --- a/src/Settings.cpp +++ b/src/Settings.cpp @@ -11,7 +11,7 @@ QHash Settings::m_hCache; QVariant Settings::getSettingsValue(const QString& group, const QString& name) { // Have a look in the cache first - QHash::iterator cacheIndex = m_hCache.find(group + name); + auto cacheIndex = m_hCache.find(group + name); if(cacheIndex != m_hCache.end()) { return cacheIndex.value(); diff --git a/src/SqlUiLexer.cpp b/src/SqlUiLexer.cpp index 4cf145af..1908bec8 100644 --- a/src/SqlUiLexer.cpp +++ b/src/SqlUiLexer.cpp @@ -132,7 +132,7 @@ void SqlUiLexer::setTableNames(const TablesAndColumnsMap& tables) autocompleteApi->clear(); listTables.clear(); setupAutoCompletion(); - for(TablesAndColumnsMap::ConstIterator it=tables.constBegin();it!=tables.constEnd();++it) + for(auto it=tables.constBegin();it!=tables.constEnd();++it) { foreach(const QString& field, it.value()) autocompleteApi->add(it.key() + "?" + QString::number(SqlUiLexer::ApiCompleterIconIdTable) + "." + diff --git a/src/sqlitedb.cpp b/src/sqlitedb.cpp index 934cb85d..7203eb75 100644 --- a/src/sqlitedb.cpp +++ b/src/sqlitedb.cpp @@ -475,7 +475,7 @@ bool DBBrowserDB::dump(const QString& filename, stream << "BEGIN TRANSACTION;\n"; // Loop through all tables first as they are required to generate views, indices etc. later - for(QList::ConstIterator it=tables.begin();it!=tables.end();++it) + for(auto it=tables.constBegin();it!=tables.constEnd();++it) { if (tablesToDump.indexOf(it->getTableName()) == -1) continue; @@ -577,7 +577,7 @@ bool DBBrowserDB::dump(const QString& filename, // Now dump all the other objects (but only if we are exporting the schema) if(exportSchema) { - for(objectMap::ConstIterator it=objMap.begin();it!=objMap.end();++it) + for(auto it=objMap.constBegin();it!=objMap.constEnd();++it) { // Make sure it's not a table again if(it.value().gettype() == "table") @@ -1048,7 +1048,7 @@ bool DBBrowserDB::renameColumn(const QString& tablename, const QString& name, sq // Save all indices, triggers and views associated with this table because SQLite deletes them when we drop the table in the next step QString otherObjectsSql; - for(objectMap::ConstIterator it=objMap.begin();it!=objMap.end();++it) + for(auto it=objMap.constBegin();it!=objMap.constEnd();++it) { // If this object references the table and it's not the table itself save it's SQL string if((*it).getTableName() == tablename && (*it).gettype() != "table") @@ -1122,10 +1122,9 @@ bool DBBrowserDB::renameTable(const QString& from_table, const QString& to_table objectMap DBBrowserDB::getBrowsableObjects() const { - objectMap::ConstIterator it; objectMap res; - for(it=objMap.begin();it!=objMap.end();++it) + for(auto it=objMap.constBegin();it!=objMap.constEnd();++it) { if(it.key() == "table" || it.key() == "view") res.insert(it.key(), it.value()); @@ -1136,7 +1135,7 @@ objectMap DBBrowserDB::getBrowsableObjects() const DBBrowserObject DBBrowserDB::getObjectByName(const QString& name) const { - for (objectMap::ConstIterator it = objMap.begin(); it != objMap.end(); ++it ) + for(auto it=objMap.constBegin();it!=objMap.constEnd();++it) { if((*it).getname() == name) return *it; diff --git a/src/sqlitetypes.cpp b/src/sqlitetypes.cpp index 1a18d066..e517eb4d 100644 --- a/src/sqlitetypes.cpp +++ b/src/sqlitetypes.cpp @@ -355,7 +355,7 @@ FieldVector& Table::primaryKeyRef() const FieldVector& Table::primaryKey() const { - ConstraintMap::ConstIterator it = m_constraints.constBegin(); + auto it = m_constraints.constBegin(); while(it != m_constraints.constEnd()) { if(it.value()->type() == Constraint::PrimaryKeyConstraintType)