def __init__(self): super(MainAppWindow, self).__init__() self.setupUi(self) self.kindle_path = '' self.savedPath = '' self.convertRun = False self.convertedCount = 0 self.copyCount = 0 self.convertedFiles = [] # self.setAcceptDrops(True) self.treeFileList.setAcceptDrops(True) self.treeFileList.installEventFilter(self) self.imgBookCover.setAcceptDrops(True) self.imgBookCover.installEventFilter(self) self.book_cover = None self.config_file = None self.log_file = None self.log = None self.config = {} self.convert_worker = None self.copy_worker = None self.is_convert_cancel = False self.rootFileList = self.treeFileList.invisibleRootItem() self.iconWhite = QIcon(':/Images/bullet_white.png') self.iconRed = QIcon(':/Images/bullet_red.png') self.iconGreen = QIcon(':/Images/bullet_green.png') self.iconGo = QIcon(':/Images/bullet_go.png') self.pixmapConnected = QPixmap(':/Images/bullet_green.png') self.pixmapNotConnected = QPixmap(':/Images/bullet_red.png') config_file_name = "fb2mobi.config" log_file_name = "fb2mobi.log" gui_config_file = 'fb2mobi-gui.config' # Определяем, где находится файл конфигурации. # Если есть в домашнем каталоге пользователя (для windows ~/f2bmobi, для остальных ~/.fb2mobi), # то используется он. # Иначе - из каталога программы application_path = os.path.normpath(fb2mobi.get_executable_path()) config_path = None if sys.platform == 'win32': config_path = os.path.normpath( os.path.join(os.path.expanduser('~'), 'fb2mobi')) else: config_path = os.path.normpath( os.path.join(os.path.expanduser('~'), '.fb2mobi')) if not os.path.exists(os.path.join(config_path, config_file_name)): config_path = application_path self.config_file = os.path.normpath( os.path.join(config_path, config_file_name)) self.log_file = os.path.normpath( os.path.join(config_path, log_file_name)) self.gui_config_file = os.path.normpath( os.path.join(config_path, gui_config_file)) self.gui_config = GuiConfig(self.gui_config_file) self.gui_config.converterConfig = ConverterConfig(self.config_file) self.log = logging.getLogger('fb2mobi') self.log.setLevel(self.gui_config.logLevel) self.log_stream_handler = logging.StreamHandler() self.log_stream_handler.setLevel( fb2mobi.get_log_level( self.gui_config.converterConfig.console_level)) self.log_stream_handler.setFormatter( logging.Formatter('%(levelname)s: %(message)s')) self.log.addHandler(self.log_stream_handler) self.log_file_handler = logging.FileHandler(filename=self.log_file, mode='a', encoding='utf-8') self.log_file_handler.setLevel( fb2mobi.get_log_level(self.gui_config.converterConfig.log_level)) self.log_file_handler.setFormatter( logging.Formatter('[%(asctime)s] %(levelname)s: %(message)s')) if self.gui_config.writeLog: self.log.addHandler(self.log_file_handler) self.gui_config.converterConfig.log = self.log # Строим базу доступных шрифтов self.font_path = os.path.normpath( os.path.join(config_path, 'profiles/fonts')) if os.path.exists(self.font_path): self.gui_config.fontDb = FontDb(self.font_path) if not self.gui_config.lastUsedTargetPath: self.gui_config.lastUsedTargetPath = os.path.abspath( os.path.expanduser("~/Desktop")) if not self.gui_config.currentFormat: self.gui_config.currentFormat = 'mobi' if not self.gui_config.currentProfile: for p in self.gui_config.converterConfig.profiles: self.gui_config.currentProfile = p break if not self.gui_config.hyphens: self.gui_config.hyphens = 'profile' if self.gui_config.geometry['x'] and self.gui_config.geometry['y']: self.move(self.gui_config.geometry['x'], self.gui_config.geometry['y']) self.resize(self.gui_config.geometry['width'], self.gui_config.geometry['height']) self.setWindowIcon(QIcon(':/Images/icon32.png')) self.treeFileList.installEventFilter(self) self.bookInfoSplitter.installEventFilter(self) self.labelStatus = QLabel() self.imgBookCover.setContextMenuPolicy(Qt.CustomContextMenu) self.imgBookCover.customContextMenuRequested[QPoint].connect( self.contextCoverMenu) self.toolBar.setIconSize(QSize(26, 26)) self.toolAdd.setIcon(QIcon(':/toolbar/add.png')) self.toolSaveToDisk.setIcon(QIcon(':/toolbar/save.png')) self.toolSendToKindle.setIcon(QIcon(':/toolbar/kindle.png')) self.toolSendToKindle.setEnabled(False) self.toolSendMail.setIcon(QIcon(':/toolbar/send.png')) self.toolSettings.setIcon(QIcon(':/toolbar/settings.png')) self.toolInfo.setIcon(QIcon(':/toolbar/info_on.png')) # Немного подстраиваем стили UI для более нативного отображения if sys.platform == 'darwin': # Для Mac OS X font = self.labelStatus.font() font.setPointSize(11) self.labelStatus.setFont(font) self.treeFileList.setFont(font) self.labelAuthor.setFont(font) self.labelBookTitle.setFont(font) self.labelSeries.setFont(font) self.labelSeriesNumber.setFont(font) self.labelBookLanguage.setFont(font) self.treeFileList.setAttribute(Qt.WA_MacShowFocusRect, 0) self.treeFileList.setStyleSheet(TREE_LIST_CSS_ACTIVE) self.labelStatus.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored) self.toolBar.setToolButtonStyle(Qt.ToolButtonTextUnderIcon) self.setUnifiedTitleAndToolBarOnMac(True) spacer = QWidget() spacer.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self.toolBar.addAction(self.toolAdd) self.toolBar.addWidget(spacer) self.toolBar.addAction(self.toolSaveToDisk) self.toolBar.addAction(self.toolSendToKindle) self.toolBar.addAction(self.toolSendMail) spacer = QWidget() spacer.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self.toolBar.addWidget(spacer) self.toolBar.addAction(self.toolInfo) self.toolBar.addAction(self.toolSettings) else: # Для Windows, Linux self.toolBar.setToolButtonStyle(Qt.ToolButtonTextBesideIcon) self.toolBar.setStyleSheet('QToolButton { padding: 4px; }') spacer = QWidget() self.toolBar.addAction(self.toolAdd) self.toolBar.addAction(self.toolSaveToDisk) self.toolBar.addAction(self.toolSendToKindle) self.toolBar.addAction(self.toolSendMail) self.toolBar.addAction(self.toolSettings) spacer = QWidget() spacer.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self.toolBar.addWidget(spacer) self.toolBar.addAction(self.toolInfo) self.toolInfo.setPriority(QAction.LowPriority) self.setBookInfoPanelVisible() self.scrollBookInfo.setVisible(self.gui_config.bookInfoVisible) if self.gui_config.bookInfoSplitterState: splitter_sizes = self.gui_config.bookInfoSplitterState.split(',') self.bookInfoSplitter.setSizes([int(i) for i in splitter_sizes]) self.statusBar().addWidget(self.labelStatus, 1) if self.gui_config.columns['0']: self.treeFileList.setColumnWidth(0, self.gui_config.columns['0']) self.treeFileList.setColumnWidth(1, self.gui_config.columns['1']) self.treeFileList.setColumnWidth(2, self.gui_config.columns['2']) self.timerKindleStatus = QTimer() self.timerKindleStatus.timeout.connect(self.checkKindleStatus) self.timerKindleStatus.start(1500) self.enableSendViaMail()
def __init__(self, app_win): super(AppEventFilter, self).__init__() self.app_win = app_win def eventFilter(self, receiver, event): if event.type() == QEvent.FileOpen: self.app_win.addFiles([event.file()]) return True else: return super(AppEventFilter, self).eventFilter(receiver, event) if __name__ == '__main__': app = QApplication(sys.argv) app_path = os.path.normpath(fb2mobi.get_executable_path()) locale_path = os.path.join(app_path, 'ui/locale') locale = QLocale.system().name()[:2] qt_translator = QTranslator() qt_translator.load(os.path.join(locale_path, 'qtbase_' + locale + '.qm')) app.installTranslator(qt_translator) app_translator = QTranslator() app_translator.load(os.path.join(locale_path, 'fb2mobi_' + locale + '.qm')) app.installTranslator(app_translator) app.setStyleSheet('QStatusBar::item { border: 0px }') mainAppWindow = MainAppWindow() mainAppWindow.show()