"Fossies" - the Fresh Open Source Software Archive

Member "ffmulticonverter-1.8.0/ffmulticonverter/preferences_dlg.py" (25 Jun 2016, 11221 Bytes) of package /linux/privat/ffmulticonverter-1.8.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "preferences_dlg.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.7.2_vs_1.8.0.

    1 # Copyright (C) 2011-2016 Ilias Stamatis <stamatis.iliass@gmail.com>
    2 #
    3 # This program is free software: you can redistribute it and/or modify
    4 # it under the terms of the GNU General Public License as published by
    5 # the Free Software Foundation, either version 3 of the License, or
    6 # (at your option) any later version.
    7 #
    8 # This program is distributed in the hope that it will be useful,
    9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
   10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   11 # GNU General Public License for more details.
   12 #
   13 # You should have received a copy of the GNU General Public License
   14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
   15 
   16 import os
   17 
   18 from PyQt5.QtCore import QSettings, QTimer
   19 from PyQt5.QtWidgets import (
   20         QDialog, QDialogButtonBox, QFileDialog, QLabel, QLineEdit,
   21         QRadioButton, QSpacerItem, QTabWidget, QToolButton, QWidget,
   22         QPlainTextEdit, QPushButton
   23         )
   24 
   25 from ffmulticonverter import utils
   26 from ffmulticonverter import config
   27 
   28 
   29 class Preferences(QDialog):
   30     def __init__(self, parent=None, test = False):
   31         super(Preferences, self).__init__(parent)
   32         self.parent = parent
   33         self.test = test
   34 
   35         saveQL = QLabel('<html><b>' + self.tr('Save files') + '</b></html>')
   36         existQL = QLabel(self.tr('Existing files:'))
   37         self.exst_prefixQRB = QRadioButton(self.tr("Add '~' prefix"))
   38         self.exst_overwriteQRB = QRadioButton(self.tr('Overwrite'))
   39         exist_layout = utils.add_to_layout(
   40                 'h', self.exst_prefixQRB, self.exst_overwriteQRB)
   41 
   42         defaultQL = QLabel(self.tr('Default output destination:'))
   43         self.defaultQLE = QLineEdit()
   44         self.defaultQTB = QToolButton()
   45         self.defaultQTB.setText('...')
   46         deafult_fol_layout = utils.add_to_layout(
   47                 'h', self.defaultQLE, self.defaultQTB)
   48         nameQL = QLabel('<html><b>' + self.tr('Name files') +'</b></html>')
   49         prefixQL = QLabel(self.tr('Prefix:'))
   50         suffixQL = QLabel(self.tr('Suffix:'))
   51         self.prefixQLE = QLineEdit()
   52         self.suffixQLE = QLineEdit()
   53         grid = utils.add_to_grid(
   54                 [prefixQL, self.prefixQLE], [suffixQL, self.suffixQLE])
   55         prefix_layout = utils.add_to_layout('h', grid, None)
   56 
   57         tabwidget1_layout = utils.add_to_layout(
   58                 'v', saveQL,
   59                 QSpacerItem(14, 13), existQL, exist_layout,
   60                 QSpacerItem(14, 13), defaultQL, deafult_fol_layout,
   61                 QSpacerItem(13, 13), nameQL, QSpacerItem(14, 13),
   62                 prefix_layout, None
   63                 )
   64 
   65         ffmpegQL = QLabel('<html><b>FFmpeg</b></html>')
   66         ffmpeg_pathQL= QLabel(self.tr('Path to executable:'))
   67         self.ffmpegpathQLE = QLineEdit()
   68 
   69         default_cmd_ffmpegQL = QLabel(self.tr('Default command:'))
   70         self.ffmpegcmdQLE = QLineEdit()
   71 
   72         vidcodecsQL = QLabel(
   73                 '<html><b>' + self.tr('Video codecs') +'</b></html>')
   74         self.vidcodecsQPTE = QPlainTextEdit()
   75         audcodecsQL = QLabel(
   76                 '<html><b>' + self.tr('Audio codecs') +'</b></html>')
   77         self.audcodecsQPTE = QPlainTextEdit()
   78         extraformatsffmpegQL = QLabel(
   79                 '<html><b>' + self.tr('Extra formats') +'</b></html>')
   80         self.extraformatsffmpegQPTE = QPlainTextEdit()
   81 
   82         gridlayout = utils.add_to_grid(
   83                 [vidcodecsQL, audcodecsQL, extraformatsffmpegQL],
   84                 [self.vidcodecsQPTE, self.audcodecsQPTE,
   85                  self.extraformatsffmpegQPTE]
   86                 )
   87 
   88         defvidcodecsQPB = QPushButton(self.tr("Default video codecs"))
   89         defaudcodecsQPB = QPushButton(self.tr("Default audio codecs"))
   90 
   91         hlayout1 = utils.add_to_layout(
   92                 'h', None, defvidcodecsQPB, defaudcodecsQPB)
   93 
   94         tabwidget2_layout = utils.add_to_layout(
   95                 'v', ffmpegQL, QSpacerItem(14, 13), ffmpeg_pathQL,
   96                 self.ffmpegpathQLE, default_cmd_ffmpegQL, self.ffmpegcmdQLE,
   97                 QSpacerItem(20, 20), gridlayout, hlayout1, None
   98                 )
   99 
  100         imagemagickQL = QLabel('<html><b>ImageMagick (convert)</b></html>')
  101         default_cmd_imageQL = QLabel(self.tr('Default options:'))
  102         self.imagecmdQLE = QLineEdit()
  103 
  104         extraformatsimageQL = QLabel(
  105                 '<html><b>' + self.tr('Extra formats') +'</b></html>')
  106         self.extraformatsimageQPTE = QPlainTextEdit()
  107 
  108         hlayout2 = utils.add_to_layout('h',
  109                 self.extraformatsimageQPTE, QSpacerItem(220,20))
  110 
  111         tabwidget3_layout = utils.add_to_layout(
  112                 'v', imagemagickQL,
  113                 QSpacerItem(14,13), default_cmd_imageQL, self.imagecmdQLE,
  114                 QSpacerItem(20,20), extraformatsimageQL, hlayout2, None
  115                 )
  116 
  117         extraformatsdocumentQL = QLabel(
  118                 '<html><b>' + self.tr('Extra formats') +'</b></html>')
  119         self.extraformatsdocumentQPTE = QPlainTextEdit()
  120 
  121         hlayout3 = utils.add_to_layout('h',
  122                 self.extraformatsdocumentQPTE, QSpacerItem(220,20))
  123 
  124         tabwidget4_layout = utils.add_to_layout(
  125                 'v', extraformatsdocumentQL, hlayout3, None)
  126 
  127         widget1 = QWidget()
  128         widget1.setLayout(tabwidget1_layout)
  129         widget2 = QWidget()
  130         widget2.setLayout(tabwidget2_layout)
  131         widget3 = QWidget()
  132         widget3.setLayout(tabwidget3_layout)
  133         widget4 = QWidget()
  134         widget4.setLayout(tabwidget4_layout)
  135         tabWidget = QTabWidget()
  136         tabWidget.addTab(widget1, self.tr('General'))
  137         tabWidget.addTab(widget2, self.tr('Audio/Video'))
  138         tabWidget.addTab(widget3, self.tr('Images'))
  139         tabWidget.addTab(widget4, self.tr('Documents'))
  140 
  141         buttonBox = QDialogButtonBox(
  142                 QDialogButtonBox.Ok|QDialogButtonBox.Cancel)
  143 
  144         final_layout = utils.add_to_layout('v', tabWidget, None, buttonBox)
  145         self.setLayout(final_layout)
  146 
  147         self.defaultQTB.clicked.connect(self.open_dir)
  148         buttonBox.accepted.connect(self.save_settings)
  149         buttonBox.rejected.connect(self.reject)
  150         defvidcodecsQPB.clicked.connect(
  151                 lambda: self.set_videocodecs(config.video_codecs))
  152         defaudcodecsQPB.clicked.connect(
  153                 lambda: self.set_audiocodecs(config.audio_codecs))
  154 
  155         self.resize(400, 450)
  156         self.setWindowTitle(self.tr('Preferences'))
  157 
  158         QTimer.singleShot(0, self.load_settings)
  159 
  160     def load_settings(self):
  161         """Load settings and update graphical widgets with loaded values."""
  162         settings = QSettings()
  163         overwrite_existing = settings.value('overwrite_existing', type=bool)
  164         default_output = settings.value('default_output', type=str)
  165         prefix = settings.value('prefix', type=str)
  166         suffix = settings.value('suffix', type=str)
  167         ffmpeg_path = settings.value('ffmpeg_path', type=str)
  168         default_command = (settings.value('default_command', type=str) or
  169                 config.default_ffmpeg_cmd)
  170         videocodecs = (settings.value('videocodecs') or config.video_codecs)
  171         audiocodecs = (settings.value('audiocodecs') or config.audio_codecs)
  172         extraformats_video = (settings.value('extraformats_video') or [])
  173         default_command_image = (settings.value('default_command_image',
  174                 type=str) or
  175                 config.default_imagemagick_cmd
  176                 )
  177         extraformats_image = (settings.value('extraformats_image') or [])
  178         extraformats_document = (settings.value('extraformats_document') or [])
  179 
  180         if overwrite_existing:
  181             self.exst_overwriteQRB.setChecked(True)
  182         else:
  183             self.exst_prefixQRB.setChecked(True)
  184 
  185         self.defaultQLE.setText(default_output)
  186         self.prefixQLE.setText(prefix)
  187         self.suffixQLE.setText(suffix)
  188         self.ffmpegpathQLE.setText(ffmpeg_path)
  189         self.ffmpegcmdQLE.setText(default_command)
  190         self.set_videocodecs(videocodecs)
  191         self.set_audiocodecs(audiocodecs)
  192         self.extraformatsffmpegQPTE.setPlainText("\n".join(extraformats_video))
  193         self.imagecmdQLE.setText(default_command_image)
  194         self.extraformatsimageQPTE.setPlainText("\n".join(extraformats_image))
  195         self.extraformatsdocumentQPTE.setPlainText("\n".join(extraformats_document))
  196 
  197     def set_videocodecs(self, codecs):
  198         self.vidcodecsQPTE.setPlainText("\n".join(codecs))
  199 
  200     def set_audiocodecs(self, codecs):
  201         self.audcodecsQPTE.setPlainText("\n".join(codecs))
  202 
  203     def open_dir(self):
  204         """Get a directory name using a standard Qt dialog and update
  205         self.defaultQLE with dir's name."""
  206         if self.defaultQLE.isEnabled():
  207             _dir = QFileDialog.getExistingDirectory(
  208                     self, 'FF Multi Converter - ' +
  209                     self.tr('Choose default output destination'), config.home
  210                     )
  211             if _dir:
  212                 self.defaultQLE.setText(_dir)
  213 
  214     @staticmethod
  215     def plaintext_to_list(widget, formats=[]):
  216         """
  217         Parse the text from a QPlainTextEdit widget and return a list.
  218         The list will consist of every text line that is a single word
  219         and it's not in the formats list. No duplicates allowed.
  220         """
  221         _list = []
  222         for line in widget.toPlainText().split("\n"):
  223             line = line.strip()
  224             if len(line.split()) == 1 and line not in (_list+formats):
  225                 _list.append(line)
  226         return _list
  227 
  228     def save_settings(self):
  229         """Set settings values by extracting the appropriate information from
  230         the graphical widgets."""
  231         videocodecs = self.plaintext_to_list(self.vidcodecsQPTE)
  232         audiocodecs = self.plaintext_to_list(self.audcodecsQPTE)
  233         extraformats_video = self.plaintext_to_list(self.extraformatsffmpegQPTE,
  234                 config.video_formats)
  235         extraformats_image = self.plaintext_to_list(self.extraformatsimageQPTE,
  236                 config.image_formats)
  237         extraformats_document = self.plaintext_to_list(
  238                 self.extraformatsdocumentQPTE, config.document_formats)
  239 
  240         settings = QSettings()
  241 
  242         ffmpeg_path = os.path.expanduser(self.ffmpegpathQLE.text())
  243         if not utils.is_installed(ffmpeg_path):
  244             ffmpeg_path = utils.is_installed('ffmpeg')
  245 
  246         settings.setValue('overwrite_existing', self.exst_overwriteQRB.isChecked())
  247         settings.setValue('default_output', self.defaultQLE.text())
  248         settings.setValue('prefix', self.prefixQLE.text())
  249         settings.setValue('suffix', self.suffixQLE.text())
  250         settings.setValue('ffmpeg_path', ffmpeg_path)
  251         settings.setValue('default_command', self.ffmpegcmdQLE.text())
  252         settings.setValue('videocodecs', sorted(videocodecs))
  253         settings.setValue('audiocodecs', sorted(audiocodecs))
  254         settings.setValue('extraformats_video', sorted(extraformats_video))
  255         settings.setValue('default_command_image', self.imagecmdQLE.text())
  256         settings.setValue('extraformats_image', sorted(extraformats_image))
  257         settings.setValue('extraformats_document', sorted(extraformats_document))
  258 
  259         self.accept()