Skip to content

Commit

Permalink
优化table model
Browse files Browse the repository at this point in the history
  • Loading branch information
jared2020 committed Dec 8, 2020
1 parent 8d0393d commit 1a578ba
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 44 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ jobs:
nmake
# tag 打包
- name: package
id: package
if: startsWith(github.event.ref, 'refs/tags/')
env:
VCINSTALLDIR: 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC'
Expand All @@ -99,9 +100,12 @@ jobs:
New-Item -ItemType Directory ${env:archiveName}
# 拷贝exe
Copy-Item bin\release\${env:targetName} ${env:archiveName}\
Copy-Item bin\release\Trans ${env:archiveName}\Trans -Recurse -Exclude *.ilk,*.exp,*.lib,*.pdb
Copy-Item bin\release\Trans ${env:archiveName}\Trans -Recurse
# 拷贝依赖
windeployqt --qmldir . ${env:archiveName}\${env:targetName}
# 删除不必要的库
$excludeList=@("*.qmlc", "*.ilk", "*.exp", "*.lib", "*.pdb")
Remove-Item -Path Qml -Include $excludeList -Recurse -Force
# 打包zip
Compress-Archive -Path ${env:archiveName} ${env:archiveName}'.zip'
# 记录packageName给后续step
Expand Down
16 changes: 16 additions & 0 deletions 3rdparty/TaoCommon/TaoModel/TaoListItemBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@ class TAO_API TaoListItemBase : public QObject
public:
explicit TaoListItemBase(QObject *parent = nullptr);
~TaoListItemBase() override;
TaoListItemBase(const TaoListItemBase &other)
{
setIsChecked(other.isChecked());
setIsSelected(other.isSelected());
setIsVisible(other.isVisible());
setIsAlternate(other.isAlternate());
}

TaoListItemBase &operator=(const TaoListItemBase &other)
{
setIsChecked(other.isChecked());
setIsSelected(other.isSelected());
setIsVisible(other.isVisible());
setIsAlternate(other.isAlternate());
return *this;
}
bool isChecked() const { return mIsChecked; }

bool isSelected() const { return mIsSelected; }
Expand Down
10 changes: 5 additions & 5 deletions 3rdparty/TaoCommon/TaoModel/TaoListModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ void TaoListModel::doRelease()
void TaoListModel::sortByRole()
{
const static auto addRessStr = QStringLiteral("address");
if (mDatas.isEmpty()) {
if (mDatas.size() <= 1) {
return;
}

Expand All @@ -207,9 +207,9 @@ void TaoListModel::sortByRole()
return addressCallback(obj2, obj1);
});
}
beginResetModel();
mDatas = copyObjs;
endResetModel();
emit dataChanged(index(0, 0), index(mDatas.count() - 1, 0));

}
} else {
if (addressCallback) {
Expand All @@ -235,9 +235,9 @@ void TaoListModel::sortByRole()
return mSortCallbacks.value(mSortRole)(obj2, obj1);
});
}
beginResetModel();
mDatas = copyObjs;
endResetModel();
emit dataChanged(index(0, 0), index(mDatas.count() - 1, 0));

}
}
updateAlternate();
Expand Down
6 changes: 5 additions & 1 deletion 3rdparty/TaoCommon/TaoModel/TaoListModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ class TAO_API TaoListModel : public TaoListModelBase<TaoListItemBase *>

void updateCalcInfo() override;

Q_INVOKABLE void notifyScrollTo(int index)
{
emit scrollTo(index);
}
public slots:
void setAllChecked(bool allChecked);

Expand All @@ -86,7 +90,7 @@ public slots:
void setCheckedCount(int checkedCount);

signals:

void scrollTo(int index);
void allCheckedChanged(bool allChecked);
void visibledCountChanged(int visibledCount);

Expand Down
66 changes: 58 additions & 8 deletions 3rdparty/TaoCommon/TaoModel/TaoListModelBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class TaoListModelBase : public QAbstractListModel
//[begin] reset data
void resetData(const QList<T> &datas);
const QList<T> &datas() const { return mDatas; }
// quick reset
void quickResetData(const QList<T> &datas);
//[end] reset data

