dialogs:provide same codepath to draw layout icons

work/spdx
Michail Vourlakos 4 years ago
parent 076deece2f
commit b846933e8e

@ -176,16 +176,48 @@ void drawBackground(QPainter *painter, const QStyle *style, const QStyleOptionMe
style->drawControl(QStyle::CE_MenuItem, &backOption, painter);
}
void drawLayoutIcon(QPainter *painter, const QStyleOption &option, const QRect &target, const bool &isBackgroundFile, const QString &iconName)
{
QRect remainedFromLayoutIcon(const QStyleOption &option, Qt::AlignmentFlag alignment)
{
if (alignment == Qt::AlignHCenter) {
return option.rect;
}
return remainedFromIcon(option, alignment);
}
void drawLayoutIcon(QPainter *painter, const QStyleOption &option, const bool &isBackgroundFile, const QString &iconName, Qt::AlignmentFlag alignment)
{
bool active = Latte::isActive(option);
bool selected = Latte::isSelected(option);
bool focused = Latte::isFocused(option);
int iconsize = option.rect.height() - 2*ICONMARGIN;
int total = iconsize + 2*ICONMARGIN + 2*MARGIN;
Qt::AlignmentFlag curalign = alignment;
if (qApp->layoutDirection() == Qt::LeftToRight || alignment == Qt::AlignHCenter) {
curalign = alignment;
} else {
curalign = alignment == Qt::AlignLeft ? Qt::AlignRight : Qt::AlignLeft;
}
QRect target;
if (curalign == Qt::AlignLeft) {
target = QRect(option.rect.x() + MARGIN + ICONMARGIN, option.rect.y() + ICONMARGIN, iconsize, iconsize);
} else if (curalign == Qt::AlignRight) {
target = QRect(option.rect.x() + option.rect.width() - total + ICONMARGIN + MARGIN, option.rect.y() + ICONMARGIN, iconsize, iconsize);
} else {
//! centered
target = QRect(option.rect.x() + ((option.rect.width() - total)/2) + ICONMARGIN + MARGIN, option.rect.y() + ICONMARGIN, iconsize, iconsize);
}
painter->save();
painter->setRenderHint(QPainter::Antialiasing, true);
if (isBackgroundFile) {
int backImageMargin = qMin(option.rect.height()/4, ICONMARGIN+2);
int backImageMargin = qMin(target.height()/4, ICONMARGIN+1);
QRect backTarget(target.x() + backImageMargin, target.y() + backImageMargin, target.width() - 2*backImageMargin, target.height() - 2*backImageMargin);
QPixmap backImage(iconName);
@ -206,6 +238,8 @@ void drawLayoutIcon(QPainter *painter, const QStyleOption &option, const QRect &
painter->drawPixmap(target, QIcon::fromTheme(iconName).pixmap(target.height(), target.height(), mode));
}
painter->restore();
}
QRect remainedFromIcon(const QStyleOption &option, Qt::AlignmentFlag alignment)

@ -47,7 +47,6 @@ QPalette::ColorGroup colorGroup(const QStyleOption &option);
QStringList subtracted(const QStringList &original, const QStringList &current);
void drawFormattedText(QPainter *painter, const QStyleOptionViewItem &option, const float textOpacity = 1.0);
void drawLayoutIcon(QPainter *painter, const QStyleOption &option, const QRect &target, const bool &isBackgroundFile, const QString &iconName);
//! background
void drawBackground(QPainter *painter, const QStyleOptionViewItem &option);
@ -57,6 +56,10 @@ void drawBackground(QPainter *painter, const QStyle *style, const QStyleOptionMe
QRect remainedFromIcon(const QStyleOption &option, Qt::AlignmentFlag alignment = Qt::AlignLeft);
void drawIcon(QPainter *painter, const QStyleOption &option, const QString &icon, Qt::AlignmentFlag alignment = Qt::AlignLeft);
//! layout icon
QRect remainedFromLayoutIcon(const QStyleOption &option, Qt::AlignmentFlag alignment = Qt::AlignLeft);
void drawLayoutIcon(QPainter *painter, const QStyleOption &option, const bool &isBackgroundFile, const QString &iconName, Qt::AlignmentFlag alignment = Qt::AlignLeft);
//! changes indicator
QRect remainedFromChangesIndicator(const QStyleOptionViewItem &option);
void drawChangesIndicator(QPainter *painter, const QStyleOptionViewItem &option);

@ -23,6 +23,7 @@
#include "generictools.h"
// Qt
#include <QApplication>
#include <QDebug>
#include <QPalette>
#include <QStyleOptionComboBox>
@ -31,8 +32,8 @@
namespace Latte {
namespace Settings {
const int MARGIN = 4;
const int VERTMARGIN = 4;
const int MARGIN = 2;
const int VERTMARGIN = 3;
LayoutsComboBox::LayoutsComboBox(QWidget *parent)
: QComboBox (parent)
@ -66,17 +67,24 @@ void LayoutsComboBox::paintEvent(QPaintEvent *event)
//! Adjust text and layout icon accordingly
int thick = opt.rect.height() - 2 * VERTMARGIN;
int textX = opt.rect.x() + thick + 1;
QRect textRect(textX, opt.rect.y(), opt.rect.width() - thick - 1, opt.rect.height());
QStyleOptionComboBox iconOpt = opt;
iconOpt.rect = QRect(opt.rect.x() + MARGIN, opt.rect.y() + VERTMARGIN, opt.rect.width() - 2*MARGIN, opt.rect.height() - 2 * VERTMARGIN);
QRect remained = Latte::remainedFromLayoutIcon(iconOpt, Qt::AlignLeft);
Latte::drawLayoutIcon(&painter, iconOpt, m_layoutIcon.isBackgroundFile, m_layoutIcon.name, Qt::AlignLeft);
QRect textRect;
if (qApp->layoutDirection() == Qt::LeftToRight) {
textRect = QRect(remained.x() - 2*MARGIN, opt.rect.y(), remained.width() + 4*MARGIN, opt.rect.height());
} else {
textRect = QRect(remained.x(), opt.rect.y(), remained.width() + 4*MARGIN, opt.rect.height());
}
QStyleOptionComboBox adjOpt = opt;
adjOpt.rect = textRect;
// draw text
painter.drawControl(QStyle::CE_ComboBoxLabel, adjOpt);
QRect iconRect(opt.rect.x() + MARGIN, opt.rect.y() + VERTMARGIN, thick, thick);
// draw layout icon
Latte::drawLayoutIcon(&painter, opt, iconRect, m_layoutIcon.isBackgroundFile, m_layoutIcon.name);
}

@ -45,33 +45,13 @@ BackgroundDelegate::BackgroundDelegate(QObject *parent)
void BackgroundDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionViewItem myOptions = option;
//! Remove the focus dotted lines
myOptions.state = (myOptions.state & ~QStyle::State_HasFocus);
Latte::Data::LayoutIcon icon = index.data(Qt::UserRole).value<Latte::Data::LayoutIcon>();
//! background
Latte::drawBackground(painter, option);
Latte::Data::LayoutIcon icon = index.data(Qt::UserRole).value<Latte::Data::LayoutIcon>();
if (!icon.isEmpty()) {
int localMargin = MARGIN-1;// icons[0].isBackgroundFile && icons.count() == 1 ? qMin(option.rect.height()/4,MARGIN+5) : MARGIN-1;
int aY = option.rect.y() + localMargin;
int thick = option.rect.height() - localMargin*2;
int centerX = option.rect.x() + (option.rect.width() / 2);
int step = thick;
int total_icons_width = (thick-step) + step;
if (total_icons_width > option.rect.width()){
step = thick/2;
total_icons_width = (thick-step) + step;
}
int startX = centerX - (total_icons_width/2);
Latte::drawLayoutIcon(painter, option, QRect(startX, aY, thick, thick), icon.isBackgroundFile, icon.name);
}
myOptions.rect = QRect(option.rect.x(), option.rect.y() + 1, option.rect.width(), option.rect.height() - 2);
Latte::drawLayoutIcon(painter, myOptions, icon.isBackgroundFile, icon.name, Qt::AlignHCenter);
}
}

@ -45,37 +45,15 @@ LayoutCmbItemDelegate::LayoutCmbItemDelegate(QObject *parent)
void LayoutCmbItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionViewItem myOptions = option;
//! Remove the focus dotted lines
myOptions.state = (myOptions.state & ~QStyle::State_HasFocus);
//! draw underlying background
QStyledItemDelegate::paint(painter, myOptions, index.model()->index(index.row(), Model::Layouts::HIDDENTEXTCOLUMN));
//! dbackground
Latte::drawBackground(painter, option);
Latte::Data::LayoutIcon icon = index.data(Model::Layouts::BACKGROUNDUSERROLE).value<Latte::Data::LayoutIcon>();
int iconsLength = (option.rect.height() + 4 * MARGIN);
if (!icon.isEmpty()) {
int localMargin = MARGIN-1;
int aY = option.rect.y() + localMargin;
int thick = option.rect.height() - localMargin*2;
int centerX = option.rect.x() + iconsLength / 2;
int step = thick;
int total_icons_width = (thick-step) + step;
if (total_icons_width > option.rect.width()){
step = thick/2;
total_icons_width = (thick-step) + step;
}
int startX = centerX - (total_icons_width/2);
Latte::drawLayoutIcon(painter, option, QRect(startX, aY, thick, thick), icon.isBackgroundFile, icon.name);
}
QRect remained = Latte::remainedFromLayoutIcon(option, Qt::AlignLeft);
Latte::drawLayoutIcon(painter, option, icon.isBackgroundFile, icon.name, Qt::AlignLeft);
myOptions.rect.setWidth(option.rect.width() - iconsLength);
myOptions.rect.moveLeft(option.rect.x() + iconsLength);
myOptions.rect = remained;
QStyledItemDelegate::paint(painter, myOptions, index);
}

Loading…
Cancel
Save