2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2010-11-01 16:29:45 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2010-11-01 16:29:45 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2010-11-01 16:29:45 +01: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-11-01 16:29:45 +01: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 17:14:20 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2010-11-01 16:29:45 +01:00
|
|
|
|
2010-07-02 13:47:14 +02:00
|
|
|
#include "refactoroverlay.h"
|
2014-09-26 09:14:03 +02:00
|
|
|
#include "textdocumentlayout.h"
|
|
|
|
|
#include "texteditor.h"
|
2010-11-01 16:29:45 +01:00
|
|
|
|
2018-04-13 14:43:59 +02:00
|
|
|
#include <utils/utilsicons.h>
|
2016-10-11 01:04:14 +02:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QPainter>
|
2010-07-02 13:47:14 +02:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QDebug>
|
2010-11-01 16:29:45 +01:00
|
|
|
|
|
|
|
|
namespace TextEditor {
|
2010-07-02 13:47:14 +02:00
|
|
|
|
2014-09-26 11:37:54 +02:00
|
|
|
RefactorOverlay::RefactorOverlay(TextEditor::TextEditorWidget *editor) :
|
2010-07-02 13:47:14 +02:00
|
|
|
QObject(editor),
|
|
|
|
|
m_editor(editor),
|
|
|
|
|
m_maxWidth(0),
|
2018-04-13 14:43:59 +02:00
|
|
|
m_icon(Utils::Icons::CODEMODEL_FIXIT.icon())
|
2010-07-02 13:47:14 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RefactorOverlay::paint(QPainter *painter, const QRect &clip)
|
|
|
|
|
{
|
|
|
|
|
m_maxWidth = 0;
|
|
|
|
|
for (int i = 0; i < m_markers.size(); ++i) {
|
2010-07-05 12:45:56 +02:00
|
|
|
paintMarker(m_markers.at(i), painter, clip);
|
2010-07-02 13:47:14 +02:00
|
|
|
}
|
|
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
if (TextDocumentLayout *documentLayout = qobject_cast<TextDocumentLayout*>(m_editor->document()->documentLayout()))
|
2010-07-02 13:47:14 +02:00
|
|
|
documentLayout->setRequiredWidth(m_maxWidth);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RefactorMarker RefactorOverlay::markerAt(const QPoint &pos) const
|
|
|
|
|
{
|
2012-11-28 20:44:03 +02:00
|
|
|
foreach (const RefactorMarker &marker, m_markers) {
|
2014-07-18 19:42:04 +02:00
|
|
|
if (marker.rect.contains(pos))
|
2010-07-02 13:47:14 +02:00
|
|
|
return marker;
|
|
|
|
|
}
|
|
|
|
|
return RefactorMarker();
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-05 12:45:56 +02:00
|
|
|
void RefactorOverlay::paintMarker(const RefactorMarker& marker, QPainter *painter, const QRect &clip)
|
2010-07-02 13:47:14 +02:00
|
|
|
{
|
2016-02-08 11:11:32 +01:00
|
|
|
const QPointF offset = m_editor->contentOffset();
|
|
|
|
|
const QRectF geometry = m_editor->blockBoundingGeometry(marker.cursor.block()).translated(offset);
|
2010-07-02 13:47:14 +02:00
|
|
|
|
|
|
|
|
if (geometry.top() > clip.bottom() + 10 || geometry.bottom() < clip.top() - 10)
|
|
|
|
|
return; // marker not visible
|
|
|
|
|
|
2016-02-08 11:11:32 +01:00
|
|
|
const QTextCursor cursor = marker.cursor;
|
|
|
|
|
const QRect cursorRect = m_editor->cursorRect(cursor);
|
2010-07-02 13:47:14 +02:00
|
|
|
|
|
|
|
|
QIcon icon = marker.icon;
|
|
|
|
|
if (icon.isNull())
|
|
|
|
|
icon = m_icon;
|
|
|
|
|
|
2016-10-18 13:40:44 +02:00
|
|
|
const qreal devicePixelRatio = painter->device()->devicePixelRatio();
|
2016-02-08 11:19:24 +01:00
|
|
|
const QSize proposedIconSize = QSize(m_editor->fontMetrics().width(QLatin1Char(' ')) + 3,
|
2016-10-18 13:40:44 +02:00
|
|
|
cursorRect.height()) * devicePixelRatio;
|
|
|
|
|
const QSize actualIconSize = icon.actualSize(proposedIconSize) / devicePixelRatio;
|
2010-07-02 13:47:14 +02:00
|
|
|
|
2016-02-08 11:19:24 +01:00
|
|
|
const int y = cursorRect.top() + ((cursorRect.height() - actualIconSize.height()) / 2);
|
2016-02-08 11:11:32 +01:00
|
|
|
const int x = cursorRect.right();
|
2016-02-08 11:19:24 +01:00
|
|
|
marker.rect = QRect(x, y, actualIconSize.width(), actualIconSize.height());
|
2010-07-05 12:06:09 +02:00
|
|
|
|
2010-07-02 13:47:14 +02:00
|
|
|
icon.paint(painter, marker.rect);
|
2016-02-08 11:11:32 +01:00
|
|
|
m_maxWidth = qMax(m_maxWidth, x + actualIconSize.width() - int(offset.x()));
|
2010-07-02 13:47:14 +02:00
|
|
|
}
|
|
|
|
|
|
2010-11-01 16:29:45 +01:00
|
|
|
} // namespace TextEditor
|