1
0
mirror of https://github.com/KDE/latte-dock.git synced 2025-01-25 14:03:58 +03:00

screensdialog:draw checkbox properly

This commit is contained in:
Michail Vourlakos 2021-06-05 21:47:15 +03:00
parent dd553a1bf4
commit 0125752084
4 changed files with 29 additions and 10 deletions

View File

@ -340,9 +340,18 @@ void drawIcon(QPainter *painter, const QStyleOption &option, const QString &icon
painter->drawPixmap(target, QIcon::fromTheme(icon).pixmap(target.height(), target.height(), mode));
}
QRect remainedFromCheckBox(const QStyleOptionButton &option, Qt::AlignmentFlag alignment)
int primitiveCheckBoxWidth(const QStyleOptionButton &option, const QWidget *widget)
{
int length = option.rect.height() - 2*MARGIN + 1;
QStyleOption copt;
copt.rect = option.rect;
int w = QApplication::style()->sizeFromContents(QStyle::CT_CheckBox, &copt, QSize(0, option.rect.height()), widget).width();
w = w > 0 ? w : option.rect.height() - 2*MARGIN;
return w;
}
QRect remainedFromCheckBox(const QStyleOptionButton &option, Qt::AlignmentFlag alignment, const QWidget *widget)
{
int length = primitiveCheckBoxWidth(option, widget) - MARGIN;
Qt::AlignmentFlag curalign = alignment;
if (qApp->layoutDirection() == Qt::LeftToRight) {
@ -357,8 +366,9 @@ QRect remainedFromCheckBox(const QStyleOptionButton &option, Qt::AlignmentFlag a
return optionRemainedRect;
}
void drawCheckBox(QPainter *painter, const QStyleOptionButton &option, Qt::AlignmentFlag alignment)
void drawCheckBox(QPainter *painter, const QStyleOptionButton &option, Qt::AlignmentFlag alignment, const QWidget *widget)
{
int length = primitiveCheckBoxWidth(option, widget) - MARGIN;
QStyleOptionButton optionbtn = option;
Qt::AlignmentFlag curalign = alignment;
@ -369,12 +379,12 @@ void drawCheckBox(QPainter *painter, const QStyleOptionButton &option, Qt::Align
curalign = alignment == Qt::AlignLeft ? Qt::AlignRight : Qt::AlignLeft;
}
QRect changesrect = (curalign == Qt::AlignLeft) ? QRect(option.rect.x() + MARGIN, option.rect.y(), option.rect.height(), option.rect.height()) :
QRect(option.rect.x() + option.rect.width() - option.rect.height() - MARGIN, option.rect.y(), option.rect.height(), option.rect.height());
QRect changesrect = (curalign == Qt::AlignLeft) ? QRect(option.rect.x() + MARGIN, option.rect.y(), length - MARGIN, option.rect.height()) :
QRect(option.rect.x() + option.rect.width() - length, option.rect.y(), length - MARGIN, option.rect.height());
optionbtn.rect = changesrect;
QApplication::style()->drawControl(QStyle::CE_CheckBox, &optionbtn, painter);
QApplication::style()->drawControl(QStyle::CE_CheckBox, &optionbtn, painter, widget);
}
QRect remainedFromChangesIndicator(const QStyleOptionViewItem &option)

View File

@ -50,8 +50,9 @@ void drawLayoutIcon(QPainter *painter, const QStyleOption &option, const bool &i
QRect remainedFromChangesIndicator(const QStyleOptionViewItem &option);
void drawChangesIndicator(QPainter *painter, const QStyleOptionViewItem &option);
QRect remainedFromCheckBox(const QStyleOptionButton &option, Qt::AlignmentFlag alignment = Qt::AlignLeft);
void drawCheckBox(QPainter *painter, const QStyleOptionButton &option, Qt::AlignmentFlag alignment = Qt::AlignLeft);
int primitiveCheckBoxWidth(const QStyleOptionButton &option, const QWidget *widget = nullptr);
QRect remainedFromCheckBox(const QStyleOptionButton &option, Qt::AlignmentFlag alignment = Qt::AlignLeft, const QWidget *widget = nullptr);
void drawCheckBox(QPainter *painter, const QStyleOptionButton &option, Qt::AlignmentFlag alignment = Qt::AlignLeft, const QWidget *widget = nullptr);
//! screen icon
QRect remainedFromScreenDrawing(const QStyleOption &option, const int &maxIconSize = -1);

View File

@ -61,6 +61,7 @@ void CheckBox::paint(QPainter *painter, const QStyleOptionViewItem &option, cons
QStyleOptionViewItem adjustedOption = option;
//! Remove the focus dotted lines
adjustedOption.state = (adjustedOption.state & ~QStyle::State_HasFocus);
adjustedOption.displayAlignment = Qt::AlignLeft;
bool originalChecked{false};
@ -73,12 +74,16 @@ void CheckBox::paint(QPainter *painter, const QStyleOptionViewItem &option, cons
bool isSelected = index.data(Model::Screens::ISSELECTEDROLE).toBool();
bool isRemovable = screen.isRemovable;
if (!isRemovable) {
//! disable selected and hover
adjustedOption.state = (adjustedOption.state & ~QStyle::State_MouseOver);
}
//! background
Latte::drawBackground(painter, adjustedOption);
//! checkbox
QStyleOptionButton checkopt;
//checkopt.initFrom(option.widget->parentWidget());
checkopt.state |= QStyle::State_Enabled;
checkopt.state |= isSelected ? QStyle::State_On : QStyle::State_Off;
checkopt.text = "";
@ -86,7 +91,7 @@ void CheckBox::paint(QPainter *painter, const QStyleOptionViewItem &option, cons
QRect remainedrect = Latte::remainedFromCheckBox(checkopt);
if (screen.isRemovable) {
Latte::drawCheckBox(painter, checkopt);
Latte::drawCheckBox(painter, checkopt, Qt::AlignLeft, option.widget);
}
adjustedOption.rect = remainedrect;

View File

@ -188,6 +188,9 @@ Qt::ItemFlags Screens::flags(const QModelIndex &index) const
if (c_screens[row].isRemovable) {
flags |= Qt::ItemIsUserCheckable;
} else {
flags &= ~Qt::ItemIsSelectable;
flags &= ~Qt::ItemIsEditable;
}
return flags;