Пример #1
0
VERSION = 'trunk'

DEFAULTS = { 'exelocation':  "C:/Program Files/JFX/OpenSesame/openvpn.exe",
                    'windeffileloc':  "C:/Program Files/OpenVPN/config",
                        'linuxbinary':  "/usr/bin/openvpn", 
                        'linuxdeffileloc': "/home",
                        'showTrayWarning': True,
                        'defaultPort': '1194',
                        'defMgmtBasePort': '16000'
                    }

EXPORTFILES = [ 'config.ovpn',  'client.crt', 'client.key', 'ca.crt', 'ta.key' ]

settings = QSettings(QSettings.IniFormat, QSettings.UserScope, "JFX", APPNAME)
settings.setDefaultFormat(QSettings.IniFormat)
iniPath = os.path.dirname(str(settings.fileName()))

platform=sys.platform

if platform == 'win32':
    defPlatformEXE = DEFAULTS['exelocation']
    defFileLoc = DEFAULTS['windeffileloc']
elif platform == 'linux2':
    defPlatformEXE = DEFAULTS['linuxbinary']
    defFileLoc = DEFAULTS['linuxdeffileloc']
else:
    raise "Unsupported platform: %s" % platform

def md5file(filename):
    """Return the hex digest of a file without loading it all into memory"""
    fh = open(filename)
