File indexing completed on 2024-04-28 04:31:55

0001 /*
0002  * Copyright (C) 2010-2015 by Stephen Allewell
0003  * steve.allewell@gmail.com
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 /** @file
0012  * This file defines an overlay rectangle with corner nodes to be used on top
0013  * of an element on the print layout pages allowing the user to change the size
0014  * and position of the underlying element.
0015  */
0016 
0017 #ifndef Boundary_H
0018 #define Boundary_H
0019 
0020 // Qt includes
0021 #include <QPoint>
0022 #include <QRect>
0023 
0024 // Forward declaration of Qt classes
0025 class QPainter;
0026 
0027 // Forward declaration of application classes
0028 class Element;
0029 
0030 /**
0031  * This class defines a rectangle around a print element allowing the user
0032  * to move the element or resize it by dragging the nodes.
0033  */
0034 class Boundary
0035 {
0036 public:
0037     /**
0038      * Constructor
0039      */
0040     Boundary();
0041 
0042     /**
0043      * Get the pointer to the associated element.
0044      *
0045      * @return a pointer to the Element instance
0046      */
0047     Element *element() const;
0048 
0049     /**
0050      * Get the node that is nearest to the supplied point within the defined
0051      * snap distance.
0052      *
0053      * @param point is a const reference to a QPoint containing the supplied point
0054      *
0055      * @return a const pointer to the closest node, a nullptr is returned if no nodes are within the snap distance
0056      */
0057     const QPoint *node(const QPoint &point) const;
0058 
0059     /**
0060      * Get the rectangle outline of the boundary.
0061      *
0062      * @return a QRect defining the position and size of the boundary
0063      */
0064     QRect rectangle() const;
0065 
0066     /**
0067      * Get the cursor shape relating to the node specified.
0068      *
0069      * @param node is a const pointer to the node to provide the cursor for
0070      *
0071      * @return a Qt::CursorShape dependant on which corner of the boundary is pointed to
0072      */
0073     Qt::CursorShape cursor(const QPoint *node);
0074 
0075     /**
0076      * Check if the boundary is valid in that a element is assigned and the boundary
0077      * element rectangle is correcly defined.
0078      *
0079      * @return @c true if the boundary is valid, @c false otherwise
0080      */
0081     bool isValid() const;
0082 
0083     /**
0084      * Set the element to be associated with the boundary.
0085      *
0086      * @param element is a pointer to the printer element
0087      */
0088     void setElement(Element *element);
0089 
0090     /**
0091      * Set the rectangle position and size for the assigned Element.
0092      *
0093      * @param rectangle is a const reference to a QRect defining the boundary
0094      */
0095     void setRectangle(const QRect &rectangle);
0096 
0097     /**
0098      * Move a node to a new position updating the other corner nodes to suit.
0099      *
0100      * @param node is a const pointer to a QPoint representing the node to move
0101      * @param point is a const reference to a QPoint containing the new position
0102      */
0103     void moveNode(const QPoint *node, const QPoint &point);
0104 
0105     /**
0106      * Render the boundary using the supplied QPainter object. The caller must
0107      * initialise the QPainter appropriately.
0108      *
0109      * @param painter is a pointer to the QPainter to draw with
0110      */
0111     void render(QPainter *painter);
0112 
0113 private:
0114     Element *m_element; /**< A pointer to the element the boundary is applied to */
0115     QPoint m_nodes[4]; /**< An array of corner nodes, topLeft, topRight, bottomRight and bottomLeft */
0116     QRect m_rectangle; /**< The rectangle defining the boundary size and position */
0117 };
0118 
0119 #endif // Boundary_H