Skip to content

Commit

Permalink
fix: 解决下载器headerview中的checkbox不居中问题
Browse files Browse the repository at this point in the history
解决下载器headerview中的checkbox不居中问题

Log: 解决下载器headerview中的checkbox不居中问题
  • Loading branch information
daizhengwen committed Apr 10, 2024
1 parent f12509b commit 4fd5f9c
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 42 deletions.
37 changes: 23 additions & 14 deletions src/include/ui/mainFrame/headerView.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,9 @@ class DownloadHeaderView : public QHeaderView
Q_OBJECT
public:
explicit DownloadHeaderView(Qt::Orientation orientation,
QWidget *parent = 0);
QWidget *parent = 0);
~DownloadHeaderView();

protected:
/**
* @brief 更新位置
*/
void updateGeometries();

private:
DCheckBox *m_headerCbx;
signals:
/**
* @brief 表头全选按键状态改变按钮
*/
void Statechanged(bool checked);
public slots:
/**
* @brief 清除表头选中状态
Expand All @@ -79,6 +66,28 @@ public slots:
* @brief 调色板改变,重新设置颜色
*/
void onPalettetypechanged(DGuiApplicationHelper::ColorType type);

/**
* @brief 处理表头的点击事件
* @param logicalIndex
*/
void onSectionClicked(int logicalIndex);

signals:
/**
* @brief 表头全选按键状态改变按钮
*/
void Statechanged(bool checked);


protected:
/**
* @brief 绘制section
*/
void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const override;

private:
bool m_isChecked = false;
};

#endif // HEADERVIEW_H
54 changes: 39 additions & 15 deletions src/src/ui/mainFrame/headerView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,14 @@

#include "headerView.h"
#include <QDebug>
#include <QPainter>
#include <QStyleOptionButton>

