qcolumnview.cpp
来自「奇趣公司比较新的qt/emd版本」· C++ 代码 · 共 1,102 行 · 第 1/3 页
CPP
1,102 行
QAbstractItemView *QColumnView::createColumn(const QModelIndex &index){ Q_D(QColumnView); QListView *view = new QListView(viewport()); view->setFrameShape(QFrame::NoFrame); view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); view->setMinimumWidth(100); view->setAttribute(Qt::WA_MacShowFocusRect, false); // Copy the 'view' behavior#ifndef QT_NO_DRAGANDDROP view->setDragDropMode(dragDropMode()); view->setDragDropOverwriteMode(dragDropOverwriteMode()); view->setDropIndicatorShown(showDropIndicator());#endif view->setAlternatingRowColors(alternatingRowColors()); view->setAutoScroll(hasAutoScroll()); view->setEditTriggers(editTriggers()); view->setHorizontalScrollMode(horizontalScrollMode()); view->setIconSize(iconSize()); view->setSelectionBehavior(selectionBehavior()); view->setSelectionMode(selectionMode()); view->setTabKeyNavigation(tabKeyNavigation()); view->setTextElideMode(textElideMode()); view->setVerticalScrollMode(verticalScrollMode()); view->setModel(model()); // Copy the custom delegate per row QMapIterator<int, QPointer<QAbstractItemDelegate> > i(d->rowDelegates); while (i.hasNext()) { i.next(); view->setItemDelegateForRow(i.key(), i.value()); } // set the delegate to be the columnview delegate QAbstractItemDelegate *delegate = view->itemDelegate(); view->setItemDelegate(d->itemDelegate); delete delegate; view->setRootIndex(index); if (model()->canFetchMore(index)) model()->fetchMore(index); return view;}/*! Returns the preview widget, or 0 if there is none. \sa setPreviewWidget(), updatePreviewWidget()*/QWidget *QColumnView::previewWidget() const{ Q_D(const QColumnView); return d->previewWidget;}/*! Sets the preview \a widget. The \a widget becomes a child of the column view, and will be destroyed when the column area is deleted or when a new widget is set. \sa previewWidget(), updatePreviewWidget()*/void QColumnView::setPreviewWidget(QWidget *widget) { Q_D(QColumnView); d->setPreviewWidget(widget);}/*! \internal*/void QColumnViewPrivate::setPreviewWidget(QWidget *widget){ Q_Q(QColumnView); if (previewColumn) { if (!columns.isEmpty() && columns.last() == previewColumn) columns.removeLast(); previewColumn->deleteLater(); } QColumnViewPreviewColumn *column = new QColumnViewPreviewColumn(q); column->setPreviewWidget(widget); previewColumn = column; previewColumn->hide(); previewColumn->setFrameShape(QFrame::NoFrame); previewColumn->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); previewColumn->setSelectionMode(QAbstractItemView::NoSelection); previewColumn->setMinimumWidth(qMax(previewColumn->verticalScrollBar()->width(), previewColumn->minimumWidth())); previewWidget = widget; previewWidget->setParent(previewColumn->viewport());}/*! Sets the column widths to the values given in the \a list. Extra values in the list are kept and used when the columns are created. If list contains too few values, only width of the rest of the columns will not be modified. \sa columnWidths(), createColumn()*/void QColumnView::setColumnWidths(const QList<int> &list){ Q_D(QColumnView); int i = 0; for (; (i < list.count() && i < d->columns.count()); ++i) { d->columns.at(i)->resize(list.at(i), d->columns.at(i)->height()); d->columnSizes[i] = list.at(i); } for (; i < list.count(); ++i) d->columnSizes.append(list.at(i));}/*! Returns a list of the width of all the columns in this view. \sa setColumnWidths()*/QList<int> QColumnView::columnWidths() const{ Q_D(const QColumnView); QList<int> list; for (int i = 0; i < d->columns.count(); ++i) list.append(d->columnSizes.at(i)); return list;}/*! \reimp*/void QColumnView::currentChanged(const QModelIndex ¤t, const QModelIndex &previous){ Q_D(QColumnView); if (!current.isValid()) { QAbstractItemView::currentChanged(current, previous); return; } QModelIndex currentParent = current.parent(); // optimize for just moving up/down in a list where the child view doesn't change if (currentParent == previous.parent() && model()->hasChildren(current) && model()->hasChildren(previous)) { for (int i = 0; i < d->columns.size(); ++i) { if (currentParent == d->columns.at(i)->rootIndex()) { if (d->columns.size() > i + 1) { QAbstractItemView::currentChanged(current, previous); return; } break; } } } // Scrolling to the right we need to have an empty spot bool found = false; if (currentParent == previous) { for (int i = 0; i < d->columns.size(); ++i) { if (currentParent == d->columns.at(i)->rootIndex()) { found = true; if (d->columns.size() < i + 2) { d->createColumn(current, false); } break; } } } if (!found) d->closeColumns(current, true); if (!model()->hasChildren(current)) emit updatePreviewWidget(current); QAbstractItemView::currentChanged(current, previous);}/* We have change the current column and need to update focus and selection models on the new current column.*/void QColumnViewPrivate::_q_changeCurrentColumn(){ Q_Q(QColumnView); if (columns.isEmpty()) return; QModelIndex current = q->currentIndex(); if (!current.isValid()) return; // We might have scrolled far to the left so we need to close all of the children closeColumns(current, true); // Set up the "current" column with focus int currentColumn = qMax(0, columns.size() - 2); QAbstractItemView *parentColumn = columns.at(currentColumn); if (q->hasFocus()) parentColumn->setFocus(Qt::OtherFocusReason); q->setFocusProxy(parentColumn); // find the column that is our current selection model and give it a new one. for (int i = 0; i < columns.size(); ++i) { if (columns.at(i)->selectionModel() == q->selectionModel()) { QItemSelectionModel *replacementSelectionModel = new QItemSelectionModel(parentColumn->model()); replacementSelectionModel->setCurrentIndex( q->selectionModel()->currentIndex(), QItemSelectionModel::Current); replacementSelectionModel->select( q->selectionModel()->selection(), QItemSelectionModel::Select); QAbstractItemView *view = columns.at(i); view->setSelectionModel(replacementSelectionModel); view->setFocusPolicy(Qt::NoFocus); if (columns.size() > i + 1) view->setCurrentIndex(columns.at(i+1)->rootIndex()); break; } } parentColumn->selectionModel()->deleteLater(); parentColumn->setFocusPolicy(Qt::StrongFocus); parentColumn->setSelectionModel(q->selectionModel()); // We want the parent selection to stay highlighted (but dimmed depending upon the color theme) if (currentColumn > 0) { parentColumn = columns.at(currentColumn - 1); if (parentColumn->currentIndex() != current.parent()) parentColumn->setCurrentIndex(current.parent()); } if (columns.last()->isHidden()) { columns.last()->setVisible(true); } if (columns.last()->selectionModel()) columns.last()->selectionModel()->clear(); updateScrollbars();}/*! \reimp*/void QColumnView::selectAll(){ if (!model() || !selectionModel()) return; QModelIndexList indexList = selectionModel()->selectedIndexes(); QModelIndex parent = rootIndex(); QItemSelection selection; if (indexList.count() >= 1) parent = indexList.at(0).parent(); if (indexList.count() == 1) { parent = indexList.at(0); if (!model()->hasChildren(parent)) parent = parent.parent(); else selection.append(QItemSelectionRange(parent, parent)); } QModelIndex tl = model()->index(0, 0, parent); QModelIndex br = model()->index(model()->rowCount(parent) - 1, model()->columnCount(parent) - 1, parent); selection.append(QItemSelectionRange(tl, br)); selectionModel()->select(selection, QItemSelectionModel::ClearAndSelect);}/* * private object implementation */QColumnViewPrivate::QColumnViewPrivate(): QAbstractItemViewPrivate(),showResizeGrips(true),offset(0),currentAnimation(ANIMATION_DURATION_MSEC),previewWidget(0),previewColumn(0){}QColumnViewPrivate::~QColumnViewPrivate(){}/*! \internal Place all of the columns where they belong inside of the viewport, resize as necessary.*/void QColumnViewPrivate::doLayout(){ Q_Q(QColumnView); if (!model || columns.isEmpty()) return; int viewportHeight = q->viewport()->height(); int x = columns.at(0)->x(); if (q->isRightToLeft()) { x = q->viewport()->width() + q->horizontalOffset(); for (int i = 0; i < columns.size(); ++i) { QAbstractItemView *view = columns.at(i); x -= view->width(); if (x != view->x() || viewportHeight != view->height()) view->setGeometry(x, 0, view->width(), viewportHeight); } } else { for (int i = 0; i < columns.size(); ++i) { QAbstractItemView *view = columns.at(i); int currentColumnWidth = view->width(); if (x != view->x() || viewportHeight != view->height()) view->setGeometry(x, 0, currentColumnWidth, viewportHeight); x += currentColumnWidth; } }}/*! \internal Draws a delegate with a > if an object has children. \sa {Model/View Programming}, QItemDelegate*/void QColumnViewDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const{ drawBackground(painter, option, index ); bool reverse = (option.direction == Qt::RightToLeft); int width = ((option.rect.height() * 2) / 3); // Modify the options to give us room to add an arrow QStyleOptionViewItemV3 opt = option; if (reverse) opt.rect.adjust(width,0,0,0); else opt.rect.adjust(0,0,-width,0); if (!(index.model()->flags(index) & Qt::ItemIsEnabled)) { opt.showDecorationSelected = true; opt.state |= QStyle::State_Selected; } QItemDelegate::paint(painter, opt, index); if (reverse) opt.rect = QRect(option.rect.x(), option.rect.y(), width, option.rect.height()); else opt.rect = QRect(option.rect.x() + option.rect.width() - width, option.rect.y(), width, option.rect.height()); // Draw > if (index.model()->hasChildren(index)) { const QWidget *view = opt.widget; QStyle *style = view ? view->style() : qApp->style(); style->drawPrimitive(QStyle::PE_IndicatorColumnViewArrow, &opt, painter, view); }}#include "moc_qcolumnview.cpp"#endif // QT_NO_COLUMNVIEW
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?