summaryrefslogtreecommitdiffstats
path: root/scribus/plugins/tools/pathcut/pathcut.cpp
blob: f177436e90a20c835a44e23153b69e08ad81df2b (plain)
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
/*
For general Scribus (>=1.3.2) copyright and licensing information please refer
to the COPYING file provided with the program. Following this notice may exist
a copyright and/or license notice that predates the release of Scribus 1.3.2
for which a new license (GPL+exception) is in place.
*/
/***************************************************************************
*   Copyright (C) 2007 by Franz Schmid                                     *
*   franz.schmid@altmuehlnet.de                                            *
*                                                                          *
*   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.                                    *
*                                                                          *
*   This program is distributed in the hope that it will be useful,        *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of         *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
*   GNU General Public License for more details.                           *
*                                                                          *
*   You should have received a copy of the GNU General Public License      *
*   along with this program; if not, write to the                          *
*   Free Software Foundation, Inc.,                                        *
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.              *
****************************************************************************/

#include "pathcut.h"
#include "scribuscore.h"
#include "scribusstructs.h"
#include "util.h"
#include "util_icon.h"
#include <QPainterPathStroker>
#include <QMessageBox>

int pathcut_getPluginAPIVersion()
{
	return PLUGIN_API_VERSION;
}

ScPlugin* pathcut_getPlugin()
{
	PathCutPlugin* plug = new PathCutPlugin();
	Q_CHECK_PTR(plug);
	return plug;
}

void pathcut_freePlugin(ScPlugin* plugin)
{
	PathCutPlugin* plug = dynamic_cast<PathCutPlugin*>(plugin);
	Q_ASSERT(plug);
	delete plug;
}

PathCutPlugin::PathCutPlugin() : ScActionPlugin()
{
	// Set action info in languageChange, so we only have to do
	// it in one place.
	languageChange();
}

PathCutPlugin::~PathCutPlugin() {};

void PathCutPlugin::languageChange()
{
	// Note that we leave the unused members unset. They'll be initialised
	// with their default ctors during construction.
	// Action name
	m_actionInfo.name = "PathCutter";
	// Action text for menu, including accel
	m_actionInfo.text = tr("Cut Polygon");
	// Menu
	m_actionInfo.icon1 = loadIcon("transform-crop-and-resize.png");
	m_actionInfo.icon2 = loadIcon("transform-crop-and-resize.png");
	m_actionInfo.menu = "ItemPathOps";
	m_actionInfo.parentMenu = "Item";
	m_actionInfo.subMenuName = tr("Path Tools");
	m_actionInfo.enabledOnStartup = false;
	m_actionInfo.notSuitableFor.append(PageItem::Line);
	m_actionInfo.notSuitableFor.append(PageItem::TextFrame);
	m_actionInfo.notSuitableFor.append(PageItem::ImageFrame);
	m_actionInfo.notSuitableFor.append(PageItem::PathText);
	m_actionInfo.notSuitableFor.append(PageItem::LatexFrame);
	m_actionInfo.forAppMode.append(modeNormal);
	m_actionInfo.needsNumObjects = 2;
	m_actionInfo.firstObjectType.append(PageItem::PolyLine);
	m_actionInfo.secondObjectType.append(PageItem::Polygon);
}

const QString PathCutPlugin::fullTrName() const
{
	return QObject::tr("PathCutter");
}

const ScActionPlugin::AboutData* PathCutPlugin::getAboutData() const
{
	AboutData* about = new AboutData;
	Q_CHECK_PTR(about);
	about->authors = QString::fromUtf8("Franz Schmid <Franz.Schmid@altmuehlnet.de>");
	about->shortDescription = tr("Cuts a Polygon by a Polyline");
	about->description = tr("Cuts a Polygon by a Polyline");
	// about->version
	// about->releaseDate
	// about->copyright
	about->license = "GPL";
	return about;
}

void PathCutPlugin::deleteAboutData(const AboutData* about) const
{
	Q_ASSERT(about);
	delete about;
}

bool PathCutPlugin::run(ScribusDoc* doc, QString)
{
	QString vers = QString(qVersion()).left(5);
	if (vers < "4.3.3")
	{
		QMessageBox::information(doc->scMW(), tr("Qt Version too old"), tr("This plugin requires at least version 4.3.3 of the Qt library"));
		return true;
	}
	ScribusDoc* currDoc = doc;
	if (currDoc == 0)
		currDoc = ScCore->primaryMainWindow()->doc;
	if (currDoc->m_Selection->count() > 1)
	{
		PageItem *Item1 = currDoc->m_Selection->itemAt(0);
		PageItem *Item2 = currDoc->m_Selection->itemAt(1);
		if (Item1->itemType() != PageItem::PolyLine)
		{
			Item1 = currDoc->m_Selection->itemAt(1);
			Item2 = currDoc->m_Selection->itemAt(0);
		}
		FPointArray path = Item1->PoLine;
		QPainterPathStroker stroke;
		stroke.setWidth(Item1->lineWidth());
		QPainterPath cutter = stroke.createStroke(path.toQPainterPath(false));
		QMatrix ms;
		ms.translate(Item1->xPos() - Item2->xPos(), Item1->yPos() - Item2->yPos());
		ms.rotate(Item1->rotation());
		cutter = ms.map(cutter);
		path.map(ms);
		FPoint start = path.point(0);
		FPoint end = path.point(path.size()-2);
		QMatrix mm;
		mm.rotate(Item2->rotation());
		QPainterPath objekt = mm.map(Item2->PoLine.toQPainterPath(true));
		if ((objekt.contains(QPointF(start.x(), start.y()))) || (objekt.contains(QPointF(end.x(), end.y()))))
		{
			QMessageBox::information(doc->scMW(), tr("Error"), tr("The cutting line must cross the polygon and\nboth end points must lie outside of the polygon"));
			return true;
		}
		QPainterPath result = objekt.subtracted(cutter);
		FPointArray points;
		points.fromQPainterPath(result);
		Item2->PoLine = points;
		Item2->Frame = false;
		Item2->ClipEdited = true;
		Item2->FrameType = 3;
		currDoc->AdjustItemSize(Item2);
		Item2->OldB2 = Item2->width();
		Item2->OldH2 = Item2->height();
		Item2->updateClip();
		Item2->ContourLine = Item2->PoLine.copy();
		currDoc->m_Selection->clear();
		currDoc->m_Selection->addItem(Item1);
		currDoc->itemSelection_DeleteItem();
		currDoc->m_Selection->clear();
		currDoc->m_Selection->addItem(Item2);
		currDoc->itemSelection_SplitItems();
		currDoc->changed();
	}
	return true;
}