ScolaSync  5.1
copyToDialog1.py
Aller à la documentation de ce fichier.
1 # $Id: copyToDialog1.py 47 2011-06-13 10:20:14Z georgesk $
2 
3 licenceEn="""
4  file copyToDialog1.py
5  this file is part of the project scolasync
6 
7  Copyright (C) 2010 Georges Khaznadar <georgesk@ofset.org>
8 
9  This program is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version3 of the License, or
12  (at your option) any later version.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program. If not, see <http://www.gnu.org/licenses/>.
21 """
22 
23 from PyQt5.QtCore import *
24 from PyQt5.QtWidgets import *
25 from PyQt5.QtGui import *
26 import os.path, subprocess
27 
28 import Ui_copyToDialog1
29 
30 # Un dialogue pour choisir un ensemble de fichiers à transférer vers
31 # une collection de clés USB.
32 # @param parent un widget
33 # @param workdir un répertoire cible sur les baladeurs
34 #
35 
37 
40 
41  def __init__(self,parent = None, workdir=""):
42  QDialog.__init__(self,parent)
43  self.mainWindow=parent
44  self._ui=Ui_copyToDialog1.Ui_Dialog()
45  self._ui.setupUi(self)
46  self._ui.travailEdit.setText(workdir)
47  self.setupFromListe()
48  self._fromDir=QDir.home()
49  self.setFromListeDir(self._fromDir)
50  self.setupToListe()
51 
53  self.ok="False"
54  self._ui.selectButton.clicked.connect(self.select)
55  self._ui.removeButton.clicked.connect(self.remove)
56  self._ui.cancelButton.clicked.connect(self.cancel)
57  self._ui.continueButton.clicked.connect(self.cont)
58  self._ui.travailEdit.editingFinished.connect(self.changeWd)
59 
60 
62 
63  def changeWd(self):
64  newDir=self._ui.travailEdit.text()
65  self.mainWindow.changeWd(newDir)
66 
67 
69 
70  def cancel(self):
71  self.close()
72 
73  # L'action provoquée par le bouton de continuation : fermeture du dialogue
74  # et self.ok devient vrai.
75  #
76 
77  def cont(self):
78  self.ok=True
79  self.close()
80 
81 
83 
84  def setupFromListe(self):
85  self._model1 = QDirModel()
86  self._model1.setFilter(QDir.AllEntries)
87  self._ui.listViewFrom.setModel(self._model1)
88  self._ui.listViewFrom.doubleClicked.connect(self.cd)
89 
90 
93 
94  def setFromListeDir(self,directory):
95  path=directory.path()
96  cwdIndex = self._model1.index(path)
97  self._ui.listViewFrom.setRootIndex(cwdIndex)
98  self._ui.lineEdit.setText(path)
99 
100 
103 
104  def cd(self,index):
105  d= "%s" %index.data()
106  p= "%s" %self._fromDir.path()
107  j=os.path.abspath(os.path.join(p,d))
108  if os.path.isdir(j):
109  self._fromDir=QDir(j)
110  self.setFromListeDir(self._fromDir)
111 
112 
114 
115  def setupToListe(self):
116  self._model2 = QStandardItemModel()
117 
119  self._proxyModel = QSortFilterProxyModel()
120  self._proxyModel.setSourceModel(self._model2)
121  self._ui.listViewTo.setModel(self._proxyModel)
122  self._proxyModel.setDynamicSortFilter(True)
123 
128 
129  # Ajoute le répertoire ou le fichier sélectionné dans le
130  # navigateur de fichiers à la liste de sélections.
131  #
132 
133  def select(self):
134  sel=self._ui.listViewFrom.selectedIndexes()
135  if len(sel)>0:
136  index=sel[0]
137  d= "%s" %index.data()
138  p= "%s" %self._fromDir.path()
139  j=os.path.abspath(os.path.join(p,d))
140  f=self._model2.findItems(j)
141  if len(f)==0:
142  self._model2.appendRow(QStandardItem(j))
143  self._proxyModel.sort(0)
144  # on lance un calcul à nouveau pour la taille totale occupée
145  self.displaySize()
146  else:
147  print ("%s est déjà sélectionné" %j)
148 
149 
151 
152  def displaySize(self):
153  total=0
154  for path in self.selectedList():
155  p=subprocess.Popen("du -s '%s' | awk '{print $1}'" %path,
156  shell=True, stdout=subprocess.PIPE)
157  size=p.communicate()[0]
158  try:
159  total+= int(size)
160  except:
161  pass
162  unit="%s" %QApplication.translate("Dialog","%s kilo-octets",None)
163  if total>1024:
164  total= 0.1*int(10*total/1024)
165  unit="%s" %QApplication.translate("Dialog","%s méga-octets",None)
166  if total>1024:
167  total= 0.1*int(10*total/1024)
168  unit="%s" %QApplication.translate("Dialog","%s giga-octets",None)
169  self._ui.lineEdit_size.setText(unit %total)
170 
171  # Supprime le répertoire ou le fichier sélectionné dans la
172  # liste de sélections.
173  #
174 
175  def remove(self):
176  sel=self._ui.listViewTo.selectedIndexes()
177  if len(sel)>0:
178  index=sel[0]
179  sourceIndex=self._proxyModel.mapToSource(index)
180  self._model2.removeRow(sourceIndex.row())
181  self._proxyModel.sort(0)
182  # on lance un calcul à nouveau pour la taille totale occupée
183  self.displaySize()
184 
185  # Renvoie une liste de répertoires et de fichiers qui ont été
186  # sélectionnés pour la copie sur clé USB.
187  # @return une liste de QStrings
188  #
189 
190  def selectedList(self):
191  sl=self._model2.findItems("*",Qt.MatchWildcard)
192  return map(lambda x: ("%s" %x.text()), sl)
193 
194 if __name__=="__main__":
195  import sys
196  app = QApplication(sys.argv)
197  windows = copyToDialog1()
198  windows.show()
199  sys.exit(app.exec_())
src.copyToDialog1.copyToDialog1.mainWindow
mainWindow
Definition: copyToDialog1.py:43
src.copyToDialog1.copyToDialog1._model2
_model2
Definition: copyToDialog1.py:116
src.copyToDialog1.copyToDialog1._proxyModel
_proxyModel
on connecte la liste d'items standard via un proxy qui autorise le tri alphabétique
Definition: copyToDialog1.py:119
src.copyToDialog1.copyToDialog1.ok
ok
Definition: copyToDialog1.py:78
src.copyToDialog1.copyToDialog1
Definition: copyToDialog1.py:36
src.copyToDialog1.copyToDialog1.remove
def remove(self)
Definition: copyToDialog1.py:175
src.copyToDialog1.copyToDialog1.cd
def cd(self, index)
Change le répertoire courant si possible.
Definition: copyToDialog1.py:104
src.copyToDialog1.copyToDialog1._model1
_model1
Definition: copyToDialog1.py:85
src.copyToDialog1.copyToDialog1._ui
_ui
Definition: copyToDialog1.py:44
QDialog
src.copyToDialog1.copyToDialog1.cancel
def cancel(self)
L'action provoquée par le bouton d'échappement : fermeture du dialogue.
Definition: copyToDialog1.py:70
src.copyToDialog1.copyToDialog1.displaySize
def displaySize(self)
Affiche la taille de la sélection courante.
Definition: copyToDialog1.py:152
src.copyToDialog1.copyToDialog1.setFromListeDir
def setFromListeDir(self, directory)
Choisit un répertoire pour la liste source.
Definition: copyToDialog1.py:94
src.copyToDialog1.copyToDialog1.changeWd
def changeWd(self)
changement du répertoire de travail
Definition: copyToDialog1.py:63
src.copyToDialog1.copyToDialog1._fromDir
_fromDir
Definition: copyToDialog1.py:48
src.copyToDialog1.copyToDialog1.cont
def cont(self)
Definition: copyToDialog1.py:77
src.copyToDialog1.copyToDialog1.selectedList
def selectedList(self)
Definition: copyToDialog1.py:190
QtCore
src.copyToDialog1.copyToDialog1.select
def select(self)
Definition: copyToDialog1.py:133
src.copyToDialog1.copyToDialog1.setupToListe
def setupToListe(self)
Met en place un visionneur de fichierspour les fichiers reçus.
Definition: copyToDialog1.py:115
src.copyToDialog1.copyToDialog1.setupFromListe
def setupFromListe(self)
Met en place un visionneur de fichiers dans la liste source.
Definition: copyToDialog1.py:84
QtGui
QtWidgets