summaryrefslogtreecommitdiffstats
path: root/src/drawparts
diff options
context:
space:
mode:
Diffstat (limited to 'src/drawparts')
-rw-r--r--src/drawparts/Makefile.am6
-rw-r--r--src/drawparts/dpline.cpp227
-rw-r--r--src/drawparts/dpline.h56
-rw-r--r--src/drawparts/dptext.cpp133
-rw-r--r--src/drawparts/dptext.h48
-rw-r--r--src/drawparts/drawpart.cpp264
-rw-r--r--src/drawparts/drawpart.h68
-rw-r--r--src/drawparts/solidshape.cpp192
-rw-r--r--src/drawparts/solidshape.h68
9 files changed, 1062 insertions, 0 deletions
diff --git a/src/drawparts/Makefile.am b/src/drawparts/Makefile.am
new file mode 100644
index 0000000..f3c1c25
--- /dev/null
+++ b/src/drawparts/Makefile.am
@@ -0,0 +1,6 @@
+INCLUDES = -I$(top_srcdir)/src -I$(top_srcdir)/src/electronics -I$(top_srcdir)/src/electronics/components -I$(top_srcdir)/src/electronics/simulation -I$(top_srcdir)/src/flowparts -I$(top_srcdir)/src/gui -I$(top_srcdir)/src/languages -I$(top_srcdir)/src/mechanics -I$(top_srcdir)/src/micro $(all_includes)
+METASOURCES = AUTO
+noinst_LTLIBRARIES = libdrawparts.la
+libdrawparts_la_SOURCES = drawpart.cpp dpline.cpp solidshape.cpp dptext.cpp
+noinst_HEADERS = drawpart.h dpline.h solidshape.h dptext.h
+
diff --git a/src/drawparts/dpline.cpp b/src/drawparts/dpline.cpp
new file mode 100644
index 0000000..59fa789
--- /dev/null
+++ b/src/drawparts/dpline.cpp
@@ -0,0 +1,227 @@
+/***************************************************************************
+ * Copyright (C) 2005 by David Saxton *
+ * david@bluehaze.org *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ ***************************************************************************/
+
+#include "dpline.h"
+#include "libraryitem.h"
+#include "resizeoverlay.h"
+#include "variant.h"
+
+#include <cmath>
+#include <kiconloader.h>
+#include <klocale.h>
+#include <qpainter.h>
+
+
+//BEGIN class DPLine
+Item* DPLine::construct( ItemDocument *itemDocument, bool newItem, const char *id )
+{
+ return new DPLine( itemDocument, newItem, id );
+}
+
+LibraryItem* DPLine::libraryItem()
+{
+ return new LibraryItem(
+ QString("dp/line"),
+ i18n("Line"),
+ i18n("Other"),
+ KGlobal::iconLoader()->loadIcon( "text", KIcon::Small ),
+ LibraryItem::lit_drawpart,
+ DPLine::construct );
+}
+
+DPLine::DPLine( ItemDocument *itemDocument, bool newItem, const char *id )
+ : DrawPart( itemDocument, newItem, id ? id : "line" )
+{
+ m_pLineOverlay = new LineOverlay(this);
+ m_name = i18n("Line");
+ m_desc = i18n("Select the line to position the end points");
+
+ createProperty( "line-color", Variant::Type::Color );
+ property("line-color")->setCaption( i18n("Line Color") );
+ property("line-color")->setValue(Qt::black);
+
+ createProperty( "line-width", Variant::Type::Int );
+ property("line-width")->setCaption( i18n("Line Width") );
+ property("line-width")->setMinValue(1);
+ property("line-width")->setMaxValue(1000);
+ property("line-width")->setValue(1);
+
+ createProperty( "line-style", Variant::Type::PenStyle );
+ property("line-style")->setCaption( i18n("Line Style") );
+ property("line-style")->setAdvanced(true);
+ setDataPenStyle( "line-style", Qt::SolidLine );
+
+ createProperty( "cap-style", Variant::Type::PenCapStyle );
+ property("cap-style")->setCaption( i18n("Cap Style") );
+ property("cap-style")->setAdvanced(true);
+ setDataPenCapStyle( "cap-style", Qt::FlatCap );
+}
+
+DPLine::~DPLine()
+{
+}
+
+void DPLine::setSelected( bool yes )
+{
+ if ( yes == isSelected() )
+ return;
+
+ DrawPart::setSelected(yes);
+ m_pLineOverlay->showResizeHandles(yes);
+}
+
+
+void DPLine::dataChanged()
+{
+ setPen( QPen( dataColor("line-color"),
+ unsigned( dataInt("line-width") ),
+ getDataPenStyle("line-style"),
+ getDataPenCapStyle("cap-style"),
+ Qt::MiterJoin ) );
+
+ postResize(); // in case the pen width has changed
+ update();
+}
+
+
+void DPLine::postResize()
+{
+ int x1 = offsetX();
+ int y1 = offsetY();
+ int x2 = x1+width();
+ int y2 = y1+height();
+
+ QPointArray p(4);
+ int pw = pen().width();
+ int dx = QABS(x1-x2);
+ int dy = QABS(y1-y2);
+ pw = pw*4/3+2; // approx pw*sqrt(2)
+ int px = x1<x2 ? -pw : pw ;
+ int py = y1<y2 ? -pw : pw ;
+ if ( dx && dy && (dx > dy ? (dx*2/dy <= 2) : (dy*2/dx <= 2)) ) {
+ // steep
+ if ( px == py ) {
+ p[0] = QPoint(x1 ,y1+py);
+ p[1] = QPoint(x2-px,y2 );
+ p[2] = QPoint(x2 ,y2-py);
+ p[3] = QPoint(x1+px,y1 );
+ } else {
+ p[0] = QPoint(x1+px,y1 );
+ p[1] = QPoint(x2 ,y2-py);
+ p[2] = QPoint(x2-px,y2 );
+ p[3] = QPoint(x1 ,y1+py);
+ }
+ } else if ( dx > dy ) {
+ // horizontal
+ p[0] = QPoint(x1+px,y1+py);
+ p[1] = QPoint(x2-px,y2+py);
+ p[2] = QPoint(x2-px,y2-py);
+ p[3] = QPoint(x1+px,y1-py);
+ } else {
+ // vertical
+ p[0] = QPoint(x1+px,y1+py);
+ p[1] = QPoint(x2+px,y2-py);
+ p[2] = QPoint(x2-px,y2-py);
+ p[3] = QPoint(x1-px,y1+py);
+ }
+ setItemPoints( p, false );
+}
+
+
+void DPLine::drawShape( QPainter & p )
+{
+ int x1 = int(x()+offsetX());
+ int y1 = int(y()+offsetY());
+ int x2 = x1+width();
+ int y2 = y1+height();
+
+ p.drawLine( x1, y1, x2, y2 );
+}
+//END class DPLine
+
+
+//BEGIN class DPArrow
+Item* DPArrow::construct( ItemDocument *itemDocument, bool newItem, const char *id )
+{
+ return new DPArrow( itemDocument, newItem, id );
+}
+
+LibraryItem* DPArrow::libraryItem()
+{
+ return new LibraryItem(
+ QString("dp/arrow"),
+ i18n("Arrow"),
+ i18n("Other"),
+ KGlobal::iconLoader()->loadIcon( "text", KIcon::Small ),
+ LibraryItem::lit_drawpart,
+ DPArrow::construct );
+}
+
+DPArrow::DPArrow( ItemDocument *itemDocument, bool newItem, const char *id )
+ : DPLine( itemDocument, newItem, id ? id : "arrow" )
+{
+ m_name = i18n("Arrow");
+
+ // We don't want to use the square cap style as it screws up drawing our arrow head
+ QStringList allowed = property("cap-style")->allowed();
+ allowed.remove( DrawPart::penCapStyleToName( Qt::SquareCap ) );
+ property("cap-style")->setAllowed(allowed);
+}
+
+DPArrow::~DPArrow()
+{
+}
+
+
+void DPArrow::drawShape( QPainter & p )
+{
+ int x1 = int(x()+offsetX());
+ int y1 = int(y()+offsetY());
+ int x2 = x1+width();
+ int y2 = y1+height();
+
+ p.drawLine( x1, y1, x2, y2 );
+
+ double dx = x2-x1;
+ double dy = y2-y1;
+
+ if ( dx == 0. && dy == 0. )
+ return;
+
+ double pi = 3.14159265358979323846264;
+ double arrow_angle = ( dx == 0 ? (dy>0?(pi/2.):(-pi/2.)) : std::atan(dy/dx) );
+ if ( dx < 0 )
+ arrow_angle += pi;
+
+ double head_angle = 0.6; // Angle of arrowhead
+ double head_length = 10.;
+
+ // Position of arrowhead
+ int x3 = int( x2 + head_length*std::cos( pi + arrow_angle - head_angle ) );
+ int y3 = int( y2 + head_length*std::sin( pi + arrow_angle - head_angle ) );
+ int x4 = int( x2 + head_length*std::cos( pi + arrow_angle + head_angle ) );
+ int y4 = int( y2 + head_length*std::sin( pi + arrow_angle + head_angle ) );
+
+ // Draw arrowhead
+ QPen pen = p.pen();
+ pen.setCapStyle( Qt::RoundCap );
+ p.setPen(pen);
+ p.setBrush(pen.color());
+ QPointArray pa(3);
+ pa[0] = QPoint( x2, y2 );
+ pa[1] = QPoint( x3, y3 );
+ pa[2] = QPoint( x4, y4 );
+ p.drawPolygon(pa);
+ p.drawPolyline(pa);
+// p.drawLine( x2, y2, x3, y3 );
+// p.drawLine( x2, y2, x4, y4 );
+}
+//END class DPLine
+
diff --git a/src/drawparts/dpline.h b/src/drawparts/dpline.h
new file mode 100644
index 0000000..e47aca8
--- /dev/null
+++ b/src/drawparts/dpline.h
@@ -0,0 +1,56 @@
+/***************************************************************************
+ * Copyright (C) 2005 by David Saxton *
+ * david@bluehaze.org *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ ***************************************************************************/
+
+#ifndef DPLINE_H
+#define DPLINE_H
+
+#include "drawpart.h"
+
+class LineOverlay;
+
+/**
+@author David Saxton
+*/
+class DPLine : public DrawPart
+{
+ public:
+ DPLine( ItemDocument *itemDocument, bool newItem, const char *id = 0L );
+ ~DPLine();
+
+ static Item* construct( ItemDocument *itemDocument, bool newItem, const char *id );
+ static LibraryItem *libraryItem();
+
+ virtual void setSelected( bool yes );
+
+ protected:
+ virtual void postResize();
+ virtual void dataChanged();
+ virtual void drawShape( QPainter &p );
+
+ LineOverlay * m_pLineOverlay;
+};
+
+/**
+@author David Saxton
+*/
+class DPArrow : public DPLine
+{
+ public:
+ DPArrow( ItemDocument *itemDocument, bool newItem, const char *id = 0L );
+ ~DPArrow();
+
+ static Item* construct( ItemDocument *itemDocument, bool newItem, const char *id );
+ static LibraryItem *libraryItem();
+
+ protected:
+ virtual void drawShape( QPainter &p );
+};
+
+#endif
diff --git a/src/drawparts/dptext.cpp b/src/drawparts/dptext.cpp
new file mode 100644
index 0000000..ebb239a
--- /dev/null
+++ b/src/drawparts/dptext.cpp
@@ -0,0 +1,133 @@
+/***************************************************************************
+ * Copyright (C) 2003-2005 by David Saxton *
+ * david@bluehaze.org *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ ***************************************************************************/
+
+#include "dptext.h"
+#include "itemdocument.h"
+#include "libraryitem.h"
+#include "resizeoverlay.h"
+
+#include <kiconloader.h>
+#include <klocale.h>
+#include <qpainter.h>
+
+Item* DPText::construct( ItemDocument *itemDocument, bool newItem, const char *id )
+{
+ return new DPText( itemDocument, newItem, id );
+}
+
+LibraryItem* DPText::libraryItem()
+{
+ QStringList idList;
+ idList << "dp/text" << "dp/canvas_text" << "canvas_text";
+
+ return new LibraryItem(
+ idList,
+ i18n("Canvas Text"),
+ i18n("Other"),
+ KGlobal::iconLoader()->loadIcon( "text", KIcon::Small ),
+ LibraryItem::lit_drawpart,
+ DPText::construct );
+}
+
+DPText::DPText( ItemDocument *itemDocument, bool newItem, const char *id )
+ : DrawPart( itemDocument, newItem, (id) ? id : "canvas_text" )
+{
+ m_rectangularOverlay = new RectangularOverlay(this);
+ m_name = i18n("Text");
+ m_desc = i18n("Doubleclick the Text Item to set the text");
+
+ createProperty( "text", Variant::Type::Multiline );
+ property("text")->setValue( i18n("Text") );
+
+ createProperty( "background", Variant::Type::Bool );
+ property("background")->setValue(false);
+ property("background")->setCaption( i18n("Display Background") );
+ property("background")->setAdvanced(true);
+
+ createProperty( "background-color", Variant::Type::Color );
+ property("background-color")->setValue(Qt::white);
+ property("background-color")->setCaption( i18n("Background Color") );
+ property("background-color")->setAdvanced(true);
+
+ createProperty( "frame-color", Variant::Type::Color );
+ property("frame-color")->setValue(Qt::black);
+ property("frame-color")->setCaption( i18n("Frame Color") );
+ property("frame-color")->setAdvanced(true);
+
+ createProperty( "text-color", Variant::Type::Color );
+ property("text-color")->setValue(Qt::black);
+ property("text-color")->setCaption( i18n("Text Color") );
+}
+
+DPText::~DPText()
+{
+}
+
+void DPText::setSelected( bool yes )
+{
+ if ( yes == isSelected() )
+ return;
+
+ DrawPart::setSelected(yes);
+ m_rectangularOverlay->showResizeHandles(yes);
+}
+
+
+void DPText::dataChanged()
+{
+ m_caption = dataString("text");
+ b_displayBackground = dataBool("background");
+ m_backgroundColor = dataColor("background-color");
+ m_textColor = dataColor("text-color");
+ m_frameColor = dataColor("frame-color");
+ update();
+}
+
+
+void DPText::postResize()
+{
+ setItemPoints( QPointArray(m_sizeRect), false );
+}
+
+
+QSize DPText::minimumSize() const
+{
+ return QSize( 48, 24 );
+}
+
+
+void DPText::drawShape( QPainter &p )
+{
+ QRect bound = m_sizeRect;
+ bound.setWidth( bound.width()-2 );
+ bound.setHeight( bound.height()-2 );
+ bound.moveBy( int(x()+1), int(y()+1) );
+
+ if (b_displayBackground)
+ {
+ p.save();
+ p.setPen( QPen( m_frameColor, 1, Qt::DotLine) );
+ p.setBrush(m_backgroundColor);
+ p.drawRect(bound);
+ p.restore();
+ }
+
+ const int pad = 6;
+
+ bound.setLeft( bound.left()+pad );
+ bound.setTop( bound.top()+pad );
+ bound.setRight( bound.right()-pad );
+ bound.setBottom( bound.bottom()-pad );
+
+ p.setPen(m_textColor);
+ p.setFont( font() );
+ p.drawText( bound, (Qt::WordBreak | Qt::AlignHCenter | Qt::AlignVCenter), m_caption );
+}
+
diff --git a/src/drawparts/dptext.h b/src/drawparts/dptext.h
new file mode 100644
index 0000000..a6f5a4a
--- /dev/null
+++ b/src/drawparts/dptext.h
@@ -0,0 +1,48 @@
+/***************************************************************************
+ * Copyright (C) 2003-2005 by David Saxton *
+ * david@bluehaze.org *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ ***************************************************************************/
+
+#ifndef CANVASTEXT_H
+#define CANVASTEXT_H
+
+#include "drawpart.h"
+
+/**
+@short Represents editable text on the canvas
+@author David Saxton
+*/
+class DPText : public DrawPart
+{
+public:
+ DPText( ItemDocument *itemDocument, bool newItem, const char *id = 0L );
+ ~DPText();
+
+ static Item* construct( ItemDocument *itemDocument, bool newItem, const char *id );
+ static LibraryItem *libraryItem();
+ static LibraryItem *libraryItemOld();
+
+ virtual void setSelected( bool yes );
+
+ virtual QSize minimumSize() const;
+
+protected:
+ virtual void postResize();
+
+private:
+ virtual void drawShape( QPainter &p );
+ void dataChanged();
+ QString m_caption;
+ bool b_displayBackground;
+ QColor m_textColor;
+ QColor m_backgroundColor;
+ QColor m_frameColor;
+ RectangularOverlay *m_rectangularOverlay;
+};
+
+#endif
diff --git a/src/drawparts/drawpart.cpp b/src/drawparts/drawpart.cpp
new file mode 100644
index 0000000..6d9708d
--- /dev/null
+++ b/src/drawparts/drawpart.cpp
@@ -0,0 +1,264 @@
+/***************************************************************************
+ * Copyright (C) 2005 by David Saxton *
+ * david@bluehaze.org *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ ***************************************************************************/
+
+#include "itemdocument.h"
+#include "itemdocumentdata.h"
+#include "drawpart.h"
+#include "variant.h"
+
+#include <klocale.h>
+#include <qbitarray.h>
+
+DrawPart::DrawPart( ItemDocument *itemDocument, bool newItem, const char *id )
+ : Item( itemDocument, newItem, id )
+{
+ itemDocument->registerItem(this);
+}
+
+
+DrawPart::~DrawPart()
+{
+}
+
+
+int DrawPart::rtti() const
+{
+ return ItemDocument::RTTI::DrawPart;
+}
+
+
+Variant * DrawPart::createProperty( const QString & id, Variant::Type::Value type )
+{
+ if ( type == Variant::Type::PenStyle )
+ {
+ QStringList penStyles;
+ penStyles << DrawPart::penStyleToName(Qt::SolidLine) << DrawPart::penStyleToName(Qt::DashLine)
+ << DrawPart::penStyleToName(Qt::DotLine) << DrawPart::penStyleToName(Qt::DashDotLine)
+ << DrawPart::penStyleToName(Qt::DashDotDotLine);
+
+ Variant * v = createProperty( id, Variant::Type::String );
+ v->setType( Variant::Type::PenStyle );
+ v->setAllowed(penStyles);
+ return v;
+ }
+
+ if ( type == Variant::Type::PenCapStyle )
+ {
+ QStringList penCapStyles;
+ penCapStyles << DrawPart::penCapStyleToName(Qt::FlatCap) << DrawPart::penCapStyleToName(Qt::SquareCap)
+ << DrawPart::penCapStyleToName(Qt::RoundCap);
+
+ Variant * v = createProperty( id, Variant::Type::String );
+ v->setType( Variant::Type::PenCapStyle );
+ v->setAllowed(penCapStyles);
+ return v;
+ }
+
+ return Item::createProperty( id, type );
+}
+
+
+Qt::PenStyle DrawPart::getDataPenStyle( const QString & id )
+{
+ return nameToPenStyle( dataString(id) );
+}
+Qt::PenCapStyle DrawPart::getDataPenCapStyle( const QString & id )
+{
+ return nameToPenCapStyle( dataString(id) );
+}
+void DrawPart::setDataPenStyle( const QString & id, Qt::PenStyle value )
+{
+ property(id)->setValue( penStyleToName(value) );
+}
+void DrawPart::setDataPenCapStyle( const QString & id, Qt::PenCapStyle value )
+{
+ property(id)->setValue( penCapStyleToName(value) );
+}
+
+
+ItemData DrawPart::itemData() const
+{
+ ItemData itemData = Item::itemData();
+
+ const VariantDataMap::const_iterator end = m_variantData.end();
+ for ( VariantDataMap::const_iterator it = m_variantData.begin(); it != end; ++it )
+ {
+ switch( it.data()->type() )
+ {
+ case Variant::Type::PenStyle:
+ itemData.dataString[it.key()] = penStyleToID( nameToPenStyle( it.data()->value().toString() ) );
+ break;
+ case Variant::Type::PenCapStyle:
+ itemData.dataString[it.key()] = penCapStyleToID( nameToPenCapStyle( it.data()->value().toString() ) );
+ break;
+ case Variant::Type::String:
+ case Variant::Type::FileName:
+ case Variant::Type::Port:
+ case Variant::Type::Pin:
+ case Variant::Type::VarName:
+ case Variant::Type::Combo:
+ case Variant::Type::Select:
+ case Variant::Type::Multiline:
+ case Variant::Type::Int:
+ case Variant::Type::Double:
+ case Variant::Type::Color:
+ case Variant::Type::Bool:
+ case Variant::Type::Raw:
+ case Variant::Type::SevenSegment:
+ case Variant::Type::KeyPad:
+ case Variant::Type::None:
+ // All of these are handled by Item
+ break;
+ }
+ }
+
+ return itemData;
+}
+
+
+void DrawPart::restoreFromItemData( const ItemData &itemData )
+{
+ Item::restoreFromItemData(itemData);
+
+ const QStringMap::const_iterator stringEnd = itemData.dataString.end();
+ for ( QStringMap::const_iterator it = itemData.dataString.begin(); it != stringEnd; ++it )
+ {
+ VariantDataMap::iterator vit = m_variantData.find(it.key());
+ if ( vit == m_variantData.end() )
+ continue;
+
+ if ( vit.data()->type() == Variant::Type::PenStyle )
+ setDataPenStyle( it.key(), idToPenStyle( it.data() ) );
+
+ else if ( vit.data()->type() == Variant::Type::PenCapStyle )
+ setDataPenCapStyle( it.key(), idToPenCapStyle( it.data() ) );
+ }
+}
+
+
+
+QString DrawPart::penStyleToID( Qt::PenStyle style )
+{
+ switch (style)
+ {
+ case Qt::SolidLine:
+ return "SolidLine";
+ case Qt::NoPen:
+ return "NoPen";
+ case Qt::DashLine:
+ return "DashLine";
+ case Qt::DotLine:
+ return "DotLine";
+ case Qt::DashDotLine:
+ return "DashDotLine";
+ case Qt::DashDotDotLine:
+ return "DashDotDotLine";
+ case Qt::MPenStyle:
+ default:
+ return ""; // ?!
+ }
+}
+Qt::PenStyle DrawPart::idToPenStyle( const QString & id )
+{
+ if ( id == "NoPen" )
+ return Qt::NoPen;
+ if ( id == "DashLine" )
+ return Qt::DashLine;
+ if ( id == "DotLine" )
+ return Qt::DotLine;
+ if ( id == "DashDotLine" )
+ return Qt::DashDotLine;
+ if ( id == "DashDotDotLine" )
+ return Qt::DashDotDotLine;
+ return Qt::SolidLine;
+}
+QString DrawPart::penCapStyleToID( Qt::PenCapStyle style )
+{
+ switch (style)
+ {
+ case Qt::FlatCap:
+ return "FlatCap";
+ case Qt::SquareCap:
+ return "SquareCap";
+ case Qt::RoundCap:
+ return "RoundCap";
+ case Qt::MPenCapStyle:
+ default:
+ return ""; // ?!
+ }
+}
+Qt::PenCapStyle DrawPart::idToPenCapStyle( const QString & id )
+{
+ if ( id == "SquareCap" )
+ return Qt::SquareCap;
+ if ( id == "RoundCap" )
+ return Qt::RoundCap;
+ return Qt::FlatCap;
+}
+
+QString DrawPart::penStyleToName( Qt::PenStyle style )
+{
+ switch (style)
+ {
+ case Qt::SolidLine:
+ return i18n("Solid");
+ case Qt::NoPen:
+ return i18n("None");
+ case Qt::DashLine:
+ return i18n("Dash");
+ case Qt::DotLine:
+ return i18n("Dot");
+ case Qt::DashDotLine:
+ return i18n("Dash Dot");
+ case Qt::DashDotDotLine:
+ return i18n("Dash Dot Dot");
+ case Qt::MPenStyle:
+ default:
+ return ""; // ?!
+ }
+}
+Qt::PenStyle DrawPart::nameToPenStyle( const QString & name )
+{
+ if ( name == i18n("None") )
+ return Qt::NoPen;
+ if ( name == i18n("Dash") )
+ return Qt::DashLine;
+ if ( name == i18n("Dot") )
+ return Qt::DotLine;
+ if ( name == i18n("Dash Dot") )
+ return Qt::DashDotLine;
+ if ( name == i18n("Dash Dot Dot") )
+ return Qt::DashDotDotLine;
+ return Qt::SolidLine;
+}
+QString DrawPart::penCapStyleToName( Qt::PenCapStyle style )
+{
+ switch (style)
+ {
+ case Qt::FlatCap:
+ return i18n("Flat");
+ case Qt::SquareCap:
+ return i18n("Square");
+ case Qt::RoundCap:
+ return i18n("Round");
+ case Qt::MPenCapStyle:
+ default:
+ return ""; // ?!
+ }
+}
+Qt::PenCapStyle DrawPart::nameToPenCapStyle( const QString & name )
+{
+ if ( name == i18n("Square") )
+ return Qt::SquareCap;
+ if ( name == i18n("Round") )
+ return Qt::RoundCap;
+ return Qt::FlatCap;
+}
+
diff --git a/src/drawparts/drawpart.h b/src/drawparts/drawpart.h
new file mode 100644
index 0000000..8dc9f50
--- /dev/null
+++ b/src/drawparts/drawpart.h
@@ -0,0 +1,68 @@
+/***************************************************************************
+ * Copyright (C) 2005 by David Saxton *
+ * david@bluehaze.org *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ ***************************************************************************/
+
+#ifndef DRAWPART_H
+#define DRAWPART_H
+
+#include <item.h>
+
+class ICNDocument;
+class ItemDocument;
+class LibraryItem;
+class RectangularOverlay;
+
+/**
+@author David Saxton
+*/
+class DrawPart : public Item
+{
+ public:
+ enum DrawAction
+ {
+ // Note: values are used for popup menu
+ da_text = 0,
+ da_line = 1,
+ da_arrow = 2,
+ da_rectangle = 3,
+ da_ellipse = 4
+ };
+
+ DrawPart( ItemDocument *itemDocument, bool newItem, const char *id );
+ virtual ~DrawPart();
+
+ int rtti() const;
+
+ virtual bool canResize() const { return true; }
+
+ virtual Variant * createProperty( const QString & id, Variant::Type::Value type );
+
+ Qt::PenStyle getDataPenStyle( const QString & id );
+ Qt::PenCapStyle getDataPenCapStyle( const QString & id );
+
+ void setDataPenStyle( const QString & id, Qt::PenStyle value );
+ void setDataPenCapStyle( const QString & id, Qt::PenCapStyle value );
+
+ virtual ItemData itemData() const;
+ virtual void restoreFromItemData( const ItemData &itemData );
+
+ // Convention for following functions: name is i18n'd name of style, id is the english one
+
+ static QString penStyleToID( Qt::PenStyle style );
+ static Qt::PenStyle idToPenStyle( const QString & id );
+ static QString penCapStyleToID( Qt::PenCapStyle style );
+ static Qt::PenCapStyle idToPenCapStyle( const QString & id );
+
+ static QString penStyleToName( Qt::PenStyle style );
+ static Qt::PenStyle nameToPenStyle( const QString & name );
+ static QString penCapStyleToName( Qt::PenCapStyle style );
+ static Qt::PenCapStyle nameToPenCapStyle( const QString & name );
+};
+
+#endif
diff --git a/src/drawparts/solidshape.cpp b/src/drawparts/solidshape.cpp
new file mode 100644
index 0000000..e60efef
--- /dev/null
+++ b/src/drawparts/solidshape.cpp
@@ -0,0 +1,192 @@
+/***************************************************************************
+ * Copyright (C) 2005 by David Saxton *
+ * david@bluehaze.org *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ ***************************************************************************/
+
+#include "solidshape.h"
+#include "libraryitem.h"
+#include "resizeoverlay.h"
+
+#include <cmath>
+#include <kiconloader.h>
+#include <klocale.h>
+#include <qpainter.h>
+
+
+//BEGIN class DPRectangle
+Item * DPRectangle::construct( ItemDocument *itemDocument, bool newItem, const char *id )
+{
+ return new DPRectangle( itemDocument, newItem, id );
+}
+
+LibraryItem* DPRectangle::libraryItem()
+{
+ return new LibraryItem(
+ QString("dp/rectangle"),
+ i18n("Rectangle"),
+ i18n("Other"),
+ KGlobal::iconLoader()->loadIcon( "text", KIcon::Small ),
+ LibraryItem::lit_drawpart,
+ DPRectangle::construct );
+}
+
+DPRectangle::DPRectangle( ItemDocument *itemDocument, bool newItem, const char *id )
+ : DrawPart( itemDocument, newItem, id ? id : "rectangle" )
+{
+ m_pRectangularOverlay = new RectangularOverlay(this);
+ m_name = i18n("Rectangle");
+
+ createProperty( "background", Variant::Type::Bool );
+ property("background")->setValue(false);
+ property("background")->setCaption( i18n("Display Background") );
+ property("background")->setAdvanced(true);
+
+ createProperty( "background-color", Variant::Type::Color );
+ property("background-color")->setValue(Qt::white);
+ property("background-color")->setCaption( i18n("Background Color") );
+ property("background-color")->setAdvanced(true);
+
+ createProperty( "line-color", Variant::Type::Color );
+ property("line-color")->setValue(Qt::black);
+ property("line-color")->setCaption( i18n("Line Color") );
+ property("line-color")->setAdvanced(true);
+
+ createProperty( "line-width", Variant::Type::Int );
+ property("line-width")->setCaption( i18n("Line Width") );
+ property("line-width")->setMinValue(1);
+ property("line-width")->setMaxValue(1000);
+ property("line-width")->setValue(1);
+ property("line-width")->setAdvanced(true);
+
+ createProperty( "line-style", Variant::Type::PenStyle );
+ property("line-style")->setAdvanced(true);
+ setDataPenStyle( "line-style", Qt::SolidLine );
+}
+
+DPRectangle::~DPRectangle()
+{
+}
+
+void DPRectangle::setSelected( bool yes )
+{
+ if ( yes == isSelected() )
+ return;
+
+ DrawPart::setSelected(yes);
+ m_pRectangularOverlay->showResizeHandles(yes);
+}
+
+
+void DPRectangle::dataChanged()
+{
+ bool displayBackground = dataBool("background");
+ QColor line_color = dataColor("line-color");
+ unsigned width = unsigned( dataInt("line-width") );
+ Qt::PenStyle style = getDataPenStyle("line-style");
+
+ setPen( QPen( line_color, width, style ) );
+
+ if (displayBackground)
+ setBrush( dataColor("background-color") );
+ else
+ setBrush( Qt::NoBrush );
+
+ postResize();
+ update();
+}
+
+
+QSize DPRectangle::minimumSize() const
+{
+ int side = QMAX(16, pen().width()+2);
+ return QSize( side, side );
+}
+
+
+void DPRectangle::postResize()
+{
+ setItemPoints( m_sizeRect, false );
+}
+
+
+QRect DPRectangle::drawRect() const
+{
+ int lw = pen().width();
+
+ if ( lw > m_sizeRect.width() )
+ lw = m_sizeRect.width();
+
+ if ( lw > m_sizeRect.height() )
+ lw = m_sizeRect.height();
+
+ return QRect( int(x() + m_sizeRect.x()+lw/2), int(y() + m_sizeRect.y()+lw/2),
+ m_sizeRect.width()-lw, m_sizeRect.height()-lw );
+}
+
+
+void DPRectangle::drawShape( QPainter & p )
+{
+ p.drawRect(drawRect());
+}
+//END class DPRectangle
+
+
+//BEGIN class DPEllipse
+Item * DPEllipse::construct( ItemDocument *itemDocument, bool newItem, const char *id )
+{
+ return new DPEllipse( itemDocument, newItem, id );
+}
+
+LibraryItem* DPEllipse::libraryItem()
+{
+ return new LibraryItem(
+ QString("dp/ellipse"),
+ i18n("Ellipse"),
+ i18n("Other"),
+ KGlobal::iconLoader()->loadIcon( "text", KIcon::Small ),
+ LibraryItem::lit_drawpart,
+ DPEllipse::construct );
+}
+
+DPEllipse::DPEllipse( ItemDocument *itemDocument, bool newItem, const char *id )
+ : DPRectangle( itemDocument, newItem, id ? id : "ellipse" )
+{
+ m_name = i18n("Ellipse");
+}
+
+DPEllipse::~DPEllipse()
+{
+}
+
+
+void DPEllipse::postResize()
+{
+ QRect br = m_sizeRect;
+
+ // Make octagon that roughly covers ellipse
+ QPointArray pa(8);
+ pa[0] = QPoint( br.x() + br.width()/4, br.y() );
+ pa[1] = QPoint( br.x() + 3*br.width()/4, br.y() );
+ pa[2] = QPoint( br.x() + br.width(), br.y() + br.height()/4 );
+ pa[3] = QPoint( br.x() + br.width(), br.y() + 3*br.height()/4 );
+ pa[4] = QPoint( br.x() + 3*br.width()/4, br.y() + br.height() );
+ pa[5] = QPoint( br.x() + br.width()/4, br.y() + br.height() );
+ pa[6] = QPoint( br.x(), br.y() + 3*br.height()/4 );
+ pa[7] = QPoint( br.x(), br.y() + br.height()/4 );
+
+ setItemPoints( pa, false );
+}
+
+
+void DPEllipse::drawShape( QPainter & p )
+{
+ p.drawEllipse(drawRect());
+}
+//END class SolidShape
+
+
diff --git a/src/drawparts/solidshape.h b/src/drawparts/solidshape.h
new file mode 100644
index 0000000..317f417
--- /dev/null
+++ b/src/drawparts/solidshape.h
@@ -0,0 +1,68 @@
+/***************************************************************************
+ * Copyright (C) 2005 by David Saxton *
+ * david@bluehaze.org *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ ***************************************************************************/
+
+#ifndef SOLIDSHAPE_H
+#define SOLIDSHAPE_H
+
+#include <drawpart.h>
+
+class RectangularOverlay;
+
+/**
+@short Represents a drawable rectangle on the canvas
+@author David Saxton
+*/
+class DPRectangle : public DrawPart
+{
+ public:
+ DPRectangle( ItemDocument *itemDocument, bool newItem, const char *id = 0L );
+ ~DPRectangle();
+
+ static Item* construct( ItemDocument *itemDocument, bool newItem, const char *id );
+ static LibraryItem *libraryItem();
+
+ virtual void setSelected( bool yes );
+
+ virtual QSize minimumSize() const;
+
+ protected:
+ virtual void drawShape( QPainter &p );
+ void dataChanged();
+ virtual void postResize();
+
+ /** Returns the rectangle to draw in, taking into account the line
+ * width */
+ QRect drawRect() const;
+
+ private:
+ RectangularOverlay *m_pRectangularOverlay;
+};
+
+/**
+@short Represents a drawable rectangle on the canvas
+@author David Saxton
+*/
+class DPEllipse : public DPRectangle
+{
+ public:
+ DPEllipse( ItemDocument *itemDocument, bool newItem, const char *id = 0L );
+ ~DPEllipse();
+
+ static Item* construct( ItemDocument *itemDocument, bool newItem, const char *id );
+ static LibraryItem *libraryItem();
+
+ protected:
+ virtual void postResize();
+
+ private:
+ virtual void drawShape( QPainter &p );
+};
+
+#endif