Esempio n. 1
0
    def headerData(self, section, orientation, role):
        if role == Qt.DisplayRole:
            if orientation == Qt.Horizontal:
                return to_qvariant(self.colnames[section])

            if orientation == Qt.Vertical:
                return section + 1
Esempio n. 2
0
def create_action(parent, text, shortcut=None, icon=None, tip=None,
                  toggled=None, triggered=None, data=None, menurole=None,
                  context=Qt.WindowShortcut):
    """Create a QAction"""
    action = QAction(text, parent)
    if triggered is not None:
        parent.connect(action, SIGNAL("triggered()"), triggered)
    if toggled is not None:
        parent.connect(action, SIGNAL("toggled(bool)"), toggled)
        action.setCheckable(True)
    if icon is not None:
        if isinstance(icon, (str, unicode)):
            icon = get_icon(icon)
        action.setIcon(icon)
    if shortcut is not None:
        action.setShortcut(shortcut)
    if tip is not None:
        action.setToolTip(tip)
        action.setStatusTip(tip)
    if data is not None:
        action.setData(to_qvariant(data))
    if menurole is not None:
        action.setMenuRole(menurole)
    #TODO: Hard-code all shortcuts and choose context=Qt.WidgetShortcut
    # (this will avoid calling shortcuts from another dockwidget
    #  since the context thing doesn't work quite well with these widgets)
    action.setShortcutContext(context)
    return action
Esempio n. 3
0
    def data(self, index, role = Qt.DisplayRole):
        row = index.row()
        col = index.column()
        colname = self.colnames[col]
        if role == Qt.DisplayRole:
            val = self.dataframe.get_value(row, colname)
            if isinstance(val, str) or isinstance(val, unicode):
                return unicode(QString(val))

            else:
                if isnan(val):
                    return QString("NaN")
                elif isinf(val):
                    return QString("Inf")
                else:
                    if abs(val) <= 1:
                        return to_qvariant(('%.3g' % val))
                    else:
                        return to_qvariant(int(round(val)))            
        elif role == Qt.TextAlignmentRole:
            return to_qvariant(Qt.AlignRight | Qt.AlignVCenter)
Esempio n. 4
0
 def headerData(self, section, orientation, role=Qt.DisplayRole):
     if role == Qt.TextAlignmentRole:
         if orientation == Qt.Horizontal:
             return to_qvariant(int(Qt.AlignHCenter | Qt.AlignVCenter))
         return to_qvariant(int(Qt.AlignRight | Qt.AlignVCenter))
     if role != Qt.DisplayRole:
         return to_qvariant()
     if orientation == Qt.Horizontal:
         if section == CONTEXT:
             return to_qvariant(_("Context"))
         elif section == NAME:
             return to_qvariant(_("Name"))
         elif section == MOD1:
             return to_qvariant(_("Mod1"))
         elif section == MOD2:
             return to_qvariant(_("Mod2"))
         elif section == MOD3:
             return to_qvariant(_("Mod3"))
         elif section == KEY:
             return to_qvariant(_("Key"))
     return to_qvariant()
Esempio n. 5
0
 def headerData(self, section, orientation, role=Qt.DisplayRole):
     if role == Qt.TextAlignmentRole:
         if orientation == Qt.Horizontal:
             return to_qvariant(int(Qt.AlignHCenter|Qt.AlignVCenter))
         return to_qvariant(int(Qt.AlignRight|Qt.AlignVCenter))
     if role != Qt.DisplayRole:
         return to_qvariant()
     if orientation == Qt.Horizontal:
         if section == CONTEXT:
             return to_qvariant(_("Context"))
         elif section == NAME:
             return to_qvariant(_("Name"))
         elif section == MOD1:
             return to_qvariant(_("Mod1"))
         elif section == MOD2:
             return to_qvariant(_("Mod2"))
         elif section == MOD3:
             return to_qvariant(_("Mod3"))
         elif section == KEY:
             return to_qvariant(_("Key"))
     return to_qvariant()
