1 /*
2 For general Scribus (>=1.3.2) copyright and licensing information please refer
3 to the COPYING file provided with the program. Following this notice may exist
4 a copyright and/or license notice that predates the release of Scribus 1.3.2
5 for which a new license (GPL+exception) is in place.
6 */
7 /***************************************************************************
8 *   Copyright (C) 2011 by Franz Schmid                                     *
9 *   franz.schmid@altmuehlnet.de                                            *
10 *                                                                          *
11 *   This program is free software; you can redistribute it and/or modify   *
12 *   it under the terms of the GNU General Public License as published by   *
13 *   the Free Software Foundation; either version 2 of the License, or      *
14 *   (at your option) any later version.                                    *
15 *                                                                          *
16 *   This program is distributed in the hope that it will be useful,        *
17 *   but WITHOUT ANY WARRANTY; without even the implied warranty of         *
18 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
19 *   GNU General Public License for more details.                           *
20 *                                                                          *
21 *   You should have received a copy of the GNU General Public License      *
22 *   along with this program; if not, write to the                          *
23 *   Free Software Foundation, Inc.,                                        *
24 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.              *
25 ****************************************************************************/
26 
27 #if defined(_MSC_VER) && !defined(_USE_MATH_DEFINES)
28 #define _USE_MATH_DEFINES
29 #endif
30 #include <cmath>
31 #include "smoothpath.h"
32 #include "scribuscore.h"
33 #include "scribusdoc.h"
34 #include "appmodes.h"
35 #include "selection.h"
36 #include "util.h"
37 #include <QPainterPath>
38 #include <QPolygonF>
39 #include <QList>
40 #include "KarbonCurveFit.h"
41 
smoothpath_getPluginAPIVersion()42 int smoothpath_getPluginAPIVersion()
43 {
44 	return PLUGIN_API_VERSION;
45 }
46 
smoothpath_getPlugin()47 ScPlugin* smoothpath_getPlugin()
48 {
49 	SmoothPathPlugin* plug = new SmoothPathPlugin();
50 	Q_CHECK_PTR(plug);
51 	return plug;
52 }
53 
smoothpath_freePlugin(ScPlugin * plugin)54 void smoothpath_freePlugin(ScPlugin* plugin)
55 {
56 	SmoothPathPlugin* plug = qobject_cast<SmoothPathPlugin*>(plugin);
57 	Q_ASSERT(plug);
58 	delete plug;
59 }
60 
SmoothPathPlugin()61 SmoothPathPlugin::SmoothPathPlugin()
62 {
63 	// Set action info in languageChange, so we only have to do
64 	// it in one place.
65 	languageChange();
66 }
67 
~SmoothPathPlugin()68 SmoothPathPlugin::~SmoothPathPlugin() {};
69 
languageChange()70 void SmoothPathPlugin::languageChange()
71 {
72 	// Note that we leave the unused members unset. They'll be initialised
73 	// with their default ctors during construction.
74 	// Action name
75 	m_actionInfo.name = "SmoothPath";
76 	// Action text for menu, including accel
77 	m_actionInfo.text = tr("Smooth Path");
78 	// Menu
79 	m_actionInfo.menu = "ItemPathOps";
80 	m_actionInfo.parentMenu = "Item";
81 	m_actionInfo.subMenuName = tr("Path Tools");
82 	m_actionInfo.enabledOnStartup = false;
83 	m_actionInfo.notSuitableFor.append(PageItem::Line);
84 	m_actionInfo.notSuitableFor.append(PageItem::TextFrame);
85 	m_actionInfo.notSuitableFor.append(PageItem::ImageFrame);
86 	m_actionInfo.notSuitableFor.append(PageItem::Polygon);
87 	m_actionInfo.notSuitableFor.append(PageItem::PathText);
88 	m_actionInfo.notSuitableFor.append(PageItem::LatexFrame);
89 	m_actionInfo.notSuitableFor.append(PageItem::Symbol);
90 	m_actionInfo.notSuitableFor.append(PageItem::RegularPolygon);
91 	m_actionInfo.notSuitableFor.append(PageItem::Arc);
92 	m_actionInfo.notSuitableFor.append(PageItem::Spiral);
93 	m_actionInfo.forAppMode.append(modeNormal);
94 	m_actionInfo.needsNumObjects = 1;
95 }
96 
fullTrName() const97 QString SmoothPathPlugin::fullTrName() const
98 {
99 	return QObject::tr("SmoothPath");
100 }
101 
getAboutData() const102 const ScActionPlugin::AboutData* SmoothPathPlugin::getAboutData() const
103 {
104 	AboutData* about = new AboutData;
105 	Q_CHECK_PTR(about);
106 	about->authors = QString::fromUtf8("Franz Schmid <Franz.Schmid@altmuehlnet.de>");
107 	about->shortDescription = tr("Smoothes a Path");
108 	about->description = tr("Converts a Polyline made of straight Lines to a smoothed Bezier Curve.");
109 	// about->version
110 	// about->releaseDate
111 	// about->copyright
112 	about->license = "GPL";
113 	return about;
114 }
115 
deleteAboutData(const AboutData * about) const116 void SmoothPathPlugin::deleteAboutData(const AboutData* about) const
117 {
118 	Q_ASSERT(about);
119 	delete about;
120 }
121 
run(ScribusDoc * doc,const QString &)122 bool SmoothPathPlugin::run(ScribusDoc* doc, const QString&)
123 {
124 	ScribusDoc* currDoc = doc;
125 	if (currDoc == nullptr)
126 		currDoc = ScCore->primaryMainWindow()->doc;
127 	if (currDoc->m_Selection->count() > 0)
128 	{
129 		PageItem *currItem = currDoc->m_Selection->itemAt(0);
130 		QPainterPath pp;
131 		if (currItem->itemType() == PageItem::PolyLine)
132 			pp = currItem->PoLine.toQPainterPath(false);
133 		else
134 			pp = currItem->PoLine.toQPainterPath(true);
135 		QList<QPolygonF> polyList = pp.toSubpathPolygons();
136 		QPainterPath result;
137 		for (int a = 0; a < polyList.count(); a++)
138 		{
139 			result.addPath(bezierFit(polyList[a], 5.0));
140 		}
141 		currItem->PoLine.fromQPainterPath(result);
142 		currItem->ClipEdited = true;
143 		currItem->FrameType = 3;
144 		currDoc->adjustItemSize(currItem);
145 		currItem->OldB2 = currItem->width();
146 		currItem->OldH2 = currItem->height();
147 		currItem->updateClip();
148 		currDoc->regionsChanged()->update(QRectF());
149 		currDoc->changed();
150 	}
151 	return true;
152 }
153