添加模型分割

This commit is contained in:
2026-04-08 14:37:01 +08:00
parent 088dd91e27
commit a79c31a056
17 changed files with 1327 additions and 183 deletions

View File

@@ -0,0 +1,52 @@
#include "widgets/LongPressSwitchToolButton.h"
#include <QApplication>
#include <QMouseEvent>
#include <QTimer>
#include <algorithm>
LongPressSwitchToolButton::LongPressSwitchToolButton(QWidget* parent)
: QToolButton(parent) {
m_timer = new QTimer(this);
m_timer->setSingleShot(true);
m_timer->setInterval(m_intervalMs);
connect(m_timer, &QTimer::timeout, this, [this]() {
if (!(QApplication::mouseButtons() & Qt::LeftButton)) {
return;
}
m_longPressEmittedForThisPress = true;
emit longPressTriggered();
});
}
void LongPressSwitchToolButton::setLongPressInterval(int ms) {
m_intervalMs = std::max(50, ms);
if (m_timer) {
m_timer->setInterval(m_intervalMs);
}
}
void LongPressSwitchToolButton::mousePressEvent(QMouseEvent* event) {
if (event->button() == Qt::LeftButton) {
m_longPressEmittedForThisPress = false;
m_timer->start();
}
QToolButton::mousePressEvent(event);
}
void LongPressSwitchToolButton::mouseReleaseEvent(QMouseEvent* event) {
const bool left = (event->button() == Qt::LeftButton);
if (left && m_timer->isActive()) {
m_timer->stop();
}
const bool hadLongPress = left && m_longPressEmittedForThisPress;
QToolButton::mouseReleaseEvent(event);
if (hadLongPress) {
m_longPressEmittedForThisPress = false;
if (isDown()) {
setDown(false);
}
update();
}
}

View File

@@ -0,0 +1,32 @@
#pragma once
#include <QToolButton>
class QMouseEvent;
class QTimer;
/**
* 可复用的工具栏按钮:短按行为与普通 QToolButton 一致;
* 左键按住超过设定时间(默认 450ms且仍未松开时发出 longPressTriggered()
* 用于在「同一工具钮」上切换子模式(类似 Photoshop 长按切换同类工具)。
*/
class LongPressSwitchToolButton final : public QToolButton {
Q_OBJECT
public:
explicit LongPressSwitchToolButton(QWidget* parent = nullptr);
void setLongPressInterval(int ms);
int longPressInterval() const { return m_intervalMs; }
signals:
void longPressTriggered();
protected:
void mousePressEvent(QMouseEvent* event) override;
void mouseReleaseEvent(QMouseEvent* event) override;
private:
QTimer* m_timer = nullptr;
int m_intervalMs = 450;
bool m_longPressEmittedForThisPress = false;
};

View File

@@ -0,0 +1,55 @@
#include "widgets/ToolOptionPopup.h"
#include <QBoxLayout>
#include <QPushButton>
#include <QToolButton>
ToolOptionPopup::ToolOptionPopup(QWidget* parent)
: QFrame(parent) {
setWindowFlags(Qt::Popup | Qt::FramelessWindowHint);
setFrameShape(QFrame::StyledPanel);
setObjectName(QStringLiteral("ToolOptionPopup"));
setStyleSheet(QStringLiteral(
"#ToolOptionPopup { background: palette(base); border: 1px solid palette(mid); border-radius: 10px; }"
"#ToolOptionPopup QPushButton { border: 1px solid transparent; padding: 8px 10px; text-align: left; }"
"#ToolOptionPopup QPushButton:hover { background: palette(midlight); }"));
}
void ToolOptionPopup::setOptions(const QVector<Option>& opts) {
m_options = opts;
rebuildUi();
}
void ToolOptionPopup::rebuildUi() {
QLayout* old = layout();
if (old) {
delete old;
}
auto* root = new QVBoxLayout(this);
root->setContentsMargins(8, 8, 8, 8);
root->setSpacing(6);
for (const auto& opt : m_options) {
auto* b = new QPushButton(opt.text, this);
b->setFlat(true);
connect(b, &QPushButton::clicked, this, [this, id = opt.id]() {
emit optionChosen(id);
close();
});
root->addWidget(b);
}
}
void ToolOptionPopup::popupNearToolButton(QToolButton* btn) {
if (!btn) {
return;
}
if (!layout()) {
rebuildUi();
}
adjustSize();
const QPoint g = btn->mapToGlobal(QPoint(btn->width(), 0));
move(g.x() + 6, g.y());
show();
raise();
}

View File

@@ -0,0 +1,32 @@
#pragma once
#include <QFrame>
#include <QString>
#include <QVector>
class QToolButton;
/// 可复用的“按钮弹出选项面板”Qt::Popup用于同一工具按钮选择子模式。
class ToolOptionPopup final : public QFrame {
Q_OBJECT
public:
struct Option {
int id = 0;
QString text;
};
explicit ToolOptionPopup(QWidget* parent = nullptr);
void setOptions(const QVector<Option>& opts);
void popupNearToolButton(QToolButton* btn);
signals:
void optionChosen(int id);
private:
void rebuildUi();
private:
QVector<Option> m_options;
};