Advertisement
kingtobbe

Untitled

May 20th, 2018
2,385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
QML 5.23 KB | None | 0 0
  1. // Copyright (C) 2017 Allegorithmic
  2. //
  3. // This software may be modified and distributed under the terms
  4. // of the MIT license.  See the LICENSE file for details.
  5.  
  6. import QtQuick 2.7
  7. import Painter 1.0
  8.  
  9. PainterPlugin
  10. {
  11.   ConfigurationStruct {
  12.     id: config
  13.   }
  14.  
  15.   // timer to display the popup since WorkerScript cannot be used
  16.   // with alg context
  17.   Timer
  18.   {
  19.     id: savePostProcess
  20.     repeat: false
  21.     interval: 1
  22.     onTriggered: {
  23.       try {
  24.         if (!internal.projectOpen) return
  25.         alg.log.info("Auto saving project (backup number " + config.actualFileIndex + ")...");
  26.         var origPath;
  27.         var projectIsSaved = true;
  28.         try {
  29.           alg.project.url();
  30.         }
  31.         catch(e) {
  32.           projectIsSaved = false
  33.         }
  34.         if (!projectIsSaved) {
  35.           // use config default
  36.           if (config.tempFileName === "") {
  37.             var date = new Date()
  38.             config.tempFileName = date.toLocaleString(Qt.locale(), "dd_MM_yyyy_hh_mm")
  39.           }
  40.           origPath = config.saveDirectoryPath + config.tempFileName + ".spp"
  41.         }
  42.         else origPath = alg.fileIO.urlToLocalFile(alg.project.url());
  43.         // remove extension
  44.         var pathTemplate = origPath.replace(/\.[^\.]*$/, "")
  45.         // replace path if needed
  46.         if (config.alwaysUseSaveDirectory) {
  47.           pathTemplate = pathTemplate.replace(/^.*\//, config.saveDirectoryPath)
  48.         }
  49.         pathTemplate = alg.fileIO.localFileToUrl(pathTemplate)
  50.  
  51.         alg.project.saveAsCopy(pathTemplate + "_autosave_" + config.actualFileIndex + ".spp")
  52.  
  53.         internal.incrFileIndex()
  54.         // close() is disable for the popup, we must affect visible directly
  55.         savePopup.visible = false
  56.       }
  57.       catch(err) {
  58.         alg.log.exception(err)
  59.         savePopup.visible = false
  60.       }
  61.     }
  62.   }
  63.  
  64.   QtObject {
  65.     id: internal
  66.     property bool projectOpen: alg.project.isOpen()
  67.     readonly property alias saving: savePopup.visible
  68.     property bool computing: false
  69.     property bool startProgress: false
  70.     onProjectOpenChanged: {
  71.       if (projectOpen) {
  72.         reinitRemainingTime()
  73.         timer.start()
  74.       }
  75.       else timer.stop()
  76.     }
  77.  
  78.     function incrFileIndex() {
  79.       ++config.actualFileIndex;
  80.       if (config.actualFileIndex >= config.filesNumber) config.actualFileIndex = 0
  81.     }
  82.  
  83.     function save() {
  84.       if(alg.project.needSaving())
  85.       {
  86.         alg.log.info("Autosaving...");
  87.         savePopup.visible = true
  88.         savePostProcess.start()
  89.       }
  90.     }
  91.  
  92.     function initRemainingTime(toTime) {
  93.       internal.startProgress = config.warningTime >= toTime
  94.       config.remainingTime = toTime
  95.     }
  96.  
  97.     function reinitRemainingTime() {
  98.       initRemainingTime(config.interval)
  99.     }
  100.  
  101.     function snooze() {
  102.       initRemainingTime(config.snooze)
  103.     }
  104.  
  105.     function onProjectChange() {
  106.       projectOpen = true
  107.       config.actualFileIndex = 0
  108.     }
  109.   }
  110.  
  111.   Timer
  112.   {
  113.     id: timer
  114.     repeat: true
  115.     interval: 1000
  116.     onTriggered: {
  117.       if (!internal.saving && config.remainingTime != 0) --config.remainingTime;
  118.       // reinitialize timer if null
  119.       if (config.remainingTime <= config.warningTime) {
  120.         internal.startProgress = true;
  121.         // If computing, wait until computation end
  122.         if (internal.computing) return
  123.         if (config.remainingTime == 0) {
  124.           internal.save();
  125.           internal.reinitRemainingTime()
  126.         }
  127.       }
  128.     }
  129.   }
  130.  
  131.   Component.onCompleted:
  132.   {
  133.     // save button
  134.     var snoozeButton = alg.ui.addWidgetToPluginToolBar( "SnoozeButton.qml" )
  135.     // bind remaining time and saving values
  136.     snoozeButton.remainingTime = Qt.binding(function() { return config.remainingTime })
  137.     snoozeButton.startProgress = Qt.binding(function() { return internal.startProgress })
  138.     snoozeButton.saving = Qt.binding(function() { return internal.saving })
  139.     snoozeButton.progressMaxValue = Qt.binding(function() { return config.warningTime })
  140.     snoozeButton.active = Qt.binding(function() { return internal.projectOpen })
  141.     // bind button status
  142.     snoozeButton.clicked.connect(internal.snooze)
  143.   }
  144.  
  145.   onProjectSaved: internal.reinitRemainingTime()
  146.  
  147.   onProjectAboutToClose: {
  148.     config.tempFileName = ""
  149.     internal.projectOpen = false
  150.   }
  151.  
  152.   onProjectOpened: internal.onProjectChange()
  153.  
  154.   onNewProjectCreated: internal.onProjectChange()
  155.  
  156.   onComputationStatusChanged: {
  157.     internal.computing = isComputing
  158.   }
  159.  
  160.   onConfigure:
  161.   {
  162.     configDialog.open();
  163.   }
  164.  
  165.   ConfigurePanel
  166.   {
  167.     id: configDialog
  168.  
  169.     onVisibleChanged: {
  170.       if (visible) timer.stop()
  171.       else if (internal.projectOpen) timer.restart()
  172.     }
  173.  
  174.     onConfigurationChanged: {
  175.       config.interval = interval
  176.       config.filesNumber = filesNumber
  177.       if (config.actualFileIndex >= filesNumber) config.actualFileIndex = 0
  178.       config.snooze = snooze
  179.       config.warningTime = warningTime
  180.       config.saveDirectoryPath = saveDirectoryPath
  181.       config.alwaysUseSaveDirectory = alwaysUseSaveDirectory
  182.       if (config.remainingTime == 0 || config.remainingTime > config.interval)
  183.         internal.reinitRemainingTime();
  184.     }
  185.   }
  186.  
  187.   SavePopup {
  188.     id: savePopup
  189.   }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement