2009-06-01 09:55:11 +00:00
|
|
|
# Created By: Virgil Dupras
|
|
|
|
# Created On: 2009-04-25
|
2010-01-01 20:11:34 +00:00
|
|
|
# Copyright 2010 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,
|
|
|
|
QAbstractItemView, QSpacerItem, QSizePolicy, QPushButton, QApplication, QMessageBox, QMainWindow,
|
2011-01-23 10:24:33 +00:00
|
|
|
QMenuBar, QMenu, QIcon, QPixmap, QLabel)
|
2011-01-15 15:29:35 +00:00
|
|
|
|
2011-01-21 12:57:54 +00:00
|
|
|
from hscommon.trans import tr, trmsg
|
2011-01-17 16:15:16 +00:00
|
|
|
from qtlib.recent import Recent
|
2011-02-01 10:43:16 +00:00
|
|
|
from qtlib.util import moveToScreenCenter
|
2011-01-15 15:29:35 +00:00
|
|
|
from core.app import NoScannableFileError
|
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
|
2011-01-15 15:29:35 +00:00
|
|
|
from .util import createActions
|
2009-06-01 09:55:11 +00:00
|
|
|
|
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')
|
2010-10-04 13:29:00 +00:00
|
|
|
self.directoriesModel = DirectoriesModel(self.app)
|
|
|
|
self.directoriesDelegate = DirectoriesDelegate()
|
2009-06-01 09:55:11 +00:00
|
|
|
self._setupUi()
|
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)
|
|
|
|
self.recentFolders.mustOpenItem.connect(self.app.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)
|
|
|
|
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-01-23 10:24:33 +00:00
|
|
|
self.promptLabel = QLabel(trmsg("SelectFolderToScanMsg"), self.centralwidget)
|
|
|
|
self.verticalLayout.addWidget(self.promptLabel)
|
2011-01-15 15:29:35 +00:00
|
|
|
self.treeView = QTreeView(self.centralwidget)
|
2009-06-01 09:55:11 +00:00
|
|
|
self.treeView.setItemDelegate(self.directoriesDelegate)
|
|
|
|
self.treeView.setModel(self.directoriesModel)
|
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)
|
2009-06-01 09:55:11 +00:00
|
|
|
header = self.treeView.header()
|
|
|
|
header.setStretchLastSection(False)
|
|
|
|
header.setResizeMode(0, QHeaderView.Stretch)
|
|
|
|
header.setResizeMode(1, QHeaderView.Fixed)
|
|
|
|
header.resizeSection(1, 100)
|
2010-10-04 13:29:00 +00:00
|
|
|
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-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()
|
|
|
|
if self.app.results.is_modified:
|
2011-01-21 12:57:54 +00:00
|
|
|
title = tr("Unsaved results")
|
|
|
|
msg = trmsg("ReallyWantToQuitMsg")
|
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-01-21 12:57:54 +00:00
|
|
|
title = trmsg("SelectFolderToAddMsg")
|
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
|
2009-06-07 07:11:52 +00:00
|
|
|
self.app.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-01-21 12:57:54 +00:00
|
|
|
title = trmsg("SelectResultToLoadMsg")
|
|
|
|
files = tr("dupeGuru Results (*.dupeguru)")
|
2011-01-15 15:29:35 +00:00
|
|
|
destination = QFileDialog.getOpenFileName(self, title, '', files)
|
|
|
|
if destination:
|
|
|
|
self.app.load_from(destination)
|
|
|
|
self.app.recentResults.insertItem(destination)
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2011-01-17 16:15:16 +00:00
|
|
|
def removeFolderButtonClicked(self):
|
2009-06-01 09:55:11 +00:00
|
|
|
indexes = self.treeView.selectedIndexes()
|
|
|
|
if not indexes:
|
|
|
|
return
|
|
|
|
index = indexes[0]
|
|
|
|
node = index.internalPointer()
|
|
|
|
if node.parent is None:
|
|
|
|
row = index.row()
|
2010-02-07 15:00:58 +00:00
|
|
|
self.app.remove_directory(row)
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2011-01-15 15:29:35 +00:00
|
|
|
def scanButtonClicked(self):
|
2011-01-21 12:57:54 +00:00
|
|
|
title = tr("Start a new scan")
|
2011-01-22 15:12:18 +00:00
|
|
|
if self.app.results.is_modified:
|
2011-01-21 12:57:54 +00:00
|
|
|
msg = trmsg("ReallyWantToContinueMsg")
|
2011-01-15 15:29:35 +00:00
|
|
|
if not self.app.confirm(title, msg):
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
self.app.start_scanning()
|
|
|
|
except NoScannableFileError:
|
2011-01-21 12:57:54 +00:00
|
|
|
msg = trmsg("NoScannableFileMsg")
|
2011-01-15 15:29:35 +00:00
|
|
|
QMessageBox.warning(self, title, msg)
|
|
|
|
|
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_())
|