class ScreamingMercury(QWidget, Ui_ScreamingMercury):
    def __init__(self, delegate, parent=None):
        QWidget.__init__(self, parent)
        self.setupUi(self)

        self.delegate = delegate

        self.small_screwdriver = DrawBinsWidget()
        self.work_layout.insertWidget(0, self.small_screwdriver)

        self.progress_window = Progress()
        self.settings_window = Settings()

        self.bin_packing_thread = BinPackingThread()

        self.addDirectory.clicked.connect(self.onAddDirectory)
        self.removeImage.clicked.connect(self.onRemoveImages)
        self.startPushButton.clicked.connect(self.onStart)
        self.methodTabWidget.currentChanged.connect(self.bin_packing_thread.setMethod)
        self.binSizeComboBox.currentIndexChanged.connect(self.bin_packing_thread.setBinSize)

        self.clearPushButton.clicked.connect(self.delegate.on_remove_all_images)

        self.settingsPushButton.clicked.connect(self.settings_window.show)

        # self.images_changed.connect(self.updateImages)

        self.bin_packing_thread.bin_packing_available.connect(self.startPushButton.setEnabled)
        self.bin_packing_thread.on_end.connect(self.startPushButton.setEnabled)
        self.bin_packing_thread.on_end.connect(self.progress_window.setHidden)
        self.bin_packing_thread.prepare_progress_signal.connect(self.progress_window.prepareProgressBar.setValue)
        self.bin_packing_thread.packing_progress_signal.connect(self.progress_window.binPackingProgressBar.setValue)
        self.bin_packing_thread.saving_progress_signal.connect(self.progress_window.savingProgressBar.setValue)

        self.startPushButton.setEnabled(self.bin_packing_thread.binPackingAvailable())

        self.directory = None
        self.images = []

        self.restoreSettings()

        if self.directory:
            self.harvestDirectory(self.directory)

        self.bin_packing_thread.update_bins.connect(self.small_screwdriver.redrawBins)

        self.outputFormatComboBox.currentIndexChanged.connect(BinPackingThread.set_exporter)
        BinPackingThread.set_exporter(self.outputFormatComboBox.currentIndex())

    def restoreSettings(self):
        # Настройки
        self.settings = QSettings(QSettings.IniFormat, QSettings.UserScope, COMPANY, APPNAME)

        print self.settings.fileName()

        # Востанавливаем ...
        # ... геометрию окна
        self.restoreGeometry(self.settings.value(SETTINGS_GEOMETRY))
        self.splitter.restoreState(self.settings.value(SETTINGS_SPLITTER1))
        self.splitter_2.restoreState(self.settings.value(SETTINGS_SPLITTER2))
        # ... размер контейнера
        self.binSizeComboBox.setCurrentIndex(int(self.settings.value(SETTINGS_SIZE, defaultValue=0)))
        # ... метод упаковки
        self.methodTabWidget.setCurrentIndex(int(self.settings.value(SETTINGS_METHOD, defaultValue=0)))
        # ... вариант упоковки от лучшего варианта или от худшего
        self.firstFitShelfVariantComboBox.setCurrentIndex(
            int(self.settings.value(SETTINGS_FIRST_FIT_VARIANT, defaultValue=0))
        )
        # ... эвристику упаковки
        self.firstFitShelfHeuristicComboBox.setCurrentIndex(
            int(self.settings.value(SETTINGS_FIRST_FIT_HEURISTIC, defaultValue=0))
        )
        # ... вариант для гильотины
        self.guillotineVariantComboBox.setCurrentIndex(int(self.settings.value(SETTINGS_VARIANT, defaultValue=0)))
        # ... эвристику для гильотины
        self.guillotineHeuristicComboBox.setCurrentIndex(int(self.settings.value(SETTINGS_HEURISTIC, defaultValue=0)))
        # ... правило разделения гильотиной
        self.splitComboBox.setCurrentIndex(int(self.settings.value(SETTINGS_SPLIT_RULE, defaultValue=0)))
        # ... масштаб отрисовки
        self.small_screwdriver.scale = float(self.settings.value(SETTINGS_DRAW_SCALE, defaultValue=1.0))

        self.outputFormatComboBox.setCurrentIndex(int(self.settings.value(SETTINGS_OUTPUT_FORMAT, defaultValue=0)))

        self.directory = self.settings.value(SETTINGS_DIRECTORY, defaultValue=None)

        self.progress_window.restoreGeometry(self.settings.value(SETTINGS_PROGRESS_WINDOW_GEOMETRY))

    def onAddDirectory(self):
        directory = QFileDialog.getExistingDirectory(caption="Select image directory", dir=self.directory)
        if directory != u"":
            self.directory = directory

            self.harvestDirectory(self.directory)

    def resetDirectory(self):
        self.directory = None
        self.images = []
        self.bin_packing_thread.setDirectory(self.directory)
        self.bin_packing_thread.setImages(self.images)
        self.updateImages(self.images)

    def harvestDirectory(self, directory):
        folder = QDir(path=directory)
        folder.setNameFilters(["*.png"])
        folder.setFilter(QDir.Files or QDir.NoDotAndDotDot)
        dit = QDirIterator(folder, flags=QDirIterator.Subdirectories, filters=QDir.Files)
        while dit.hasNext():
            im = folder.relativeFilePath(dit.next())
            if not re.search("atlas", im):
                self.images.append(im)
        self.bin_packing_thread.setDirectory(self.directory)
        self.bin_packing_thread.setImages(self.images)
        self.updateImages(self.images)

    def onRemoveImages(self):
        row = self.imageList.currentRow()

        del self.images[row]
        self.updateImages(self.images)

        self.imageList.setCurrentRow(row)

    def onStart(self):
        self.startPushButton.setEnabled(False)
        self.bin_packing_thread.start()
        self.progress_window.show()

    def updateImages(self, images):
        self.imageList.clear()
        self.imageList.addItems(images)

    def keyPressEvent(self, e):
        if e.key() == Qt.Key_Escape:
            self.close()

    def closeEvent(self, e):
        self.progress_window.close()
        self.settings.setValue(SETTINGS_GEOMETRY, self.saveGeometry())
        self.settings.setValue(SETTINGS_SPLITTER1, self.splitter.saveState())
        self.settings.setValue(SETTINGS_SPLITTER2, self.splitter_2.saveState())
        self.settings.setValue(SETTINGS_SIZE, self.binSizeComboBox.currentIndex())

        self.settings.setValue(SETTINGS_METHOD, self.methodTabWidget.currentIndex())

        self.settings.setValue(SETTINGS_FIRST_FIT_VARIANT, self.firstFitShelfVariantComboBox.currentIndex())
        self.settings.setValue(SETTINGS_FIRST_FIT_HEURISTIC, self.firstFitShelfHeuristicComboBox.currentIndex())

        self.settings.setValue(SETTINGS_VARIANT, self.guillotineVariantComboBox.currentIndex())
        self.settings.setValue(SETTINGS_HEURISTIC, self.guillotineHeuristicComboBox.currentIndex())
        self.settings.setValue(SETTINGS_SPLIT_RULE, self.splitComboBox.currentIndex())

        self.settings.setValue(SETTINGS_DRAW_SCALE, self.small_screwdriver.scale)

        self.settings.setValue(SETTINGS_OUTPUT_FORMAT, self.outputFormatComboBox.currentIndex())

        self.settings.setValue(SETTINGS_DIRECTORY, self.directory)

        # Progress Window
        self.settings.setValue(SETTINGS_PROGRESS_WINDOW_GEOMETRY, self.progress_window.saveGeometry())