//[begin] add data
Expand Down Expand Up @@ -50,8 +52,7 @@ TaoListModelBase<T>::TaoListModelBase(QObject *parent) : Super(parent)
{
}
template<class T>
TaoListModelBase<T>::TaoListModelBase(const QList<T> &datas, QObject *parent)
: Super(parent), mDatas(datas)
TaoListModelBase<T>::TaoListModelBase(const QList<T> &datas, QObject *parent) : Super(parent), mDatas(datas)
{
}
template<class T>
Expand Down Expand Up @@ -95,14 +96,64 @@ void TaoListModelBase<T>::resetData(const QList<T> &datas)
qDeleteAll(oldObjs);
updateCalcInfo();
}

template<class T>
void TaoListModelBase<T>::quickResetData(const QList<T> &datas)
{
if (datas.isEmpty()) {
clear();
return;
}
if (mDatas.isEmpty()) {
append(datas);
} else {
auto delta = mDatas.count() - datas.count();
if (delta > 0) {
//删掉多余的
beginRemoveRows({}, datas.count(), mDatas.count() - 1);
for (int i = 0; i < delta; ++i) {
auto pObj = mDatas.last();
mDatas.removeLast();
pObj->deleteLater();
}
endRemoveRows();
//标记旧的
auto copy = mDatas;
//存储新的
mDatas = datas;
//刷新数据
emit dataChanged(index(0, 0), index(datas.count() - 1, 0));
qDeleteAll(copy);
} else if (delta < 0) {
//标记旧的
auto copy = mDatas;
beginInsertRows({}, mDatas.count(), mDatas.count() - delta);
//存储新的
mDatas = datas;
endInsertRows();
//刷新数据
emit dataChanged(index(0, 0), index(datas.count() - 1, 0));
qDeleteAll(copy);
} else {
//标记旧的
auto copy = mDatas;
//存储新的
mDatas = datas;
//刷新数据
emit dataChanged(index(0, 0), index(datas.count() - 1, 0));
qDeleteAll(copy);
}
updateCalcInfo();
}
}

template<class T>
void TaoListModelBase<T>::append(const QList<T> &datas)
{
if (datas.count() <= 0)
{
if (datas.count() <= 0) {
return;
}
beginInsertRows({}, mDatas.count(), mDatas.count() + datas.count() -1);
beginInsertRows({}, mDatas.count(), mDatas.count() + datas.count() - 1);
mDatas.append(datas);
endInsertRows();
updateCalcInfo();
Expand All @@ -121,7 +172,7 @@ void TaoListModelBase<T>::insert(int row, const QList<T> &datas)
if (row < 0 || row > mDatas.size()) {
return;
}
beginInsertRows({}, row, row + datas.count() - 1);
beginInsertRows({}, row, row + datas.count() - 1);
int srow = row;
for (const auto &obj : datas) {
mDatas.insert(srow, obj);
Expand All @@ -133,8 +184,7 @@ void TaoListModelBase<T>::insert(int row, const QList<T> &datas)
template<class T>
void TaoListModelBase<T>::clear()
{
if (mDatas.count() <= 0)
{
if (mDatas.count() <= 0) {
return;
}
beginRemoveRows({}, 0, mDatas.count() - 1);
Expand Down
7 changes: 5 additions & 2 deletions examples/TaoQuickShow/Src/DeviceAddTable/DeviceAddItem.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include "DeviceAddItem.h"

DeviceAddItem::DeviceAddItem(QObject *parent) : TaoListItemBase(parent) { }
#include <QHostAddress>
DeviceAddItem::DeviceAddItem(QObject *parent) : TaoListItemBase(parent)
{
connect(this, &DeviceAddItem::addressChanged, this, [this]() { m_ipv4Address = QHostAddress(address()).toIPv4Address(); });
}

DeviceAddItem::~DeviceAddItem() { }
bool DeviceAddItem::match(const QString &key)
Expand Down
7 changes: 5 additions & 2 deletions examples/TaoQuickShow/Src/DeviceAddTable/DeviceAddItem.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "Common/PropertyHelper.h"
#include "TaoModel/TaoListItemBase.h"
#include"Common/PropertyHelper.h"
#include <QObject>
class DeviceAddItem : public TaoListItemBase
{
Expand All @@ -11,11 +11,14 @@ class DeviceAddItem : public TaoListItemBase
AUTO_PROPERTY(QString, address, "")
AUTO_PROPERTY(QString, modelString, "")
AUTO_PROPERTY(bool, online, false)

public:
explicit DeviceAddItem(QObject *parent = nullptr);
virtual ~DeviceAddItem() override;

bool match(const QString &key) override;
private:
quint32 toIPv4Address() const { return m_ipv4Address; }

private:
quint32 m_ipv4Address = 0;
};
23 changes: 6 additions & 17 deletions examples/TaoQuickShow/Src/DeviceAddTable/DeviceAddModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class DeviceAddModelPrivate
DeviceAddModel::DeviceAddModel(QObject *parent) : TaoListModel(parent), d(new DeviceAddModelPrivate)
{
setHeaderRoles(sHeaderRoles);

}

DeviceAddModel::~DeviceAddModel()
Expand Down Expand Up @@ -70,7 +69,7 @@ void DeviceAddModel::initData()
auto c2 = std::chrono::high_resolution_clock::now();
auto micro = std::chrono::duration_cast<std::chrono::milliseconds>(c2 - c1).count();
qWarning() << "general" << N << "cost" << micro << "ms";
resetData(objs);
quickResetData(objs);
}

void DeviceAddModel::addOne()
Expand Down Expand Up @@ -168,33 +167,24 @@ void DeviceAddModel::sortByName(Qt::SortOrder order)
return (static_cast<DeviceAddItem *>(obj1))->name() > (static_cast<DeviceAddItem *>(obj2))->name();
});
}
beginResetModel();
mDatas = copyObjs;
endResetModel();
emit dataChanged(index(0, 0), index(mDatas.count() - 1, 0));
}

