Skip to content

Commit

Permalink
Improve rendering of large values in ValueEditor (#3675)
Browse files Browse the repository at this point in the history
Fix issues: #3641, #3625
Partially fix: #3599
  • Loading branch information
uglide authored Aug 17, 2016
1 parent a686e60 commit 80d9416
Show file tree
Hide file tree
Showing 20 changed files with 128 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ int main(int argc, char *argv[])
QApplication::setOrganizationDomain("redisdesktop.com");
QApplication::setOrganizationName("redisdesktop");

QFontDatabase::addApplicationFont("://fonts/OpenSans-Regular.ttf");
QFontDatabase::addApplicationFont("://fonts/OpenSans.ttc");
QFont defaultFont("OpenSans", 10);
QApplication::setFont(defaultFont);

Expand Down
4 changes: 4 additions & 0 deletions src/rdm.pro
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ win32 {
CONFIG += c++11
RC_FILE += $$PWD/resources/rdm.rc

win32-msvc* {
QMAKE_LFLAGS += /LARGEADDRESSAWARE
}

release: DESTDIR = ./../bin/windows/release
debug: DESTDIR = ./../bin/windows/debug
}
Expand Down
3 changes: 2 additions & 1 deletion src/resources/fonts.qrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<RCC>
<qresource prefix="/">
<file>fonts/OpenSans-Regular.ttf</file>
<file>fonts/OpenSans.ttc</file>
<file>fonts/Inconsolata-Regular.ttf</file>
</qresource>
</RCC>
Binary file added src/resources/fonts/Inconsolata-Regular.ttf
Binary file not shown.
Binary file removed src/resources/fonts/OpenSans-Bold.ttf
Binary file not shown.
Binary file removed src/resources/fonts/OpenSans-BoldItalic.ttf
Binary file not shown.
Binary file removed src/resources/fonts/OpenSans-ExtraBold.ttf
Binary file not shown.
Binary file removed src/resources/fonts/OpenSans-ExtraBoldItalic.ttf
Binary file not shown.
Binary file removed src/resources/fonts/OpenSans-Italic.ttf
Binary file not shown.
Binary file removed src/resources/fonts/OpenSans-Light.ttf
Binary file not shown.
Binary file removed src/resources/fonts/OpenSans-LightItalic.ttf
Binary file not shown.
Binary file removed src/resources/fonts/OpenSans-Regular.ttf
Binary file not shown.
Binary file removed src/resources/fonts/OpenSans-Semibold.ttf
Binary file not shown.
Binary file removed src/resources/fonts/OpenSans-SemiboldItalic.ttf
Binary file not shown.
Binary file added src/resources/fonts/OpenSans.ttc
Binary file not shown.
28 changes: 15 additions & 13 deletions src/resources/qml/editors/MultilineEditor.qml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import QtQuick 2.0
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.2
import QtQuick.Layouts 1.1

import "./formatters/formatters.js" as Formatters
Expand Down Expand Up @@ -83,25 +84,26 @@ ColumnLayout
console.log("Text editor was disabled")
}

text: {
if (!formatter) return ''
var val
if (formatter.binary === true)
val = formatter.getFormatted(binaryUtils.valueToBinary(value))
else
val = formatter.getFormatted(binaryUtils.toUtf(value))

if (val === undefined) {
formatterSelector.currentIndex = 0
binaryFlag.visible = false
}
text: {
if (!formatter || !value)
return ''

return (val === undefined) ? '' : val
if (formatter.binary === true) {
return formatter.getFormatted(binaryUtils.valueToBinary(value)) || ''
} else {
return formatter.getFormatted(binaryUtils.toUtf(value)) || ''
}
}

property var formatter: {
var index = formatterSelector.currentIndex ? formatterSelector.currentIndex : Formatters.defaultFormatterIndex
return Formatters.enabledFormatters[index]
}

style: TextAreaStyle {
renderType: Text.QtRendering
}
font { family: monospacedFont.name }
wrapMode: TextEdit.WrapAnywhere
}
}
17 changes: 9 additions & 8 deletions src/resources/qml/editors/formatters/formatters.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.import "./msgpack.js" as MsgPack
.import "./hexy.js" as Hexy
.import "./php-unserialize.js" as PHPUnserialize
.import "./json-tools.js" as JSONFormatter

