Add a clear() method to empty the list of the model (better than
creating a new one every time) and rename m_posts to m_items
This commit is contained in:
		
					parent
					
						
							
								6a3a866ad5
							
						
					
				
			
			
				commit
				
					
						521bdad42c
					
				
			
		
					 1 changed files with 30 additions and 14 deletions
				
			
		| 
						 | 
					@ -22,7 +22,7 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Own
 | 
					// Own
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "danboorutablemodel.h"
 | 
					#include "danboorupostmodel.h"
 | 
				
			||||||
#include "libdanbooru/danboorupost.h"
 | 
					#include "libdanbooru/danboorupost.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Qt
 | 
					// Qt
 | 
				
			||||||
| 
						 | 
					@ -32,47 +32,50 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace Danbooru {
 | 
					namespace Danbooru {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    DanbooruTableModel::DanbooruTableModel(QObject* parent): QAbstractListModel(parent)
 | 
					    DanbooruPostModel::DanbooruPostModel(QObject* parent): QAbstractListModel(parent)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    DanbooruTableModel::~DanbooruTableModel()
 | 
					    DanbooruPostModel::~DanbooruPostModel()
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        qDeleteAll(m_posts);
 | 
					
 | 
				
			||||||
        m_posts.clear();
 | 
					        if (!m_items.isEmpty()) {
 | 
				
			||||||
 | 
					            qDeleteAll(m_items);
 | 
				
			||||||
 | 
					            m_items.clear();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    int DanbooruTableModel::rowCount(const QModelIndex& parent) const
 | 
					    int DanbooruPostModel::rowCount(const QModelIndex& parent) const
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        Q_UNUSED(parent)
 | 
					        Q_UNUSED(parent)
 | 
				
			||||||
        return m_posts.size();
 | 
					        return m_items.size();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    void DanbooruTableModel::addPost(Danbooru::DanbooruPost* post)
 | 
					    void DanbooruPostModel::addPost(Danbooru::DanbooruPost* post)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        beginInsertRows(QModelIndex(), m_posts.size(), m_posts.size() + 1);
 | 
					        beginInsertRows(QModelIndex(), m_items.size(), m_items.size() + 1);
 | 
				
			||||||
        m_posts.append(post);
 | 
					        m_items.append(post);
 | 
				
			||||||
        endInsertRows();
 | 
					        endInsertRows();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    QVariant DanbooruTableModel::data(const QModelIndex& index, int role) const
 | 
					    QVariant DanbooruPostModel::data(const QModelIndex& index, int role) const
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (!index.isValid()) {
 | 
					        if (!index.isValid()) {
 | 
				
			||||||
            return QVariant();
 | 
					            return QVariant();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (index.row() >= m_posts.size() || index.row() < 0) {
 | 
					        if (index.row() >= m_items.size() || index.row() < 0) {
 | 
				
			||||||
            return QVariant();
 | 
					            return QVariant();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (m_posts.isEmpty()) {
 | 
					        if (m_items.isEmpty()) {
 | 
				
			||||||
            return QVariant();
 | 
					            return QVariant();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        DanbooruPost* post = m_posts.at(index.row());
 | 
					        DanbooruPost* post = m_items.at(index.row());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (!post) {
 | 
					        if (!post) {
 | 
				
			||||||
            return QVariant();
 | 
					            return QVariant();
 | 
				
			||||||
| 
						 | 
					@ -98,4 +101,17 @@ namespace Danbooru {
 | 
				
			||||||
        return QVariant();
 | 
					        return QVariant();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    void DanbooruPostModel::clear() {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (m_items.isEmpty()) {
 | 
				
			||||||
 | 
					            return;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        beginRemoveRows(QModelIndex(),0, m_items.size());
 | 
				
			||||||
 | 
					        qDeleteAll(m_items);
 | 
				
			||||||
 | 
					        m_items.clear();
 | 
				
			||||||
 | 
					        endRemoveRows();
 | 
				
			||||||
 | 
					        reset();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue