2009-06-01 09:55:11 +00:00
|
|
|
# Created By: Virgil Dupras
|
|
|
|
# Created On: 2009-04-25
|
2011-04-12 08:04:01 +00:00
|
|
|
# Copyright 2011 Hardcoded Software (http://www.hardcoded.net)
|
2009-08-05 08:59:46 +00:00
|
|
|
#
|
2010-09-30 10:17:41 +00:00
|
|
|
# This software is licensed under the "BSD" License as described in the "LICENSE" file,
|
2009-08-05 08:59:46 +00:00
|
|
|
# which should be included with this package. The terms are also available at
|
2010-09-30 10:17:41 +00:00
|
|
|
# http://www.hardcoded.net/licenses/bsd_license
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2011-01-18 10:07:56 +00:00
|
|
|
from PyQt4.QtCore import QRect
|
2011-01-15 15:29:35 +00:00
|
|
|
from PyQt4.QtGui import (QWidget, QFileDialog, QHeaderView, QVBoxLayout, QHBoxLayout, QTreeView,
|
2011-09-22 14:35:17 +00:00
|
|
|
QAbstractItemView, QSpacerItem, QSizePolicy, QPushButton, QApplication, QMainWindow, QMenuBar,
|
|
|
|
QMenu, QIcon, QPixmap, QLabel)
|
2011-01-15 15:29:35 +00:00
|
|
|
|
2011-11-01 19:44:18 +00:00
|
|
|
from hscommon.trans import trget
|
2011-01-17 16:15:16 +00:00
|
|
|
from qtlib.recent import Recent
|
2012-03-04 16:14:59 +00:00
|
|
|
from qtlib.util import moveToScreenCenter, createActions
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2009-10-14 13:29:46 +00:00
|
|
|
from . import platform
|
|
|
|
from .directories_model import DirectoriesModel, DirectoriesDelegate
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2011-11-01 19:44:18 +00:00
|
|
|
tr = trget('ui')
|
|
|
|
|
2011-01-15 15:29:35 +00:00
|
|
|
class DirectoriesDialog(QMainWindow):
|
2009-06-01 09:55:11 +00:00
|
|
|
def __init__(self, parent, app):
|
2011-01-15 15:29:35 +00:00
|
|
|
QMainWindow.__init__(self, None)
|
2009-06-01 09:55:11 +00:00
|
|
|
self.app = app
|
2009-10-14 13:29:46 +00:00
|
|
|
self.lastAddedFolder = platform.INITIAL_FOLDER_IN_DIALOGS
|
2011-01-17 16:15:16 +00:00
|
|
|
self.recentFolders = Recent(self.app, 'recentFolders')
|
2009-06-01 09:55:11 +00:00
|
|
|
self._setupUi()
|
2012-01-16 14:29:57 +00:00
|
|
|
self.directoriesModel = DirectoriesModel(self.app.model.directory_tree, view=self.treeView)
|
2011-11-28 19:21:10 +00:00
|
|
|
self.directoriesDelegate = DirectoriesDelegate()
|
|
|
|
self.treeView.setItemDelegate(self.directoriesDelegate)
|
|
|
|
self._setupColumns()
|
2011-01-17 16:15:16 +00:00
|
|
|
self.app.recentResults.addMenu(self.menuLoadRecent)
|
|
|
|
self.app.recentResults.addMenu(self.menuRecentResults)
|
|
|
|
self.recentFolders.addMenu(self.menuRecentFolders)
|
|
|
|
self._updateAddButton()
|
2009-06-01 09:55:11 +00:00
|
|
|
self._updateRemoveButton()
|
2011-01-17 16:15:16 +00:00
|
|
|
self._updateLoadResultsButton()
|
|
|
|
self._setupBindings()
|
|
|
|
|
|
|
|
def _setupBindings(self):
|
2011-01-15 15:29:35 +00:00
|
|
|
self.scanButton.clicked.connect(self.scanButtonClicked)
|
2011-01-17 16:15:16 +00:00
|
|
|
self.loadResultsButton.clicked.connect(self.actionLoadResults.trigger)
|
|
|
|
self.addFolderButton.clicked.connect(self.actionAddFolder.trigger)
|
|
|
|
self.removeFolderButton.clicked.connect(self.removeFolderButtonClicked)
|
2011-01-15 15:29:35 +00:00
|
|
|
self.treeView.selectionModel().selectionChanged.connect(self.selectionChanged)
|
2011-01-17 16:15:16 +00:00
|
|
|
self.app.recentResults.itemsChanged.connect(self._updateLoadResultsButton)
|
|
|
|
self.recentFolders.itemsChanged.connect(self._updateAddButton)
|
2011-09-20 19:06:29 +00:00
|
|
|
self.recentFolders.mustOpenItem.connect(self.app.model.add_directory)
|
2011-01-27 09:27:17 +00:00
|
|
|
self.directoriesModel.foldersAdded.connect(self.directoriesModelAddedFolders)
|
2010-08-15 10:27:15 +00:00
|
|
|
self.app.willSavePrefs.connect(self.appWillSavePrefs)
|
2011-01-17 16:15:16 +00:00
|
|
|
|
2011-01-15 15:29:35 +00:00
|
|
|
def _setupActions(self):
|
|
|
|
# (name, shortcut, icon, desc, func)
|
|
|
|
ACTIONS = [
|
2011-01-21 12:57:54 +00:00
|
|
|
('actionLoadResults', 'Ctrl+L', '', tr("Load Results..."), self.loadResultsTriggered),
|
|
|
|
('actionShowResultsWindow', '', '', tr("Results Window"), self.app.showResultsWindow),
|
|
|
|
('actionAddFolder', '', '', tr("Add Folder..."), self.addFolderTriggered),
|
2011-01-15 15:29:35 +00:00
|
|
|
]
|
|
|
|
createActions(ACTIONS, self)
|
|
|
|
|
|
|
|
def _setupMenu(self):
|
|
|
|
self.menubar = QMenuBar(self)
|
|
|
|
self.menubar.setGeometry(QRect(0, 0, 42, 22))
|
|
|
|
self.menuFile = QMenu(self.menubar)
|
2011-01-21 12:57:54 +00:00
|
|
|
self.menuFile.setTitle(tr("File"))
|
2011-01-15 15:29:35 +00:00
|
|
|
self.menuView = QMenu(self.menubar)
|
2011-01-21 12:57:54 +00:00
|
|
|
self.menuView.setTitle(tr("View"))
|
2011-01-15 15:29:35 +00:00
|
|
|
self.menuHelp = QMenu(self.menubar)
|
2011-01-21 12:57:54 +00:00
|
|
|
self.menuHelp.setTitle(tr("Help"))
|
2011-01-15 15:29:35 +00:00
|
|
|
self.menuLoadRecent = QMenu(self.menuFile)
|
2011-01-21 12:57:54 +00:00
|
|
|
self.menuLoadRecent.setTitle(tr("Load Recent Results"))
|
2011-01-15 15:29:35 +00:00
|
|
|
self.setMenuBar(self.menubar)
|
|
|
|
|
|
|
|
self.menuFile.addAction(self.actionLoadResults)
|
|
|
|
self.menuFile.addAction(self.menuLoadRecent.menuAction())
|
|
|
|
self.menuFile.addSeparator()
|
|
|
|
self.menuFile.addAction(self.app.actionQuit)
|
|
|
|
self.menuView.addAction(self.app.actionPreferences)
|
|
|
|
self.menuView.addAction(self.actionShowResultsWindow)
|
2012-03-14 16:47:21 +00:00
|
|
|
self.menuView.addAction(self.app.actionIgnoreList)
|
2011-01-15 15:29:35 +00:00
|
|
|
self.menuHelp.addAction(self.app.actionShowHelp)
|
|
|
|
self.menuHelp.addAction(self.app.actionRegister)
|
|
|
|
self.menuHelp.addAction(self.app.actionCheckForUpdate)
|
|
|
|
self.menuHelp.addAction(self.app.actionOpenDebugLog)
|
|
|
|
self.menuHelp.addAction(self.app.actionAbout)
|
|
|
|
|
|
|
|
self.menubar.addAction(self.menuFile.menuAction())
|
|
|
|
self.menubar.addAction(self.menuView.menuAction())
|
|
|
|
self.menubar.addAction(self.menuHelp.menuAction())
|
2011-01-17 16:15:16 +00:00
|
|
|
|
|
|
|
# Recent folders menu
|
|
|
|
self.menuRecentFolders = QMenu()
|
|
|
|
self.menuRecentFolders.addAction(self.actionAddFolder)
|
|
|
|
self.menuRecentFolders.addSeparator()
|
|
|
|
|
|
|
|
# Recent results menu
|
|
|
|
self.menuRecentResults = QMenu()
|
|
|
|
self.menuRecentResults.addAction(self.actionLoadResults)
|
|
|
|
self.menuRecentResults.addSeparator()
|
2011-01-15 15:29:35 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def _setupUi(self):
|
2011-01-15 15:29:35 +00:00
|
|
|
self.setWindowTitle(self.app.NAME)
|
2010-10-04 13:29:00 +00:00
|
|
|
self.resize(420, 338)
|
2011-01-15 15:29:35 +00:00
|
|
|
self.centralwidget = QWidget(self)
|
|
|
|
self.verticalLayout = QVBoxLayout(self.centralwidget)
|
2011-11-04 18:37:07 +00:00
|
|
|
self.promptLabel = QLabel(tr("Select folders to scan and press \"Scan\"."), self.centralwidget)
|
2011-01-23 10:24:33 +00:00
|
|
|
self.verticalLayout.addWidget(self.promptLabel)
|
2011-01-15 15:29:35 +00:00
|
|
|
self.treeView = QTreeView(self.centralwidget)
|
2011-11-28 20:25:18 +00:00
|
|
|
self.treeView.setSelectionMode(QAbstractItemView.ExtendedSelection)
|
|
|
|
self.treeView.setSelectionBehavior(QAbstractItemView.SelectRows)
|
2010-10-04 13:29:00 +00:00
|
|
|
self.treeView.setAcceptDrops(True)
|
2011-01-15 15:29:35 +00:00
|
|
|
triggers = QAbstractItemView.DoubleClicked|QAbstractItemView.EditKeyPressed\
|
|
|
|
|QAbstractItemView.SelectedClicked
|
|
|
|
self.treeView.setEditTriggers(triggers)
|
2010-10-04 13:29:00 +00:00
|
|
|
self.treeView.setDragDropOverwriteMode(True)
|
|
|
|
self.treeView.setDragDropMode(QAbstractItemView.DropOnly)
|
|
|
|
self.treeView.setUniformRowHeights(True)
|
|
|
|
self.verticalLayout.addWidget(self.treeView)
|
|
|
|
self.horizontalLayout = QHBoxLayout()
|
2011-01-18 10:07:56 +00:00
|
|
|
self.removeFolderButton = QPushButton(self.centralwidget)
|
|
|
|
self.removeFolderButton.setIcon(QIcon(QPixmap(":/minus")))
|
2011-01-17 16:15:16 +00:00
|
|
|
self.removeFolderButton.setShortcut("Del")
|
|
|
|
self.horizontalLayout.addWidget(self.removeFolderButton)
|
2011-01-18 10:07:56 +00:00
|
|
|
self.addFolderButton = QPushButton(self.centralwidget)
|
|
|
|
self.addFolderButton.setIcon(QIcon(QPixmap(":/plus")))
|
2011-01-17 16:15:16 +00:00
|
|
|
self.horizontalLayout.addWidget(self.addFolderButton)
|
|
|
|
spacerItem1 = QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)
|
2010-10-04 13:29:00 +00:00
|
|
|
self.horizontalLayout.addItem(spacerItem1)
|
2011-01-17 16:15:16 +00:00
|
|
|
self.loadResultsButton = QPushButton(self.centralwidget)
|
2011-01-21 12:57:54 +00:00
|
|
|
self.loadResultsButton.setText(tr("Load Results"))
|
2011-01-17 16:15:16 +00:00
|
|
|
self.horizontalLayout.addWidget(self.loadResultsButton)
|
2011-01-15 15:29:35 +00:00
|
|
|
self.scanButton = QPushButton(self.centralwidget)
|
2011-01-21 12:57:54 +00:00
|
|
|
self.scanButton.setText(tr("Scan"))
|
2011-01-15 15:29:35 +00:00
|
|
|
self.scanButton.setDefault(True)
|
|
|
|
self.horizontalLayout.addWidget(self.scanButton)
|
2010-10-04 13:29:00 +00:00
|
|
|
self.verticalLayout.addLayout(self.horizontalLayout)
|
2011-01-15 15:29:35 +00:00
|
|
|
self.setCentralWidget(self.centralwidget)
|
|
|
|
|
|
|
|
self._setupActions()
|
|
|
|
self._setupMenu()
|
2011-01-17 16:15:16 +00:00
|
|
|
|
2010-08-15 10:27:15 +00:00
|
|
|
if self.app.prefs.directoriesWindowRect is not None:
|
|
|
|
self.setGeometry(self.app.prefs.directoriesWindowRect)
|
2011-02-01 10:43:16 +00:00
|
|
|
else:
|
|
|
|
moveToScreenCenter(self)
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2011-11-28 19:21:10 +00:00
|
|
|
def _setupColumns(self):
|
|
|
|
header = self.treeView.header()
|
|
|
|
header.setStretchLastSection(False)
|
|
|
|
header.setResizeMode(0, QHeaderView.Stretch)
|
|
|
|
header.setResizeMode(1, QHeaderView.Fixed)
|
|
|
|
header.resizeSection(1, 100)
|
|
|
|
|
2011-01-17 16:15:16 +00:00
|
|
|
def _updateAddButton(self):
|
|
|
|
if self.recentFolders.isEmpty():
|
|
|
|
self.addFolderButton.setMenu(None)
|
|
|
|
else:
|
|
|
|
self.addFolderButton.setMenu(self.menuRecentFolders)
|
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def _updateRemoveButton(self):
|
|
|
|
indexes = self.treeView.selectedIndexes()
|
|
|
|
if not indexes:
|
2011-01-17 16:15:16 +00:00
|
|
|
self.removeFolderButton.setEnabled(False)
|
2009-06-01 09:55:11 +00:00
|
|
|
return
|
2011-01-17 16:15:16 +00:00
|
|
|
self.removeFolderButton.setEnabled(True)
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2011-01-17 16:15:16 +00:00
|
|
|
def _updateLoadResultsButton(self):
|
|
|
|
if self.app.recentResults.isEmpty():
|
|
|
|
self.loadResultsButton.setMenu(None)
|
|
|
|
else:
|
|
|
|
self.loadResultsButton.setMenu(self.menuRecentResults)
|
|
|
|
|
2011-01-15 15:29:35 +00:00
|
|
|
#--- QWidget overrides
|
|
|
|
def closeEvent(self, event):
|
|
|
|
event.accept()
|
2011-09-20 19:06:29 +00:00
|
|
|
if self.app.model.results.is_modified:
|
2011-01-21 12:57:54 +00:00
|
|
|
title = tr("Unsaved results")
|
2011-11-04 17:10:11 +00:00
|
|
|
msg = tr("You have unsaved results, do you really want to quit?")
|
2011-01-15 15:29:35 +00:00
|
|
|
if not self.app.confirm(title, msg):
|
|
|
|
event.ignore()
|
|
|
|
if event.isAccepted():
|
|
|
|
QApplication.quit()
|
|
|
|
|
2010-08-15 10:27:15 +00:00
|
|
|
#--- Events
|
2011-01-17 16:15:16 +00:00
|
|
|
def addFolderTriggered(self):
|
2011-11-04 17:10:11 +00:00
|
|
|
title = tr("Select a folder to add to the scanning list")
|
2009-06-01 09:55:11 +00:00
|
|
|
flags = QFileDialog.ShowDirsOnly
|
2010-08-11 14:39:06 +00:00
|
|
|
dirpath = str(QFileDialog.getExistingDirectory(self, title, self.lastAddedFolder, flags))
|
2009-06-01 09:55:11 +00:00
|
|
|
if not dirpath:
|
|
|
|
return
|
2009-10-14 13:29:46 +00:00
|
|
|
self.lastAddedFolder = dirpath
|
2011-09-20 19:06:29 +00:00
|
|
|
self.app.model.add_directory(dirpath)
|
2011-01-17 16:15:16 +00:00
|
|
|
self.recentFolders.insertItem(dirpath)
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2010-08-15 10:27:15 +00:00
|
|
|
def appWillSavePrefs(self):
|
|
|
|
self.app.prefs.directoriesWindowRect = self.geometry()
|
|
|
|
|
2011-01-27 09:27:17 +00:00
|
|
|
def directoriesModelAddedFolders(self, folders):
|
|
|
|
for folder in folders:
|
|
|
|
self.recentFolders.insertItem(folder)
|
|
|
|
|
2011-01-15 15:29:35 +00:00
|
|
|
def loadResultsTriggered(self):
|
2011-11-04 17:10:11 +00:00
|
|
|
title = tr("Select a results file to load")
|
2011-05-29 13:12:24 +00:00
|
|
|
files = ';;'.join([tr("dupeGuru Results (*.dupeguru)"), tr("All Files (*.*)")])
|
2011-01-15 15:29:35 +00:00
|
|
|
destination = QFileDialog.getOpenFileName(self, title, '', files)
|
|
|
|
if destination:
|
2011-09-20 19:06:29 +00:00
|
|
|
self.app.model.load_from(destination)
|
2011-01-15 15:29:35 +00:00
|
|
|
self.app.recentResults.insertItem(destination)
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2011-01-17 16:15:16 +00:00
|
|
|
def removeFolderButtonClicked(self):
|
2011-11-28 19:52:48 +00:00
|
|
|
self.directoriesModel.model.remove_selected()
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2011-01-15 15:29:35 +00:00
|
|
|
def scanButtonClicked(self):
|
2011-09-20 19:06:29 +00:00
|
|
|
if self.app.model.results.is_modified:
|
2011-09-22 14:35:17 +00:00
|
|
|
title = tr("Start a new scan")
|
2011-11-04 17:10:11 +00:00
|
|
|
msg = tr("You have unsaved results, do you really want to continue?")
|
2011-01-15 15:29:35 +00:00
|
|
|
if not self.app.confirm(title, msg):
|
|
|
|
return
|
2011-09-22 14:35:17 +00:00
|
|
|
self.app.model.start_scanning()
|
2011-01-15 15:29:35 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def selectionChanged(self, selected, deselected):
|
|
|
|
self._updateRemoveButton()
|
|
|
|
|
2010-10-04 13:29:00 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys
|
2011-01-23 10:24:33 +00:00
|
|
|
from . import dg_rc
|
2010-10-04 13:29:00 +00:00
|
|
|
from ..testapp import TestApp
|
|
|
|
app = QApplication([])
|
|
|
|
dgapp = TestApp()
|
|
|
|
dialog = DirectoriesDialog(None, dgapp)
|
|
|
|
dialog.show()
|
|
|
|
sys.exit(app.exec_())
|