88#include < QMessageBox>
99#include < QApplication>
1010#include < unordered_map>
11+ #include < map>
1112
1213DbStructureModel::DbStructureModel (DBBrowserDB& db, QObject* parent)
1314 : QAbstractItemModel(parent),
@@ -170,7 +171,7 @@ void DbStructureModel::reloadData()
170171 buildTree (itemAll, " main" );
171172
172173 // Add the temporary database as a node if it isn't empty. Make sure it's always second if it exists.
173- if (!m_db.schemata [" temp" ].isEmpty ())
174+ if (!m_db.schemata [" temp" ].empty ())
174175 {
175176 QTreeWidgetItem* itemTemp = new QTreeWidgetItem (itemAll);
176177 itemTemp->setIcon (ColumnName, QIcon (QString (" :/icons/database" )));
@@ -180,16 +181,16 @@ void DbStructureModel::reloadData()
180181 }
181182
182183 // Now load all the other schemata last
183- for (auto it= m_db.schemata . constBegin ();it!=m_db. schemata . constEnd ();++it )
184+ for (const auto & it : m_db.schemata )
184185 {
185186 // Don't load the main and temp schema again
186- if (it.key () != " main" && it.key () != " temp" )
187+ if (it.first != " main" && it.first != " temp" )
187188 {
188189 QTreeWidgetItem* itemSchema = new QTreeWidgetItem (itemAll);
189190 itemSchema->setIcon (ColumnName, QIcon (QString (" :/icons/database" )));
190- itemSchema->setText (ColumnName, QString::fromStdString (it.key () ));
191+ itemSchema->setText (ColumnName, QString::fromStdString (it.first ));
191192 itemSchema->setText (ColumnObjectType, " database" );
192- buildTree (itemSchema, it.key () );
193+ buildTree (itemSchema, it.first );
193194 }
194195 }
195196
@@ -301,6 +302,15 @@ bool DbStructureModel::dropMimeData(const QMimeData* data, Qt::DropAction action
301302 }
302303}
303304
305+ static long calc_number_of_objects_by_type (const objectMap& objmap, const std::string& type)
306+ {
307+ auto objects = objmap.equal_range (type);
308+ if (objects.first == objmap.end ())
309+ return 0 ;
310+ else
311+ return std::distance (objects.first , objects.second ) + 1 ;
312+ }
313+
304314void DbStructureModel::buildTree (QTreeWidgetItem* parent, const std::string& schema)
305315{
306316 // Build a map from object type to tree node to simplify finding the correct tree node later
@@ -312,32 +322,34 @@ void DbStructureModel::buildTree(QTreeWidgetItem* parent, const std::string& sch
312322 // Prepare tree
313323 QTreeWidgetItem* itemTables = new QTreeWidgetItem (parent);
314324 itemTables->setIcon (ColumnName, QIcon (QString (" :/icons/table" )));
315- itemTables->setText (ColumnName, tr (" Tables (%1)" ).arg (objmap. values ( " table" ). count ( )));
325+ itemTables->setText (ColumnName, tr (" Tables (%1)" ).arg (calc_number_of_objects_by_type (objmap, " table" )));
316326 typeToParentItem.insert ({" table" , itemTables});
317327
318328 QTreeWidgetItem* itemIndices = new QTreeWidgetItem (parent);
319329 itemIndices->setIcon (ColumnName, QIcon (QString (" :/icons/index" )));
320- itemIndices->setText (ColumnName, tr (" Indices (%1)" ).arg (objmap. values ( " index" ). count ( )));
330+ itemIndices->setText (ColumnName, tr (" Indices (%1)" ).arg (calc_number_of_objects_by_type (objmap, " index" )));
321331 typeToParentItem.insert ({" index" , itemIndices});
322332
323333 QTreeWidgetItem* itemViews = new QTreeWidgetItem (parent);
324334 itemViews->setIcon (ColumnName, QIcon (QString (" :/icons/view" )));
325- itemViews->setText (ColumnName, tr (" Views (%1)" ).arg (objmap. values ( " view" ). count ( )));
335+ itemViews->setText (ColumnName, tr (" Views (%1)" ).arg (calc_number_of_objects_by_type (objmap, " view" )));
326336 typeToParentItem.insert ({" view" , itemViews});
327337
328338 QTreeWidgetItem* itemTriggers = new QTreeWidgetItem (parent);
329339 itemTriggers->setIcon (ColumnName, QIcon (QString (" :/icons/trigger" )));
330- itemTriggers->setText (ColumnName, tr (" Triggers (%1)" ).arg (objmap. values ( " trigger" ). count ( )));
340+ itemTriggers->setText (ColumnName, tr (" Triggers (%1)" ).arg (calc_number_of_objects_by_type (objmap, " trigger" )));
331341 typeToParentItem.insert ({" trigger" , itemTriggers});
332342
333343 // Get all database objects and sort them by their name
334- QMultiMap <std::string, sqlb::ObjectPtr> dbobjs;
335- for (auto it : objmap)
336- dbobjs.insert (it ->name (), it);
344+ std::map <std::string, sqlb::ObjectPtr> dbobjs;
345+ for (const auto & it : objmap)
346+ dbobjs.insert ({it. second ->name (), it. second } );
337347
338348 // Add the database objects to the tree nodes
339- for (auto it : dbobjs)
349+ for (const auto & obj : dbobjs)
340350 {
351+ sqlb::ObjectPtr it = obj.second ;
352+
341353 // Object node
342354 QTreeWidgetItem* item = addNode (typeToParentItem.at (sqlb::Object::typeToString (it->type ())), it, schema);
343355
0 commit comments