File indexing completed on 2024-03-24 17:26:50

0001 /*
0002     This file is part of the Okteta Gui library, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2003, 2008, 2019 Friedrich W. H. Kossebau <kossebau@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #include "valuebytearraycolumnrenderer_p.hpp"
0010 
0011 // lib
0012 #include "helper.hpp"
0013 // lib
0014 #include <abstractcolumnstylist.hpp>
0015 // Okteta core
0016 #include <Okteta/ValueCodec>
0017 #include <Okteta/CharCodec>
0018 // KF
0019 #include <KColorScheme>
0020 // Qt
0021 #include <QPainter>
0022 
0023 
0024 namespace Okteta {
0025 
0026 void ValueByteArrayColumnRendererPrivate::setValueCodec(ValueCoding valueCoding, const ValueCodec* valueCodec)
0027 {
0028     mValueCoding = valueCoding;
0029     mValueCodec = valueCodec;
0030     mDecodedByteText.resize(mValueCodec->encodingWidth());
0031 
0032     // recalculate depend sizes
0033     recalcByteWidth();
0034 
0035     if (mLinePosLeftPixelX) {
0036         recalcX();
0037     }
0038 }
0039 
0040 bool ValueByteArrayColumnRendererPrivate::setBinaryGapWidth(PixelX binaryGapWidth)
0041 {
0042     // no changes?
0043     if (mBinaryGapWidth == binaryGapWidth) {
0044         return false;
0045     }
0046 
0047     mBinaryGapWidth = binaryGapWidth;
0048 
0049     // recalculate depend sizes
0050     recalcByteWidth();
0051 
0052     if (mLinePosLeftPixelX) {
0053         recalcX();
0054     }
0055     return true;
0056 }
0057 
0058 void ValueByteArrayColumnRendererPrivate::recalcByteWidth()
0059 {
0060     // use 0 as reference, using a fixed font should always yield same width
0061     mValueCodec->encode(&mDecodedByteText, 0, Byte(0));
0062     if (mValueCoding == BinaryCoding) {
0063 #if (QT_VERSION >= QT_VERSION_CHECK(5, 11, 0))
0064         const int binaryHalfWidth = mFontMetrics.horizontalAdvance(mDecodedByteText.left(4));
0065 #else
0066         const int binaryHalfWidth = mFontMetrics.width(mDecodedByteText.left(4));
0067 #endif
0068         mBinaryHalfOffset = binaryHalfWidth + mBinaryGapWidth;
0069         setByteWidth(mBinaryHalfOffset + binaryHalfWidth);
0070     } else {
0071 #if (QT_VERSION >= QT_VERSION_CHECK(5, 11, 0))
0072         setByteWidth(mFontMetrics.horizontalAdvance(mDecodedByteText));
0073 #else
0074         setByteWidth(mFontMetrics.width(mDecodedByteText));
0075 #endif
0076     }
0077 }
0078 
0079 // perhaps sometimes there will be a grammar
0080 void ValueByteArrayColumnRendererPrivate::renderEditedByte(QPainter* painter, Byte byte, const QString& editBuffer)
0081 {
0082     Q_Q(ValueByteArrayColumnRenderer);
0083 
0084     const Character byteChar = mCharCodec->decode(byte);
0085 
0086     const QPalette& palette = mStylist->palette();
0087     KColorScheme colorScheme(palette.currentColorGroup(), KColorScheme::View);
0088     const KColorScheme::ForegroundRole foregroundRole =
0089         mByteTypeColored ? foregroundRoleForChar(byteChar) : KColorScheme::NormalText;
0090     const QBrush brush = colorScheme.foreground(foregroundRole);
0091     painter->fillRect(0, 0, byteWidth(), q->lineHeight(), brush);
0092 
0093     const QBrush backgroundBrush = colorScheme.background();
0094     const QColor& charColor = backgroundBrush.color();
0095     renderCode(painter, editBuffer, charColor);
0096 }
0097 
0098 void ValueByteArrayColumnRendererPrivate::renderByteText(QPainter* painter,
0099                                                          Byte byte, Character byteChar, const QColor& color) const
0100 {
0101     Q_UNUSED(byteChar)
0102 
0103     mValueCodec->encode(&mDecodedByteText, 0, byte);
0104     renderCode(painter, mDecodedByteText, color);
0105 }
0106 
0107 void ValueByteArrayColumnRendererPrivate::renderCode(QPainter* painter, const QString& code, const QColor& color) const
0108 {
0109     painter->setPen(color);
0110     if (mValueCoding == Okteta::BinaryCoding) {
0111         // leave a gap in the middle
0112         painter->drawText(0, mDigitBaseLine, code.left(4));
0113         painter->drawText(mBinaryHalfOffset, mDigitBaseLine, code.right(4));
0114     } else {
0115         painter->drawText(0, mDigitBaseLine, code);
0116     }
0117 }
0118 
0119 }