PathSelection.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #
  2. # This file is part of the LibreOffice project.
  3. #
  4. # This Source Code Form is subject to the terms of the Mozilla Public
  5. # License, v. 2.0. If a copy of the MPL was not distributed with this
  6. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  7. #
  8. # This file incorporates work covered by the following license notice:
  9. #
  10. # Licensed to the Apache Software Foundation (ASF) under one or more
  11. # contributor license agreements. See the NOTICE file distributed
  12. # with this work for additional information regarding copyright
  13. # ownership. The ASF licenses this file to you under the Apache
  14. # License, Version 2.0 (the "License"); you may not use this file
  15. # except in compliance with the License. You may obtain a copy of
  16. # the License at http://www.apache.org/licenses/LICENSE-2.0 .
  17. #
  18. import traceback
  19. from ..common.PropertyNames import PropertyNames
  20. from ..common.FileAccess import FileAccess
  21. from ..common.SystemDialog import SystemDialog
  22. class PathSelection(object):
  23. class DialogTypes(object):
  24. FOLDER = 0
  25. FILE = 1
  26. class TransferMode(object):
  27. SAVE = 0
  28. LOAD = 1
  29. def __init__(self, xMSF, CurUnoDialog, TransferMode, DialogType):
  30. self.CurUnoDialog = CurUnoDialog
  31. self.xMSF = xMSF
  32. self.iDialogType = DialogType
  33. self.iTransferMode = TransferMode
  34. self.sDefaultDirectory = ""
  35. self.sDefaultName = ""
  36. self.sDefaultFilter = ""
  37. self.usedPathPicker = False
  38. self.CMDSELECTPATH = 1
  39. self.TXTSAVEPATH = 1
  40. def insert(
  41. self, DialogStep, XPos, YPos, Width,
  42. CurTabIndex, LabelText, Enabled, TxtHelpURL, BtnHelpURL):
  43. self.CurUnoDialog.insertControlModel(
  44. "com.sun.star.awt.UnoControlFixedTextModel", "lblSaveAs",
  45. (PropertyNames.PROPERTY_ENABLED,
  46. PropertyNames.PROPERTY_HEIGHT,
  47. PropertyNames.PROPERTY_LABEL,
  48. PropertyNames.PROPERTY_POSITION_X,
  49. PropertyNames.PROPERTY_POSITION_Y,
  50. PropertyNames.PROPERTY_STEP,
  51. PropertyNames.PROPERTY_TABINDEX,
  52. PropertyNames.PROPERTY_WIDTH),
  53. (Enabled, 8, LabelText, XPos, YPos, DialogStep,
  54. CurTabIndex, Width))
  55. self.xSaveTextBox = self.CurUnoDialog.insertTextField(
  56. "txtSavePath", "callXPathSelectionListener",
  57. (PropertyNames.PROPERTY_ENABLED,
  58. PropertyNames.PROPERTY_HEIGHT,
  59. PropertyNames.PROPERTY_HELPURL,
  60. PropertyNames.PROPERTY_POSITION_X,
  61. PropertyNames.PROPERTY_POSITION_Y,
  62. PropertyNames.PROPERTY_STEP,
  63. PropertyNames.PROPERTY_TABINDEX,
  64. PropertyNames.PROPERTY_WIDTH),
  65. (Enabled, 12, TxtHelpURL, XPos, YPos + 10, DialogStep,
  66. (CurTabIndex + 1), Width - 26), self)
  67. self.CurUnoDialog.xDialogModel.txtSavePath.Enabled = False
  68. self.CurUnoDialog.insertButton("cmdSelectPath", "triggerPathPicker",
  69. (PropertyNames.PROPERTY_ENABLED,
  70. PropertyNames.PROPERTY_HEIGHT,
  71. PropertyNames.PROPERTY_HELPURL,
  72. PropertyNames.PROPERTY_LABEL,
  73. PropertyNames.PROPERTY_POSITION_X,
  74. PropertyNames.PROPERTY_POSITION_Y,
  75. PropertyNames.PROPERTY_STEP,
  76. PropertyNames.PROPERTY_TABINDEX,
  77. PropertyNames.PROPERTY_WIDTH),
  78. (Enabled, 14, BtnHelpURL, "...",XPos + Width - 16, YPos + 9,
  79. DialogStep, (CurTabIndex + 2), 16), self)
  80. def addSelectionListener(self, xAction):
  81. self.xAction = xAction
  82. def getSelectedPath(self):
  83. return self.xSaveTextBox.Text
  84. def initializePath(self):
  85. try:
  86. myFA = FileAccess(self.xMSF)
  87. self.xSaveTextBox.setText(
  88. myFA.getPath(self.sDefaultDirectory + \
  89. "/" + \
  90. self.sDefaultName, None))
  91. except Exception:
  92. traceback.print_exc()
  93. def triggerPathPicker(self):
  94. try:
  95. if self.iTransferMode == self.TransferMode.SAVE:
  96. if self.iDialogType == self.DialogTypes.FOLDER:
  97. #TODO: write code for picking a folder for saving
  98. return
  99. elif self.iDialogType == self.DialogTypes.FILE:
  100. self.usedPathPicker = True
  101. myFilePickerDialog = \
  102. SystemDialog.createStoreDialog(self.xMSF)
  103. myFilePickerDialog.callStoreDialog(
  104. self.sDefaultDirectory,
  105. self.sDefaultName, self.sDefaultFilter)
  106. sStorePath = myFilePickerDialog.sStorePath
  107. if sStorePath is not None:
  108. myFA = FileAccess(self.xMSF)
  109. self.xSaveTextBox.Text = myFA.getPath(sStorePath, None)
  110. self.sDefaultDirectory = \
  111. FileAccess.getParentDir(sStorePath)
  112. self.sDefaultName = myFA.getFilename(sStorePath)
  113. return
  114. elif self.iTransferMode == self.TransferMode.LOAD:
  115. if self.iDialogType == self.DialogTypes.FOLDER:
  116. #TODO: write code for picking a folder for loading
  117. return
  118. elif self.iDialogType == self.DialogTypes.FILE:
  119. #TODO: write code for picking a file for loading
  120. return
  121. except Exception:
  122. traceback.print_exc()
  123. def callXPathSelectionListener(self):
  124. if self.xAction is not None:
  125. self.xAction.validatePath()