void DeviceAddModel::sortByAddress(Qt::SortOrder order)
{
const auto addressToUint = [](const QString &address)->uint32_t {
auto list = address.split('.');
if (list.size() != 4) {
return 0;
}
return list.at(0).toUInt() * 256^3 + list.at(1).toUInt()* 256^2 + list.at(2).toUInt()* 256 + list.at(3).toUInt();
};
QList<TaoListItemBase *> copyObjs = mDatas;
if (order == Qt::SortOrder::AscendingOrder) {
std::sort(copyObjs.begin(), copyObjs.end(), [=](TaoListItemBase *obj1, TaoListItemBase *obj2) -> bool {
return addressToUint(static_cast<DeviceAddItem *>(obj1)->address()) < addressToUint(static_cast<DeviceAddItem *>(obj2)->address());
return static_cast<DeviceAddItem *>(obj1)->toIPv4Address() < static_cast<DeviceAddItem *>(obj2)->toIPv4Address();
});
} else {
std::sort(copyObjs.begin(), copyObjs.end(), [=](TaoListItemBase *obj1, TaoListItemBase *obj2) -> bool {
return addressToUint(static_cast<DeviceAddItem *>(obj1)->address()) > addressToUint(static_cast<DeviceAddItem *>(obj2)->address());
return static_cast<DeviceAddItem *>(obj1)->toIPv4Address() > static_cast<DeviceAddItem *>(obj2)->toIPv4Address();
});
}
beginResetModel();
mDatas = copyObjs;
endResetModel();
emit dataChanged(index(0, 0), index(mDatas.count() - 1, 0));
}

void DeviceAddModel::sortByModel(Qt::SortOrder order)
Expand All @@ -209,9 +199,8 @@ void DeviceAddModel::sortByModel(Qt::SortOrder order)
return (static_cast<DeviceAddItem *>(obj1))->modelString().toULongLong() > (static_cast<DeviceAddItem *>(obj2))->modelString().toULongLong();
});
}
beginResetModel();
mDatas = copyObjs;
endResetModel();
emit dataChanged(index(0, 0), index(mDatas.count() - 1, 0));
}

DeviceAddItem *DeviceAddModel::genOne(uint32_t value)
Expand Down
19 changes: 11 additions & 8 deletions src/TaoQuick/imports/TaoQuick/Qml/CusTable/CusTableRow.qml
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,22 @@ Rectangle {
}
Repeater {
model: roles
Item {
id: columnItem
Loader {
x: xList[index + 1]
width: widthList[index + 1]
height: parent.height
CusLabel {
id: textLabel
text: dataObj ? (qsTr(String(dataObj[roles[index]])) + CusConfig.transString) : ""
asynchronous: true
sourceComponent: Item {
anchors.fill: parent
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
color: textColor
CusLabel {
text: dataObj ? (qsTr(String(dataObj[roles[index]])) + CusConfig.transString) : ""
anchors.fill: parent
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
color: textColor
}
}
}

}
}

0 comments on commit 1a578ba

Please sign in to comment.