/** Reimplemented from QAbstractDataModel::data */ QVariant PackageModel::data(const QModelIndex &index, int role) const { QModelIndex parent = index.parent(); if ( parent.isValid() ) { AbstractPackageController *c = rootItems[parent.row()]; return c->data( index.row(), index.column(), role ); } else { if ( index.row() < rootItems.count() ) { return rootItems[ index.row() ]->data( role ); } else { return QVariant(); } } }
void DownloadViewDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { QModelIndex parent = index.parent(); if (!parent.isValid()) return; QStyleOptionViewItem options = option; bool isInstalled = (qobject_cast<const PackageModel*>(index.model())) ->isInstalled(index); if (isInstalled) { QColor c = options.palette.color(QPalette::Text); c.setAlphaF(0.4); options.palette.setColor(QPalette::Text, c); options.palette.setColor(QPalette::HighlightedText, c); } ViewDelegate::paint(painter, options, index); }
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override { if (hasIndex(index.row(), index.column(), index.parent())) { const Item &item(m_items[index.row()]); switch (role) { // Return a pixmap so the view will draw it at the given size instead of the view icon size case Qt::DecorationRole: return item.pixmap; case Qt::ForegroundRole: return item.pen.brush(); case Qt::DisplayRole: return LegendModel::tr(item.label.constData()); case Qt::SizeHintRole: return QSize(-1, (item.pixmap.height() / item.pixmap.devicePixelRatio()) + (2 * 2)); } } return QVariant(); }
void SampleView::updateSampleChemSymbol(QString chemSymbol){ // Grab model index of currently selected sample. QModelIndex currentIndex = sampleListView_->selectionModel()->currentIndex(); // In order to keep the sample name constant as the user updates the chemical symbol, grab the index of the name value // of the currently selected sample and store it as a string. Sample name is index-0 of model. QModelIndex indexOfName(currentIndex.model()->index(currentIndex.row(), 0, currentIndex.parent())); QString currentNameValue_ = (indexOfName.data(Qt::DisplayRole).toString()); // The updated sample title. QStandardItem *item = new QStandardItem(currentNameValue_ + " - " + chemSymbol); // Set the model at the selected index row. Recall column 4 (index 3) is the sample title. sampleModel_->setItem(currentIndex.row(), 3, item); // Ensure proper mapping from edit widgets to model. Behaviour is buggy without it. modelToWidgetMapper_->submit(); // Once a single character has changed the focus is lost on the sampleListView and any additional changes to the edit will not be // noticed by the sampleListView and cause program to crash. This returns focus, and more imporantly the index selection, back to the sampleListView. sampleListView_->selectionModel()->setCurrentIndex(currentIndex, QItemSelectionModel::Select); }
bool SpectraOutputCatalog::setData(const QModelIndex & index, const QVariant & value, int role) { if (index.parent()!=QModelIndex() || m_readOnly) return false; if (role==Qt::DisplayRole || role==Qt::EditRole) { switch (index.column()) { case NameColumn: return false; case DepthColumn: { bool ok; const double d = value.toDouble(&ok); if (ok) { m_outputs[index.row()]->setDepth(d); } else { return false; } break; } case TypeColumn: { bool ok; AbstractMotion::Type type = AbstractMotion::variantToType(value, &ok); if (ok) m_outputs[index.row()]->setType(type); else return false; break; } } } emit wasModified(); emit dataChanged(index, index); return true; }
/*! * \param name Server name * \param newname New server name * \param network Network name (optional) * * Changes a server name.\n * If network isn't specified, this applies to servers without parents. */ void ServerModel::renameServer(QString name, QString newname, QString network) { QStandardItem *parent; if (network.length() == 0) network = "NONE"; if (network == "NONE") parent = invisibleRootItem(); else parent = itemFromIndex( netmap.value(network) ); QModelIndex parentIdx = indexFromItem(parent); // Parent index QModelIndex current; // Item's index if (network != "NONE") { for (int r = 0 ;; r++) { QModelIndex idx = parentIdx.child(r, 0); if (! idx.isValid()) return; // No relevant child found, stop. if (idx.data().toString() == name) { current = idx; break; } } } else { current = nonemap.value(name); nonemap.remove(name); nonemap.insert(newname, current); } int row = current.row(); QModelIndex serverIndex = index(row, 0, current.parent()); QStandardItem *item = itemFromIndex(serverIndex); item->setText(newname); }
QModelIndex TreeViewFind::nextIndex(const QModelIndex &idx, bool *wrapped) const { if (wrapped) *wrapped = false; QAbstractItemModel *model = d->m_view->model(); // pathological if (!idx.isValid()) return model->index(0, 0); if (model->rowCount(idx) > 0) { // node with children return idx.child(0, 0); } // leaf node QModelIndex nextIndex; QModelIndex current = idx; while (!nextIndex.isValid()) { int row = current.row(); int column = current.column(); current = current.parent(); if (column + 1 < model->columnCount(current)) { nextIndex = model->index(row, column + 1, current); } else { if (row + 1 < model->rowCount(current)) { // Same parent has another child nextIndex = model->index(row + 1, 0, current); } else { // go up one parent if (!current.isValid()) { // we start from the beginning if (wrapped) *wrapped = true; nextIndex = model->index(0, 0); } } } } return nextIndex; }
/*! \internal */ QVariant QDeclarativePlaceEditorialModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); if (index.row() >= rowCount(index.parent()) || index.row() < 0) return QVariant(); const QPlaceEditorial &description = m_content.value(index.row()); switch (role) { case TextRole: return description.text(); case TitleRole: return description.title(); case LanguageRole: return description.language(); } return QDeclarativePlaceContentModel::data(index, role); }
/** * Override of pathFromIndex(). */ QString ConceptHierarchyCompleter::pathFromIndex(const QModelIndex &index) const { // Calculate the last path. QStringList lineage; for (QModelIndex i = index; i.isValid(); i = i.parent()) lineage.prepend(model()->data(i, completionRole()).toString()); QString path = lineage.join(this->lineageSeparator); // Make sure that previously entered paths are also kept. QLineEdit * widget = (QLineEdit *) this->widget(); QStringList list = widget->text().split(this->entrySeparator); if (list.size() > 1) { // Delete the last path in the list, which was uncomplete. list.removeLast(); // Add the *completed* last path again at the end of the list. list.append(path); // Merge all paths together again. path = list.join(this->entrySeparator); } return path; }
QVariant StackedDocumentModel::data( const QModelIndex& index, int role ) const { if ( index == QModelIndex() || index.row() < 0 || index.row() >= rowCount( index.parent() ) || index.column() != 0 ) { return QVariant(); } Document* document = mStacker->document( index.row() ); switch ( role ) { case Qt::DecorationRole: return document->property( Document::Decoration ); case Qt::DisplayRole: return document->property( Document::Title ).toString(); case Qt::ToolTipRole: return document->property( Document::FilePath ); case Qt::CheckStateRole: return document->property( Document::ReadOnly ).toBool() ? Qt::Checked : Qt::Unchecked; } return QVariant(); }
QModelIndex FindDialog::getNextIndex(QModelIndex index) { QAbstractItemModel *model = m_parent->ui.tree_view->model(); assert(NULL != model); QModelIndex firstChild = model->index(0, 0, index); if (firstChild.isValid()) return firstChild; QModelIndex current = index, ancester = index.parent(); while (ancester.isValid()) { int row = current.row(); QModelIndex littleBrother = ancester.child(row + 1, 0); if (littleBrother.isValid()) return littleBrother; current = ancester; ancester = ancester.parent(); } return QModelIndex(); // 没有节点了 }
/*! \internal */ QVariant QDeclarativePlaceContentModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); if (index.row() >= rowCount(index.parent()) || index.row() < 0) return QVariant(); const QPlaceContent &content = m_content.value(index.row()); switch (role) { case SupplierRole: return QVariant::fromValue(static_cast<QObject *>(m_suppliers.value(content.supplier().supplierId()))); case PlaceUserRole: return QVariant::fromValue(static_cast<QObject *>(m_users.value(content.user().userId()))); case AttributionRole: return content.attribution(); default: return QVariant(); } }
void ViewDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const { QModelIndex parent = index.parent(); if ( !parent.isValid() ) return; QStyleOptionViewItem options = option; //if a package has focus, ensure that it's state is selected //so that it will always show with a filled in background highlight //(as opposed to a border/box) if (options.state & QStyle::State_HasFocus) { options.showDecorationSelected = true; options.state = options.state | QStyle::State_Selected; } QtopiaItemDelegate::paint( painter, options, index ); }
int QProtobufModel::rowCount(const QModelIndex &_index) const { PBMessage* msg = getMessage(_index); if (msg == m_rootItem) return 1; PBMessage* parent = getMessage(_index.parent()); auto descr = parent->GetDescriptor(); auto field = descr->FindFieldByName(msg->GetTypeName()); if (field->is_repeated()) { int count = parent->GetReflection()->FieldSize(*parent, field); return count; } else return 1; }
int WangColorModel::rowCount(const QModelIndex &parent) const { if (!mWangSet) return 0; if (!parent.isValid()) return 2; if (!parent.parent().isValid()) { if (parent.row() == 0) { int n = mWangSet->edgeColorCount(); return (n == 1)? 0 : n; } if (parent.row() == 1) { int n = mWangSet->cornerColorCount(); return (n == 1)? 0 : n; } } return 0; }
int MimetypeItemModel::rowCount(const QModelIndex& parent) const { if (parent.column() > 0) { return 0; } if (parent.parent().isValid()) { return 0; } else if (parent.isValid()) { QString media = XdgMimeInfoCache::mediatypes().value(parent.row()); return XdgMimeInfoCache::subtypes(media).size(); } else { return XdgMimeInfoCache::mediatypes().size(); } }
void MainWindow::onTableSelectionChanged(QItemSelection const & selected, QItemSelection const & /*deselected*/) { BoolGuard guard(m_ignoreTableSelection); QModelIndexList lst = selected.indexes(); if (lst.empty()) return; m_ui->m_fileTree->selectionModel()->select(selected, QItemSelectionModel::ClearAndSelect); QModelIndex item = lst.first().parent(); while (item != QModelIndex()) { if (!m_ui->m_fileTree->isExpanded(item)) m_ui->m_fileTree->expand(item); item = item.parent(); } QModelIndex sourceIndex = m_model->mapToSource(lst.first()); if (m_fileModel->canFetchMore(sourceIndex)) m_fileModel->fetchMore(sourceIndex); }
void LibraryModel::DeleteGenerator(const QModelIndex& index) { if (index.parent() != ItemToIndex(smart_playlist_node_)) return; // Remove the item from the tree smart_playlist_node_->DeleteNotify(index.row()); QSettings s; s.beginGroup(kSmartPlaylistsSettingsGroup); // Rewrite all the items to the settings s.beginWriteArray(backend_->songs_table(), smart_playlist_node_->children.count()); int i = 0; for (LibraryItem* item : smart_playlist_node_->children) { s.setArrayIndex(i++); s.setValue("name", item->display_text); s.setValue("type", item->key); s.setValue("data", item->smart_playlist_data); } s.endArray(); }
/*! \internal */ QVariant QDeclarativePlaceImageModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); if (index.row() >= rowCount(index.parent()) || index.row() < 0) return QVariant(); const QPlaceImage &image = m_content.value(index.row()); switch (role) { case UrlRole: return image.url(); case ImageIdRole: return image.imageId(); case MimeTypeRole: return image.mimeType(); } return QDeclarativePlaceContentModel::data(index, role); }
void BookmarksDialog::newFolder() { QModelIndex currentIndex = tree->currentIndex(); QModelIndex idx = currentIndex; QModelIndex sourceIndex = m_proxyModel->mapToSource(idx); const BookmarkNode *sourceNode = m_bookmarksModel->node(sourceIndex); int row = -1; // default: append new folder as last item in selected folder if (sourceNode && sourceNode->type() != BookmarkNode::Folder) { // if selected item is not a folder, add new folder to parent folder, // but direcly below the selected item idx = idx.parent(); row = currentIndex.row() + 1; } if (!idx.isValid()) idx = m_proxyModel->index(1, 0); // Select Bookmarks menu as default idx = m_proxyModel->mapToSource(idx); BookmarkNode *parent = m_bookmarksManager->bookmarksModel()->node(idx); BookmarkNode *node = new BookmarkNode(BookmarkNode::Folder); node->title = tr("New Folder"); m_bookmarksManager->addBookmark(parent, node, row); }
QVariant ClientResourceModel::data(const QModelIndex& index, int role) const { if (role == Qt::DecorationRole && index.column() == 0) { if (!index.parent().isValid()) return m_iconProvider.icon(QFileIconProvider::Drive); if (hasChildren(index)) return m_iconProvider.icon(QFileIconProvider::Folder); #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) QList<QMimeType> types = m_mimeDb.mimeTypesForFileName(index.data(Qt::DisplayRole).toString()); foreach( const QMimeType &mt, types) { QIcon icon = QIcon::fromTheme(mt.iconName()); if (!icon.isNull()) return icon; icon = QIcon::fromTheme(mt.genericIconName()); if (!icon.isNull()) return icon; } #endif return m_iconProvider.icon(QFileIconProvider::File); }
void HistoryTreeModel::sourceRowsInserted(const QModelIndex &parent, int start, int end) { Q_UNUSED(parent); // Avoid warnings when compiling release Q_ASSERT(!parent.isValid()); if (start != 0 || start != end) { m_sourceRowCache.clear(); reset(); return; } m_sourceRowCache.clear(); QModelIndex treeIndex = mapFromSource(sourceModel()->index(start, 0)); QModelIndex treeParent = treeIndex.parent(); if (rowCount(treeParent) == 1) { beginInsertRows(QModelIndex(), 0, 0); endInsertRows(); } else { beginInsertRows(treeParent, treeIndex.row(), treeIndex.row()); endInsertRows(); } }
void Desktopwidget::stackRight (void) { QModelIndex ind = _view->getSelectedItem (); if (!ind.isValid ()) return; QModelIndex parent = ind.parent (); int count = ind.model ()->rowCount (parent); int row = ind.row (); if (row < count - 1) row++; else if (count) row = 0; if (count) { _view->setSelectionRange (row, 1); _view->scrollTo (ind.model ()->index (row, 0, parent)); } }
QString KMyMoneyAccountCombo::Private::fullAccountName(const QAbstractItemModel* model, const QModelIndex& _index, bool includeMainCategory) const { QString rc; if(_index.isValid()) { QModelIndex index = _index; QString sep; do { rc = QString("%1%2%3").arg(model->data(index).toString()).arg(sep).arg(rc); sep = QLatin1String(":"); index = index.parent(); } while(index.isValid()); if(!includeMainCategory) { QRegExp mainCategory(QString("[^%1]+%2(.*)").arg(sep).arg(sep)); if(mainCategory.exactMatch(rc)) { rc = mainCategory.cap(1); } } } return rc; }
void TreeProxyModel::onRowsInserted( const QModelIndex& parent, int /* start */, int /* end */ ) { if ( m_filter.isEmpty() ) return; if ( sender() != m_model ) return; PlayableItem* pi = m_model->itemFromIndex( m_model->index( parent.row(), 0, parent.parent() ) ); if ( pi->artist().isNull() ) return; Tomahawk::AlbumsRequest* cmd = m_model->collection()->requestAlbums( pi->artist() ); cmd->setFilter( m_filter ); connect( dynamic_cast< QObject* >( cmd ), SIGNAL( albums( QList<Tomahawk::album_ptr> ) ), SLOT( onFilterAlbums( QList<Tomahawk::album_ptr> ) ) ); cmd->enqueue(); }
void DiagnosticCodeSelector::newSelection(const QModelIndex& index) { if (!index.isValid()) { return; } // Now, we want to get an index to potentially different columns of // the same object. Note that index.row() is NOT unique, it's just the row // number for a given parent. // To get a different column, we go via the parent back to the child: // http://doc.qt.io/qt-5/qmodelindex.html#details QModelIndex parent = index.parent(); QModelIndex code_index = parent.child( index.row(), DiagnosticCode::COLUMN_CODE); QModelIndex description_index = parent.child( index.row(), DiagnosticCode::COLUMN_DESCRIPTION); QString code = code_index.data().toString(); QString description = description_index.data().toString(); emit codeChanged(code, description); emit finished(); }
void TreeProxyModel::onRowsInserted( const QModelIndex& parent, int /* start */, int /* end */ ) { if ( m_filter.isEmpty() ) return; if ( sender() != m_model ) return; TreeModelItem* pi = m_model->itemFromIndex( m_model->index( parent.row(), 0, parent.parent() ) ); if ( pi->artist().isNull() ) return; DatabaseCommand_AllAlbums* cmd = new DatabaseCommand_AllAlbums( m_model->collection() ); cmd->setArtist( pi->artist() ); cmd->setFilter( m_filter ); connect( cmd, SIGNAL( albums( QList<Tomahawk::album_ptr>, QVariant ) ), SLOT( onFilterAlbums( QList<Tomahawk::album_ptr> ) ) ); Database::instance()->enqueue( QSharedPointer<DatabaseCommand>( cmd ) ); }
void mail_listview::remove_msg(mail_msg* msg, bool select_next) { DBG_PRINTF(8, "mail_listview::remove_msg(mail_id=%d, select_next=%d)", msg->get_id(), select_next); mail_item_model* model = this->model(); QStandardItem* item = model->item_from_id(msg->get_id()); if (!item) return; QStandardItem* nearest = NULL; QSet<QStandardItem*> expanded_set; if (select_next) { QModelIndex index_below = indexBelow(item->index()); if (index_below.isValid()) { nearest = model->itemFromIndex(index_below); DBG_PRINTF(5, "nearest got from index_below"); } else { QModelIndex index_above = indexAbove(item->index()); if (index_above.isValid()) { nearest = model->itemFromIndex(index_above); DBG_PRINTF(5, "nearest got from index_above"); } } if (nearest) { /* record the expansion states of all child items to set them back after the parent's removal */ collect_expansion_states(item, expanded_set); } } model->remove_msg(msg); if (select_next && nearest) { QModelIndex index = nearest->index(); DBG_PRINTF(5,"making current index row=%d col=%d parent is valid=%d", index.row(), index.column(), index.parent().isValid()?1:0); foreach (QStandardItem *child, expanded_set) { setExpanded(child->index(), true); }
void LocationBar::setIndex( const QModelIndex &index ) { qDeleteAll( buttons ); buttons.clear(); qDeleteAll( actions ); actions.clear(); QModelIndex i = index; bool first = true; while( true ) { QString text = model->getTitle( i ); QAbstractButton *btn = new LocationButton( text, first, !first, this ); btn->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Fixed ); buttons.append( btn ); QAction *action = new QAction( text, this ); actions.append( action ); CONNECT( btn, clicked(), action, trigger() ); mapper->setMapping( action, model->itemId( i ) ); CONNECT( action, triggered(), mapper, map() ); first = false; if( i.isValid() ) i = i.parent(); else break; } QString prefix; for( int a = actions.count() - 1; a >= 0 ; a-- ) { actions[a]->setText( prefix + actions[a]->text() ); prefix += QString(" "); } if( isVisible() ) layOut( size() ); }
QVariant JobStatusModel::data( const QModelIndex& index, int role ) const { if ( !hasIndex( index.row(), index.column(), index.parent() ) ) return QVariant(); JobStatusItem* item = m_items[ index.row() ]; switch ( role ) { case Qt::DecorationRole: return item->icon(); case Qt::ToolTipRole: case Qt::DisplayRole: { if ( m_collapseCount.contains( item->type() ) ) return m_collapseCount[ item->type() ].last()->mainText(); else return item->mainText(); } case RightColumnRole: { if ( m_collapseCount.contains( item->type() ) ) return m_collapseCount[ item->type() ].count(); else return item->rightColumnText(); } case AllowMultiLineRole: return item->allowMultiLine(); case JobDataRole: return QVariant::fromValue< JobStatusItem* >( item ); } return QVariant(); }