From 9d59b5831000704a1ed39c22a6043ba658993159 Mon Sep 17 00:00:00 2001 From: Shamus Hammons Date: Thu, 14 Mar 2013 23:41:22 -0500 Subject: [PATCH] Initial stab at text object. Nonfunctional ATM. --- architektonas.pro | 4 ++ src/dimension.cpp | 96 +-------------------------------- src/dimension.h | 1 - src/drawlineaction.cpp | 5 ++ src/drawtextaction.cpp | 87 ++++++++++++++++++++++++++++++ src/drawtextaction.h | 25 +++++++++ src/line.cpp | 120 ++++++++++++++++------------------------- src/line.h | 2 - src/text.cpp | 15 ++++++ src/text.h | 32 +++++++++++ 10 files changed, 216 insertions(+), 171 deletions(-) create mode 100644 src/drawtextaction.cpp create mode 100644 src/drawtextaction.h create mode 100644 src/text.cpp create mode 100644 src/text.h diff --git a/architektonas.pro b/architektonas.pro index 5287f4d..40ac752 100644 --- a/architektonas.pro +++ b/architektonas.pro @@ -52,6 +52,7 @@ HEADERS = \ src/drawingview.h \ src/drawcircleaction.h \ src/drawlineaction.h \ + src/drawtextaction.h \ src/ellipse.h \ src/fileio.h \ src/generaltab.h \ @@ -61,6 +62,7 @@ HEADERS = \ src/object.h \ src/painter.h \ src/settingsdialog.h \ + src/text.h \ src/vector.h SOURCES = \ @@ -75,6 +77,7 @@ SOURCES = \ src/drawingview.cpp \ src/drawcircleaction.cpp \ src/drawlineaction.cpp \ + src/drawtextaction.cpp \ src/ellipse.cpp \ src/fileio.cpp \ src/generaltab.cpp \ @@ -83,5 +86,6 @@ SOURCES = \ src/object.cpp \ src/painter.cpp \ src/settingsdialog.cpp \ + src/text.cpp \ src/vector.cpp diff --git a/src/dimension.cpp b/src/dimension.cpp index d2aeaf6..eb978fd 100644 --- a/src/dimension.cpp +++ b/src/dimension.cpp @@ -9,6 +9,7 @@ // WHO WHEN WHAT // --- ---------- ------------------------------------------------------------ // JLH 04/04/2011 Created this file, basic rendering +// JLH 03/14/2013 Updated to new connection system // #include "dimension.h" @@ -26,23 +27,10 @@ Dimension::Dimension(Vector p1, Vector p2, DimensionType dt/*= DTLinear*/ ,Objec } -#if 0 -// This is bad, p1 & p2 could be NULL, causing much consternation... -Dimension::Dimension(Vector * p1, Vector * p2, DimensionType dt/*= DTLinear*/ , Object * p/*= NULL*/): - Object(*p1, p), endpoint(*p2), - dragging(false), draggingHandle1(false), draggingHandle2(false), - length(p2->Magnitude()), type(dt), point1(p1), point2(p2) -{ -} -#endif - - // This is bad, p1 & p2 could be NULL, causing much consternation... Dimension::Dimension(Connection p1, Connection p2, DimensionType dt/*= DTLinear*/ , Object * p/*= NULL*/): -/* Object(p1.object->GetPointForParameter(p1.t), p), - endpoint(p2.object->GetPointForParameter(p2.t)),*/ dragging(false), draggingHandle1(false), draggingHandle2(false), - /*length(p2->Magnitude()),*/length(0), type(dt), point1(p1), point2(p2) + length(0), type(dt), point1(p1), point2(p2) { } @@ -56,19 +44,11 @@ Dimension::~Dimension() { // If there are valid Vector pointers in here, use them to update the internal // positions. Otherwise, we just use the internal positions by default. -#if 0 - if (point1) - position = *point1; - - if (point2) - endpoint = *point2; -#else if (point1.object) position = point1.object->GetPointAtParameter(point1.t); if (point2.object) endpoint = point2.object->GetPointAtParameter(point2.t); -#endif if (state == OSSelected) painter->SetPen(QPen(Qt::red, 2.0, Qt::DotLine)); @@ -107,50 +87,8 @@ Dimension::~Dimension() painter->SetFont(QFont("Arial", 10.0 * Painter::zoom * SCREEN_ZOOM)); Vector v1((p1.x - p2.x) / 2.0, (p1.y - p2.y) / 2.0); Point ctr = p2 + v1; - // This is in pixels, which isn't even remotely correct... !!! FIX !!! QString dimText = QString("%1\"").arg(Vector(endpoint - position).Magnitude()); -// int textWidth = QFontMetrics(painter->font()).width(dimText); -// int textHeight = QFontMetrics(painter->font()).height(); -#if 0 -//We have to do transformation voodoo to make the text come out readable and in correct orientation... -//Some things to note here: if angle > 90 degrees, then we need to take the negative of the angle -//for our text. -painter->save(); -painter->translate(ctr.x, ctr.y); -int yOffset = -8; -//16 : printf("textHeight: %d\n", textHeight); - -//Fix text so it isn't upside down... -if ((angle > PI * 0.5) && (angle < PI * 1.5)) -{ - angle += PI; - yOffset = 18; -} - -painter->rotate(angle * RADIANS_TO_DEGREES); -painter->scale(1.0, -1.0); -//painter->translate(-textWidth / 2, -24); -// painter->drawText(0, 0, textWidth, 20, Qt::AlignCenter, dimText); - // This version draws the y-coord from the baseline of the font - painter->DrawText(-textWidth / 2, yOffset, dimText); -//painter->setPen(QPen(QColor(0xFF, 0x20, 0x20), 1.0, Qt::SolidLine)); -//painter->drawLine(20, 0, -20, 0); -//painter->drawLine(0, 20, 0, -20); -painter->restore(); -#else -// painter->DrawText(QRectF(QPointF(ctr.x, ctr.y), QPointF(ctr.x + textWidth, ctr.y + textHeight)), Qt::AlignVCenter, dimText); -// Now that we've taken our own good advice, maybe we should have the painter class -// do a nice abstracted text draw routine? :-) painter->DrawAngledText(ctr, angle, dimText); -#endif - -/* -All of the preceeding makes me think that rather than try to compensate for Qt's unbelieveably -AWFUL decision to go with a wrong-handed graphics subsystem, it may be better to just stuff -all of that crap into some kind of subclass that handles all the nastiness behind the scenes. -I mean, really, all this crap just to get some proplerly rendered text on the screen? How -retarded is that? :-/ -*/ } @@ -347,22 +285,6 @@ about keeping track of old states... } -#if 0 -void Dimension::SetPoint1(Vector * v) -{ - point1 = v; - needUpdate = true; -} - - -void Dimension::SetPoint2(Vector * v) -{ - point2 = v; - needUpdate = true; -} -#endif - - /*virtual*/ void Dimension::Enumerate(FILE * file) { fprintf(file, "DIMENSION (%lf,%lf) (%lf,%lf) %i\n", position.x, position.y, endpoint.x, endpoint.y, type); @@ -438,20 +360,6 @@ void Dimension::SetPoint2(Vector * v) } -#if 0 -Vector Dimension::GetPoint1(void) -{ - return position; -} - - -Vector Dimension::GetPoint2(void) -{ - return endpoint; -} -#endif - - void Dimension::FlipSides(void) { #if 0 diff --git a/src/dimension.h b/src/dimension.h index be8e5c5..2784e83 100644 --- a/src/dimension.h +++ b/src/dimension.h @@ -10,7 +10,6 @@ class Dimension: public Object { public: Dimension(Vector, Vector, DimensionType dt = DTLinear, Object * p = 0); -// Dimension(Vector *, Vector *, DimensionType dt = DTLinear, Object * p = 0); Dimension(Connection, Connection, DimensionType dt = DTLinear, Object * p = 0); ~Dimension(); diff --git a/src/drawlineaction.cpp b/src/drawlineaction.cpp index 780ad58..b0b992a 100644 --- a/src/drawlineaction.cpp +++ b/src/drawlineaction.cpp @@ -26,6 +26,7 @@ DrawLineAction::DrawLineAction(): state(0), line(NULL) { } + DrawLineAction::~DrawLineAction() { } @@ -47,6 +48,7 @@ DrawLineAction::~DrawLineAction() } } + /*virtual*/ void DrawLineAction::MouseDown(Vector point) { if (state == FIRST_POINT) @@ -55,6 +57,7 @@ DrawLineAction::~DrawLineAction() p2 = point; } + /*virtual*/ void DrawLineAction::MouseMoved(Vector point) { if (state == FIRST_POINT) @@ -63,6 +66,7 @@ DrawLineAction::~DrawLineAction() p2 = point; } + /*virtual*/ void DrawLineAction::MouseReleased(void) { if (state == FIRST_POINT) @@ -81,3 +85,4 @@ DrawLineAction::~DrawLineAction() p1 = p2; } } + diff --git a/src/drawtextaction.cpp b/src/drawtextaction.cpp new file mode 100644 index 0000000..fafdf95 --- /dev/null +++ b/src/drawtextaction.cpp @@ -0,0 +1,87 @@ +// drawtextaction.cpp: Draw text object +// +// Part of the Architektonas Project +// (C) 2011 Underground Software +// See the README and GPLv3 files for licensing and warranty information +// +// JLH = James Hammons +// +// WHO WHEN WHAT +// --- ---------- ------------------------------------------------------------ +// JLH 03/14/2013 Created this file +// + +#include "drawtextaction.h" +#include "painter.h" +#include "text.h" +//#include "vector.h" + + +#define FIRST_POINT 0 +#define NEXT_POINT 1 + + +DrawTextAction::DrawTextAction(): state(0), text(NULL) +{ +} + + +DrawTextAction::~DrawTextAction() +{ +} + + +/*virtual*/ void DrawTextAction::Draw(Painter * painter) +{ + painter->SetPen(QPen(Qt::red, 2.0, Qt::DotLine)); + + // I think stuff like crosshairs should be done in the DrawingView, tho + if (state == FIRST_POINT) + { + painter->DrawHandle(p1); + } + else + { +// painter->DrawLine(p1, p2); +// painter->DrawHandle(p2); + } +} + + +/*virtual*/ void DrawTextAction::MouseDown(Vector point) +{ + if (state == FIRST_POINT) + p1 = point; +// else +// p2 = point; +} + + +/*virtual*/ void DrawTextAction::MouseMoved(Vector point) +{ + if (state == FIRST_POINT) + p1 = point; +// else +// p2 = point; +} + + +/*virtual*/ void DrawTextAction::MouseReleased(void) +{ + if (state == FIRST_POINT) + { +// p2 = p1; + state = NEXT_POINT; + } + else if (state == NEXT_POINT) + { + // We create the new object here, and then pass it off to the + // DrawingView which stuffs it into the document. +// text = new Text(p1, p2); + // We don't need no stinkin' sentinels, when we have signals & slots! + emit ObjectReady(text); + +// p1 = p2; + } +} + diff --git a/src/drawtextaction.h b/src/drawtextaction.h new file mode 100644 index 0000000..23c7c6a --- /dev/null +++ b/src/drawtextaction.h @@ -0,0 +1,25 @@ +#ifndef __DRAWTEXTACTION_H__ +#define __DRAWTEXTACTION_H__ + +#include "action.h" + +class Text; + +class DrawTextAction: public Action +{ + public: + DrawTextAction(); + ~DrawTextAction(); + + virtual void Draw(Painter *); + virtual void MouseDown(Vector); + virtual void MouseMoved(Vector); + virtual void MouseReleased(void); + + private: + int state; + Text * text; + Vector p1; +}; + +#endif // __DRAWTEXTACTION_H__ diff --git a/src/line.cpp b/src/line.cpp index 244af94..d37106e 100644 --- a/src/line.cpp +++ b/src/line.cpp @@ -30,6 +30,7 @@ Line::Line(Vector p1, Vector p2, Object * p/*= NULL*/): Object(p1, p), endpoint( { } + Line::~Line() { // Taking care of connections should be done by the Container, as we don't know @@ -48,6 +49,7 @@ Line::~Line() #endif } + /*virtual*/ void Line::Draw(Painter * painter) { painter->SetPen(QPen(Qt::red, 2.0, Qt::DotLine)); @@ -238,6 +240,7 @@ Like so: return false; } + /*virtual*/ void Line::PointerMoved(Vector point) { // Hit test tells us what we hit (if anything) through boolean variables. It @@ -335,6 +338,7 @@ the horizontal line or vertical line that intersects from the current mouse posi } } + /*virtual*/ void Line::PointerReleased(void) { if (draggingHandle1 || draggingHandle2) @@ -385,6 +389,7 @@ about keeping track of old states... state = oldState; } + // Check to see if the point passed in coincides with any we have. If so, return a // pointer to it; otherwise, return NULL. /*virtual*/ Vector * Line::GetPointAt(Vector v) @@ -398,37 +403,63 @@ about keeping track of old states... } -#if 0 -void Line::SetDimensionOnPoint1(Dimension * dimension) +/*virtual*/ void Line::Enumerate(FILE * file) { - dimPoint1 = dimension; + fprintf(file, "LINE (%lf,%lf) (%lf,%lf)\n", position.x, position.y, endpoint.x, endpoint.y); +} + - if (dimension) - dimension->SetPoint1(position); +/*virtual*/ Object * Line::Copy(void) +{ +#warning "!!! This doesn't take care of attached Dimensions !!!" +/* +This is a real problem. While having a pointer in the Dimension to this line's points is fast & easy, +it creates a huge problem when trying to replicate an object like this. + +Maybe a way to fix that then, is to have reference numbers instead of pointers. That way, if you copy +them, ... you might still have problems. Because you can't be sure if a copy will be persistant or not, +you then *definitely* do not want them to have the same reference number. +*/ + return new Line(position, endpoint, parent); } -void Line::SetDimensionOnPoint2(Dimension * dimension) + +/*virtual*/ Vector Line::GetPointAtParameter(double parameter) { - dimPoint2 = dimension; + if (parameter <= 0) + return position; + else if (parameter >= 1.0) + return endpoint; + + // Our parameter lies between zero and one, so calculate it! + Vector v(endpoint, position); + double length = v.Magnitude(); + // We scale the magnitude of v so that it lies between 0 and 1... + // By multiplying the parameter by the magnitude, we obtain the point we + // want. No scaling necessary as it's inherent in the approach! + double spotOnLength = length * parameter; - if (dimension) - dimension->SetPoint2(endpoint); + // To get our point, we use the initial point of the line and add in our + // scaled point. + Vector result = position + (v * spotOnLength); + return result; } -#else + + void Line::SetDimensionOnLine(Dimension * dimension/*=NULL*/) { // If they don't pass one in, create it for the caller. if (dimension == NULL) { -printf("Line::SetDimensionOnLine(): Creating new dimension...\n"); +//printf("Line::SetDimensionOnLine(): Creating new dimension...\n"); // dimension = new Dimension(position, endpoint, DTLinear, this); dimension = new Dimension(Connection(this, 0), Connection(this, 1.0), DTLinear, this); if (parent) -{ -printf("Line::SetDimensionOnLine(): Adding to parent...\n"); +//{ +//printf("Line::SetDimensionOnLine(): Adding to parent...\n"); parent->Add(dimension); -} +//} } else { @@ -437,26 +468,9 @@ printf("Line::SetDimensionOnLine(): Adding to parent...\n"); } // Make sure the Dimension is connected to us... -#if 0 - connected.push_back(Connection(dimension, 0)); - connected.push_back(Connection(dimension, 1.0)); -#else Connect(dimension, 0); Connect(dimension, 1.0); -#endif - -// attachedDimension = dimension; - -#if 0 - // After we set the points here, we don't have to care about them anymore. - if (dimension) - { - dimension->SetPoint1(&position); - dimension->SetPoint2(&endpoint); - } -#endif } -#endif Object * Line::FindAttachedDimension(void) @@ -472,7 +486,7 @@ Object * Line::FindAttachedDimension(void) { for(uint j=i+1; j= 1.0) - return endpoint; - - // Our parameter lies between zero and one, so calculate it! - Vector v(endpoint, position); - double length = v.Magnitude(); - // We scale the magnitude of v so that it lies between 0 and 1... - // By multiplying the parameter by the magnitude, we obtain the point we - // want. No scaling necessary as it's inherent in the approach! - double spotOnLength = length * parameter; - - // To get our point, we use the initial point of the line and add in our - // scaled point. - Vector result = position + (v * spotOnLength); - return result; -} - diff --git a/src/line.h b/src/line.h index e9b73c7..b7d32f2 100644 --- a/src/line.h +++ b/src/line.h @@ -20,8 +20,6 @@ class Line: public Object virtual void Enumerate(FILE *); virtual Object * Copy(void); virtual Vector GetPointAtParameter(double parameter); -// void SetDimensionOnPoint1(Dimension *); -// void SetDimensionOnPoint2(Dimension *); void SetDimensionOnLine(Dimension * d = 0); Object * FindAttachedDimension(void); diff --git a/src/text.cpp b/src/text.cpp new file mode 100644 index 0000000..af34ea2 --- /dev/null +++ b/src/text.cpp @@ -0,0 +1,15 @@ +// text.cpp: Draw text object +// +// Part of the Architektonas Project +// (C) 2011 Underground Software +// See the README and GPLv3 files for licensing and warranty information +// +// JLH = James Hammons +// +// WHO WHEN WHAT +// --- ---------- ------------------------------------------------------------ +// JLH 03/14/2013 Created this file +// + +#include "text.h" + diff --git a/src/text.h b/src/text.h new file mode 100644 index 0000000..351a1a1 --- /dev/null +++ b/src/text.h @@ -0,0 +1,32 @@ +#ifndef __TEXT_H__ +#define __TEXT_H__ + +#include "object.h" +#include + +class Text: public Object +{ + public: + Text(Vector, QString, Object * p = 0); + ~Text(); + + virtual void Draw(Painter *); + virtual Vector Center(void); + virtual bool Collided(Vector); + virtual void PointerMoved(Vector); + virtual void PointerReleased(void); + virtual void Enumerate(FILE *); +// virtual Object * Copy(void); + virtual Vector GetPointAtParameter(double parameter); + + protected: + Vector oldPoint; // Used for dragging + + private: + bool dragging; + bool draggingHandle1; + bool draggingHandle2; + bool objectWasDragged; +}; + +#endif // __TEXT_H__ -- 2.37.2