Esempio n. 6
0
 def data(self, index, role=Qt.DisplayRole):
     if not index.isValid() or \
        not (0 <= index.row() < len(self.shortcuts)):
         return to_qvariant()
     shortcut = self.shortcuts[index.row()]
     key = shortcut.key
     column = index.column()
     if role == Qt.DisplayRole:
         if column == CONTEXT:
             return to_qvariant(shortcut.context)
         elif column == NAME:
             return to_qvariant(shortcut.name)
         elif column == MOD1:
             return to_qvariant(Key.MODIFIERNAMES[key.modifiers[0]])
         elif column == MOD2:
             return to_qvariant(Key.MODIFIERNAMES[key.modifiers[1]])
         elif column == MOD3:
             return to_qvariant(Key.MODIFIERNAMES[key.modifiers[2]])
         elif column == KEY:
             return to_qvariant(Key.KEYS[key.key])
     elif role == Qt.TextAlignmentRole:
         return to_qvariant(int(Qt.AlignHCenter | Qt.AlignVCenter))
     return to_qvariant()
Esempio n. 7
0
 def data(self, index, role=Qt.DisplayRole):
     if not index.isValid() or \
        not (0 <= index.row() < len(self.shortcuts)):
         return to_qvariant()
     shortcut = self.shortcuts[index.row()]
     key = shortcut.key
     column = index.column()
     if role == Qt.DisplayRole:
         if column == CONTEXT:
             return to_qvariant(shortcut.context)
         elif column == NAME:
             return to_qvariant(shortcut.name)
         elif column == MOD1:
             return to_qvariant(Key.MODIFIERNAMES[key.modifiers[0]])
         elif column == MOD2:
             return to_qvariant(Key.MODIFIERNAMES[key.modifiers[1]])
         elif column == MOD3:
             return to_qvariant(Key.MODIFIERNAMES[key.modifiers[2]])
         elif column == KEY:
             return to_qvariant(Key.KEYS[key.key])
     elif role == Qt.TextAlignmentRole:
         return to_qvariant(int(Qt.AlignHCenter|Qt.AlignVCenter))
     return to_qvariant()
Esempio n. 8
0
    def __init__(self, parent, text, choices = None, option = None, tip = None):
        super(MyComboBox, self).__init__(parent)
        """choices: couples (name, key)"""
        label = QLabel(text)
        combobox = QComboBox(parent)
        if tip is not None:
            combobox.setToolTip(tip)
        if choices:
            for name, key in choices:
                combobox.addItem(name, to_qvariant(key))

        layout = QHBoxLayout()
        for subwidget in (label, combobox):
            layout.addWidget(subwidget)
        layout.addStretch(1)
        layout.setContentsMargins(0, 0, 0, 0)
        self.setLayout(layout)
        self.box = combobox
Esempio n. 9
0
 def create_combobox(self, text, choices, option, default=NoDefault,
                     tip=None):
     """choices: couples (name, key)"""
     label = QLabel(text)
     combobox = QComboBox()
     if tip is not None:
         combobox.setToolTip(tip)
     for name, key in choices:
         combobox.addItem(name, to_qvariant(key))
     self.comboboxes[combobox] = (option, default)
     layout = QHBoxLayout()
     for subwidget in (label, combobox):
         layout.addWidget(subwidget)
     layout.addStretch(1)
     layout.setContentsMargins(0, 0, 0, 0)
     widget = QWidget(self)
     widget.setLayout(layout)
     return widget
Esempio n. 10
0
 def create_combobox(self,
                     text,
                     choices,
                     option,
                     default=NoDefault,
                     tip=None):
     """choices: couples (name, key)"""
     label = QLabel(text)
     combobox = QComboBox()
     if tip is not None:
         combobox.setToolTip(tip)
     for name, key in choices:
         combobox.addItem(name, to_qvariant(key))
     self.comboboxes[combobox] = (option, default)
     layout = QHBoxLayout()
     for subwidget in (label, combobox):
         layout.addWidget(subwidget)
     layout.addStretch(1)
     layout.setContentsMargins(0, 0, 0, 0)
     widget = QWidget(self)
     widget.setLayout(layout)
     return widget
