File indexing completed on 2024-04-21 05:31:00

0001 /*
0002     SPDX-FileCopyrightText: 2021 Michail Vourlakos <mvourlakos@gmail.com>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "namedelegate.h"
0007 
0008 // local
0009 #include "../viewsmodel.h"
0010 #include "../../generic/generictools.h"
0011 #include "../../generic/genericviewtools.h"
0012 #include "../../../data/screendata.h"
0013 #include "../../../data/viewdata.h"
0014 
0015 // KDE
0016 #include <KLocalizedString>
0017 
0018 namespace Latte {
0019 namespace Settings {
0020 namespace View {
0021 namespace Delegate {
0022 
0023 NameDelegate::NameDelegate(QObject *parent)
0024     : QStyledItemDelegate(parent)
0025 {
0026 }
0027 
0028 void NameDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
0029 {
0030     editor->setGeometry(Latte::remainedFromScreenDrawing(option, false));
0031 }
0032 
0033 void NameDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
0034 {
0035     QStyleOptionViewItem myOptions = option;
0036     //! Remove the focus dotted lines
0037     myOptions.state = (myOptions.state & ~QStyle::State_HasFocus);
0038     myOptions.text = index.model()->data(index, Qt::DisplayRole).toString();
0039     myOptions.displayAlignment = static_cast<Qt::Alignment>(index.model()->data(index, Qt::TextAlignmentRole).toInt());
0040 
0041     bool isEmpty = myOptions.text.isEmpty();
0042     bool isActive = index.data(Model::Views::ISACTIVEROLE).toBool();
0043     bool isMoveOrigin = index.data(Model::Views::ISMOVEORIGINROLE).toBool();
0044     bool isChanged = (index.data(Model::Views::ISCHANGEDROLE).toBool() || index.data(Model::Views::HASCHANGEDVIEWROLE).toBool());
0045 
0046     bool hasErrors = index.data(Model::Views::ERRORSROLE).toBool();
0047     bool hasWarnings = index.data(Model::Views::WARNINGSROLE).toBool();
0048 
0049     Latte::Data::Screen screen = index.data(Model::Views::SCREENROLE).value<Latte::Data::Screen>();
0050     Latte::Data::View view = index.data(Model::Views::VIEWROLE).value<Latte::Data::View>();
0051 
0052     float textopacity = 1.0;
0053 
0054     if (isEmpty) {
0055         myOptions.text = "&lt; " + i18n("optional") + " &gt;";
0056         textopacity = 0.5;
0057     }
0058 
0059     if (isActive) {
0060         myOptions.text = "<b>" + myOptions.text + "</b>";
0061     }
0062 
0063     if (isChanged || isMoveOrigin) {
0064         myOptions.text = "<i>" + myOptions.text + "</i>";
0065     }
0066 
0067     if (isMoveOrigin) {
0068         textopacity = 0.25;
0069     }
0070 
0071     Latte::drawBackground(painter, option);
0072 
0073     // draw changes indicator
0074     QRect remainedrect = Latte::remainedFromChangesIndicator(option);
0075     if (isChanged) {
0076         Latte::drawChangesIndicator(painter, option);
0077     }
0078     myOptions.rect = remainedrect;
0079 
0080     // draw errors/warnings
0081     if (hasErrors || hasWarnings) {
0082         remainedrect = Latte::remainedFromIcon(myOptions, Qt::AlignRight, -1, 2);
0083         if (hasErrors) {
0084             Latte::drawIcon(painter, myOptions, "data-error", Qt::AlignRight, -1, 2);
0085         } else if (hasWarnings) {
0086             Latte::drawIcon(painter, myOptions, "data-warning", Qt::AlignRight, -1, 2);
0087         }
0088         myOptions.rect = remainedrect;
0089     }
0090 
0091     // draw screen icon
0092     int maxiconsize = -1; //disabled
0093     remainedrect = Latte::remainedFromScreenDrawing(myOptions, screen.isScreensGroup(), maxiconsize);
0094     QRect availableScreenRect = Latte::drawScreen(painter, myOptions, screen.isScreensGroup(), screen.geometry, maxiconsize, textopacity);
0095     Latte::drawView(painter, myOptions, view, availableScreenRect, textopacity);
0096 
0097     myOptions.rect = remainedrect;
0098     Latte::drawFormattedText(painter, myOptions, textopacity);
0099 }
0100 
0101 }
0102 }
0103 }
0104 }