mirror of
https://github.com/jaredtao/TaoQuick.git
synced 2025-01-17 20:12:54 +08:00
model 优化
This commit is contained in:
parent
5295584e3f
commit
fb7967176f
@ -1,106 +1,37 @@
|
||||
#pragma once
|
||||
|
||||
#include "Common/PropertyHelper.h"
|
||||
#include "TaoCommonGlobal.h"
|
||||
#include <QObject>
|
||||
class TAO_API QuickListItemBase : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(bool isChecked READ isChecked WRITE setIsChecked NOTIFY isCheckedChanged)
|
||||
Q_PROPERTY(bool isSelected READ isSelected WRITE setIsSelected NOTIFY isSelectedChanged)
|
||||
Q_PROPERTY(bool isVisible READ isVisible WRITE setIsVisible NOTIFY isVisibleChanged)
|
||||
Q_PROPERTY(bool isAlternate READ isAlternate WRITE setIsAlternate NOTIFY isAlternateChanged)
|
||||
AUTO_PROPERTY(bool, isChecked, false)
|
||||
AUTO_PROPERTY(bool, isSelected, false)
|
||||
AUTO_PROPERTY(bool, isAlternate, false)
|
||||
public:
|
||||
explicit QuickListItemBase(QObject* parent = nullptr);
|
||||
~QuickListItemBase() override;
|
||||
virtual ~QuickListItemBase() override;
|
||||
QuickListItemBase(const QuickListItemBase& other)
|
||||
{
|
||||
setIsChecked(other.isChecked());
|
||||
setIsSelected(other.isSelected());
|
||||
setIsVisible(other.isVisible());
|
||||
setIsAlternate(other.isAlternate());
|
||||
set_isChecked(other.isChecked());
|
||||
set_isSelected(other.isSelected());
|
||||
set_isAlternate(other.isAlternate());
|
||||
}
|
||||
|
||||
QuickListItemBase& operator=(const QuickListItemBase& other)
|
||||
{
|
||||
setIsChecked(other.isChecked());
|
||||
setIsSelected(other.isSelected());
|
||||
setIsVisible(other.isVisible());
|
||||
setIsAlternate(other.isAlternate());
|
||||
set_isChecked(other.isChecked());
|
||||
set_isSelected(other.isSelected());
|
||||
set_isAlternate(other.isAlternate());
|
||||
return *this;
|
||||
}
|
||||
bool isChecked() const
|
||||
{
|
||||
return mIsChecked;
|
||||
}
|
||||
|
||||
bool isSelected() const
|
||||
{
|
||||
return mIsSelected;
|
||||
}
|
||||
|
||||
bool isVisible() const
|
||||
{
|
||||
return mIsVisible;
|
||||
}
|
||||
bool isAlternate() const
|
||||
{
|
||||
return mIsAlternate;
|
||||
}
|
||||
// Model call this for search. return true if contents match key, others return false.
|
||||
virtual bool match(const QString& key)
|
||||
{
|
||||
Q_UNUSED(key)
|
||||
return true;
|
||||
}
|
||||
public slots:
|
||||
void setIsChecked(bool isChecked)
|
||||
{
|
||||
if (mIsChecked == isChecked)
|
||||
return;
|
||||
|
||||
mIsChecked = isChecked;
|
||||
emit isCheckedChanged(mIsChecked);
|
||||
}
|
||||
|
||||
void setIsSelected(bool isSelected)
|
||||
{
|
||||
if (mIsSelected == isSelected)
|
||||
return;
|
||||
|
||||
mIsSelected = isSelected;
|
||||
emit isSelectedChanged(mIsSelected);
|
||||
}
|
||||
|
||||
void setIsVisible(bool isVisible)
|
||||
{
|
||||
// if (mIsVisible == isVisible)
|
||||
// return;
|
||||
|
||||
mIsVisible = isVisible;
|
||||
emit isVisibleChanged(mIsVisible);
|
||||
}
|
||||
void setIsAlternate(bool isAlternate)
|
||||
{
|
||||
if (mIsAlternate == isAlternate)
|
||||
{
|
||||
return;
|
||||
}
|
||||
mIsAlternate = isAlternate;
|
||||
emit isAlternateChanged(mIsAlternate);
|
||||
}
|
||||
|
||||
signals:
|
||||
void isCheckedChanged(bool isChecked);
|
||||
|
||||
void isSelectedChanged(bool isSelected);
|
||||
|
||||
void isVisibleChanged(bool isVisible);
|
||||
|
||||
void isAlternateChanged(bool isAlternate);
|
||||
|
||||
private:
|
||||
bool mIsChecked = false;
|
||||
bool mIsSelected = false;
|
||||
bool mIsVisible = true;
|
||||
bool mIsAlternate = false;
|
||||
};
|
||||
|
@ -19,14 +19,14 @@ void QuickListModel::check(int row, bool checked)
|
||||
return;
|
||||
}
|
||||
|
||||
mDatas.at(row)->setIsChecked(checked);
|
||||
mDatas.at(row)->set_isChecked(checked);
|
||||
if (mDatas.at(row)->isSelected())
|
||||
{
|
||||
for (const auto& obj : mDatas)
|
||||
{
|
||||
if (obj->isVisible() && obj->isSelected())
|
||||
if (obj->isSelected())
|
||||
{
|
||||
obj->setIsChecked(checked);
|
||||
obj->set_isChecked(checked);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -40,7 +40,7 @@ void QuickListModel::check(int row, bool checked)
|
||||
{
|
||||
for (const auto& obj : mDatas)
|
||||
{
|
||||
if (obj->isVisible() && false == obj->isChecked())
|
||||
if (false == obj->isChecked())
|
||||
{
|
||||
allCheck = false;
|
||||
break;
|
||||
@ -58,10 +58,8 @@ void QuickListModel::setAllChecked(bool allChecked)
|
||||
{
|
||||
for (const auto& obj : mDatas)
|
||||
{
|
||||
if (obj->isVisible())
|
||||
{
|
||||
obj->setIsChecked(allChecked);
|
||||
}
|
||||
|
||||
obj->set_isChecked(allChecked);
|
||||
}
|
||||
updateCheckedCount();
|
||||
if (mAllChecked == allChecked)
|
||||
@ -120,10 +118,8 @@ void QuickListModel::deselectAll()
|
||||
{
|
||||
for (const auto& obj : mDatas)
|
||||
{
|
||||
if (obj->isVisible())
|
||||
{
|
||||
obj->setIsSelected(false);
|
||||
}
|
||||
|
||||
obj->set_isSelected(false);
|
||||
}
|
||||
updateSelectedCount();
|
||||
emit selectedAction();
|
||||
@ -133,10 +129,8 @@ void QuickListModel::selectAll()
|
||||
{
|
||||
for (const auto& obj : mDatas)
|
||||
{
|
||||
if (obj->isVisible())
|
||||
{
|
||||
obj->setIsSelected(true);
|
||||
}
|
||||
|
||||
obj->set_isSelected(true);
|
||||
}
|
||||
updateSelectedCount();
|
||||
emit selectedAction();
|
||||
@ -157,7 +151,7 @@ void QuickListModel::select(int row)
|
||||
{
|
||||
return;
|
||||
}
|
||||
mDatas.at(row)->setIsSelected(true);
|
||||
mDatas.at(row)->set_isSelected(true);
|
||||
updateSelectedCount();
|
||||
emit selectedAction();
|
||||
}
|
||||
@ -168,7 +162,7 @@ void QuickListModel::deselect(int row)
|
||||
{
|
||||
return;
|
||||
}
|
||||
mDatas.at(row)->setIsSelected(false);
|
||||
mDatas.at(row)->set_isSelected(false);
|
||||
updateSelectedCount();
|
||||
emit selectedAction();
|
||||
}
|
||||
@ -179,7 +173,7 @@ void QuickListModel::selectRange(int from, int to)
|
||||
int maxRow = qMax(from, to);
|
||||
for (int i = 0; i < mDatas.size(); ++i)
|
||||
{
|
||||
mDatas.at(i)->setIsSelected(mDatas.at(i)->isVisible() && minRow <= i && i <= maxRow);
|
||||
mDatas.at(i)->set_isSelected(minRow <= i && i <= maxRow);
|
||||
}
|
||||
updateSelectedCount();
|
||||
emit selectedAction();
|
||||
@ -189,7 +183,7 @@ void QuickListModel::selectSingle(int row)
|
||||
{
|
||||
for (int i = 0; i < mDatas.size(); ++i)
|
||||
{
|
||||
mDatas.at(i)->setIsSelected(i == row);
|
||||
mDatas.at(i)->set_isSelected(i == row);
|
||||
}
|
||||
updateSelectedCount();
|
||||
emit selectedAction();
|
||||
@ -261,57 +255,37 @@ void QuickListModel::sortByRole()
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (mSortOrder == Qt::SortOrder::AscendingOrder)
|
||||
if (const auto& sortCall = mSortCallbacksAscend.value(sortRole()))
|
||||
{
|
||||
if (const auto& sortCall = mSortCallbacksAscend.value(mSortRole))
|
||||
QList<QuickListItemBase*> copyObjs;
|
||||
copyObjs = mDatas;
|
||||
if (sortOrder() == Qt::SortOrder::AscendingOrder)
|
||||
{
|
||||
QList<QuickListItemBase*> copyObjs;
|
||||
copyObjs = mDatas;
|
||||
std::sort(copyObjs.begin(), copyObjs.end(), sortCall);
|
||||
mDatas = copyObjs;
|
||||
emit dataChanged(index(0, 0), index(mDatas.count() - 1, 0));
|
||||
updateAlternate();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (const auto& sortCall = mSortCallbacksDescend.value(mSortRole))
|
||||
else
|
||||
{
|
||||
QList<QuickListItemBase*> copyObjs;
|
||||
copyObjs = mDatas;
|
||||
std::sort(copyObjs.begin(), copyObjs.end(), sortCall);
|
||||
mDatas = copyObjs;
|
||||
emit dataChanged(index(0, 0), index(mDatas.count() - 1, 0));
|
||||
updateAlternate();
|
||||
// swap param position
|
||||
std::sort(copyObjs.begin(), copyObjs.end(), [=](QuickListItemBase* a, QuickListItemBase* b) { return sortCall(b, a); });
|
||||
}
|
||||
|
||||
mDatas = copyObjs;
|
||||
emit dataChanged(index(0, 0), index(mDatas.count() - 1, 0));
|
||||
updateAlternate();
|
||||
}
|
||||
}
|
||||
|
||||
void QuickListModel::setHeaderRoles(const QStringList& headerRoles)
|
||||
QList<QuickListItemBase*> QuickListModel::allCheckedDatas() const
|
||||
{
|
||||
if (mHeaderRoles == headerRoles)
|
||||
return;
|
||||
|
||||
mHeaderRoles = headerRoles;
|
||||
emit headerRolesChanged(mHeaderRoles);
|
||||
}
|
||||
|
||||
void QuickListModel::setSortOrder(Qt::SortOrder sortOrder)
|
||||
{
|
||||
if (mSortOrder == sortOrder)
|
||||
return;
|
||||
|
||||
mSortOrder = sortOrder;
|
||||
emit sortOrderChanged(mSortOrder);
|
||||
}
|
||||
|
||||
void QuickListModel::setSortRole(const QString& sortRole)
|
||||
{
|
||||
if (mSortRole == sortRole)
|
||||
return;
|
||||
|
||||
mSortRole = sortRole;
|
||||
emit sortRoleChanged(mSortRole);
|
||||
QList<QuickListItemBase*> dataList;
|
||||
for (auto data : mAllDatas)
|
||||
{
|
||||
if (data->isChecked())
|
||||
{
|
||||
dataList.push_back(data);
|
||||
}
|
||||
}
|
||||
return dataList;
|
||||
}
|
||||
|
||||
void QuickListModel::updateAllCheck()
|
||||
@ -319,7 +293,7 @@ void QuickListModel::updateAllCheck()
|
||||
bool allCheck = false;
|
||||
if (!mDatas.empty())
|
||||
{
|
||||
allCheck = std::all_of(mDatas.begin(), mDatas.end(), [](QuickListItemBase* obj) { return obj->isVisible() && obj->isChecked(); });
|
||||
allCheck = std::all_of(mDatas.begin(), mDatas.end(), [](QuickListItemBase* obj) { return obj->isChecked(); });
|
||||
}
|
||||
if (mAllChecked == allCheck)
|
||||
return;
|
||||
@ -330,59 +304,30 @@ void QuickListModel::updateAllCheck()
|
||||
|
||||
void QuickListModel::updateVisibleCount()
|
||||
{
|
||||
int count = mDatas.count(); // std::count_if(mDatas.begin(), mDatas.end(), [](QuickListItemBase *obj) { return obj->isVisible(); });
|
||||
setVisibledCount(count);
|
||||
int count = mDatas.count();
|
||||
set_visibledCount(count);
|
||||
}
|
||||
|
||||
void QuickListModel::updateSelectedCount()
|
||||
{
|
||||
int count = std::count_if(mDatas.begin(), mDatas.end(), [](QuickListItemBase* obj) { return obj->isVisible() && obj->isSelected(); });
|
||||
setSelectedCount(count);
|
||||
int count = std::count_if(mDatas.begin(), mDatas.end(), [](QuickListItemBase* obj) { return obj->isSelected(); });
|
||||
set_selectedCount(count);
|
||||
}
|
||||
|
||||
void QuickListModel::updateCheckedCount()
|
||||
{
|
||||
int count = std::count_if(mDatas.begin(), mDatas.end(), [](QuickListItemBase* obj) { return obj->isVisible() && obj->isChecked(); });
|
||||
setCheckedCount(count);
|
||||
int count = std::count_if(mDatas.begin(), mDatas.end(), [](QuickListItemBase* obj) { return obj->isChecked(); });
|
||||
set_checkedCount(count);
|
||||
}
|
||||
void QuickListModel::updateAlternate()
|
||||
{
|
||||
bool alter = false;
|
||||
for (const auto& obj : mDatas)
|
||||
{
|
||||
if (obj->isVisible())
|
||||
{
|
||||
obj->setIsAlternate(alter);
|
||||
alter = !alter;
|
||||
}
|
||||
obj->set_isAlternate(alter);
|
||||
alter = !alter;
|
||||
}
|
||||
}
|
||||
void QuickListModel::setVisibledCount(int visibledCount)
|
||||
{
|
||||
if (mVisibledCount == visibledCount)
|
||||
return;
|
||||
|
||||
mVisibledCount = visibledCount;
|
||||
emit visibledCountChanged(mVisibledCount);
|
||||
}
|
||||
|
||||
void QuickListModel::setSelectedCount(int selectedCount)
|
||||
{
|
||||
if (mSelectedCount == selectedCount)
|
||||
return;
|
||||
|
||||
mSelectedCount = selectedCount;
|
||||
emit selectedCountChanged(mSelectedCount);
|
||||
}
|
||||
|
||||
void QuickListModel::setCheckedCount(int checkedCount)
|
||||
{
|
||||
if (mCheckedCount == checkedCount)
|
||||
return;
|
||||
|
||||
mCheckedCount = checkedCount;
|
||||
emit checkedCountChanged(mCheckedCount);
|
||||
}
|
||||
|
||||
void QuickListModel::updateCalcInfo()
|
||||
{
|
||||
|
@ -1,21 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include "Common/PropertyHelper.h"
|
||||
#include "QuickListItemBase.h"
|
||||
#include "QuickModelBase.hpp"
|
||||
#include "TaoCommonGlobal.h"
|
||||
#include <QTimer>
|
||||
|
||||
class TAO_API QuickListModel : public QuickModelBase<QuickListItemBase*>
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(bool allChecked READ allChecked WRITE setAllChecked NOTIFY allCheckedChanged)
|
||||
Q_PROPERTY(int visibledCount READ visibledCount NOTIFY visibledCountChanged)
|
||||
Q_PROPERTY(int selectedCount READ selectedCount NOTIFY selectedCountChanged)
|
||||
Q_PROPERTY(int checkedCount READ checkedCount NOTIFY checkedCountChanged)
|
||||
|
||||
Q_PROPERTY(QStringList headerRoles READ headerRoles WRITE setHeaderRoles NOTIFY headerRolesChanged)
|
||||
Q_PROPERTY(Qt::SortOrder sortOrder READ sortOrder WRITE setSortOrder NOTIFY sortOrderChanged)
|
||||
Q_PROPERTY(QString sortRole READ sortRole WRITE setSortRole NOTIFY sortRoleChanged)
|
||||
AUTO_PROPERTY(int, visibledCount, 0)
|
||||
AUTO_PROPERTY(int, selectedCount, 0)
|
||||
AUTO_PROPERTY(int, checkedCount, 0)
|
||||
|
||||
AUTO_PROPERTY(QStringList, headerRoles, {})
|
||||
AUTO_PROPERTY(Qt::SortOrder, sortOrder, Qt::AscendingOrder)
|
||||
AUTO_PROPERTY(QString, sortRole, {})
|
||||
AUTO_PROPERTY(QStringList, noSortRoles, {})
|
||||
public:
|
||||
using Super = QuickModelBase<QuickListItemBase*>;
|
||||
explicit QuickListModel(QObject* parent = nullptr);
|
||||
@ -51,6 +53,8 @@ public:
|
||||
mVisibleCallback = callback;
|
||||
}
|
||||
|
||||
QList<QuickListItemBase*> allCheckedDatas() const;
|
||||
|
||||
//[begin] select
|
||||
Q_INVOKABLE void deselectAll();
|
||||
Q_INVOKABLE void selectAll();
|
||||
@ -66,51 +70,17 @@ public:
|
||||
Q_INVOKABLE void doRelease();
|
||||
|
||||
//[begin] sort
|
||||
const QStringList& headerRoles() const
|
||||
{
|
||||
return mHeaderRoles;
|
||||
}
|
||||
|
||||
Qt::SortOrder sortOrder() const
|
||||
{
|
||||
return mSortOrder;
|
||||
}
|
||||
|
||||
const QString& sortRole() const
|
||||
{
|
||||
return mSortRole;
|
||||
}
|
||||
using SortCallback = std::function<bool(QuickListItemBase*, QuickListItemBase*)>;
|
||||
// Map <key, callBack> ,key should match to headerRoles
|
||||
void setSortCallbacksAscend(const QMap<QString, SortCallback>& callbacksMap)
|
||||
{
|
||||
mSortCallbacksAscend = callbacksMap;
|
||||
}
|
||||
void setSortCallbacksDescend(const QMap<QString, SortCallback>& callbacksMap)
|
||||
{
|
||||
mSortCallbacksDescend = callbacksMap;
|
||||
}
|
||||
|
||||
Q_INVOKABLE virtual void sortByRole();
|
||||
//[end] sort
|
||||
|
||||
//[begin] count
|
||||
int visibledCount() const
|
||||
{
|
||||
return mVisibledCount;
|
||||
}
|
||||
|
||||
int selectedCount() const
|
||||
{
|
||||
return mSelectedCount;
|
||||
}
|
||||
|
||||
int checkedCount() const
|
||||
{
|
||||
return mCheckedCount;
|
||||
}
|
||||
//[end] count
|
||||
|
||||
void updateCalcInfo() override;
|
||||
|
||||
Q_INVOKABLE void notifyScrollTo(int index)
|
||||
@ -120,33 +90,12 @@ public:
|
||||
public slots:
|
||||
void setAllChecked(bool allChecked);
|
||||
|
||||
void setHeaderRoles(const QStringList& headerRoles);
|
||||
|
||||
void setSortOrder(Qt::SortOrder sortOrder);
|
||||
|
||||
void setSortRole(const QString& sortRole);
|
||||
|
||||
void setVisibledCount(int visibledCount);
|
||||
void setSelectedCount(int selectedCount);
|
||||
void setCheckedCount(int checkedCount);
|
||||
|
||||
signals:
|
||||
void scrollTo(int index);
|
||||
void allCheckedChanged(bool allChecked);
|
||||
void visibledCountChanged(int visibledCount);
|
||||
|
||||
void selectedAction();
|
||||
|
||||
void selectedCountChanged(int selectedCount);
|
||||
|
||||
void checkedCountChanged(int checkedCount);
|
||||
|
||||
void headerRolesChanged(const QStringList& headerRoles);
|
||||
|
||||
void sortOrderChanged(Qt::SortOrder sortOrder);
|
||||
|
||||
void sortRoleChanged(const QString& sortRole);
|
||||
|
||||
void signalUpdateCalcCount();
|
||||
|
||||
void beginSearch();
|
||||
@ -164,15 +113,10 @@ protected:
|
||||
protected:
|
||||
bool mAllChecked = false;
|
||||
bool mIsPressed = false;
|
||||
Qt::SortOrder mSortOrder = Qt::AscendingOrder;
|
||||
int mVisibledCount = 0;
|
||||
int mSelectedCount = 0;
|
||||
int mCheckedCount = 0;
|
||||
|
||||
int mLastPressedRow = -1;
|
||||
QStringList mHeaderRoles;
|
||||
QString mSortRole;
|
||||
|
||||
QMap<QString, SortCallback> mSortCallbacksAscend;
|
||||
QMap<QString, SortCallback> mSortCallbacksDescend;
|
||||
QString mSearchkey;
|
||||
QTimer mSearchTimer;
|
||||
VisibleCallback mVisibleCallback;
|
||||
|
@ -96,7 +96,9 @@ QuickModelBase<T>::QuickModelBase(const QList<T>& datas, QObject* parent)
|
||||
, mDatas(datas)
|
||||
{
|
||||
for (auto it = datas.begin(); it != datas.end(); it++)
|
||||
{
|
||||
mAllDatas.insert(*it);
|
||||
}
|
||||
}
|
||||
template <class T>
|
||||
QuickModelBase<T>::~QuickModelBase()
|
||||
@ -140,9 +142,14 @@ void QuickModelBase<T>::resetData(const QList<T>& datas)
|
||||
beginResetModel();
|
||||
mDatas = datas;
|
||||
for (auto it = datas.begin(); it != datas.end(); it++)
|
||||
{
|
||||
mAllDatas.insert(*it);
|
||||
}
|
||||
endResetModel();
|
||||
qDeleteAll(oldObjs);
|
||||
if (!oldObjs.isEmpty())
|
||||
{
|
||||
qDeleteAll(oldObjs);
|
||||
}
|
||||
updateCalcInfo();
|
||||
}
|
||||
|
||||
@ -156,7 +163,9 @@ void QuickModelBase<T>::append(const QList<T>& datas)
|
||||
beginInsertRows({}, mDatas.count(), mDatas.count() + datas.count() - 1);
|
||||
mDatas.append(datas);
|
||||
for (auto it = datas.begin(); it != datas.end(); it++)
|
||||
{
|
||||
mAllDatas.insert(*it);
|
||||
}
|
||||
endInsertRows();
|
||||
updateCalcInfo();
|
||||
}
|
||||
@ -195,7 +204,10 @@ void QuickModelBase<T>::clear()
|
||||
{
|
||||
beginRemoveRows({}, 0, mdatacount - 1);
|
||||
}
|
||||
qDeleteAll(mAllDatas);
|
||||
if (!mAllDatas.isEmpty())
|
||||
{
|
||||
qDeleteAll(mAllDatas);
|
||||
}
|
||||
mDatas.clear();
|
||||
mAllDatas.clear();
|
||||
|
||||
|
@ -67,84 +67,6 @@ Item {
|
||||
}
|
||||
}
|
||||
|
||||
//12 items in one page
|
||||
property int itemCountPerPage:12
|
||||
property int currentPage:1
|
||||
|
||||
//Pages round down
|
||||
property int page:cusView.count/itemCountPerPage
|
||||
|
||||
//Actual number of pages (exactly divide pageCount = page, otherwise pageCount is 1 page more than page)
|
||||
property int pageCount: page*itemCountPerPage<cusView.count?page+1:page
|
||||
|
||||
//--listView Page turning function
|
||||
function changePage(next){
|
||||
var currentViewIndex = cusView.indexAt(cusView.contentX,cusView.contentY)
|
||||
if(currentViewIndex%itemCountPerPage!==0)
|
||||
{ //the last page
|
||||
if(!next){//Page forward
|
||||
cusView.positionViewAtIndex((page-1)*itemCountPerPage,ListView.Beginning)
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var i = 0
|
||||
for(; i<pageCount;i++){
|
||||
if(i*itemCountPerPage<=currentViewIndex&¤tViewIndex<(i+1)*itemCountPerPage){
|
||||
//Find which page the current ViewIndex belongs to
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
console.log("currentViewIndex ",currentViewIndex)
|
||||
|
||||
//Page forward and backward
|
||||
currentViewIndex =(next?(i+1):(i-1))*itemCountPerPage
|
||||
|
||||
//Ranges
|
||||
currentViewIndex = currentViewIndex<0?0:currentViewIndex
|
||||
currentViewIndex = currentViewIndex>=cusView.count - 1?cusView.count - 1:currentViewIndex
|
||||
|
||||
console.log("nextViewIndex ",currentViewIndex)
|
||||
|
||||
cusView.positionViewAtIndex(currentViewIndex,ListView.Beginning)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
//--Calculate page number
|
||||
function showPage(){
|
||||
var currentViewIndex = cusView.indexAt(cusView.contentX,cusView.contentY)
|
||||
if(currentViewIndex%itemCountPerPage!==0){
|
||||
//the last page
|
||||
console.log("final")
|
||||
currentPage = pageCount
|
||||
}else{
|
||||
for(var i = 0; i<pageCount;i++){
|
||||
if(i*itemCountPerPage<=currentViewIndex&¤tViewIndex<(i+1)*itemCountPerPage){
|
||||
//Find which page the current ViewIndex belongs to
|
||||
currentPage = i + 1
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
console.log("currentPage",currentPage)
|
||||
console.log("page",page)
|
||||
console.log("pageCount",pageCount)
|
||||
|
||||
}
|
||||
|
||||
signal lastpage()
|
||||
onLastpage:{
|
||||
changePage(false)
|
||||
showPage()
|
||||
}
|
||||
signal nextpage()
|
||||
onNextpage:{
|
||||
changePage(true)
|
||||
showPage()
|
||||
}
|
||||
|
||||
CusTableView {
|
||||
id: cusView
|
||||
y: cusHeader.y + cusHeader.height
|
||||
@ -247,7 +169,6 @@ Item {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Column {
|
||||
x: cusHeader.splitingIndex > 0 ? (cusHeader.x + cusHeader.xList[cusHeader.splitingIndex + 1]) : 0
|
||||
y: cusView.y
|
||||
@ -369,26 +290,6 @@ Item {
|
||||
deviceAddModel.deselectAll()
|
||||
}
|
||||
}
|
||||
CusButton_Blue {
|
||||
width: 120
|
||||
text: qsTr("Last Page") + trans.transString
|
||||
onClicked: {
|
||||
mainItem.lastpage()
|
||||
// changePage(false)
|
||||
// showPage()
|
||||
}
|
||||
}
|
||||
CusButton_Blue {
|
||||
width: 120
|
||||
text: qsTr("Next Page") + trans.transString
|
||||
onClicked: {
|
||||
mainItem.nextpage()
|
||||
// changePage(true)
|
||||
// showPage()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
CusTextField {
|
||||
|
@ -21,7 +21,26 @@ DeviceAddModel::DeviceAddModel(QObject* parent)
|
||||
: QuickListModel(parent)
|
||||
, d(new DeviceAddModelPrivate)
|
||||
{
|
||||
setHeaderRoles(sHeaderRoles);
|
||||
set_headerRoles(sHeaderRoles);
|
||||
set_sortRole("name");
|
||||
QMap<QString, SortCallback> maps;
|
||||
maps["name"] = [](QuickListItemBase* b1, QuickListItemBase* b2) -> bool {
|
||||
const auto& d1 = static_cast<DeviceAddItem*>(b1);
|
||||
const auto& d2 = static_cast<DeviceAddItem*>(b2);
|
||||
return d1->name() < d2->name();
|
||||
};
|
||||
maps["address"] = [](QuickListItemBase* b1, QuickListItemBase* b2) -> bool {
|
||||
const auto& d1 = static_cast<DeviceAddItem*>(b1);
|
||||
const auto& d2 = static_cast<DeviceAddItem*>(b2);
|
||||
return d1->toIPv4Address() < d2->toIPv4Address();
|
||||
};
|
||||
maps["modelString"] = [](QuickListItemBase* b1, QuickListItemBase* b2) -> bool {
|
||||
const auto& d1 = static_cast<DeviceAddItem*>(b1);
|
||||
const auto& d2 = static_cast<DeviceAddItem*>(b2);
|
||||
return d1->modelString() < d2->modelString();
|
||||
};
|
||||
|
||||
QuickListModel::setSortCallbacksAscend(maps);
|
||||
}
|
||||
|
||||
DeviceAddModel::~DeviceAddModel()
|
||||
@ -29,38 +48,9 @@ DeviceAddModel::~DeviceAddModel()
|
||||
delete d;
|
||||
}
|
||||
|
||||
void DeviceAddModel::sortByRole()
|
||||
{
|
||||
if (mDatas.count() <= 1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
int index = sHeaderRoles.indexOf(mSortRole);
|
||||
switch (index)
|
||||
{
|
||||
case 0:
|
||||
{
|
||||
sortByName(mSortOrder);
|
||||
break;
|
||||
}
|
||||
case 1:
|
||||
{
|
||||
sortByAddress(mSortOrder);
|
||||
break;
|
||||
}
|
||||
case 2:
|
||||
{
|
||||
sortByModel(mSortOrder);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
updateAlternate();
|
||||
}
|
||||
void DeviceAddModel::initData()
|
||||
{
|
||||
const int N = 50000;
|
||||
const int N = 500;
|
||||
|
||||
QList<QuickListItemBase*> objs;
|
||||
objs.reserve(N);
|
||||
@ -80,6 +70,7 @@ void DeviceAddModel::initData()
|
||||
auto micro = std::chrono::duration_cast<std::chrono::milliseconds>(c2 - c1).count();
|
||||
qWarning() << "general" << N << "cost" << micro << "ms";
|
||||
resetData(objs);
|
||||
sortByRole();
|
||||
}
|
||||
|
||||
void DeviceAddModel::addOne()
|
||||
@ -118,7 +109,7 @@ void DeviceAddModel::insertBeforeSelected()
|
||||
for (int i = 0; i < mDatas.count(); ++i)
|
||||
{
|
||||
const auto& obj = mDatas.at(i);
|
||||
if (obj->isVisible() && obj->isSelected())
|
||||
if (obj->isSelected())
|
||||
{
|
||||
pos = i;
|
||||
break;
|
||||
@ -148,7 +139,7 @@ void DeviceAddModel::removeSelected()
|
||||
for (int i = 0; i < mDatas.count();)
|
||||
{
|
||||
const auto& obj = mDatas.at(i);
|
||||
if (obj->isVisible() && obj->isSelected())
|
||||
if (obj->isSelected())
|
||||
{
|
||||
removeAt(i);
|
||||
}
|
||||
@ -164,7 +155,7 @@ void DeviceAddModel::removeChecked()
|
||||
for (int i = 0; i < mDatas.count();)
|
||||
{
|
||||
const auto& obj = mDatas.at(i);
|
||||
if (obj->isVisible() && obj->isChecked())
|
||||
if (obj->isChecked())
|
||||
{
|
||||
removeAt(i);
|
||||
}
|
||||
@ -180,63 +171,6 @@ void DeviceAddModel::removeRow(int row)
|
||||
removeAt(row);
|
||||
}
|
||||
|
||||
void DeviceAddModel::sortByName(Qt::SortOrder order)
|
||||
{
|
||||
QList<QuickListItemBase*> copyObjs = mDatas;
|
||||
if (order == Qt::SortOrder::AscendingOrder)
|
||||
{
|
||||
std::sort(copyObjs.begin(), copyObjs.end(), [](QuickListItemBase* obj1, QuickListItemBase* obj2) -> bool {
|
||||
return (static_cast<DeviceAddItem*>(obj1))->name() < (static_cast<DeviceAddItem*>(obj2))->name();
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
std::sort(copyObjs.begin(), copyObjs.end(), [](QuickListItemBase* obj1, QuickListItemBase* obj2) -> bool {
|
||||
return (static_cast<DeviceAddItem*>(obj1))->name() > (static_cast<DeviceAddItem*>(obj2))->name();
|
||||
});
|
||||
}
|
||||
mDatas = copyObjs;
|
||||
emit dataChanged(index(0, 0), index(mDatas.count() - 1, 0));
|
||||
}
|
||||
|
||||
void DeviceAddModel::sortByAddress(Qt::SortOrder order)
|
||||
{
|
||||
QList<QuickListItemBase*> copyObjs = mDatas;
|
||||
if (order == Qt::SortOrder::AscendingOrder)
|
||||
{
|
||||
std::sort(copyObjs.begin(), copyObjs.end(), [=](QuickListItemBase* obj1, QuickListItemBase* obj2) -> bool {
|
||||
return static_cast<DeviceAddItem*>(obj1)->toIPv4Address() < static_cast<DeviceAddItem*>(obj2)->toIPv4Address();
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
std::sort(copyObjs.begin(), copyObjs.end(), [=](QuickListItemBase* obj1, QuickListItemBase* obj2) -> bool {
|
||||
return static_cast<DeviceAddItem*>(obj1)->toIPv4Address() > static_cast<DeviceAddItem*>(obj2)->toIPv4Address();
|
||||
});
|
||||
}
|
||||
mDatas = copyObjs;
|
||||
emit dataChanged(index(0, 0), index(mDatas.count() - 1, 0));
|
||||
}
|
||||
|
||||
void DeviceAddModel::sortByModel(Qt::SortOrder order)
|
||||
{
|
||||
QList<QuickListItemBase*> copyObjs = mDatas;
|
||||
if (order == Qt::SortOrder::AscendingOrder)
|
||||
{
|
||||
std::sort(copyObjs.begin(), copyObjs.end(), [](QuickListItemBase* obj1, QuickListItemBase* obj2) -> bool {
|
||||
return (static_cast<DeviceAddItem*>(obj1))->modelString().toULongLong() < (static_cast<DeviceAddItem*>(obj2))->modelString().toULongLong();
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
std::sort(copyObjs.begin(), copyObjs.end(), [](QuickListItemBase* obj1, QuickListItemBase* obj2) -> bool {
|
||||
return (static_cast<DeviceAddItem*>(obj1))->modelString().toULongLong() > (static_cast<DeviceAddItem*>(obj2))->modelString().toULongLong();
|
||||
});
|
||||
}
|
||||
mDatas = copyObjs;
|
||||
emit dataChanged(index(0, 0), index(mDatas.count() - 1, 0));
|
||||
}
|
||||
|
||||
DeviceAddItem* DeviceAddModel::genOne(uint32_t value)
|
||||
{
|
||||
auto item = new DeviceAddItem;
|
||||
|
@ -7,10 +7,11 @@ class DeviceAddItem;
|
||||
class DeviceAddModel : public QuickListModel
|
||||
{
|
||||
Q_OBJECT
|
||||
using Super = QuickListModel;
|
||||
|
||||
public:
|
||||
explicit DeviceAddModel(QObject* parent = nullptr);
|
||||
virtual ~DeviceAddModel() override;
|
||||
Q_INVOKABLE virtual void sortByRole() override;
|
||||
public slots:
|
||||
void doUpdateName(int row, const QString& name);
|
||||
|
||||
@ -29,10 +30,6 @@ public slots:
|
||||
void removeRow(int row);
|
||||
|
||||
private:
|
||||
void sortByName(Qt::SortOrder order);
|
||||
void sortByAddress(Qt::SortOrder order);
|
||||
void sortByModel(Qt::SortOrder order);
|
||||
|
||||
DeviceAddItem* genOne(uint32_t value);
|
||||
|
||||
private:
|
||||
|
@ -7,7 +7,6 @@ import "../.."
|
||||
Rectangle {
|
||||
id: cusTableRow
|
||||
height: visible ? CusConfig.fixedHeight : 0
|
||||
visible: dataObj ? dataObj["isVisible"] : false
|
||||
|
||||
property bool isSelected: dataObj ? dataObj["isSelected"] : false
|
||||
property bool isChecked: dataObj ? dataObj["isChecked"] : false
|
||||
|
Loading…
x
Reference in New Issue
Block a user