2014-09-23 12:56:28 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2014-09-23 12:56:28 +02:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** 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.
|
2014-09-23 12:56:28 +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.
|
2014-09-23 12:56:28 +02:00
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "opendocumentstreeview.h"
|
|
|
|
|
|
2016-08-03 17:55:54 +02:00
|
|
|
#include <utils/utilsicons.h>
|
2014-09-23 12:56:28 +02:00
|
|
|
|
|
|
|
|
#include <QApplication>
|
|
|
|
|
#include <QHeaderView>
|
|
|
|
|
#include <QPainter>
|
|
|
|
|
#include <QStyledItemDelegate>
|
|
|
|
|
|
|
|
|
|
namespace Core {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
|
|
|
|
class OpenDocumentsDelegate : public QStyledItemDelegate
|
|
|
|
|
{
|
|
|
|
|
public:
|
2018-07-21 21:11:46 +02:00
|
|
|
explicit OpenDocumentsDelegate(QObject *parent = nullptr);
|
2014-09-23 12:56:28 +02:00
|
|
|
|
|
|
|
|
void setCloseButtonVisible(bool visible);
|
|
|
|
|
void handlePressed(const QModelIndex &index);
|
|
|
|
|
void paint(QPainter *painter, const QStyleOptionViewItem &option,
|
2018-07-21 21:11:46 +02:00
|
|
|
const QModelIndex &index) const override;
|
2014-09-23 12:56:28 +02:00
|
|
|
|
|
|
|
|
mutable QModelIndex pressedIndex;
|
2018-07-21 21:11:46 +02:00
|
|
|
bool closeButtonVisible = true;
|
2014-09-23 12:56:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
OpenDocumentsDelegate::OpenDocumentsDelegate(QObject *parent)
|
2018-07-21 21:11:46 +02:00
|
|
|
: QStyledItemDelegate(parent)
|
2014-09-23 12:56:28 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OpenDocumentsDelegate::setCloseButtonVisible(bool visible)
|
|
|
|
|
{
|
|
|
|
|
closeButtonVisible = visible;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OpenDocumentsDelegate::handlePressed(const QModelIndex &index)
|
|
|
|
|
{
|
|
|
|
|
if (index.column() == 1)
|
|
|
|
|
pressedIndex = index;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OpenDocumentsDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
|
|
|
|
|
const QModelIndex &index) const
|
|
|
|
|
{
|
|
|
|
|
if (option.state & QStyle::State_MouseOver) {
|
|
|
|
|
if ((QApplication::mouseButtons() & Qt::LeftButton) == 0)
|
|
|
|
|
pressedIndex = QModelIndex();
|
|
|
|
|
QBrush brush = option.palette.alternateBase();
|
|
|
|
|
if (index == pressedIndex)
|
|
|
|
|
brush = option.palette.dark();
|
|
|
|
|
painter->fillRect(option.rect, brush);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QStyledItemDelegate::paint(painter, option, index);
|
|
|
|
|
|
|
|
|
|
if (closeButtonVisible && index.column() == 1 && option.state & QStyle::State_MouseOver) {
|
2015-11-23 16:41:54 +01:00
|
|
|
const QIcon icon = (option.state & QStyle::State_Selected) ?
|
2016-08-03 17:55:54 +02:00
|
|
|
Utils::Icons::CLOSE_BACKGROUND.icon()
|
|
|
|
|
: Utils::Icons::CLOSE_FOREGROUND.icon();
|
2014-09-23 12:56:28 +02:00
|
|
|
|
|
|
|
|
QRect iconRect(option.rect.right() - option.rect.height(),
|
|
|
|
|
option.rect.top(),
|
|
|
|
|
option.rect.height(),
|
|
|
|
|
option.rect.height());
|
|
|
|
|
|
|
|
|
|
icon.paint(painter, iconRect, Qt::AlignRight | Qt::AlignVCenter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
|
|
|
|
|
OpenDocumentsTreeView::OpenDocumentsTreeView(QWidget *parent) :
|
|
|
|
|
Utils::TreeView(parent)
|
|
|
|
|
{
|
|
|
|
|
m_delegate = new Internal::OpenDocumentsDelegate(this);
|
|
|
|
|
setItemDelegate(m_delegate);
|
|
|
|
|
setIndentation(0);
|
|
|
|
|
setUniformRowHeights(true);
|
|
|
|
|
setTextElideMode(Qt::ElideMiddle);
|
|
|
|
|
setFrameStyle(QFrame::NoFrame);
|
|
|
|
|
setAttribute(Qt::WA_MacShowFocusRect, false);
|
2016-07-11 12:35:40 +02:00
|
|
|
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
|
2014-09-23 12:56:28 +02:00
|
|
|
viewport()->setAttribute(Qt::WA_Hover);
|
|
|
|
|
|
|
|
|
|
setSelectionMode(QAbstractItemView::SingleSelection);
|
|
|
|
|
setSelectionBehavior(QAbstractItemView::SelectRows);
|
|
|
|
|
setActivationMode(Utils::SingleClickActivation);
|
|
|
|
|
|
|
|
|
|
installEventFilter(this);
|
|
|
|
|
viewport()->installEventFilter(this);
|
|
|
|
|
|
|
|
|
|
connect(this, &OpenDocumentsTreeView::pressed,
|
|
|
|
|
m_delegate, &Internal::OpenDocumentsDelegate::handlePressed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OpenDocumentsTreeView::setModel(QAbstractItemModel *model)
|
|
|
|
|
{
|
|
|
|
|
Utils::TreeView::setModel(model);
|
|
|
|
|
header()->hide();
|
|
|
|
|
header()->setStretchLastSection(false);
|
|
|
|
|
header()->setSectionResizeMode(0, QHeaderView::Stretch);
|
|
|
|
|
header()->setSectionResizeMode(1, QHeaderView::Fixed);
|
|
|
|
|
header()->resizeSection(1, 16);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OpenDocumentsTreeView::setCloseButtonVisible(bool visible)
|
|
|
|
|
{
|
|
|
|
|
m_delegate->setCloseButtonVisible(visible);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool OpenDocumentsTreeView::eventFilter(QObject *obj, QEvent *event)
|
|
|
|
|
{
|
|
|
|
|
if (obj == this && event->type() == QEvent::KeyPress
|
|
|
|
|
&& currentIndex().isValid()) {
|
2018-07-21 21:11:46 +02:00
|
|
|
auto ke = static_cast<QKeyEvent*>(event);
|
2014-09-23 12:56:28 +02:00
|
|
|
if ((ke->key() == Qt::Key_Delete
|
|
|
|
|
|| ke->key() == Qt::Key_Backspace)
|
|
|
|
|
&& ke->modifiers() == 0) {
|
|
|
|
|
emit closeActivated(currentIndex());
|
|
|
|
|
}
|
|
|
|
|
} else if (obj == viewport()
|
|
|
|
|
&& event->type() == QEvent::MouseButtonRelease) {
|
2018-07-21 21:11:46 +02:00
|
|
|
auto me = static_cast<QMouseEvent*>(event);
|
2014-09-23 12:56:28 +02:00
|
|
|
if (me->button() == Qt::MiddleButton
|
|
|
|
|
&& me->modifiers() == Qt::NoModifier) {
|
|
|
|
|
QModelIndex index = indexAt(me->pos());
|
|
|
|
|
if (index.isValid()) {
|
|
|
|
|
emit closeActivated(index);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Core
|