1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkBlockItem.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkBlockItem.h"
// Get my new commands
#include "vtkCommand.h"
#include "vtkBrush.h"
#include "vtkContext2D.h"
#include "vtkContextDevice2D.h"
#include "vtkContextMouseEvent.h"
#include "vtkContextScene.h"
#include "vtkLogger.h"
#include "vtkObjectFactory.h"
#include "vtkPen.h"
#include "vtkStdString.h"
#include "vtkTextProperty.h"
#include "vtkVector.h"
#include "vtkVectorOperators.h"
VTK_ABI_NAMESPACE_BEGIN
namespace
{
float vtkComputePosition(int alignment, float pos, float size, int vp_size, int margin)
{
switch (alignment)
{
case vtkBlockItem::LEFT:
case vtkBlockItem::BOTTOM:
return margin;
case vtkBlockItem::RIGHT:
case vtkBlockItem::TOP:
return (vp_size - size - margin);
case vtkBlockItem::CENTER:
return (0.5 * (vp_size - size - margin));
case vtkBlockItem::CUSTOM:
default:
return pos;
}
}
}
//------------------------------------------------------------------------------
vtkStandardNewMacro(vtkBlockItem);
vtkCxxSetObjectMacro(vtkBlockItem, LabelProperties, vtkTextProperty);
//------------------------------------------------------------------------------
vtkBlockItem::vtkBlockItem()
: Dimensions{ 0, 0, 0, 0 }
, MouseOver(false)
, scalarFunction(nullptr)
, LabelProperties(vtkTextProperty::New())
, HorizontalAlignment(vtkBlockItem::CUSTOM)
, VerticalAlignment(vtkBlockItem::CUSTOM)
, AutoComputeDimensions(false)
, Padding{ 5, 5 }
, Margins{ 10, 10 }
{
this->LabelProperties->SetVerticalJustificationToCentered();
this->LabelProperties->SetJustificationToCentered();
this->LabelProperties->SetColor(0.0, 0.0, 0.0);
this->LabelProperties->SetFontSize(24);
this->Brush->SetColor(255, 0, 0);
this->MouseOverBrush->SetColor(0, 255, 0);
this->Pen->SetColor(0, 0, 0);
}
//------------------------------------------------------------------------------
vtkBlockItem::~vtkBlockItem()
{
this->SetLabelProperties(nullptr);
}
//------------------------------------------------------------------------------
bool vtkBlockItem::Paint(vtkContext2D* painter)
{
this->CachedTextProp->ShallowCopy(painter->GetTextProp());
this->CachedPen->DeepCopy(painter->GetPen());
this->CachedBrush->DeepCopy(painter->GetBrush());
painter->ApplyTextProp(this->LabelProperties);
vtkVector4<float> dims(this->Dimensions);
const vtkVector2i tileScale = this->Scene->GetLogicalTileScale();
// if requested, resize the dims to fit the label.
if (this->AutoComputeDimensions)
{
float bounds[4];
painter->ComputeStringBounds(this->Label, bounds);
vtkLogF(TRACE, "label bds: x=%f, y=%f, w=%f, h=%f", bounds[0], bounds[1], bounds[2], bounds[3]);
dims[2] = bounds[2] + 2 * this->Padding[0] * tileScale[0];
dims[3] = bounds[3] + 2 * this->Padding[1] * tileScale[1];
}
// if requested, update the position for the box.
if (this->AutoComputeDimensions)
{
const vtkVector2i geometry(this->GetScene()->GetViewWidth(), this->GetScene()->GetViewHeight());
vtkLogF(TRACE, "size %d, %d", geometry[0], geometry[1]);
dims[0] = vtkComputePosition(
this->HorizontalAlignment, dims[0], dims[2], geometry[0], this->Margins[0]);
dims[1] =
vtkComputePosition(this->VerticalAlignment, dims[1], dims[3], geometry[1], this->Margins[1]);
}
this->Dimensions[0] = dims[0];
this->Dimensions[1] = dims[1];
this->Dimensions[2] = dims[2];
this->Dimensions[3] = dims[3];
painter->ApplyPen(this->Pen);
painter->ApplyBrush(this->MouseOver ? this->MouseOverBrush : this->Brush);
painter->DrawRect(
this->Dimensions[0], this->Dimensions[1], this->Dimensions[2], this->Dimensions[3]);
if (this->AutoComputeDimensions)
{
// put the label in the box (minus the Padding).
float rect[4];
rect[0] = this->Dimensions[0] + this->Padding[0] * tileScale[0];
rect[1] = this->Dimensions[1] + this->Padding[1] * tileScale[1];
rect[2] = this->Dimensions[2] - 2 * this->Padding[0] * tileScale[0];
rect[3] = this->Dimensions[3] - 2 * this->Padding[1] * tileScale[1];
painter->DrawStringRect(rect, this->Label);
}
else
{
// anchor label at center of the box (this what was done traditionally)
float x = this->Dimensions[0] + 0.5 * this->Dimensions[2];
float y = this->Dimensions[1] + 0.5 * this->Dimensions[3];
painter->DrawString(x, y, this->Label);
}
if (this->scalarFunction)
{
// We have a function pointer - do something...
}
this->PaintChildren(painter);
painter->ApplyTextProp(this->CachedTextProp);
painter->ApplyPen(this->CachedPen);
painter->ApplyBrush(this->CachedBrush);
return true;
}
//------------------------------------------------------------------------------
bool vtkBlockItem::Hit(const vtkContextMouseEvent& mouse)
{
if (!this->GetVisible() || !this->GetInteractive())
{
return false;
}
vtkVector2f pos = mouse.GetPos();
if (pos[0] > this->Dimensions[0] && pos[0] < this->Dimensions[0] + this->Dimensions[2] &&
pos[1] > this->Dimensions[1] && pos[1] < this->Dimensions[1] + this->Dimensions[3])
{
return true;
}
else
{
return this->vtkAbstractContextItem::Hit(mouse);
}
}
//------------------------------------------------------------------------------
bool vtkBlockItem::MouseEnterEvent(const vtkContextMouseEvent&)
{
this->MouseOver = true;
this->GetScene()->SetDirty(true);
return true;
}
//------------------------------------------------------------------------------
bool vtkBlockItem::MouseMoveEvent(const vtkContextMouseEvent& mouse)
{
vtkVector2f delta = mouse.GetPos() - mouse.GetLastPos();
if (mouse.GetButton() == vtkContextMouseEvent::LEFT_BUTTON)
{
// Move the block by this amount
this->Dimensions[0] += delta.GetX();
this->Dimensions[1] += delta.GetY();
this->GetScene()->SetDirty(true);
this->InvokeEvent(vtkCommand::InteractionEvent);
return true;
}
else if (mouse.GetButton() == mouse.MIDDLE_BUTTON)
{
// Resize the block by this amount
this->Dimensions[0] += delta.GetX();
this->Dimensions[1] += delta.GetY();
this->Dimensions[2] -= delta.GetX();
this->Dimensions[3] -= delta.GetY();
this->GetScene()->SetDirty(true);
this->InvokeEvent(vtkCommand::InteractionEvent);
return true;
}
else if (mouse.GetButton() == mouse.RIGHT_BUTTON)
{
// Resize the block by this amount
this->Dimensions[2] += delta.GetX();
this->Dimensions[3] += delta.GetY();
this->GetScene()->SetDirty(true);
this->InvokeEvent(vtkCommand::InteractionEvent);
return true;
}
return false;
}
//------------------------------------------------------------------------------
bool vtkBlockItem::MouseLeaveEvent(const vtkContextMouseEvent&)
{
this->MouseOver = false;
this->GetScene()->SetDirty(true);
return true;
}
//------------------------------------------------------------------------------
bool vtkBlockItem::MouseButtonPressEvent(const vtkContextMouseEvent&)
{
return true;
}
//------------------------------------------------------------------------------
bool vtkBlockItem::MouseButtonReleaseEvent(const vtkContextMouseEvent&)
{
return true;
}
//------------------------------------------------------------------------------
void vtkBlockItem::SetLabel(const vtkStdString& label)
{
if (this->Label != label)
{
this->Label = label;
this->Modified();
}
}
//------------------------------------------------------------------------------
vtkStdString vtkBlockItem::GetLabel()
{
return this->Label;
}
//------------------------------------------------------------------------------
void vtkBlockItem::SetScalarFunctor(double (*ScalarFunction)(double, double))
{
this->scalarFunction = ScalarFunction;
}
//------------------------------------------------------------------------------
void vtkBlockItem::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
VTK_ABI_NAMESPACE_END
|