Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions src/plugins/binarytools/mainframe/binarytoolsconfigview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
#include "binarytoolsconfigview.h"
#include "environmentview.h"
#include "advancedsettingsdialog.h"
#include "models/binarytoolsmodel.h"

Check warning on line 8 in src/plugins/binarytools/mainframe/binarytoolsconfigview.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "models/binarytoolsmodel.h" not found.

Check warning on line 8 in src/plugins/binarytools/mainframe/binarytoolsconfigview.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: "models/binarytoolsmodel.h" not found.
#include "models/toolitemdelegate.h"

Check warning on line 9 in src/plugins/binarytools/mainframe/binarytoolsconfigview.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "models/toolitemdelegate.h" not found.

Check warning on line 9 in src/plugins/binarytools/mainframe/binarytoolsconfigview.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: "models/toolitemdelegate.h" not found.
#include "configure/binarytoolsmanager.h"

Check warning on line 10 in src/plugins/binarytools/mainframe/binarytoolsconfigview.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "configure/binarytoolsmanager.h" not found.

Check warning on line 10 in src/plugins/binarytools/mainframe/binarytoolsconfigview.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: "configure/binarytoolsmanager.h" not found.
#include "widgets/combinationcomboxbox.h"

Check warning on line 11 in src/plugins/binarytools/mainframe/binarytoolsconfigview.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "widgets/combinationcomboxbox.h" not found.

Check warning on line 11 in src/plugins/binarytools/mainframe/binarytoolsconfigview.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: "widgets/combinationcomboxbox.h" not found.
#include "widgets/iconcombobox.h"

Check warning on line 12 in src/plugins/binarytools/mainframe/binarytoolsconfigview.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "widgets/iconcombobox.h" not found.

Check warning on line 12 in src/plugins/binarytools/mainframe/binarytoolsconfigview.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: "widgets/iconcombobox.h" not found.
#include "constants.h"

Check warning on line 13 in src/plugins/binarytools/mainframe/binarytoolsconfigview.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "constants.h" not found.

Check warning on line 13 in src/plugins/binarytools/mainframe/binarytoolsconfigview.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: "constants.h" not found.

#include "base/baseitemdelegate.h"
#include "common/widget/variablechooser.h"

Check warning on line 15 in src/plugins/binarytools/mainframe/binarytoolsconfigview.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "common/widget/variablechooser.h" not found.

Check warning on line 15 in src/plugins/binarytools/mainframe/binarytoolsconfigview.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: "common/widget/variablechooser.h" not found.

#include <DComboBox>

Check warning on line 17 in src/plugins/binarytools/mainframe/binarytoolsconfigview.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <DComboBox> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 17 in src/plugins/binarytools/mainframe/binarytoolsconfigview.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <DComboBox> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <DCheckBox>
#include <DLineEdit>
#include <DToolButton>
Expand Down Expand Up @@ -143,20 +143,25 @@
DFrame *widget = new DFrame(q);
widget->setFixedWidth(200);
QVBoxLayout *mainLayout = new QVBoxLayout(widget);
mainLayout->setContentsMargins(10, 10, 10, 6);
mainLayout->setContentsMargins(0, 6, 0, 0);
mainLayout->setSpacing(0);

QVBoxLayout *viewLayout = new QVBoxLayout;
viewLayout->setContentsMargins(8, 0, 8, 0);
toolTree = new DTreeView(q);
toolTree->setIndentation(0);
toolTree->setFrameShape(QFrame::NoFrame);
toolTree->setDragEnabled(false);
toolTree->header()->setVisible(false);
toolTree->setModel(&treeModel);
toolTree->setItemDelegate(new BaseItemDelegate(toolTree));
toolTree->setItemDelegate(new ToolItemDelegate(toolTree));
toolTree->setIconSize({ 16, 16 });
toolTree->setEditTriggers(QAbstractItemView::DoubleClicked | QAbstractItemView::EditKeyPressed);
viewLayout->addWidget(toolTree);

addBtn = new DToolButton(q);
addBtn->setIconSize({ 16, 16 });
addBtn->setIconSize({ 12, 12 });
addBtn->setFixedHeight(24);
addBtn->setIcon(DStyle::standardIcon(q->style(), DStyle::SP_IncreaseElement));
addBtn->setPopupMode(QToolButton::InstantPopup);
QMenu *addMenu = new QMenu(q);
Expand All @@ -168,16 +173,18 @@
addBtn->setMenu(addMenu);

delBtn = new DToolButton(q);
delBtn->setIconSize({ 16, 16 });
delBtn->setIconSize({ 12, 12 });
delBtn->setFixedSize(24, 24);
delBtn->setIcon(DStyle::standardIcon(q->style(), DStyle::SP_DecreaseElement));

QHBoxLayout *btnLayout = new QHBoxLayout();
btnLayout->setContentsMargins(0, 0, 0, 0);
btnLayout->setContentsMargins(8, 6, 8, 6);
btnLayout->addWidget(addBtn);
btnLayout->addWidget(delBtn);
btnLayout->addStretch(1);

