QDS Annotations Preview fixes

- Performance issues fix
 - Fixes bug with writing comment into wrong column
 - Fix for very wide columns

Task: QDS-1916
Change-Id: I1f0648fa16cf7dbff9c077cb71382a89be6f86ce
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
Aleksei German
2020-04-07 14:10:25 +02:00
parent 965250bae0
commit 3a96c2b3dc
2 changed files with 78 additions and 34 deletions

View File

@@ -31,6 +31,7 @@
#include <QAction> #include <QAction>
#include <QMenu> #include <QMenu>
#include <QMessageBox> #include <QMessageBox>
#include <QTextDocument>
#include <coreplugin/icore.h> #include <coreplugin/icore.h>
#include <utils/theme/theme.h> #include <utils/theme/theme.h>
@@ -41,6 +42,8 @@
namespace QmlDesigner { namespace QmlDesigner {
const int penWidth = 2;
FormEditorAnnotationIcon::FormEditorAnnotationIcon(const ModelNode &modelNode, QGraphicsItem *parent) FormEditorAnnotationIcon::FormEditorAnnotationIcon(const ModelNode &modelNode, QGraphicsItem *parent)
: QGraphicsObject(parent) : QGraphicsObject(parent)
, m_modelNode(modelNode) , m_modelNode(modelNode)
@@ -64,7 +67,7 @@ FormEditorAnnotationIcon::FormEditorAnnotationIcon(const ModelNode &modelNode, Q
if (scene) { if (scene) {
m_readerIsActive = scene->annotationVisibility(); m_readerIsActive = scene->annotationVisibility();
if (m_readerIsActive) { if (m_readerIsActive) {
drawReader(); createReader();
} }
} }
@@ -106,10 +109,12 @@ void FormEditorAnnotationIcon::paint(QPainter *painter, const QStyleOptionGraphi
m_annotation = m_modelNode.annotation(); m_annotation = m_modelNode.annotation();
if (m_readerIsActive) if (m_readerIsActive)
resetReader(); drawReader();
else
hideReader();
} }
else { else {
hideReader(); removeReader();
} }
setEnabled(hasAuxData); setEnabled(hasAuxData);
@@ -145,7 +150,7 @@ void FormEditorAnnotationIcon::setActive(bool readerStatus)
if (m_readerIsActive) if (m_readerIsActive)
resetReader(); resetReader();
else else
hideReader(); removeReader();
update(); update();
} }
@@ -176,10 +181,10 @@ void FormEditorAnnotationIcon::mousePressEvent(QGraphicsSceneMouseEvent * event)
if (button == Qt::LeftButton) { if (button == Qt::LeftButton) {
if (m_readerIsActive) { if (m_readerIsActive) {
hideReader(); removeReader();
m_readerIsActive = false; m_readerIsActive = false;
} else { } else {
drawReader(); resetReader();
m_readerIsActive = true; m_readerIsActive = true;
} }
} }
@@ -211,13 +216,40 @@ void FormEditorAnnotationIcon::contextMenuEvent(QGraphicsSceneContextMenuEvent *
event->accept(); event->accept();
} }
void FormEditorAnnotationIcon::resetReader() void FormEditorAnnotationIcon::drawReader()
{
if (!childItems().isEmpty()) {
for (QGraphicsItem *item : childItems()) {
item->show();
}
}
else {
createReader();
}
}
void FormEditorAnnotationIcon::hideReader()
{
if (!childItems().isEmpty()) {
for (QGraphicsItem *item : childItems()) {
item->hide();
}
}
}
void FormEditorAnnotationIcon::quickResetReader()
{ {
hideReader(); hideReader();
drawReader(); drawReader();
} }
void FormEditorAnnotationIcon::drawReader() void FormEditorAnnotationIcon::resetReader()
{
removeReader();
createReader();
}
void FormEditorAnnotationIcon::createReader()
{ {
const qreal width = 290; const qreal width = 290;
const qreal height = 200; const qreal height = 200;
@@ -239,48 +271,48 @@ void FormEditorAnnotationIcon::drawReader()
QGraphicsItem *commentBubble = createCommentBubble(commentRect, comment.title(), QGraphicsItem *commentBubble = createCommentBubble(commentRect, comment.title(),
comment.author(), comment.text(), comment.author(), comment.text(),
comment.timestampStr(), this); comment.timestampStr(), this);
commentBubble->setPos(commentPosition);
commentPosition += QPointF(width + offset, 0);
comments.push_back(commentBubble); comments.push_back(commentBubble);
} }
int currentColumn = 0;
qreal columnHeight = 0;
const qreal maxHeight = 650; const qreal maxHeight = 650;
const QPointF commentsStartPosition(cornerPosition.x(), cornerPosition.y() + titleRect.height() + (offset*2)); const QPointF commentsStartPosition(cornerPosition.x(), cornerPosition.y() + titleRect.height() + (offset*2));
QPointF newPos(commentsStartPosition); QPointF newPos(commentsStartPosition);
qreal columnHeight = commentsStartPosition.y();
for (QGraphicsItem *comment : comments) { for (QGraphicsItem *comment : comments) {
qreal itemHeight = comment->boundingRect().height();
if ((columnHeight + offset + itemHeight) > maxHeight) { comment->setPos(newPos); //first place comment in its new position, then calculate position for next comment
// have no extra space
columnHeight = 0;
++currentColumn;
newPos = commentsStartPosition + QPointF(currentColumn * (offset + width), 0); const qreal itemHeight = comment->boundingRect().height();
} else { const qreal itemWidth = comment->boundingRect().width();
//few normal comments, lets stack them
const qreal possibleHeight = columnHeight + offset + itemHeight;
qreal newX = 0;
if ((itemWidth > (width + penWidth)) || (possibleHeight > maxHeight)) {
//move coords to the new column
columnHeight = commentsStartPosition.y();
newX = newPos.x() + offset + itemWidth;
} }
else {
//move coords lower in the same column
columnHeight += itemHeight + offset; columnHeight += itemHeight + offset;
newX = newPos.x();
}
comment->setPos(newPos); newPos = { newX, columnHeight };
newPos += QPointF(0, itemHeight + offset);
} }
} }
} }
void FormEditorAnnotationIcon::hideReader() void FormEditorAnnotationIcon::removeReader()
{ {
if (!childItems().isEmpty()) if (!childItems().isEmpty())
qDeleteAll(childItems()); qDeleteAll(childItems());
} }
QGraphicsItem *FormEditorAnnotationIcon::createCommentBubble(const QRectF &rect, const QString &title, QGraphicsItem *FormEditorAnnotationIcon::createCommentBubble(QRectF rect, const QString &title,
const QString &author, const QString &text, const QString &author, const QString &text,
const QString &date, QGraphicsItem *parent) const QString &date, QGraphicsItem *parent)
{ {
@@ -313,13 +345,21 @@ QGraphicsItem *FormEditorAnnotationIcon::createCommentBubble(const QRectF &rect,
textItem->setPos(authorItem->x(), authorItem->boundingRect().height() + authorItem->y() + 5); textItem->setPos(authorItem->x(), authorItem->boundingRect().height() + authorItem->y() + 5);
textItem->update(); textItem->update();
qreal contentRect = titleItem->boundingRect().height() if (textItem->boundingRect().width() > textItem->textWidth()) {
textItem->setTextWidth(textItem->boundingRect().width());
textItem->update();
rect.setWidth(textItem->boundingRect().width());
}
const qreal contentRect = titleItem->boundingRect().height()
+ authorItem->boundingRect().height() + authorItem->boundingRect().height()
+ textItem->boundingRect().height(); + textItem->boundingRect().height();
if ((contentRect + 60) > rect.height()) { if ((contentRect + 60) > rect.height())
frameItem->setRect(rect.x(), rect.y(), rect.width(), contentRect+60); rect.setHeight(contentRect+60);
}
frameItem->setRect(rect);
QGraphicsTextItem *dateItem = new QGraphicsTextItem(frameItem); QGraphicsTextItem *dateItem = new QGraphicsTextItem(frameItem);
dateItem->setPlainText(tr("Edited: ") + date); dateItem->setPlainText(tr("Edited: ") + date);
@@ -330,7 +370,7 @@ QGraphicsItem *FormEditorAnnotationIcon::createCommentBubble(const QRectF &rect,
QPen pen; QPen pen;
pen.setCosmetic(true); pen.setCosmetic(true);
pen.setWidth(2); pen.setWidth(penWidth);
pen.setCapStyle(Qt::RoundCap); pen.setCapStyle(Qt::RoundCap);
pen.setJoinStyle(Qt::BevelJoin); pen.setJoinStyle(Qt::BevelJoin);
pen.setColor(frameColor); pen.setColor(frameColor);

View File

@@ -54,6 +54,7 @@ public:
bool isReaderActive(); bool isReaderActive();
void setActive(bool readerStatus); void setActive(bool readerStatus);
void quickResetReader();
void resetReader(); void resetReader();
protected: protected:
@@ -68,7 +69,10 @@ protected:
private: private:
void drawReader(); void drawReader();
void hideReader(); void hideReader();
QGraphicsItem *createCommentBubble(const QRectF &rect, const QString &title,
void createReader();
void removeReader();
QGraphicsItem *createCommentBubble(QRectF rect, const QString &title,
const QString &author, const QString &text, const QString &author, const QString &text,
const QString &date, QGraphicsItem *parent); const QString &date, QGraphicsItem *parent);
QGraphicsItem *createTitleBubble(const QRectF &rect, const QString &text, QGraphicsItem *parent); QGraphicsItem *createTitleBubble(const QRectF &rect, const QString &text, QGraphicsItem *parent);