2014-05-05 14:31:54 +02:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2014-05-05 14:31:54 +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-05-05 14:31:54 +02:00
|
|
|
**
|
2015-09-18 11:34:48 +02:00
|
|
|
** GNU General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
2016-01-15 14:57:40 +01:00
|
|
|
** 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-05-05 14:31:54 +02:00
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "nameitemdelegate.h"
|
|
|
|
|
|
|
|
#include <qmath.h>
|
|
|
|
|
|
|
|
#include "navigatorview.h"
|
|
|
|
#include "navigatortreeview.h"
|
|
|
|
#include "qproxystyle.h"
|
|
|
|
|
2017-05-11 13:31:55 +02:00
|
|
|
#include <metainfo.h>
|
|
|
|
#include <modelnodecontextmenu.h>
|
|
|
|
#include <qmlobjectnode.h>
|
2020-10-22 09:42:36 +02:00
|
|
|
#include <theme.h>
|
2017-05-11 13:31:55 +02:00
|
|
|
|
|
|
|
#include <coreplugin/messagebox.h>
|
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
2014-05-05 14:31:54 +02:00
|
|
|
#include <QLineEdit>
|
|
|
|
#include <QPen>
|
|
|
|
#include <QPixmapCache>
|
|
|
|
#include <QMouseEvent>
|
|
|
|
#include <QPainter>
|
2020-03-18 11:20:44 +01:00
|
|
|
#include <QPainterPath>
|
2014-05-05 14:31:54 +02:00
|
|
|
|
|
|
|
namespace QmlDesigner {
|
|
|
|
|
2020-10-22 09:42:36 +02:00
|
|
|
int NameItemDelegate::iconOffset = 0;
|
|
|
|
|
2014-05-05 14:31:54 +02:00
|
|
|
static QPixmap generateWavyPixmap(qreal maxRadius, const QPen &pen)
|
|
|
|
{
|
|
|
|
QPixmap pixmap;
|
|
|
|
|
2014-05-06 12:44:44 +02:00
|
|
|
const qreal radiusBase = qMax(qreal(1), maxRadius);
|
2014-05-05 14:31:54 +02:00
|
|
|
const qreal halfPeriod = qMax(qreal(2), qreal(radiusBase * 1.61803399)); // the golden ratio
|
|
|
|
const int width = qCeil(100 / (2 * halfPeriod)) * (2 * halfPeriod);
|
|
|
|
const int radius = qFloor(radiusBase);
|
|
|
|
|
|
|
|
QPainterPath path;
|
|
|
|
|
|
|
|
qreal xs = 0;
|
|
|
|
qreal ys = radius;
|
|
|
|
|
|
|
|
while (xs < width) {
|
|
|
|
xs += halfPeriod;
|
|
|
|
ys = -ys;
|
|
|
|
path.quadTo(xs - halfPeriod / 2, ys, xs, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
pixmap = QPixmap(width, radius * 2);
|
|
|
|
pixmap.fill(Qt::transparent);
|
|
|
|
{
|
|
|
|
QPen wavePen = pen;
|
|
|
|
wavePen.setCapStyle(Qt::SquareCap);
|
|
|
|
|
|
|
|
// This is to protect against making the line too fat, as happens on Mac OS X
|
|
|
|
// due to it having a rather thick width for the regular underline.
|
|
|
|
const qreal maxPenWidth = .8 * radius;
|
|
|
|
if (wavePen.widthF() > maxPenWidth)
|
|
|
|
wavePen.setWidth(maxPenWidth);
|
|
|
|
|
|
|
|
QPainter imgPainter(&pixmap);
|
|
|
|
imgPainter.setPen(wavePen);
|
|
|
|
imgPainter.setRenderHint(QPainter::Antialiasing);
|
|
|
|
imgPainter.translate(0, radius);
|
|
|
|
imgPainter.drawPath(path);
|
|
|
|
}
|
|
|
|
|
2014-05-06 12:44:44 +02:00
|
|
|
return pixmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
static QPixmap getWavyPixmap(qreal maxRadius, const QPen &pen)
|
|
|
|
{
|
|
|
|
const QString pixmapKey = QStringLiteral("WaveUnderline-Bauhaus");
|
|
|
|
|
|
|
|
QPixmap pixmap;
|
2019-08-19 11:55:05 +02:00
|
|
|
if (QPixmapCache::find(pixmapKey, &pixmap))
|
2014-05-06 12:44:44 +02:00
|
|
|
return pixmap;
|
|
|
|
|
|
|
|
pixmap = generateWavyPixmap(maxRadius, pen);
|
|
|
|
|
|
|
|
QPixmapCache::insert(pixmapKey, pixmap);
|
2014-05-05 14:31:54 +02:00
|
|
|
|
|
|
|
return pixmap;
|
|
|
|
}
|
|
|
|
|
2017-05-11 13:31:55 +02:00
|
|
|
NameItemDelegate::NameItemDelegate(QObject *parent)
|
|
|
|
: QStyledItemDelegate(parent)
|
2014-05-05 14:31:54 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-07-15 17:38:27 +02:00
|
|
|
static int drawIcon(QPainter *painter, const QStyleOptionViewItem &styleOption, const QModelIndex &modelIndex)
|
2014-05-06 12:44:44 +02:00
|
|
|
{
|
2020-10-22 09:42:36 +02:00
|
|
|
const QIcon icon = modelIndex.data(Qt::DecorationRole).value<QIcon>();
|
|
|
|
int pixmapSize = icon.isNull() ? 4 : 16;
|
2015-07-15 17:38:27 +02:00
|
|
|
QPixmap pixmap = icon.pixmap(pixmapSize, pixmapSize);
|
2017-05-11 11:23:28 +02:00
|
|
|
|
2020-10-22 09:42:36 +02:00
|
|
|
painter->drawPixmap(styleOption.rect.x() + 1 + delegateMargin,
|
|
|
|
styleOption.rect.y() + 2 + delegateMargin,
|
|
|
|
pixmap);
|
|
|
|
|
|
|
|
pixmapSize += delegateMargin;
|
2014-05-05 14:31:54 +02:00
|
|
|
|
2014-05-06 12:44:44 +02:00
|
|
|
return pixmapSize;
|
|
|
|
}
|
2014-05-05 14:31:54 +02:00
|
|
|
|
2014-05-06 12:44:44 +02:00
|
|
|
static QRect drawText(QPainter *painter,
|
2017-05-05 14:34:21 +02:00
|
|
|
const QStyleOptionViewItem &styleOption,
|
|
|
|
const QModelIndex &modelIndex,
|
|
|
|
int iconOffset)
|
2014-05-06 12:44:44 +02:00
|
|
|
{
|
2015-07-15 17:38:27 +02:00
|
|
|
QString displayString = modelIndex.data(Qt::DisplayRole).toString();
|
2014-05-06 12:44:44 +02:00
|
|
|
QPoint displayStringOffset;
|
|
|
|
int width = 0;
|
|
|
|
|
2015-07-15 17:38:27 +02:00
|
|
|
// Check text length does not exceed available space
|
2020-10-22 09:42:36 +02:00
|
|
|
const int extraSpace = 12 + iconOffset;
|
|
|
|
|
|
|
|
displayString = styleOption.fontMetrics.elidedText(displayString,
|
|
|
|
Qt::ElideMiddle,
|
|
|
|
styleOption.rect.width() - extraSpace);
|
|
|
|
displayStringOffset = QPoint(5 + iconOffset, -5 - delegateMargin);
|
2014-05-05 14:31:54 +02:00
|
|
|
|
2019-02-11 10:32:46 +01:00
|
|
|
width = styleOption.fontMetrics.horizontalAdvance(displayString);
|
2014-05-05 14:31:54 +02:00
|
|
|
|
2020-10-22 09:42:36 +02:00
|
|
|
const QPoint textPosition = styleOption.rect.bottomLeft() + displayStringOffset;
|
2014-05-06 12:44:44 +02:00
|
|
|
painter->drawText(textPosition, displayString);
|
2014-05-05 14:31:54 +02:00
|
|
|
|
2014-05-06 12:44:44 +02:00
|
|
|
QRect textFrame;
|
|
|
|
textFrame.setTopLeft(textPosition);
|
|
|
|
textFrame.setWidth(width);
|
2014-05-05 14:31:54 +02:00
|
|
|
|
2014-05-06 12:44:44 +02:00
|
|
|
return textFrame;
|
|
|
|
}
|
2014-05-05 14:31:54 +02:00
|
|
|
|
2020-10-22 09:42:36 +02:00
|
|
|
static bool isThisOrAncestorLocked(const QModelIndex &modelIndex)
|
2017-05-11 14:07:03 +02:00
|
|
|
{
|
2020-10-22 09:42:36 +02:00
|
|
|
return modelIndex.model()->data(modelIndex, ItemOrAncestorLocked).toBool();
|
2017-05-11 13:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static ModelNode getModelNode(const QModelIndex &modelIndex)
|
|
|
|
{
|
|
|
|
return modelIndex.model()->data(modelIndex, ModelNodeRole).value<ModelNode>();
|
2017-05-11 14:07:03 +02:00
|
|
|
}
|
|
|
|
|
2014-05-06 12:44:44 +02:00
|
|
|
static void drawRedWavyUnderLine(QPainter *painter,
|
|
|
|
const QStyleOptionViewItem &styleOption,
|
2015-07-15 17:38:27 +02:00
|
|
|
const QRect &textFrame)
|
2014-05-06 12:44:44 +02:00
|
|
|
{
|
2015-07-15 17:38:27 +02:00
|
|
|
painter->translate(0, textFrame.y() + 1);
|
|
|
|
QPen pen;
|
|
|
|
pen.setColor(Qt::red);
|
|
|
|
const qreal underlineOffset = styleOption.fontMetrics.underlinePos();
|
|
|
|
const QPixmap wave = getWavyPixmap(qMax(underlineOffset, pen.widthF()), pen);
|
|
|
|
const int descent = styleOption.fontMetrics.descent();
|
|
|
|
|
|
|
|
painter->setBrushOrigin(painter->brushOrigin().x(), 0);
|
|
|
|
painter->fillRect(textFrame.x(), 0, qCeil(textFrame.width()), qMin(wave.height(), descent), wave);
|
2014-05-06 12:44:44 +02:00
|
|
|
}
|
|
|
|
|
2020-10-22 09:42:36 +02:00
|
|
|
static void setId(const QModelIndex &index, const QString &newId)
|
|
|
|
{
|
|
|
|
ModelNode modelNode = getModelNode(index);
|
|
|
|
|
|
|
|
if (!modelNode.isValid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (modelNode.id() == newId)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!modelNode.isValidId(newId)) {
|
|
|
|
Core::AsynchronousMessageBox::warning(NavigatorTreeView::tr("Invalid Id"),
|
|
|
|
NavigatorTreeView::tr("%1 is an invalid id.").arg(newId));
|
|
|
|
} else if (modelNode.view()->hasId(newId)) {
|
|
|
|
Core::AsynchronousMessageBox::warning(NavigatorTreeView::tr("Invalid Id"),
|
|
|
|
NavigatorTreeView::tr("%1 already exists.").arg(newId));
|
|
|
|
} else {
|
|
|
|
modelNode.setIdWithRefactoring(newId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void openContextMenu(const QModelIndex &index, const QPoint &pos)
|
|
|
|
{
|
|
|
|
const ModelNode modelNode = getModelNode(index);
|
|
|
|
QTC_ASSERT(modelNode.isValid(), return);
|
|
|
|
ModelNodeContextMenu::showContextMenu(modelNode.view(), pos, QPoint(), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize NameItemDelegate::sizeHint(const QStyleOptionViewItem & /*option*/,
|
|
|
|
const QModelIndex & /*modelIndex*/) const
|
|
|
|
{
|
|
|
|
return {15, 20 + (2 * delegateMargin)};
|
|
|
|
}
|
|
|
|
|
2014-05-06 12:44:44 +02:00
|
|
|
void NameItemDelegate::paint(QPainter *painter,
|
|
|
|
const QStyleOptionViewItem &styleOption,
|
|
|
|
const QModelIndex &modelIndex) const
|
|
|
|
{
|
|
|
|
painter->save();
|
2020-10-22 09:42:36 +02:00
|
|
|
|
|
|
|
if (styleOption.state & QStyle::State_MouseOver && !isThisOrAncestorLocked(modelIndex))
|
|
|
|
painter->fillRect(styleOption.rect.adjusted(0, delegateMargin, 0, -delegateMargin),
|
|
|
|
Theme::getColor(Theme::Color::DSsliderHandle));
|
|
|
|
|
2014-05-06 12:44:44 +02:00
|
|
|
if (styleOption.state & QStyle::State_Selected)
|
2014-07-07 15:14:53 +02:00
|
|
|
NavigatorTreeView::drawSelectionBackground(painter, styleOption);
|
2014-05-06 12:44:44 +02:00
|
|
|
|
2020-10-22 09:42:36 +02:00
|
|
|
iconOffset = drawIcon(painter, styleOption, modelIndex);
|
2014-05-06 12:44:44 +02:00
|
|
|
|
2020-10-22 09:42:36 +02:00
|
|
|
if (isThisOrAncestorLocked(modelIndex))
|
2017-05-05 14:34:21 +02:00
|
|
|
painter->setOpacity(0.5);
|
|
|
|
|
2015-07-15 17:38:27 +02:00
|
|
|
QRect textFrame = drawText(painter, styleOption, modelIndex, iconOffset);
|
2014-05-06 12:44:44 +02:00
|
|
|
|
2017-05-11 13:31:55 +02:00
|
|
|
if (QmlObjectNode(getModelNode(modelIndex)).hasError())
|
2015-07-15 17:38:27 +02:00
|
|
|
drawRedWavyUnderLine(painter, styleOption, textFrame);
|
2014-05-05 14:31:54 +02:00
|
|
|
|
|
|
|
painter->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
QWidget *NameItemDelegate::createEditor(QWidget *parent,
|
2020-10-22 09:42:36 +02:00
|
|
|
const QStyleOptionViewItem &/*option*/,
|
2014-05-05 14:31:54 +02:00
|
|
|
const QModelIndex &index) const
|
|
|
|
{
|
2017-05-11 13:06:10 +02:00
|
|
|
if (!getModelNode(index).isValid())
|
2018-07-24 23:56:45 +02:00
|
|
|
return nullptr;
|
2014-05-05 14:31:54 +02:00
|
|
|
|
|
|
|
return new QLineEdit(parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NameItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
|
|
|
|
{
|
2017-05-11 13:06:10 +02:00
|
|
|
const ModelNode node = getModelNode(index);
|
2017-05-05 14:34:21 +02:00
|
|
|
const QString value = node.id();
|
2014-05-05 14:31:54 +02:00
|
|
|
|
2018-07-24 23:56:45 +02:00
|
|
|
auto lineEdit = static_cast<QLineEdit*>(editor);
|
2014-05-05 14:31:54 +02:00
|
|
|
lineEdit->setText(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NameItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
|
|
|
|
{
|
2019-07-23 10:58:00 +02:00
|
|
|
Q_UNUSED(model)
|
2018-07-24 23:56:45 +02:00
|
|
|
auto lineEdit = static_cast<QLineEdit*>(editor);
|
2020-10-22 09:42:36 +02:00
|
|
|
setId(index, lineEdit->text());
|
2014-05-05 14:31:54 +02:00
|
|
|
lineEdit->clearFocus();
|
|
|
|
}
|
|
|
|
|
2020-10-22 09:42:36 +02:00
|
|
|
bool NameItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *, const QStyleOptionViewItem &, const QModelIndex &index)
|
|
|
|
{
|
|
|
|
if (event->type() == QEvent::MouseButtonRelease) {
|
|
|
|
auto mouseEvent = static_cast<QMouseEvent *>(event);
|
|
|
|
if (mouseEvent->button() == Qt::RightButton) {
|
|
|
|
openContextMenu(index, mouseEvent->globalPos());
|
|
|
|
mouseEvent->accept();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-05 14:31:54 +02:00
|
|
|
void NameItemDelegate::updateEditorGeometry(QWidget *editor,
|
|
|
|
const QStyleOptionViewItem &option,
|
2020-10-22 09:42:36 +02:00
|
|
|
const QModelIndex &/*index*/) const
|
2014-05-05 14:31:54 +02:00
|
|
|
{
|
2018-07-24 23:56:45 +02:00
|
|
|
auto lineEdit = static_cast<QLineEdit*>(editor);
|
2020-10-22 09:42:36 +02:00
|
|
|
lineEdit->setTextMargins(0, 0, 0, 2);
|
|
|
|
// + 2 is shifting the QLineEdit to the left so it will align with the text when activated
|
|
|
|
lineEdit->setGeometry(option.rect.adjusted(iconOffset + 2, delegateMargin, 0, -delegateMargin));
|
2014-05-05 14:31:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace QmlDesigner
|