Advertisement
Guest User

Untitled

a guest
Aug 5th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
QML 7.09 KB | None | 0 0
  1. /***************************************************************************
  2.  *   Copyright (C) 2019 by Dr_i-glu4IT <dr@i-glu4it.ru>                             *
  3.  ***************************************************************************/
  4.  
  5. import QtQuick 2.5
  6. import QtQuick.Layouts 1.1
  7. import QtQuick.Controls 1.4 as QtControls
  8. import QtMultimedia 5.8
  9. import org.kde.plasma.core 2.0 as PlasmaCore
  10. import org.kde.plasma.extras 2.0 as PlasmaExtras
  11. import org.kde.plasma.plasmoid 2.0
  12. import org.kde.plasma.components 2.0 as PlasmaComponents
  13.  
  14. Item {
  15.     id: root
  16.     Layout.fillWidth: true
  17.     Layout.fillHeight: true
  18.     width: 200
  19.     height: 300
  20.    
  21.     ServersModel {
  22.         id: serversModel
  23.     }
  24.    
  25.     Component.onCompleted: {
  26.         reloadServerModel();
  27.     }
  28.    
  29.     Connections {
  30.         target: plasmoid.configuration
  31.         onServersChanged: {
  32.             reloadServerModel();
  33.         }
  34.     }
  35.    
  36.     MediaPlayer
  37.     {
  38.         id: playMusic
  39.         onError: {
  40.             playMusic.stop();
  41.             reloadServerModel();
  42.         }
  43.         onStopped: {
  44.            
  45.         }
  46.            onStatusChanged:
  47.          {
  48.         }
  49.        volume: 0.8
  50.     }
  51.    
  52.     Plasmoid.compactRepresentation: Item {
  53.         id: comp
  54.         width: parent.width
  55.             height: parent.width
  56.            
  57.         PlasmaCore.IconItem {
  58.             anchors.fill: parent
  59.             source:  "radio"
  60.             width: parent.width
  61.             height: parent.width
  62.         }
  63.        
  64.         Timer  {
  65.             id: elapsedTimer
  66.             interval: 3000;
  67.             running: true;
  68.            repeat: true;
  69.            onTriggered: toolTip.mainText =  i18n("Advanced Radio Player"), toolTip.subText = (isPlaying()) ? playMusic.metaData.title : i18n("Choose station")
  70.        }
  71.        
  72.         MouseArea {
  73.             id: mouseArea
  74.             width: parent.width
  75.             height: parent.width
  76.             anchors.fill: parent
  77.             hoverEnabled: true
  78.             onClicked: {
  79.                 plasmoid.expanded = !plasmoid.expanded
  80.             }
  81.         onWheel: {
  82.             elapsedTimer.stop()
  83.               if (wheel.angleDelta.y > 0 && playMusic.volume < 1) {
  84.                  playMusic.volume += 0.05
  85.               } else if (wheel.angleDelta.y < 0 && playMusic.volume > 0){
  86.                    playMusic.volume -= 0.05
  87.               }
  88.              toolTip.mainText = Math.round(playMusic.volume * 100) + '%'
  89.              toolTip.subText = i18n('Volume')
  90.               elapsedTimer.restart()
  91.         }
  92.         }
  93.         PlasmaCore.ToolTipArea {
  94.         id: toolTip
  95.         width: parent.width
  96.             height: parent.width
  97.         anchors.fill: parent
  98.         mainText: i18n("Advanced Radio Player")
  99.         subText: (playMusic.metaData.title === undefined) ? i18n("Unknown") : playMusic.metaData.title
  100.         icon: "radio"
  101.     }
  102.     }
  103.  
  104.     Plasmoid.fullRepresentation: Item {
  105.         Layout.preferredWidth: 200
  106.         Layout.preferredHeight: 300
  107.         clip:true
  108.         Rectangle {
  109.                    id: square
  110.                    color: "transparent"
  111.                   width: 200
  112.                     height: 20
  113.                 PlasmaComponents.Label {
  114.                     id: nameText2
  115.                     height: parent.height
  116.                     text:  isPlaying() ? playMusic.metaData.title : "Advanced Radio Player"
  117.                     verticalAlignment: Text.AlignVCenter
  118.                     onTextChanged: {
  119.                      anim.restart()
  120.                     }
  121.                  }
  122.                  NumberAnimation {
  123.                       property: "x"
  124.                       id: anim
  125.                       target: nameText2
  126.                       from: (isPlaying()) ? square.width : square.width + 100
  127.                       to: (isPlaying()) ? -nameText2.width : -nameText2.width / 2 + square.width / 2;
  128.                          duration: 20 * Math.abs(to - from);
  129.                          loops: (isPlaying()) ? Animation.Infinite : 1
  130.                     }
  131.                     MouseArea {
  132.                         id: mouseArea2
  133.             width: parent.width
  134.             height: parent.width
  135.             anchors.fill: parent
  136.             hoverEnabled: true
  137.             onEntered: {
  138.             (isPlaying()) ? anim.pause() : anim.resume()
  139.             }
  140.             onExited: {
  141.                 anim.resume()
  142.             }
  143.                     }
  144.                }
  145.         ListView {
  146.             id: serversListView
  147.             anchors.fill: parent
  148.             anchors.topMargin:25
  149.             anchors.bottomMargin:25
  150.             model: serversModel
  151.             clip:true
  152.        
  153.             delegate: Item {
  154.                 height: nameText.paintedHeight * 1.3
  155.                 width: parent.width
  156.                 PlasmaComponents.Label {
  157.                     id: icon
  158.                     height: parent.height
  159.                     text: model.status == 1  ? "▶" : model.index + 1
  160.                     width: 15
  161.                     verticalAlignment: Text.AlignVCenter
  162.                     horizontalAlignment: Text.AlignHCenter
  163.                 }
  164.                PlasmaComponents.Label {
  165.                     id: nameText
  166.                     anchors.left: icon.right
  167.                     anchors.leftMargin: 5
  168.                     height: parent.height
  169.                     text: model.name.length == 0 ? model.hostname : model.name
  170.                     verticalAlignment: Text.AlignVCenter
  171.                     elide: Text.ElideRight
  172.                    
  173.                 }
  174.                
  175.                 MouseArea {
  176.                     id: mouseArea
  177.                     anchors.top: icon.top
  178.                     anchors.bottom: icon.bottom
  179.                     anchors.left: icon.left
  180.                     width: serversListView.width
  181.                     hoverEnabled: true
  182.                     onEntered: {
  183.                         icon.text = model.status == 1 ? '■' : '▶'
  184.                     }
  185.                     onExited: {
  186.                          icon.text = model.status == 1 ? '▶' : model.index + 1
  187.                     }
  188.                     onClicked: {
  189.                         refreshServer(model.index)
  190.                     }
  191.                 }
  192.             }
  193.  
  194.         }
  195.         Rectangle {
  196.                    id: square2
  197.                    color: "transparent"
  198.                   width: 200
  199.                     height: 30
  200.                     anchors.top: serversListView.bottom
  201.  
  202.                    
  203.                     PlasmaComponents.Label {
  204.                     id: nameText3
  205.                     font.pixelSize : 10
  206.                     height: parent.height
  207.                     width: parent.width
  208.                     text: isPlaying() ? 'Bitrate: ' + Math.round(playMusic.metaData.audioBitRate / 1000) + 'Kb/s Genre: ' + playMusic.metaData.genre : "Choose station and enjoy..."
  209.                     verticalAlignment: Text.AlignVCenter
  210.                     horizontalAlignment: Text.AlignHCenter
  211.                     elide: Text.ElideRight
  212.                  }
  213.             }
  214.         PlasmaComponents.Button {
  215.             anchors.centerIn: parent
  216.             text: i18n("Add stations")
  217.             visible: serversModel.count == 0
  218.             onClicked: plasmoid.action("configure").trigger();
  219.         }
  220.     }
  221.    
  222.     function reloadServerModel() {
  223.         serversModel.clear();
  224.         playMusic.stop()
  225.         var servers = JSON.parse(plasmoid.configuration.servers);
  226.        
  227.         for(var i = 0; i < servers.length; i++) {
  228.             if(servers[i].active) {
  229.                 serversModel.append(servers[i]);
  230.             }
  231.         }
  232.     }
  233.    
  234.     function refreshServer(index) {
  235.          serversModel.setProperty(index, "status", 0);
  236.         if( isPlaying() && playMusic.source == serversModel.get(index).hostname) {
  237.             playMusic.stop()
  238.             serversModel.setProperty(index, "status", 0)
  239.         }
  240.         else
  241.         {  
  242.            //reloadServerModel()
  243.             playMusic.source = serversModel.get(index).hostname
  244.             serversModel.setProperty(index, "status", 1);
  245.             playMusic.play()
  246.            
  247.         }
  248.     }
  249.    
  250.     function isPlaying()
  251.     {
  252.         return playMusic.playbackState == MediaPlayer.PlayingState
  253.     }
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement