File indexing completed on 2024-02-18 05:43:37

0001 /***************************************************************************
0002  *   Copyright (C) 2003-2004 by David Saxton                               *
0003  *   david@bluehaze.org                                                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  ***************************************************************************/
0010 
0011 #ifndef CONROUTER_H
0012 #define CONROUTER_H
0013 
0014 #include "cells.h"
0015 
0016 #include <QList>
0017 #include <QPoint>
0018 
0019 class ICNDocument;
0020 class Cell;
0021 
0022 typedef QList<QPoint> QPointList;
0023 typedef QList<QPointList> QPointListList;
0024 
0025 /**
0026 Abstraction for the routing of a connector.
0027 
0028 NB: As a general rule of thumb, the point references stored as members of this
0029 class are in Cell-space (i.e. 8^2 x smaller than Canvas-space), and the
0030 interfacing functions take or give point references in Canvas-space (unless
0031 otherwise indicated).
0032 
0033 @author David Saxton
0034 */
0035 class ConRouter
0036 {
0037 public:
0038     ConRouter(ICNDocument *cv);
0039     ~ConRouter();
0040 
0041     /**
0042      * What this class is all about - finding a route, from (sx,sy) to (ex,ey).
0043      */
0044     void mapRoute(int sx, int sy, int ex, int ey);
0045     /**
0046      * Translates the precalculated routepoints by the given amount
0047      */
0048     void translateRoute(int dx, int dy);
0049     /**
0050      * Sets the route to the given canvas points
0051      * @param reverse if true, the points in pointList will be reversed
0052      */
0053     void setPoints(const QPointList &pointList, bool reverse = false);
0054     /**
0055      * Sets the route to the given route points
0056      */
0057     void setRoutePoints(const QPointList &pointList);
0058     /**
0059      * @returns true if the start or end points differ from that of the current route
0060      */
0061     bool needsRouting(int sx, int sy, int ex, int ey) const;
0062     /**
0063      * Returns the list of canvas points
0064      */
0065     QPointList pointList(bool reverse) const;
0066     /**
0067      * Returns a pointer to the internall cellPointList
0068      */
0069     QPointList *cellPointList()
0070     {
0071         return &m_cellPointList;
0072     }
0073     /**
0074      * This will return two lists of Canvas points from the splitting of the
0075      * route at the Canvas point "pos". The internall stored points are not
0076      * affected.
0077      */
0078     QPointListList splitPoints(const QPoint &pos) const;
0079     /**
0080      * This will return a list of Canvas pointLists from the route, divided
0081      * into n parts (at n-1 equally spaced places).
0082      */
0083     QPointListList dividePoints(uint n) const;
0084 
0085 protected:
0086     /**
0087      * Check a line of the ICNDocument cells for a valid route
0088      */
0089     bool checkLineRoute(int scx, int scy, int ecx, int ecy, int maxConScore, int maxCIScore);
0090     void checkACell(int x, int y, Cell *prev, int prevX, int prevY, int nextScore);
0091     void checkCell(int x, int y); // Gets the shortest route from the final cell
0092     /**
0093      * Remove duplicated points from the route
0094      */
0095     void removeDuplicatePoints();
0096 
0097     int xcells, ycells;
0098     int m_lcx, m_lcy; // Last x / y from mapRoute, if we need a point on the route
0099     Cells *cellsPtr;
0100     TempLabelMap tempLabels;
0101     ICNDocument *p_icnDocument;
0102     QPointList m_cellPointList;
0103 };
0104 
0105 #endif