1 /**
2  * \file GuiSetBorder.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Edwin Leuven
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11 
12 #include <config.h>
13 
14 #include "GuiSetBorder.h"
15 
16 #include <QPainter>
17 #include <QMouseEvent>
18 #include <QPaintEvent>
19 
20 
GuiSetBorder(QWidget * parent,Qt::WindowFlags fl)21 GuiSetBorder::GuiSetBorder(QWidget * parent, Qt::WindowFlags fl)
22 	: QWidget(parent, fl), buffer(75, 75)
23 {
24 	/* length of corner line */
25 	l = buffer.width() / 10;
26 	/* margin */
27 	m = buffer.height() / 10;
28 
29 	w = buffer.width();
30 	h = buffer.height();
31 
32 	init();
33 
34 	setMinimumSize(w,h);
35 	setMaximumSize(w,h);
36 }
37 
38 
paintEvent(QPaintEvent * e)39 void GuiSetBorder::paintEvent(QPaintEvent * e)
40 {
41 	QWidget::paintEvent(e);
42 	QPainter painter(this);
43 	painter.drawPixmap(0, 0, buffer);
44 }
45 
46 
init()47 void GuiSetBorder::init()
48 {
49 	buffer.fill();
50 	QPainter paint(&buffer);
51 
52 	paint.setPen(Qt::black);
53 
54 	// FIXME: wow, readable !! :)
55 
56 	paint.drawLine(m + l , m, m + l, m + l);
57 	paint.drawLine(w - (m + l), m, w - (m + l), m + l);
58 
59 	paint.drawLine(m, m + l , m + l, m + l);
60 	paint.drawLine(m, h - (m + l), m + l, h - (m + l));
61 
62 	paint.drawLine(m + l ,h - m, m + l ,h - (m + l));
63 	paint.drawLine(w - (m + l), h - m, w - (m + l), h - (m + l));
64 
65 	paint.drawLine(h - m, m+l, h - (m + l), m + l);
66 	paint.drawLine(h - m, h - (m + l), h - (m + l),h - (m + l));
67 }
68 
69 
mousePressEvent(QMouseEvent * e)70 void GuiSetBorder::mousePressEvent(QMouseEvent * e)
71 {
72 	if (e->y() > e->x()) {
73 		if (e->y() < height() - e->x()) {
74 			if (left_.enabled) {
75 				setLeft(left_.set == LINE_SET ? LINE_UNSET : LINE_SET);
76 				// emit signal
77 				leftSet();
78 			}
79 		} else {
80 			if (bottom_.enabled) {
81 				setBottom(bottom_.set == LINE_SET ? LINE_UNSET : LINE_SET);
82 				// emit signal
83 				bottomSet();
84 			}
85 		}
86 	} else {
87 		if (e->y() < height() - e->x()) {
88 			if (top_.enabled) {
89 				setTop(top_.set == LINE_SET ? LINE_UNSET : LINE_SET);
90 				// emit signal
91 				topSet();
92 			}
93 		} else {
94 			if (right_.enabled) {
95 				setRight(right_.set == LINE_SET ? LINE_UNSET : LINE_SET);
96 				// emit signal
97 				rightSet();
98 			}
99 		}
100 	}
101 	update();
102 	// emit signal
103 	clicked();
104 }
105 
106 
drawLine(QColor const & col,int x,int y,int x2,int y2)107 void GuiSetBorder::drawLine(QColor const & col, int x, int y, int x2, int y2)
108 {
109 	QPainter paint(&buffer);
110 	QPen p = paint.pen();
111 	p.setWidth(2);
112 	p.setColor(col);
113 	paint.setPen(p);
114 	paint.drawLine(x, y, x2, y2);
115 }
116 
117 
drawLeft(BorderState draw)118 void GuiSetBorder::drawLeft(BorderState draw)
119 {
120 	QColor col;
121 	switch (draw) {
122 	case LINE_SET:
123 		col = Qt::black;
124 		break;
125 	case LINE_UNSET:
126 		col = Qt::white;
127 		break;
128 	case LINE_UNDECIDED:
129 	case LINE_UNDEF:
130 		col = Qt::lightGray;
131 		break;
132 	}
133 	if (!left_.enabled)
134 		col = QColor(Qt::lightGray);
135 	drawLine(col, m + l, m + l + 2, m + l, h - m - l - 1);
136 }
137 
138 
drawRight(BorderState draw)139 void GuiSetBorder::drawRight(BorderState draw)
140 {
141 	QColor col;
142 	switch (draw) {
143 	case LINE_SET:
144 		col = Qt::black;
145 		break;
146 	case LINE_UNSET:
147 		col = Qt::white;
148 		break;
149 	case LINE_UNDECIDED:
150 	case LINE_UNDEF:
151 		col = Qt::lightGray;
152 		break;
153 	}
154 	if (!right_.enabled)
155 		col = QColor(Qt::lightGray);
156 	drawLine(col, h - m - l + 1, m + l + 2, h - m - l + 1, h - m - l - 1);
157 }
158 
159 
drawTop(BorderState draw)160 void GuiSetBorder::drawTop(BorderState draw)
161 {
162 	QColor col;
163 	switch (draw) {
164 	case LINE_SET:
165 		col = Qt::black;
166 		break;
167 	case LINE_UNSET:
168 		col = Qt::white;
169 		break;
170 	case LINE_UNDECIDED:
171 	case LINE_UNDEF:
172 		col = Qt::lightGray;
173 		break;
174 	}
175 	if (!top_.enabled)
176 		col = QColor(Qt::lightGray);
177 	drawLine(col, m + l + 2, m + l, w - m - l - 1, m + l);
178 }
179 
180 
drawBottom(BorderState draw)181 void GuiSetBorder::drawBottom(BorderState draw)
182 {
183 	QColor col;
184 	switch (draw) {
185 	case LINE_SET:
186 		col = Qt::black;
187 		break;
188 	case LINE_UNSET:
189 		col = Qt::white;
190 		break;
191 	case LINE_UNDECIDED:
192 	case LINE_UNDEF:
193 		col = Qt::lightGray;
194 		break;
195 	}
196 	if (!bottom_.enabled)
197 		col = QColor(Qt::lightGray);
198 	drawLine(col, m + l + 2, w - m - l + 1, w - m - l - 1, w - m - l + 1);
199 }
200 
201 
setLeftEnabled(bool enabled)202 void GuiSetBorder::setLeftEnabled(bool enabled)
203 {
204 	left_.enabled = enabled;
205 	drawLeft(left_.set);
206 }
207 
208 
setRightEnabled(bool enabled)209 void GuiSetBorder::setRightEnabled(bool enabled)
210 {
211 	right_.enabled = enabled;
212 	drawRight(right_.set);
213 }
214 
215 
setTopEnabled(bool enabled)216 void GuiSetBorder::setTopEnabled(bool enabled)
217 {
218 	top_.enabled = enabled;
219 	drawTop(top_.set);
220 }
221 
222 
setBottomEnabled(bool enabled)223 void GuiSetBorder::setBottomEnabled(bool enabled)
224 {
225 	bottom_.enabled = enabled;
226 	drawBottom(bottom_.set);
227 }
228 
229 
setLeft(BorderState border)230 void GuiSetBorder::setLeft(BorderState border)
231 {
232 	left_.set = border;
233 	drawLeft(border);
234 }
235 
236 
setRight(BorderState border)237 void GuiSetBorder::setRight(BorderState border)
238 {
239 	right_.set = border;
240 	drawRight(border);
241 }
242 
243 
setTop(BorderState border)244 void GuiSetBorder::setTop(BorderState border)
245 {
246 	top_.set = border;
247 	drawTop(border);
248 }
249 
250 
setBottom(BorderState border)251 void GuiSetBorder::setBottom(BorderState border)
252 {
253 	bottom_.set = border;
254 	drawBottom(border);
255 }
256 
257 
setAll(BorderState border)258 void GuiSetBorder::setAll(BorderState border)
259 {
260 	setLeft(border);
261 	setRight(border);
262 	setTop(border);
263 	setBottom(border);
264 }
265 
266 
getLeft()267 GuiSetBorder::BorderState GuiSetBorder::getLeft()
268 {
269 	return left_.set;
270 }
271 
272 
getRight()273 GuiSetBorder::BorderState GuiSetBorder::getRight()
274 {
275 	return right_.set;
276 }
277 
278 
getTop()279 GuiSetBorder::BorderState GuiSetBorder::getTop()
280 {
281 	return top_.set;
282 }
283 
284 
getBottom()285 GuiSetBorder::BorderState GuiSetBorder::getBottom()
286 {
287 	return bottom_.set;
288 }
289 
290 #include "moc_GuiSetBorder.cpp"
291