1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
CStandardEtatModel::CStandardEtatModel(CDomEtats *ptDomEtats)
{
m_ptDomEtats = ptDomEtats;
m_ptRootEtat = new CStandardEtat();
if (m_ptRootEtat != 0) {
m_ptRootVisible = new CStandardEtat(m_ptRootEtat);
if (m_ptRootVisible != 0) {
m_ptRootVisible->setupEtat(QString("Livre des états"), Root);
m_ptRootEtat->appendChild(m_ptRootVisible);
setupModel(m_ptRootVisible);
}
}
}
QModelIndex CStandardEtatModel::rootIndex()
{
return createIndex (0, 0, m_ptRootVisible);
}
void CStandardEtatModel::setupModel(CStandardEtat *ptParentItem, CInfoEtat *ptParentInfo)
{
bool rep;
CInfoEtat *ptInfoEtat;
CStandardEtat *ptItemEtat;
ptInfoEtat = new CInfoEtat (m_ptDomEtats);
rep = ptInfoEtat->First(ptParentInfo);
while (rep) {
ptItemEtat = new CStandardEtat(ptParentItem);
ptItemEtat->setupEtat(ptInfoEtat->Nom(), ptInfoEtat->Type());
ptParentItem->appendChild(ptItemEtat);
setupModel(ptItemEtat, ptInfoEtat);
rep = ptInfoEtat->Next(ptParentInfo);
}
}
bool CStandardEtatModel::insertRows(int row, int count, const QModelIndex &indexParent)
{
CStandardEtat *ptParent;
ptParent = static_cast<CStandardEtat*>(indexParent.internalPointer());
beginInsertRows(indexParent, rowCount(indexParent), rowCount(indexParent));
ptParent->appendChild(m_ptChildAdded);
endInsertRows();
return true;
}
bool CStandardEtatModel::addRow(const QModelIndex &indexParent, QString Nom, TypeEtat Type)
{
CStandardEtat *ptParent;
ptParent = static_cast<CStandardEtat*>(indexParent.internalPointer());
m_ptChildAdded = new CStandardEtat(ptParent);
if (m_ptChildAdded != 0) {
m_ptChildAdded->setupEtat(Nom, Type);
return insertRows (0, 1, indexParent);
}
return false;
}
bool CStandardEtatModel::setData(const QModelIndex &Index, const QVariant &Value, int role)
{
CStandardEtat *ptIndex;
if (!Index.isValid()) {
qDebug() << "## CStandardEtatModel::setData invalide";
return false;
}
ptIndex = static_cast<CStandardEtat*>(Index.internalPointer());
if (ptIndex->typeEtat() != Root) {
ptIndex->setState(((Qt::CheckState)Value.toInt() == Qt::Unchecked) ? false : true);
qDebug() << "## CStandardEtatModel::setData : valeur =" << Value.toInt() << "Unchecked =" << Qt::Unchecked << "Checked ="<< Qt::Checked << "text = " << ptIndex->Nom();
emit dataChanged (Index, Index);
return true;
}
return QAbstractItemModel::setData(Index, Value, role);
}
int CStandardEtatModel::columnCount(const QModelIndex &Parent) const
{
return 1;
}
QVariant CStandardEtatModel::data(const QModelIndex &index, int role) const
{
CStandardEtat *ptItem;
if (!index.isValid())
return QVariant();
if ((role != Qt::DisplayRole) && (role != TypeEtatRole) && (role != Qt::FontRole) && (role !=Qt::ForegroundRole) && (role != Qt::CheckStateRole))
return QVariant();
ptItem = static_cast<CStandardEtat*>(index.internalPointer());
if (ptItem != 0) {
if (role == Qt::CheckStateRole) {
qDebug() << "## CStandardEtatModel::data = " << ptItem->State() << "text = " << ptItem->Nom();
return (ptItem->State() == true) ? Qt::Checked : Qt::Unchecked;
}
return ptItem->Data(role);
}
return QVariant();
}
Qt::ItemFlags CStandardEtatModel::flags(const QModelIndex &index) const
{
if (!index.isValid()) {
// qDebug() << "## CStandardEtatModel::flags invalid";
// return 0;
return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsSelectable;
}
// qDebug() << "## CStandardEtatModel::flags valid";
return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsSelectable;
}
QVariant CStandardEtatModel::headerData(int section, Qt::Orientation orientation, int role) const
{
return QVariant();
}
QModelIndex CStandardEtatModel::index(int row, int column, const QModelIndex &parent) const
{
CStandardEtat *ptParentItem, *ptChildItem;
if (!hasIndex(row, column, parent))
return QModelIndex();
if (!parent.isValid())
ptParentItem = m_ptRootEtat;
else
ptParentItem = static_cast<CStandardEtat*>(parent.internalPointer());
ptChildItem = ptParentItem->Child(row);
if (ptChildItem)
return createIndex(row, column, ptChildItem);
else
return QModelIndex();
}
QModelIndex CStandardEtatModel::parent(const QModelIndex &index) const
{
CStandardEtat *ptParentItem, *ptChildItem;
// qDebug() << "## CStandardEtatModel::parent";
if (!index.isValid())
return QModelIndex();
ptChildItem = static_cast<CStandardEtat*>(index.internalPointer());
ptParentItem = ptChildItem->Parent();
if (ptParentItem == m_ptRootEtat)
return QModelIndex();
return createIndex(ptParentItem->Row(), 0, ptParentItem);
}
int CStandardEtatModel::rowCount(const QModelIndex &parent) const
{
CStandardEtat *ptParentItem;
if (parent.column() > 0)
return 0;
if (!parent.isValid())
ptParentItem = m_ptRootEtat;
else
ptParentItem = static_cast<CStandardEtat*>(parent.internalPointer());
return ptParentItem->childCount();
} |
Partager