Skip to content

Commit

Permalink
- Added measurement tool to ASAP
Browse files Browse the repository at this point in the history
  • Loading branch information
GeertLitjens committed Oct 14, 2016
1 parent c238915 commit 716a450
Show file tree
Hide file tree
Showing 12 changed files with 366 additions and 21 deletions.
34 changes: 19 additions & 15 deletions ASAP/annotation/AnnotationWorkstationExtensionPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "AnnotationToMask.h"
#include "DotQtAnnotation.h"
#include "PolyQtAnnotation.h"
#include "MeasurementQtAnnotation.h"
#include "MeasurementAnnotationTool.h"
#include "PointSetQtAnnotation.h"
#include "io/multiresolutionimageinterface/MultiResolutionImage.h"
#include "../PathologyViewer.h"
Expand Down Expand Up @@ -101,20 +103,8 @@ void AnnotationWorkstationExtensionPlugin::onClearButtonPressed() {

void AnnotationWorkstationExtensionPlugin::clear() {
if (_generatedAnnotation) {
PolyQtAnnotation* tmp = dynamic_cast<PolyQtAnnotation*>(_generatedAnnotation);
if (tmp) {
if (tmp->getInterpolationType() == "spline") {
std::dynamic_pointer_cast<SplineAnnotationTool>(_annotationTools[2])->cancelAnnotation();
}
else {
std::dynamic_pointer_cast<PolyAnnotationTool>(_annotationTools[1])->cancelAnnotation();
}
}
else {
PointSetQtAnnotation* tmp2 = dynamic_cast<PointSetQtAnnotation*>(_generatedAnnotation);
if (tmp2) {
std::dynamic_pointer_cast<PointSetAnnotationTool>(_annotationTools[3])->cancelAnnotation();
}
for (std::vector<std::shared_ptr<ToolPluginInterface> >::iterator it = _annotationTools.begin(); it != _annotationTools.end(); ++it) {
std::dynamic_pointer_cast<AnnotationTool>((*it))->cancelAnnotation();
}
}
_treeWidget->clearSelection();
Expand Down Expand Up @@ -407,6 +397,9 @@ void AnnotationWorkstationExtensionPlugin::onLoadButtonPressed(const std::string
annot = new PolyQtAnnotation((*it), this, _viewer->getSceneScale());
dynamic_cast<PolyQtAnnotation*>(annot)->setInterpolationType("spline");
}
else if ((*it)->getType() == Annotation::Type::MEASUREMENT) {
annot = new MeasurementQtAnnotation((*it), this, _viewer->getSceneScale());
}
else if ((*it)->getType() == Annotation::Type::POINTSET) {
annot = new PointSetQtAnnotation((*it), this, _viewer->getSceneScale());
}
Expand Down Expand Up @@ -636,6 +629,7 @@ QDockWidget* AnnotationWorkstationExtensionPlugin::getDockWidget() {
}

void AnnotationWorkstationExtensionPlugin::onNewImageLoaded(std::weak_ptr<MultiResolutionImage> img, std::string fileName) {
_img = img;
if (_dockWidget) {
_dockWidget->setEnabled(true);
}
Expand All @@ -646,7 +640,6 @@ void AnnotationWorkstationExtensionPlugin::onNewImageLoaded(std::weak_ptr<MultiR
onLoadButtonPressed(annotationPath);
}
}
_img = img;
if (std::shared_ptr<MultiResolutionImage> local_img = _img.lock()) {
std::vector<double> spacing = local_img->getSpacing();
if (spacing.size() > 1) {
Expand All @@ -658,6 +651,10 @@ void AnnotationWorkstationExtensionPlugin::onNewImageLoaded(std::weak_ptr<MultiR
}
}

std::weak_ptr<MultiResolutionImage> AnnotationWorkstationExtensionPlugin::getCurrentImage() {
return _img;
}

void AnnotationWorkstationExtensionPlugin::onImageClosed() {
clear();
if (_dockWidget) {
Expand All @@ -675,6 +672,8 @@ bool AnnotationWorkstationExtensionPlugin::initialize(PathologyViewer* viewer) {
_annotationTools.push_back(tool);
tool.reset(new PointSetAnnotationTool(this, viewer));
_annotationTools.push_back(tool);
tool.reset(new MeasurementAnnotationTool(this, viewer));
_annotationTools.push_back(tool);
_annotationService.reset(new AnnotationService());
return true;
}
Expand Down Expand Up @@ -710,6 +709,11 @@ void AnnotationWorkstationExtensionPlugin::startAnnotation(float x, float y, con
PointSetQtAnnotation* temp = new PointSetQtAnnotation(annot, this, _viewer->getSceneScale());
_generatedAnnotation = temp;
}
else if (type == "measurementannotation") {
annot->setType(Annotation::Type::MEASUREMENT);
MeasurementQtAnnotation* temp = new MeasurementQtAnnotation(annot, this, _viewer->getSceneScale());
_generatedAnnotation = temp;
}
else {
return;
}
Expand Down
1 change: 1 addition & 0 deletions ASAP/annotation/AnnotationWorkstationExtensionPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public :
bool eventFilter(QObject* watched, QEvent* event);
void deleteAnnotation(QtAnnotation* annotation);
void deleteAnnotationGroup(QtAnnotationGroup* group);
std::weak_ptr<MultiResolutionImage> getCurrentImage();
void clearSelection();
bool canClose();

Expand Down
6 changes: 5 additions & 1 deletion ASAP/annotation/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PROJECT(AnnotationPlugin)
INCLUDE_DIRECTORIES(
${Annotation_SOURCE_DIR}
${ANNOTATION_SOURCE_DIR}
${Boost_INCLUDE_DIRS}
${PugiXML_INCLUDE_DIR}
${DIAGPathology_BINARY_DIR}
Expand All @@ -20,10 +20,12 @@ SET(ANNOTATION_PLUGIN_HEADERS
AnnotationTool.h
DotAnnotationTool.h
PolyAnnotationTool.h
MeasurementAnnotationTool.h
PointSetAnnotationTool.h
SplineAnnotationTool.h
QtAnnotation.h
QtAnnotationGroup.h
MeasurementQtAnnotation.h
PolyQtAnnotation.h
PointSetQtAnnotation.h
DotQtAnnotation.h
Expand All @@ -32,13 +34,15 @@ SET(ANNOTATION_PLUGIN_HEADERS
SET(ANNOTATION_PLUGIN_SOURCE
AnnotationTool.cpp
AnnotationWorkstationExtensionPlugin.cpp
MeasurementAnnotationTool.cpp
DotAnnotationTool.cpp
PolyAnnotationTool.cpp
PointSetAnnotationTool.cpp
SplineAnnotationTool.cpp
QtAnnotation.cpp
QtAnnotationGroup.cpp
PolyQtAnnotation.cpp
MeasurementQtAnnotation.cpp
PointSetQtAnnotation.cpp
DotQtAnnotation.cpp
)
Expand Down
144 changes: 144 additions & 0 deletions ASAP/annotation/MeasurementAnnotationTool.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
#include "MeasurementAnnotationTool.h"
#include "QtAnnotation.h"
#include "AnnotationWorkstationExtensionPlugin.h"
#include <QAction>
#include <QPen>
#include <QGraphicsLineItem>
#include <QGraphicsItem>
#include "PolyQtAnnotation.h"
#include "Annotation.h"
#include "../PathologyViewer.h"
#include <math.h>
#include <numeric>
#include <iostream>
#include <QTimeLine>
#include "io/multiresolutionimageinterface/MultiResolutionImage.h"

MeasurementAnnotationTool::QGraphicsTextItemWithBackground::QGraphicsTextItemWithBackground(const QString &text) :
QGraphicsTextItem(text) { }

QRectF MeasurementAnnotationTool::QGraphicsTextItemWithBackground::boundingRect() const {
return QGraphicsTextItem::boundingRect().adjusted(-5, -5, 5, 5);
}

void MeasurementAnnotationTool::QGraphicsTextItemWithBackground::paint(QPainter *painter, const QStyleOptionGraphicsItem *o, QWidget *w) {
painter->setPen(Qt::NoPen);
painter->setBrush(QBrush(QColor(0, 0, 0, 75)));
painter->drawRect(boundingRect().adjusted(-5, -5, 5, 5));
painter->setBrush(QBrush(Qt::white));
QGraphicsTextItem::paint(painter, o, w);
}

MeasurementAnnotationTool::MeasurementAnnotationTool(AnnotationWorkstationExtensionPlugin* annotationPlugin, PathologyViewer* viewer) :
AnnotationTool(annotationPlugin, viewer),
_activeLine(NULL),
_sizeText(NULL)
{
}

void MeasurementAnnotationTool::mouseMoveEvent(QMouseEvent *event) {
if (_viewer) {
if (_generating) {
if (!_activeLine) {
_activeLine = new QGraphicsLineItem();
_activeLine->setZValue(std::numeric_limits<float>::max());
_viewer->scene()->addItem(_activeLine);
}
if (!_sizeText) {
_sizeText = new QGraphicsTextItemWithBackground("");
_sizeText->setZValue(std::numeric_limits<float>::max());
_sizeText->setFlag(QGraphicsItem::ItemIgnoresTransformations);
_sizeText->setDefaultTextColor(Qt::white);
_viewer->scene()->addItem(_sizeText);
}
QPointF scenePos = _viewer->mapToScene(event->pos());
if (std::shared_ptr<MultiResolutionImage> local_img = _annotationPlugin->getCurrentImage().lock()) {
std::vector<double> spacing = local_img->getSpacing();
float sizeInPixels = sqrt(pow(scenePos.x() / _viewer->getSceneScale() - _last.getX() / _viewer->getSceneScale(), 2) + pow(scenePos.y() / _viewer->getSceneScale() - _last.getY() / _viewer->getSceneScale(), 2));
if (spacing.size() > 0) {
float sizeInUnits = sizeInPixels * spacing[0];
QString unit = " um";
if (sizeInUnits > 1000) {
sizeInUnits /= 1000;
unit = " mm";
}
_sizeText->setPlainText(QString::number(sizeInUnits, 'g', 4) + unit);
}
else {
_sizeText->setPlainText(QString::number(sizeInPixels) + QString(" pixels"));
}
_sizeText->setPos(scenePos.x() + 20 / _viewer->transform().m11(), scenePos.y() + 20 / _viewer->transform().m11());
}
_activeLine->setPen(QPen(QBrush(Qt::black), 3. / _viewer->transform().m11()));
_activeLine->setLine(_last.getX(), _last.getY(), scenePos.x(), scenePos.y());
}
AnnotationTool::mouseMoveEvent(event);
event->accept();
}
}

void MeasurementAnnotationTool::keyPressEvent(QKeyEvent *event) {
AnnotationTool::keyPressEvent(event);
if (event->key() == Qt::Key::Key_Delete) {
if (!_generating && _annotationPlugin->getActiveAnnotation()) {
if (_annotationPlugin->getActiveAnnotation()->getAnnotation()->getCoordinates().size() <= 2) {
_annotationPlugin->deleteAnnotation(_annotationPlugin->getActiveAnnotation());
}
}
}
}

void MeasurementAnnotationTool::cancelAnnotation() {
if (_generating) {
AnnotationTool::cancelAnnotation();
if (_activeLine) {
_activeLine->hide();
_viewer->scene()->removeItem(_activeLine);
delete _activeLine;
_activeLine = NULL;
}
if (_sizeText) {
_sizeText->hide();
_viewer->scene()->removeItem(_sizeText);
delete _sizeText;
_sizeText = NULL;
}
}
}

void MeasurementAnnotationTool::addCoordinate(const QPointF& scenePos) {
if (_annotationPlugin->getGeneratedAnnotation()->getAnnotation()->getCoordinates().size() > 0) {
_annotationPlugin->getGeneratedAnnotation()->addCoordinate(scenePos.x() / _viewer->getSceneScale(), scenePos.y() / _viewer->getSceneScale());
_annotationPlugin->finishAnnotation();
if (_activeLine) {
_viewer->scene()->removeItem(_activeLine);
delete _activeLine;
_activeLine = NULL;
}
if (_sizeText) {
_viewer->scene()->removeItem(_sizeText);
delete _sizeText;
_sizeText = NULL;
}
_start = Point(-1, -1);
_last = _start;
_generating = false;
}
else {
_annotationPlugin->getGeneratedAnnotation()->addCoordinate(scenePos.x() / _viewer->getSceneScale(), scenePos.y() / _viewer->getSceneScale());
_last = Point(scenePos.x(), scenePos.y());
}
}

QAction* MeasurementAnnotationTool::getToolButton() {
if (!_button) {
_button = new QAction("MeasurementAnnotation", this);
_button->setObjectName(QString::fromStdString(name()));
_button->setIcon(QIcon(QPixmap(":/AnnotationWorkstationExtensionPlugin_icons/measure.png")));
}
return _button;
}

std::string MeasurementAnnotationTool::name() {
return std::string("measurementannotation");
}
39 changes: 39 additions & 0 deletions ASAP/annotation/MeasurementAnnotationTool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef MEASUREMENTANNOTATIONTOOL_H
#define MEASUREMENTANNOTATIONTOOL_H

#include "AnnotationTool.h"
#include "core/Point.h"
#include <QGraphicsTextItem>

class AnnotationWorkstationExtensionPlugin;
class PathologyViewer;
class QGraphicsLineItem;

class ANNOTATIONPLUGIN_EXPORT MeasurementAnnotationTool : public AnnotationTool {
Q_OBJECT

public :
MeasurementAnnotationTool(AnnotationWorkstationExtensionPlugin* annotationPlugin, PathologyViewer* viewer);
virtual std::string name();
virtual QAction* getToolButton();
void mouseMoveEvent(QMouseEvent *event);
void keyPressEvent(QKeyEvent *event);
void cancelAnnotation();

private:

class QGraphicsTextItemWithBackground : public QGraphicsTextItem
{
public:
QGraphicsTextItemWithBackground(const QString &text);
QRectF boundingRect() const;

void paint(QPainter *painter, const QStyleOptionGraphicsItem *o, QWidget *w);
};

void addCoordinate(const QPointF& scenePos);
QGraphicsLineItem* _activeLine;
QGraphicsTextItemWithBackground* _sizeText;
};

#endif
Loading

0 comments on commit 716a450

Please sign in to comment.