1 /*
2 Copyright (C) 2011 Elvis Stansvik <elvstone@gmail.com>
3 
4 For general Scribus (>=1.3.2) copyright and licensing information please refer
5 to the COPYING file provided with the program. Following this notice may exist
6 a copyright and/or license notice that predates the release of Scribus 1.3.2
7 for which a new license (GPL+exception) is in place.
8 */
9 #ifndef TABLESIDESELECTOR_H
10 #define TABLESIDESELECTOR_H
11 
12 #include <QFlags>
13 #include <QPen>
14 #include <QList>
15 #include <QFrame>
16 
17 class QPaintEvent;
18 class QMouseEvent;
19 class QEvent;
20 
21 /**
22  * Simple square shaped widget for picking different sides of a cell or table.
23  *
24  * Used when editing cell and table borders.
25  */
26 class TableSideSelector : public QFrame
27 {
28 	Q_OBJECT
29 
30 	Q_PROPERTY(Sides selection READ selection WRITE setSelection RESET clearSelection NOTIFY selectionChanged)
31 	Q_PROPERTY(Style style READ style WRITE setStyle)
32 
33 public:
34 	/**
35 	 * This enum describes the sides that can be selected. A selection can be
36 	 * expressed as an ORed combination of Left, Right, Top and Bottom.
37 	 */
38 	enum Side
39 	{
40 		None = 0,   /**< None of the sides are selected. */
41 		Left = 1,   /**< The left side is selected. */
42 		Right = 2,  /**< The right side is selected. */
43 		Top = 4,    /**< The top side is selected. */
44 		Bottom = 8, /**< The bottom side is selected. */
45 		All = Left | Right | Top | Bottom
46 	};
47 	Q_DECLARE_FLAGS(Sides, Side)
48 
49 	/**
50 	 * This enum describes two possible styles for the selector. If the cell style is used,
51 	 * the selector will resemble a cell. In the table style, a table.
52 	 */
53 	enum Style
54 	{
55 		CellStyle,  /**< The selector will resemble a cell. */
56 		TableStyle, /**< The selector will resemble table. */
57 	};
58 
59 	/// Creates a new side selector with all sides selected.
60 	TableSideSelector(QWidget* parent);
61 
62 	/// Returns the current selection as an ORed combination of enum flags.
selection()63 	Sides selection() const { return m_selection; }
64 
65 	/// Returns the current selection as a list of enum values.
66 	QList<TableSideSelector::Side> selectionList() const;
67 
68 	/// Sets the current selection to @a selection.
setSelection(Sides selection)69 	void setSelection(Sides selection) { m_selection = selection; emit selectionChanged(); }
70 
71 	/// Clears the current selection.
clearSelection()72 	void clearSelection() { m_selection = None; emit selectionChanged(); }
73 
74 	/// Returns the current style of the selector.
style()75 	Style style() const { return m_style; }
76 
77 	/// Sets the style of the selector to @a style
setStyle(Style style)78 	void setStyle(Style style) { m_style = style; }
79 
80 	/// Returns the size hint of the table side selector.
sizeHint()81 	QSize sizeHint() const { return QSize(80, 80); }
82 
83 	/// Returns @a width, as the selector is supposed to be square shaped.
heightForWidth(int width)84 	int heightForWidth(int width) const { return width; }
85 
86 signals:
87 	/// Emitted when the selections has changed.
88 	void selectionChanged();
89 
90 protected:
91 	/// Paints the table side selector.
92 	void paintEvent(QPaintEvent* event);
93 	/// Toggles selection of the edge closest to mouse pointer.
94 	void mousePressEvent(QMouseEvent* event);
95 	/// Highlights the edge closest to mouse pointer.
96 	void mouseMoveEvent(QMouseEvent* event);
97 	/// Removes any highlighted edge.
98 	void leaveEvent(QEvent* event);
99 
100 private:
101 	/// Utility function to return the side closest to @a point.
102 	Side closestSide(const QPointF& point) const;
103 
104 private:
105 	/// The current selection.
106 	Sides m_selection;
107 
108 	/// The current style of the selector.
109 	Style m_style;
110 
111 	/// The side currently closest to the mouse pointer.
112 	Side m_highlighted;
113 
114 	/// The left side line.
115 	QLineF m_left;
116 
117 	/// The right side line.
118 	QLineF m_right;
119 
120 	/// The top side line.
121 	QLineF m_top;
122 
123 	/// The bottom side line.
124 	QLineF m_bottom;
125 };
126 
127 Q_DECLARE_OPERATORS_FOR_FLAGS(TableSideSelector::Sides)
128 
129 #endif // TABLESIDESELECTOR_H
130