File indexing completed on 2024-04-21 05:53:10

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         const int binaryHalfWidth = mFontMetrics.horizontalAdvance(mDecodedByteText.left(4));
0064         mBinaryHalfOffset = binaryHalfWidth + mBinaryGapWidth;
0065         setByteWidth(mBinaryHalfOffset + binaryHalfWidth);
0066     } else {
0067         setByteWidth(mFontMetrics.horizontalAdvance(mDecodedByteText));
0068     }
0069 }
0070 
0071 // perhaps sometimes there will be a grammar
0072 void ValueByteArrayColumnRendererPrivate::renderEditedByte(QPainter* painter, Byte byte, const QString& editBuffer)
0073 {
0074     Q_Q(ValueByteArrayColumnRenderer);
0075 
0076     const Character byteChar = mCharCodec->decode(byte);
0077 
0078     const QPalette& palette = mStylist->palette();
0079     KColorScheme colorScheme(palette.currentColorGroup(), KColorScheme::View);
0080     const KColorScheme::ForegroundRole foregroundRole =
0081         mByteTypeColored ? foregroundRoleForChar(byteChar) : KColorScheme::NormalText;
0082     const QBrush brush = colorScheme.foreground(foregroundRole);
0083     painter->fillRect(0, 0, byteWidth(), q->lineHeight(), brush);
0084 
0085     const QBrush backgroundBrush = colorScheme.background();
0086     const QColor& charColor = backgroundBrush.color();
0087     renderCode(painter, editBuffer, charColor);
0088 }
0089 
0090 void ValueByteArrayColumnRendererPrivate::renderByteText(QPainter* painter,
0091                                                          Byte byte, Character byteChar, const QColor& color) const
0092 {
0093     Q_UNUSED(byteChar)
0094 
0095     mValueCodec->encode(&mDecodedByteText, 0, byte);
0096     renderCode(painter, mDecodedByteText, color);
0097 }
0098 
0099 void ValueByteArrayColumnRendererPrivate::renderCode(QPainter* painter, const QString& code, const QColor& color) const
0100 {
0101     painter->setPen(color);
0102     if (mValueCoding == Okteta::BinaryCoding) {
0103         // leave a gap in the middle
0104         painter->drawText(0, mDigitBaseLine, code.left(4));
0105         painter->drawText(mBinaryHalfOffset, mDigitBaseLine, code.right(4));
0106     } else {
0107         painter->drawText(0, mDigitBaseLine, code);
0108     }
0109 }
0110 
0111 }