Changed a lot of code to reduce boilerplate on models in the future.

So, I changed a lot of code to reduce boilerplate on models in the
future. Currently we do not have a lot of models, but this can increase
quite rapdly. There's a second TreeModel in the works, the Yearly
Statistics, this patch will save around 250 LOC for this new model,
and more and more models will give us a greater saving.

Iwll do that for the table models in the future too - I did the tree
models now because they are the most complex case and I didn't wanted
to create a second tree model without this.

Signed-off-by: Tomaz Canabrava <tcanabrava@kde.org>
This commit is contained in:
Tomaz Canabrava 2013-06-17 18:59:50 -03:00
parent 14ccbbf6e8
commit ae68ae38bb
5 changed files with 165 additions and 154 deletions

View file

@ -123,51 +123,54 @@ private:
*
*/
struct TreeItemDT {
struct TreeItem {
Q_DECLARE_TR_FUNCTIONS (TreeItemDT);
public:
enum Column {NR, DATE, RATING, DEPTH, DURATION, TEMPERATURE, TOTALWEIGHT,
SUIT, CYLINDER, NITROX, SAC, OTU, MAXCNS, LOCATION, COLUMNS };
enum ExtraRoles{STAR_ROLE = Qt::UserRole + 1, DIVE_ROLE, SORT_ROLE};
virtual ~TreeItemDT();
int columnCount() const {
return COLUMNS;
};
virtual ~TreeItem();
virtual QVariant data (int column, int role) const;
int row() const;
QList<TreeItemDT *> children;
TreeItemDT *parent;
QList<TreeItem*> children;
TreeItem *parent;
};
struct TripItem;
class DiveTripModel : public QAbstractItemModel
class TreeModel : public QAbstractItemModel
{
Q_OBJECT
public:
enum Layout{TREE, LIST, CURRENT};
TreeModel(QObject *parent = 0);
virtual ~TreeModel();
DiveTripModel(QObject *parent = 0);
~DiveTripModel();
/*reimp*/ Qt::ItemFlags flags(const QModelIndex &index) const;
/*reimp*/ QVariant data(const QModelIndex &index, int role) const;
/*reimp*/ QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
virtual QVariant data(const QModelIndex &index, int role) const;
/*reimp*/ int rowCount(const QModelIndex &parent = QModelIndex()) const;
/*reimp*/ int columnCount(const QModelIndex &parent = QModelIndex()) const;
/*reimp*/ QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
/*reimp*/ QModelIndex parent(const QModelIndex &child) const;
protected:
int columns;
TreeItem *rootItem;
};
class DiveTripModel : public TreeModel {
public:
enum Column {NR, DATE, RATING, DEPTH, DURATION, TEMPERATURE, TOTALWEIGHT,
SUIT, CYLINDER, NITROX, SAC, OTU, MAXCNS, LOCATION, COLUMNS };
enum ExtraRoles{STAR_ROLE = Qt::UserRole + 1, DIVE_ROLE, SORT_ROLE};
enum Layout{TREE, LIST, CURRENT};
Qt::ItemFlags flags(const QModelIndex &index) const;
virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
DiveTripModel(QObject* parent = 0);
Layout layout() const;
void setLayout(Layout layout);
private:
void setupModelData();
TreeItemDT *rootItem;
QMap<dive_trip_t*, TripItem*> trips;
Layout currentLayout;
};
@ -185,12 +188,12 @@ public:
virtual Qt::ItemFlags flags(const QModelIndex& index) const;
virtual bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole);
void update();
public slots:
void remove(const QModelIndex& index);
private:
int numRows;
};
#endif