Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save drawn shape on submit in single shape mode #8807

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from

Conversation

klakhov
Copy link
Contributor

@klakhov klakhov commented Dec 10, 2024

Motivation and context

Resolved #8713

It would make sense to save the currently drawn shape (e.g., points, polygon, polyline) when clicking Submit on the last frame. On non-last frames, we click Done to finish drawing, but on the last frame, we need to click Done and then Submit. If Done is not clicked, the shape is lost, which is inconvenient for users.

How has this been tested?

Checklist

  • I submit my changes into the develop branch
  • I have created a changelog fragment
  • [ ] I have updated the documentation accordingly
  • [ ] I have added tests to cover my changes
  • I have linked related issues (see GitHub docs)
  • I have increased versions of npm packages if it is necessary
    (cvat-canvas,
    cvat-core,
    cvat-data and
    cvat-ui)

License

  • I submit my code changes under the same MIT License that covers the project.
    Feel free to contact the maintainers if that's a concern.

Summary by CodeRabbit

  • New Features

    • Enhanced drawing functionality in the sidebar for more flexible shape annotation.
    • Added a "Done" button that dynamically appears based on current drawing state.
  • Bug Fixes

    • Streamlined logic for determining drawing completion and button visibility.
  • Chores

    • Introduced utility functions to improve drawing operations and interaction handling.

@klakhov klakhov requested a review from bsekachev as a code owner December 10, 2024 08:52
@klakhov klakhov added the ui/ux label Dec 10, 2024
Copy link
Contributor

coderabbitai bot commented Dec 10, 2024

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

The changes in this pull request enhance the drawing functionality within the SingleShapeSidebar component and related areas of the CVAT UI. Two new utility functions, finishDraw and finishDrawAvailable, are introduced to streamline the process of determining when drawing can be completed and to finalize drawing actions. The finishOnThisFrame callback is modified to utilize these functions, improving the control flow for handling user interactions in the annotation process.

Changes

File Path Change Summary
cvat-ui/src/components/annotation-page/single-shape-workspace/single-shape-sidebar/single-shape-sidebar.tsx Integrated finishDraw and finishDrawAvailable into finishOnThisFrame for enhanced drawing logic.
cvat-ui/src/components/annotation-page/top-bar/left-group.tsx Replaced hardcoded array check for "Done" button visibility with a call to finishDrawAvailable.
cvat-ui/src/containers/annotation-page/top-bar/top-bar.tsx Simplified onFinishDraw method to utilize finishDraw, streamlining the drawing completion logic.
cvat-ui/src/utils/drawing.ts Added finishDraw and finishDrawAvailable functions to manage drawing operations based on active control.

Assessment against linked issues

