File indexing completed on 2024-04-21 13:07:37

0001 /*
0002     SPDX-FileCopyrightText: 2017-2018 Michail Vourlakos <mvourlakos@gmail.com>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "layoutnamedelegate.h"
0007 
0008 // local
0009 #include "../layoutsmodel.h"
0010 #include "../../generic/generictools.h"
0011 
0012 // Qt
0013 #include <QApplication>
0014 #include <QBitmap>
0015 #include <QDebug>
0016 #include <QEvent>
0017 #include <QKeyEvent>
0018 #include <QLineEdit>
0019 #include <QMouseEvent>
0020 #include <QPainter>
0021 #include <QStandardItemModel>
0022 
0023 namespace Latte {
0024 namespace Settings {
0025 namespace Layout {
0026 namespace Delegate {
0027 
0028 const int INDICATORCHANGESLENGTH = 6;
0029 const int INDICATORCHANGESMARGIN = 2;
0030 
0031 LayoutName::LayoutName(QObject *parent)
0032     : QStyledItemDelegate(parent)
0033 {
0034 }
0035 
0036 QWidget *LayoutName::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
0037 {
0038     Q_UNUSED(option);
0039     Q_UNUSED(index);
0040 
0041     QLineEdit *editor = new QLineEdit(parent);
0042     return editor;
0043 }
0044 
0045 void LayoutName::setEditorData(QWidget *editor, const QModelIndex &index) const
0046 {
0047     QLineEdit *lineEditor = qobject_cast<QLineEdit *>(editor);
0048 
0049     if (lineEditor) {
0050         QString name = index.data(Qt::UserRole).toString();
0051         lineEditor->setText(name);
0052     }
0053 }
0054 
0055 void LayoutName::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
0056 {
0057     QLineEdit *lineEditor = qobject_cast<QLineEdit *>(editor);
0058 
0059     if (lineEditor) {
0060         model->setData(index, lineEditor->text(), Qt::UserRole);
0061     }
0062 }
0063 
0064 void LayoutName::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
0065 {
0066     bool inMultiple = index.data(Model::Layouts::INMULTIPLELAYOUTSROLE).toBool();
0067 
0068     bool isLocked = index.data(Model::Layouts::ISLOCKEDROLE).toBool();
0069     bool isActive = index.data(Model::Layouts::ISACTIVEROLE).toBool();
0070     bool isConsideredActive = index.data(Model::Layouts::ISCONSIDEREDACTIVEROLE).toBool();
0071 
0072     bool isNewLayout = index.data(Model::Layouts::ISNEWLAYOUTROLE).toBool();
0073     bool hasChanges = index.data(Model::Layouts::LAYOUTHASCHANGESROLE).toBool();
0074     bool hasErrors = index.data(Model::Layouts::ERRORSROLE).toBool();
0075     bool hasWarnings = index.data(Model::Layouts::WARNINGSROLE).toBool();
0076 
0077     QString name = index.data(Qt::UserRole).toString();
0078 
0079     bool isChanged = (isNewLayout || hasChanges);
0080 
0081     QStyleOptionViewItem myOptions = option;
0082     myOptions.text = name;
0083 
0084     //! Remove the focus dotted lines
0085     myOptions.state = (myOptions.state & ~QStyle::State_HasFocus);
0086     myOptions.displayAlignment = static_cast<Qt::Alignment>(index.model()->data(index, Qt::TextAlignmentRole).toInt());;
0087 
0088     //! backround
0089     Latte::drawBackground(painter, option);
0090 
0091     painter->setRenderHint(QPainter::Antialiasing, true);
0092 
0093     //! Changes Indicator
0094     QRect remainedrect = Latte::remainedFromChangesIndicator(myOptions);
0095     if (isChanged) {
0096         Latte::drawChangesIndicator(painter, option);
0097     }
0098 
0099     myOptions.rect = remainedrect;
0100 
0101     if (hasErrors || hasWarnings) {
0102         remainedrect = Latte::remainedFromIcon(myOptions, Qt::AlignRight, -1, 2);
0103         if (hasErrors) {
0104             Latte::drawIcon(painter, myOptions, "data-error", Qt::AlignRight, -1, 2);
0105         } else if (hasWarnings) {
0106             Latte::drawIcon(painter, myOptions, "data-warning", Qt::AlignRight, -1, 2);
0107         }
0108         myOptions.rect = remainedrect;
0109     }
0110 
0111     if (isConsideredActive) {
0112         remainedrect = Latte::remainedFromIcon(myOptions, Qt::AlignRight, -1, 1);
0113         Latte::drawIcon(painter, myOptions, "favorite", Qt::AlignRight, -1, 1);
0114         myOptions.rect = remainedrect;
0115     }
0116 
0117     if (isLocked) {
0118         remainedrect = Latte::remainedFromIcon(myOptions, Qt::AlignRight, -1, 1);
0119         Latte::drawIcon(painter, myOptions, "object-locked", Qt::AlignRight, -1, 1);
0120         myOptions.rect = remainedrect;
0121     }
0122 
0123     if (isActive) {
0124         myOptions.text = "<b>" + myOptions.text + "</b>";
0125     }
0126 
0127     if (isChanged) {
0128         myOptions.text = "<i>" + myOptions.text + "</i>";
0129     }
0130 
0131     Latte::drawFormattedText(painter, myOptions);
0132 }
0133 
0134 }
0135 }
0136 }
0137 }