Dobra Bracia programiści @MarekR22 i @tajny_agent, rzecz wygląda następująco - tak wygląda instancja MechDesignWidget:
a tak jest zaprojektowany w Designerze:
Te nazwy w QGroupBoxach + drabinka rysowana na biało + przycisk "Remove stuff" to właśnie jest instancja MechPartWidget, który w Designerze jest zdefiniowany następująco:
owe QLabele 10/10 + ikona są właśnie przedmiotem makra/lambdy - jeśli w strukturze LocationData pokazanej niżej któryś typ hardpointów jest 0 to wtedy odpowiadające mu 3 widgety mają zostać schowane.
Kod kostruktora MechDesignWidget:
MechDesignWidget::MechDesignWidget(const QString &variantName, QWidget *parent)
: QWidget(parent), ui(new Ui::MechDesign)
{
ui->setupUi(this);
//tak, mam świadomość że tu następuje silne powiązanie MechDesignWidget z MechsDatabase które da się uniknąć przesłaniem MechData jako parametr konstruktora
mech = MechsDatabase::getInstance().getMechData(variantName);
//dla wygody definicja struktury MechData która oczywiście siedzi gdzie indziej
struct MechData
{
struct LocationData
{
LocationData(const QString &name, int size): locationName(name), inventorySlotsCount(size)
{}
LocationData() = default;
QString locationName;
int inventorySlotsCount;
int energyHardpoints = 0;
int ballisticHardpoints = 0;
int missileHardpoints = 0;
int supportHardpoints = 0;
QVector<const InventoryItem*> inventory;
};
QString name;
QString variantName;
QString weighClass;
QString hardpointsAndJumpJetsInfo;
int totalTonnage;
int maxJumpJets;
QVector<LocationData> locations;
};
//setup mech parts - its name,hardpoints number and slots inventory size
foreach (const MechData::LocationData &location, mech.locations)
{
MechPartWidget *mechPartWidget;
if (location.locationName.compare("Head", Qt::CaseInsensitive) == 0)
{
mechPartWidget = ui->head;
}
else if (location.locationName.compare("LeftArm", Qt::CaseInsensitive) == 0)
{
mechPartWidget = ui->leftArm;
}
else if (location.locationName.compare("LeftTorso", Qt::CaseInsensitive) == 0)
{
mechPartWidget = ui->leftTorso;
}
else if (location.locationName.compare("LeftLeg", Qt::CaseInsensitive) == 0)
{
mechPartWidget = ui->leftLeg;
}
else if (location.locationName.compare("CenterTorso", Qt::CaseInsensitive) == 0)
{
mechPartWidget = ui->centralTorso;
}
else if (location.locationName.compare("RightTorso", Qt::CaseInsensitive) == 0)
{
mechPartWidget = ui->rightTorso;
}
else if (location.locationName.compare("RightArm", Qt::CaseInsensitive) == 0)
{
mechPartWidget = ui->rightArm;
}
else// if (location.locationName.compare("RightLeg", Qt::CaseInsensitive) == 0)
{
mechPartWidget = ui->rightLeg;
}
mechPartWidget->setupMechPart(location);
//po staremu było coś w stylu
//mechPartWidget->setPartName(location.locationName);
//mechPartWidget->setPartName(location.inventorySlotsCount);
//mechPartWidget->setupHardpoints(location.energyHardpoints, location.ballisticHardpoints, location.missileHardpoints, location.supportHardpoints
//mechPartWidget->set
}
}
i teraz MechPartWidget::setupMechPart wygląda tak
void MechPartWidget::setupMechPart(const MechData::LocationData &locationData)
{
//set name of the part
QString name = locationData.locationName;
//Add space between left/right/center and part name for nicer look in GUI
name = name.replace("Right", "Right ").replace("Left", "Left ").replace("Center", "Center ");
ui->groupBox->setTitle(name.toUpper());
//set size of inventory
ui->inventory->setSlotsCount(locationData.inventorySlotsCount);
//remember number of hardpoints
hardpointsCount[HardpointTypes::Ballistic] = locationData.ballisticHardpoints;
hardpointsCount[HardpointTypes::Missile] = locationData.missileHardpoints;
hardpointsCount[HardpointTypes::Energy] = locationData.energyHardpoints;
hardpointsCount[HardpointTypes::Antipersonnel] = locationData.supportHardpoints;
auto setupHardpoint = [](int count, QLabel *left, QLabel *icon, QLabel *max)->void
{
if (count == 0)
{
left->hide();
max->hide();
icon->hide();
}
else
{
QString n(QString::number(count));
left->setText(n);
max->setText('/' + n);
}
};
//setup hardpoint-related GUI elements
setupHardpoint(locationData.ballisticHardpoints, ui->labelBallisticHardpointsLeft, ui->labelBallisticIcon, ui->labelMaxBallisticHardpoints);
setupHardpoint(locationData.missileHardpoints, ui->labelMissileHardpointsLeft, ui->labelMissileIcon, ui->labelMaxMissileHardpoints);
setupHardpoint(locationData.energyHardpoints, ui->labelEnergyHardpointsLeft, ui->labelEnergyIcon, ui->labelMaxEnergyHardpoints);
setupHardpoint(locationData.supportHardpoints, ui->labelSupportHardpointsLeft, ui->labelSupportIcon, ui->labelMaxSupportHardpoints);
//setup inventory
ui->inventory->setupInventory(locationData.inventory);
}
przedtem miałem 3 różne funkcje MechPartWidget::setName(), setSlotsCount() i właśnie setupHardpoints() które były wywoływane w konstruktorze MechDesignWidget, ale zrefaktorowałem to na obecną postać.