📄 qcolumnview.cpp
字号:
default: break; } return QModelIndex();}/*! \reimp*/void QColumnView::resizeEvent(QResizeEvent *event){ Q_D(QColumnView); d->doLayout(); d->updateScrollbars(); if (!isRightToLeft()) { int diff = event->oldSize().width() - event->size().width(); if (diff < 0 && horizontalScrollBar()->isVisible() && horizontalScrollBar()->value() == horizontalScrollBar()->maximum()) { horizontalScrollBar()->setMaximum(horizontalScrollBar()->maximum() + diff); } } QAbstractItemView::resizeEvent(event);}/*! \internal*/void QColumnViewPrivate::updateScrollbars(){ Q_Q(QColumnView); if (currentAnimation.state() == QTimeLine::Running) return; // find the total horizontal length of the laid out columns int horizontalLength = 0; if (!columns.isEmpty()) { horizontalLength = (columns.last()->x() + columns.last()->width()) - columns.first()->x(); if (horizontalLength <= 0) // reverse mode horizontalLength = (columns.first()->x() + columns.first()->width()) - columns.last()->x(); } QSize viewportSize = q->viewport()->size(); if (horizontalLength < viewportSize.width() && q->horizontalScrollBar()->value() == 0) { q->horizontalScrollBar()->setRange(0, 0); } else { int visibleLength = qMin(horizontalLength + q->horizontalOffset(), viewportSize.width()); int hiddenLength = horizontalLength - visibleLength; if (hiddenLength != q->horizontalScrollBar()->maximum()) q->horizontalScrollBar()->setRange(0, hiddenLength); } if (!columns.isEmpty()) { int pageStepSize = columns.at(0)->width(); if (pageStepSize != q->horizontalScrollBar()->pageStep()) q->horizontalScrollBar()->setPageStep(pageStepSize); } bool visible = (q->horizontalScrollBar()->maximum() > 0); if (visible != q->horizontalScrollBar()->isVisible()) q->horizontalScrollBar()->setVisible(visible);}/*! \reimp*/int QColumnView::horizontalOffset() const{ Q_D(const QColumnView); return d->offset;}/*! \reimp*/int QColumnView::verticalOffset() const{ return 0;}/*! \reimp*/QRegion QColumnView::visualRegionForSelection(const QItemSelection &selection) const{ int ranges = selection.count(); if (ranges == 0) return QRect(); // Note that we use the top and bottom functions of the selection range // since the data is stored in rows. int firstRow = selection.at(0).top(); int lastRow = selection.at(0).top(); for (int i = 0; i < ranges; ++i) { firstRow = qMin(firstRow, selection.at(i).top()); lastRow = qMax(lastRow, selection.at(i).bottom()); } QModelIndex firstIdx = model()->index(qMin(firstRow, lastRow), 0, rootIndex()); QModelIndex lastIdx = model()->index(qMax(firstRow, lastRow), 0, rootIndex()); if (firstIdx == lastIdx) return visualRect(firstIdx); QRegion firstRegion = visualRect(firstIdx); QRegion lastRegion = visualRect(lastIdx); return firstRegion.unite(lastRegion);}/*! \reimp*/void QColumnView::setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags command){ Q_UNUSED(rect); Q_UNUSED(command);}/*! \reimp*/void QColumnView::setSelectionModel(QItemSelectionModel * newSelectionModel){ Q_D(const QColumnView); for (int i = 0; i < d->columns.size(); ++i) { if (d->columns.at(i)->selectionModel() == selectionModel()) { d->columns.at(i)->setSelectionModel(newSelectionModel); break; } } QAbstractItemView::setSelectionModel(newSelectionModel);}/*! \reimp*/QSize QColumnView::sizeHint() const{ Q_D(const QColumnView); QSize sizeHint; for (int i = 0; i < d->columns.size(); ++i) { sizeHint += d->columns.at(i)->sizeHint(); } return sizeHint.expandedTo(QAbstractItemView::sizeHint());}/*! \internal Move all widgets from the corner grip and to the right */void QColumnViewPrivate::_q_gripMoved(int offset){ Q_Q(QColumnView); QObject *grip = q->sender(); Q_ASSERT(grip); if (q->isRightToLeft()) offset = -1 * offset; bool found = false; for (int i = 0; i < columns.size(); ++i) { if (!found && columns.at(i)->cornerWidget() == grip) { found = true; columnSizes[i] = columns.at(i)->width(); if (q->isRightToLeft()) columns.at(i)->move(columns.at(i)->x() + offset, 0); continue; } if (!found) continue; int currentX = columns.at(i)->x(); columns.at(i)->move(currentX + offset, 0); } updateScrollbars();}/*! \internal Find where the current columns intersect parent's columns Delete any extra columns and insert any needed columns. */void QColumnViewPrivate::closeColumns(const QModelIndex &parent, bool build){ if (columns.isEmpty()) return; bool clearAll = !parent.isValid(); bool passThroughRoot = false; QList<QModelIndex> dirsToAppend; // Find the last column that matches the parent's tree int currentColumn = -1; QModelIndex parentIndex = parent; while (currentColumn == -1 && parentIndex.isValid()) { if (columns.isEmpty()) break; parentIndex = parentIndex.parent(); if (root == parentIndex) passThroughRoot = true; if (!parentIndex.isValid()) break; for (int i = columns.size() - 1; i >= 0; --i) { if (columns.at(i)->rootIndex() == parentIndex) { currentColumn = i; break; } } if (currentColumn == -1) dirsToAppend.append(parentIndex); } // Someone wants to go to an index that can be reached without changing // the root index, don't allow them if (!clearAll && !passThroughRoot && currentColumn == -1) return; if (currentColumn == -1 && parent.isValid()) currentColumn = 0; // Optimization so we don't go deleting and then creating the same thing bool alreadyExists = false; if (build && columns.size() > currentColumn + 1) { bool viewingParent = (columns.at(currentColumn + 1)->rootIndex() == parent); bool viewingChild = (!model->hasChildren(parent) && !columns.at(currentColumn + 1)->rootIndex().isValid()); if (viewingParent || viewingChild) { currentColumn++; alreadyExists = true; } } // Delete columns that don't match our path for (int i = columns.size() - 1; i > currentColumn; --i) { QAbstractItemView* notShownAnymore = columns.at(i); columns.removeAt(i); notShownAnymore->setVisible(false); if (notShownAnymore != previewColumn) notShownAnymore->deleteLater(); } if (columns.isEmpty()) { offset = 0; updateScrollbars(); } // Now fill in missing columns while (!dirsToAppend.isEmpty()) { QAbstractItemView *newView = createColumn(dirsToAppend.takeLast(), true); if (!dirsToAppend.isEmpty()) newView->setCurrentIndex(dirsToAppend.last()); } if (build && !alreadyExists) createColumn(parent, false);}void QColumnViewPrivate::_q_clicked(const QModelIndex &index){ Q_Q(QColumnView); QModelIndex parent = index.parent(); QAbstractItemView *columnClicked = 0; for (int column = 0; column < columns.count(); ++column) { if (columns.at(column)->rootIndex() == parent) { columnClicked = columns[column]; break; } } if (q->selectionModel() && columnClicked) { QItemSelectionModel::SelectionFlags flags = QItemSelectionModel::Current; if (columnClicked->selectionModel()->isSelected(index)) flags |= QItemSelectionModel::Select; q->selectionModel()->setCurrentIndex(index, flags); }}/*! \internal Create a new column for \a index. A grip is attached if requested and it is shown if requested. Return the new view \sa createColumn() setPreviewWidget() \sa doLayout()*/QAbstractItemView *QColumnViewPrivate::createColumn(const QModelIndex &index, bool show){ Q_Q(QColumnView); QAbstractItemView *view = 0; if (model->hasChildren(index)) { view = q->createColumn(index); q->connect(view, SIGNAL(clicked(const QModelIndex &)), q, SLOT(_q_clicked(const QModelIndex &))); } else { if (!previewColumn) setPreviewWidget(new QWidget(q)); view = previewColumn; view->setMinimumWidth(qMax(view->minimumWidth(), previewWidget->minimumWidth())); } q->connect(view, SIGNAL(activated(const QModelIndex &)), q, SIGNAL(activated(const QModelIndex &))); q->connect(view, SIGNAL(clicked(const QModelIndex &)), q, SIGNAL(clicked(const QModelIndex &))); q->connect(view, SIGNAL(doubleClicked(const QModelIndex &)), q, SIGNAL(doubleClicked(const QModelIndex &))); q->connect(view, SIGNAL(entered(const QModelIndex &)), q, SIGNAL(entered(const QModelIndex &))); q->connect(view, SIGNAL(pressed(const QModelIndex &)), q, SIGNAL(pressed(const QModelIndex &))); view->setFocusPolicy(Qt::NoFocus); view->setParent(q->viewport()); Q_ASSERT(view); // Setup corner grip if (showResizeGrips) { QColumnViewGrip *grip = new QColumnViewGrip(view); view->setCornerWidget(grip); q->connect(grip, SIGNAL(gripMoved(int)), q, SLOT(_q_gripMoved(int))); } if (columnSizes.count() > columns.count()) { view->setGeometry(0, 0, columnSizes.at(columns.count()), q->viewport()->height()); } else { int initialWidth = view->sizeHint().width(); if (q->isRightToLeft()) view->setGeometry(q->viewport()->width() - initialWidth, 0, initialWidth, q->viewport()->height()); else view->setGeometry(0, 0, initialWidth, q->viewport()->height()); columnSizes.resize(qMax(columnSizes.count(), columns.count() + 1)); columnSizes[columns.count()] = initialWidth; } if (!columns.isEmpty() && columns.last()->isHidden()) columns.last()->setVisible(true); columns.append(view); doLayout(); updateScrollbars(); if (show && view->isHidden()) view->setVisible(true); return view;}/*! \fn void QColumnView::updatePreviewWidget(const QModelIndex &index) This signal is emitted when the preview widget should be updated to provide rich information about \a index \sa previewWidget() *//*! To use a custom widget for the final column when you select an item overload this function and return a widget. \a index is the root index that will be assigned to the view. Return the new view. QColumnView will automatically take ownership of the widget. \sa setPreviewWidget() */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -