QWidget* fastEdit(const QModelIndex &i) // Consider this as QAbstractItemView::edit( ) { QWidget *v = itemDelegate()->createEditor(viewport(), viewOptions(), i); if (v) itemDelegate()->setEditorData(v, i); return v; }
void ColorListBox::addItem(ColorPixmapItem* item, ColorListBox::PixmapType type) { QListWidget::addItem(item); if (itemDelegate()) delete itemDelegate(); if (type == ColorListBox::fancyPixmap) setItemDelegate(new ColorFancyItemDelegate()); else if (type == ColorListBox::widePixmap) setItemDelegate(new ColorWideItemDelegate()); else if (type == ColorListBox::smallPixmap) setItemDelegate(new ColorSmallItemDelegate()); }
void ColorListBox::insertFancyPixmapItems(ColorList& list) { ColorList::Iterator it; ScribusDoc* doc = list.document(); for (it = list.begin(); it != list.end(); ++it) { if (it.key() == CommonStrings::None || it.key() == CommonStrings::tr_NoneColor) continue; addItem( new ColorPixmapItem(it.value(), doc, it.key()) ); } if (itemDelegate()) delete itemDelegate(); setItemDelegate(new ColorFancyItemDelegate()); }
void TabSwitcher::showSwitcher() { setModel(new AppModel(this, m_tabs)); if (!model()->rowCount()) return; QStyleOptionViewItem option; int w = 0; int h = 0; int maxApp = 15; for(int i = 0; i < model()->rowCount(); ++i){ QSize siz = itemDelegate()->sizeHint(option, model()->index(i, 0)); w = qMax(w, siz.width()); h += siz.height(); if (i > maxApp) break; } w += 2 * frameWidth(); h += 2 * frameWidth(); resize(w, h); QPoint pos = m_tabs->mapToGlobal(m_tabs->geometry().topLeft()); move(pos.x()+m_tabs->geometry().width()/2 - w / 2, pos.y()+m_tabs->geometry().height()/2 - h / 2); show(); }
ActionView::ActionView(QWidget * parent /*= nullptr*/) : QListView(parent) , mModel{new QStandardItemModel{this}} , mProxy{new QSortFilterProxyModel{this}} , mMaxItemsToShow(10) { setEditTriggers(QAbstractItemView::NoEditTriggers); setSizeAdjustPolicy(AdjustToContents); setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setSelectionBehavior(SelectRows); setSelectionMode(SingleSelection); SingleActivateStyle * s = new SingleActivateStyle; s->setParent(this); setStyle(s); mProxy->setSourceModel(mModel); mProxy->setDynamicSortFilter(true); mProxy->setFilterRole(FilterRole); mProxy->setFilterCaseSensitivity(Qt::CaseInsensitive); mProxy->sort(0); { QScopedPointer<QItemSelectionModel> guard{selectionModel()}; setModel(mProxy); } { QScopedPointer<QAbstractItemDelegate> guard{itemDelegate()}; setItemDelegate(new DelayedIconDelegate{this}); } connect(this, &QAbstractItemView::activated, this, &ActionView::onActivated); }
void RepoTreeView::showRepoItemToolTip(const RepoItem *item, const QPoint& pos, const QRect& rect) { RepoItemDelegate *delegate = (RepoItemDelegate *)itemDelegate(); delegate->showRepoItemToolTip(item, pos, viewport(), rect); }
void ActionComboBoxWidget::paintEvent(QPaintEvent *event) { Q_UNUSED(event); QPainter painter(this); QStyleOptionComboBox comboBoxOption; comboBoxOption.initFrom(this); comboBoxOption.editable = false; style()->drawComplexControl(QStyle::CC_ComboBox, &comboBoxOption, &painter, this); QStyleOptionViewItem viewItemOption; viewItemOption.initFrom(this); viewItemOption.displayAlignment |= Qt::AlignVCenter; if (currentIndex() >= 0) { itemDelegate()->paint(&painter, viewItemOption, m_view->getModel()->index(currentIndex(), 0)); } else { const int textMargin = (style()->pixelMetric(QStyle::PM_FocusFrameHMargin, 0, this) + 1); painter.setFont(viewItemOption.font); painter.drawText(viewItemOption.rect.adjusted(textMargin, 0, -textMargin, 0), Qt::AlignVCenter, tr("Select Action")); } }
void QmlConsoleView::drawBranches(QPainter *painter, const QRect &rect, const QModelIndex &index) const { static_cast<QmlConsoleItemDelegate *>(itemDelegate())->drawBackground(painter, rect, index, false); Utils::TreeView::drawBranches(painter, rect, index); }
void PivotTable::setNodesTotalNumber ( int newNodesNumb ) { PivotTableDelegate* delegate = dynamic_cast<PivotTableDelegate*>( itemDelegate() ); Q_ASSERT( delegate != 0 ); delegate->setNodesTotalNumber( newNodesNumb ); }
void TransfersView::slotItemActivated(const QModelIndex & index) { if (!index.isValid()) return; TransferTreeModel * transferTreeModel = KGet::model(); ModelItem * item = transferTreeModel->itemFromIndex(index); TransfersViewDelegate *view_delegate = static_cast <TransfersViewDelegate *> (itemDelegate()); if(!item) return; if(!item->isGroup() && index.column() == 0) { if(!view_delegate->isExtended(index)) { TransferHandler *handler = item->asTransfer()->transferHandler(); QWidget *widget = getDetailsWidgetForTransfer(handler); m_editingIndexes.append(index); view_delegate->extendItem(widget, index); } else { m_editingIndexes.removeAll(index); view_delegate->contractItem(index); } KGet::actionCollection()->action("transfer_show_details")->setChecked(view_delegate->isExtended(index)); } else if (!item->isGroup() && static_cast<TransferModelItem*>(item)->transferHandler()->status() == Job::Finished) { new KRun(static_cast<TransferModelItem*>(item)->transferHandler()->dest(), this); } }
void UIInformationView::keyPressEvent(QKeyEvent *pEvent) { /* Copy the text: */ if (pEvent == QKeySequence::Copy) { QString strText; /* Get Selection model: */ QItemSelectionModel *pSelectionModel = selectionModel(); if (pSelectionModel) { /* Check all the selected-indexes and copy the text: */ foreach (const QModelIndex &index, pSelectionModel->selectedIndexes()) { UIInformationItem *pItem = dynamic_cast<UIInformationItem*>(itemDelegate(index)); if (pItem) { /* Update the corresponding data: */ pItem->updateData(index); /* Get and add the html-data of item: */ strText.append(pItem->htmlData()); } } } /* Set the text to text-edit and copy from it: */ m_pTextEdit->setText(strText); m_pTextEdit->selectAll(); m_pTextEdit->copy(); /* Accept/acknowledge event: */ pEvent->accept(); }
ComboBox::ComboBox(QWidget *parent, const QString& defaultText) : QComboBox(parent) { if (! defaultText.isEmpty()) mDefaultText = defaultText; else mDefaultText = tr("None"); clear(); if (itemDelegate()) itemDelegate()->deleteLater(); mItemDelegate = new ComboBoxItemDelegate(this, this); this->setItemDelegate(mItemDelegate); connect(mItemDelegate, SIGNAL(removeItem(int)), this, SLOT(onRemoveItem(int))); this->view()->viewport()->installEventFilter(this); connect(this, SIGNAL(activated(int)), this, SLOT(onItemActivated(int))); }
void TreeView::startDrag(Qt::DropActions supportedActions) { QModelIndex index = selectedIndexes().value(0); if (!index.isValid()) return; QMimeData *data = model()->mimeData(QModelIndexList() << index); if (!data) return; QRect rect; QPixmap pixmap; QPoint point; { QAbstractItemDelegate *delegate = itemDelegate(index); QStyleOptionViewItemV4 option = viewOptions(); option.locale = this->locale(); option.locale.setNumberOptions(QLocale::OmitGroupSeparator); option.widget = this; option.state |= QStyle::State_Selected; option.rect = visualRect(index); point = option.rect.topLeft(); option.rect.moveTo(0, 0); option.rect.setSize(delegate->sizeHint(option, index)); rect = option.rect; pixmap = QPixmap(rect.size()); pixmap.fill(Qt::transparent); QPainter painter(&pixmap); delegate->paint(&painter, option, index); } QDrag *drag = new QDrag(this); drag->setPixmap(pixmap); drag->setMimeData(data); point = QCursor::pos() - viewport()->mapToGlobal(point); drag->setHotSpot(point); // drag->setHotSpot(QCursor::pos() - rect.topLeft()); Qt::DropAction setDefaultDropAction = QAbstractItemView::defaultDropAction(); Qt::DropAction defaultDropAction = Qt::IgnoreAction; if (setDefaultDropAction != Qt::IgnoreAction && (supportedActions & setDefaultDropAction)) defaultDropAction = setDefaultDropAction; else if (supportedActions & Qt::CopyAction && dragDropMode() != QAbstractItemView::InternalMove) defaultDropAction = Qt::CopyAction; if (drag->exec(supportedActions, defaultDropAction) == Qt::IgnoreAction && index.data(ItemTypeRole).toInt() == ContactType) { if (QWidget *widget = QApplication::topLevelAt(QCursor::pos())) { if (widget->window() == this->window()) return; } Event ev("contact-list-drop", QCursor::pos() - point, index.data(BuddyRole)); ev.send(); } // debug() << "DropAction" << drag->exec(supportedActions, defaultDropAction); // if (drag->exec(supportedActions, defaultDropAction) == Qt::MoveAction) // d->clearOrRemove(); // {} }
void ListView_Impl::reset() { QListView::reset(); ListView_Delegate *delegate = qobject_cast<ListView_Delegate *>(itemDelegate()); if (delegate) delegate->invalidate(); }
void views::treeView::commitData ( QWidget * editor ) { QModelIndexList list=selectedIndexes(); QVariant var=QVariant::fromValue<QModelIndexList>(list); itemDelegate()->setProperty("modelList",var ); QTreeView::commitData(editor); return ; }
void QvisPlotListBox::setApplyOperators(bool val) { QPlotDelegate *delegate = dynamic_cast<QPlotDelegate *>(itemDelegate()); if(delegate != NULL) { delegate->setApplyOperators(val); update(); } }
void PsiContactListView::contactAlert(const QModelIndex& realIndex) { QModelIndex index = proxyIndex(realIndex); itemDelegate()->contactAlert(index); bool alerting = index.data(ContactListModel::IsAlertingRole).toBool(); if (alerting && PsiOptions::instance()->getOption("options.ui.contactlist.ensure-contact-visible-on-event").toBool()) { ensureVisible(index); } }
/** * QT 4.7.1 拖拽的函数默认调用过程: * * Model.supportedDropActions() * * View.startDrag() * Model.mimeData() // 获取拖拽数据 * Model.supportedDragActions() * Model.supportedDropActions() * QDrag.exec() // 阻塞,直到拖拽结束 * * // 在拖拽过程中,如果拖拽进入某个节点 * View.dragEnterEvent() // 默认实现会拒绝拖拽,需要重写并调用 QDragEnterEvent.acceptProposedAction() * Model.mimeTypes() * View.dragMoveEvent() * Model.mimeTypes() * View.dragLeaveEvent() * * // 拖拽结束后 * View.dropEvent() * Model.dropMimeData() // QT用来接收数据 * * // exec()执行完后,如果是 MoveAction,则调用 * Model.removeSelectedRows() // QT用来删除数据 * * 上述默认过程对跨 View 拖放支持不好, 下面的startDrag()函数摘自默认实现,并 * 将部分代码替换掉以免调用removeSelectedRows()函数 */ void DirectoryTree::startDrag(Qt::DropActions) { const QModelIndexList indexes = selectedIndexes(); if (indexes.count() == 0) return; // setup pixmap QRect rect = visualRect(indexes.at(0)); QList<QRect> rects; for (size_t i = 0, s = indexes.count(); i < s; ++i) { rects.append(visualRect(indexes.at(i))); rect |= rects.at(i); } rect = rect.intersected(viewport()->rect()); QPixmap pixmap(rect.size()); pixmap.fill(palette().base().color()); QPainter painter(&pixmap); QStyleOptionViewItem option = viewOptions(); option.state |= QStyle::State_Selected; for (size_t j = 0, s = indexes.count(); j < s; ++j) { option.rect = QRect(rects.at(j).topLeft() - rect.topLeft(), rects.at(j).size()); itemDelegate()->paint(&painter, option, indexes.at(j)); } painter.end(); // create drag object QDrag *drag = new QDrag(this); drag->setPixmap(pixmap); drag->setMimeData(model()->mimeData(indexes)); drag->setHotSpot(viewport()->mapFromGlobal(QCursor::pos()) - rect.topLeft()); /** * 在拖动之前,先把 parent 和 row 保存下来, * 在 Model.dropMimeData() 中统一进行底层数据和视图层数据的修改, * 避免索引混乱 */ DirectoryTreeModel *m = (DirectoryTreeModel*) model(); QModelIndex parent = indexes.at(0).parent(); int row = indexes.at(0).row(); m->prepare_draging_drop(parent, row); /* * 这里的原来的实现 removeSelectedRows() 是我们不需要的, 且 QDrag->start() 总是允许 Qt::CopyAction * 而我们需要的是,内部用 Qt::MoveAction,外部用 Qt::CopyAction且内部不支持 Qt::CopyAction且内部不支持 * Qt::CopyAction,否则导致错误 * if (drag->start(supportedActions) == Qt::MoveAction) removeSelectedRows(); */ drag->exec(Qt::MoveAction | Qt::CopyAction, Qt::MoveAction); }
QWidget* SettingsBottomView::createEditorForIndex(const QModelIndex& index) { QAbstractItemDelegate* delegate = itemDelegate(index); QStyleOptionViewItem sovi; sovi.displayAlignment = Qt::AlignRight; sovi.decorationPosition = QStyleOptionViewItem::Right; QWidget* editor = delegate->createEditor(NULL, sovi, index); delegate->setEditorData(editor, index); return editor; }
void ListView_Impl::dataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight, const QVector<int>& roles) { #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)) QAbstractItemView::dataChanged(topLeft, bottomRight, roles); #else QAbstractItemView::dataChanged(topLeft, bottomRight); #endif ListView_Delegate *delegate = qobject_cast<ListView_Delegate *>(itemDelegate()); if (delegate) delegate->invalidate(); }
// Please be carefull when reimplementing methods which are used to inform the view about changes to the data // to be on the safe side: call QTreeView's method aswell (or TreeViewTouch's) BufferView::BufferView(QWidget *parent) : TreeViewTouch(parent) { connect(this, SIGNAL(collapsed(const QModelIndex &)), SLOT(storeExpandedState(const QModelIndex &))); connect(this, SIGNAL(expanded(const QModelIndex &)), SLOT(storeExpandedState(const QModelIndex &))); setSelectionMode(QAbstractItemView::ExtendedSelection); QAbstractItemDelegate *oldDelegate = itemDelegate(); BufferViewDelegate *tristateDelegate = new BufferViewDelegate(this); setItemDelegate(tristateDelegate); delete oldDelegate; }
int toResultTableView::visibleRows() const { // TODO this is ugly hack and needs some validation //this->visibleRegion(); int h = sizeHintForRow(0); QAbstractItemDelegate *d = itemDelegate(); QSize s = d->sizeHint(QStyleOptionViewItem(), QModelIndex()); int hh = height(); int hhh = s.height(); int rows = hh / s.height() + 1; if (rows < toConfigurationNewSingle::Instance().option(ToConfiguration::Database::InitialFetchInt).toInt()) return toConfigurationNewSingle::Instance().option(ToConfiguration::Database::InitialFetchInt).toInt(); return rows; }
bool PsiFilteredContactListView::handleKeyPressEvent(QKeyEvent* e) { updateKeyboardModifiers(e); switch (e->key()) { case Qt::Key_Enter: case Qt::Key_Return: { if (state() == EditingState) return false; QModelIndex currentIndex = this->currentIndex(); if (currentIndex.isValid()) { // TODO: probably should select the item from the filteredListView_ // in the contactListView_ as well activate(currentIndex); } return true; } case Qt::Key_Home: { selectIndex(0); return true; } case Qt::Key_End: { selectIndex(model()->rowCount()-1); return true; } case Qt::Key_Up: case Qt::Key_Down: { moveSelection(1, e->key() == Qt::Key_Up ? Backward : Forward); return true; } case Qt::Key_PageUp: case Qt::Key_PageDown: { int delta = 0; QModelIndex index = model()->index(0, 0, QModelIndex()); if (index.isValid()) { int itemHeight = itemDelegate()->sizeHint(QStyleOptionViewItem(), index).height(); if (itemHeight) delta = viewport()->height() / itemHeight; } moveSelection(delta, e->key() == Qt::Key_PageUp ? Backward : Forward); return true; } default: ; } return false; }
void KoResourceSelector::paintEvent( QPaintEvent *pe ) { QComboBox::paintEvent( pe ); QStyleOptionComboBox option; option.initFrom( this ); QRect r = style()->subControlRect( QStyle::CC_ComboBox, &option, QStyle::SC_ComboBoxEditField, this ); QStyleOptionViewItem viewOption; viewOption.initFrom( this ); viewOption.rect = r; QPainter painter( this ); itemDelegate()->paint( &painter, viewOption, view()->currentIndex() ); }
void QgsLocatorResultsView::recalculateSize() { // try to show about 20 rows int rowSize = 20 * itemDelegate()->sizeHint( viewOptions(), model()->index( 0, 0 ) ).height(); // try to take up a sensible portion of window width (about half) int width = std::max( 300, window()->size().width() / 2 ); QSize newSize( width, rowSize + frameWidth() * 2 ); // resize the floating widget this is contained within parentWidget()->resize( newSize ); QTreeView::resize( newSize ); header()->resizeSection( 0, width / 2 ); header()->resizeSection( 1, 0 ); }
QSize ResultList::sizeHint() const { static QSize sizePerRow; if (sizePerRow.isEmpty()) { const QStyleOptionViewItem opt; const QModelIndex idx; sizePerRow = itemDelegate()->sizeHint(opt, idx); } if (sizePerRow.isEmpty()) return QListView::sizeHint(); // ### where does the number 11 come from here? return QSize(width(), (sizePerRow.height() * model()->rowCount()) + 11); }
void ItemList::startDrag(Qt::DropActions supportedActions) { const QModelIndexList indexes = selectedIndexes(); if (indexes.count() == 0) return; // setup pixmap QRect rect = visualRect(indexes.at(0)); QList<QRect> rects; for (size_t i = 0, s = indexes.count(); i < s; ++i) { rects.append(visualRect(indexes.at(i))); rect |= rects.at(i); } rect = rect.intersected(viewport()->rect()); QPixmap pixmap(rect.size()); pixmap.fill(palette().base().color()); QPainter painter(&pixmap); QStyleOptionViewItem option = viewOptions(); option.state |= QStyle::State_Selected; for (size_t j = 0, s = indexes.count(); j < s; ++j) { option.rect = QRect(rects.at(j).topLeft() - rect.topLeft(), rects.at(j).size()); itemDelegate()->paint(&painter, option, indexes.at(j)); } painter.end(); // create drag object QDrag *drag = new QDrag(this); drag->setPixmap(pixmap); drag->setMimeData(model()->mimeData(indexes)); drag->setHotSpot(viewport()->mapFromGlobal(QCursor::pos()) - rect.topLeft()); /* * 这里的原来的实现被注释掉 if (drag->start(supportedActions) == Qt::MoveAction) removeSelectedRows(); */ ItemListModel *m = dynamic_cast<ItemListModel*>(model()); assert(NULL != m); /// QAbstractItemModel::supportedDragActions()居然不是虚函数,我C了!!! Qt::DropAction rs = drag->exec(Qt::MoveAction | Qt::CopyAction, Qt::MoveAction); if (rs == Qt::MoveAction) { assert(indexes.count() != 0); m->_remove_after_drag(indexes.at(0).row()); } }
int MovableTableWidget::sizeHintForColumn(int column) const { ensurePolished(); QStyleOptionViewItem option = viewOptions(); int hint = 0; QModelIndex index; for (int row = 0; row <= rowCount(); ++row) { index = indexFromItem(item(row, column)); hint = qMax(hint, itemDelegate(index)->sizeHint(option, index).width()); } return showGrid() ? hint + 1 : hint; }
int QCustomTableWidget::sizeHintForColumn(int column) const { QStyleOptionViewItem option(viewOptions()); int hint = 0; QAbstractItemDelegate *delegate = itemDelegate(); QAbstractItemModel *itemModel = model(); for(int row = 0; row < rowCount(); row++) { hint = qMax(hint, delegate->sizeHint(option, itemModel->index(row,column)).width()); } if (showGrid()) { hint++; } return hint; }
void ProposalListView::modifyDelegate(Qt::KeyboardModifiers mods) { if (_defSubtextMode == SubTextMode::Action){ delete itemDelegate(); switch (mods) { case Qt::ControlModifier: setItemDelegate(new SubTextDelegate(Qt::UserRole+10+_actionCtrl)); break; case Qt::MetaModifier: setItemDelegate(new SubTextDelegate(Qt::UserRole+10+_actionMeta)); break; case Qt::AltModifier: setItemDelegate(new SubTextDelegate(Qt::UserRole+10+_actionAlt)); break; default: setItemDelegate(new SubTextDelegate(Qt::UserRole+10)); break; } update(); } if (_selSubtextMode == SubTextMode::Action){ delete _selectedDelegate; switch (mods) { case Qt::ControlModifier: _selectedDelegate = new SubTextDelegate(Qt::UserRole+10+_actionCtrl); break; case Qt::MetaModifier: _selectedDelegate = new SubTextDelegate(Qt::UserRole+10+_actionMeta); break; case Qt::AltModifier: _selectedDelegate = new SubTextDelegate(Qt::UserRole+10+_actionAlt); break; default: _selectedDelegate = new SubTextDelegate(Qt::UserRole+10); break; } // Give all the custom rows the new delegate // and let the new delgates be visible for (int i : _customDelegateRows){ setItemDelegateForRow(i, _selectedDelegate); update(model()->index(i,0)); } } }