File indexing completed on 2024-04-14 15:52:50

0001 /*
0002     This file is part of the Okteta Gui library, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2003, 2007-2009, 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 #ifndef OKTETA_ABSTRACTBYTEARRAYCOLUMNRENDERER_P_HPP
0010 #define OKTETA_ABSTRACTBYTEARRAYCOLUMNRENDERER_P_HPP
0011 
0012 // lib
0013 #include "abstractbytearraycolumnrenderer.hpp"
0014 #include "abstractcolumnrenderer_p.hpp"
0015 #include "oktetagui.hpp"
0016 // Okteta core
0017 #include <Okteta/AbstractByteArrayModel>
0018 #include <Okteta/Bookmarkable>
0019 #include <Okteta/Character>
0020 // Qt
0021 #include <QFontMetrics>
0022 
0023 class QColor;
0024 class QBrush;
0025 
0026 namespace Okteta {
0027 
0028 class Bookmarkable;
0029 
0030 class AbstractByteArrayColumnRendererPrivate : public AbstractColumnRendererPrivate
0031 {
0032 public:
0033     AbstractByteArrayColumnRendererPrivate(AbstractByteArrayColumnRenderer* q,
0034                                            AbstractColumnStylist* stylist,
0035                                            AbstractByteArrayModel* byteArrayModel,
0036                                            ByteArrayTableLayout* layout,
0037                                            ByteArrayTableRanges* ranges);
0038 
0039     ~AbstractByteArrayColumnRendererPrivate() override;
0040 
0041 public: // AbstractColumnRenderer API
0042     void renderFirstLine(QPainter* painter, const PixelXRange& Xs, Line firstLineIndex);
0043     void renderNextLine(QPainter* painter);
0044 
0045 public:
0046     void prepareRendering(const PixelXRange& Xs);
0047 
0048 public:
0049     void renderLinePositions(QPainter* painter, Line lineIndex, const LineRange& linePositions);
0050     /** paints a cursor based on the type of the byte.
0051      * @param painter The QPainter
0052      * @param byteIndex Index of the byte to paint the cursor for. If -1 a space is used as char.
0053      */
0054     void renderCursor(QPainter* painter, Address byteIndex);
0055     /** paints the byte with background.
0056      * @param painter The QPainter
0057      * @param byteIndex Index of the byte to paint. If -1 only the background is painted.
0058      */
0059     void renderByte(QPainter* painter, Address byteIndex);
0060     /** paints the byte with background and a frame around.
0061      * @param painter The QPainter
0062      * @param byteIndex Index of the byte to paint the frame for. If -1 a space is used as char.
0063      * @param style the style of the framing
0064      */
0065     void renderFramedByte(QPainter* painter, Address byteIndex, AbstractByteArrayColumnRenderer::FrameStyle style);
0066 
0067 public: // modification access
0068     /** sets the spacing in the hex column
0069      * @param byteSpacingWidth spacing between the bytes in pixels
0070      * @param noOfGroupedBytes numbers of grouped bytes, 0 means no grouping
0071      * @param groupSpacingWidth spacing between the groups in pixels
0072      * returns true if there was a change
0073      */
0074     bool setSpacing(PixelX byteSpacingWidth, int noOfGroupedBytes = 0, PixelX groupSpacingWidth = 0);
0075     /** sets the spacing between the bytes in the hex column
0076      * @param byteSpacingWidth spacing between the bytes in pixels
0077      * returns true if there was a change
0078      */
0079     bool setByteSpacingWidth(PixelX byteSpacingWidth);
0080     /** sets the number of grouped bytes in the hex column
0081      * @param noOfGroupedBytes numbers of grouped bytes, 0 means no grouping
0082      * returns true if there was a change
0083      */
0084     bool setNoOfGroupedBytes(int noOfGroupedBytes);
0085     /** sets the spacing between the groups of bytes in the hex column
0086      * @param groupSpacingWidth spacing between the groups in pixels
0087      * returns true if there was a change
0088      */
0089     bool setGroupSpacingWidth(PixelX groupSpacingWidth);
0090     /** sets the metrics of the used font
0091      */
0092     void setFontMetrics(const QFontMetrics& fontMetrics);
0093     /** */
0094     void set(AbstractByteArrayModel* byteArrayModel);
0095     /** creates new buffer for x-values; to be called on any change of NoOfBytesPerLine or metrics */
0096     void resetXBuffer();
0097     /** sets the codec to be used by the char column. */
0098     void setCharCodec(const CharCodec* charCodec);
0099 
0100     void setByteTypeColored(bool byteTypeColored);
0101 
0102 public: // functional logic
0103     /** returns byte linePositions covered by pixels with absolute x-coord x */
0104     LinePositionRange linePositionsOfX(PixelX x, PixelX width) const;
0105     /** returns byte pos at pixel with absolute x-coord x */
0106     LinePosition linePositionOfX(PixelX x) const;
0107     /** returns byte pos at pixel with absolute x-coord x, and sets the flag to true if we are closer to the right */
0108     LinePosition magneticLinePositionOfX(PixelX x) const;
0109     /** returns absolute x-coord of byte at position posInLine */
0110     PixelX xOfLinePosition(LinePosition posInLine) const;
0111     /** returns right absolute x-coord of byte at position posInLine */
0112     PixelX rightXOfLinePosition(LinePosition posInLine) const;
0113     /** returns byte pos at pixel with relative x-coord x */
0114     LinePosition linePositionOfColumnX(PixelX x) const;
0115     /** returns byte linePositions covered by pixels with relative x-coord x */
0116     LinePositionRange linePositionsOfColumnXs(PixelX x, PixelX width) const;
0117     /** returns relative x-coord of byte at position posInLine */
0118     PixelX columnXOfLinePosition(LinePosition posInLine) const;
0119     /** returns right relative x-coord of byte at position posInLine */
0120     PixelX columnRightXOfLinePosition(LinePosition posInLine) const;
0121     /** returns the linePositions that overlap with the x-coords relative to the view */
0122     LinePositionRange visibleLinePositions(PixelX x, PixelX width) const;
0123     /** returns the */
0124     PixelXRange xsOfLinePositionsInclSpaces(const LinePositionRange& linePositions) const;
0125     /** */
0126     PixelXRange columnXsOfLinePositionsInclSpaces(const LinePositionRange& linePositions) const;
0127 
0128     QRect byteRect(Coord coord) const;
0129 
0130 public: // value access
0131     PixelX byteWidth() const;
0132     PixelX digitWidth() const;
0133     PixelX groupSpacingWidth() const;
0134     PixelX byteSpacingWidth() const;
0135     int noOfGroupedBytes() const;
0136 
0137     LinePosition firstLinePos() const;
0138     LinePosition lastLinePos()  const;
0139     LinePositionRange visibleLinePositions() const;
0140     const ByteArrayTableLayout* layout() const;
0141     bool isByteTypeColored() const;
0142 
0143 protected: // API to be redefined
0144     virtual void renderByteText(QPainter* painter, Byte byte, Character charByte, const QColor& color) const = 0;
0145     /** default implementation sets byte width to one digit width */
0146     virtual void recalcByteWidth();
0147 
0148 protected:
0149     void renderPlain(QPainter* painter, const LinePositionRange& linePositions, Address byteIndex);
0150     void renderSelection(QPainter* painter, const LinePositionRange& linePositions, Address byteIndex, int flag);
0151     void renderMarking(QPainter* painter, const LinePositionRange& linePositions, Address byteIndex, int flag);
0152     void renderRange(QPainter* painter, const QBrush& brush, const LinePositionRange& linePositions, int flag);
0153     void renderSelectionSpaceBehind(QPainter* painter, LinePosition linePosition);
0154     void renderSpaceBehind(QPainter* painter, const QBrush& brush, LinePosition linePosition);
0155     void renderBookmark(QPainter* painter, const QBrush& brush);
0156 
0157     void recalcX();
0158 
0159     bool getNextSelectedAddressRange(AddressRange* selectedRange, unsigned int* flag, const AddressRange& range) const;
0160     bool getNextMarkedAddressRange(AddressRange* markedRange, unsigned int* flag, const AddressRange& range) const;
0161 
0162     void setByteWidth(int byteWidth);
0163 
0164 protected:
0165     /** pointer to the buffer */
0166     AbstractByteArrayModel* mByteArrayModel;
0167     /** pointer to the layout */
0168     const ByteArrayTableLayout* mLayout;
0169     /** pointer to the ranges */
0170     ByteArrayTableRanges* mRanges;
0171     /** */
0172     Bookmarkable* mBookmarks;
0173     /** */
0174     const CharCodec* mCharCodec;
0175 
0176     /** */
0177     PixelX mDigitWidth = 0;
0178     /** */
0179     PixelY mDigitBaseLine = 0;
0180 
0181     QFontMetrics mFontMetrics;
0182 
0183 protected: // individual data
0184     /** total width of byte display in pixel */
0185     PixelX mByteWidth = 0;
0186     /** width of inserting cursor in pixel */
0187 //     PixelX mCursorWidth;
0188     /** size of the line margin */
0189     PixelX mByteSpacingWidth;
0190     /** width of spacing in pixel */
0191     PixelX mGroupSpacingWidth;
0192 
0193     /** number of grouped bytes */
0194     int mNoOfGroupedBytes;
0195 
0196     /** pointer to array with buffered linePositions (relative to column position)
0197      * a spacing gets assigned to the left byte -> ...c|c|c |c|c...
0198      */
0199     PixelX* mLinePosLeftPixelX = nullptr;
0200     PixelX* mLinePosRightPixelX = nullptr;
0201     /** index of right position */
0202     LinePosition mLastLinePos = 0;
0203 
0204     /** */
0205     bool mByteTypeColored = true;
0206 
0207 protected: // buffering rendering data
0208     LinePositionRange mRenderLinePositions;
0209     Line mRenderLine;
0210     PixelX mRenderX;
0211     PixelX mRenderWidth;
0212 
0213 protected:
0214     AbstractByteArrayColumnRenderer* const q_ptr;
0215 
0216 private:
0217     Q_DECLARE_PUBLIC(AbstractByteArrayColumnRenderer)
0218 };
0219 
0220 inline AbstractByteArrayColumnRendererPrivate::AbstractByteArrayColumnRendererPrivate(AbstractByteArrayColumnRenderer* q,
0221                                                                                       AbstractColumnStylist* stylist,
0222                                                                                       AbstractByteArrayModel* byteArrayModel,
0223                                                                                       ByteArrayTableLayout* layout,
0224                                                                                       ByteArrayTableRanges* ranges)
0225     : AbstractColumnRendererPrivate(stylist)
0226     , mByteArrayModel(byteArrayModel)
0227     , mLayout(layout)
0228     , mRanges(ranges)
0229     , mBookmarks(qobject_cast<Bookmarkable*>(byteArrayModel))
0230     , mFontMetrics(QFont())
0231     , mByteSpacingWidth(DefaultByteSpacingWidth)
0232     , mGroupSpacingWidth(DefaultGroupSpacingWidth)
0233     , mNoOfGroupedBytes(DefaultNoOfGroupedBytes)
0234     , q_ptr(q)
0235 {
0236 }
0237 
0238 inline AbstractByteArrayColumnRendererPrivate::~AbstractByteArrayColumnRendererPrivate()
0239 {
0240     delete [] mLinePosLeftPixelX;
0241     delete [] mLinePosRightPixelX;
0242 }
0243 
0244 inline PixelX AbstractByteArrayColumnRendererPrivate::byteWidth()         const { return mByteWidth; }
0245 inline PixelX AbstractByteArrayColumnRendererPrivate::digitWidth()        const { return mDigitWidth; }
0246 inline PixelX AbstractByteArrayColumnRendererPrivate::byteSpacingWidth()  const { return mByteSpacingWidth; }
0247 inline PixelX AbstractByteArrayColumnRendererPrivate::groupSpacingWidth() const { return mGroupSpacingWidth; }
0248 
0249 inline int AbstractByteArrayColumnRendererPrivate::noOfGroupedBytes()      const { return mNoOfGroupedBytes; }
0250 
0251 inline LinePosition AbstractByteArrayColumnRendererPrivate::firstLinePos() const { return mRenderLinePositions.start(); }
0252 inline LinePosition AbstractByteArrayColumnRendererPrivate::lastLinePos()  const { return mRenderLinePositions.end(); }
0253 inline LinePositionRange AbstractByteArrayColumnRendererPrivate::visibleLinePositions() const { return mRenderLinePositions; }
0254 
0255 inline const ByteArrayTableLayout* AbstractByteArrayColumnRendererPrivate::layout() const { return mLayout; }
0256 
0257 inline void AbstractByteArrayColumnRendererPrivate::setCharCodec(const CharCodec* charCodec)
0258 {
0259     mCharCodec = charCodec;
0260 }
0261 
0262 inline void AbstractByteArrayColumnRendererPrivate::setByteTypeColored(bool byteTypeColored)
0263 {
0264     mByteTypeColored = byteTypeColored;
0265 }
0266 inline bool AbstractByteArrayColumnRendererPrivate::isByteTypeColored() const { return mByteTypeColored; }
0267 
0268 inline void AbstractByteArrayColumnRendererPrivate::setByteWidth(int byteWidth) { mByteWidth = byteWidth; }
0269 
0270 }
0271 
0272 #endif