Python PyQt4.QtCore.QModelIndex() Examples

The following are 29 code examples of PyQt4.QtCore.QModelIndex(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module PyQt4.QtCore , or try the search function .
Example #1
Source File: modeltest.py    From lic with GNU General Public License v3.0 6 votes vote down vote up
def rowCount(self):
        """
        Tests self.model's implementation of QtCore.QAbstractItemModel::rowCount() and hasChildren()
        
        self.models that are dynamically populated are not as fully tested here.
        """
        # check top row
        topindex = self.model.index(0, 0, QtCore.QModelIndex())
        rows = self.model.rowCount(topindex)
        assert(rows >= 0)
        if rows > 0:
            assert(self.model.hasChildren(topindex) == True)

        secondlvl = self.model.index(0, 0, topindex)
        if secondlvl.isValid():
            # check a row count where parent is valid
            rows = self.model.rowCount(secondlvl)
            assert(rows >= 0)
            if rows > 0:
                assert(self.model.hasChildren(secondlvl) == True)
        
        # The self.models rowCount() is tested more extensively in checkChildren,
        # but this catches the big mistakes 
Example #2
Source File: savestatesdialog.py    From pyqtggpo with GNU General Public License v2.0 6 votes vote down vote up
def setFilter(self, userFilter):
        if userFilter:
            userFilter = userFilter.strip().lower()
        if not userFilter:
            self.filteredGames = self.allGames[:]
        else:
            if not userFilter.startswith('*'):
                userFilter = '*' + userFilter
            if not userFilter.endswith('*'):
                userFilter += '*'
            self.filteredGames = []
            for d in self.allGames:
                if fnmatch.fnmatch(d[self.NAME].lower(), userFilter) or \
                        fnmatch.fnmatch(d[self.DESCRIPTION].lower(), userFilter):
                    self.filteredGames.append(d)
        self.sort(self.lastSort, self.lastSortDirection)
        rowcount = len(self.filteredGames)
        if rowcount > 0:
            idx1 = self.createIndex(0, 0)
            idx2 = self.createIndex(rowcount - 1, self.N_DISPLAY_COLUMNS - 1)
        else:
            idx1 = idx2 = QtCore.QModelIndex()
        self.dataChanged.emit(idx1, idx2) 
Example #3
Source File: __init__.py    From nupic.studio with GNU General Public License v2.0 6 votes vote down vote up
def update(self, header, data):
    self.header = header
    self.data = data

    numCols = len(self.header)
    self.setColumnCount(numCols)
    numRows = len(self.data)
    self.setRowCount(numRows)

    for col in range(numCols):
      self.setHeaderData(col, QtCore.Qt.Horizontal, self.header[col])

    for row in range(numRows):
      for col in range(numCols):
        value = self.data[row][col]
        self.setData(self.index(row, col, QtCore.QModelIndex()), value) 
Example #4
Source File: modeltest.py    From lic with GNU General Public License v3.0 6 votes vote down vote up
def hasIndex(self):
        """
        Tests self.model's implementation of QtCore.QAbstractItemModel::hasIndex()
        """
        # Make sure that invalid values returns an invalid index
        assert(self.model.hasIndex(-2, -2) == False)
        assert(self.model.hasIndex(-2, 0) == False)
        assert(self.model.hasIndex(0, -2) == False)

        rows = self.model.rowCount(QtCore.QModelIndex())
        cols = self.model.columnCount(QtCore.QModelIndex())

        # check out of bounds
        assert(self.model.hasIndex(rows, cols) == False)
        assert(self.model.hasIndex(rows + 1, cols + 1) == False)

        if rows > 0:
            assert(self.model.hasIndex(0, 0) == True)

        # hasIndex() is tested more extensively in checkChildren()
        # but this catches the big mistakes 
Example #5
Source File: ddt4all.py    From ddt4all with GNU General Public License v3.0 6 votes vote down vote up
def ecuSel(self, index):
        if index.parent() == core.QModelIndex():
            return
        item = self.list.model().itemData(self.list.model().index(index.row(), 0, index.parent()))
        if qt5:
            selected = item[0]
        else:
            selected = utf8(item[0].toString())
        target = self.ecuscan.ecu_database.getTarget(selected)
        name = selected
        if target:
            self.ecuscan.addTarget(target)
            if target.addr in self.ecuscan.ecu_database.addr_group_mapping:
                group = self.ecuscan.ecu_database.addr_group_mapping[target.addr]
            else:
                group = "Unknown"
            print name, group
            name = "[ " + group + " ] " + name
        if selected:
            if name not in options.main_window.ecunamemap:
                options.main_window.ecunamemap[name] = selected
                self.treeview_ecu.addItem(name) 
Example #6
Source File: ObjectCollection.py    From FlatCAM with MIT License 6 votes vote down vote up
def set_active(self, name):
        """
        Selects object by name from the project list. This triggers the
        list_selection_changed event and call on_list_selection_changed.

        :param name: Name of the FlatCAM Object
        :return: None
        """
        obj = self.get_by_name(name)
        item = obj.item
        group = self.group_items[obj.kind]

        group_index = self.index(group.row(), 0, Qt.QModelIndex())
        item_index = self.index(item.row(), 0, group_index)

        self.view.selectionModel().select(item_index, QtGui.QItemSelectionModel.Select) 
Example #7
Source File: modeltest.py    From lic with GNU General Public License v3.0 6 votes vote down vote up
def rowsInserted(self, parent, start, end):
        """
        Confirm that what was said was going to happen actually did
        """
        c = self.insert.pop()
        assert(c['parent'] == parent)
        assert(c['oldSize'] + (end - start + 1) == self.model.rowCount(parent))
        assert(c['last'] == self.model.data(self.model.index(start - 1, 0, c['parent'])))

        # if c['next'] != self.model.data(model.index(end+1, 0, c['parent'])):
        #   qDebug << start << end
        #   for i in range(0, self.model.rowCount(QtCore.QModelIndex())):
        #       qDebug << self.model.index(i, 0).data().toString()
        #   qDebug() << c['next'] << self.model.data(model.index(end+1, 0, c['parent']))

        assert(c['next'] == self.model.data(self.model.index(end + 1, 0, c['parent']))) 
Example #8
Source File: variables.py    From hashmal with GNU General Public License v3.0 5 votes vote down vote up
def rowCount(self, parent = QtCore.QModelIndex()):
        return len(self.vars_data) 
Example #9
Source File: modeltest.py    From lic with GNU General Public License v3.0 5 votes vote down vote up
def columnCount(self):
        """
        Tests self.model's implementation of QtCore.QAbstractItemModel::columnCount() and hasChildren()
        """
        # check top row
        topidx = self.model.index(0, 0, QtCore.QModelIndex())
        assert(self.model.columnCount(topidx) >= 0)

        # check a column count where parent is valid
        childidx = self.model.index(0, 0, topidx)
        if childidx.isValid() :
            assert(self.model.columnCount(childidx) >= 0)

        # columnCount() is tested more extensively in checkChildren,
        # but this catches the big mistakes 
Example #10
Source File: modeltest.py    From lic with GNU General Public License v3.0 5 votes vote down vote up
def parent(self):
        """
        Tests self.model's implementation of QtCore.QAbstractItemModel::parent()
        """
        # Make sure the self.model wont crash and will return an invalid QtCore.QModelIndex
        # when asked for the parent of an invalid index
        assert(self.model.parent(QtCore.QModelIndex()) == QtCore.QModelIndex())

        if self.model.rowCount(QtCore.QModelIndex()) == 0:
            return;

        # Column 0              | Column 1  |
        # QtCore.Qself.modelIndex()         |           |
        #    \- topidx          | topidx1   |
        #         \- childix    | childidx1 |

        # Common error test #1, make sure that a top level index has a parent
        # that is an invalid QtCore.Qself.modelIndex
        topidx = self.model.index(0, 0, QtCore.QModelIndex())
        assert(self.model.parent(topidx) == QtCore.QModelIndex())

        # Common error test #2, make sure that a second level index has a parent
        # that is the first level index
        if self.model.rowCount(topidx) > 0 :
            childidx = self.model.index(0, 0, topidx)
            assert(self.model.parent(childidx) == topidx)

        # Common error test #3, the second column should NOT have the same children
        # as the first column in a row
        # Usually the second column shouldn't have children
        topidx1 = self.model.index(0, 1, QtCore.QModelIndex())
        if self.model.rowCount(topidx1) > 0:
            childidx = self.model.index(0, 0, topidx)
            childidx1 = self.model.index(0, 0, topidx1)
            assert(childidx != childidx1)

        # Full test, walk n levels deep through the self.model making sure that all
        # parent's children correctly specify their parent
        self.checkChildren(QtCore.QModelIndex()) 
Example #11
Source File: variables.py    From hashmal with GNU General Public License v3.0 5 votes vote down vote up
def columnCount(self, parent = QtCore.QModelIndex()):
        return 2 
Example #12
Source File: modeltest.py    From lic with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, _model, parent):
        """
        Connect to all of the models signals, Whenever anything happens recheck everything.
        """
        QtCore.QObject.__init__(self, parent)
        self.model = _model  # 29jan11rg: this used to be:  self._model = model
        # self.model = sip.cast(_model, QtCore.QAbstractItemModel)  #29jan11rg: I don't understand why this is here.  The check for index.model() == self.model will always fail!
        self.insert = []
        self.remove = []
        self.fetchingMore = False
        assert(self.model)

        self.connect(self.model, QtCore.SIGNAL("columnsAboutToBeInserted(const QModelIndex&, int, int)"), self.runAllTests)
        self.connect(self.model, QtCore.SIGNAL("columnsAboutToBeRemoved(const QModelIndex&, int, int)"), self.runAllTests)
        self.connect(self.model, QtCore.SIGNAL("columnsBeInserted(const QModelIndex&, int, int)"), self.runAllTests)
        self.connect(self.model, QtCore.SIGNAL("columnsRemoved(const QModelIndex&, int, int)"), self.runAllTests)
        self.connect(self.model, QtCore.SIGNAL("dataChanged(const QModelIndex&, const QModelIndex&)"), self.runAllTests)
        self.connect(self.model, QtCore.SIGNAL("headerDataChanged(Qt::Orientation, int, int)"), self.runAllTests)
        self.connect(self.model, QtCore.SIGNAL("layoutAboutToBeChanged()"), self.runAllTests)
        self.connect(self.model, QtCore.SIGNAL("layoutChanged()"), self.runAllTests)
        self.connect(self.model, QtCore.SIGNAL("modelReset()"), self.runAllTests)
        self.connect(self.model, QtCore.SIGNAL("rowsAboutToBeInserted(const QModelIndex&, int, int)"), self.runAllTests)
        self.connect(self.model, QtCore.SIGNAL("rowsAboutToBeRemoved(const QModelIndex&, int, int)"), self.runAllTests)
        self.connect(self.model, QtCore.SIGNAL("rowsBeInserted(const QModelIndex&, int, int)"), self.runAllTests)
        self.connect(self.model, QtCore.SIGNAL("rowsRemoved(const QModelIndex&, int, int)"), self.runAllTests)

        # Special checks for inserting/removing
        self.connect(self.model, QtCore.SIGNAL("rowsAboutToBeInserted(const QModelIndex&, int, int)"), self.rowsAboutToBeInserted)
        self.connect(self.model, QtCore.SIGNAL("rowsAboutToBeRemoved(const QModelIndex&, int, int)"), self.rowsAboutToBeRemoved)
        self.connect(self.model, QtCore.SIGNAL("rowsBeInserted(const QModelIndex&, int, int)"), self.rowsInserted)
        self.connect(self.model, QtCore.SIGNAL("rowsRemoved(const QModelIndex&, int, int)"), self.rowsRemoved)
        self.runAllTests() 
