Browse Source

added the dirWidget directory selection box
-it includes a unit test at the end

Pat Beirne 8 years ago
parent
commit
5a315bd369
2 changed files with 97 additions and 4 deletions
  1. 60 0
      dirWidget
  2. 37 4
      dirnotes

+ 60 - 0
dirWidget

@@ -0,0 +1,60 @@
+#!/usr/bin/python
+''' a widget that shows only a dir listing
+'''
+
+import sys,os,argparse
+from PyQt4.QtGui import *
+from PyQt4 import QtGui, QtCore
+
+class DirWidget(QListWidget):
+	''' a simple widget that shows a list of directories, staring
+	at the directory passed into the constructor
+
+	a mouse click or 'enter' key will send a 'selected' signal to
+	anyone who connects to this.
+
+	the .. parent directory is shown for all dirs except /
+
+	the most interesting parts of the interface are:
+	constructor - send in the directory to view
+	method - currentPath() returns text of current path
+	signal - selected calls a slot with a single arg: new path
+	'''
+
+	selected = QtCore.pyqtSignal(str)
+	def __init__(self, parent=None,directory='.'):
+		super(DirWidget,self).__init__(parent)
+		self.directory = directory
+		self.refill()
+		self.itemActivated.connect(self.selectionByLWI)
+		# it would be nice to pick up single-mouse-click for selection as well
+		# but that seems to be a system-preferences global
+	def selectionByLWI(self, li):
+		self.directory = os.path.abspath(self.directory + '/' + str(li.text()))
+		self.refill()
+		self.selected.emit(self.directory)
+	def refill(self):
+		current,dirs,files =  os.walk(self.directory,followlinks=True).next()
+		dirs.sort()
+		if '/' not in dirs:
+			dirs = ['..'] + dirs
+		self.clear()
+		for d in dirs:
+			li = QListWidgetItem(d,self)
+	def currentPath(self):
+		return self.directory
+		
+if __name__=="__main__":
+	def alert(some_text):
+		os.system('notify-send -t 1500 "file: '+str(some_text) + '"')
+
+	a = QApplication([])
+
+	qmw = QMainWindow()
+	dw = DirWidget(qmw)
+	dw.selected.connect(alert)
+	
+	qmw.setCentralWidget(dw)
+	qmw.show()
+	
+	a.exec_()

+ 37 - 4
dirnotes

@@ -51,7 +51,7 @@ class DataBase:
 		
 	def getData(self, fileName):
 		c = self.db.cursor()
-		c.execute("select * from dirnotes where name=?",(fileName,))
+		c.execute("select * from dirnotes where name=? order by comment_date desc",(os.path.abspath(fileName),))
 		return c.fetchone()
 	def setData(self, fileName, _date, _size, comment):
 		c = self.db.cursor()
@@ -119,11 +119,13 @@ class DirNotes(QMainWindow):
 		'''
 	def __init__(self, filename, db, parent=None):
 		super(DirNotes,self).__init__(parent)
+		self.db = db
+
 		win = QWidget()
 		self.setCentralWidget(win)
-		self.db = db
 
 		lb = QTableWidget()
+		self.lb = lb
 		lb.setColumnCount(2)
 		lb.horizontalHeader().setResizeMode( 1, QHeaderView.Stretch );
 		lb.verticalHeader().setDefaultSectionSize(20);	# thinner rows
@@ -159,12 +161,22 @@ class DirNotes(QMainWindow):
 
 		e = QLabel("View and edit file comments stored in extended attributes user.xdg.comment")
 
+		b1 = QPushButton("restore from database")
+		dirLeft = QFileDialog(self,"",current)
+		dirLeft.setMaximumHeight(250)
+		dirLeft.setOption(QFileDialog.DontUseNativeDialog)
+		dirLeft.setFileMode(QFileDialog.Directory)
+		dirLeft.setOption(QFileDialog.ShowDirsOnly)
+		
 		layout = QVBoxLayout()
 		layout.addWidget(e)
+		layout.addWidget(b1)
+		layout.addWidget(dirLeft)
 		layout.addWidget(lb)
 		win.setLayout(layout)
 		
 		lb.itemChanged.connect(self.change)
+		b1.pressed.connect(self.restore_from_database)
 
 		QShortcut(QKeySequence("Ctrl+Q"), self, self.close)	
 		self.setWindowTitle("test")
@@ -178,7 +190,13 @@ class DirNotes(QMainWindow):
 		r = the_file.setComment(str(x.text())) 
 		if r:
 			self.db.log(the_file.getName(),x.text())
-
+	def restore_from_database(self):
+		print("restore from database")
+		fileName = str(self.lb.item(self.lb.currentRow(),0).text())
+		fo_row = self.db.getData(fileName)
+		if len(fo_row)>1:
+			comment = fo_row[3]
+			print(fileName,fo_row[0],comment)
 		
 if __name__=="__main__":
 	p = parse()
@@ -224,5 +242,20 @@ user.xdg.publisher
 
 ''' TODO: also need a way to display-&-restore comments from the database '''
 
-
+''' QFileDialog
+	-make my own?
+	-existing one has
+		-history
+		-back button
+		-up button
+		-but we don't need
+			-directory date
+			-icon option
+			-url browser (unless we go network file system)
+			-new folder button
+			-file type chooser
+			-text entry box
+			-choose & cancel buttons
+	
+	'''