Skip to content

Commit

Permalink
chore: clean code
Browse files Browse the repository at this point in the history
清理代码
  • Loading branch information
zsien committed Sep 2, 2024
1 parent f8c5de6 commit acb9ae2
Show file tree
Hide file tree
Showing 8 changed files with 0 additions and 251 deletions.
2 changes: 0 additions & 2 deletions panels/dock/tray/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ qt_add_qml_module(dock-tray
SOURCES
traysortordermodel.cpp
traysortordermodel.h
trayitempositionregister.cpp
trayitempositionregister.h
trayitempositionmanager.cpp
trayitempositionmanager.h
ksortfilterproxymodel.cpp
Expand Down
5 changes: 0 additions & 5 deletions panels/dock/tray/package/TrayContainer.qml
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ Item {
readonly property int itemPadding: DDT.TrayItemPositionManager.itemPadding

property int trayHeight: 50
property size containerSize: DDT.TrayItemPositionManager.visualSize
property bool isDragging: DDT.TraySortOrderModel.actionsAlwaysVisible
property bool animationEnable: false
// visiualIndex default value is -1
property int dropHoverIndex: -1
Expand Down Expand Up @@ -183,9 +181,6 @@ Item {
DDT.TrayItemPositionManager.orientation = Qt.binding(function() {
return root.isHorizontal ? Qt.Horizontal : Qt.Vertical
});
DDT.TrayItemPositionManager.visualItemCount = Qt.binding(function() {
return root.model.rowCount
});
DDT.TrayItemPositionManager.dockHeight = Qt.binding(function() {
return root.trayHeight
});
Expand Down
5 changes: 0 additions & 5 deletions panels/dock/tray/package/TrayItemPositioner.qml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ Control {

property bool isDragging: DDT.TraySortOrderModel.actionsAlwaysVisible

DDT.TrayItemPositionRegister.visualIndex: DelegateModel.visualIndex
DDT.TrayItemPositionRegister.visualSize: (model.sectionType !== "stashed") ? Qt.size(width, height) : Qt.size(0, 0)
DDT.TrayItemPositionRegister.surfaceId: model.surfaceId
DDT.TrayItemPositionRegister.sectionType: model.sectionType

width: visualSize.width !== 0 ? visualSize.width : DDT.TrayItemPositionManager.itemVisualSize.width
height: visualSize.height !== 0 ? visualSize.height : DDT.TrayItemPositionManager.itemVisualSize.height

Expand Down
3 changes: 0 additions & 3 deletions panels/dock/tray/package/tray.qml
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,6 @@ AppletItem {
trayHeight: isHorizontal ? tray.implicitHeight : tray.implicitWidth
surfaceAcceptor: isTrayPluginPopup
color: "transparent"
Component.onCompleted: {
DDT.TrayItemPositionManager.layoutHealthCheck(1500)
}
}

function isTrayPluginPopup(surfaceId) {
Expand Down
111 changes: 0 additions & 111 deletions panels/dock/tray/trayitempositionmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,80 +15,6 @@ static const int itemPadding = 4;
static const int itemSpacing = 2;
static const QSize itemVisualSize = QSize(itemSize + itemPadding * 2, itemSize + itemPadding * 2);

void TrayItemPositionManager::registerVisualItemSize(int index, const QSize &size)
{
while (m_registeredItemsSize.count() < (index + 1)) {
m_registeredItemsSize.append(itemVisualSize);
}
QSize oldSize = m_registeredItemsSize[index];
m_registeredItemsSize[index] = size;

// The registered itemsize may change, and the layout needs to be updated when it does.
if (oldSize != size) {
emit visualItemSizeChanged();
}
}

QSize TrayItemPositionManager::visualItemSize(int index) const
{
if (m_registeredItemsSize.count() <= index) return itemVisualSize;
return m_registeredItemsSize.at(index);
}

QSize TrayItemPositionManager::visualSize(int index, bool includeLastSpacing) const
{
if (m_orientation == Qt::Horizontal) {
int width = 0;
for (int i = 0; i <= index; i++) {
width += (visualItemSize(i).width() + itemSpacing);
}
return QSize((!includeLastSpacing && index > 0) ? (width - itemSpacing) : width, m_dockHeight);
} else {
int height = 0;
for (int i = 0; i <= index; i++) {
height += (visualItemSize(i).height() + itemSpacing);
}
return QSize(m_dockHeight, (!includeLastSpacing && index > 0) ? (height - itemSpacing) : height);
}
}

DropIndex TrayItemPositionManager::itemIndexByPoint(const QPoint point) const
{
if (m_orientation == Qt::Horizontal) {
int pos = point.x();
int width = 0;
for (int i = 0; i < m_visualItemCount; i++) {
int visualWidth = visualItemSize(i).width();
if (pos < (width + visualWidth + itemSpacing)) {
pos -= width;
return DropIndex {
.index = i,
.isOnItem = pos <= visualWidth,
.isBefore = pos < (visualWidth / 2)
};
}
width += (visualWidth + itemSpacing);
}
return DropIndex { .index = m_visualItemCount - 1 };
} else {
int pos = point.y();
int height = 0;
for (int i = 0; i <= m_visualItemCount; i++) {
int visualHeight = visualItemSize(i).height();
if (pos < (height + visualHeight + itemSpacing)) {
pos -= height;
return DropIndex {
.index = i,
.isOnItem = pos <= visualHeight,
.isBefore = pos < (visualHeight / 2)
};
}
height += (visualHeight + itemSpacing);
}
return DropIndex { .index = m_visualItemCount - 1 };
}
}

Qt::Orientation TrayItemPositionManager::orientation() const
{
return m_orientation;
Expand All @@ -99,49 +25,12 @@ int TrayItemPositionManager::dockHeight() const
return m_dockHeight;
}

// This should only be used to check layout issue or workaround layout issues.
// Do NOT rely on this to correct layout issue in a long run!
void TrayItemPositionManager::layoutHealthCheck(int delayMs)
{
QTimer::singleShot(delayMs, [this](){
if (m_dockHeight == 0) {
qWarning() << "dock height is not valid, aborting layout health check...";
return;
}
QSize result(visualSize(m_visualItemCount - 1, false));
if (m_visualSize != result) {
qWarning() << "layout size not matched, will trigger a force re-layout...";
emit orientationChanged(m_orientation);
} else {
qDebug() << "no problem founded while performing layout health check!";
}
});
qDebug() << "layout health check scheduled!";
}

TrayItemPositionManager::TrayItemPositionManager(QObject *parent)

Check warning on line 28 in panels/dock/tray/trayitempositionmanager.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Member variable 'TrayItemPositionManager::m_dockHeight' is not initialized in the constructor.
: QObject(parent)
{
m_itemSpacing = itemSpacing;
m_itemPadding = itemPadding;
m_itemVisualSize = itemVisualSize;

Check warning on line 33 in panels/dock/tray/trayitempositionmanager.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

When an object of a class is created, the constructors of all member variables are called consecutively in the order the variables are declared, even if you don't explicitly write them to the initialization list. You could avoid assigning 'm_itemVisualSize' a value by passing the value to the constructor in the initialization list.

connect(this, &TrayItemPositionManager::visualItemCountChanged,
this, &TrayItemPositionManager::updateVisualSize);
connect(this, &TrayItemPositionManager::dockHeightChanged,
this, &TrayItemPositionManager::updateVisualSize);
connect(this, &TrayItemPositionManager::orientationChanged,
this, &TrayItemPositionManager::updateVisualSize);
connect(this, &TrayItemPositionManager::visualItemSizeChanged,
this, &TrayItemPositionManager::updateVisualSize);
}

void TrayItemPositionManager::updateVisualSize()
{
if (m_dockHeight == 0) return;
QSize result(visualSize(m_visualItemCount - 1, false));
qDebug() << "updateVisualSize()" << m_dockHeight << result;
setProperty("visualSize", result);
}

}
14 changes: 0 additions & 14 deletions panels/dock/tray/trayitempositionmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ class TrayItemPositionManager : public QObject
// dock properties, to notify tray items its property has been changed
Q_PROPERTY(Qt::Orientation orientation MEMBER m_orientation NOTIFY orientationChanged)
Q_PROPERTY(int dockHeight MEMBER m_dockHeight NOTIFY dockHeightChanged)
// position manager properties, when tray items reports visualPositionChanged, re-calculate this
Q_PROPERTY(QSize visualSize MEMBER m_visualSize NOTIFY visualSizeChanged)
// position manager properties, use to know how to calculate the actual width of visualSize
Q_PROPERTY(int visualItemCount MEMBER m_visualItemCount NOTIFY visualItemCountChanged)
Q_PROPERTY(QSize itemVisualSize MEMBER m_itemVisualSize CONSTANT FINAL)
Q_PROPERTY(int itemSpacing MEMBER m_itemSpacing CONSTANT FINAL)
Q_PROPERTY(int itemPadding MEMBER m_itemPadding CONSTANT FINAL)
Expand All @@ -51,13 +47,8 @@ class TrayItemPositionManager : public QObject
return &instance();
}

void registerVisualItemSize(int index, const QSize & size);
QSize visualItemSize(int index) const;
QSize visualSize(int index, bool includeLastSpacing = true) const;
Q_INVOKABLE DropIndex itemIndexByPoint(const QPoint point) const;
Qt::Orientation orientation() const;
int dockHeight() const;
Q_INVOKABLE void layoutHealthCheck(int delayMs = 200);

signals:
void orientationChanged(Qt::Orientation);
Expand All @@ -69,13 +60,8 @@ class TrayItemPositionManager : public QObject
private:
explicit TrayItemPositionManager(QObject *parent = nullptr);

void updateVisualSize();

Qt::Orientation m_orientation;
QSize m_visualSize;
int m_dockHeight;
int m_visualItemCount;
QList<QSize> m_registeredItemsSize;
QSize m_itemVisualSize;
int m_itemSpacing;
int m_itemPadding;
Expand Down
54 changes: 0 additions & 54 deletions panels/dock/tray/trayitempositionregister.cpp

This file was deleted.

57 changes: 0 additions & 57 deletions panels/dock/tray/trayitempositionregister.h

This file was deleted.

0 comments on commit acb9ae2

Please sign in to comment.