Example #13
Source File: variables.py    From hashmal with GNU General Public License v3.0 5 votes vote down vote up
def set_key(self, key, value):
        self.beginInsertRows( QtCore.QModelIndex(), self.rowCount(), self.rowCount() )
        self.vars_data[key] = value
        self.reverse_lookup[value] = key
        self.endInsertRows() 
Example #14
Source File: variables.py    From hashmal with GNU General Public License v3.0 5 votes vote down vote up
def remove_key(self, key):
        row = self.vars_data.keys().index(key)
        self.beginRemoveRows( QtCore.QModelIndex(), row, row )
        value = self.vars_data[key]
        del self.reverse_lookup[value]
        del self.vars_data[key]
        self.endRemoveRows() 
Example #15
Source File: tx_builder.py    From hashmal with GNU General Public License v3.0 5 votes vote down vote up
def rowCount(self, parent=QModelIndex()):
        return 1 
Example #16
Source File: segywidgetcollection.py    From segyviewer with GNU Lesser General Public License v3.0 5 votes vote down vote up
def _modify_qtree(self, tree, items):
        for i in items:
            tree.setRowHidden(i, QModelIndex(), True) 
Example #17
Source File: data_source_model.py    From time_trial with MIT License 5 votes vote down vote up
def add_data(self, item):
#        index = self.createIndex(len(self.data_store), 0)
        self.beginInsertRows(QtCore.QModelIndex(), len(self.data_store), len(self.data_store))
        self.data_store.append(item)
        item.color = self.default_colors[len(self.data_store)-1]
