10#include <QtWidgets/QApplication>
11#include <QtWidgets/QHeaderView>
12#include <QtGui/QKeyEvent>
13#include <QtGui/QMouseEvent>
14#include <QtWidgets/QMenu>
15#include <QtGui/QPainter>
29 if (option.state & QStyle::State_MouseOver) {
31QT_WARNING_DISABLE_DEPRECATED
32 if ((QApplication::mouseButtons() & Qt::LeftButton) == 0)
33 pressedIndex = QModelIndex();
35 QBrush brush = option.palette.alternateBase();
36 if (index == pressedIndex)
37 brush = option.palette.dark();
38 painter->fillRect(option.rect, brush);
41 QStyledItemDelegate::paint(painter, option, index);
43 if (index.column() == 1 && index.model()->rowCount() > 1
44 && option.state & QStyle::State_MouseOver) {
45 QIcon icon((option.state & QStyle::State_Selected)
46 ?
":/qt-project.org/assistant/images/closebutton.png"
47 :
":/qt-project.org/assistant/images/darkclosebutton.png");
49 const QRect iconRect(option.rect.right() - option.rect.height(),
50 option.rect.top(), option.rect.height(), option.rect.height());
51 icon.paint(painter, iconRect, Qt::AlignRight | Qt::AlignVCenter);
58 : m_allowContextMenu(
true)
63 setItemDelegate((m_delegate =
new OpenPagesDelegate(
this)));
65 setTextElideMode(Qt::ElideMiddle);
66 setAttribute(Qt::WA_MacShowFocusRect,
false);
68 viewport()->setAttribute(Qt::WA_Hover);
69 setSelectionBehavior(QAbstractItemView::SelectRows);
70 setSelectionMode(QAbstractItemView::SingleSelection);
73 header()->setStretchLastSection(
false);
74 header()->setSectionResizeMode(0, QHeaderView::Stretch);
75 header()->setSectionResizeMode(1, QHeaderView::Fixed);
76 header()->resizeSection(1, 18);
78 installEventFilter(
this);
79 setUniformRowHeights(
true);
80 setContextMenuPolicy(Qt::CustomContextMenu);
82 connect(
this, &QAbstractItemView::clicked,
83 this, &OpenPagesWidget::handleClicked);
84 connect(
this, &QAbstractItemView::pressed,
85 this, &OpenPagesWidget::handlePressed);
86 connect(
this, &QWidget::customContextMenuRequested,
87 this, &OpenPagesWidget::contextMenuRequested);
101 QItemSelectionModel *
const selModel = selectionModel();
102 selModel->select(current,
103 QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
104 selModel->clearSelection();
106 setCurrentIndex(current);
107 scrollTo(currentIndex());
113 m_allowContextMenu = ok;
120 if (!index.isValid() || !m_allowContextMenu)
123 if (index.column() == 1)
124 index = index.sibling(index.row(), 0);
126 QAction *closeEditor = contextMenu.addAction(tr(
"Close %1").arg(index.data()
128 QAction *closeOtherEditors = contextMenu.addAction(tr(
"Close All Except %1")
129 .arg(index.data().toString()));
131 if (model()->rowCount() == 1) {
132 closeEditor->setEnabled(
false);
133 closeOtherEditors->setEnabled(
false);
136 QAction *action = contextMenu.exec(mapToGlobal(pos));
137 if (action == closeEditor)
138 emit closePage(index);
139 else if (action == closeOtherEditors)
140 emit closePagesExcept(index);
146 if (index.column() == 0)
147 emit setCurrentPage(index);
149 if (index.column() == 1)
150 m_delegate->pressedIndex = index;
158 if (index.column() == 1) {
159 if (model()->rowCount() > 1)
160 emit closePage(index);
162 QWidget *vp = viewport();
163 const QPoint &cursorPos = QCursor::pos();
164 QMouseEvent e(QEvent::MouseMove, vp->mapFromGlobal(cursorPos), cursorPos,
165 Qt::NoButton, {}, {});
166 QCoreApplication::sendEvent(vp, &e);
174 return QWidget::eventFilter(obj, event);
176 if (event->type() == QEvent::KeyPress) {
177 QKeyEvent *ke =
static_cast<QKeyEvent*>(event);
178 if (currentIndex().isValid() && ke->modifiers() == 0) {
179 const int key = ke->key();
180 if (key == Qt::Key_Return || key == Qt::Key_Enter
181 || key == Qt::Key_Space) {
182 emit setCurrentPage(currentIndex());
183 }
else if ((key == Qt::Key_Delete || key == Qt::Key_Backspace)
184 && model()->rowCount() > 1) {
185 emit closePage(currentIndex());
188 }
else if (event->type() == QEvent::KeyRelease) {
189 QKeyEvent *ke =
static_cast<QKeyEvent*>(event);
190 if (ke->modifiers() == 0
191 && (ke->key() == Qt::Key_Up || ke->key() == Qt::Key_Down)) {
192 emit setCurrentPage(currentIndex());
195 return QWidget::eventFilter(obj, event);
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
This pure abstract function must be reimplemented if you want to provide custom rendering.
void allowContextMenu(bool ok)
~OpenPagesWidget() override
Combined button and popup list for selecting options.