copyToDialog1.py
Aller à la documentation de ce fichier.
# $Id: copyToDialog1.py 47 2011-06-13 10:20:14Z georgesk $
licenceEn="""
file copyToDialog1.py
this file is part of the project scolasync
Copyright (C) 2010 Georges Khaznadar <georgesk@ofset.org>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import os.path, subprocess
import Ui_copyToDialog1
# Un dialogue pour choisir un ensemble de fichiers à transférer vers
# une collection de clés USB.
# @param parent un widget
# @param workdir un répertoire cible sur les baladeurs
#
class copyToDialog1(QDialog):
def __init__(self,parent = None, workdir=""):
QDialog.__init__(self,parent)
self.mainWindow=parent
self._ui=Ui_copyToDialog1.Ui_Dialog()
self._ui.setupUi(self)
self._ui.travailEdit.setText(workdir)
self.setupFromListe()
self._fromDir=QDir.home()
self.setFromListeDir(self._fromDir)
self.setupToListe()
self.ok="False"
self._ui.selectButton.clicked.connect(self.select)
self._ui.removeButton.clicked.connect(self.remove)
self._ui.cancelButton.clicked.connect(self.cancel)
self._ui.continueButton.clicked.connect(self.cont)
self._ui.travailEdit.editingFinished.connect(self.changeWd)
def changeWd(self):
newDir=self._ui.travailEdit.text()
self.mainWindow.changeWd(newDir)
def cancel(self):
self.close()
# L'action provoquée par le bouton de continuation : fermeture du dialogue
# et self.ok devient vrai.
#
def cont(self):
self.ok=True
self.close()
def setupFromListe(self):
self._model1 = QDirModel()
self._model1.setFilter(QDir.AllEntries)
self._ui.listViewFrom.setModel(self._model1)
self._ui.listViewFrom.doubleClicked.connect(self.cd)
def setFromListeDir(self,directory):
path=directory.path()
cwdIndex = self._model1.index(path)
self._ui.listViewFrom.setRootIndex(cwdIndex)
self._ui.lineEdit.setText(path)
def cd(self,index):
d= "%s" %index.data()
p= "%s" %self._fromDir.path()
j=os.path.abspath(os.path.join(p,d))
if os.path.isdir(j):
self._fromDir=QDir(j)
self.setFromListeDir(self._fromDir)
def setupToListe(self):
self._model2 = QStandardItemModel()
self._proxyModel = QSortFilterProxyModel()
self._proxyModel.setSourceModel(self._model2)
self._ui.listViewTo.setModel(self._proxyModel)
self._proxyModel.setDynamicSortFilter(True)
# Ajoute le répertoire ou le fichier sélectionné dans le
# navigateur de fichiers à la liste de sélections.
#
def select(self):
sel=self._ui.listViewFrom.selectedIndexes()
if len(sel)>0:
index=sel[0]
d= "%s" %index.data()
p= "%s" %self._fromDir.path()
j=os.path.abspath(os.path.join(p,d))
f=self._model2.findItems(j)
if len(f)==0:
self._model2.appendRow(QStandardItem(j))
self._proxyModel.sort(0)
# on lance un calcul à nouveau pour la taille totale occupée
self.displaySize()
else:
print ("%s est déjà sélectionné" %j)
def displaySize(self):
total=0
for path in self.selectedList():
p=subprocess.Popen("du -s '%s' | awk '{print $1}'" %path,
shell=True, stdout=subprocess.PIPE)
size=p.communicate()[0]
try:
total+= int(size)
except:
pass
unit="%s" %QApplication.translate("Dialog","%s kilo-octets",None)
if total>1024:
total= 0.1*int(10*total/1024)
unit="%s" %QApplication.translate("Dialog","%s méga-octets",None)
if total>1024:
total= 0.1*int(10*total/1024)
unit="%s" %QApplication.translate("Dialog","%s giga-octets",None)
self._ui.lineEdit_size.setText(unit %total)
# Supprime le répertoire ou le fichier sélectionné dans la
# liste de sélections.
#
def remove(self):
sel=self._ui.listViewTo.selectedIndexes()
if len(sel)>0:
index=sel[0]
sourceIndex=self._proxyModel.mapToSource(index)
self._model2.removeRow(sourceIndex.row())
self._proxyModel.sort(0)
# on lance un calcul à nouveau pour la taille totale occupée
self.displaySize()
# Renvoie une liste de répertoires et de fichiers qui ont été
# sélectionnés pour la copie sur clé USB.
# @return une liste de QStrings
#
def selectedList(self):
sl=self._model2.findItems("*",Qt.MatchWildcard)
return map(lambda x: ("%s" %x.text()), sl)
if __name__=="__main__":
import sys
app = QApplication(sys.argv)
windows = copyToDialog1()
windows.show()
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