Rev 18007 | Rev 19067 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
11056 | fschmid | 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) 2007 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., * |
||
18122 | mrdocs | 24 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * |
11056 | fschmid | 25 | ****************************************************************************/ |
26 | |||
27 | #include "pathcut.h" |
||
28 | #include "scribuscore.h" |
||
13805 | jghali | 29 | #include "scribusdoc.h" |
11056 | fschmid | 30 | #include "scribusstructs.h" |
13805 | jghali | 31 | #include "selection.h" |
11056 | fschmid | 32 | #include "util.h" |
11057 | fschmid | 33 | #include "util_icon.h" |
11056 | fschmid | 34 | #include <QPainterPathStroker> |
35 | #include <QMessageBox> |
||
36 | |||
37 | int pathcut_getPluginAPIVersion() |
||
38 | { |
||
39 | return PLUGIN_API_VERSION; |
||
40 | } |
||
41 | |||
42 | ScPlugin* pathcut_getPlugin() |
||
43 | { |
||
44 | PathCutPlugin* plug = new PathCutPlugin(); |
||
45 | Q_CHECK_PTR(plug); |
||
46 | return plug; |
||
47 | } |
||
48 | |||
49 | void pathcut_freePlugin(ScPlugin* plugin) |
||
50 | { |
||
51 | PathCutPlugin* plug = dynamic_cast<PathCutPlugin*>(plugin); |
||
52 | Q_ASSERT(plug); |
||
53 | delete plug; |
||
54 | } |
||
55 | |||
56 | PathCutPlugin::PathCutPlugin() : ScActionPlugin() |
||
57 | { |
||
58 | // Set action info in languageChange, so we only have to do |
||
59 | // it in one place. |
||
60 | languageChange(); |
||
61 | } |
||
62 | |||
63 | PathCutPlugin::~PathCutPlugin() {}; |
||
64 | |||
65 | void PathCutPlugin::languageChange() |
||
66 | { |
||
67 | // Note that we leave the unused members unset. They'll be initialised |
||
68 | // with their default ctors during construction. |
||
69 | // Action name |
||
70 | m_actionInfo.name = "PathCutter"; |
||
71 | // Action text for menu, including accel |
||
72 | m_actionInfo.text = tr("Cut Polygon"); |
||
15675 | fschmid | 73 | m_actionInfo.helpText = tr("Cuts a Polygon with a Path."); |
11056 | fschmid | 74 | // Menu |
11057 | fschmid | 75 | m_actionInfo.icon1 = loadIcon("transform-crop-and-resize.png"); |
76 | m_actionInfo.icon2 = loadIcon("transform-crop-and-resize.png"); |
||
11056 | fschmid | 77 | m_actionInfo.menu = "ItemPathOps"; |
78 | m_actionInfo.parentMenu = "Item"; |
||
79 | m_actionInfo.subMenuName = tr("Path Tools"); |
||
12566 | cbradney | 80 | m_actionInfo.enabledOnStartup = false; |
11056 | fschmid | 81 | m_actionInfo.notSuitableFor.append(PageItem::Line); |
82 | m_actionInfo.notSuitableFor.append(PageItem::TextFrame); |
||
83 | m_actionInfo.notSuitableFor.append(PageItem::ImageFrame); |
||
84 | m_actionInfo.notSuitableFor.append(PageItem::PathText); |
||
85 | m_actionInfo.notSuitableFor.append(PageItem::LatexFrame); |
||
15060 | fschmid | 86 | m_actionInfo.notSuitableFor.append(PageItem::Symbol); |
16191 | fschmid | 87 | m_actionInfo.notSuitableFor.append(PageItem::RegularPolygon); |
16215 | fschmid | 88 | m_actionInfo.notSuitableFor.append(PageItem::Arc); |
16311 | fschmid | 89 | m_actionInfo.notSuitableFor.append(PageItem::Spiral); |
11056 | fschmid | 90 | m_actionInfo.forAppMode.append(modeNormal); |
91 | m_actionInfo.needsNumObjects = 2; |
||
92 | m_actionInfo.firstObjectType.append(PageItem::PolyLine); |
||
93 | m_actionInfo.secondObjectType.append(PageItem::Polygon); |
||
94 | } |
||
95 | |||
96 | const QString PathCutPlugin::fullTrName() const |
||
97 | { |
||
98 | return QObject::tr("PathCutter"); |
||
99 | } |
||
100 | |||
101 | const ScActionPlugin::AboutData* PathCutPlugin::getAboutData() const |
||
102 | { |
||
103 | AboutData* about = new AboutData; |
||
104 | Q_CHECK_PTR(about); |
||
105 | about->authors = QString::fromUtf8("Franz Schmid <Franz.Schmid@altmuehlnet.de>"); |
||
106 | about->shortDescription = tr("Cuts a Polygon by a Polyline"); |
||
107 | about->description = tr("Cuts a Polygon by a Polyline"); |
||
108 | // about->version |
||
109 | // about->releaseDate |
||
110 | // about->copyright |
||
111 | about->license = "GPL"; |
||
112 | return about; |
||
113 | } |
||
114 | |||
115 | void PathCutPlugin::deleteAboutData(const AboutData* about) const |
||
116 | { |
||
117 | Q_ASSERT(about); |
||
118 | delete about; |
||
119 | } |
||
120 | |||
121 | bool PathCutPlugin::run(ScribusDoc* doc, QString) |
||
122 | { |
||
123 | QString vers = QString(qVersion()).left(5); |
||
124 | if (vers < "4.3.3") |
||
125 | { |
||
126 | QMessageBox::information(doc->scMW(), tr("Qt Version too old"), tr("This plugin requires at least version 4.3.3 of the Qt library")); |
||
127 | return true; |
||
128 | } |
||
129 | ScribusDoc* currDoc = doc; |
||
130 | if (currDoc == 0) |
||
131 | currDoc = ScCore->primaryMainWindow()->doc; |
||
132 | if (currDoc->m_Selection->count() > 1) |
||
133 | { |
||
134 | PageItem *Item1 = currDoc->m_Selection->itemAt(0); |
||
135 | PageItem *Item2 = currDoc->m_Selection->itemAt(1); |
||
136 | if (Item1->itemType() != PageItem::PolyLine) |
||
137 | { |
||
138 | Item1 = currDoc->m_Selection->itemAt(1); |
||
139 | Item2 = currDoc->m_Selection->itemAt(0); |
||
140 | } |
||
141 | FPointArray path = Item1->PoLine; |
||
142 | QPainterPathStroker stroke; |
||
143 | stroke.setWidth(Item1->lineWidth()); |
||
144 | QPainterPath cutter = stroke.createStroke(path.toQPainterPath(false)); |
||
13951 | fschmid | 145 | QTransform ms; |
11056 | fschmid | 146 | ms.translate(Item1->xPos() - Item2->xPos(), Item1->yPos() - Item2->yPos()); |
147 | ms.rotate(Item1->rotation()); |
||
148 | cutter = ms.map(cutter); |
||
149 | path.map(ms); |
||
150 | FPoint start = path.point(0); |
||
151 | FPoint end = path.point(path.size()-2); |
||
13951 | fschmid | 152 | QTransform mm; |
11056 | fschmid | 153 | mm.rotate(Item2->rotation()); |
154 | QPainterPath objekt = mm.map(Item2->PoLine.toQPainterPath(true)); |
||
155 | if ((objekt.contains(QPointF(start.x(), start.y()))) || (objekt.contains(QPointF(end.x(), end.y())))) |
||
156 | { |
||
157 | QMessageBox::information(doc->scMW(), tr("Error"), tr("The cutting line must cross the polygon and\nboth end points must lie outside of the polygon")); |
||
158 | return true; |
||
159 | } |
||
160 | QPainterPath result = objekt.subtracted(cutter); |
||
161 | FPointArray points; |
||
162 | points.fromQPainterPath(result); |
||
163 | Item2->PoLine = points; |
||
164 | Item2->ClipEdited = true; |
||
165 | Item2->FrameType = 3; |
||
166 | currDoc->AdjustItemSize(Item2); |
||
167 | Item2->OldB2 = Item2->width(); |
||
168 | Item2->OldH2 = Item2->height(); |
||
169 | Item2->updateClip(); |
||
170 | Item2->ContourLine = Item2->PoLine.copy(); |
||
15685 | fschmid | 171 | // currDoc->m_Selection->clear(); |
172 | // currDoc->m_Selection->addItem(Item1); |
||
173 | // currDoc->itemSelection_DeleteItem(); |
||
11056 | fschmid | 174 | currDoc->m_Selection->clear(); |
175 | currDoc->m_Selection->addItem(Item2); |
||
176 | currDoc->itemSelection_SplitItems(); |
||
177 | currDoc->changed(); |
||
178 | } |
||
179 | return true; |
||
180 | } |