code slide...
[qt-labs:qml-presentation-system.git] / src / CodeSlide.qml
1 import QtQuick 2.0
2
3 Slide {
4     id: slide;
5
6     property string codeFontFamily: "Courier New"
7     property string code;
8     property real codeFontSize: baseFontSize * 0.6;
9
10
11
12     Rectangle {
13         id: background
14         anchors.fill: parent
15         radius: height / 10;
16         gradient: Gradient {
17             GradientStop { position: 0; color: Qt.rgba(0.8, 0.8, 0.8, 0.5); }
18             GradientStop { position: 1; color: Qt.rgba(0.2, 0.2, 0.2, 0.5); }
19         }
20         border.color: slide.slideTextColor;
21         border.width: height / 250;
22         antialiasing: true
23     }
24
25     onCodeChanged: {
26         listModel.clear();
27         var codeLines = slide.code.split("\n");
28         for (var i=0; i<codeLines.length; ++i) {
29             listModel.append({
30                                 line: i,
31                                 code: codeLines[i]
32                              });
33         }
34     }
35
36
37
38     ListModel {
39         id: listModel
40     }
41
42
43     onVisibleChanged: {
44         listView.focus = slide.visible;
45         listView.currentIndex = -1;
46     }
47
48     ListView {
49         id: listView;
50
51         anchors.fill: parent;
52         anchors.margins: background.radius / 2
53         clip: true
54
55         model: listModel;
56         focus: true;
57
58         MouseArea {
59             anchors.fill: parent
60             onClicked: {
61                 listView.focus = true;
62                 listView.currentIndex = listView.indexAt(mouse.x, mouse.y + listView.contentY);
63             }
64
65         }
66
67         delegate: Item {
68
69             id: itemDelegate
70
71             height: lineLabel.height
72             width: parent.width
73
74             Rectangle {
75                 id: lineLabelBackground
76                 width: lineLabel.height * 3;
77                 height: lineLabel.height;
78                 color: slide.slideTextColor;
79                 opacity: 0.1;
80             }
81
82             Text {
83                 id: lineLabel
84                 anchors.right: lineLabelBackground.right;
85                 text: line + ":"
86                 color: slide.slideTextColor;
87                 font.family: slide.codeFontFamily
88                 font.pixelSize: slide.codeFontSize
89                 font.bold: itemDelegate.ListView.isCurrentItem;
90                 opacity: itemDelegate.ListView.isCurrentItem ? 1 : 0.8;
91
92             }
93
94             Rectangle {
95                 id: lineContentBackground
96                 anchors.fill: lineContent;
97                 anchors.leftMargin: -height / 2;
98                 color: slide.slideTextColor
99                 opacity: 0.2
100                 visible: itemDelegate.ListView.isCurrentItem;
101             }
102
103             Text {
104                 id: lineContent
105                 anchors.left: lineLabelBackground.right
106                 anchors.leftMargin: lineContent.height;
107                 anchors.right: parent.right;
108                 color: slide.slideTextColor;
109                 text: code;
110                 font.family: slide.codeFontFamily
111                 font.pixelSize: slide.codeFontSize
112                 font.bold: itemDelegate.ListView.isCurrentItem;
113                 opacity: itemDelegate.ListView.isCurrentItem ? 1 : 0.8;
114             }
115         }
116     }
117
118
119
120 }