def __init__(self): super(Main, self).__init__() if not os.path.exists(dbPath): os.mkdir(dbPath) if os.path.exists(os.path.join(dbPath[:-3], u'db.pkl')): pass # TODO: convert old db to new else: dialog = Settings() dialog.exec_() self.db = DB(dbPath) self.db.finished.connect(self.enableButtons) self.db.artistsStatisticsChanged.connect(self.updateArtistsStatistics) self.db.albumsStatisticsChanged.connect(self.updateAlbumsStatistics) from interfaces.main import Ui_main self.ui = Ui_main() widget = QtGui.QWidget() self.ui.setupUi(widget) self.ui.artists = View(self.db.artists, TableView( self.__settings.value(u'artistsView').toByteArray() ), Main.__dbsettings.value( u'image/artist/enabled', 2 ).toBool(), self.ui.splitter) delegate = ADRItemDelegate() self.ui.artists.view.setItemDelegateForColumn(0, delegate) self.ui.albums = View(self.db.albums, ADRTableView( self.__settings.value(u'albumsView').toByteArray() ), Main.__dbsettings.value( u'image/album/enabled', 2 ).toBool(), self.ui.splitter) self.ui.tracks = View(self.db.tracks, TableView( self.__settings.value(u'tracksView').toByteArray() ), None, self.ui.splitter) self.ui.tracks.view.setAlternatingRowColors(True) self.ui.artists.view.selectionModel().selectionChanged.connect( self.ui.albums.model.setSelection ) self.ui.albums.view.selectionModel().selectionChanged.connect( self.ui.tracks.model.setSelection ) self.ui.plugins = {} self.ui.splitter.restoreState( self.__settings.value(u'splitters').toByteArray() ) self.setCentralWidget(widget) self.rt = Distributor(self.db.iterator()) self.rt.stepped.connect(self.statusBar().showMessage) self.rt.finished.connect(self.enableButtons) self.ui.local.clicked.connect(self.disableButtons) self.ui.local.clicked.connect(self.db.start) self.ui.remote.clicked.connect(self.disableButtons) self.ui.remote.clicked.connect(self.rt.start) self.ui.close.clicked.connect(self.close) self.ui.save.clicked.connect(self.save) self.ui.settings.clicked.connect(self.showSettings) self.statusBar() self.setWindowTitle(u'gayeogi ' + __version__) self.translators = list() self.loadPluginsTranslators() self.loadPlugins()
class Main(QtGui.QMainWindow): __settings = QSettings(u'gayeogi', u'gayeogi') __dbsettings = QSettings(u'gayeogi', u'Databases') def __init__(self): super(Main, self).__init__() if not os.path.exists(dbPath): os.mkdir(dbPath) if os.path.exists(os.path.join(dbPath[:-3], u'db.pkl')): pass # TODO: convert old db to new else: dialog = Settings() dialog.exec_() self.db = DB(dbPath) self.db.finished.connect(self.enableButtons) self.db.artistsStatisticsChanged.connect(self.updateArtistsStatistics) self.db.albumsStatisticsChanged.connect(self.updateAlbumsStatistics) from interfaces.main import Ui_main self.ui = Ui_main() widget = QtGui.QWidget() self.ui.setupUi(widget) self.ui.artists = View(self.db.artists, TableView( self.__settings.value(u'artistsView').toByteArray() ), Main.__dbsettings.value( u'image/artist/enabled', 2 ).toBool(), self.ui.splitter) delegate = ADRItemDelegate() self.ui.artists.view.setItemDelegateForColumn(0, delegate) self.ui.albums = View(self.db.albums, ADRTableView( self.__settings.value(u'albumsView').toByteArray() ), Main.__dbsettings.value( u'image/album/enabled', 2 ).toBool(), self.ui.splitter) self.ui.tracks = View(self.db.tracks, TableView( self.__settings.value(u'tracksView').toByteArray() ), None, self.ui.splitter) self.ui.tracks.view.setAlternatingRowColors(True) self.ui.artists.view.selectionModel().selectionChanged.connect( self.ui.albums.model.setSelection ) self.ui.albums.view.selectionModel().selectionChanged.connect( self.ui.tracks.model.setSelection ) self.ui.plugins = {} self.ui.splitter.restoreState( self.__settings.value(u'splitters').toByteArray() ) self.setCentralWidget(widget) self.rt = Distributor(self.db.iterator()) self.rt.stepped.connect(self.statusBar().showMessage) self.rt.finished.connect(self.enableButtons) self.ui.local.clicked.connect(self.disableButtons) self.ui.local.clicked.connect(self.db.start) self.ui.remote.clicked.connect(self.disableButtons) self.ui.remote.clicked.connect(self.rt.start) self.ui.close.clicked.connect(self.close) self.ui.save.clicked.connect(self.save) self.ui.settings.clicked.connect(self.showSettings) self.statusBar() self.setWindowTitle(u'gayeogi ' + __version__) self.translators = list() self.loadPluginsTranslators() self.loadPlugins() def disableButtons(self): """Disable some buttons one mustn't use during the update.""" self.ui.local.setDisabled(True) self.ui.remote.setDisabled(True) self.ui.save.setDisabled(True) self.ui.settings.setDisabled(True) def enableButtons(self): """Enable buttons disabled by Main.disableButtons. Also shows the "Done" message. """ self.ui.local.setEnabled(True) self.ui.remote.setEnabled(True) self.ui.save.setEnabled(True) self.ui.settings.setEnabled(True) self.statusBar().showMessage(self.trUtf8('Done')) def loadPluginsTranslators(self): reload(gayeogi.plugins) app = QtGui.QApplication.instance() for plugin in gayeogi.plugins.__all__: translator = QTranslator() if translator.load(plugin + u'_' + locale, os.path.join(lnPath, u'plugins', u'langs')): self.translators.append(translator) app.installTranslator(translator) def removePluginsTranslators(self): app = QtGui.QApplication.instance() for translator in self.translators: app.removeTranslator(translator) def loadPlugins(self): def depends(plugin): for p in gayeogi.plugins.__all__: class_ = getattr(gayeogi.plugins, p).Main if plugin in class_.depends and class_.loaded: return True return False for plugin in gayeogi.plugins.__all__: class_ = getattr(gayeogi.plugins, plugin).Main __settings_ = QSettings(u'gayeogi', class_.name) option = __settings_.value(u'enabled', 0).toInt()[0] if option and not class_.loaded: class__ = class_(self.ui, self.db.artists, self.appendPlugin, self.removePlugin) class__.load() self.ui.plugins[plugin] = class__ elif not option and class_.loaded: self.ui.plugins[plugin].unload() for d in self.ui.plugins[plugin].depends: if not self.ui.plugins[d].loaded \ and d in self.ui.plugins.keys(): del self.ui.plugins[d] if not depends(plugin): del self.ui.plugins[plugin] def appendPlugin(self, parent, child, position): parent = getattr(self.ui, parent) if position == 'start': position = 0 elif position == 'end': position = len(parent.parent().children()) - 7 if isinstance(parent, QtGui.QLayout): widget = parent.itemAt(position) if not widget: parent.insertWidget(position, child) else: if isinstance(widget, QtGui.QTabWidget): widget.addTab(child, child.name) else: try: widget.name except AttributeError: parent.insertWidget(position, child) else: widget = parent.takeAt(position).widget() tab = QtGui.QTabWidget() tab.setTabPosition(tab.South) tab.addTab(widget, widget.name) tab.addTab(child, child.name) parent.insertWidget(position, tab) def removePlugin(self, parent, child, position): parent = getattr(self.ui, parent) if position == 'start': position = 0 elif position == 'end': position = len(parent.parent().children()) - 8 if isinstance(parent, QtGui.QLayout): widget = parent.itemAt(position).widget() try: if widget.name == child.name: parent.takeAt(position).widget().deleteLater() except AttributeError: for i in range(widget.count()): if widget.widget(i).name == child.name: widget.removeTab(i) if widget.count() == 1: tmp = widget.widget(0) parent.takeAt(position).widget().deleteLater() parent.insertWidget(position, tmp) parent.itemAt(position).widget().show() def showSettings(self): u"""Show settings dialog and then update accordingly.""" def __save(): self.removePluginsTranslators() self.loadPluginsTranslators() self.loadPlugins() dialog = Settings() dialog.ok.clicked.connect(__save) dialog.exec_() def save(self): u"""Save database to file.""" self.db.save() self.statusBar().showMessage(self.trUtf8('Saved')) def updateArtistsStatistics(self, a, d, r): """Updates global artists' statistics. :a: A statistics. :d: D statistics. :r: R statistics. """ self.ui.artistsGreen.setText(unicode(a)) self.ui.artistsYellow.setText(unicode(d)) self.ui.artistsRed.setText(unicode(r)) def updateAlbumsStatistics(self, a, d, r): """Updated global albums' statistics. @note: Attributes as in Main.updateArtistsStatistics. """ self.ui.albumsGreen.setText(unicode(a)) self.ui.albumsYellow.setText(unicode(d)) self.ui.albumsRed.setText(unicode(r)) def closeEvent(self, event): def unload(): for plugin in self.ui.plugins.values(): plugin.unload() self.__settings.setValue( u'splitters', self.ui.splitter.saveState() ) self.__settings.setValue(u'artistsView', self.ui.artists.view.horizontalHeader().saveState() ) self.__settings.setValue(u'albumsView', self.ui.albums.view.horizontalHeader().saveState() ) self.__settings.setValue(u'tracksView', self.ui.tracks.view.horizontalHeader().saveState() ) if self.db.modified: from interfaces.confirmation import ConfirmationDialog dialog = ConfirmationDialog() dialog.buttons.accepted.connect(self.save) dialog.buttons.accepted.connect(unload) dialog.buttons.rejected.connect(event.ignore) dialog.buttons.helpRequested.connect(unload) dialog.exec_() else: unload()