pathstroker.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "pathstroker.h"
00028 #include "scribuscore.h"
00029 #include "scribusstructs.h"
00030 #include "util.h"
00031 #include <QPainterPathStroker>
00032
00033 int pathstroker_getPluginAPIVersion()
00034 {
00035 return PLUGIN_API_VERSION;
00036 }
00037
00038 ScPlugin* pathstroker_getPlugin()
00039 {
00040 PathStrokerPlugin* plug = new PathStrokerPlugin();
00041 Q_CHECK_PTR(plug);
00042 return plug;
00043 }
00044
00045 void pathstroker_freePlugin(ScPlugin* plugin)
00046 {
00047 PathStrokerPlugin* plug = dynamic_cast<PathStrokerPlugin*>(plugin);
00048 Q_ASSERT(plug);
00049 delete plug;
00050 }
00051
00052 PathStrokerPlugin::PathStrokerPlugin() : ScActionPlugin()
00053 {
00054
00055
00056 languageChange();
00057 }
00058
00059 PathStrokerPlugin::~PathStrokerPlugin() {};
00060
00061 void PathStrokerPlugin::languageChange()
00062 {
00063
00064
00065
00066 m_actionInfo.name = "PathStroker";
00067
00068 m_actionInfo.text = tr("Create Path from Stroke");
00069
00070 m_actionInfo.menu = "ItemPathOps";
00071 m_actionInfo.parentMenu = "Item";
00072 m_actionInfo.subMenuName = tr("Path Tools");
00073 m_actionInfo.enabledOnStartup = true;
00074 m_actionInfo.notSuitableFor.append(PageItem::Line);
00075 m_actionInfo.notSuitableFor.append(PageItem::TextFrame);
00076 m_actionInfo.notSuitableFor.append(PageItem::ImageFrame);
00077 m_actionInfo.notSuitableFor.append(PageItem::PathText);
00078 m_actionInfo.notSuitableFor.append(PageItem::LatexFrame);
00079 m_actionInfo.forAppMode.append(modeNormal);
00080 m_actionInfo.needsNumObjects = 1;
00081 }
00082
00083 const QString PathStrokerPlugin::fullTrName() const
00084 {
00085 return QObject::tr("PathStroker");
00086 }
00087
00088 const ScActionPlugin::AboutData* PathStrokerPlugin::getAboutData() const
00089 {
00090 AboutData* about = new AboutData;
00091 Q_CHECK_PTR(about);
00092 about->authors = QString::fromUtf8("Franz Schmid <Franz.Schmid@altmuehlnet.de>");
00093 about->shortDescription = tr("Create Path from Stroke");
00094 about->description = tr("Converts the stroke of a Path to a filled Path.");
00095
00096
00097
00098 about->license = "GPL";
00099 return about;
00100 }
00101
00102 void PathStrokerPlugin::deleteAboutData(const AboutData* about) const
00103 {
00104 Q_ASSERT(about);
00105 delete about;
00106 }
00107
00108 bool PathStrokerPlugin::run(ScribusDoc* doc, QString)
00109 {
00110 ScribusDoc* currDoc = doc;
00111 if (currDoc == 0)
00112 currDoc = ScCore->primaryMainWindow()->doc;
00113 if (currDoc->m_Selection->count() > 0)
00114 {
00115 QList<double> m_array;
00116 PageItem *currItem = currDoc->m_Selection->itemAt(0);
00117 FPointArray path = currItem->PoLine;
00118 QPainterPath pp;
00119 if (currItem->itemType() == PageItem::PolyLine)
00120 pp = path.toQPainterPath(false);
00121 else
00122 pp = path.toQPainterPath(true);
00123 QPainterPathStroker stroke;
00124 stroke.setCapStyle(currItem->lineEnd());
00125 stroke.setJoinStyle(currItem->lineJoin());
00126 if (currItem->lineStyle() == Qt::SolidLine)
00127 stroke.setDashPattern(currItem->lineStyle());
00128 else
00129 {
00130 getDashArray(currItem->lineStyle(), 1, m_array);
00131 stroke.setDashPattern(m_array.toVector());
00132 }
00133 stroke.setWidth(currItem->lineWidth());
00134 QPainterPath result = stroke.createStroke(pp);
00135 if (currItem->startArrowIndex() != 0)
00136 {
00137 FPoint Start = currItem->PoLine.point(0);
00138 for (uint xx = 1; xx < currItem->PoLine.size(); xx += 2)
00139 {
00140 FPoint Vector = currItem->PoLine.point(xx);
00141 if ((Start.x() != Vector.x()) || (Start.y() != Vector.y()))
00142 {
00143 double r = atan2(Start.y()-Vector.y(),Start.x()-Vector.x())*(180.0/M_PI);
00144 QMatrix arrowTrans;
00145 FPointArray arrow = currDoc->arrowStyles.at(currItem->startArrowIndex()-1).points.copy();
00146 arrowTrans.translate(Start.x(), Start.y());
00147 arrowTrans.rotate(r);
00148 arrowTrans.scale(currItem->lineWidth(), currItem->lineWidth());
00149 arrow.map(arrowTrans);
00150 result.addPath(arrow.toQPainterPath(true));
00151 break;
00152 }
00153 }
00154 }
00155 if (currItem->endArrowIndex() != 0)
00156 {
00157 FPoint End = currItem->PoLine.point(currItem->PoLine.size()-2);
00158 for (uint xx = currItem->PoLine.size()-1; xx > 0; xx -= 2)
00159 {
00160 FPoint Vector = currItem->PoLine.point(xx);
00161 if ((End.x() != Vector.x()) || (End.y() != Vector.y()))
00162 {
00163 double r = atan2(End.y()-Vector.y(),End.x()-Vector.x())*(180.0/M_PI);
00164 QMatrix arrowTrans;
00165 FPointArray arrow = currDoc->arrowStyles.at(currItem->endArrowIndex()-1).points.copy();
00166 arrowTrans.translate(End.x(), End.y());
00167 arrowTrans.rotate(r);
00168 arrowTrans.scale(currItem->lineWidth(), currItem->lineWidth());
00169 arrow.map(arrowTrans);
00170 result.addPath(arrow.toQPainterPath(true));
00171 break;
00172 }
00173 }
00174 }
00175 currDoc->m_Selection->clear();
00176 PageItem* newItem = currDoc->convertItemTo(currItem, PageItem::Polygon);
00177 newItem->setLineWidth(0);
00178 newItem->setLineStyle(Qt::SolidLine);
00179 newItem->setFillColor(newItem->lineColor());
00180 newItem->setFillShade(newItem->lineShade());
00181 newItem->setFillTransparency(newItem->lineTransparency());
00182 newItem->setFillBlendmode(newItem->lineBlendmode());
00183 FPointArray points;
00184 points.fromQPainterPath(result);
00185 newItem->PoLine = points;
00186 newItem->Frame = false;
00187 newItem->ClipEdited = true;
00188 newItem->FrameType = 3;
00189 currDoc->AdjustItemSize(newItem);
00190 newItem->OldB2 = newItem->width();
00191 newItem->OldH2 = newItem->height();
00192 newItem->updateClip();
00193 newItem->ContourLine = newItem->PoLine.copy();
00194 newItem->setFillEvenOdd(false);
00195 currDoc->m_Selection->addItem(newItem);
00196 currDoc->changed();
00197 }
00198 return true;
00199 }