File indexing completed on 2024-04-21 05:30:56

0001 /*
0002     SPDX-FileCopyrightText: 2021 Michail Vourlakos <mvourlakos@gmail.com>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "genericviewtools.h"
0007 
0008 // local
0009 #include "generictools.h"
0010 
0011 // Qt
0012 #include <QApplication>
0013 #include <QDebug>
0014 #include <QStyle>
0015 #include <QTextDocument>
0016 
0017 namespace Latte {
0018 
0019 const int ICONMARGIN = 1;
0020 const int INDICATORCHANGESLENGTH = 6;
0021 const int INDICATORCHANGESMARGIN = 5;
0022 const int MARGIN = 2;
0023 
0024 void drawView(QPainter *painter, const QStyleOption &option, const Latte::Data::View &view, const QRect &availableScreenRect, const float brushOpacity)
0025 {
0026     int thick = 4;
0027     painter->save();
0028 
0029     bool selected = Latte::isSelected(option);
0030     QPalette::ColorRole viewColorRole = !selected ? QPalette::Highlight : QPalette::Text;
0031     QPen pen; pen.setWidth(thick);
0032     QColor pencolor = option.palette.color(Latte::colorGroup(option), viewColorRole);
0033     pencolor.setAlphaF(brushOpacity);
0034     pen.setColor(pencolor);
0035     painter->setPen(pen);
0036 
0037     int x = availableScreenRect.x();
0038     int y = availableScreenRect.y();
0039 
0040     int length = view.isVertical() ? availableScreenRect.height() - thick + 1 : availableScreenRect.width() - thick + 1;
0041     int max_length = length;
0042 
0043     if (view.alignment != Latte::Types::Justify) {
0044         length = 0.5 * length;
0045     }
0046 
0047     //! provide even screen length
0048     if (length % 2 == 1) {
0049         length = qMin(max_length, length + 1);
0050     }
0051 
0052     int screen_edge = (view.screenEdgeMargin > 0) ? 2 : 0;
0053 
0054     if (view.edge == Plasma::Types::TopEdge) {
0055         y = availableScreenRect.y() + thick/2 + screen_edge;
0056     } else if (view.edge == Plasma::Types::BottomEdge) {
0057         y = availableScreenRect.y() + availableScreenRect.height() - 1 - screen_edge;
0058     } else if (view.edge == Plasma::Types::LeftEdge) {
0059         x = availableScreenRect.x() + thick/2 + screen_edge;
0060     } else if (view.edge == Plasma::Types::RightEdge) {
0061         x = availableScreenRect.x() + availableScreenRect.width() - 1 - screen_edge;
0062     }
0063 
0064     if (view.isHorizontal()) {
0065         if (view.alignment == Latte::Types::Left) {
0066             x = availableScreenRect.x() + thick / 2;
0067         } else if (view.alignment == Latte::Types::Right) {
0068             x = availableScreenRect.x() + availableScreenRect.width() - length - 1;
0069         } else if (view.alignment == Latte::Types::Center) {
0070             int leftmargin = (availableScreenRect.width()/2) - (length/2);
0071             x = availableScreenRect.x() + leftmargin + 1;
0072         } else if (view.alignment == Latte::Types::Justify) {
0073             x = availableScreenRect.x() + thick / 2;
0074         }
0075     } else if (view.isVertical()) {
0076         if (view.alignment == Latte::Types::Top) {
0077             y = availableScreenRect.y() + thick / 2;
0078         } else if (view.alignment == Latte::Types::Bottom) {
0079             y = availableScreenRect.y() + availableScreenRect.height() - length - 1;
0080         } else if (view.alignment == Latte::Types::Center) {
0081             int topmargin = (availableScreenRect.height()/2) - (length/2);
0082             y = availableScreenRect.y() + topmargin + 1;
0083         } else if (view.alignment == Latte::Types::Justify) {
0084             y = availableScreenRect.y() + thick / 2;
0085         }
0086     }
0087 
0088     if (view.isHorizontal()) {
0089         painter->drawLine(x, y, x + length, y);
0090     } else if (view.isVertical()) {
0091         painter->drawLine(x, y, x, y + length);
0092     }
0093 
0094     painter->restore();
0095 }
0096 
0097 }