complexwidgets.cpp
来自「QT 开发环境里面一个很重要的文件」· C++ 代码 · 共 932 行 · 第 1/2 页
CPP
932 行
/******************************************************************************** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.**** This file is part of the plugins of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://www.trolltech.com/products/qt/opensource.html**** If you are unsure which license is appropriate for your use, please** review the following information:** http://www.trolltech.com/products/qt/licensing.html or contact the** sales department at sales@trolltech.com.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "complexwidgets.h"#include <qapplication.h>#include <qabstractbutton.h>#include <qevent.h>#include <qheaderview.h>#include <qtabbar.h>#include <qcombobox.h>#include <qlistview.h>#include <qtableview.h>#include <qlineedit.h>#include <qstyle.h>#include <qstyleoption.h>#include <qtooltip.h>#include <qwhatsthis.h>#include <qdebug.h>#include <private/qtabbar_p.h>#ifndef QT_NO_ACCESSIBILITYQString Q_GUI_EXPORT qt_accStripAmp(const QString &text);#ifndef QT_NO_ITEMVIEWSQAccessibleItemRow::QAccessibleItemRow(QAbstractItemView *aView, const QModelIndex &index) : row(index), view(aView){}QRect QAccessibleItemRow::rect(int child) const{ if (!row.isValid() || !view) return QRect(); QRect r; if (child) { r = view->visualRect(childIndex(child)); } else { QModelIndex parent = row.parent(); const int colCount = row.model()->columnCount(parent); for (int i = 0; i < colCount; ++i) r |= view->visualRect(row.model()->index(row.row(), i, parent)); } return r.translated(view->viewport()->mapToGlobal(QPoint(0, 0)));}QString QAccessibleItemRow::text(Text t, int child) const{ if (!child) return QString(); QModelIndex idx = childIndex(child); if (!idx.isValid()) return QString(); QString value; switch (t) { case Description: value = idx.model()->data(idx, Qt::AccessibleDescriptionRole).toString(); break; case Value: value = idx.model()->data(idx, Qt::AccessibleTextRole).toString(); if (value.isEmpty()) value = idx.model()->data(idx, Qt::DisplayRole).toString(); break; default: break; } return value;}void QAccessibleItemRow::setText(Text t, int child, const QString &text){ if (!child) return; QModelIndex idx = childIndex(child); if (!idx.isValid()) return; switch (t) { case Description: const_cast<QAbstractItemModel *>(idx.model())->setData(idx, text, Qt::AccessibleDescriptionRole); break; case Value: const_cast<QAbstractItemModel *>(idx.model())->setData(idx, text, Qt::EditRole); break; default: break; }}QModelIndex QAccessibleItemRow::childIndex(int child) const{ return row.sibling(row.row(), child - 1);}bool QAccessibleItemRow::isValid() const{ return row.isValid();}QObject *QAccessibleItemRow::object() const{ return 0;}int QAccessibleItemRow::childCount() const{ return row.model()->columnCount(row.parent());}int QAccessibleItemRow::indexOfChild(const QAccessibleInterface *iface) const{ if (!iface || iface->role(0) != Row) return -1; QModelIndex idx = static_cast<const QAccessibleItemRow *>(iface)->row; if (!idx.isValid()) return -1; return idx.column() + 1;}QAccessible::Relation QAccessibleItemRow::relationTo(int child, const QAccessibleInterface *other, int otherChild) const{ if (!child && !otherChild && other->object() == view) return Child; if (!child && !otherChild && other == this) return Self; if (!child && otherChild && other == this) return Ancestor; if (child && otherChild && other == this) return Sibling; return Unrelated;}int QAccessibleItemRow::childAt(int x, int y) const{ if (!view) return -1; QModelIndex idx = view->indexAt(view->viewport()->mapFromGlobal(QPoint(x, y))); if (idx.isValid() && idx.parent() == row.parent() && idx.row() == row.row()) return idx.column() + 1; return -1;}QAbstractItemView::CursorAction QAccessibleItemRow::toCursorAction( QAccessible::Relation rel){ switch (rel) { case QAccessible::Up: return QAbstractItemView::MoveUp; case QAccessible::Down: return QAbstractItemView::MoveDown; case QAccessible::Left: return QAbstractItemView::MoveLeft; case QAccessible::Right: return QAbstractItemView::MoveRight; default: Q_ASSERT(false); } // should never be reached. return QAbstractItemView::MoveRight;}int QAccessibleItemRow::navigate(RelationFlag relation, int index, QAccessibleInterface **iface) const{ *iface = 0; if (!view) return -1; switch (relation) { case Ancestor: { QObject *object = view; for (int i = 0; i < index - 1; ++i) { if (object) object = object->parent(); } if (object) { *iface = QAccessibleInterface::queryAccessibleInterface(object); return 0; } return -1; } case Child: { if (!index) return -1; QModelIndex idx = childIndex(index); if (!idx.isValid()) return -1; return idx.column() + 1; } case Sibling: if (index) return navigate(Child, index, iface); return -1; case Up: case Down: case Left: case Right: { // This is in the "not so nice" category. In order to find out which item // is geometrically around, we have to set the current index, navigate // and restore the index as well as the old selection view->setUpdatesEnabled(false); const QModelIndex oldIdx = view->currentIndex(); const QModelIndex currentIndex = index ? childIndex(index) : QModelIndex(row); const QItemSelection oldSelection = view->selectionModel()->selection(); view->setCurrentIndex(currentIndex); const QModelIndex idx = view->moveCursor(toCursorAction(relation), Qt::NoModifier); view->setCurrentIndex(oldIdx); view->selectionModel()->select(oldSelection, QItemSelectionModel::ClearAndSelect); view->setUpdatesEnabled(true); if (!idx.isValid()) return -1; if (idx.parent() != row.parent() || idx.row() != row.row()) *iface = new QAccessibleItemRow(view, idx); return index ? idx.column() + 1 : 0; } default: break; } return -1;}QAccessible::Role QAccessibleItemRow::role(int child) const{ if (!child) return Row; return Cell;}QAccessible::State QAccessibleItemRow::state(int child) const{ State st = Normal; if (!view) return st; if (child) { QModelIndex idx = childIndex(child); if (!idx.isValid()) return st; if (view->selectionModel()->isSelected(idx)) st |= Selected; if (idx.model()->data(idx, Qt::CheckStateRole).toInt() == Qt::Checked) st |= Checked; Qt::ItemFlags flags = idx.flags(); if (flags & Qt::ItemIsSelectable) { st |= Selectable; if (view->selectionMode() == QAbstractItemView::MultiSelection) st |= MultiSelectable; if (view->selectionMode() == QAbstractItemView::ExtendedSelection) st |= ExtSelectable; } } else { if (view->selectionModel()->isRowSelected(row.row(), row.parent())) st |= Selected; } return st;}int QAccessibleItemRow::userActionCount(int) const{ return 0;}QString QAccessibleItemRow::actionText(int, Text, int) const{ return QString();}static QItemSelection rowAt(const QModelIndex &idx){ return QItemSelection(idx.sibling(idx.row(), 0), idx.sibling(idx.row(), idx.model()->columnCount(idx.parent())));}bool QAccessibleItemRow::doAction(int action, int child, const QVariantList & /*params*/){ if (!view) return false; QModelIndex idx = child ? childIndex(child) : QModelIndex(row); if (!idx.isValid()) return false; QItemSelectionModel::SelectionFlags command = QItemSelectionModel::NoUpdate; switch (action) { case SetFocus: view->setCurrentIndex(idx); return true; case ExtendSelection: if (!child) return false; view->selectionModel()->select(QItemSelection(view->currentIndex(), idx), QItemSelectionModel::SelectCurrent); return true; case Select: command = QItemSelectionModel::ClearAndSelect; break; case ClearSelection: command = QItemSelectionModel::Clear; break; case RemoveSelection: command = QItemSelectionModel::Deselect; break; case AddToSelection: command = QItemSelectionModel::SelectCurrent; break; } if (command == QItemSelectionModel::NoUpdate) return false; if (child) view->selectionModel()->select(idx, command); else view->selectionModel()->select(rowAt(row), command); return true;}QAccessibleItemView::QAccessibleItemView(QWidget *w) : QAccessibleWidget(w){ Q_ASSERT(itemView());}QAbstractItemView *QAccessibleItemView::itemView() const{ return qobject_cast<QAbstractItemView *>(object());}QModelIndex QAccessibleItemView::childIndex(int child) const{ return itemView()->model()->index(child - 1, 0);}int QAccessibleItemView::childCount() const{ if (itemView()->model() == 0) return 0; return itemView()->model()->rowCount();}QString QAccessibleItemView::text(Text t, int child) const{ if (!child) return QAccessibleWidget::text(t, child); QAccessibleItemRow item(itemView(), childIndex(child)); return item.text(t, 1);}void QAccessibleItemView::setText(Text t, int child, const QString &text){ if (!child) { QAccessibleWidget::setText(t, child, text); return; } QAccessibleItemRow item(itemView(), childIndex(child)); item.setText(t, 1, text);}QRect QAccessibleItemView::rect(int child) const{ if (!child) return QAccessibleWidget::rect(child); QAccessibleItemRow item(itemView(), childIndex(child)); return item.rect(0);}QAccessible::Role QAccessibleItemView::role(int child) const{ if (child) return Row; QAbstractItemView *view = itemView();#ifndef QT_NO_TABLEVIEW if (qobject_cast<QTableView *>(view)) return Table;#endif#ifndef QT_NO_LISTVIEW if (qobject_cast<QListView *>(view)) return List;#endif return Tree;}QAccessible::State QAccessibleItemView::state(int child) const{ if (!child) return QAccessibleWidget::state(child); QAccessibleItemRow item(itemView(), childIndex(child)); return item.state(0);}int QAccessibleItemView::navigate(RelationFlag relation, int index, QAccessibleInterface **iface) const{ if (relation == Child) { QModelIndex idx = childIndex(index); if (!idx.isValid()) { *iface = 0; return -1; } *iface = new QAccessibleItemRow(itemView(), childIndex(index)); return 0; } return QAccessibleWidget::navigate(relation, index, iface);}/*! \class QAccessibleHeader qaccessiblewidget.h \brief The QAccessibleHeader class implements the QAccessibleInterface for header widgets. \internal \ingroup accessibility*//*! Constructs a QAccessibleHeader object for \a w.*/QAccessibleHeader::QAccessibleHeader(QWidget *w): QAccessibleWidget(w)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?