#        self.dataChanged.emit(index, index)
        self.endInsertRows() 
Example #18
Source File: modeltest.py    From lic with GNU General Public License v3.0 5 votes vote down vote up
def nonDestructiveBasicTest(self):
        """
        nonDestructiveBasicTest tries to call a number of the basic functions (not all)
        to make sure the model doesn't outright segfault, testing the functions that makes sense.
        """
        assert(self.model.buddy(QtCore.QModelIndex()) == QtCore.QModelIndex())
        self.model.canFetchMore(QtCore.QModelIndex())
        assert(self.model.columnCount(QtCore.QModelIndex()) >= 0)
        assert(self.model.data(QtCore.QModelIndex(), QtCore.Qt.DisplayRole) == QtCore.QVariant())
        self.fetchingMore = True
        self.model.fetchMore(QtCore.QModelIndex())
        self.fetchingMore = False
        flags = self.model.flags(QtCore.QModelIndex())
        assert(int(flags & QtCore.Qt.ItemIsEnabled) == QtCore.Qt.ItemIsEnabled or int(flags & QtCore.Qt.ItemIsEnabled) == 0)
        self.model.hasChildren(QtCore.QModelIndex())
        self.model.hasIndex(0, 0)
        self.model.headerData(0, QtCore.Qt.Horizontal, QtCore.Qt.DisplayRole)
        self.model.index(0, 0, QtCore.QModelIndex())
        self.model.itemData(QtCore.QModelIndex())
        cache = QtCore.QVariant()
        self.model.match(QtCore.QModelIndex(), -1, cache)
        self.model.mimeTypes()
        assert(self.model.parent(QtCore.QModelIndex()) == QtCore.QModelIndex())
        assert(self.model.rowCount(QtCore.QModelIndex()) >= 0)
        variant = QtCore.QVariant()
        self.model.setData(QtCore.QModelIndex(), variant, -1)
        self.model.setHeaderData(-1, QtCore.Qt.Horizontal, QtCore.QVariant())
        self.model.setHeaderData(0, QtCore.Qt.Horizontal, QtCore.QVariant())
        self.model.setHeaderData(999999, QtCore.Qt.Horizontal, QtCore.QVariant())
        self.model.sibling(0, 0, QtCore.QModelIndex())
        self.model.span(QtCore.QModelIndex())
        self.model.supportedDropActions() 
Example #19
Source File: DblSidedTool.py    From FlatCAM with MIT License 5 votes vote down vote up
def reset_fields(self):
        self.object_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
        self.box_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex())) 
Example #20
Source File: ObjectCollection.py    From FlatCAM with MIT License 5 votes vote down vote up
def update_view(self):
        self.dataChanged.emit(Qt.QModelIndex(), Qt.QModelIndex()) 
Example #21
Source File: ObjectCollection.py    From FlatCAM with MIT License 5 votes vote down vote up
def delete_active(self):
        selections = self.view.selectedIndexes()
        if len(selections) == 0:
            return

        active = selections[0].internalPointer()
        group = active.parent_item

        self.beginRemoveRows(self.index(group.row(), 0, Qt.QModelIndex()), active.row(), active.row())

        group.remove_child(active)

        self.endRemoveRows() 
Example #22
Source File: ObjectCollection.py    From FlatCAM with MIT License 5 votes vote down vote up
def append(self, obj, active=False):
        FlatCAMApp.App.log.debug(str(inspect.stack()[1][3]) + " --> OC.append()")

        name = obj.options["name"]

        # Check promises and clear if exists
        if name in self.promises:
            self.promises.remove(name)
            FlatCAMApp.App.log.debug("Promised object %s became available." % name)
            FlatCAMApp.App.log.debug("%d promised objects remaining." % len(self.promises))

        # Prevent same name
        while name in self.get_names():
            ## Create a new name
            # Ends with number?
            FlatCAMApp.App.log.debug("new_object(): Object name (%s) exists, changing." % name)
            match = re.search(r'(.*[^\d])?(\d+)$', name)
            if match:  # Yes: Increment the number!
                base = match.group(1) or ''
                num = int(match.group(2))
                name = base + str(num + 1)
            else:  # No: add a number!
                name += "_1"
        obj.options["name"] = name

        obj.set_ui(obj.ui_type())

        # Required before appending (Qt MVC)
        group = self.group_items[obj.kind]
        group_index = self.index(group.row(), 0, Qt.QModelIndex())
        self.beginInsertRows(group_index, group.child_count(), group.child_count())

        # Append new item
        obj.item = TreeItem(None, self.icons[obj.kind], obj, group)

        # Required after appending (Qt MVC)
        self.endInsertRows()

        # Expand group
        if group.child_count() is 1:
            self.view.setExpanded(group_index, True) 
