Beispiel #1
0
    def setup_ui(self):
        """Display each setting widget with saved values from registry/ini.

        Todo:
            Renable word_wrap widget when QDelegate Sizing issue fixed.
        """
        self.key_combo_edit = HotKeyEdit(self)

        # Allow user to insert <SUPER> on a Win OS
        self.super_check = QtGui.QCheckBox('Win')
        self.super_check.setToolTip('Insert <SUPER>')
        if '<SUPER>' in self.key_combo_edit.text().upper():
            self.super_check.setCheckState(QtCore.Qt.Checked)

        # Number of lines to display
        self.line_count_spin = QtGui.QSpinBox(self)
        self.line_count_spin.setRange(1, 10)
        self.line_count_spin.setValue(settings.get_lines_to_display())

        # Where to open the dialog
        self.open_at_pos_combo = QtGui.QComboBox(self)
        self.open_at_pos_combo.addItem('Mouse cursor', 0)
        self.open_at_pos_combo.addItem('Last position', 1)
        self.open_at_pos_combo.addItem('System tray', 2)

        # Word wrap display text
        self.word_wrap = QtGui.QCheckBox('Word wrap')
        self.word_wrap.setCheckState(_check_state(settings.get_word_wrap()))

        # Send paste key stroke when content set to clipboard
        self.paste_check = QtGui.QCheckBox('Paste in active window after '
                                           'selection')
        self.paste_check.setCheckState(_check_state(settings.get_send_paste()))

        # Ignore applications
        group_box = QtGui.QGroupBox('Ignore the following applications')
        self.exclude_list = QtGui.QLineEdit(self)
        self.exclude_list.setPlaceholderText('KeePass.exe;binaryname')
        self.exclude_list.setText(settings.get_exclude())

        # Create seperate layout for ignore applications
        vbox = QtGui.QVBoxLayout()
        vbox.addWidget(self.exclude_list)
        group_box.setLayout(vbox)

        # Save and cancel buttons
        button_box = QtGui.QDialogButtonBox(QtGui.QDialogButtonBox.Save|
                                            QtGui.QDialogButtonBox.Cancel)

        # Create form layout to align widgets
        layout = QtGui.QFormLayout()
        layout.setFieldGrowthPolicy(QtGui.QFormLayout.FieldsStayAtSizeHint)
        layout.addRow('Global shortcut:', self.key_combo_edit)
        layout.addRow('', self.super_check)
        layout.addRow('Open window at:', self.open_at_pos_combo)
        layout.addRow('Lines to display:', self.line_count_spin)

        # Set main layout
        main_layout = QtGui.QVBoxLayout(self)
        main_layout.addLayout(layout)
        # main_layout.addWidget(self.word_wrap)
        main_layout.addWidget(self.paste_check)
        main_layout.addWidget(group_box)
        main_layout.addWidget(button_box)
        self.setLayout(main_layout)

        # LINUX: I use Windows key to move windows with my wm
        self.setFocus(QtCore.Qt.PopupFocusReason)

        button_box.accepted.connect(self.save)
        button_box.rejected.connect(self.cancel)

        self.connect(self.super_check, QtCore.SIGNAL('stateChanged(int)'), 
                     self.insert_win_key)
    def _on_new_item(self, mime_data):
        """Append new clipboard contents to database.

        Performs checksum for new data vs database. If duplicate found, then 
        the time is updated in a seperate function. Once parent data is 
        created, the mime data is converted to QByteArray and stored in data 
        table as a blob.

        Args:
            mime_data (QMimeData): clipboard contents mime data

        Returns: 
            True (bool): Successfully added data to model.
            None: User just set new clipboard data trigger dataChanged() to be
                emited. Data does not have any text. Duplicate found. Storing 
                data in database fails.

        TODO: Clean up this function as there are too many random returns. 
            Store images.
        """
        # Do not perform the new item process because user just set clipboard 
        # contents
        if self.ignore_created:
            self.ignore_created = False
            return None

        # Check if process that set clipboard is on exclude list
        # TODO: Make class that handles platform dependency
        if sys.platform.startswith('win32'):
            proc_name = clipboards.get_win32_owner()
        elif sys.platform.startswith('linux'):
            proc_name = clipboards.get_x11_owner()
        else:
            proc_name = None

        # Make user entered apps lowercase and into a list
        if proc_name is not None:
            ignore_list = settings.get_exclude().lower().split(';')
            if proc_name.lower() in ignore_list:
                logging.info('Ignoring clipboard change by %s.' % proc_name)
                return None

        logging.debug('Clipboard Formats: %s' % str(mime_data.formats()))

        checksum = utils.calculate_checksum(mime_data)
        if checksum == None:
            return None
        elif self._duplicate(checksum): # If duplicate found then exit function
            return None

        text = utils.create_full_title(mime_data)

        # title_short used in list row view so clean it up by removing white
        # space, dedent, and striping uncessary line breaks
        title_short = utils.clean_up_text(text)
        title_short = utils.remove_extra_lines(text=title_short,
                                    line_count=settings.get_lines_to_display())

        date = QtCore.QDateTime.currentMSecsSinceEpoch()

        parent_id = database.insert_main(date=date, 
                                         titleshort=title_short,
                                         titlefull=text,
                                         checksum=checksum)

        # Store mime data into database
        if not parent_id:
            logging.error('Failed to create entry in database.')
            return None

        # Highlight top item and then insert mime data
        self.model_main.select() # Update view
        index = QtCore.QModelIndex(self.view_main.model().index(0, TITLESHORT))
        self.view_main.setCurrentIndex(index)

        # Convert mime data based on format to ByteArray
        data_insert = []
        for format in MIME_REFERENCES:
            if mime_data.hasFormat(format):
                byte_data = QtCore.QByteArray(mime_data.data(format))
                data_insert.append([format, byte_data])

        for format, __ in data_insert:
            logging.debug('Format Saved: %s' % format)
        
        # Insert mime data into database
        for format, byte_data in data_insert:
            database.insert_mime(parent_id, format, byte_data)

        # Free memory?
        # del data_insert, index, parent_id, title_short, text, codec, encoder
        # del bytes, checksum_string, checksum, mime_data, proc_name

        return True