File indexing completed on 2024-05-12 16:34:24

0001 /* This file is part of the KDE project
0002    Copyright 2011 Silvio Heinrich <plassy@web.de>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #ifndef H_SELECTION_RECT_H
0021 #define H_SELECTION_RECT_H
0022 
0023 #include <QRectF>
0024 #include <QPointF>
0025 #include <QSizeF>
0026 #include <limits>
0027 
0028 /**
0029  * This class represents a rectangle that can be moved and resized
0030  * inbetween a constraining rectangle.
0031  */
0032 class SelectionRect
0033 {
0034 public:
0035     typedef int HandleFlags;
0036 
0037     enum PositionInfo
0038     {
0039         INSIDE_RECT = 0x01,
0040         TOP_HANDLE = 0x02,
0041         BOTTOM_HANDLE = 0x04,
0042         LEFT_HANDLE = 0x08,
0043         RIGHT_HANDLE = 0x10,
0044         TOP_LEFT_HANDLE = TOP_HANDLE|LEFT_HANDLE,
0045         TOP_RIGHT_HANDLE = TOP_HANDLE|RIGHT_HANDLE,
0046         BOTTOM_LEFT_HANDLE = BOTTOM_HANDLE|LEFT_HANDLE,
0047         BOTTOM_RIGHT_HANDLE = BOTTOM_HANDLE|RIGHT_HANDLE
0048     };
0049 
0050     explicit SelectionRect(const QRectF &rect = QRectF(), qreal handleSize = 10.0);
0051 
0052     void setRect(const QRectF &rect);
0053     void setHandleSize(qreal size);
0054     void setAspectRatio(qreal aspect);
0055     void setConstrainingRect(const QRectF &rect);
0056     void setConstrainingAspectRatio(qreal aspect);
0057     bool beginDragging(const QPointF &pos);
0058     void doDragging(const QPointF &pos);
0059     void finishDragging();
0060 
0061     int getNumHandles() const { return 8; }
0062     QRectF getRect() const { return m_rect; }
0063 
0064     HandleFlags getHandleFlags(const QPointF &pos) const;
0065     HandleFlags getHandleFlags(int handleIndex) const;
0066     QRectF getHandleRect(HandleFlags handle) const;
0067 
0068 private:
0069     void fixAspect(HandleFlags handle);
0070 
0071 private:
0072     QPointF m_tempPos;
0073     QRectF m_rect;
0074     qreal m_aspectRatio;
0075     qreal m_lConstr;
0076     qreal m_rConstr;
0077     qreal m_tConstr;
0078     qreal m_bConstr;
0079     qreal m_aConstr;
0080     qreal m_handleSize;
0081     HandleFlags m_currentHandle;
0082 };
0083 
0084 #endif // H_SELECTION_RECT_H