/** A method that simplifies checking a view's current item and selection */ static void verifyCurrentItemAndSelection(const QAbstractItemView& view, const QModelIndex& expectedCurrent, const QModelIndexList& expectedSelection) { QCOMPARE(view.currentIndex(), expectedCurrent); const QModelIndexList selectedIndexes = view.selectionModel()->selectedIndexes(); QCOMPARE(selectedIndexes.count(), expectedSelection.count()); foreach(const QModelIndex& index, expectedSelection) { QVERIFY(selectedIndexes.contains(index)); }
void StartPage::handle_selectionChanged() { QItemSelectionModel * sel_model = qobject_cast<QItemSelectionModel *>(sender()); Q_ASSERT(sel_model); QAbstractItemView * area = area_of(sel_model->model()); Q_ASSERT(area); QString button_name = area->property("open_button_name").toString(); QAbstractButton * button = findChild<QAbstractButton *>(button_name); Q_ASSERT(button); button->setEnabled( area->currentIndex().isValid() ); }
bool BAutoCompletionHelper::eventFilter(QObject *object, QEvent *event) { if (event->type() != QEvent::KeyPress) return false; QKeyEvent *ke = static_cast<QKeyEvent *>(event); if (ke->key() != Qt::Key_Enter && ke->key() != Qt::Key_Return) return false; QAbstractItemView *popup = qobject_cast<QAbstractItemView *>(object); if (!popup || !popup->isVisible()) return false; popup->hide(); completerActivated(popup->currentIndex()); ke->ignore(); return true; }
void tst_QColumnView::selectedColumns() { ColumnView view; QDirModel model; view.setModel(&model); view.resize(800,300); view.show(); QModelIndex home = model.index(QDir::homePath()); view.setCurrentIndex(home); QTest::qWait(ANIMATION_DELAY); for (int i = 0; i < view.createdColumns.count(); ++i) { QAbstractItemView *column = view.createdColumns.at(i); if (!column) continue; if (!column->rootIndex().isValid() || column->rootIndex() == home) continue; QTRY_VERIFY(column->currentIndex().isValid()); } }
void LedgerDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { QStyleOptionViewItem opt = option; initStyleOption(&opt, index); // never change the background of the cell the mouse is hovering over opt.state &= ~QStyle::State_MouseOver; // show the focus only on the detail column opt.state &= ~QStyle::State_HasFocus; if(index.column() == LedgerModel::DetailColumn) { QAbstractItemView* view = qobject_cast< QAbstractItemView* >(parent()); if(view) { if(view->currentIndex().row() == index.row()) { opt.state |= QStyle::State_HasFocus; } } } painter->save(); // Background QStyle *style = opt.widget ? opt.widget->style() : QApplication::style(); style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, opt.widget); // Do not paint text if the edit widget is shown const LedgerView *view = qobject_cast<const LedgerView *>(opt.widget); if (view && view->indexWidget(index)) { painter->restore(); return; } const int margin = style->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1; const QRect textArea = QRect(opt.rect.x() + margin, opt.rect.y() + margin, opt.rect.width() - 2 * margin, opt.rect.height() - 2 * margin); QStringList lines; if(index.column() == LedgerModel::DetailColumn) { lines << index.model()->data(index, LedgerModel::PayeeNameRole).toString(); lines << index.model()->data(index, LedgerModel::CounterAccountRole).toString(); lines << index.model()->data(index, LedgerModel::SingleLineMemoRole).toString(); lines.removeAll(QString()); } const bool erroneous = index.model()->data(index, LedgerModel::ErroneousRole).toBool(); const bool selected = opt.state & QStyle::State_Selected; // draw the text items if(!opt.text.isEmpty() || !lines.isEmpty()) { // check if it is a scheduled transaction and display it as inactive if(!index.model()->data(index, LedgerModel::ScheduleIdRole).toString().isEmpty()) { opt.state &= ~QStyle::State_Enabled; } QPalette::ColorGroup cg = (opt.state & QStyle::State_Enabled) ? QPalette::Normal : QPalette::Disabled; if (cg == QPalette::Normal && !(opt.state & QStyle::State_Active)) { cg = QPalette::Inactive; } if (opt.state & QStyle::State_Selected) { painter->setPen(opt.palette.color(cg, QPalette::HighlightedText)); } else { painter->setPen(opt.palette.color(cg, QPalette::Text)); } if (opt.state & QStyle::State_Editing) { painter->setPen(opt.palette.color(cg, QPalette::Text)); painter->drawRect(textArea.adjusted(0, 0, -1, -1)); } // Don't play with the color if it's selected // otherwise switch the color if the transaction has errors if(erroneous && !selected) { painter->setPen(m_erroneousColor); } // collect data for the various colums if(index.column() == LedgerModel::DetailColumn) { for(int i = 0; i < lines.count(); ++i) { painter->drawText(textArea.adjusted(0, (opt.fontMetrics.lineSpacing() + 5) * i, 0, 0), opt.displayAlignment, lines[i]); } } else { painter->drawText(textArea, opt.displayAlignment, opt.text); } } // draw the focus rect if(opt.state & QStyle::State_HasFocus) { QStyleOptionFocusRect o; o.QStyleOption::operator=(opt); o.rect = style->proxy()->subElementRect(QStyle::SE_ItemViewItemFocusRect, &opt, opt.widget); o.state |= QStyle::State_KeyboardFocusChange; o.state |= QStyle::State_Item; QPalette::ColorGroup cg = (opt.state & QStyle::State_Enabled) ? QPalette::Normal : QPalette::Disabled; o.backgroundColor = opt.palette.color(cg, (opt.state & QStyle::State_Selected) ? QPalette::Highlight : QPalette::Window); style->proxy()->drawPrimitive(QStyle::PE_FrameFocusRect, &o, painter, opt.widget); } if((index.column() == LedgerModel::DetailColumn) && erroneous) { QPixmap attention; attention.loadFromData(attentionSign, sizeof(attentionSign), 0, 0); style->proxy()->drawItemPixmap(painter, option.rect, Qt::AlignRight | Qt::AlignTop, attention); } painter->restore(); #if 0 const QHeaderView* horizontalHeader = view->horizontalHeader(); const QHeaderView* verticalHeader = view->verticalHeader(); const QWidget* viewport = view->viewport(); const bool showGrid = view->showGrid() && !view->indexWidget(index); const int gridSize = showGrid ? 1 : 0; const int gridHint = style->styleHint(QStyle::SH_Table_GridLineColor, &option, view); const QColor gridColor = static_cast<QRgb>(gridHint); const QPen gridPen = QPen(gridColor, 0, view->gridStyle()); const bool rightToLeft = view->isRightToLeft(); const int viewportOffset = horizontalHeader->offset(); // QStyledItemDelegate::paint(painter, opt, index); if(!horizontalHeader->isSectionHidden(LedgerModel::DateColumn)) { QDate postDate = index.data(LedgerModel::PostDateRole).toDate(); if(postDate.isValid()) { int ofs = horizontalHeader->sectionViewportPosition(LedgerModel::DateColumn) + viewportOffset; QRect oRect = opt.rect; opt.displayAlignment = Qt::AlignLeft | Qt::AlignTop; opt.rect.setLeft(opt.rect.left()+ofs); opt.rect.setTop(opt.rect.top()+margin); opt.rect.setWidth(horizontalHeader->sectionSize(LedgerModel::DateColumn)); opt.text = KGlobal::locale()->formatDate(postDate, QLocale::ShortFormat); style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget); opt.rect = oRect; } } if(!horizontalHeader->isSectionHidden(LedgerModel::DetailColumn)) { QString payee = index.data(LedgerModel::PayeeRole).toString(); QString counterAccount = index.data(LedgerModel::CounterAccountRole).toString(); QString txt = payee; if(payee.length() > 0) txt += '\n'; txt += counterAccount; int ofs = horizontalHeader->sectionViewportPosition(LedgerModel::DetailColumn) + viewportOffset; QRect oRect = opt.rect; opt.displayAlignment = Qt::AlignLeft | Qt::AlignTop; opt.rect.setLeft(opt.rect.left()+ofs); opt.rect.setTop(opt.rect.top()+margin); opt.rect.setWidth(horizontalHeader->sectionSize(LedgerModel::DetailColumn)); opt.text = txt; style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget); opt.rect = oRect; } #if 0 opt.features |= QStyleOptionViewItemV2::HasDisplay; QString txt = QString("%1").arg(index.isValid() ? "true" : "false"); if(index.isValid()) txt += QString(" %1 - %2").arg(index.row()).arg(view->verticalHeader()->sectionViewportPosition(index.row())); opt.text = displayText(txt, opt.locale); style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget); #endif // paint grid if(showGrid) { painter->save(); QPen old = painter->pen(); painter->setPen(gridPen); // qDebug() << "Paint grid for" << index.row() << "in" << opt.rect; for(int i=0; i < horizontalHeader->count(); ++i) { if(!horizontalHeader->isSectionHidden(i)) { int ofs = horizontalHeader->sectionViewportPosition(i) + viewportOffset; if(!rightToLeft) { ofs += horizontalHeader->sectionSize(i) - gridSize; } if(ofs-viewportOffset < viewport->width()) { // I have no idea, why I need to paint the grid for the selected row and the one below // but it was the only way to get this working correctly. Otherwise the grid was missing // while moving the mouse over the view from bottom to top. painter->drawLine(opt.rect.x()+ofs, opt.rect.y(), opt.rect.x()+ofs, opt.rect.height()); painter->drawLine(opt.rect.x()+ofs, opt.rect.y()+verticalHeader->sectionSize(index.row()), opt.rect.x()+ofs, opt.rect.height()); } } } painter->setPen(old); painter->restore(); } #endif }