2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2010-04-12 14:55:41 +02:00
|
|
|
|
2010-04-12 16:43:23 +02:00
|
|
|
#include "openpagesswitcher.h"
|
2010-04-12 14:55:41 +02:00
|
|
|
|
|
|
|
|
#include "openpageswidget.h"
|
|
|
|
|
|
2012-08-23 15:53:58 +02:00
|
|
|
#include <utils/hostosinfo.h>
|
|
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QEvent>
|
2010-04-12 14:55:41 +02:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QKeyEvent>
|
|
|
|
|
#include <QVBoxLayout>
|
2010-04-12 14:55:41 +02:00
|
|
|
|
|
|
|
|
using namespace Help::Internal;
|
|
|
|
|
|
|
|
|
|
const int gWidth = 300;
|
|
|
|
|
const int gHeight = 200;
|
|
|
|
|
|
2019-09-02 14:03:05 +02:00
|
|
|
OpenPagesSwitcher::OpenPagesSwitcher(QAbstractItemModel *model)
|
2018-04-19 10:47:13 +02:00
|
|
|
: QFrame(nullptr, Qt::Popup)
|
2010-04-12 14:55:41 +02:00
|
|
|
, m_openPagesModel(model)
|
|
|
|
|
{
|
|
|
|
|
resize(gWidth, gHeight);
|
|
|
|
|
|
|
|
|
|
m_openPagesWidget = new OpenPagesWidget(m_openPagesModel, this);
|
|
|
|
|
|
2010-04-15 17:13:10 +02:00
|
|
|
// We disable the frame on this list view and use a QFrame around it instead.
|
|
|
|
|
// This improves the look with QGTKStyle.
|
2012-08-23 15:53:58 +02:00
|
|
|
if (!Utils::HostOsInfo::isMacHost())
|
|
|
|
|
setFrameStyle(m_openPagesWidget->frameStyle());
|
2010-04-15 17:13:10 +02:00
|
|
|
m_openPagesWidget->setFrameStyle(QFrame::NoFrame);
|
|
|
|
|
|
2010-04-12 14:55:41 +02:00
|
|
|
m_openPagesWidget->allowContextMenu(false);
|
|
|
|
|
m_openPagesWidget->installEventFilter(this);
|
2010-04-15 17:13:10 +02:00
|
|
|
|
2018-11-11 10:08:29 +01:00
|
|
|
auto layout = new QVBoxLayout(this);
|
2019-08-29 10:36:01 +02:00
|
|
|
layout->setContentsMargins(0, 0, 0, 0);
|
2010-04-15 17:13:10 +02:00
|
|
|
layout->addWidget(m_openPagesWidget);
|
2010-04-12 14:55:41 +02:00
|
|
|
|
2015-01-29 14:32:36 +01:00
|
|
|
connect(m_openPagesWidget, &OpenPagesWidget::closePage,
|
|
|
|
|
this, &OpenPagesSwitcher::closePage);
|
|
|
|
|
connect(m_openPagesWidget, &OpenPagesWidget::setCurrentPage,
|
|
|
|
|
this, &OpenPagesSwitcher::setCurrentPage);
|
2010-04-12 14:55:41 +02:00
|
|
|
}
|
|
|
|
|
|
2018-11-11 10:08:29 +01:00
|
|
|
OpenPagesSwitcher::~OpenPagesSwitcher() = default;
|
2010-04-12 14:55:41 +02:00
|
|
|
|
2010-04-12 16:43:23 +02:00
|
|
|
void OpenPagesSwitcher::gotoNextPage()
|
2010-04-12 14:55:41 +02:00
|
|
|
{
|
|
|
|
|
selectPageUpDown(-1);
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-12 16:43:23 +02:00
|
|
|
void OpenPagesSwitcher::gotoPreviousPage()
|
2010-04-12 14:55:41 +02:00
|
|
|
{
|
|
|
|
|
selectPageUpDown(1);
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-12 16:43:23 +02:00
|
|
|
void OpenPagesSwitcher::selectAndHide()
|
2010-04-12 14:55:41 +02:00
|
|
|
{
|
|
|
|
|
setVisible(false);
|
2021-02-25 15:29:40 +02:00
|
|
|
QModelIndex index = m_openPagesWidget->currentIndex();
|
|
|
|
|
if (index.isValid())
|
|
|
|
|
emit setCurrentPage(index);
|
2010-04-12 14:55:41 +02:00
|
|
|
}
|
|
|
|
|
|
2019-09-03 09:03:38 +02:00
|
|
|
void OpenPagesSwitcher::selectCurrentPage(int index)
|
2010-04-12 14:55:41 +02:00
|
|
|
{
|
2019-09-03 09:03:38 +02:00
|
|
|
m_openPagesWidget->selectCurrentPage(index);
|
2010-04-12 14:55:41 +02:00
|
|
|
}
|
|
|
|
|
|
2010-04-12 16:43:23 +02:00
|
|
|
void OpenPagesSwitcher::setVisible(bool visible)
|
2010-04-12 14:55:41 +02:00
|
|
|
{
|
|
|
|
|
QWidget::setVisible(visible);
|
|
|
|
|
if (visible)
|
|
|
|
|
setFocus();
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-12 16:43:23 +02:00
|
|
|
void OpenPagesSwitcher::focusInEvent(QFocusEvent *event)
|
2010-04-12 14:55:41 +02:00
|
|
|
{
|
|
|
|
|
Q_UNUSED(event)
|
|
|
|
|
m_openPagesWidget->setFocus();
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-12 16:43:23 +02:00
|
|
|
bool OpenPagesSwitcher::eventFilter(QObject *object, QEvent *event)
|
2010-04-12 14:55:41 +02:00
|
|
|
{
|
|
|
|
|
if (object == m_openPagesWidget) {
|
|
|
|
|
if (event->type() == QEvent::KeyPress) {
|
2018-11-11 10:08:29 +01:00
|
|
|
auto ke = static_cast<const QKeyEvent*>(event);
|
2010-04-12 14:55:41 +02:00
|
|
|
if (ke->key() == Qt::Key_Escape) {
|
|
|
|
|
setVisible(false);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const int key = ke->key();
|
|
|
|
|
if (key == Qt::Key_Return || key == Qt::Key_Enter || key == Qt::Key_Space) {
|
|
|
|
|
emit setCurrentPage(m_openPagesWidget->currentIndex());
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2012-08-23 15:53:58 +02:00
|
|
|
const Qt::KeyboardModifiers modifier = Utils::HostOsInfo::isMacHost()
|
|
|
|
|
? Qt::AltModifier : Qt::ControlModifier;
|
2010-08-04 15:34:25 +02:00
|
|
|
if (key == Qt::Key_Backtab
|
|
|
|
|
&& (ke->modifiers() == (modifier | Qt::ShiftModifier)))
|
|
|
|
|
gotoNextPage();
|
|
|
|
|
else if (key == Qt::Key_Tab && (ke->modifiers() == modifier))
|
|
|
|
|
gotoPreviousPage();
|
2010-04-12 14:55:41 +02:00
|
|
|
} else if (event->type() == QEvent::KeyRelease) {
|
2018-11-11 10:08:29 +01:00
|
|
|
auto ke = static_cast<const QKeyEvent*>(event);
|
2010-04-12 14:55:41 +02:00
|
|
|
if (ke->modifiers() == 0
|
|
|
|
|
/*HACK this is to overcome some event inconsistencies between platforms*/
|
|
|
|
|
|| (ke->modifiers() == Qt::AltModifier
|
|
|
|
|
&& (ke->key() == Qt::Key_Alt || ke->key() == -1))) {
|
|
|
|
|
selectAndHide();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return QWidget::eventFilter(object, event);
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-12 16:43:23 +02:00
|
|
|
void OpenPagesSwitcher::selectPageUpDown(int summand)
|
2010-04-12 14:55:41 +02:00
|
|
|
{
|
|
|
|
|
const int pageCount = m_openPagesModel->rowCount();
|
|
|
|
|
if (pageCount < 2)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
const QModelIndexList &list = m_openPagesWidget->selectionModel()->selectedIndexes();
|
|
|
|
|
if (list.isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
QModelIndex index = list.first();
|
|
|
|
|
if (!index.isValid())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
index = m_openPagesModel->index((index.row() + summand + pageCount) % pageCount, 0);
|
|
|
|
|
if (index.isValid()) {
|
|
|
|
|
m_openPagesWidget->setCurrentIndex(index);
|
|
|
|
|
m_openPagesWidget->scrollTo(index, QAbstractItemView::PositionAtCenter);
|
|
|
|
|
}
|
|
|
|
|
}
|