File indexing completed on 2024-12-08 13:30:58

0001 // clang-format off
0002 /*
0003  *  This file is part of KDiff3.
0004  *
0005  * SPDX-FileCopyrightText: 2002-2011 Joachim Eibl, joachim.eibl at gmx.de
0006  * SPDX-FileCopyrightText: 2018-2020 Michael Reeves reeves.87@gmail.com
0007  * SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 // clang-format on
0010 
0011 #ifndef RLPAINTER_H
0012 #define RLPAINTER_H
0013 
0014 #include "TypeUtils.h"
0015 
0016 #include <QPainter>
0017 // Helper class that swaps left and right for some commands.
0018 
0019 /*
0020     This class assists with changing KDiff3 between RTL and LTR layout.
0021 */
0022 class RLPainter: public QPainter
0023 {
0024   private:
0025     qint32 m_xOffset;
0026     qint32 m_fontWidth;
0027     bool bRightToLeft = false;
0028 
0029   public:
0030     RLPainter(QPaintDevice* pd, bool bRTL, qint32 width, qint32 fontWidth):
0031         QPainter(pd)
0032     {
0033         bRightToLeft = bRTL;
0034         if(bRTL)
0035         {
0036             m_fontWidth = fontWidth;
0037             m_xOffset = width - 1;
0038         }
0039         else
0040         {
0041             m_fontWidth = 0;
0042             m_xOffset = 0;
0043         }
0044     }
0045 
0046     void fillRect(qint32 x, qint32 y, qint32 w, qint32 h, const QBrush& b)
0047     {
0048         if(!bRightToLeft)
0049             QPainter::fillRect(m_xOffset + x, y, w, h, b);
0050         else
0051             QPainter::fillRect(m_xOffset - x - w, y, w, h, b);
0052     }
0053 
0054     void drawText(qint32 x, qint32 y, const QString& s, bool bAdapt = false)
0055     {
0056         qint32 len = SafeInt<qint32>(s.length());
0057         Qt::LayoutDirection ld = (!bRightToLeft || !bAdapt) ? Qt::LeftToRight : Qt::RightToLeft;
0058         // Qt will automaticly reverse the text as needed just set the layout direction
0059         QPainter::setLayoutDirection(ld);
0060         if(ld == Qt::RightToLeft)
0061         {
0062             QPainter::drawText(m_xOffset - m_fontWidth * len - x, y, s);
0063             return;
0064         }
0065         QPainter::drawText(m_xOffset - m_fontWidth * len + x, y, s);
0066     }
0067 
0068     void drawLine(qint32 x1, qint32 y1, qint32 x2, qint32 y2)
0069     {
0070         if(bRightToLeft)
0071             QPainter::drawLine(m_xOffset - x1, y1, m_xOffset - x2, y2);
0072         else
0073             QPainter::drawLine(m_xOffset + x1, y1, m_xOffset + x2, y2);
0074     }
0075 };
0076 
0077 #endif