Commit 9413bc09 authored by Rafał Rzepecki's avatar Rafał Rzepecki

Show binary data

parent 711ea55d
......@@ -3,6 +3,7 @@ set(kwalletmanager_SRCS
kwalletmanager.cpp
kwalletmanagerwidget.cpp
kwalletmanagerwidgetitem.cpp
kwhexview.cpp
walletcontrolwidget.cpp
main.cpp
kwalletpopup.cpp
......
......@@ -137,6 +137,7 @@ KWalletEditor::KWalletEditor(QWidget *parent, const char *name)
connect(_showContents, &QToolButton::clicked, this, &KWalletEditor::showPasswordContents);
connect(_hideContents, &QToolButton::clicked, this, &KWalletEditor::hidePasswordContents);
_binaryViewShow->setChecked(_alwaysShowContents);
// createActions();
// TODO: remove kwalleteditor.rc file
}
......@@ -558,6 +559,7 @@ void KWalletEditor::entrySelectionChanged(QTreeWidgetItem *item)
item->text(0)));
_saveChanges->setEnabled(false);
_undoChanges->setEnabled(false);
_binaryView->setData(ba);
}
}
break;
......
/*
* Copyright 2016 Rafał Rzepecki <divided.mind@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "kwhexview.h"
#include <qtextstream.h>
#include <QTextCodec>
KWHexView::KWHexView(QWidget* parent): QPlainTextEdit(parent)
{
setReadOnly(true);
setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));
setWordWrapMode(QTextOption::NoWrap);
}
enum { hexStride = 4 };
template<class It>
static QString toHex(It it, It end)
{
QString text;
QTextStream ts(&text);
ts << hex << qSetFieldWidth(2) << qSetPadChar('0');
while (it < end) {
const auto sEnd = qMin(it + hexStride, end);
while (it < sEnd)
ts << static_cast<quint8>(*(it++));
ts << qSetFieldWidth(0) << " " << qSetFieldWidth(2);
}
return text;
}
template<class It>
static QString toText(It begin, It end)
{
QString text = QString::fromLatin1(begin, end - begin);
for (auto &ch: text)
if (!ch.isPrint()) ch = '.';
return text;
}
int KWHexView::calculateStride()
{
const auto w = viewport()->width();
const auto em = fontMetrics().averageCharWidth();
const auto chars = w / em - 1;
auto stride = chars / 3 / hexStride * hexStride;
while (stride * 3 + (stride / hexStride) + 1 > chars)
stride -= hexStride;
return qMax(static_cast<int>(hexStride), stride);
}
void KWHexView::setData(const QByteArray& ba)
{
data = ba;
showData();
}
void KWHexView::showData()
{
QString text;
QTextStream ts(&text);
ts << left;
const auto stride = calculateStride();
const auto hexwidth = stride * 2 + (stride / hexStride) + 1;
for (auto it = data.begin(); it < data.end(); it += stride) {
auto end = qMin(it + stride, data.end());
ts << qSetFieldWidth(hexwidth) << toHex(it, end);
ts << qSetFieldWidth(0) << toText(it, end) << endl;
}
setPlainText(text);
}
void KWHexView::resizeEvent(QResizeEvent* e)
{
QPlainTextEdit::resizeEvent(e);
if (e->size() != e->oldSize()) showData();
}
/*
* Copyright 2016 Rafał Rzepecki <divided.mind@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef KWHEXVIEW_H
#define KWHEXVIEW_H
#include <QPlainTextEdit>
class KWHexView : public QPlainTextEdit
{
Q_OBJECT
public:
explicit KWHexView(QWidget* parent = nullptr);
void setData(const QByteArray &ba);
protected:
void resizeEvent(QResizeEvent* e) override;
private:
QByteArray data;
int calculateStride();
void showData();
};
#endif // KWHEXVIEW_H
......@@ -182,73 +182,43 @@
</layout>
</widget>
<widget class="QWidget" name="page3"/>
<widget class="QWidget" name="page4">
<widget class="QWidget" name="binaryPage">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
<widget class="QCheckBox" name="_binaryViewShow">
<property name="text">
<string>Show &amp;Contents</string>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
<property name="checked">
<bool>true</bool>
</property>
</spacer>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QLabel" name="textLabel1_2">
<property name="text">
<string>This is a binary data entry. It cannot be edited as its format is unknown and application specific.</string>
</property>
<property name="alignment">
<set>Qt::AlignVCenter</set>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
<widget class="KWHexView" name="_binaryView">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>1</verstretch>
</sizepolicy>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer_2">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Expanding</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
<height>0</height>
</size>
</property>
</spacer>
......@@ -356,10 +326,32 @@
<extends>QLabel</extends>
<header>ksqueezedtextlabel.h</header>
</customwidget>
<customwidget>
<class>KWHexView</class>
<extends>QPlainTextEdit</extends>
<header>kwhexview.h</header>
</customwidget>
</customwidgets>
<includes>
<include location="global">kiconloader.h</include>
</includes>
<resources/>
<connections/>
<connections>
<connection>
<sender>_binaryViewShow</sender>
<signal>toggled(bool)</signal>
<receiver>_binaryView</receiver>
<slot>setVisible(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>224</x>
<y>83</y>
</hint>
<hint type="destinationlabel">
<x>237</x>
<y>127</y>
</hint>
</hints>
</connection>
</connections>
</ui>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment