FeedsModel work.

This commit is contained in:
Martin Rotter 2013-12-11 18:54:09 +01:00
parent 2233dffb85
commit 0cdfdd686d
21 changed files with 222 additions and 62 deletions

View File

@ -269,8 +269,8 @@ set(APP_SOURCES
src/core/messagesproxymodel.cpp src/core/messagesproxymodel.cpp
src/core/feedsmodel.cpp src/core/feedsmodel.cpp
src/core/feedsproxymodel.cpp src/core/feedsproxymodel.cpp
src/core/basefeedsmodelitem.cpp src/core/feedsmodelitem.cpp
src/core/basefeedsmodelcategory.cpp src/core/feedsmodelcategory.cpp
src/core/feedsmodelrootitem.cpp src/core/feedsmodelrootitem.cpp
src/core/feedsmodelnonrootitem.cpp src/core/feedsmodelnonrootitem.cpp
src/core/feedsmodelfeed.cpp src/core/feedsmodelfeed.cpp

View File

@ -1,10 +0,0 @@
#include "core/basefeedsmodelcategory.h"
BaseFeedsModelCategory::BaseFeedsModelCategory(BaseFeedsModelItem *parent_item)
: FeedsModelNonRootItem(parent_item) {
}
BaseFeedsModelCategory::~BaseFeedsModelCategory() {
qDebug("Destroying BaseFeedsModelCategory instance.");
}

View File

@ -1,11 +0,0 @@
#include <QDebug>
#include "core/basefeedsmodelitem.h"
BaseFeedsModelItem::BaseFeedsModelItem() {
}
BaseFeedsModelItem::~BaseFeedsModelItem() {
qDebug("Destroying BaseFeedsModelItem instance.");
}

View File

@ -1,12 +1,103 @@
#include "core/feedsmodel.h" #include "core/feedsmodel.h"
#include "core/feedsmodelrootitem.h" #include "core/feedsmodelrootitem.h"
#include "core/feedsmodelnonrootitem.h"
FeedsModel::FeedsModel(QObject *parent) : QAbstractItemModel(parent) { FeedsModel::FeedsModel(QObject *parent) : QAbstractItemModel(parent) {
m_rootItem = new FeedsModelRootItem(); m_rootItem = new FeedsModelRootItem();
m_rootItem->m_childItems.append(new FeedsModelNonRootItem(m_rootItem));
} }
FeedsModel::~FeedsModel() { FeedsModel::~FeedsModel() {
qDebug("Destroying FeedsModel instance."); qDebug("Destroying FeedsModel instance.");
delete m_rootItem; delete m_rootItem;
} }
QVariant FeedsModel::data(const QModelIndex &index, int role) const {
return QVariant();
}
QVariant FeedsModel::headerData(int section,
Qt::Orientation orientation,
int role) const {
if (orientation == Qt::Horizontal) {
switch (role) {
case Qt::DisplayRole:
return "aaa";
default:
return QVariant();
}
}
else {
return QVariant();
}
}
QModelIndex FeedsModel::index(int row, int column, const QModelIndex &parent) const {
if (!hasIndex(row, column, parent)) {
return QModelIndex();
}
FeedsModelItem *parent_item;
if (!parent.isValid()) {
parent_item = m_rootItem;
}
else {
parent_item = static_cast<FeedsModelItem*>(parent.internalPointer());
}
FeedsModelItem *child_item = parent_item->child(row);
if (child_item) {
return createIndex(row, column, child_item);
}
else {
return QModelIndex();
}
}
QModelIndex FeedsModel::parent(const QModelIndex &child) const {
if (!child.isValid()) {
return QModelIndex();
}
FeedsModelItem *child_item = static_cast<FeedsModelItem*>(child.internalPointer());
FeedsModelItem *parent_item = child_item->parent();
if (parent_item == m_rootItem) {
return QModelIndex();
}
else {
return createIndex(parent_item->row(), 0, parent_item);
}
}
int FeedsModel::rowCount(const QModelIndex &parent) const {
FeedsModelItem *parent_item;
if (parent.column() > 0) {
return 0;
}
if (!parent.isValid()) {
parent_item = m_rootItem;
}
else {
parent_item = static_cast<FeedsModelItem*>(parent.internalPointer());
}
return parent_item->childCount();
}
int FeedsModel::columnCount(const QModelIndex &parent) const {
if (parent.isValid()) {
return static_cast<FeedsModelItem*>(parent.internalPointer())->columnCount();
}
else {
return m_rootItem->columnCount();
}
}

View File

@ -10,12 +10,17 @@ class FeedsModel : public QAbstractItemModel {
Q_OBJECT Q_OBJECT
public: public:
// Constructors and destructors.
explicit FeedsModel(QObject *parent = 0); explicit FeedsModel(QObject *parent = 0);
virtual ~FeedsModel(); virtual ~FeedsModel();
signals: QVariant data(const QModelIndex &index, int role) const;
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
QModelIndex index(int row, int column, const QModelIndex &parent) const;
QModelIndex parent(const QModelIndex &child) const;
int columnCount(const QModelIndex &parent) const;
int rowCount(const QModelIndex &parent) const;
public slots:
private: private:

View File

@ -0,0 +1,10 @@
#include "core/feedsmodelcategory.h"
FeedsModelCategory::FeedsModelCategory(FeedsModelItem *parent_item)
: FeedsModelNonRootItem(parent_item) {
}
FeedsModelCategory::~FeedsModelCategory() {
qDebug("Destroying BaseFeedsModelCategory instance.");
}

View File

@ -6,12 +6,11 @@
// Base class for all categories contained in FeedsModel. // Base class for all categories contained in FeedsModel.
// NOTE: This class is derived to create PARTICULAR category types. // NOTE: This class is derived to create PARTICULAR category types.
class BaseFeedsModelCategory : public FeedsModelNonRootItem class FeedsModelCategory : public FeedsModelNonRootItem {
{
public: public:
// Constructors and destructors // Constructors and destructors
explicit BaseFeedsModelCategory(BaseFeedsModelItem *parent_item); explicit FeedsModelCategory(FeedsModelItem *parent_item);
virtual ~BaseFeedsModelCategory(); virtual ~FeedsModelCategory();
}; };

View File

@ -1,7 +1,7 @@
#include "core/feedsmodelfeed.h" #include "core/feedsmodelfeed.h"
FeedsModelFeed::FeedsModelFeed(BaseFeedsModelItem *parent_item) FeedsModelFeed::FeedsModelFeed(FeedsModelItem *parent_item)
:FeedsModelNonRootItem(parent_item) { :FeedsModelNonRootItem(parent_item) {
} }

View File

@ -10,7 +10,7 @@ class FeedsModelFeed : public FeedsModelNonRootItem
{ {
public: public:
// Constructors and destructors. // Constructors and destructors.
explicit FeedsModelFeed(BaseFeedsModelItem *parent_item); explicit FeedsModelFeed(FeedsModelItem *parent_item);
virtual ~FeedsModelFeed(); virtual ~FeedsModelFeed();
int childCount() const; int childCount() const;

View File

@ -0,0 +1,11 @@
#include <QDebug>
#include "core/feedsmodelitem.h"
FeedsModelItem::FeedsModelItem() {
}
FeedsModelItem::~FeedsModelItem() {
qDebug("Destroying BaseFeedsModelItem instance.");
}

View File

@ -6,17 +6,19 @@
// Base class for all items contained in FeedsModel. // Base class for all items contained in FeedsModel.
class BaseFeedsModelItem { class FeedsModelItem {
public: public:
// Constructors and destructors. // Constructors and destructors.
explicit BaseFeedsModelItem(); explicit FeedsModelItem();
virtual ~BaseFeedsModelItem(); virtual ~FeedsModelItem();
// Returns parent item of this item. // Returns parent item of this item.
// NOTE: Model ROOT item has NULL parent. // NOTE: Model ROOT item has NULL parent.
virtual BaseFeedsModelItem *parent() = 0; virtual FeedsModelItem *parent() = 0;
virtual int childCount() const = 0; virtual int childCount() const = 0;
virtual int columnCount() const = 0; virtual int columnCount() const = 0;
virtual FeedsModelItem *child(int row) = 0;
virtual int row() const = 0;
protected: protected:
QIcon m_icon; QIcon m_icon;

View File

@ -1,7 +1,7 @@
#include "core/feedsmodelnonrootitem.h" #include "core/feedsmodelnonrootitem.h"
FeedsModelNonRootItem::FeedsModelNonRootItem(BaseFeedsModelItem *parent_item) FeedsModelNonRootItem::FeedsModelNonRootItem(FeedsModelItem *parent_item)
: FeedsModelRootItem(), m_parentItem(parent_item) { : FeedsModelRootItem(), m_parentItem(parent_item) {
} }
@ -9,6 +9,15 @@ FeedsModelNonRootItem::~FeedsModelNonRootItem() {
qDebug("Destroying FeedsModelNonRootItem instance."); qDebug("Destroying FeedsModelNonRootItem instance.");
} }
BaseFeedsModelItem *FeedsModelNonRootItem::parent() { FeedsModelItem *FeedsModelNonRootItem::parent() {
return m_parentItem; return m_parentItem;
} }
int FeedsModelNonRootItem::row() const {
if (m_parentItem) {
return static_cast<FeedsModelRootItem*>(m_parentItem)->m_childItems.indexOf((FeedsModelItem*) this);
}
else {
return 0;
}
}

View File

@ -10,13 +10,14 @@
class FeedsModelNonRootItem : public FeedsModelRootItem { class FeedsModelNonRootItem : public FeedsModelRootItem {
public: public:
// Constructors and destructors. // Constructors and destructors.
explicit FeedsModelNonRootItem(BaseFeedsModelItem *parent_item); explicit FeedsModelNonRootItem(FeedsModelItem *parent_item);
virtual ~FeedsModelNonRootItem(); virtual ~FeedsModelNonRootItem();
BaseFeedsModelItem *parent(); FeedsModelItem *parent();
int row() const;
protected: protected:
BaseFeedsModelItem *m_parentItem; FeedsModelItem *m_parentItem;
}; };
#endif // FEEDSMODELNONROOTITEM_H #endif // FEEDSMODELNONROOTITEM_H

View File

@ -2,7 +2,7 @@
FeedsModelRootItem::FeedsModelRootItem() FeedsModelRootItem::FeedsModelRootItem()
: BaseFeedsModelItem() { : FeedsModelItem() {
} }
FeedsModelRootItem::~FeedsModelRootItem() { FeedsModelRootItem::~FeedsModelRootItem() {
@ -10,14 +10,22 @@ FeedsModelRootItem::~FeedsModelRootItem() {
qDeleteAll(m_childItems); qDeleteAll(m_childItems);
} }
BaseFeedsModelItem *FeedsModelRootItem::parent() { FeedsModelItem *FeedsModelRootItem::parent() {
return NULL; return NULL;
} }
FeedsModelItem *FeedsModelRootItem::child(int row) {
return m_childItems.at(0);
}
int FeedsModelRootItem::columnCount() const { int FeedsModelRootItem::columnCount() const {
return 2; return 2;
} }
int FeedsModelRootItem::row() const {
return 0;
}
int FeedsModelRootItem::childCount() const { int FeedsModelRootItem::childCount() const {
return m_childItems.count(); return m_childItems.count();
} }

View File

@ -1,24 +1,29 @@
#ifndef FEEDMODELROOTITEM_H #ifndef FEEDMODELROOTITEM_H
#define FEEDMODELROOTITEM_H #define FEEDMODELROOTITEM_H
#include "core/basefeedsmodelitem.h" #include "core/feedsmodelitem.h"
// Represents ROOT item of FeedsModel. // Represents ROOT item of FeedsModel.
// NOTE: This class is derived to add functionality for // NOTE: This class is derived to add functionality for
// all non-root items of FeedsModel. // all non-root items of FeedsModel.
class FeedsModelRootItem : public BaseFeedsModelItem { class FeedsModelRootItem : public FeedsModelItem {
friend class FeedsModelNonRootItem;
friend class FeedsModel;
public: public:
// Constructors and destructors. // Constructors and destructors.
explicit FeedsModelRootItem(); explicit FeedsModelRootItem();
virtual ~FeedsModelRootItem(); virtual ~FeedsModelRootItem();
BaseFeedsModelItem *parent(); FeedsModelItem *parent();
FeedsModelItem *child(int row);
int childCount() const; int childCount() const;
int columnCount() const; int columnCount() const;
int row() const;
protected: protected:
QList<BaseFeedsModelItem*> m_childItems; QList<FeedsModelItem*> m_childItems;
}; };

View File

@ -1,10 +1,26 @@
#include "core/feedsproxymodel.h" #include "core/feedsproxymodel.h"
#include "core/feedsmodel.h"
FeedsProxyModel::FeedsProxyModel(QObject *parent) FeedsProxyModel::FeedsProxyModel(QObject *parent)
: QSortFilterProxyModel(parent) { : QSortFilterProxyModel(parent) {
m_sourceModel = new FeedsModel(this);
setSourceModel(m_sourceModel);
} }
FeedsProxyModel::~FeedsProxyModel() { FeedsProxyModel::~FeedsProxyModel() {
qDebug("Destroying FeedsProxyModel instance"); qDebug("Destroying FeedsProxyModel instance");
} }
FeedsModel *FeedsProxyModel::sourceModel() {
return m_sourceModel;
}
bool FeedsProxyModel::lessThan(const QModelIndex &left,
const QModelIndex &right) const {
return QSortFilterProxyModel::lessThan(left, right);
}

View File

@ -4,17 +4,25 @@
#include <QSortFilterProxyModel> #include <QSortFilterProxyModel>
class FeedsModel;
class FeedsProxyModel : public QSortFilterProxyModel { class FeedsProxyModel : public QSortFilterProxyModel {
Q_OBJECT Q_OBJECT
public: public:
// Constructors and destructors.
explicit FeedsProxyModel(QObject *parent = 0); explicit FeedsProxyModel(QObject *parent = 0);
virtual ~FeedsProxyModel(); virtual ~FeedsProxyModel();
signals: FeedsModel *sourceModel();
public slots: protected:
// Compares two rows of data.
bool lessThan(const QModelIndex &left, const QModelIndex &right) const;
private:
// Source model pointer.
FeedsModel *m_sourceModel;
}; };
#endif // FEEDSPROXYMODEL_H #endif // FEEDSPROXYMODEL_H

View File

@ -29,27 +29,27 @@ bool MessagesProxyModel::lessThan(const QModelIndex &left, const QModelIndex &ri
} }
QModelIndexList MessagesProxyModel::mapListFromSource(const QModelIndexList &indexes, bool deep) { QModelIndexList MessagesProxyModel::mapListFromSource(const QModelIndexList &indexes, bool deep) {
QModelIndexList mapped_idxs; QModelIndexList mapped_indexes;
foreach (const QModelIndex &index, indexes) { foreach (const QModelIndex &index, indexes) {
if (deep) { if (deep) {
mapped_idxs << mapFromSource(m_sourceModel->index(index.row(), index.column())); mapped_indexes << mapFromSource(m_sourceModel->index(index.row(), index.column()));
} }
else { else {
mapped_idxs << mapFromSource(index); mapped_indexes << mapFromSource(index);
} }
} }
return mapped_idxs; return mapped_indexes;
} }
QModelIndexList MessagesProxyModel::mapListToSource(const QModelIndexList &indexes) { QModelIndexList MessagesProxyModel::mapListToSource(const QModelIndexList &indexes) {
QModelIndexList source_idxs; QModelIndexList source_indexes;
foreach (const QModelIndex &index, indexes) { foreach (const QModelIndex &index, indexes) {
source_idxs << mapToSource(index); source_indexes << mapToSource(index);
} }
return source_idxs; return source_indexes;
} }

View File

@ -1,7 +1,13 @@
#include "gui/feedsview.h" #include "gui/feedsview.h"
#include "core/feedsmodel.h"
#include "core/feedsproxymodel.h"
FeedsView::FeedsView(QWidget *parent) : QTreeView(parent) { FeedsView::FeedsView(QWidget *parent) : QTreeView(parent) {
m_proxyModel = new FeedsProxyModel(this);
m_sourceModel = m_proxyModel->sourceModel();
setModel(m_proxyModel);
} }
FeedsView::~FeedsView() { FeedsView::~FeedsView() {

View File

@ -4,6 +4,9 @@
#include <QTreeView> #include <QTreeView>
class FeedsModel;
class FeedsProxyModel;
class FeedsView : public QTreeView { class FeedsView : public QTreeView {
Q_OBJECT Q_OBJECT
@ -15,6 +18,10 @@ class FeedsView : public QTreeView {
public slots: public slots:
private:
FeedsModel *m_sourceModel;
FeedsProxyModel *m_proxyModel;
}; };
#endif // FEEDSVIEW_H #endif // FEEDSVIEW_H

View File

@ -29,16 +29,19 @@ void TabBar::wheelEvent(QWheelEvent *event) {
// Make sure rotating works. // Make sure rotating works.
if (number_of_tabs > 1) { if (number_of_tabs > 1) {
if (event->delta() > 0 && current_index == 0) { if (event->delta() > 0) {
setCurrentIndex(number_of_tabs - 1); // Scroll to the LEFT tab.
setCurrentIndex(current_index == 0 ?
number_of_tabs - 1 :
current_index - 1);
} }
else if (event->delta() < 0 && current_index == number_of_tabs - 1) { else if (event->delta() < 0) {
setCurrentIndex(0); // Scroll to the RIGHT tab.
setCurrentIndex(current_index == number_of_tabs - 1 ?
0 :
current_index + 1);
} }
} }
else {
QTabBar::wheelEvent(event);
}
} }
void TabBar::mousePressEvent(QMouseEvent *event) { void TabBar::mousePressEvent(QMouseEvent *event) {