mainLayout->addWidget(toolTree);
mainLayout->addLayout(viewLayout);
mainLayout->addWidget(new DHorizontalLine(q));
mainLayout->addLayout(btnLayout);
return widget;
}
Expand Down Expand Up @@ -244,6 +251,7 @@
DLabel *cmdIconLabel = new DLabel(BinaryToolsConfigView::tr("Tool icon:"), q);
iconCB = new IconComboBox(q);
iconCB->setFixedWidth(240);
iconCB->setFocusPolicy(Qt::NoFocus);
mainLayout->addWidget(cmdIconLabel, IconRow, 0);
mainLayout->addWidget(iconCB, IconRow, 1);

Expand Down Expand Up @@ -434,7 +442,7 @@
const auto tool = treeModel.toolForIndex(currentIndex);
if (!tool)
return;

dialog.setAdvancedSettings(tool->advSettings);
int code = dialog.exec();
if (code == 1) {
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/binarytools/mainframe/binarytoolsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ BinaryToolsDialog::BinaryToolsDialog(QDialog *parent)
DWidget *mainFrame = new DWidget(this);
addContent(mainFrame);
QVBoxLayout *vLayout = new QVBoxLayout(mainFrame);
vLayout->setSpacing(0);
d->configView = new BinaryToolsConfigView(mainFrame);

DLabel *configLabel = new DLabel(tr("Running Configuration:"), this);
vLayout->addWidget(configLabel);
vLayout->addSpacing(15);
vLayout->addWidget(d->configView);

QHBoxLayout *buttonLayout = new QHBoxLayout(mainFrame);
Expand All @@ -52,6 +54,7 @@ BinaryToolsDialog::BinaryToolsDialog(QDialog *parent)
buttonLayout->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
buttonLayout->setContentsMargins(0, 0, 0, 0);

vLayout->addSpacing(20);
vLayout->addLayout(buttonLayout);

connect(okButton, &DPushButton::clicked, this, &BinaryToolsDialog::saveClicked);
Expand Down
209 changes: 209 additions & 0 deletions src/plugins/binarytools/models/toolitemdelegate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
// SPDX-FileCopyrightText: 2025 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include "toolitemdelegate.h"

#include <DGuiApplicationHelper>

Check warning on line 7 in src/plugins/binarytools/models/toolitemdelegate.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <DGuiApplicationHelper> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 7 in src/plugins/binarytools/models/toolitemdelegate.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <DGuiApplicationHelper> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <DStyle>

Check warning on line 8 in src/plugins/binarytools/models/toolitemdelegate.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <DStyle> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 8 in src/plugins/binarytools/models/toolitemdelegate.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <DStyle> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <DLineEdit>
#ifdef DTKWIDGET_CLASS_DPaletteHelper
# include <DPaletteHelper>
#endif

#include <QPainter>
#include <QTreeView>
#include <QMouseEvent>

DWIDGET_USE_NAMESPACE

ToolItemDelegate::ToolItemDelegate(QAbstractItemView *parent)
: DStyledItemDelegate(parent)
{
}

void ToolItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if (!index.isValid())
return DStyledItemDelegate::paint(painter, option, index);

QStyleOptionViewItem opt = option;
DStyledItemDelegate::initStyleOption(&opt, index);

painter->setRenderHint(QPainter::Antialiasing);
drawBackground(painter, opt);
drawToolItem(painter, opt, index);
}

QSize ToolItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
return { view()->width(), 24 };
}

QWidget *ToolItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const
{
auto edit = new DLineEdit(parent);
edit->setAutoFillBackground(true);
edit->setBackgroundRole(QPalette::Base);
return edit;
}

void ToolItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
auto edit = qobject_cast<DLineEdit *>(editor);
edit->setText(index.data(Qt::DisplayRole).toString());
}

void ToolItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
auto edit = qobject_cast<DLineEdit *>(editor);
const auto &text = edit->text();
if (!text.isEmpty())
model->setData(index, text);
}

void ToolItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if (view()) {
QRect rect = view()->visualRect(index);
editor->setGeometry(rect);
} else {
DStyledItemDelegate::updateEditorGeometry(editor, option, index);
}
}

bool ToolItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
const QStyleOptionViewItem &option,
const QModelIndex &index)
{
if (event->type() == QEvent::MouseButtonRelease || event->type() == QEvent::MouseButtonDblClick) {
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
const QRect arRect = arrowRect(option.rect);
if (arRect.contains(mouseEvent->pos())) {
if (view()->isExpanded(index)) {
view()->collapse(index);
} else {
view()->expand(index);
}
return true;
}
}

return false;
}

QTreeView *ToolItemDelegate::view() const
{
return qobject_cast<QTreeView *>(parent());
}