Esempio n. 11
0
 def load_from_conf(self):
     """Load settings from configuration file"""
     for checkbox, (option, default) in self.checkboxes.items():
         checkbox.setChecked(self.get_option(option, default))
         self.connect(checkbox, SIGNAL("clicked(bool)"),
                      lambda _foo, opt=option: self.has_been_modified(opt))
     for radiobutton, (option, default) in self.radiobuttons.items():
         radiobutton.setChecked(self.get_option(option, default))
         
         self.connect(radiobutton, SIGNAL("toggled(bool)"),
                      lambda _foo, opt=option: self.has_been_modified(opt))
     for lineedit, (option, default) in self.lineedits.items():
         lineedit.setText(self.get_option(option, default))
         self.connect(lineedit, SIGNAL("textChanged(QString)"),
                      lambda _foo, opt=option: self.has_been_modified(opt))
     for spinbox, (option, default) in self.spinboxes.items():
         spinbox.setValue(self.get_option(option, default))
         self.connect(spinbox, SIGNAL('valueChanged(int)'),
                      lambda _foo, opt=option: self.has_been_modified(opt))
     for combobox, (option, default) in self.comboboxes.items():
         value = self.get_option(option, default)
         for index in range(combobox.count()):
             data = from_qvariant(combobox.itemData(index), unicode)
             # For PyQt API v2, it is necessary to convert `data` to 
             # unicode in case the original type was not a string, like an 
             # integer for example (see src.gui.qt.compat.from_qvariant):
             if unicode(data) == unicode(value):
                 break
         combobox.setCurrentIndex(index)
         self.connect(combobox, SIGNAL('currentIndexChanged(int)'),
                      lambda _foo, opt=option: self.has_been_modified(opt))
     for (fontbox, sizebox), option in self.fontboxes.items():
         font = self.get_font(option)
         fontbox.setCurrentFont(font)
         sizebox.setValue(font.pointSize())
         if option is None:
             property = 'plugin_font'
         else:
             property = option
         self.connect(fontbox, SIGNAL('currentIndexChanged(int)'),
                      lambda _foo, opt=property: self.has_been_modified(opt))
         self.connect(sizebox, SIGNAL('valueChanged(int)'),
                      lambda _foo, opt=property: self.has_been_modified(opt))
     for clayout, (option, default) in self.coloredits.items():
         property = to_qvariant(option)
         edit = clayout.lineedit
         btn = clayout.colorbtn
         edit.setText(self.get_option(option, default))
         self.connect(btn, SIGNAL('clicked()'),
                      lambda opt=option: self.has_been_modified(opt))
         self.connect(edit, SIGNAL("textChanged(QString)"),
                      lambda _foo, opt=option: self.has_been_modified(opt))
     
     for dateedit, (option, default) in self.dateedits.items():
         def extract_qdate_from_str(x):
             return QDate(int(x[:4]), int(x[5:7]), int(x[8:10]))
         dateedit.setDate(extract_qdate_from_str(self.get_option(option, default)))
         self.connect(dateedit, SIGNAL('dateChanged(QDate)'),
                      lambda _foo, opt=option: self.has_been_modified(opt))
     
 
     for (clayout, cb_bold, cb_italic
          ), (option, default) in self.scedits.items():
         edit = clayout.lineedit
         btn = clayout.colorbtn
         color, bold, italic = self.get_option(option, default)
         edit.setText(color)
         cb_bold.setChecked(bold)
         cb_italic.setChecked(italic)
         self.connect(btn, SIGNAL('clicked()'),
                      lambda opt=option: self.has_been_modified(opt))
         self.connect(edit, SIGNAL("textChanged(QString)"),
                      lambda _foo, opt=option: self.has_been_modified(opt))
         self.connect(cb_bold, SIGNAL("clicked(bool)"),
                      lambda _foo, opt=option: self.has_been_modified(opt))
         self.connect(cb_italic, SIGNAL("clicked(bool)"),
                      lambda _foo, opt=option: self.has_been_modified(opt))
