2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2010-03-29 14:57:02 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2010-03-29 14:57:02 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2010-03-29 14:57:02 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2016-01-15 14:57:40 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2010-03-29 14:57:02 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2010-12-17 16:01:08 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2010-03-29 14:57:02 +02:00
|
|
|
|
|
|
|
|
#include "openpageswidget.h"
|
2010-04-12 14:55:41 +02:00
|
|
|
|
|
|
|
|
#include "centralwidget.h"
|
2010-03-29 14:57:02 +02:00
|
|
|
#include "openpagesmodel.h"
|
|
|
|
|
|
2010-07-30 22:16:59 +02:00
|
|
|
#include <coreplugin/coreconstants.h>
|
|
|
|
|
|
2014-09-23 12:56:28 +02:00
|
|
|
#include <QAbstractItemModel>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QApplication>
|
|
|
|
|
#include <QMenu>
|
2010-03-29 14:57:02 +02:00
|
|
|
|
|
|
|
|
using namespace Help::Internal;
|
|
|
|
|
|
|
|
|
|
// -- OpenPagesWidget
|
|
|
|
|
|
2014-09-23 12:56:28 +02:00
|
|
|
OpenPagesWidget::OpenPagesWidget(OpenPagesModel *sourceModel, QWidget *parent)
|
|
|
|
|
: OpenDocumentsTreeView(parent)
|
2010-04-12 14:55:41 +02:00
|
|
|
, m_allowContextMenu(true)
|
2010-03-29 14:57:02 +02:00
|
|
|
{
|
2014-09-23 12:56:28 +02:00
|
|
|
setModel(sourceModel);
|
2010-03-29 14:57:02 +02:00
|
|
|
|
|
|
|
|
setContextMenuPolicy(Qt::CustomContextMenu);
|
2014-09-23 12:56:28 +02:00
|
|
|
updateCloseButtonVisibility();
|
|
|
|
|
|
|
|
|
|
connect(this, &OpenDocumentsTreeView::activated,
|
|
|
|
|
this, &OpenPagesWidget::handleActivated);
|
|
|
|
|
connect(this, &OpenDocumentsTreeView::closeActivated,
|
|
|
|
|
this, &OpenPagesWidget::handleCloseActivated);
|
|
|
|
|
connect(this, &OpenDocumentsTreeView::customContextMenuRequested,
|
|
|
|
|
this, &OpenPagesWidget::contextMenuRequested);
|
2016-06-28 23:07:19 +03:00
|
|
|
connect(model(), &QAbstractItemModel::rowsInserted,
|
|
|
|
|
this, &OpenPagesWidget::updateCloseButtonVisibility);
|
|
|
|
|
connect(model(), &QAbstractItemModel::rowsRemoved,
|
|
|
|
|
this, &OpenPagesWidget::updateCloseButtonVisibility);
|
2010-03-29 14:57:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OpenPagesWidget::~OpenPagesWidget()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-12 14:55:41 +02:00
|
|
|
void OpenPagesWidget::selectCurrentPage()
|
|
|
|
|
{
|
|
|
|
|
QItemSelectionModel * const selModel = selectionModel();
|
|
|
|
|
selModel->clearSelection();
|
|
|
|
|
selModel->select(model()->index(CentralWidget::instance()->currentIndex(), 0),
|
|
|
|
|
QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
|
|
|
|
|
scrollTo(currentIndex());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OpenPagesWidget::allowContextMenu(bool ok)
|
|
|
|
|
{
|
|
|
|
|
m_allowContextMenu = ok;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-28 23:07:19 +03:00
|
|
|
// -- private
|
2010-03-29 14:57:02 +02:00
|
|
|
|
|
|
|
|
void OpenPagesWidget::contextMenuRequested(QPoint pos)
|
|
|
|
|
{
|
2010-04-26 11:56:37 +02:00
|
|
|
QModelIndex index = indexAt(pos);
|
2010-04-12 14:55:41 +02:00
|
|
|
if (!index.isValid() || !m_allowContextMenu)
|
2010-03-29 14:57:02 +02:00
|
|
|
return;
|
|
|
|
|
|
2010-04-26 11:56:37 +02:00
|
|
|
if (index.column() == 1)
|
|
|
|
|
index = index.sibling(index.row(), 0);
|
2010-03-29 14:57:02 +02:00
|
|
|
QMenu contextMenu;
|
|
|
|
|
QAction *closeEditor = contextMenu.addAction(tr("Close %1").arg(index.data()
|
|
|
|
|
.toString()));
|
|
|
|
|
QAction *closeOtherEditors = contextMenu.addAction(tr("Close All Except %1")
|
|
|
|
|
.arg(index.data().toString()));
|
|
|
|
|
|
|
|
|
|
if (model()->rowCount() == 1) {
|
|
|
|
|
closeEditor->setEnabled(false);
|
|
|
|
|
closeOtherEditors->setEnabled(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QAction *action = contextMenu.exec(mapToGlobal(pos));
|
|
|
|
|
if (action == closeEditor)
|
|
|
|
|
emit closePage(index);
|
|
|
|
|
else if (action == closeOtherEditors)
|
|
|
|
|
emit closePagesExcept(index);
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-23 12:56:28 +02:00
|
|
|
void OpenPagesWidget::handleActivated(const QModelIndex &index)
|
2010-03-29 14:57:02 +02:00
|
|
|
{
|
2014-09-23 12:56:28 +02:00
|
|
|
if (index.column() == 0) {
|
2010-03-29 14:57:02 +02:00
|
|
|
emit setCurrentPage(index);
|
2014-09-23 12:56:28 +02:00
|
|
|
} else if (index.column() == 1) { // the funky close button
|
2010-03-29 14:57:02 +02:00
|
|
|
if (model()->rowCount() > 1)
|
|
|
|
|
emit closePage(index);
|
|
|
|
|
|
2014-09-23 12:56:28 +02:00
|
|
|
// work around a bug in itemviews where the delegate wouldn't get the QStyle::State_MouseOver
|
2010-03-29 14:57:02 +02:00
|
|
|
QWidget *vp = viewport();
|
|
|
|
|
const QPoint &cursorPos = QCursor::pos();
|
2018-04-19 10:47:13 +02:00
|
|
|
QMouseEvent e(QEvent::MouseMove, vp->mapFromGlobal(cursorPos), cursorPos, Qt::NoButton, nullptr, nullptr);
|
2010-03-29 14:57:02 +02:00
|
|
|
QCoreApplication::sendEvent(vp, &e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-23 12:56:28 +02:00
|
|
|
void OpenPagesWidget::handleCloseActivated(const QModelIndex &index)
|
|
|
|
|
{
|
|
|
|
|
if (model()->rowCount() > 1)
|
|
|
|
|
emit closePage(index);
|
|
|
|
|
}
|
2010-03-29 14:57:02 +02:00
|
|
|
|
2014-09-23 12:56:28 +02:00
|
|
|
void OpenPagesWidget::updateCloseButtonVisibility()
|
2010-03-29 14:57:02 +02:00
|
|
|
{
|
2014-09-23 12:56:28 +02:00
|
|
|
setCloseButtonVisible(model() && model()->rowCount() > 1);
|
2010-03-29 14:57:02 +02:00
|
|
|
}
|