У меня проблема с логической структурой моего проекта:
class AbstractItem : public QObject{
public:
AbstractItem() {}
int id;
QString name;
};
class BiggestItem : public AbstractItem
{
public:
BiggestItem() {}
//Item can contains a lot of specific parameters
int specificForBiggest1;
//Item must contains lists of smaller items and groups
QList<BigItem *> m_bigItems;
QList<Group *> m_bigItemGroups;
};
class BigItem : public AbstractItem
{
public:
BigItem() {}
//Item can contains a lot of specific parameters
int specificForBig1;
//Item must contains lists of smaller items and groups
QList<MediumItem *> m_mediumItems;
QList<Group *> m_mediumItemGroups;
signals:
void mediumItemsChanged(QList<MediumItem *> mediumItems);
public slots:
void setMediumItems(const QList<MediumItem *> &mediumItems)
{
if(m_mediumItems == mediumItems)
return;
m_mediumItems = mediumItems;
emit mediumItemsChanged(m_mediumItems);
}
};
class MediumItem : public AbstractItem
{
public:
MediumItem() {}
//Item can contains a lot of specific parameters
int specificForMedium1;
QList<SmallItemType1 *> m_smallItemsType1;
QList<SmallItemType2 *> m_smallItemsType2;
QList<Group *> m_smallItemsType1Groups;
QList<Group *> m_smallItemsType2Groups;
signals:
void smallItemsType1Changed(QList<SmallItemType1 *> smallItemsType1);
void smallItemsType2Changed(QList<SmallItemType2 *> smallItemsType2);
public slots:
void setSmallItemsType1(const QList<SmallItemType1 *> &smallItemsType1)
{
if(m_smallItemsType1 == smallItemsType1)
return;
m_smallItemsType1 = smallItemsType1;
emit smallItemsType1Changed(m_smallItemsType1);
}
void setSmallItemsType2(const QList<SmallItemType2 *> &smallItemsType2)
{
if(m_smallItemsType2 == smallItemsType2)
return;
m_smallItemsType2 = smallItemsType2;
emit smallItemsType2Changed(m_smallItemsType2);
}
};
class AbstractSmallItem : public AbstractItem
{
public:
AbstractSmallItem() {}
int param1;
int param2;
//... a lot of parameters
int param99;
signals:
void param1Changed(int param1);
void param2Changed(int param2);
//...
void param99Changed(int param99);
};
class SmallItemType1 : public AbstractSmallItem
{
public:
SmallItemType1() {
param1 = 0;
param2 = 10;
//... a lot of default values
param99 = 123;
}
};
class SmallItemType2 : public AbstractSmallItem
{
public:
SmallItemType2() {
param1 = 35;
param2 = 15;
//... a lot of default values
param99 = 67;
}
};
class Group
{
public:
Group() {}
QList<AbstractItem *> itemList;
AbstractItem * m_masterItem;
void addItem(AbstractItem *i){
if(itemList.isEmpty())
{
auto isBig = qobject_cast<BigItem*>(i);
if (isBig != 0) {
m_masterItem = new BigItem();
auto mI = qobject_cast<BigItem*>(m_masterItem);
mI->m_mediumItems.append(isBig->m_mediumItems);
//1 parameters - 2 connections
QObject::connect(mI,&BigItem::mediumItemsChanged,
isBig,&BigItem::setMediumItems);
QObject::connect(isBig,&BigItem::mediumItemsChanged,
mI,&BigItem::setMediumItems);
}
auto isMedium = qobject_cast<MediumItem*>(i);
if (isMedium != 0) {
m_masterItem = new MediumItem();
auto mI = qobject_cast<MediumItem*>(m_masterItem);
mI->m_smallItemsType1.append(isMedium->m_smallItemsType1);
mI->m_smallItemsType2.append(isMedium->m_smallItemsType2);
//****Connect signals and slots
//2 parameters - 4 connections
//****
}
auto isSmallType1 = qobject_cast<SmallItemType1*>(i);
if (isSmallType1 != 0) {
m_masterItem = new SmallItemType1();
//Cast masterItem and connect signals and slots
//but a lot of parameters need to a lot^2 connections
}
auto isSmallType2 = qobject_cast<SmallItemType2*>(i);
if (isSmallType2 != 0) {
m_masterItem = new SmallItemType2();
//Cast masterItem and connect signals and slots
//but a lot of parameters need to a lot^2 connections
}
}
itemList.append(i);
}
void removeItem(AbstractItem *i){
if(itemList.contains(i))
itemList.removeOne(i);
}
};
Какой путь лучше?
- Создать все чисто виртуальные методы класса Items в классе abstractItem и не использовать qobject_cast?
- Создать некоторый класс Group (BigItemGroup, MediumItemGroup и т. Д.), Унаследованный от AbstractGroup, реализовать методы и не использовать use qobject_cast?
- Использовать шаблонный класс, если это возможно?
- Что-то еще?
Я в тупике. Thanx!