ScolaSync 1.0
copyToDialog1.py
Aller à la documentation de ce fichier.
00001 # -*- coding: utf-8 -*-    
00002 # $Id: copyToDialog1.py 47 2011-06-13 10:20:14Z georgesk $      
00003 
00004 licenceEn="""
00005     file copyToDialog1.py
00006     this file is part of the project scolasync
00007     
00008     Copyright (C) 2010 Georges Khaznadar <georgesk@ofset.org>
00009 
00010     This program is free software: you can redistribute it and/or modify
00011     it under the terms of the GNU General Public License as published by
00012     the Free Software Foundation, either version3 of the License, or
00013     (at your option) any later version.
00014 
00015     This program is distributed in the hope that it will be useful,
00016     but WITHOUT ANY WARRANTY; without even the implied warranty of
00017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018     GNU General Public License for more details.
00019 
00020     You should have received a copy of the GNU General Public License
00021     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00022 """
00023 
00024 from PyQt4.QtCore import *
00025 from PyQt4.QtGui import *
00026 import os.path, subprocess
00027 
00028 import Ui_copyToDialog1
00029 
00030 ##
00031 # 
00032 #     Un dialogue pour choisir un ensemble de fichiers à transférer vers
00033 #     une collection de clés USB.
00034 #     @param parent un widget
00035 #     @param workdir un répertoire cible sur les baladeurs
00036 #     
00037 class copyToDialog1(QDialog):
00038     ##
00039     # 
00040     #         Le constructeur
00041     #         @param parent un QWidget
00042     #         
00043     def __init__(self,parent = None, workdir=""):
00044         QDialog.__init__(self,parent)
00045         self.mainWindow=parent
00046         self._ui=Ui_copyToDialog1.Ui_Dialog()
00047         self._ui.setupUi(self)
00048         self._ui.travailEdit.setText(workdir)
00049         self.setupFromListe()
00050         self._fromDir=QDir.home()
00051         self.setFromListeDir(self._fromDir)
00052         self.setupToListe()
00053         ##
00054         # \var ok vrai si on a cliqué sur Continuer ...
00055         self.ok="False"
00056         QObject.connect(self._ui.selectButton, SIGNAL("clicked()"), self.select)
00057         QObject.connect(self._ui.removeButton, SIGNAL("clicked()"), self.remove)
00058         QObject.connect(self._ui.cancelButton, SIGNAL("clicked()"), self.cancel)
00059         QObject.connect(self._ui.continueButton, SIGNAL("clicked()"), self.cont)
00060         QObject.connect(self._ui.travailEdit, SIGNAL("editingFinished()"), self.changeWd)
00061 
00062     ##
00063     # 
00064     #         changement du répertoire de travail
00065     #         
00066     def changeWd(self):
00067         newDir=u"%s" %self._ui.travailEdit.text().toUtf8()
00068         self.mainWindow.changeWd(newDir)
00069 
00070     ##
00071     # 
00072     #         L'action provoquée par le bouton d'échappement : fermeture du dialogue
00073     #         
00074     def cancel(self):
00075         self.close()
00076 
00077     ##
00078     # 
00079     #         L'action provoquée par le bouton de continuation : fermeture du dialogue
00080     #         et self.ok devient vrai.
00081     #         
00082     def cont(self):
00083         self.ok=True
00084         self.close()
00085         
00086     ##
00087     # 
00088     #         Met en place un visionneur de fichiers dans la liste source
00089     #         
00090     def setupFromListe(self):
00091         self._model1 = QDirModel()
00092         self._model1.setFilter(QDir.AllEntries)
00093         self._ui.listViewFrom.setModel(self._model1)
00094         QObject.connect(self._ui.listViewFrom, SIGNAL("doubleClicked(QModelIndex)"), self.cd)
00095         
00096     ##
00097     # 
00098     #         Choisit un répertoire pour la liste source
00099     #         @param directory une instance de QDir
00100     #         
00101     def setFromListeDir(self,directory):
00102         path=directory.path()
00103         cwdIndex = self._model1.index(path)
00104         self._ui.listViewFrom.setRootIndex(cwdIndex)
00105         self._ui.lineEdit.setText(path)
00106 
00107     ##
00108     # 
00109     #         Change le répertoire courant si possible
00110     #         @param ev un évènement
00111     #         
00112     def cd(self,index):
00113         d= "%s" %index.data().toString()
00114         p= "%s" %self._fromDir.path()
00115         j=os.path.abspath(os.path.join(p,d))
00116         if os.path.isdir(j):
00117             self._fromDir=QDir(j)
00118             self.setFromListeDir(self._fromDir)
00119         
00120     ##
00121     # 
00122     #         Met en place un visionneur de fichierspour les fichiers reçus
00123     #         
00124     def setupToListe(self):
00125         self._model2 = QStandardItemModel()
00126         ### on connecte la liste d'items standard via un
00127         ### proxy qui autorise le tri alphabétique
00128         self._proxyModel = QSortFilterProxyModel()
00129         self._proxyModel.setSourceModel(self._model2)
00130         self._ui.listViewTo.setModel(self._proxyModel)
00131         self._proxyModel.setDynamicSortFilter(True)
00132         ###### apparemment les drops venus de la liste voisine
00133         ###### ne fonctionnent pas et c'est bien dommage !!!
00134         ## self._ui.listViewTo.setDragEnabled(True)
00135         ## self._ui.listViewTo.setAcceptDrops(True)
00136         ## self._ui.listViewTo.setDropIndicatorShown(True);
00137 
00138     ##
00139     # 
00140     #         Ajoute le répertoire ou le fichier sélectionné dans le
00141     #         navigateur de fichiers à la liste de sélections.
00142     #         
00143     def select(self):
00144         sel=self._ui.listViewFrom.selectedIndexes()
00145         if len(sel)>0:
00146             index=sel[0]
00147             d= u"%s" %index.data().toString()
00148             p= u"%s" %self._fromDir.path()
00149             j=os.path.abspath(os.path.join(p,d))
00150             f=self._model2.findItems(j)
00151             if len(f)==0:
00152                 self._model2.appendRow(QStandardItem(j))
00153                 self._proxyModel.sort(0)
00154                 # on lance un calcul à nouveau pour la taille totale occupée
00155                 self.displaySize()
00156             else:
00157                 print j, "est déjà sélectionné"
00158 
00159     ##
00160     # 
00161     #         Affiche la taille de la sélection courante
00162     #         
00163     def displaySize(self):
00164         total=0
00165         for path in self.selectedList():
00166             p=subprocess.Popen(u"du -s '%s' | awk '{print $1}'" %path,
00167                                shell=True, stdout=subprocess.PIPE)
00168             size=p.communicate()[0]
00169             try:
00170                 total+= int(size)
00171             except:
00172                 pass
00173         unit=u"%s" %QApplication.translate("Dialog","%s kilo-octets",None, QApplication.UnicodeUTF8)
00174         if total>1024:
00175             total= 0.1*int(10*total/1024)
00176             unit=u"%s" %QApplication.translate("Dialog","%s méga-octets",None, QApplication.UnicodeUTF8)
00177         if total>1024:
00178             total= 0.1*int(10*total/1024)
00179             unit=u"%s" %QApplication.translate("Dialog","%s giga-octets",None, QApplication.UnicodeUTF8)
00180         self._ui.lineEdit_size.setText(unit %total)
00181 
00182     ##
00183     # 
00184     #         Supprime le répertoire ou le fichier sélectionné dans la
00185     #         liste de sélections.
00186     #         
00187     def remove(self):
00188         sel=self._ui.listViewTo.selectedIndexes()
00189         if len(sel)>0:
00190             index=sel[0]
00191             sourceIndex=self._proxyModel.mapToSource(index)
00192             self._model2.removeRow(sourceIndex.row())
00193             self._proxyModel.sort(0)
00194             # on lance un calcul à nouveau pour la taille totale occupée
00195             self.displaySize()
00196 
00197     ##
00198     # 
00199     #         Renvoie une liste de répertoires et de fichiers qui ont été
00200     #         sélectionnés pour la copie sur clé USB.
00201     #         @return une liste de QStrings
00202     #         
00203     def selectedList(self):
00204         sl=self._model2.findItems("*",Qt.MatchWildcard)
00205         return map(lambda x: ("%s" %x.text()), sl)
00206         
00207 if __name__=="__main__":
00208     import sys
00209     app = QApplication(sys.argv)
00210     windows = copyToDialog1()
00211     windows.show()
00212     sys.exit(app.exec_())
00213 
 Tout Classes Espaces de nommage Fichiers Fonctions Variables