2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2010-03-29 14:57:02 +02:00
|
|
|
**
|
2014-01-07 13:27:11 +01:00
|
|
|
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
2012-10-02 09:12:39 +02:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
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
|
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
|
|
|
|
** use the contact form at http://qt.digia.com/contact-us.
|
2010-03-29 14:57:02 +02:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
2012-10-02 09:12:39 +02:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
|
|
|
** General Public License version 2.1 as published by the Free Software
|
|
|
|
|
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
|
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
|
|
|
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
|
|
|
**
|
|
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
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);
|
|
|
|
|
connect(model(), SIGNAL(rowsInserted(QModelIndex,int,int)),
|
|
|
|
|
this, SLOT(updateCloseButtonVisibility()));
|
|
|
|
|
connect(model(), SIGNAL(rowsRemoved(QModelIndex,int,int)),
|
|
|
|
|
this, SLOT(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;
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-29 14:57:02 +02:00
|
|
|
// -- private slots
|
|
|
|
|
|
|
|
|
|
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();
|
2014-09-23 12:56:28 +02:00
|
|
|
QMouseEvent e(QEvent::MouseMove, vp->mapFromGlobal(cursorPos), cursorPos, Qt::NoButton, 0, 0);
|
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
|
|
|
}
|