Example #23
Source File: ObjectCollection.py    From FlatCAM with MIT License 5 votes vote down vote up
def parent(self, index=None):
        if not index.isValid():
            return QtCore.QModelIndex()

        parent_item = index.internalPointer().parent_item

        if parent_item == self.root_item:
            return QtCore.QModelIndex()

        return self.createIndex(parent_item.row(), 0, parent_item) 
Example #24
Source File: ObjectCollection.py    From FlatCAM with MIT License 5 votes vote down vote up
def index(self, row, column=0, parent=None, *args, **kwargs):
        if not self.hasIndex(row, column, parent):
            return QtCore.QModelIndex()

        if not parent.isValid():
            parent_item = self.root_item
        else:
            parent_item = parent.internalPointer()

        child_item = parent_item.child(row)

        if child_item:
            return self.createIndex(row, column, child_item)
        else:
            return QtCore.QModelIndex() 
Example #25
Source File: completionlineedit.py    From pyqtggpo with GNU General Public License v2.0 5 votes vote down vote up
def parent(self, childIndex=None):
        return QModelIndex() 
Example #26
Source File: completionlineedit.py    From pyqtggpo with GNU General Public License v2.0 5 votes vote down vote up
def index(self, row, column, parent=None, *args, **kwargs):
        if column == 0:
            if 0 <= row < self.rowCount():
                return self.createIndex(row, column)
        return QModelIndex() 
