mirror of
https://github.com/KDE/latte-dock.git
synced 2025-02-02 05:47:18 +03:00
simplify layouts icon drawing logic
--in the past a layout could draw more than own icons in order to be identified. Now things are simplified and at all cases ONLY ONE icon is chosen for each layout based on criteria. In all cases if the user has chosen a specific icon for its layout that is respected for all drawing cases.
This commit is contained in:
parent
d903da90e7
commit
82ce6b5140
@ -41,6 +41,11 @@ LayoutIcon::LayoutIcon(const LayoutIcon &o)
|
||||
{
|
||||
}
|
||||
|
||||
bool LayoutIcon::isEmpty() const
|
||||
{
|
||||
return (id.isEmpty() && name.isEmpty());
|
||||
}
|
||||
|
||||
LayoutIcon &LayoutIcon::operator=(LayoutIcon &&rhs)
|
||||
{
|
||||
id = rhs.id;
|
||||
|
@ -41,6 +41,8 @@ public:
|
||||
//! Layout data
|
||||
bool isBackgroundFile{true};
|
||||
|
||||
bool isEmpty() const;
|
||||
|
||||
//! Operators
|
||||
LayoutIcon &operator=(const LayoutIcon &rhs);
|
||||
LayoutIcon &operator=(LayoutIcon &&rhs);
|
||||
|
@ -51,9 +51,9 @@ void BackgroundDelegate::paint(QPainter *painter, const QStyleOptionViewItem &op
|
||||
//! draw underlying background
|
||||
QStyledItemDelegate::paint(painter, myOptions, index.model()->index(index.row(), Model::Layouts::HIDDENTEXTCOLUMN));
|
||||
|
||||
QList<Latte::Data::LayoutIcon> icons = index.data(Qt::UserRole).value<QList<Latte::Data::LayoutIcon>>();
|
||||
Latte::Data::LayoutIcon icon = index.data(Qt::UserRole).value<Latte::Data::LayoutIcon>();
|
||||
|
||||
if (icons.count() > 0) {
|
||||
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;
|
||||
@ -61,55 +61,19 @@ void BackgroundDelegate::paint(QPainter *painter, const QStyleOptionViewItem &op
|
||||
|
||||
int centerX = option.rect.x() + (option.rect.width() / 2);
|
||||
int step = thick;
|
||||
int total_icons_width = (thick-step) + icons.count() * step;
|
||||
int total_icons_width = (thick-step) + step;
|
||||
|
||||
if (total_icons_width > option.rect.width()){
|
||||
step = thick/2;
|
||||
total_icons_width = (thick-step) + icons.count() * step;
|
||||
total_icons_width = (thick-step) + step;
|
||||
}
|
||||
|
||||
int startX = centerX - (total_icons_width/2);
|
||||
|
||||
for (int i=0; i<icons.count(); ++i) {
|
||||
int tX = startX + (i * step);
|
||||
drawIcon(painter, option, QRect(tX, aY, thick, thick), icons[i]);
|
||||
}
|
||||
Latte::drawLayoutIcon(painter, option, QRect(startX, aY, thick, thick), icon);
|
||||
}
|
||||
}
|
||||
|
||||
void BackgroundDelegate::drawIcon(QPainter *painter, const QStyleOptionViewItem &option, const QRect &target, const Latte::Data::LayoutIcon &icon) const
|
||||
{
|
||||
bool active = Latte::isActive(option);
|
||||
bool selected = Latte::isSelected(option);
|
||||
bool focused = Latte::isFocused(option);
|
||||
|
||||
painter->setRenderHint(QPainter::Antialiasing, true);
|
||||
|
||||
if (icon.isBackgroundFile) {
|
||||
int backImageMargin = qMin(option.rect.height()/4, MARGIN+2);
|
||||
QRect backTarget(target.x() + backImageMargin, target.y() + backImageMargin, target.width() - 2*backImageMargin, target.height() - 2*backImageMargin);
|
||||
|
||||
QPixmap backImage(icon.name);
|
||||
backImage = backImage.copy(backTarget);
|
||||
|
||||
QPalette::ColorRole textColorRole = selected ? QPalette::HighlightedText : QPalette::Text;
|
||||
|
||||
QBrush imageBrush(backImage);
|
||||
QPen pen; pen.setWidth(1);
|
||||
pen.setColor(option.palette.color(Latte::colorGroup(option), textColorRole));
|
||||
|
||||
painter->setBrush(imageBrush);
|
||||
painter->setPen(pen);
|
||||
|
||||
painter->drawEllipse(backTarget);
|
||||
} else {
|
||||
QIcon::Mode mode = ((active && (selected || focused)) ? QIcon::Selected : QIcon::Normal);
|
||||
|
||||
painter->drawPixmap(target, QIcon::fromTheme(icon.name).pixmap(target.height(), target.height(), mode));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -42,9 +42,6 @@ public:
|
||||
|
||||
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
|
||||
|
||||
private:
|
||||
void drawIcon(QPainter *painter, const QStyleOptionViewItem &option, const QRect &target, const Latte::Data::LayoutIcon &icon) const;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -51,11 +51,11 @@ void LayoutCmbItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem
|
||||
//! draw underlying background
|
||||
QStyledItemDelegate::paint(painter, myOptions, index.model()->index(index.row(), Model::Layouts::HIDDENTEXTCOLUMN));
|
||||
|
||||
QList<Latte::Data::LayoutIcon> icons = index.data(Model::Layouts::BACKGROUNDUSERROLE).value<QList<Latte::Data::LayoutIcon>>();
|
||||
Latte::Data::LayoutIcon icon = index.data(Model::Layouts::BACKGROUNDUSERROLE).value<Latte::Data::LayoutIcon>();
|
||||
|
||||
int iconsLength = (option.rect.height() + 4 * MARGIN);
|
||||
|
||||
if (icons.count() > 0) {
|
||||
if (!icon.isEmpty()) {
|
||||
int localMargin = MARGIN-1;
|
||||
|
||||
int aY = option.rect.y() + localMargin;
|
||||
@ -63,19 +63,15 @@ void LayoutCmbItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem
|
||||
|
||||
int centerX = option.rect.x() + iconsLength / 2;
|
||||
int step = thick;
|
||||
int total_icons_width = (thick-step) + icons.count() * step;
|
||||
int total_icons_width = (thick-step) + step;
|
||||
|
||||
if (total_icons_width > option.rect.width()){
|
||||
step = thick/2;
|
||||
total_icons_width = (thick-step) + icons.count() * step;
|
||||
total_icons_width = (thick-step) + step;
|
||||
}
|
||||
|
||||
int startX = centerX - (total_icons_width/2);
|
||||
|
||||
for (int i=0; i<icons.count(); ++i) {
|
||||
int tX = startX + (i * step);
|
||||
Latte::drawLayoutIcon(painter, option, QRect(tX, aY, thick, thick), icons[i]);
|
||||
}
|
||||
Latte::drawLayoutIcon(painter, option, QRect(startX, aY, thick, thick), icon);
|
||||
}
|
||||
|
||||
myOptions.rect.setWidth(option.rect.width() - iconsLength);
|
||||
|
@ -407,49 +407,43 @@ void Layouts::setIconsPath(QString iconsPath)
|
||||
m_iconsPath = iconsPath;
|
||||
}
|
||||
|
||||
QList<Latte::Data::LayoutIcon> Layouts::iconsForCentralLayout(const int &row) const
|
||||
Latte::Data::LayoutIcon Layouts::iconForCentralLayout(const int &row) const
|
||||
{
|
||||
QList<Latte::Data::LayoutIcon> icons;
|
||||
Latte::Data::LayoutIcon _icon;
|
||||
|
||||
if (!m_layoutsTable[row].icon.isEmpty()) {
|
||||
//! if there is specific icon set from the user for this layout we draw only that icon
|
||||
Latte::Data::LayoutIcon icon;
|
||||
icon.name = m_layoutsTable[row].icon;
|
||||
icon.isBackgroundFile = false;
|
||||
icons << icon;
|
||||
return icons;
|
||||
_icon.name = m_layoutsTable[row].icon;
|
||||
_icon.isBackgroundFile = false;
|
||||
return _icon;
|
||||
}
|
||||
|
||||
if (inMultipleMode()) {
|
||||
if (m_layoutsTable[row].activities.contains(Latte::Data::Layout::ALLACTIVITIESID)) {
|
||||
Latte::Data::LayoutIcon icon;
|
||||
icon.name = m_activitiesTable[Latte::Data::Layout::ALLACTIVITIESID].icon;
|
||||
icon.isBackgroundFile = false;
|
||||
icons << icon;
|
||||
_icon.name = m_activitiesTable[Latte::Data::Layout::ALLACTIVITIESID].icon;
|
||||
_icon.isBackgroundFile = false;
|
||||
return _icon;
|
||||
} else if (m_layoutsTable[row].activities.contains(Latte::Data::Layout::FREEACTIVITIESID)) {
|
||||
Latte::Data::LayoutIcon icon;
|
||||
icon.name = m_activitiesTable[Latte::Data::Layout::FREEACTIVITIESID].icon;
|
||||
icon.isBackgroundFile = false;
|
||||
icons << icon;
|
||||
_icon.name = m_activitiesTable[Latte::Data::Layout::FREEACTIVITIESID].icon;
|
||||
_icon.isBackgroundFile = false;
|
||||
return _icon;
|
||||
} else {
|
||||
QStringList activitiesIds = m_layoutsTable[row].activities;
|
||||
|
||||
for(int i=0; i<activitiesIds.count(); ++i) {
|
||||
QString id = activitiesIds[i];
|
||||
if (m_activitiesTable.containsId(id)) {
|
||||
Latte::Data::LayoutIcon icon;
|
||||
icon.name = m_activitiesTable[id].icon;
|
||||
icon.isBackgroundFile = false;
|
||||
icons << icon;
|
||||
_icon.name = m_activitiesTable[id].icon;
|
||||
_icon.isBackgroundFile = false;
|
||||
//! first activity icon found
|
||||
return icons;
|
||||
return _icon;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//! fallback icon: background image
|
||||
if (icons.count() == 0) {
|
||||
if (_icon.isEmpty()) {
|
||||
QString colorPath;
|
||||
|
||||
if (m_layoutsTable[row].backgroundStyle == Layout::PatternBackgroundStyle && m_layoutsTable[row].background.isEmpty()) {
|
||||
@ -459,31 +453,29 @@ QList<Latte::Data::LayoutIcon> Layouts::iconsForCentralLayout(const int &row) co
|
||||
}
|
||||
|
||||
if (QFileInfo(colorPath).exists()) {
|
||||
Latte::Data::LayoutIcon icon;
|
||||
icon.isBackgroundFile = true;
|
||||
icon.name = colorPath;
|
||||
icons << icon;
|
||||
_icon.isBackgroundFile = true;
|
||||
_icon.name = colorPath;
|
||||
return _icon;
|
||||
}
|
||||
}
|
||||
|
||||
return icons;
|
||||
return Latte::Data::LayoutIcon();
|
||||
}
|
||||
|
||||
QList<Latte::Data::LayoutIcon> Layouts::icons(const int &row) const
|
||||
Latte::Data::LayoutIcon Layouts::icon(const int &row) const
|
||||
{
|
||||
return iconsForCentralLayout(row);
|
||||
return iconForCentralLayout(row);
|
||||
}
|
||||
|
||||
const Latte::Data::LayoutIcon Layouts::currentLayoutIcon(const QString &id) const
|
||||
{
|
||||
int row = rowForId(id);
|
||||
QList<Latte::Data::LayoutIcon> iconsList;
|
||||
|
||||
if (row >= 0) {
|
||||
iconsList = icons(row);
|
||||
return icon(row);
|
||||
}
|
||||
|
||||
return iconsList.count() > 0 ? iconsList[0] : Latte::Data::LayoutIcon();
|
||||
return Latte::Data::LayoutIcon();
|
||||
}
|
||||
|
||||
QString Layouts::sortableText(const int &priority, const int &row) const
|
||||
@ -564,10 +556,10 @@ QVariant Layouts::data(const QModelIndex &index, int role) const
|
||||
} else if (role == LAYOUTHASCHANGESROLE) {
|
||||
return isNewLayout ? true : (original != m_layoutsTable[row]);
|
||||
} else if (role == BACKGROUNDUSERROLE) {
|
||||
QList<Latte::Data::LayoutIcon> iconsList = icons(row);
|
||||
QVariant iconsVariant;
|
||||
iconsVariant.setValue<QList<Latte::Data::LayoutIcon>>(iconsList);
|
||||
return iconsVariant;
|
||||
Latte::Data::LayoutIcon _icon = icon(row);
|
||||
QVariant iconVariant;
|
||||
iconVariant.setValue<Latte::Data::LayoutIcon>(_icon);
|
||||
return iconVariant;
|
||||
}
|
||||
|
||||
switch (column) {
|
||||
@ -586,10 +578,10 @@ QVariant Layouts::data(const QModelIndex &index, int role) const
|
||||
if (role == Qt::DisplayRole) {
|
||||
return m_layoutsTable[row].background;
|
||||
} else if (role == Qt::UserRole) {
|
||||
QList<Latte::Data::LayoutIcon> iconsList = icons(row);
|
||||
QVariant iconsVariant;
|
||||
iconsVariant.setValue<QList<Latte::Data::LayoutIcon>>(iconsList);
|
||||
return iconsVariant;
|
||||
Latte::Data::LayoutIcon _icon = icon(row);
|
||||
QVariant iconVariant;
|
||||
iconVariant.setValue<Latte::Data::LayoutIcon>(_icon);
|
||||
return iconVariant;
|
||||
}
|
||||
break;
|
||||
case NAMECOLUMN:
|
||||
|
@ -177,8 +177,8 @@ private:
|
||||
|
||||
QStringList cleanStrings(const QStringList &original, const QStringList &occupied);
|
||||
|
||||
QList<Latte::Data::LayoutIcon> icons(const int &row) const;
|
||||
QList<Latte::Data::LayoutIcon> iconsForCentralLayout(const int &row) const;
|
||||
Latte::Data::LayoutIcon icon(const int &row) const;
|
||||
Latte::Data::LayoutIcon iconForCentralLayout(const int &row) const;
|
||||
|
||||
private:
|
||||
QString m_iconsPath;
|
||||
|
Loading…
x
Reference in New Issue
Block a user