Esempio n. 12
0
    def load_from_conf(self):
        """Load settings from configuration file"""
        for checkbox, (option, default) in self.checkboxes.items():
            checkbox.setChecked(self.get_option(option, default))
            self.connect(checkbox,
                         SIGNAL("clicked(bool)"),
                         lambda _foo, opt=option: self.has_been_modified(opt))
        for radiobutton, (option, default) in self.radiobuttons.items():
            radiobutton.setChecked(self.get_option(option, default))

            self.connect(radiobutton,
                         SIGNAL("toggled(bool)"),
                         lambda _foo, opt=option: self.has_been_modified(opt))
        for lineedit, (option, default) in self.lineedits.items():
            lineedit.setText(self.get_option(option, default))
            self.connect(lineedit,
                         SIGNAL("textChanged(QString)"),
                         lambda _foo, opt=option: self.has_been_modified(opt))
        for spinbox, (option, default) in self.spinboxes.items():
            spinbox.setValue(self.get_option(option, default))
            self.connect(spinbox,
                         SIGNAL('valueChanged(int)'),
                         lambda _foo, opt=option: self.has_been_modified(opt))
        for combobox, (option, default) in self.comboboxes.items():
            value = self.get_option(option, default)
            for index in range(combobox.count()):
                data = from_qvariant(combobox.itemData(index), unicode)
                # For PyQt API v2, it is necessary to convert `data` to
                # unicode in case the original type was not a string, like an
                # integer for example (see src.gui.qt.compat.from_qvariant):
                if unicode(data) == unicode(value):
                    break
            combobox.setCurrentIndex(index)
            self.connect(combobox,
                         SIGNAL('currentIndexChanged(int)'),
                         lambda _foo, opt=option: self.has_been_modified(opt))
        for (fontbox, sizebox), option in self.fontboxes.items():
            font = self.get_font(option)
            fontbox.setCurrentFont(font)
            sizebox.setValue(font.pointSize())
            if option is None:
                property = 'plugin_font'
            else:
                property = option
            self.connect(
                fontbox,
                SIGNAL('currentIndexChanged(int)'),
                lambda _foo, opt=property: self.has_been_modified(opt))
            self.connect(
                sizebox,
                SIGNAL('valueChanged(int)'),
                lambda _foo, opt=property: self.has_been_modified(opt))
        for clayout, (option, default) in self.coloredits.items():
            property = to_qvariant(option)
            edit = clayout.lineedit
            btn = clayout.colorbtn
            edit.setText(self.get_option(option, default))
            self.connect(btn,
                         SIGNAL('clicked()'),
                         lambda opt=option: self.has_been_modified(opt))
            self.connect(edit,
                         SIGNAL("textChanged(QString)"),
                         lambda _foo, opt=option: self.has_been_modified(opt))

        for dateedit, (option, default) in self.dateedits.items():

            def extract_qdate_from_str(x):
                return QDate(int(x[:4]), int(x[5:7]), int(x[8:10]))

            dateedit.setDate(
                extract_qdate_from_str(self.get_option(option, default)))
            self.connect(dateedit,
                         SIGNAL('dateChanged(QDate)'),
                         lambda _foo, opt=option: self.has_been_modified(opt))

        for (clayout, cb_bold, cb_italic), (option,
                                            default) in self.scedits.items():
            edit = clayout.lineedit
            btn = clayout.colorbtn
            color, bold, italic = self.get_option(option, default)
            edit.setText(color)
            cb_bold.setChecked(bold)
            cb_italic.setChecked(italic)
            self.connect(btn,
                         SIGNAL('clicked()'),
                         lambda opt=option: self.has_been_modified(opt))
            self.connect(edit,
                         SIGNAL("textChanged(QString)"),
                         lambda _foo, opt=option: self.has_been_modified(opt))
            self.connect(cb_bold,
                         SIGNAL("clicked(bool)"),
                         lambda _foo, opt=option: self.has_been_modified(opt))
            self.connect(cb_italic,
                         SIGNAL("clicked(bool)"),
                         lambda _foo, opt=option: self.has_been_modified(opt))
Esempio n. 13
0
 def setModelData(self, editor, model, index):
     if index.column() in (MOD1, MOD2, MOD3, KEY):
         model.setData(index, to_qvariant(editor.currentText()))
     else:
         QItemDelegate.setModelData(self, editor, model, index)
Esempio n. 14
0
 def setModelData(self, editor, model, index):
     if index.column() in (MOD1, MOD2, MOD3, KEY):
         model.setData(index, to_qvariant(editor.currentText()))
     else:
         QItemDelegate.setModelData(self, editor, model, index)
Esempio n. 15
0
def set_item_user_text(item, text):
    """Set QTreeWidgetItem user role string"""
    item.setData(0, Qt.UserRole, to_qvariant(text))