Objective Addressed Explanation
Finish current annotation on the last frame in single shape mode on Submit click (#8713)

🐇 In the garden, I hop and play,
With new tools to brighten my day!
When the last frame calls for a finish,
I’ll save my work, and won’t diminish!
With a click of Submit, my task is done,
Oh, how I love to draw and run! 🌼✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (1)
cvat-ui/src/utils/drawing.ts (1)

19-29: Consider adding JSDoc comments for better documentation.

While the function implementation is clean and handles different canvas types well, adding JSDoc comments would improve maintainability by documenting the expected behavior for different control states.

+/**
+ * Finishes the current drawing operation on the canvas
+ * @param canvas - The canvas instance (2D or 3D)
+ * @param activeControl - The current active control
+ */
 export function finishDraw(canvas: Canvas | Canvas3d, activeControl: ActiveControl): void {
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 094c62d and 2530973.

📒 Files selected for processing (4)
  • cvat-ui/src/components/annotation-page/single-shape-workspace/single-shape-sidebar/single-shape-sidebar.tsx (2 hunks)
  • cvat-ui/src/components/annotation-page/top-bar/left-group.tsx (2 hunks)
  • cvat-ui/src/containers/annotation-page/top-bar/top-bar.tsx (2 hunks)
  • cvat-ui/src/utils/drawing.ts (1 hunks)
🔇 Additional comments (6)
cvat-ui/src/utils/drawing.ts (1)

9-17: LGTM! Well-structured utility function.

The function provides a clear and maintainable way to check drawing control states. The comprehensive list of drawing controls makes it easy to extend in the future.

cvat-ui/src/components/annotation-page/top-bar/left-group.tsx (1)

82-82: LGTM! Good use of the utility function.

The change improves code maintainability by centralizing the drawing control logic in a dedicated utility.

cvat-ui/src/containers/annotation-page/top-bar/top-bar.tsx (2)

550-550: LGTM! Good use of the utility function.

The change improves code maintainability by moving the canvas interaction logic to a dedicated utility.


Line range hint 19-29: Verify implementation of shape saving on submit.

While the utility functions are well-implemented, I don't see direct implementation of saving shapes on submit in single shape mode. This might be implemented in other files not included in the review.

cvat-ui/src/components/annotation-page/single-shape-workspace/single-shape-sidebar/single-shape-sidebar.tsx (2)

40-40: LGTM: Clean import of drawing utilities

The new utility functions are imported cleanly and follow the existing import style.


299-304: LGTM: Proper implementation of shape saving on submit

The implementation correctly handles the shape saving process by:

  1. Checking if drawing can be finished using finishDrawAvailable
  2. Only attempting to finish drawing when available
  3. Using the canvas instance from the store
  4. Maintaining the existing control flow

This change successfully addresses the issue of shapes not being saved when clicking submit.

Let's verify the integration with the drawing utilities:

✅ Verification successful

Implementation correctly integrates with drawing utilities and maintains expected behavior

The verification confirms that:

  • finishDrawAvailable correctly checks for supported drawing controls (POLYGON, POLYLINE, POINTS, AI_TOOLS, OPENCV_TOOLS)
  • finishDraw properly handles both standard drawing and AI/OpenCV tools
  • The functions are imported and used consistently across the codebase
  • Canvas draw calls are properly managed without conflicts
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the implementation of finishDraw and finishDrawAvailable
# Expected: These functions should be defined in utils/drawing and handle all shape types

# Check the implementation of the drawing utilities
ast-grep --pattern 'export function finishDrawAvailable($_) {
  $$$
}'

# Check for any direct canvas.draw calls that might conflict
rg -A 2 'canvas\.draw\(' 

Length of output: 1198


Script:

#!/bin/bash
# Let's check the implementation of both functions in the drawing utilities
rg -A 10 "export function finishDrawAvailable|export function finishDraw" cvat-ui/src/utils/drawing.ts

# Also check where these functions are imported and used
rg "import.*finishDraw.*from.*drawing" 

Length of output: 1173

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (3)
cvat-ui/src/utils/drawing.ts (1)

19-29: Consider adding JSDoc comments.

The function handles different canvas types and controls well, but could benefit from documentation explaining the behavior difference between AI/OpenCV tools and other drawing modes.

+/**
+ * Finishes the current drawing operation based on the active control type.
+ * For AI/OpenCV tools, disables interaction mode.
+ * For other drawing controls, disables drawing mode.
+ * @param canvas - The canvas instance (2D or 3D)
+ * @param activeControl - The currently active control
+ */
 export function finishDraw(canvas: Canvas | Canvas3d, activeControl: ActiveControl): void {
cvat-ui/src/components/annotation-page/single-shape-workspace/single-shape-sidebar/single-shape-sidebar.tsx (2)

301-302: Add null check for canvas instance

Consider adding a null check for the canvas instance to prevent potential runtime errors.

-                const canvas = store.getState().annotation.canvas.instance as Canvas;
-                finishDraw(canvas, activeControl);
+                const canvas = store.getState().annotation.canvas.instance;
+                if (canvas) {
+                    finishDraw(canvas as Canvas, activeControl);
+                }

299-304: Well-structured implementation of the save-on-submit feature

The changes follow good architectural principles:

  1. Minimal and focused modification
  2. Clear separation of concerns (drawing logic in utilities)
  3. Maintains existing component behavior
  4. Type-safe implementation

This approach makes the code maintainable and testable.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 094c62d and 2530973.

📒 Files selected for processing (4)
  • cvat-ui/src/components/annotation-page/single-shape-workspace/single-shape-sidebar/single-shape-sidebar.tsx (2 hunks)
  • cvat-ui/src/components/annotation-page/top-bar/left-group.tsx (2 hunks)
  • cvat-ui/src/containers/annotation-page/top-bar/top-bar.tsx (2 hunks)
  • cvat-ui/src/utils/drawing.ts (1 hunks)
🔇 Additional comments (5)
cvat-ui/src/utils/drawing.ts (1)

9-17: LGTM! Well-structured utility function.

The function provides a clear and maintainable way to check if drawing can be finished for a given control.

cvat-ui/src/components/annotation-page/top-bar/left-group.tsx (1)

21-21: LGTM! Good refactoring.

Moving the control check logic to a utility function improves code maintainability and reusability.

Also applies to: 82-82

cvat-ui/src/containers/annotation-page/top-bar/top-bar.tsx (1)

44-44: LGTM! Good simplification.

Utilizing the utility function reduces code duplication and improves maintainability by centralizing the drawing completion logic.

Also applies to: 550-550

cvat-ui/src/components/annotation-page/single-shape-workspace/single-shape-sidebar/single-shape-sidebar.tsx (2)

40-40: LGTM: Clean import of new utility functions

The new utility functions are properly imported and organized with the existing imports.


299-304: LGTM: Drawing completion logic properly integrated

The new code block correctly implements the feature to save drawn shapes on submit by checking if drawing can be finished and completing it before saving.

@klakhov klakhov requested a review from nmanovic as a code owner December 10, 2024 09:40
@codecov-commenter
Copy link

codecov-commenter commented Dec 10, 2024

Codecov Report

Attention: Patch coverage is 93.75000% with 1 line in your changes missing coverage. Please review.

Project coverage is 73.88%. Comparing base (12f886c) to head (29d58d4).

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #8807   +/-   ##
========================================
  Coverage    73.88%   73.88%           
========================================
  Files          408      409    +1     
  Lines        44104    44115   +11     
  Branches      3986     3991    +5     
========================================
+ Hits         32586    32595    +9     
- Misses       11518    11520    +2     
Components Coverage Δ
cvat-ui 78.35% <93.75%> (+<0.01%) ⬆️
cvat-server 70.08% <ø> (ø)

@bsekachev
Copy link
Member

bsekachev commented Dec 12, 2024

The patch does not seem to work.

First activeControl is not dependency of finishOnThisFrame. So, the callback probably uses outdated value.
Secondly, cancelCurrentCanvasOp is called before from another hook (probably because annotations array gets updated)

@bsekachev
Copy link
Member

demo

@klakhov
Copy link
Contributor Author

klakhov commented Dec 20, 2024

I’ve fixed the issue mentioned above. The problem was with the activeControl dependency, and activeShapeType was incorrectly determined based on the label type in the global state. If the label type is any, activeShapeType should correspond to the current label type selected.

@zhiltsov-max Could you check if fix works for you?

@zhiltsov-max
Copy link
Contributor

zhiltsov-max commented Dec 24, 2024

Looks ok, but it probably works incorrectly if there is predefined number of points used. If set to > 1, the shape is saved automatically even with less number of points. I think maybe it will be better to disable skip/submit if there are not enough points placed. Actually, it seems to be a bug in the implementation of predefined number of points - the shape can be finished by pressing N before all the required points are placed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Finish current annotation on the last frame in single shape mode on Submit click
4 participants