/* * Copyright 2017-2018 Michail Vourlakos * * This file is part of Latte-Dock * * Latte-Dock is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * Latte-Dock is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "layoutnamedelegate.h" // local #include "../layoutsmodel.h" #include "../../generic/generictools.h" // Qt #include #include #include #include #include #include #include #include #include namespace Latte { namespace Settings { namespace Layout { namespace Delegate { const int INDICATORCHANGESLENGTH = 6; const int INDICATORCHANGESMARGIN = 2; LayoutName::LayoutName(QObject *parent) : QStyledItemDelegate(parent) { } QWidget *LayoutName::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const { Q_UNUSED(option); Q_UNUSED(index); QLineEdit *editor = new QLineEdit(parent); return editor; } void LayoutName::setEditorData(QWidget *editor, const QModelIndex &index) const { QLineEdit *lineEditor = qobject_cast(editor); if (lineEditor) { QString name = index.data(Qt::UserRole).toString(); lineEditor->setText(name); } } void LayoutName::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { QLineEdit *lineEditor = qobject_cast(editor); if (lineEditor) { model->setData(index, lineEditor->text(), Qt::UserRole); } } void LayoutName::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { bool inMultiple = index.data(Model::Layouts::INMULTIPLELAYOUTSROLE).toBool(); bool isLocked = index.data(Model::Layouts::ISLOCKEDROLE).toBool(); bool isActive = index.data(Model::Layouts::ISACTIVEROLE).toBool(); bool isConsideredActive = index.data(Model::Layouts::ISCONSIDEREDACTIVEROLE).toBool(); bool isNewLayout = index.data(Model::Layouts::ISNEWLAYOUTROLE).toBool(); bool hasChanges = index.data(Model::Layouts::LAYOUTHASCHANGESROLE).toBool(); QString name = index.data(Qt::UserRole).toString(); bool isChanged = (isNewLayout || hasChanges); bool drawTwoIcons = isLocked && isConsideredActive; QStyleOptionViewItem adjustedOption = option; //! Remove the focus dotted lines adjustedOption.state = (adjustedOption.state & ~QStyle::State_HasFocus); adjustedOption.displayAlignment = Qt::AlignLeft | Qt::AlignVCenter; painter->setRenderHint(QPainter::Antialiasing, true); QRect optionRect = Latte::remainedFromChangesIndicator(option); Latte::drawChangesIndicatorBackground(painter, option); adjustedOption.rect = optionRect; if (isLocked || isConsideredActive) { QStandardItemModel *model = (QStandardItemModel *) index.model(); bool active = Latte::isActive(option); bool enabled = Latte::isEnabled(option); bool selected = Latte::isSelected(option); bool focused = Latte::isFocused(option); bool hovered = Latte::isHovered(option); //! font metrics QFontMetrics fm(option.font); int textWidth = fm.boundingRect(name).width(); int thick = optionRect.height(); int length = drawTwoIcons ? (2*thick /*+ 2*/) : thick; int startWidth = (qApp->layoutDirection() == Qt::RightToLeft) ? length : 0; int endWidth = (qApp->layoutDirection() == Qt::RightToLeft) ? 0 : length; QRect destinationS(optionRect.x(), optionRect.y(), startWidth, thick); QRect destinationE(optionRect.x() + optionRect.width() - endWidth, optionRect.y(), endWidth, thick); QStyleOptionViewItem myOptionS = adjustedOption; QStyleOptionViewItem myOptionE = adjustedOption; QStyleOptionViewItem myOptionMain = adjustedOption; myOptionMain.font.setBold(isActive); myOptionMain.font.setItalic(isChanged); myOptionS.rect = destinationS; myOptionE.rect = destinationE; myOptionMain.rect.moveLeft(optionRect.x() + startWidth); myOptionMain.rect.setWidth(optionRect.width() - startWidth - endWidth); QStyledItemDelegate::paint(painter, myOptionMain, index); //! draw background below icons //! HIDDENTEXTCOLUMN is just needed to draw empty background rectangles QStyledItemDelegate::paint(painter, myOptionS, model->index(index.row(), Model::Layouts::HIDDENTEXTCOLUMN)); QStyledItemDelegate::paint(painter, myOptionE, model->index(index.row(), Model::Layouts::HIDDENTEXTCOLUMN)); //! First Icon QIcon firstIcon = isLocked && !drawTwoIcons ? QIcon::fromTheme("object-locked") : QIcon::fromTheme("favorites"); QIcon::Mode mode = ((active && (selected || focused)) ? QIcon::Selected : QIcon::Normal); if (qApp->layoutDirection() == Qt::LeftToRight) { int firstIconX = optionRect.x() + optionRect.width() - endWidth; painter->drawPixmap(QRect(firstIconX, optionRect.y(), thick, thick), firstIcon.pixmap(thick, thick, mode)); //debug //painter->drawLine(firstIconX, optionRect.y(), firstIconX, optionRect.y()+thick); //painter->drawLine(firstIconX+thick - 1, optionRect.y(), firstIconX+thick - 1, optionRect.y()+thick); if (drawTwoIcons) { int secondIconX = optionRect.x() + optionRect.width() - thick; QIcon secondIcon = QIcon::fromTheme("object-locked"); painter->drawPixmap(QRect(secondIconX, optionRect.y(), thick, thick), secondIcon.pixmap(thick, thick, mode)); //debug //painter->drawLine(secondIconX, optionRect.y(), secondIconX, optionRect.y()+thick); //painter->drawLine(secondIconX + thick - 1, optionRect.y(), secondIconX + thick - 1,optionRect.y()+thick); } } else { painter->drawPixmap(QRect(optionRect.x(), optionRect.y(), thick, thick), firstIcon.pixmap(thick, thick, mode)); if (drawTwoIcons) { QIcon secondIcon = QIcon::fromTheme("object-locked"); painter->drawPixmap(QRect(optionRect.x() + thick, optionRect.y(), thick, thick), secondIcon.pixmap(thick, thick, mode)); } } if (isChanged) { Latte::drawChangesIndicator(painter, option); } return; } //! Draw valid text area adjustedOption.font.setBold(isActive); adjustedOption.font.setItalic(isChanged); QStyledItemDelegate::paint(painter, adjustedOption, index); if (isChanged) { Latte::drawChangesIndicator(painter, option); } } } } } }