DownloadHeaderView::DownloadHeaderView(Qt::Orientation orientation, QWidget *parent)
: QHeaderView(orientation, parent)
{
m_headerCbx = new QCheckBox(this);

// connect(m_headerCbx,&DCheckBox::stateChanged,this,&HeaderView::get_stateChanged);
connect(m_headerCbx,
&DCheckBox::clicked,
this,
&DownloadHeaderView::Statechanged);
connect(this, &DownloadHeaderView::sectionClicked, this, &DownloadHeaderView::onSectionClicked);
connect(DGuiApplicationHelper::instance(),
&DGuiApplicationHelper::paletteTypeChanged,
this,
Expand All @@ -52,8 +49,6 @@ DownloadHeaderView::DownloadHeaderView(Qt::Orientation orientation, QWidget *par
this,
&DownloadHeaderView::onPalettetypechanged);

m_headerCbx->setFixedSize(20, 20);
m_headerCbx->setVisible(true);
//setSectionResizeMode(QHeaderView::ResizeToContents); // 设置resize模式自适应,不能由程序和用户更改

if (DGuiApplicationHelper::instance()->themeType() == 2) {
Expand All @@ -67,19 +62,40 @@ DownloadHeaderView::DownloadHeaderView(Qt::Orientation orientation, QWidget *par

DownloadHeaderView::~DownloadHeaderView()
{
delete (m_headerCbx);
}

void DownloadHeaderView::updateGeometries()
void DownloadHeaderView::onHeaderChecked(bool checked)
{
m_headerCbx->resize(this->height()-2, this->height()-2);
m_headerCbx->move(sectionPosition(0) + 5, 1);
QHeaderView::updateGeometries();
m_isChecked = checked;
updateSection(0);
}

void DownloadHeaderView::onHeaderChecked(bool checked)
void DownloadHeaderView::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
{
m_headerCbx->setChecked(checked);

if (logicalIndex == 0) {
painter->save();
QStyleOptionHeader opt;
initStyleOption(&opt);
QStyle::State state = QStyle::State_None;
state |= QStyle::State_Enabled;
opt.rect = rect;
opt.section = logicalIndex;
opt.state |= state;
style()->drawControl(QStyle::CE_Header, &opt, painter, this);
painter->restore();
painter->save();
QStyleOptionButton checkBoxStyle;
checkBoxStyle.state = m_isChecked ? QStyle::State_On : QStyle::State_Off;
checkBoxStyle.state |= QStyle::State_Enabled;
checkBoxStyle.rect = rect;
checkBoxStyle.rect.setX(rect.x() + 5);
checkBoxStyle.rect.setWidth(20);
QApplication::style()->drawControl(QStyle::CE_CheckBox, &checkBoxStyle, painter);
painter->restore();
return;
}
QHeaderView::paintSection(painter, rect, logicalIndex);
}

void DownloadHeaderView::onPalettetypechanged(DGuiApplicationHelper::ColorType type)
Expand All @@ -94,3 +110,11 @@ void DownloadHeaderView::onPalettetypechanged(DGuiApplicationHelper::ColorType t
}
setPalette(p);
}

void DownloadHeaderView::onSectionClicked(int logicalIndex)
{
if (logicalIndex == 0) {
m_isChecked = !m_isChecked;
Q_EMIT Statechanged(m_isChecked);
}
}
25 changes: 12 additions & 13 deletions src/src/ui/mainFrame/itemDelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ void ItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
{
if (index.row() == m_hoverRow) {
painter->fillRect(option.rect, Dtk::Gui::DGuiApplicationHelper::instance()->applicationPalette().frameBorder());
// QColor(0,0,0,13)QColor(255,255,255,26)
// QColor(0,0,0,13)QColor(255,255,255,26)
}
if (index.row() % 2 != 0) {
painter->fillRect(option.rect, QBrush(QColor(0, 0, 0, 8)));
} else {
if(DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::LightType){
if (DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::LightType) {
painter->fillRect(option.rect, QBrush(QColor(255, 255, 255, 150)));
} else {
painter->fillRect(option.rect, QBrush(QColor(255, 255, 255, 10)));
Expand Down Expand Up @@ -114,8 +114,7 @@ void ItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
checkBoxStyle.rect = option.rect;
checkBoxStyle.rect.setX(option.rect.x() + 5);
checkBoxStyle.rect.setWidth(20);
QSharedPointer<DCheckBox> checkBtn = QSharedPointer<DCheckBox>(new DCheckBox, &QObject::deleteLater);
QApplication::style()->drawControl(QStyle::CE_CheckBox, &checkBoxStyle, painter, checkBtn.data());
QApplication::style()->drawControl(QStyle::CE_CheckBox, &checkBoxStyle, painter);
} else if (column == 1) {
if (isSelected) {
painter->setPen(Dtk::Gui::DGuiApplicationHelper::instance()->applicationPalette().highlight().color());
Expand Down Expand Up @@ -144,13 +143,13 @@ void ItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
pic = icon.pixmap(20, 20);
}

painter->drawPixmap(option.rect.x(), option.rect.y() + (option.rect.height() - 20)/2, pic);
painter->drawPixmap(option.rect.x(), option.rect.y() + (option.rect.height() - 20) / 2, pic);

const QString path = index.data(TableModel::SavePath).toString();
int status = index.data(TableModel::Status).toInt();
if ((!QFileInfo::exists(path)) && (status == Global::DownloadTaskStatus::Complete || status == Global::DownloadTaskStatus::Removed)) { //文件不存在的任务,添加提示
QPixmap errorPic = QIcon(":icons/icon/error.svg").pixmap(12, 12);
painter->drawPixmap(option.rect.x() + 10, option.rect.y() + (option.rect.height() - 20)/2 + 8, errorPic);
painter->drawPixmap(option.rect.x() + 10, option.rect.y() + (option.rect.height() - 20) / 2 + 8, errorPic);
}

const QRect rectText = localRect.marginsRemoved(QMargins(25, 2, 0, 5));
Expand Down Expand Up @@ -179,7 +178,7 @@ void ItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
m_bgImage->load(":/icons/icon/progressbar_bg.png");
m_frontImage->load(":/icons/icon/progressbar_fg.png");
} else {
if(DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::LightType){
if (DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::LightType) {
m_bgImage->load(":/icons/icon/bar-bg.png");
m_frontImage->load(":/icons/icon/bar-front.png");
} else {
Expand Down Expand Up @@ -237,8 +236,8 @@ void ItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
+ "% " + speed;
if (index.data(TableModel::announceList).toInt()) {
str += " " + tr("Resources:") + " " + QString("%1/%2")
.arg(index.data(TableModel::connection).toInt())
.arg(index.data(TableModel::announceList).toInt());
.arg(index.data(TableModel::connection).toInt())
.arg(index.data(TableModel::announceList).toInt());
}
str += " " + tr("Time left ") + index.data(TableModel::Time).toString();

Expand Down Expand Up @@ -335,8 +334,8 @@ bool ItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
QVariant value;
value = model->data(index, TableModel::Ischecked);
Qt::CheckState state = (static_cast<Qt::CheckState>(value.toInt()) == Qt::Checked
? Qt::Unchecked
: Qt::Checked);
? Qt::Unchecked
: Qt::Checked);
ret = model->setData(index, state, TableModel::Ischecked); // 取反后存入模型
} else {
ret = QStyledItemDelegate::editorEvent(event, model, option, index);
Expand All @@ -356,9 +355,9 @@ QWidget *ItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem
QValidator *validator = new QRegExpValidator(regx, pEdit);
pEdit->lineEdit()->setValidator(validator);
pEdit->lineEdit()->setMaxLength(83);
connect(pEdit, &DLineEdit::textChanged, this, [=](QString filename) {
connect(pEdit, &DLineEdit::textChanged, this, [ = ](QString filename) {

Check warning on line 358 in src/src/ui/mainFrame/itemDelegate.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Parameter 'filename' is passed by value. It could be passed as a const reference which is usually faster and recommended in C++.
DLineEdit *pEdit = qobject_cast<DLineEdit *>(sender());
if(pEdit == nullptr) {
if (pEdit == nullptr) {
return ;
}
QString str = index.data(TableModel::FileName).toString();
Expand Down

0 comments on commit 4fd5f9c

Please sign in to comment.