Пример #1
0
void QHexEditPrivate::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);

    // draw some patterns if needed
    painter.fillRect(event->rect(), this->palette().color(QPalette::Base));
    if (_addressArea)
        painter.fillRect(QRect(_xPosAdr, event->rect().top(), _xPosHex - GAP_ADR_HEX + 2, height()), _addressAreaColor);
    if (_asciiArea)
    {
        int linePos = _xPosAscii - (GAP_HEX_ASCII / 2);
        painter.setPen(Qt::gray);
        painter.drawLine(linePos, event->rect().top(), linePos, height());
    }

    painter.setPen(this->palette().color(QPalette::WindowText));

    // calc position
    int firstLineIdx = ((event->rect().top()/ _charHeight) - _charHeight) * BYTES_PER_LINE;
    if (firstLineIdx < 0)
        firstLineIdx = 0;
    int lastLineIdx = ((event->rect().bottom() / _charHeight) + _charHeight) * BYTES_PER_LINE;
    if (lastLineIdx > _data.size())
        lastLineIdx = _data.size();
    int yPosStart = ((firstLineIdx) / BYTES_PER_LINE) * _charHeight + _charHeight;

    // paint address area
    if (_addressArea)
    {
        for (int lineIdx = firstLineIdx, yPos = yPosStart; lineIdx < lastLineIdx; lineIdx += BYTES_PER_LINE, yPos +=_charHeight)
        {
            QString address = QString("%1")
                              .arg(lineIdx + _addressOffset, _realAddressNumbers, 16, QChar('0'));
//            address.insert(address.length()-4,':');
            painter.drawText(_xPosAdr, yPos, address.toUpper());
        }
    }

    // paint hex area
    QByteArray hexBa(_data.mid(firstLineIdx, lastLineIdx - firstLineIdx + 1).toHex());
    QBrush highLighted = QBrush(_highlightingColor);
    QPen colHighlighted = QPen(this->palette().color(QPalette::WindowText));
    QBrush selected = QBrush(_selectionColor);
    QPen colSelected = QPen(Qt::white);
    QPen colStandard = QPen(this->palette().color(QPalette::WindowText));

    painter.setBackgroundMode(Qt::TransparentMode);

    for (int lineIdx = firstLineIdx, yPos = yPosStart; lineIdx < lastLineIdx; lineIdx += BYTES_PER_LINE, yPos +=_charHeight)
    {
        QByteArray hex;
        int xPos = _xPosHex;

        QString hl_block;
        int hl_block_i;

        if (_highlighting)
        {
            painter.setPen(Qt::yellow);
            hl_block_i=0;
            hl_block="██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██";


               for (int colIdx = 0; ((lineIdx + colIdx) < _data.size() and (colIdx < BYTES_PER_LINE)); colIdx++)
                   {
                       int posBa = lineIdx + colIdx;

                           if (_changedData[posBa])
                           {

                               hl_block[hl_block_i] = QChar(' ');
                               hl_block[hl_block_i+1] = QChar(' ');
                           }

                           hl_block_i=hl_block_i+3;

                   }
               if(hl_block_i!=0)
               {
               while(hl_block_i<hl_block.size())
               {
                   hl_block[hl_block_i] = QChar(' ');
                   hl_block_i++;
               }

               painter.drawText(xPos, yPos, hl_block);
           }
        }

        if (_exteralHl.size()!=0)
        {
            painter.setPen(Qt::green);
            hl_block_i=0;
            hl_block="██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██";


               for (int colIdx = 0; ((lineIdx + colIdx) < _data.size() and (colIdx < BYTES_PER_LINE)); colIdx++)
                   {
                       int posBa = lineIdx + colIdx;

                           if (_exteralHl[posBa]=='\0')
                           {

                               hl_block[hl_block_i] = QChar(' ');
                               hl_block[hl_block_i+1] = QChar(' ');
                           }

                           hl_block_i=hl_block_i+3;

                   }
               if(hl_block_i!=0)
               {
               while(hl_block_i<hl_block.size())
               {
                   hl_block[hl_block_i] = QChar(' ');
                   hl_block_i++;
               }

               painter.drawText(xPos, yPos, hl_block);
           }
        }

//        if (_selected)
            painter.setPen(Qt::cyan);
            hl_block_i=0;
            hl_block="██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██";

               for (int colIdx = 0; ((lineIdx + colIdx) < _data.size() and (colIdx < BYTES_PER_LINE)); colIdx++)
                   {
                       int posBa = lineIdx + colIdx;

                           if (!((getSelectionBegin() <= posBa) && (getSelectionEnd() > posBa)))
                           {

                               hl_block[hl_block_i] = QChar(' ');
                               hl_block[hl_block_i+1] = QChar(' ');
                           }

                           hl_block_i=hl_block_i+3;

                   }

               if(hl_block_i!=0)
               {
               while(hl_block_i<hl_block.size())
               {
                   hl_block[hl_block_i] = QChar(' ');
                   hl_block_i++;
               }

               painter.drawText(xPos, yPos, hl_block);
               }


        painter.setPen(this->palette().color(QPalette::WindowText));

        hex = hexBa.mid((lineIdx - firstLineIdx) * 2, BYTES_PER_LINE * 2);

        QString new_hex="                                                "; //BYTES_PER_LINE*3

        int i_new_hex = 0;

        for(int i_hex=0;i_hex<hex.size();i_hex++,i_new_hex++)
        {
             new_hex[i_new_hex] = hex[i_hex];
             if((i_hex+1)%2 == 0)i_new_hex++;
        }

        painter.drawText(xPos, yPos, new_hex.toUpper());
    }
    painter.setBackgroundMode(Qt::TransparentMode);
    painter.setPen(this->palette().color(QPalette::WindowText));

    // paint ascii area
    if (_asciiArea)
    {
        for (int lineIdx = firstLineIdx, yPos = yPosStart; lineIdx < lastLineIdx; lineIdx += BYTES_PER_LINE, yPos +=_charHeight)
        {
            QByteArray ascii = _data.mid(lineIdx, BYTES_PER_LINE);
            for (int idx=0; idx < ascii.size(); idx++)
                if (((char)ascii[idx] < 0x20) or ((char)ascii[idx] > 0x7e))
                    ascii[idx] = '.';
            painter.drawText(_xPosAscii, yPos, ascii);
        }
    }

    // paint cursor
    if (_blink)
    {
        if (_overwriteMode)
            painter.fillRect(_cursorX, _cursorY + _charHeight - 2, _charWidth, 2, this->palette().color(QPalette::WindowText));
        else
            painter.fillRect(_cursorX, _cursorY, 2, _charHeight, this->palette().color(QPalette::WindowText));
    }

    if (_size != _data.size())
    {
        _size = _data.size();
        emit currentSizeChanged(_size);
    }


    QString adr; adr.setNum(_cursorPosition/2,16); adr = adr.toUpper();
    while(adr.length()<8)
    {
        adr.prepend("0");
    }

    QString val; val.setNum(_data[_cursorPosition/2],16); val = val.toUpper();
    if(val.length()<2)val.prepend("0");
    val = val.mid(val.length()-2,2);

    QString bval; bval.setNum(_data[_cursorPosition/2],2);
    while(bval.length()<8)bval.prepend("0");

    if(bval.length()>8)
    {
        bval = bval.mid(bval.length()-8,8);
    }

    emit updateText("Address: "+adr+" Value: "+val+" Binary: "+bval);
}
Пример #2
0
void QHexEditPrivate::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);

    // draw some patterns if needed
    painter.fillRect(event->rect(), this->palette().color(QPalette::Base));
    if (_addressArea)
        painter.fillRect(QRect(_xPosAdr, event->rect().top(), _xPosHex - GAP_ADR_HEX + 2, height()), _addressAreaColor);
    if (_asciiArea)
    {
        int linePos = _xPosAscii - (GAP_HEX_ASCII / 2);
        painter.setPen(Qt::gray);
        painter.drawLine(linePos, event->rect().top(), linePos, height());
    }

    painter.setPen(this->palette().color(QPalette::WindowText));

    // calc position
    int firstLineIdx = ((event->rect().top()/ _charHeight) - _charHeight) * BYTES_PER_LINE;
    if (firstLineIdx < 0)
        firstLineIdx = 0;
    int lastLineIdx = ((event->rect().bottom() / _charHeight) + _charHeight) * BYTES_PER_LINE;
    if (lastLineIdx > _xData.size())
        lastLineIdx = _xData.size();
    int yPosStart = ((firstLineIdx) / BYTES_PER_LINE) * _charHeight + _charHeight;

    // paint address area
    if (_addressArea)
    {
        for (int lineIdx = firstLineIdx, yPos = yPosStart; lineIdx < lastLineIdx; lineIdx += BYTES_PER_LINE, yPos +=_charHeight)
        {
            QString address = QString("%1")
                              .arg(lineIdx + _xData.addressOffset(), _xData.realAddressNumbers(), 16, QChar('0'));
            painter.drawText(_xPosAdr, yPos, address);
        }
    }

    // paint hex area
    QByteArray hexBa(_xData.data().mid(firstLineIdx, lastLineIdx - firstLineIdx + 1).toHex());
    QBrush highLighted = QBrush(_highlightingColor);
    QPen colHighlighted = QPen(this->palette().color(QPalette::WindowText));
    QBrush selected = QBrush(_selectionColor);
    QPen colSelected = QPen(Qt::white);
    QPen colStandard = QPen(this->palette().color(QPalette::WindowText));

    painter.setBackgroundMode(Qt::TransparentMode);

    for (int lineIdx = firstLineIdx, yPos = yPosStart; lineIdx < lastLineIdx; lineIdx += BYTES_PER_LINE, yPos +=_charHeight)
    {
        QByteArray hex;
        int xPos = _xPosHex;
        for (int colIdx = 0; ((lineIdx + colIdx) < _xData.size() && (colIdx < BYTES_PER_LINE)); colIdx++)
        {
            int posBa = lineIdx + colIdx;
            if ((getSelectionBegin() <= posBa) && (getSelectionEnd() > posBa))
            {
                painter.setBackground(selected);
                painter.setBackgroundMode(Qt::OpaqueMode);
                painter.setPen(colSelected);
            }
            else
            {
                if (_highlighting)
                {
                    // hilight diff bytes
                    painter.setBackground(highLighted);
                    if (_xData.dataChanged(posBa))
                    {
                        painter.setPen(colHighlighted);
                        painter.setBackgroundMode(Qt::OpaqueMode);
                    }
                    else
                    {
                        painter.setPen(colStandard);
                        painter.setBackgroundMode(Qt::TransparentMode);
                    }
                }
            }

            // render hex value
            if (colIdx == 0)
            {
                hex = hexBa.mid((lineIdx - firstLineIdx) * 2, 2);
                painter.drawText(xPos, yPos, hex.toUpper());
                xPos += 2 * _charWidth;
            } else {
                hex = hexBa.mid((lineIdx + colIdx - firstLineIdx) * 2, 2).prepend(" ");
                painter.drawText(xPos, yPos, hex.toUpper());
                xPos += 3 * _charWidth;
            }

        }
    }
    painter.setBackgroundMode(Qt::TransparentMode);
    painter.setPen(this->palette().color(QPalette::WindowText));

    // paint ascii area
    if (_asciiArea)
    {
        for (int lineIdx = firstLineIdx, yPos = yPosStart; lineIdx < lastLineIdx; lineIdx += BYTES_PER_LINE, yPos +=_charHeight)
        {
            int xPosAscii = _xPosAscii;
            for (int colIdx = 0; ((lineIdx + colIdx) < _xData.size() && (colIdx < BYTES_PER_LINE)); colIdx++)
            {
                painter.drawText(xPosAscii, yPos, _xData.asciiChar(lineIdx + colIdx));
                xPosAscii += _charWidth;
            }
        }
    }

    // paint cursor
    if (_blink && !_readOnly && hasFocus())
    {
        if (_overwriteMode)
            painter.fillRect(_cursorX, _cursorY + _charHeight - 2, _charWidth, 2, this->palette().color(QPalette::WindowText));
        else
            painter.fillRect(_cursorX, _cursorY, 2, _charHeight, this->palette().color(QPalette::WindowText));
    }

    if (_size != _xData.size())
    {
        _size = _xData.size();
        emit currentSizeChanged(_size);
    }
}
Пример #3
0
void QHexEditPrivate::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);

    // draw some patterns if needed
    painter.fillRect(event->rect(), this->palette().color(QPalette::Base));
    if (_addressArea)
        painter.fillRect(QRect(_xPosAdr, event->rect().top(), _xPosHex - GAP_ADR_HEX + 2, height()), _addressAreaColor);
    if (_asciiArea)
    {
        int linePos = _xPosAscii - (GAP_HEX_ASCII / 2);
        painter.setPen(Qt::gray);
        painter.drawLine(linePos, event->rect().top(), linePos, height());
    }

    painter.setPen(this->palette().color(QPalette::WindowText));

    // calc position
    int firstLineIdx = ((event->rect().top()/ _charHeight) - _charHeight) * BYTES_PER_LINE;
    if (firstLineIdx < 0)
        firstLineIdx = 0;
    int lastLineIdx = ((event->rect().bottom() / _charHeight) + _charHeight) * BYTES_PER_LINE;
    if (lastLineIdx > _data.size())
        lastLineIdx = _data.size();
    int yPosStart = ((firstLineIdx) / BYTES_PER_LINE) * _charHeight + _charHeight;

    // paint address area
    if (_addressArea)
    {
        for (int lineIdx = firstLineIdx, yPos = yPosStart; lineIdx < lastLineIdx; lineIdx += BYTES_PER_LINE, yPos +=_charHeight)
        {
            QString address = QString("%1")
                              .arg(lineIdx + _addressOffset, _realAddressNumbers, 16, QChar('0'));
            painter.drawText(_xPosAdr, yPos, address);
        }
    }

    // paint hex area
    QByteArray hexBa(_data.mid(firstLineIdx, lastLineIdx - firstLineIdx + 1).toHex());
    QBrush highLighted = QBrush(_highlightingColor);
    painter.setBackground(highLighted);
    painter.setBackgroundMode(Qt::TransparentMode);
    for (int lineIdx = firstLineIdx, yPos = yPosStart; lineIdx < lastLineIdx; lineIdx += BYTES_PER_LINE, yPos +=_charHeight)
    {
        QByteArray hex;
        int xPos = _xPosHex;
        for (int colIdx = 0; ((lineIdx + colIdx) < _data.size() and (colIdx < BYTES_PER_LINE)); colIdx++)
        {
            // hilight diff bytes
            if (_highlighting)
            {
                int posBa = lineIdx + colIdx;
                if (posBa >= _originalData.size())
                    painter.setBackgroundMode(Qt::TransparentMode);
                else
                    if (_data[posBa] == _originalData[posBa])
                        painter.setBackgroundMode(Qt::TransparentMode);
                    else
                        painter.setBackgroundMode(Qt::OpaqueMode);
            }

            // render hex value
            if (colIdx == 0)
            {
                hex = hexBa.mid((lineIdx - firstLineIdx) * 2, 2);
                painter.drawText(xPos, yPos, hex);
                xPos += 2 * _charWidth;
            } else {
                hex = hexBa.mid((lineIdx + colIdx - firstLineIdx) * 2, 2).prepend(" ");
                painter.drawText(xPos, yPos, hex);
                xPos += 3 * _charWidth;
            }
        }
    }
    painter.setBackgroundMode(Qt::TransparentMode);

    // paint ascii area
    if (_asciiArea)
    {
        for (int lineIdx = firstLineIdx, yPos = yPosStart; lineIdx < lastLineIdx; lineIdx += BYTES_PER_LINE, yPos +=_charHeight)
        {
            QByteArray ascii = _data.mid(lineIdx, BYTES_PER_LINE);
            for (int idx=0; idx < ascii.size(); idx++)
                if (((char)ascii[idx] < 0x20) or ((char)ascii[idx] > 0x7e))
                    ascii[idx] = '.';
            painter.drawText(_xPosAscii, yPos, ascii);
        }
    }

    // paint cursor
    if (_blink)
    {
        if (_overwriteMode)
            painter.fillRect(_cursorX, _cursorY + _charHeight - 2, _charWidth, 2, this->palette().color(QPalette::WindowText));
        else
            painter.fillRect(_cursorX, _cursorY, 2, _charHeight, this->palette().color(QPalette::WindowText));
    }

    if (_size != _data.size())
    {
        _size = _data.size();
        emit currentSizeChanged(_size);
    }
}