Just sketching some ideas here:
class Card
{
// ....
};
{
Q_OBJECT
public:
// returns an invalid card if ID is not found, e.g. Card object with empty "id"
Card addCardById
(const QString &id
);
Card addCardByName
(const QString &name
);
Card card
(const QString &id
) const;
QList<Card> cards() const;
int count() const;
signals:
void cardRemoved
(const QString &id
);
private:
QList<Card> m_cards;
};
class Card
{
QString id;
QString name;
// ....
};
class CardManager : public QObject
{
Q_OBJECT
public:
// returns an invalid card if ID is not found, e.g. Card object with empty "id"
Card addCardById(const QString &id);
Card addCardByName(const QString &name);
Card card(const QString &id) const;
QList<Card> cards() const;
int count() const;
signals:
void cardAdded(const QString &id);
void cardRemoved(const QString &id);
private:
QList<Card> m_cards;
};
To copy to clipboard, switch view to plain text mode
class Album
{
};
class AlbumManager
: public QObject{
Q_OBJECT
public:
Album createAlbum
(const QString &name
);
Album album
(const QString &id
) const;
QList<Album> albums() const;
int count() const;
signals:
void albumAdded
(const QString &id
);
void albumRemoved
(const QString &id
);
};
class Album
{
QString id;
QString name;
QStringList cardIDs;
};
class AlbumManager : public QObject
{
Q_OBJECT
public:
Album createAlbum(const QString &name);
Album album(const QString &id) const;
QList<Album> albums() const;
int count() const;
void addCard(const QString &albumId, const QString &cardId);
void removeCard(const QString &albumId, const QString &cardId);
signals:
void albumAdded(const QString &id);
void albumRemoved(const QString &id);
void cardAdded(const QString &albumId, const QStringList &cardIDs);
void cardRemoved(const QString &albumId, const QStringList &cardIDs);
};
To copy to clipboard, switch view to plain text mode
{
Q_OBJECT
public:
enum Roles {
IdRole = Qt::UserRole + 1,
NameRole,
// ....
}
explicit AbstractCardModel
(CardManager
*cardManager,
QObject *parent
= 0);
// this uses cardForIndex to get the card, then uses role to return the actual data
protected:
QHash<int, QByteArray> roleNames() const;
virtual Card cardForIndex
(const QModelIndex &index
) const = 0;
protected:
CardManager *m_cardManager;
};
class AbstractCardModel : public QAbstractListModel
{
Q_OBJECT
public:
enum Roles {
IdRole = Qt::UserRole + 1,
NameRole,
// ....
}
explicit AbstractCardModel(CardManager *cardManager, QObject *parent = 0);
// this uses cardForIndex to get the card, then uses role to return the actual data
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
protected:
QHash<int, QByteArray> roleNames() const;
virtual Card cardForIndex(const QModelIndex &index) const = 0;
protected:
CardManager *m_cardManager;
};
To copy to clipboard, switch view to plain text mode
// for showing the cards of a single album
class AlbumCardsModel : public AbstractCardsModel
{
Q_OBJECT
public:
AlbumCardsModel
(AlbumManager
*albumManager, CardManager
*cardManager,
QObject *parent
= 0);
void showAlbum
(const QString &albumId
);
protected:
private slots:
// gets connected to m_albumManager's albumRemoved() signal
void onAlbumRemoved
(const QString &albumId
);
private:
AlbumManager *m_albumManager;
Album m_album;
};
void AlbumCardsModel
::showAlbum(const QString &albumId
) {
beginResetModel();
m_album = m_albumManager->album(albumId);
endResetModel();
}
int AlbumCardsModel::rowCount(const QModelIndex&) const
{
return m_album.cardIDs.count();
}
Card AlbumCardsModel
::cardForIndex(const QModelIndex &index
) const{
return m_cardManager->card(m_album.cardIDs.at(index.row());
}
void AlbumCardsModel
::onAlbumRemoved(const QString &albumId
) {
if (albumId != m_album.id) return;
beginResetModel();
m_album = Album();
endResetModel();
}
void AlbumCardsModel
::onCardAdded(const QString &albumId,
const QString &cardId
) {
if (albumId != m_album.id) return;
const Album updatedAlbum = m_albumManager->album(albumId);
const int row = updatedAlbum.cardIDs.indexOf(cardId);
m_album = updatedAlbum;
endInsertRows();
}
void AlbumCardsModel
::onCardRemoved(const QString &albumId,
const QString &cardId
) {
if (albumId != m_album.id) return;
const Album updatedAlbum = m_albumManager->album(albumId);
const int row = m_album.cardIDs.indexOf(cardId);
m_album = updatedAlbum;
endRemoveRows();
}
// for showing the cards of a single album
class AlbumCardsModel : public AbstractCardsModel
{
Q_OBJECT
public:
AlbumCardsModel(AlbumManager *albumManager, CardManager *cardManager, QObject *parent = 0);
void showAlbum(const QString &albumId);
int rowCount(const QModelIndex &parent) const;
protected:
Card cardForIndex(const QModelIndex &index) const;
private slots:
// gets connected to m_albumManager's albumRemoved() signal
void onAlbumRemoved(const QString &albumId);
void onCardAdded(const QString &albumId, const QString cardId);
void onCardRemoved(const QString &albumId, const QString cardId);
private:
AlbumManager *m_albumManager;
Album m_album;
};
void AlbumCardsModel::showAlbum(const QString &albumId)
{
beginResetModel();
m_album = m_albumManager->album(albumId);
endResetModel();
}
int AlbumCardsModel::rowCount(const QModelIndex&) const
{
return m_album.cardIDs.count();
}
Card AlbumCardsModel::cardForIndex(const QModelIndex &index) const
{
return m_cardManager->card(m_album.cardIDs.at(index.row());
}
void AlbumCardsModel::onAlbumRemoved(const QString &albumId)
{
if (albumId != m_album.id) return;
beginResetModel();
m_album = Album();
endResetModel();
}
void AlbumCardsModel::onCardAdded(const QString &albumId, const QString &cardId)
{
if (albumId != m_album.id) return;
const Album updatedAlbum = m_albumManager->album(albumId);
const int row = updatedAlbum.cardIDs.indexOf(cardId);
beginInsertRows(QModelIndex(), row, row);
m_album = updatedAlbum;
endInsertRows();
}
void AlbumCardsModel::onCardRemoved(const QString &albumId, const QString &cardId)
{
if (albumId != m_album.id) return;
const Album updatedAlbum = m_albumManager->album(albumId);
const int row = m_album.cardIDs.indexOf(cardId);
beginRemoveRows(QModelIndex(), row, row);
m_album = updatedAlbum;
endRemoveRows();
}
To copy to clipboard, switch view to plain text mode
Cheers,
_
Bookmarks