void ToolItemDelegate::drawBackground(QPainter *painter, const QStyleOptionViewItem &option) const
{
painter->save();
if (option.state.testFlag(QStyle::State_Selected)) {
QColor bgColor = option.palette.color(QPalette::Normal, QPalette::Highlight);
painter->setBrush(bgColor);
painter->setPen(Qt::NoPen);
painter->drawRoundedRect(option.rect, 6, 6);
} else if (option.state.testFlag(QStyle::State_MouseOver)) {
#ifdef DTKWIDGET_CLASS_DPaletteHelper
DPalette palette = DPaletteHelper::instance()->palette(option.widget);
#else
DPalette palette = DGuiApplicationHelper::instance()->applicationPalette();
#endif
painter->setBrush(palette.brush(DPalette::ItemBackground));
painter->setPen(Qt::NoPen);
painter->drawRoundedRect(option.rect, 6, 6);
}
painter->restore();
}

void ToolItemDelegate::drawToolItem(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QRect extraRect;
if (!index.parent().isValid())
extraRect = drawExpandArrow(painter, option, index);
else
extraRect = drawItemIcon(painter, option, index);

QRect itemRect = option.rect;
itemRect.setLeft(extraRect.right() + 6);

QString toolName = index.data(Qt::DisplayRole).toString();
if (option.state & QStyle::State_Selected) {
painter->setPen(option.palette.color(QPalette::Normal, QPalette::HighlightedText));
} else {
painter->setPen(option.palette.color(QPalette::Normal, QPalette::Text));
}

QString displayText = option.fontMetrics.elidedText(toolName, Qt::ElideRight, itemRect.width());
painter->drawText(itemRect, displayText);
}

QRect ToolItemDelegate::drawItemIcon(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QRect iconRect = option.rect;
iconRect.setSize(view()->iconSize());
iconRect.moveLeft(iconRect.left() + 24);
iconRect.moveTop(option.rect.top() + (option.rect.bottom() - iconRect.bottom()) / 2);

QIcon::Mode iconMode = QIcon::Normal;
if (!(option.state.testFlag(QStyle::State_Enabled)))
iconMode = QIcon::Disabled;
if (option.state.testFlag(QStyle::State_Selected))
iconMode = QIcon::Selected;

auto px = option.icon.pixmap(view()->iconSize(), iconMode);
px.setDevicePixelRatio(qApp->devicePixelRatio());

qreal x = iconRect.x();
qreal y = iconRect.y();
qreal w = px.width() / px.devicePixelRatio();
qreal h = px.height() / px.devicePixelRatio();
y += (iconRect.size().height() - h) / 2.0;
x += (iconRect.size().width() - w) / 2.0;

painter->drawPixmap(qRound(x), qRound(y), px);
return iconRect;
}

QRect ToolItemDelegate::drawExpandArrow(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QStyleOptionViewItem opt = option;
opt.rect = arrowRect(opt.rect).marginsRemoved(QMargins(5, 5, 5, 5));

painter->save();
bool isSelected = (option.state & QStyle::State_Selected) && option.showDecorationSelected;
if (isSelected) {
painter->setPen(option.palette.color(QPalette::Active, QPalette::HighlightedText));
} else {
painter->setPen(option.palette.color(QPalette::Active, QPalette::Text));
}

auto style = option.widget->style();
if (view()->isExpanded(index)) {
style->drawPrimitive(QStyle::PE_IndicatorArrowDown, &opt, painter, nullptr);
} else {
style->drawPrimitive(QStyle::PE_IndicatorArrowRight, &opt, painter, nullptr);
}

painter->restore();
return opt.rect;
}

QRect ToolItemDelegate::arrowRect(const QRect &itemRect) const
{
QRect arrowRect = itemRect;

arrowRect.setSize(QSize(20, 20));
arrowRect.moveLeft(arrowRect.left() + 4);
arrowRect.moveTop(itemRect.top() + (itemRect.bottom() - arrowRect.bottom()) / 2);

return arrowRect;
}
46 changes: 46 additions & 0 deletions src/plugins/binarytools/models/toolitemdelegate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// SPDX-FileCopyrightText: 2025 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef TOOLITEMDELEGATE_H
#define TOOLITEMDELEGATE_H

#include <DStyledItemDelegate>

class QTreeView;
class ToolItemDelegate : public DTK_WIDGET_NAMESPACE::DStyledItemDelegate
{
Q_OBJECT
public:
explicit ToolItemDelegate(QAbstractItemView *parent = nullptr);

void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override;
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
void setEditorData(QWidget *editor, const QModelIndex &index) const override;
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override;

protected:
bool editorEvent(QEvent *event,
QAbstractItemModel *model,
const QStyleOptionViewItem &option,
const QModelIndex &index) override;

private:
QTreeView *view() const;
void drawBackground(QPainter *painter, const QStyleOptionViewItem &option) const;
void drawToolItem(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const;
QRect drawItemIcon(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const;
QRect drawExpandArrow(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const;

QRect arrowRect(const QRect &itemRect) const;
};

#endif // TOOLITEMDELEGATE_H
Loading
Loading