/**
Plain formatter
Expand Down Expand Up @@ -74,11 +75,8 @@ var json = {
htmlOutput: false,

getFormatted: function (raw) {

try {
var parsed = JSON.parse(raw)
return JSON.stringify(parsed, undefined, 4)

try {
return JSONFormatter.prettyPrint(raw)
} catch (e) {
return "Error: Invalid JSON"
}
Expand All @@ -95,9 +93,7 @@ var json = {

getRaw: function (formatted) {
try {
var parsed = JSON.parse(formatted)
return JSON.stringify(parsed)

return JSONFormatter.minify(formatted)
} catch (e) {
return formatted
}
Expand Down Expand Up @@ -180,6 +176,11 @@ var enabledFormatters = [plain, json, msgpack, hex, hexTable, phpserialized]

function guessFormatter(isBinary, value)
{
// NOTE(u_glide): Use hex or plain formatter if value is large
if (binaryUtils.binaryStringLength(value) > 100000) {
return isBinary? 3 : 0
}

var tryFormatters = isBinary? [2, 5, 3, 4] : [1, 5, 2]

for (var index in tryFormatters) {
Expand Down
88 changes: 88 additions & 0 deletions src/resources/qml/editors/formatters/json-tools.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
Based on:
json-format v.1.1
http://github.com/phoboslab/json-format
Released under MIT license:
http://www.opensource.org/licenses/mit-license.php
*/

var prettyPrint = function( json ) {
var p = []
var out = ""
var indent = 0;

var push = function( m ) { return '\\' + p.push( m ) + '\\'; }
var pop = function( m, i ) { return p[i-1] }
var tabs = function( count ) { return new Array( count + 1 ).join( ' ' ); }

// Extract backslashes and strings
json = json
.replace( /\\./g, push )
.replace( /(".*?"|'.*?')/g, push )
.replace( /\s+/, '' );

// Indent and insert newlines
for( var i = 0; i < json.length; i++ ) {
var c = json.charAt(i);

switch(c) {
case '{':
case '[':
out += c + "\n" + tabs(++indent);
break;
case '}':
case ']':
out += "\n" + tabs(--indent) + c;
break;
case ',':
out += ",\n" + tabs(indent);
break;
case ':':
out += ": ";
break;
default:
out += c;
break;
}
}

// Strip whitespace from numeric arrays and put backslashes
// and strings back in
out = out
.replace( /\[[\d,\s]+?\]/g, function(m){ return m.replace(/\s/g,''); } )
.replace( /\\(\d+)\\/g, pop ) // strings
.replace( /\\(\d+)\\/g, pop ); // backslashes in strings

return out;
};


/*!
Based on:
* pretty-data - nodejs plugin to pretty-print or minify data in XML, JSON and CSS formats.
*
* Version - 0.40.0
* Copyright (c) 2012 Vadim Kiryukhin
* vkiryukhin @ gmail.com
* http://www.eslinstructor.net/pretty-data/
* Dual licensed under the MIT and GPL licenses
*/
var minify = function(json) {

return json.replace(/\s{0,}\{\s{0,}/g,"{")
.replace(/\s{0,}\[$/g,"[")
.replace(/\[\s{0,}/g,"[")
.replace(/:\s{0,}\[/g,':[')
.replace(/\s{0,}\}\s{0,}/g,"}")
.replace(/\s{0,}\]\s{0,}/g,"]")
.replace(/\"\s{0,}\,/g,'",')
.replace(/\,\s{0,}\"/g,',"')
.replace(/\"\s{0,}:/g,'":')
.replace(/:\s{0,}\"/g,':"')
.replace(/:\s{0,}\[/g,':[')
.replace(/\,\s{0,}\[/g,',[')
.replace(/\,\s{2,}/g,', ')
.replace(/\]\s{0,},\s{0,}\[/g,'],[');
};

8 changes: 8 additions & 0 deletions src/resources/qml/value-editor.qml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ Rectangle {
color: "transparent"
property var currentValueFormatter

FontLoader {
id: monospacedFont
Component.onCompleted: {
source = "qrc:/fonts/Inconsolata-Regular.ttf"
}
}


TabView {
id: tabs
objectName: "rdm_qml_tabs"
Expand Down
1 change: 1 addition & 0 deletions src/resources/rdm.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@
<file>qml/parts/RichTextWithLinks.qml</file>
<file>qml/3rdparty/php-unserialize-js/phpUnserialize.js</file>
<file>qml/bulk-operations/BulkOperationsDialog.qml</file>
<file>qml/editors/formatters/json-tools.js</file>
</qresource>
</RCC>

0 comments on commit 80d9416

Please sign in to comment.