Example #27
Source File: qtpandas.py    From Computable with MIT License 5 votes vote down vote up
def columnCount(self, index=QModelIndex()):
        return self.df.shape[1] 
Example #28
Source File: qtpandas.py    From Computable with MIT License 5 votes vote down vote up
def rowCount(self, index=QModelIndex()):
        return self.df.shape[0] 
Example #29
Source File: modeltest.py    From lic with GNU General Public License v3.0 4 votes vote down vote up
def data(self):
        """
        Tests self.model's implementation of QtCore.QAbstractItemModel::data()
        """
        # Invalid index should return an invalid qvariant
        assert(not self.model.data(QtCore.QModelIndex(), QtCore.Qt.DisplayRole).isValid())

        if self.model.rowCount(QtCore.QModelIndex()) == 0:
            return

        # A valid index should have a valid QtCore.QVariant data
        assert(self.model.index(0, 0, QtCore.QModelIndex()).isValid())

        # shouldn't be able to set data on an invalid index
        assert(self.model.setData(QtCore.QModelIndex(), QtCore.QVariant("foo"), QtCore.Qt.DisplayRole) == False)

        # General Purpose roles that should return a QString
        variant = self.model.data(self.model.index(0, 0, QtCore.QModelIndex()), QtCore.Qt.ToolTipRole)
        if variant.isValid():
            assert(variant.canConvert(QtCore.QVariant.String))
        variant = self.model.data(self.model.index(0, 0, QtCore.QModelIndex()), QtCore.Qt.StatusTipRole)
        if variant.isValid():
            assert(variant.canConvert(QtCore.QVariant.String))
        variant = self.model.data(self.model.index(0, 0, QtCore.QModelIndex()), QtCore.Qt.WhatsThisRole)
        if variant.isValid():
            assert(variant.canConvert(QtCore.QVariant.String))
        
        # General Purpose roles that should return a QSize
        variant = self.model.data(self.model.index(0, 0, QtCore.QModelIndex()), QtCore.Qt.SizeHintRole)
        if variant.isValid():
            assert(variant.canConvert(QtCore.QVariant.Size))

        # General Purpose roles that should return a QFont
        variant = self.model.data(self.model.index(0, 0, QtCore.QModelIndex()), QtCore.Qt.FontRole)
        if variant.isValid():
            assert(variant.canConvert(QtCore.QVariant.Font))
        
        # Check that the alignment is one we know about
        variant = self.model.data(self.model.index(0, 0, QtCore.QModelIndex()), QtCore.Qt.TextAlignmentRole)
        if variant.isValid():
            alignment = variant.toInt()[0]
            assert(alignment == (alignment & int(QtCore.Qt.AlignHorizontal_Mask | QtCore.Qt.AlignVertical_Mask)))

        # General Purpose roles that should return a QColor
        variant = self.model.data(self.model.index(0, 0, QtCore.QModelIndex()), QtCore.Qt.BackgroundColorRole)
        if variant.isValid():
            assert(variant.canConvert(QtCore.QVariant.Color))
        variant = self.model.data(self.model.index(0, 0, QtCore.QModelIndex()), QtCore.Qt.TextColorRole)
        if variant.isValid():
            assert(variant.canConvert(QtCore.QVariant.Color))

        # Check that the "check state" is one we know about.
        variant = self.model.data(self.model.index(0, 0, QtCore.QModelIndex()), QtCore.Qt.CheckStateRole)
        if variant.isValid():
            state = variant.toInt()[0]
            assert(state == QtCore.Qt.Unchecked or
                state == QtCore.Qt.PartiallyChecked or
                state == QtCore.Qt.Checked)