Skip to content

Commit

Permalink
Delete empty annotations automatically
Browse files Browse the repository at this point in the history
After switching away from having one selected, since people often
accidentally create empty annotations and leave them strewn around. If
the user wanted to keep the annotation, they can hit undo and to make it
come back.

Implements #1351.
  • Loading branch information
askmeaboutlo0m committed Oct 24, 2024
1 parent b1254ac commit 7feff19
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/libclient/canvas/paintengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,19 @@ uint16_t PaintEngine::findAvailableAnnotationId(uint8_t forUser) const
return 0;
}

drawdance::Annotation PaintEngine::getAnnotationById(int annotationId) const
{
drawdance::AnnotationList annotations = viewCanvasState().annotations();
int count = annotations.count();
for(int i = 0; i < count; ++i) {
drawdance::Annotation annotation = annotations.at(i);
if(annotation.id() == annotationId) {
return annotation;
}
}
return drawdance::Annotation::null();
}

drawdance::Annotation
PaintEngine::getAnnotationAt(int x, int y, int expand) const
{
Expand Down
3 changes: 3 additions & 0 deletions src/libclient/canvas/paintengine.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ class PaintEngine final : public QObject {
//! Find an unused ID for a new annotation
uint16_t findAvailableAnnotationId(uint8_t forUser) const;

//! Get the annotation at the given point
drawdance::Annotation getAnnotationById(int annotationId) const;

//! Get the annotation at the given point
drawdance::Annotation getAnnotationAt(int x, int y, int expand) const;

Expand Down
14 changes: 14 additions & 0 deletions src/libclient/document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ Document::Document(
connect(
this, &Document::buildStreamResetImageFinished, this,
&Document::startSendingStreamResetSnapshot, Qt::QueuedConnection);

connect(
m_toolctrl, &tools::ToolController::deleteAnnotationRequested, this,
&Document::deleteAnnotation);
}

void Document::initCanvas()
Expand Down Expand Up @@ -1627,6 +1631,16 @@ void Document::fillArea(const QColor &color, DP_BlendMode mode, float opacity)
m_messageBuffer.clear();
}

void Document::deleteAnnotation(int annotationId)
{
unsigned int contextId = m_client->myId();
net::Message msgs[] = {
net::makeUndoPointMessage(contextId),
net::makeAnnotationDeleteMessage(contextId, annotationId),
};
m_client->sendMessages(2, msgs);
}

void Document::removeEmptyAnnotations()
{
if(!m_canvas) {
Expand Down
1 change: 1 addition & 0 deletions src/libclient/document.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ public slots:
void copyLayer();
void cutLayer();

void deleteAnnotation(int annotationId);
void removeEmptyAnnotations();
void clearArea();
void fillArea(const QColor &color, DP_BlendMode mode, float opacity);
Expand Down
8 changes: 8 additions & 0 deletions src/libclient/tools/toolcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ void ToolController::setActiveTool(Tool::Type tool)
void ToolController::setActiveAnnotation(uint16_t id)
{
if(m_activeAnnotation != id) {
if(m_activeAnnotation != 0) {
drawdance::Annotation a =
m_model->paintEngine()->getAnnotationById(m_activeAnnotation);
if(!a.isNull() && a.textBytes().isEmpty()) {
emit deleteAnnotationRequested(a.id());
}
}

m_activeAnnotation = id;
emit activeAnnotationChanged(id);
}
Expand Down
1 change: 1 addition & 0 deletions src/libclient/tools/toolcontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ public slots:
void toolSwitchRequested(tools::Tool::Type tool);
void showMessageRequested(const QString &message);
void toolNoticeRequested(const QString &text);
void deleteAnnotationRequested(int annotationId);

void floodFillStateChanged(bool running, bool pending);
void toolStateChanged(int state);
Expand Down

0 comments on commit 7feff19

Please sign in to comment.