Avatar.qml 1.66 KB
Newer Older
1
import QtQuick 2.7
2
import QtGraphicalEffects 1.0
3

4 5
import Linphone 1.0
import Linphone.Styles 1.0
6
import Utils 1.0
7

8 9 10
// ===================================================================

Item {
11
  property alias image: imageToFilter.source
12
  property alias presenceLevel: presenceLevel.level
13 14
  property string username

15 16
  property var _initialsRegex: /^\s*([^\s]+)(?:\s+([^\s]+))?/

17
  function _computeInitials () {
18
    var result = username.match(_initialsRegex)
19

20 21 22 23
    Utils.assert(
      result != null,
      'Unable to get initials of: \'' + username + '\''
    )
24

25 26 27 28 29 30
    return result[1].charAt(0).toUpperCase() +
      (
        result[2] != null
          ? result[2].charAt(0).toUpperCase()
          : ''
      )
31
  }
32

33 34 35
  // Image mask. (Circle)
  Rectangle {
    id: mask
36

37 38 39 40
    anchors.fill: parent
    color: AvatarStyle.mask.color
    radius: AvatarStyle.mask.radius
  }
41

42 43 44 45
  // Initials.
  Text {
    anchors.centerIn: parent
    color: AvatarStyle.initials.color
46 47 48 49 50 51 52 53 54 55
    font.pointSize: {
      var width

      if (parent.width > 0) {
        width = parent.width / AvatarStyle.initials.ratio
      }

      return AvatarStyle.initials.fontSize * (width || 1)
    }

56 57
    text: _computeInitials()
  }
58

59 60 61 62
  Image {
    anchors.fill: parent
    id: imageToFilter
    fillMode: Image.PreserveAspectFit
63

64 65 66 67
    // Image must be invisible.
    // The only visible image is the OpacityMask!
    visible: false
  }
68

69 70 71 72 73 74 75 76
  // Avatar.
  OpacityMask {
    anchors.fill: imageToFilter
    maskSource: mask
    source: imageToFilter
  }

  // Presence.
77 78 79
  PresenceLevel {
    id: presenceLevel

80 81 82 83 84
    anchors.bottom: parent.bottom
    anchors.right: parent.right
    height: parent.height / 3
    width: parent.width / 3
  }
85
}