Skip to content

Commit

Permalink
Hunkview key handling for listview
Browse files Browse the repository at this point in the history
Also switched to QtRendering, which looks nicer (at least at +dpi modes), by adding a TextElement thats a TextEdit for easy selection possiblity.
  • Loading branch information
stofte committed Jan 3, 2019
1 parent 355c13d commit 9d7c5a3
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 48 deletions.
1 change: 1 addition & 0 deletions app/qml.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
<file>qml/components/DiffStatusIcon.qml</file>
<file>qml/base/TypedArrayListModel.qml</file>
<file>qml/components/CustomScrollBar.qml</file>
<file>qml/base/TextElement.qml</file>
</qresource>
</RCC>
13 changes: 13 additions & 0 deletions app/qml/base/TextElement.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import QtQuick 2.11
import "../style"

TextEdit {
property bool selectableText: false
// seems that if text elements overlap, renderType switches to QtRendering?
// eg, seen in hunkview linenumber listings
renderType: Text.QtRendering
font.pointSize: Style.fontPointSize
font.family: Style.fontName
readOnly: true
selectByMouse: selectableText
}
30 changes: 30 additions & 0 deletions app/qml/components/CustomScrollBar.qml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ ScrollBar {
property real scrollContainerSize: 0
property real scrollContainerSizePrev: 0
property real scrollContentSize: 0
property real pageScrollOverlapSize: 35
property real pageScrollStepSize: (scrollContainerSize - pageScrollOverlapSize) / scrollContentSize
// scrollTarget is assumed to point to the content being scrolled,
// so we can detect when it changes, to reset the scroll position.
property variant scrollTarget
Expand Down Expand Up @@ -43,6 +45,34 @@ ScrollBar {
}
}
}
// Use `Keys.forwardTo: [scrollRef]` to send keys to the scrollbar to handle
// tradtional keyboard keys for list scrolling/paging
Keys.onPressed: {
if (!root.visible || !root.enabled) return;
var oldStepSize;
if (event.key === Qt.Key_Down) {
root.increase();
} else if (event.key === Qt.Key_Up) {
root.decrease();
} else if (event.key === Qt.Key_PageDown) {
oldStepSize = root.stepSize;
root.stepSize = pageScrollStepSize;
root.increase();
root.stepSize = oldStepSize;
} else if (event.key === Qt.Key_PageUp) {
oldStepSize = root.stepSize;
root.stepSize = pageScrollStepSize;
root.decrease();
root.stepSize = oldStepSize;
} else if (event.key === Qt.Key_Home) {
root.position = 0;
} else if (event.key === Qt.Key_End) {
oldStepSize = root.stepSize;
root.stepSize = 1;
root.increase();
root.stepSize = oldStepSize;
}
}
onScrollTargetChanged: {
position = 0;
}
Expand Down
89 changes: 41 additions & 48 deletions app/qml/components/HunkView.qml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import QtQuick 2.9
import QtQuick 2.11
import QtQuick.Controls 2.2
import QtQuick.Layouts 1.3
import QtQuick.Controls 1.4 as QQC14
Expand Down Expand Up @@ -35,32 +35,28 @@ Rectangle {
anchors.left: parent.left
clip: true
color: "transparent"

DiffStatusIcon {
y: 1
x: 0
statusValue: headerRectRef.isComparison ? "Deleted" : statusText
iconSize: 13
}

DiffStatusIcon {
x: 0
y: fnTopRef.y + fnTopRef.contentHeight
visible: headerRectRef.isComparison
statusValue: "Added"
iconSize: 13
}

TextItem {
TextElement {
id: fnTopRef
x: 20
y: 4
width: parent.width - 30
wrapMode: Text.WrapAnywhere
text: headerRectRef.isComparison ? filenameOld : filenameNew
}

TextItem {
TextElement {
id: fnBotRef
x: 20
y: fnTopRef.y + fnTopRef.contentHeight + 3
Expand Down Expand Up @@ -146,6 +142,7 @@ Rectangle {
}
return h;
}
Keys.forwardTo: [hunksMainScrollRef]
Rectangle {
// The hunkListViewRef listview contains variable height elements. Even
// with just a few elements in the list, Qt will compute the full height,
Expand Down Expand Up @@ -174,9 +171,6 @@ Rectangle {
delegate: Component {
Item {
property bool isFloatingScrollBar: index === root.floatScrollBarIndex
property int indexProperty: index
property int itemLineCount: originsJsonModel.model.count || 0
property real itemContentHeight: height
id: hunkListRootItemRef
height: diffRef.contentHeight + 10 + hunkBotScrollRef.height + hunkTitleRectRef.height
width: parent.width
Expand All @@ -196,7 +190,7 @@ Rectangle {
width: parent.width
height: 20
color: "transparent"
TextItem {
TextElement {
x: 5
y: 4
opacity: 0.6
Expand All @@ -222,6 +216,24 @@ Rectangle {
}
color: "transparent"
clip: true
Component {
id: lineNumComponentRef
Rectangle {
height: 10
width: parent.width
color: "transparent"
TextElement {
anchors.right: parent.right
anchors.top: parent.top
height: 10
width: parent.width
horizontalAlignment: Text.AlignRight
font.pointSize: Style.fontPointSize - 2
font.family: Style.fontNameFixedWidth
text: value === MAX_U32_INT ? " " : value
}
}
}
ListView {
id: linesOldListRef
anchors.top: parent.top
Expand All @@ -232,22 +244,7 @@ Rectangle {
height: diffRef.height
model: linesOldJsonModel.model
interactive: false
delegate: Component {
Rectangle {
height: 10
width: parent.width
color: "transparent"
TextItem {
anchors.right: parent.right
anchors.top: parent.top
height: 10
width: parent.width
horizontalAlignment: Text.AlignRight
font.family: Style.fontNameFixedWidth
text: value === MAX_U32_INT ? " " : value
}
}
}
delegate: lineNumComponentRef
}
ListView {
id: linesNewListRef
Expand All @@ -259,22 +256,7 @@ Rectangle {
height: diffRef.height
model: linesNewJsonModel.model
interactive: false
delegate: Component {
Rectangle {
height: 10
width: parent.width
color: "transparent"
TextItem {
anchors.right: parent.right
anchors.top: parent.top
height: 10
width: parent.width
horizontalAlignment: Text.AlignRight
font.family: Style.fontNameFixedWidth
text: value === MAX_U32_INT ? " " : value
}
}
}
delegate: lineNumComponentRef
}
}
Rectangle {
Expand Down Expand Up @@ -319,7 +301,7 @@ Rectangle {
height: 10
width: 15
color: mapOriginToColor(value)
TextItem {
TextElement {
y: 0
anchors.right: parent.right
anchors.rightMargin: 4
Expand All @@ -330,15 +312,15 @@ Rectangle {
}
}
}
TextEdit {
TextElement {
id: diffRef
x: 15
y: 0
// ensure the full width can always be selected
width: hunkListingsRectRef.width
font.family: Style.fontNameFixedWidth
readOnly: true
selectByMouse: true
font.pointSize: Style.fontPointSize - 2
selectableText: true
text: hunk
// todo some font metrics stuff.
// Should equal a 4 space sized tab (at least for the adobefont+dpi settings, etc)
Expand Down Expand Up @@ -382,12 +364,23 @@ Rectangle {
height: parent.height
orientation: Qt.Vertical
size: height / hunkListViewRef.height
stepSize: 1 / (hunkItemLineCount * 0.5)
// 20 is 2 * lineheights.
// todo: if everything in the scroll container divided by 10,
// scroll steps would always align with lines.
stepSize: 1 / (hunkListViewRef.height / 20)
scrollContainerSize: parent.height
scrollContentSize: hunkListViewRef.height
captureMouseWheel: true
capturePositiveSide: false
containerOtherSize: parent.width
}
}
// Todo some nicer way of handling focus
MouseArea {
anchors.fill: parent;
onPressed: {
mouse.accepted = false;
root.forceActiveFocus();
}
}
}

0 comments on commit 9d7c5a3

Please sign in to comment.