Scribus Developer Documentation - API
logo-pen

Scribus Developer Documentation - API

pathcut.cpp

Go to the documentation of this file.
00001 /*
00002 For general Scribus (>=1.3.2) copyright and licensing information please refer
00003 to the COPYING file provided with the program. Following this notice may exist
00004 a copyright and/or license notice that predates the release of Scribus 1.3.2
00005 for which a new license (GPL+exception) is in place.
00006 */
00007 /***************************************************************************
00008 *   Copyright (C) 2007 by Franz Schmid                                     *
00009 *   franz.schmid@altmuehlnet.de                                            *
00010 *                                                                          *
00011 *   This program is free software; you can redistribute it and/or modify   *
00012 *   it under the terms of the GNU General Public License as published by   *
00013 *   the Free Software Foundation; either version 2 of the License, or      *
00014 *   (at your option) any later version.                                    *
00015 *                                                                          *
00016 *   This program is distributed in the hope that it will be useful,        *
00017 *   but WITHOUT ANY WARRANTY; without even the implied warranty of         *
00018 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
00019 *   GNU General Public License for more details.                           *
00020 *                                                                          *
00021 *   You should have received a copy of the GNU General Public License      *
00022 *   along with this program; if not, write to the                          *
00023 *   Free Software Foundation, Inc.,                                        *
00024 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.              *
00025 ****************************************************************************/
00026 
00027 #include "pathcut.h"
00028 #include "scribuscore.h"
00029 #include "scribusstructs.h"
00030 #include "util.h"
00031 #include "util_icon.h"
00032 #include <QPainterPathStroker>
00033 #include <QMessageBox>
00034 
00035 int pathcut_getPluginAPIVersion()
00036 {
00037     return PLUGIN_API_VERSION;
00038 }
00039 
00040 ScPlugin* pathcut_getPlugin()
00041 {
00042     PathCutPlugin* plug = new PathCutPlugin();
00043     Q_CHECK_PTR(plug);
00044     return plug;
00045 }
00046 
00047 void pathcut_freePlugin(ScPlugin* plugin)
00048 {
00049     PathCutPlugin* plug = dynamic_cast<PathCutPlugin*>(plugin);
00050     Q_ASSERT(plug);
00051     delete plug;
00052 }
00053 
00054 PathCutPlugin::PathCutPlugin() : ScActionPlugin()
00055 {
00056     // Set action info in languageChange, so we only have to do
00057     // it in one place.
00058     languageChange();
00059 }
00060 
00061 PathCutPlugin::~PathCutPlugin() {};
00062 
00063 void PathCutPlugin::languageChange()
00064 {
00065     // Note that we leave the unused members unset. They'll be initialised
00066     // with their default ctors during construction.
00067     // Action name
00068     m_actionInfo.name = "PathCutter";
00069     // Action text for menu, including accel
00070     m_actionInfo.text = tr("Cut Polygon");
00071     // Menu
00072     m_actionInfo.icon1 = loadIcon("transform-crop-and-resize.png");
00073     m_actionInfo.icon2 = loadIcon("transform-crop-and-resize.png");
00074     m_actionInfo.menu = "ItemPathOps";
00075     m_actionInfo.parentMenu = "Item";
00076     m_actionInfo.subMenuName = tr("Path Tools");
00077     m_actionInfo.enabledOnStartup = true;
00078     m_actionInfo.notSuitableFor.append(PageItem::Line);
00079     m_actionInfo.notSuitableFor.append(PageItem::TextFrame);
00080     m_actionInfo.notSuitableFor.append(PageItem::ImageFrame);
00081     m_actionInfo.notSuitableFor.append(PageItem::PathText);
00082     m_actionInfo.notSuitableFor.append(PageItem::LatexFrame);
00083     m_actionInfo.forAppMode.append(modeNormal);
00084     m_actionInfo.needsNumObjects = 2;
00085     m_actionInfo.firstObjectType.append(PageItem::PolyLine);
00086     m_actionInfo.secondObjectType.append(PageItem::Polygon);
00087 }
00088 
00089 const QString PathCutPlugin::fullTrName() const
00090 {
00091     return QObject::tr("PathCutter");
00092 }
00093 
00094 const ScActionPlugin::AboutData* PathCutPlugin::getAboutData() const
00095 {
00096     AboutData* about = new AboutData;
00097     Q_CHECK_PTR(about);
00098     about->authors = QString::fromUtf8("Franz Schmid <Franz.Schmid@altmuehlnet.de>");
00099     about->shortDescription = tr("Cuts a Polygon by a Polyline");
00100     about->description = tr("Cuts a Polygon by a Polyline");
00101     // about->version
00102     // about->releaseDate
00103     // about->copyright
00104     about->license = "GPL";
00105     return about;
00106 }
00107 
00108 void PathCutPlugin::deleteAboutData(const AboutData* about) const
00109 {
00110     Q_ASSERT(about);
00111     delete about;
00112 }
00113 
00114 bool PathCutPlugin::run(ScribusDoc* doc, QString)
00115 {
00116     QString vers = QString(qVersion()).left(5);
00117     if (vers < "4.3.3")
00118     {
00119         QMessageBox::information(doc->scMW(), tr("Qt Version too old"), tr("This plugin requires at least version 4.3.3 of the Qt library"));
00120         return true;
00121     }
00122     ScribusDoc* currDoc = doc;
00123     if (currDoc == 0)
00124         currDoc = ScCore->primaryMainWindow()->doc;
00125     if (currDoc->m_Selection->count() > 1)
00126     {
00127         PageItem *Item1 = currDoc->m_Selection->itemAt(0);
00128         PageItem *Item2 = currDoc->m_Selection->itemAt(1);
00129         if (Item1->itemType() != PageItem::PolyLine)
00130         {
00131             Item1 = currDoc->m_Selection->itemAt(1);
00132             Item2 = currDoc->m_Selection->itemAt(0);
00133         }
00134         FPointArray path = Item1->PoLine;
00135         QPainterPathStroker stroke;
00136         stroke.setWidth(Item1->lineWidth());
00137         QPainterPath cutter = stroke.createStroke(path.toQPainterPath(false));
00138         QMatrix ms;
00139         ms.translate(Item1->xPos() - Item2->xPos(), Item1->yPos() - Item2->yPos());
00140         ms.rotate(Item1->rotation());
00141         cutter = ms.map(cutter);
00142         path.map(ms);
00143         FPoint start = path.point(0);
00144         FPoint end = path.point(path.size()-2);
00145         QMatrix mm;
00146         mm.rotate(Item2->rotation());
00147         QPainterPath objekt = mm.map(Item2->PoLine.toQPainterPath(true));
00148         if ((objekt.contains(QPointF(start.x(), start.y()))) || (objekt.contains(QPointF(end.x(), end.y()))))
00149         {
00150             QMessageBox::information(doc->scMW(), tr("Error"), tr("The cutting line must cross the polygon and\nboth end points must lie outside of the polygon"));
00151             return true;
00152         }
00153         QPainterPath result = objekt.subtracted(cutter);
00154         FPointArray points;
00155         points.fromQPainterPath(result);
00156         Item2->PoLine = points;
00157         Item2->Frame = false;
00158         Item2->ClipEdited = true;
00159         Item2->FrameType = 3;
00160         currDoc->AdjustItemSize(Item2);
00161         Item2->OldB2 = Item2->width();
00162         Item2->OldH2 = Item2->height();
00163         Item2->updateClip();
00164         Item2->ContourLine = Item2->PoLine.copy();
00165         currDoc->m_Selection->clear();
00166         currDoc->m_Selection->addItem(Item1);
00167         currDoc->itemSelection_DeleteItem();
00168         currDoc->m_Selection->clear();
00169         currDoc->m_Selection->addItem(Item2);
00170         currDoc->itemSelection_SplitItems();
00171         currDoc->changed();
00172     }
00173     return true;
00174 }
Created with personalized Doxygen. This is documentation for GPL software Scribus - Open Source Desktop Publishing.