Skip to content

Commit

Permalink
Handle orbitingStars when filtering in Qt Solar System Browser
Browse files Browse the repository at this point in the history
  • Loading branch information
ajtribick committed Jan 14, 2024
1 parent bf52fa7 commit f210f99
Showing 1 changed file with 43 additions and 24 deletions.
67 changes: 43 additions & 24 deletions src/celestia/qt/qtsolarsystembrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,13 @@ class SolarSystemBrowser::SolarSystemTreeModel : public QAbstractItemModel, publ

TreeItem* createTreeItem(Selection sel, TreeItem* parent, int childIndex);
void addTreeItemChildren(TreeItem* item,
PlanetarySystem* sys,
const PlanetarySystem* sys,
const std::vector<Star*>* orbitingStars);
void addTreeItemChildrenFiltered(TreeItem* item,
PlanetarySystem* sys);
const PlanetarySystem* sys,
const std::vector<Star*>* orbitingStars);
void addTreeItemChildrenGrouped(TreeItem* item,
PlanetarySystem* sys,
const PlanetarySystem* sys,
const std::vector<Star*>* orbitingStars,
Selection parent);
TreeItem* createGroupTreeItem(int classification,
Expand Down Expand Up @@ -256,7 +257,7 @@ SolarSystemBrowser::SolarSystemTreeModel::createTreeItem(Selection sel,

const std::vector<Star*>* orbitingStars = nullptr;

PlanetarySystem* sys = nullptr;
const PlanetarySystem* sys = nullptr;
if (sel.body() != nullptr)
{
sys = sel.body()->getSatellites();
Expand All @@ -277,7 +278,7 @@ SolarSystemBrowser::SolarSystemTreeModel::createTreeItem(Selection sel,
if (groupByClass && sys != nullptr)
addTreeItemChildrenGrouped(item, sys, orbitingStars, sel);
else if (bodyFilter != 0 && sys != nullptr)
addTreeItemChildrenFiltered(item, sys);
addTreeItemChildrenFiltered(item, sys, orbitingStars);
else
addTreeItemChildren(item, sys, orbitingStars);

Expand All @@ -287,7 +288,7 @@ SolarSystemBrowser::SolarSystemTreeModel::createTreeItem(Selection sel,

void
SolarSystemBrowser::SolarSystemTreeModel::addTreeItemChildren(TreeItem* item,
PlanetarySystem* sys,
const PlanetarySystem* sys,
const std::vector<Star*>* orbitingStars)
{
// Calculate the number of children: the number of orbiting stars plus
Expand Down Expand Up @@ -330,7 +331,8 @@ SolarSystemBrowser::SolarSystemTreeModel::addTreeItemChildren(TreeItem* item,

void
SolarSystemBrowser::SolarSystemTreeModel::addTreeItemChildrenFiltered(TreeItem* item,
PlanetarySystem* sys)
const PlanetarySystem* sys,
const std::vector<Star*>* orbitingStars)
{
std::vector<Body*> bodies;

Expand All @@ -343,14 +345,32 @@ SolarSystemBrowser::SolarSystemTreeModel::addTreeItemChildrenFiltered(TreeItem*

// Calculate the total number of children
// WARN: max(size_t) > max(int) so in theory it's possible to have a negative value
if ((item->nChildren = static_cast<int>(bodies.size())) <= 0)
item->nChildren = static_cast<int>(bodies.size());
if (orbitingStars != nullptr)
item->nChildren += static_cast<int>(orbitingStars->size());

if (item->nChildren == 0)
return;

item->children = new TreeItem*[item->nChildren];

// Add the orbiting stars
int childIndex = 0;
if (orbitingStars != nullptr)
{
for (Star* star : *orbitingStars)
{
item->children[childIndex] = createTreeItem(star, item, childIndex);
++childIndex;
}
}

// Add the direct children
for (int i = 0; i < (int) bodies.size(); i++)
item->children[i] = createTreeItem(bodies[i], item, i);
for (Body* body : bodies)
{
item->children[childIndex] = createTreeItem(body, item, childIndex);
++childIndex;
}
}


Expand All @@ -361,7 +381,7 @@ SolarSystemBrowser::SolarSystemTreeModel::addTreeItemChildrenFiltered(TreeItem*
// large collections of such objects.
void
SolarSystemBrowser::SolarSystemTreeModel::addTreeItemChildrenGrouped(TreeItem* item,
PlanetarySystem* sys,
const PlanetarySystem* sys,
const std::vector<Star*>* orbitingStars,
Selection parent)
{
Expand Down Expand Up @@ -460,19 +480,18 @@ SolarSystemBrowser::SolarSystemTreeModel::addTreeItemChildrenGrouped(TreeItem* i
// Add the stars
if (orbitingStars != nullptr)
{
for (unsigned int i = 0; i < orbitingStars->size(); i++)
for (Star* star : *orbitingStars)
{
Selection child(orbitingStars->at(i));
item->children[childIndex] = createTreeItem(child, item, childIndex);
childIndex++;
item->children[childIndex] = createTreeItem(star, item, childIndex);
++childIndex;
}
}

// Add the direct children
for (int i = 0; i < (int) normal.size(); i++)
for (Body* body : normal)
{
item->children[childIndex] = createTreeItem(normal[i], item, childIndex);
childIndex++;
item->children[childIndex] = createTreeItem(body, item, childIndex);
++childIndex;
}

// Add the groups
Expand All @@ -481,47 +500,47 @@ SolarSystemBrowser::SolarSystemTreeModel::addTreeItemChildrenGrouped(TreeItem* i
item->children[childIndex] = createGroupTreeItem(Body::MinorMoon,
minorMoons,
item, childIndex);
childIndex++;
++childIndex;
}

if (!asteroids.empty())
{
item->children[childIndex] = createGroupTreeItem(Body::Asteroid,
asteroids,
item, childIndex);
childIndex++;
++childIndex;
}

if (!spacecraft.empty())
{
item->children[childIndex] = createGroupTreeItem(Body::Spacecraft,
spacecraft,
item, childIndex);
childIndex++;
++childIndex;
}

if (!surfaceFeatures.empty())
{
item->children[childIndex] = createGroupTreeItem(Body::SurfaceFeature,
surfaceFeatures,
item, childIndex);
childIndex++;
++childIndex;
}

if (!components.empty())
{
item->children[childIndex] = createGroupTreeItem(Body::Component,
components,
item, childIndex);
childIndex++;
++childIndex;
}

if (!other.empty())
{
item->children[childIndex] = createGroupTreeItem(Body::Unknown,
other,
item, childIndex);
childIndex++;
++childIndex;
}
}
}
Expand Down

0 comments on commit f210f99

Please sign in to comment.