1
0
mirror of https://github.com/jaredtao/TaoQuick.git synced 2025-01-31 21:22:58 +08:00

model 优化

This commit is contained in:
jared 2023-05-06 10:02:01 +08:00
parent 5295584e3f
commit fb7967176f
8 changed files with 109 additions and 446 deletions

View File

@ -1,106 +1,37 @@
#pragma once #pragma once
#include "Common/PropertyHelper.h"
#include "TaoCommonGlobal.h" #include "TaoCommonGlobal.h"
#include <QObject> #include <QObject>
class TAO_API QuickListItemBase : public QObject class TAO_API QuickListItemBase : public QObject
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(bool isChecked READ isChecked WRITE setIsChecked NOTIFY isCheckedChanged) AUTO_PROPERTY(bool, isChecked, false)
Q_PROPERTY(bool isSelected READ isSelected WRITE setIsSelected NOTIFY isSelectedChanged) AUTO_PROPERTY(bool, isSelected, false)
Q_PROPERTY(bool isVisible READ isVisible WRITE setIsVisible NOTIFY isVisibleChanged) AUTO_PROPERTY(bool, isAlternate, false)
Q_PROPERTY(bool isAlternate READ isAlternate WRITE setIsAlternate NOTIFY isAlternateChanged)
public: public:
explicit QuickListItemBase(QObject* parent = nullptr); explicit QuickListItemBase(QObject* parent = nullptr);
~QuickListItemBase() override; virtual ~QuickListItemBase() override;
QuickListItemBase(const QuickListItemBase& other) QuickListItemBase(const QuickListItemBase& other)
{ {
setIsChecked(other.isChecked()); set_isChecked(other.isChecked());
setIsSelected(other.isSelected()); set_isSelected(other.isSelected());
setIsVisible(other.isVisible()); set_isAlternate(other.isAlternate());
setIsAlternate(other.isAlternate());
} }
QuickListItemBase& operator=(const QuickListItemBase& other) QuickListItemBase& operator=(const QuickListItemBase& other)
{ {
setIsChecked(other.isChecked()); set_isChecked(other.isChecked());
setIsSelected(other.isSelected()); set_isSelected(other.isSelected());
setIsVisible(other.isVisible()); set_isAlternate(other.isAlternate());
setIsAlternate(other.isAlternate());
return *this; 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. // Model call this for search. return true if contents match key, others return false.
virtual bool match(const QString& key) virtual bool match(const QString& key)
{ {
Q_UNUSED(key) Q_UNUSED(key)
return true; 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: private:
bool mIsChecked = false;
bool mIsSelected = false;
bool mIsVisible = true;
bool mIsAlternate = false;
}; };

View File

@ -19,14 +19,14 @@ void QuickListModel::check(int row, bool checked)
return; return;
} }
mDatas.at(row)->setIsChecked(checked); mDatas.at(row)->set_isChecked(checked);
if (mDatas.at(row)->isSelected()) if (mDatas.at(row)->isSelected())
{ {
for (const auto& obj : mDatas) 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) for (const auto& obj : mDatas)
{ {
if (obj->isVisible() && false == obj->isChecked()) if (false == obj->isChecked())
{ {
allCheck = false; allCheck = false;
break; break;
@ -58,10 +58,8 @@ void QuickListModel::setAllChecked(bool allChecked)
{ {
for (const auto& obj : mDatas) for (const auto& obj : mDatas)
{ {
if (obj->isVisible())
{ obj->set_isChecked(allChecked);
obj->setIsChecked(allChecked);
}
} }
updateCheckedCount(); updateCheckedCount();
if (mAllChecked == allChecked) if (mAllChecked == allChecked)
@ -120,10 +118,8 @@ void QuickListModel::deselectAll()
{ {
for (const auto& obj : mDatas) for (const auto& obj : mDatas)
{ {
if (obj->isVisible())
{ obj->set_isSelected(false);
obj->setIsSelected(false);
}
} }
updateSelectedCount(); updateSelectedCount();
emit selectedAction(); emit selectedAction();
@ -133,10 +129,8 @@ void QuickListModel::selectAll()
{ {
for (const auto& obj : mDatas) for (const auto& obj : mDatas)
{ {
if (obj->isVisible())
{ obj->set_isSelected(true);
obj->setIsSelected(true);
}
} }
updateSelectedCount(); updateSelectedCount();
emit selectedAction(); emit selectedAction();
@ -157,7 +151,7 @@ void QuickListModel::select(int row)
{ {
return; return;
} }
mDatas.at(row)->setIsSelected(true); mDatas.at(row)->set_isSelected(true);
updateSelectedCount(); updateSelectedCount();
emit selectedAction(); emit selectedAction();
} }
@ -168,7 +162,7 @@ void QuickListModel::deselect(int row)
{ {
return; return;
} }
mDatas.at(row)->setIsSelected(false); mDatas.at(row)->set_isSelected(false);
updateSelectedCount(); updateSelectedCount();
emit selectedAction(); emit selectedAction();
} }
@ -179,7 +173,7 @@ void QuickListModel::selectRange(int from, int to)
int maxRow = qMax(from, to); int maxRow = qMax(from, to);
for (int i = 0; i < mDatas.size(); ++i) 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(); updateSelectedCount();
emit selectedAction(); emit selectedAction();
@ -189,7 +183,7 @@ void QuickListModel::selectSingle(int row)
{ {
for (int i = 0; i < mDatas.size(); ++i) for (int i = 0; i < mDatas.size(); ++i)
{ {
mDatas.at(i)->setIsSelected(i == row); mDatas.at(i)->set_isSelected(i == row);
} }
updateSelectedCount(); updateSelectedCount();
emit selectedAction(); emit selectedAction();
@ -261,57 +255,37 @@ void QuickListModel::sortByRole()
{ {
return; return;
} }
if (mSortOrder == Qt::SortOrder::AscendingOrder) if (const auto& sortCall = mSortCallbacksAscend.value(sortRole()))
{
if (const auto& sortCall = mSortCallbacksAscend.value(mSortRole))
{ {
QList<QuickListItemBase*> copyObjs; QList<QuickListItemBase*> copyObjs;
copyObjs = mDatas; copyObjs = mDatas;
if (sortOrder() == Qt::SortOrder::AscendingOrder)
{
std::sort(copyObjs.begin(), copyObjs.end(), sortCall); std::sort(copyObjs.begin(), copyObjs.end(), sortCall);
mDatas = copyObjs;
emit dataChanged(index(0, 0), index(mDatas.count() - 1, 0));
updateAlternate();
}
} }
else else
{ {
if (const auto& sortCall = mSortCallbacksDescend.value(mSortRole)) // swap param position
{ std::sort(copyObjs.begin(), copyObjs.end(), [=](QuickListItemBase* a, QuickListItemBase* b) { return sortCall(b, a); });
QList<QuickListItemBase*> copyObjs; }
copyObjs = mDatas;
std::sort(copyObjs.begin(), copyObjs.end(), sortCall);
mDatas = copyObjs; mDatas = copyObjs;
emit dataChanged(index(0, 0), index(mDatas.count() - 1, 0)); emit dataChanged(index(0, 0), index(mDatas.count() - 1, 0));
updateAlternate(); updateAlternate();
} }
}
QList<QuickListItemBase*> QuickListModel::allCheckedDatas() const
{
QList<QuickListItemBase*> dataList;
for (auto data : mAllDatas)
{
if (data->isChecked())
{
dataList.push_back(data);
} }
} }
return dataList;
void QuickListModel::setHeaderRoles(const QStringList& headerRoles)
{
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);
} }
void QuickListModel::updateAllCheck() void QuickListModel::updateAllCheck()
@ -319,7 +293,7 @@ void QuickListModel::updateAllCheck()
bool allCheck = false; bool allCheck = false;
if (!mDatas.empty()) 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) if (mAllChecked == allCheck)
return; return;
@ -330,58 +304,29 @@ void QuickListModel::updateAllCheck()
void QuickListModel::updateVisibleCount() void QuickListModel::updateVisibleCount()
{ {
int count = mDatas.count(); // std::count_if(mDatas.begin(), mDatas.end(), [](QuickListItemBase *obj) { return obj->isVisible(); }); int count = mDatas.count();
setVisibledCount(count); set_visibledCount(count);
} }
void QuickListModel::updateSelectedCount() void QuickListModel::updateSelectedCount()
{ {
int count = std::count_if(mDatas.begin(), mDatas.end(), [](QuickListItemBase* obj) { return obj->isVisible() && obj->isSelected(); }); int count = std::count_if(mDatas.begin(), mDatas.end(), [](QuickListItemBase* obj) { return obj->isSelected(); });
setSelectedCount(count); set_selectedCount(count);
} }
void QuickListModel::updateCheckedCount() void QuickListModel::updateCheckedCount()
{ {
int count = std::count_if(mDatas.begin(), mDatas.end(), [](QuickListItemBase* obj) { return obj->isVisible() && obj->isChecked(); }); int count = std::count_if(mDatas.begin(), mDatas.end(), [](QuickListItemBase* obj) { return obj->isChecked(); });
setCheckedCount(count); set_checkedCount(count);
} }
void QuickListModel::updateAlternate() void QuickListModel::updateAlternate()
{ {
bool alter = false; bool alter = false;
for (const auto& obj : mDatas) for (const auto& obj : mDatas)
{ {
if (obj->isVisible()) obj->set_isAlternate(alter);
{
obj->setIsAlternate(alter);
alter = !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() void QuickListModel::updateCalcInfo()

View File

@ -1,21 +1,23 @@
#pragma once #pragma once
#include "Common/PropertyHelper.h"
#include "QuickListItemBase.h" #include "QuickListItemBase.h"
#include "QuickModelBase.hpp" #include "QuickModelBase.hpp"
#include "TaoCommonGlobal.h" #include "TaoCommonGlobal.h"
#include <QTimer> #include <QTimer>
class TAO_API QuickListModel : public QuickModelBase<QuickListItemBase*> class TAO_API QuickListModel : public QuickModelBase<QuickListItemBase*>
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(bool allChecked READ allChecked WRITE setAllChecked NOTIFY allCheckedChanged) Q_PROPERTY(bool allChecked READ allChecked WRITE setAllChecked NOTIFY allCheckedChanged)
Q_PROPERTY(int visibledCount READ visibledCount NOTIFY visibledCountChanged) AUTO_PROPERTY(int, visibledCount, 0)
Q_PROPERTY(int selectedCount READ selectedCount NOTIFY selectedCountChanged) AUTO_PROPERTY(int, selectedCount, 0)
Q_PROPERTY(int checkedCount READ checkedCount NOTIFY checkedCountChanged) AUTO_PROPERTY(int, checkedCount, 0)
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(QStringList, headerRoles, {})
AUTO_PROPERTY(Qt::SortOrder, sortOrder, Qt::AscendingOrder)
AUTO_PROPERTY(QString, sortRole, {})
AUTO_PROPERTY(QStringList, noSortRoles, {})
public: public:
using Super = QuickModelBase<QuickListItemBase*>; using Super = QuickModelBase<QuickListItemBase*>;
explicit QuickListModel(QObject* parent = nullptr); explicit QuickListModel(QObject* parent = nullptr);
@ -51,6 +53,8 @@ public:
mVisibleCallback = callback; mVisibleCallback = callback;
} }
QList<QuickListItemBase*> allCheckedDatas() const;
//[begin] select //[begin] select
Q_INVOKABLE void deselectAll(); Q_INVOKABLE void deselectAll();
Q_INVOKABLE void selectAll(); Q_INVOKABLE void selectAll();
@ -66,51 +70,17 @@ public:
Q_INVOKABLE void doRelease(); Q_INVOKABLE void doRelease();
//[begin] sort //[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*)>; using SortCallback = std::function<bool(QuickListItemBase*, QuickListItemBase*)>;
// Map <key, callBack> ,key should match to headerRoles // Map <key, callBack> ,key should match to headerRoles
void setSortCallbacksAscend(const QMap<QString, SortCallback>& callbacksMap) void setSortCallbacksAscend(const QMap<QString, SortCallback>& callbacksMap)
{ {
mSortCallbacksAscend = callbacksMap; mSortCallbacksAscend = callbacksMap;
} }
void setSortCallbacksDescend(const QMap<QString, SortCallback>& callbacksMap)
{
mSortCallbacksDescend = callbacksMap;
}
Q_INVOKABLE virtual void sortByRole(); Q_INVOKABLE virtual void sortByRole();
//[end] sort //[end] sort
//[begin] count
int visibledCount() const
{
return mVisibledCount;
}
int selectedCount() const
{
return mSelectedCount;
}
int checkedCount() const
{
return mCheckedCount;
}
//[end] count
void updateCalcInfo() override; void updateCalcInfo() override;
Q_INVOKABLE void notifyScrollTo(int index) Q_INVOKABLE void notifyScrollTo(int index)
@ -120,33 +90,12 @@ public:
public slots: public slots:
void setAllChecked(bool allChecked); 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: signals:
void scrollTo(int index); void scrollTo(int index);
void allCheckedChanged(bool allChecked); void allCheckedChanged(bool allChecked);
void visibledCountChanged(int visibledCount);
void selectedAction(); 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 signalUpdateCalcCount();
void beginSearch(); void beginSearch();
@ -164,15 +113,10 @@ protected:
protected: protected:
bool mAllChecked = false; bool mAllChecked = false;
bool mIsPressed = false; bool mIsPressed = false;
Qt::SortOrder mSortOrder = Qt::AscendingOrder;
int mVisibledCount = 0;
int mSelectedCount = 0;
int mCheckedCount = 0;
int mLastPressedRow = -1; int mLastPressedRow = -1;
QStringList mHeaderRoles;
QString mSortRole;
QMap<QString, SortCallback> mSortCallbacksAscend; QMap<QString, SortCallback> mSortCallbacksAscend;
QMap<QString, SortCallback> mSortCallbacksDescend;
QString mSearchkey; QString mSearchkey;
QTimer mSearchTimer; QTimer mSearchTimer;
VisibleCallback mVisibleCallback; VisibleCallback mVisibleCallback;

View File

@ -96,7 +96,9 @@ QuickModelBase<T>::QuickModelBase(const QList<T>& datas, QObject* parent)
, mDatas(datas) , mDatas(datas)
{ {
for (auto it = datas.begin(); it != datas.end(); it++) for (auto it = datas.begin(); it != datas.end(); it++)
{
mAllDatas.insert(*it); mAllDatas.insert(*it);
}
} }
template <class T> template <class T>
QuickModelBase<T>::~QuickModelBase() QuickModelBase<T>::~QuickModelBase()
@ -140,9 +142,14 @@ void QuickModelBase<T>::resetData(const QList<T>& datas)
beginResetModel(); beginResetModel();
mDatas = datas; mDatas = datas;
for (auto it = datas.begin(); it != datas.end(); it++) for (auto it = datas.begin(); it != datas.end(); it++)
{
mAllDatas.insert(*it); mAllDatas.insert(*it);
}
endResetModel(); endResetModel();
if (!oldObjs.isEmpty())
{
qDeleteAll(oldObjs); qDeleteAll(oldObjs);
}
updateCalcInfo(); updateCalcInfo();
} }
@ -156,7 +163,9 @@ void QuickModelBase<T>::append(const QList<T>& datas)
beginInsertRows({}, mDatas.count(), mDatas.count() + datas.count() - 1); beginInsertRows({}, mDatas.count(), mDatas.count() + datas.count() - 1);
mDatas.append(datas); mDatas.append(datas);
for (auto it = datas.begin(); it != datas.end(); it++) for (auto it = datas.begin(); it != datas.end(); it++)
{
mAllDatas.insert(*it); mAllDatas.insert(*it);
}
endInsertRows(); endInsertRows();
updateCalcInfo(); updateCalcInfo();
} }
@ -195,7 +204,10 @@ void QuickModelBase<T>::clear()
{ {
beginRemoveRows({}, 0, mdatacount - 1); beginRemoveRows({}, 0, mdatacount - 1);
} }
if (!mAllDatas.isEmpty())
{
qDeleteAll(mAllDatas); qDeleteAll(mAllDatas);
}
mDatas.clear(); mDatas.clear();
mAllDatas.clear(); mAllDatas.clear();

View File

@ -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&&currentViewIndex<(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&&currentViewIndex<(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 { CusTableView {
id: cusView id: cusView
y: cusHeader.y + cusHeader.height y: cusHeader.y + cusHeader.height
@ -247,7 +169,6 @@ Item {
} }
} }
} }
Column { Column {
x: cusHeader.splitingIndex > 0 ? (cusHeader.x + cusHeader.xList[cusHeader.splitingIndex + 1]) : 0 x: cusHeader.splitingIndex > 0 ? (cusHeader.x + cusHeader.xList[cusHeader.splitingIndex + 1]) : 0
y: cusView.y y: cusView.y
@ -369,26 +290,6 @@ Item {
deviceAddModel.deselectAll() 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 { CusTextField {

View File

@ -21,7 +21,26 @@ DeviceAddModel::DeviceAddModel(QObject* parent)
: QuickListModel(parent) : QuickListModel(parent)
, d(new DeviceAddModelPrivate) , 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() DeviceAddModel::~DeviceAddModel()
@ -29,38 +48,9 @@ DeviceAddModel::~DeviceAddModel()
delete d; 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() void DeviceAddModel::initData()
{ {
const int N = 50000; const int N = 500;
QList<QuickListItemBase*> objs; QList<QuickListItemBase*> objs;
objs.reserve(N); objs.reserve(N);
@ -80,6 +70,7 @@ void DeviceAddModel::initData()
auto micro = std::chrono::duration_cast<std::chrono::milliseconds>(c2 - c1).count(); auto micro = std::chrono::duration_cast<std::chrono::milliseconds>(c2 - c1).count();
qWarning() << "general" << N << "cost" << micro << "ms"; qWarning() << "general" << N << "cost" << micro << "ms";
resetData(objs); resetData(objs);
sortByRole();
} }
void DeviceAddModel::addOne() void DeviceAddModel::addOne()
@ -118,7 +109,7 @@ void DeviceAddModel::insertBeforeSelected()
for (int i = 0; i < mDatas.count(); ++i) for (int i = 0; i < mDatas.count(); ++i)
{ {
const auto& obj = mDatas.at(i); const auto& obj = mDatas.at(i);
if (obj->isVisible() && obj->isSelected()) if (obj->isSelected())
{ {
pos = i; pos = i;
break; break;
@ -148,7 +139,7 @@ void DeviceAddModel::removeSelected()
for (int i = 0; i < mDatas.count();) for (int i = 0; i < mDatas.count();)
{ {
const auto& obj = mDatas.at(i); const auto& obj = mDatas.at(i);
if (obj->isVisible() && obj->isSelected()) if (obj->isSelected())
{ {
removeAt(i); removeAt(i);
} }
@ -164,7 +155,7 @@ void DeviceAddModel::removeChecked()
for (int i = 0; i < mDatas.count();) for (int i = 0; i < mDatas.count();)
{ {
const auto& obj = mDatas.at(i); const auto& obj = mDatas.at(i);
if (obj->isVisible() && obj->isChecked()) if (obj->isChecked())
{ {
removeAt(i); removeAt(i);
} }
@ -180,63 +171,6 @@ void DeviceAddModel::removeRow(int row)
removeAt(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) DeviceAddItem* DeviceAddModel::genOne(uint32_t value)
{ {
auto item = new DeviceAddItem; auto item = new DeviceAddItem;

View File

@ -7,10 +7,11 @@ class DeviceAddItem;
class DeviceAddModel : public QuickListModel class DeviceAddModel : public QuickListModel
{ {
Q_OBJECT Q_OBJECT
using Super = QuickListModel;
public: public:
explicit DeviceAddModel(QObject* parent = nullptr); explicit DeviceAddModel(QObject* parent = nullptr);
virtual ~DeviceAddModel() override; virtual ~DeviceAddModel() override;
Q_INVOKABLE virtual void sortByRole() override;
public slots: public slots:
void doUpdateName(int row, const QString& name); void doUpdateName(int row, const QString& name);
@ -29,10 +30,6 @@ public slots:
void removeRow(int row); void removeRow(int row);
private: private:
void sortByName(Qt::SortOrder order);
void sortByAddress(Qt::SortOrder order);
void sortByModel(Qt::SortOrder order);
DeviceAddItem* genOne(uint32_t value); DeviceAddItem* genOne(uint32_t value);
private: private:

View File

@ -7,7 +7,6 @@ import "../.."
Rectangle { Rectangle {
id: cusTableRow id: cusTableRow
height: visible ? CusConfig.fixedHeight : 0 height: visible ? CusConfig.fixedHeight : 0
visible: dataObj ? dataObj["isVisible"] : false
property bool isSelected: dataObj ? dataObj["isSelected"] : false property bool isSelected: dataObj ? dataObj["isSelected"] : false
property bool isChecked: dataObj ? dataObj["isChecked"] : false property bool isChecked: dataObj ? dataObj["isChecked"] : false