Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
openpagesswitcher.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
5
9#include "tracer.h"
10
11#include <QtCore/QEvent>
12
13#include <QtGui/QKeyEvent>
14#include <QtWidgets/QVBoxLayout>
15
17
18const int gWidth = 300;
19const int gHeight = 200;
20
21OpenPagesSwitcher::OpenPagesSwitcher(OpenPagesModel *model)
22 : QFrame(nullptr, Qt::Popup)
23 , m_openPagesModel(model)
24{
26 resize(gWidth, gHeight);
27
28 m_openPagesWidget = new OpenPagesWidget(m_openPagesModel);
29
30 // We disable the frame on this list view and use a QFrame around it instead.
31 // This improves the look with QGTKStyle.
32#ifndef Q_OS_MAC
33 setFrameStyle(m_openPagesWidget->frameStyle());
34#endif
35 m_openPagesWidget->setFrameStyle(QFrame::NoFrame);
36
37 m_openPagesWidget->allowContextMenu(false);
38 m_openPagesWidget->installEventFilter(this);
39
40 QVBoxLayout *layout = new QVBoxLayout(this);
41 layout->setContentsMargins(QMargins());
42 layout->addWidget(m_openPagesWidget);
43
44 connect(m_openPagesWidget, &OpenPagesWidget::closePage,
45 this, &OpenPagesSwitcher::closePage);
46 connect(m_openPagesWidget, &OpenPagesWidget::setCurrentPage,
47 this, &OpenPagesSwitcher::setCurrentPage);
48}
49
54
56{
58 selectPageUpDown(1);
59}
60
62{
64 selectPageUpDown(-1);
65}
66
68{
70 setVisible(false);
71 emit setCurrentPage(m_openPagesWidget->currentIndex());
72}
73
79
80void OpenPagesSwitcher::setVisible(bool visible)
81{
83 QWidget::setVisible(visible);
84 if (visible)
85 setFocus();
86}
87
88void OpenPagesSwitcher::focusInEvent(QFocusEvent *event)
89{
91 Q_UNUSED(event);
92 m_openPagesWidget->setFocus();
93}
94
95bool OpenPagesSwitcher::eventFilter(QObject *object, QEvent *event)
96{
98 if (object == m_openPagesWidget) {
99 if (event->type() == QEvent::KeyPress) {
100 QKeyEvent *ke = static_cast<QKeyEvent*>(event);
101 if (ke->key() == Qt::Key_Escape) {
102 setVisible(false);
103 return true;
104 }
105
106 const int key = ke->key();
107 if (key == Qt::Key_Return || key == Qt::Key_Enter || key == Qt::Key_Space) {
108 emit setCurrentPage(m_openPagesWidget->currentIndex());
109 return true;
110 }
111
112 Qt::KeyboardModifier modifier = Qt::ControlModifier;
113#ifdef Q_OS_MAC
114 modifier = Qt::AltModifier;
115#endif
116 if (key == Qt::Key_Backtab
117 && (ke->modifiers() == (modifier | Qt::ShiftModifier)))
119 else if (key == Qt::Key_Tab && (ke->modifiers() == modifier))
121 } else if (event->type() == QEvent::KeyRelease) {
122 QKeyEvent *ke = static_cast<QKeyEvent*>(event);
123 if (ke->modifiers() == 0
124 /*HACK this is to overcome some event inconsistencies between platforms*/
125 || (ke->modifiers() == Qt::AltModifier
126 && (ke->key() == Qt::Key_Alt || ke->key() == -1))) {
128 }
129 }
130 }
131 return QWidget::eventFilter(object, event);
132}
133
134void OpenPagesSwitcher::selectPageUpDown(int summand)
135{
137 const int pageCount = m_openPagesModel->rowCount();
138 if (pageCount < 2)
139 return;
140
141 const QModelIndexList &list = m_openPagesWidget->selectionModel()->selectedIndexes();
142 if (list.isEmpty())
143 return;
144
145 QModelIndex index = list.first();
146 if (!index.isValid())
147 return;
148
149 index = m_openPagesModel->index((index.row() + summand + pageCount) % pageCount, 0);
150 if (index.isValid()) {
151 m_openPagesWidget->setCurrentIndex(index);
152 m_openPagesWidget->scrollTo(index, QAbstractItemView::PositionAtCenter);
153 }
154}
155
156QT_END_NAMESPACE
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Returns the number of rows under the given parent.
void focusInEvent(QFocusEvent *event) override
This event handler can be reimplemented in a subclass to receive keyboard focus events (focus receive...
void setVisible(bool visible) override
bool eventFilter(QObject *object, QEvent *event) override
Filters events if this object has been installed as an event filter for the watched object.
void allowContextMenu(bool ok)
\inmodule QtCore
Combined button and popup list for selecting options.
const int gHeight
QT_BEGIN_NAMESPACE const int gWidth
#define TRACE_OBJ
Definition tracer.h:34