Unity 8
 All Classes Functions Properties
Result.qml
1 /*
2  * Copyright (C) 2012, 2013 Canonical, Ltd.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 3.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 import QtQuick 2.0
18 import Ubuntu.Components 0.1
19 
20 Item {
21  property string contextSnippetText
22  property variant contextSnippetHighlights
23  property string nameText
24  property variant nameHighlights
25 
26  function highlightedText(text, highlights) {
27  var hText = "";
28  var nextIndexToProcess = 0;
29  if (highlights && highlights.length % 2 == 0) {
30  for (var i = 0; i < highlights.length - 1; i += 2) {
31  var highlightStart = highlights[i];
32  var highlightEnd = highlights[i + 1];
33  if (highlightEnd < highlightStart)
34  continue;
35  if (highlightStart < nextIndexToProcess)
36  continue;
37  if (highlightStart != nextIndexToProcess) {
38  // Prev non marked text
39  hText += text.substr(nextIndexToProcess, highlightStart - nextIndexToProcess);
40  }
41 
42  // Marked text
43  hText += "<font color=\"#ffffff\">" + text.substr(highlightStart, highlightEnd - highlightStart + 1) + "</font>";
44  nextIndexToProcess = highlightEnd + 1;
45  }
46  }
47  if (nextIndexToProcess != text.length) {
48  // End non marked text
49  hText += text.substr(nextIndexToProcess);
50  }
51  return hText;
52  }
53 
54  Label {
55  id: actionLabel
56  objectName: "actionLabel"
57  anchors.left: parent.left
58  anchors.leftMargin: units.gu(1)
59  anchors.verticalCenter: parent.verticalCenter
60  width: parent.width / 2
61  fontSize: "medium"
62  elide: Text.ElideRight
63  maximumLineCount: 1
64  text: highlightedText(nameText, nameHighlights)
65  color: "#80ffffff"
66  }
67 
68  Label {
69  horizontalAlignment: Text.AlignRight
70  id: contextSnippetLabel
71  objectName: "contextSnippetLabel"
72  width: parent.width / 2 - units.gu(1)
73  visible: text != ""
74  anchors.right: parent.right
75  anchors.rightMargin: units.gu(1)
76  anchors.verticalCenter: parent.verticalCenter
77  fontSize: "small"
78  opacity: 0.5
79  elide: Text.ElideRight
80  maximumLineCount: 2
81  text: highlightedText(contextSnippetText, contextSnippetHighlights)
82  color: "#80ffffff"
83  }
84 }