1 /**************************************************************************
2 * Otter Browser: Web browser controlled by the user, not vice-versa.
3 * Copyright (C) 2016 - 2017 Michal Dutkiewicz aka Emdek <michal@emdek.pl>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 **************************************************************************/
19 
20 #include "MacPlatformStyle.h"
21 
22 #include <QtGui/QPainter>
23 #include <QtWidgets/QStyleOption>
24 
25 namespace Otter
26 {
27 
MacPlatformStyle(const QString & name)28 MacPlatformStyle::MacPlatformStyle(const QString &name) : Style(name)
29 {
30 }
31 
drawControl(ControlElement element,const QStyleOption * option,QPainter * painter,const QWidget * widget) const32 void MacPlatformStyle::drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const
33 {
34 	switch (element)
35 	{
36 		case CE_TabBarTab:
37 			{
38 				const QStyleOptionTab *tabOption(qstyleoption_cast<const QStyleOptionTab*>(option));
39 
40 				if (tabOption && tabOption->documentMode)
41 				{
42 					painter->save();
43 
44 					if (tabOption->position == QStyleOptionTab::Beginning || tabOption->position == QStyleOptionTab::Middle)
45 					{
46 						painter->setPen(QColor(100, 100, 100, 200));
47 						painter->drawLine(option->rect.topRight(), option->rect.bottomRight());
48 					}
49 
50 					if (option->state.testFlag(State_MouseOver))
51 					{
52 						painter->fillRect(option->rect, QColor(0, 0, 0, (option->state.testFlag(State_Selected) ? 80 : 50)));
53 					}
54 
55 					painter->restore();
56 
57 					return;
58 				}
59 			}
60 
61 			break;
62 		case CE_ToolBar:
63 			QProxyStyle::drawControl(element, option, painter, widget);
64 
65 			return;
66 		default:
67 			break;
68 	}
69 
70 	Style::drawControl(element, option, painter, widget);
71 }
72 
drawPrimitive(PrimitiveElement element,const QStyleOption * option,QPainter * painter,const QWidget * widget) const73 void MacPlatformStyle::drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const
74 {
75 	switch (element)
76 	{
77 		case PE_IndicatorTabClose:
78 			if (widget && widget->underMouse())
79 			{
80 				QRect rectangle(option->rect);
81 
82 				if (rectangle.width() > 8)
83 				{
84 					const int offset((8 - rectangle.width()) / 2);
85 
86 					rectangle = rectangle.marginsAdded(QMargins(offset, offset, offset, offset));
87 				}
88 
89 				QPen pen(QColor(60, 60, 60));
90 				pen.setWidthF(1.25);
91 
92 				painter->save();
93 				painter->setRenderHint(QPainter::Antialiasing);
94 				painter->setPen(pen);
95 				painter->drawLine(rectangle.topLeft(), rectangle.bottomRight());
96 				painter->drawLine(rectangle.topRight(), rectangle.bottomLeft());
97 				painter->restore();
98 			}
99 
100 			return;
101 		case PE_PanelStatusBar:
102 			QProxyStyle::drawPrimitive(element, option, painter, widget);
103 
104 			return;
105 		case PE_FrameTabBarBase:
106 			{
107 				const QStyleOptionTabBarBase *tabBarBaseOption(qstyleoption_cast<const QStyleOptionTabBarBase*>(option));
108 
109 				if (tabBarBaseOption && tabBarBaseOption->documentMode)
110 				{
111 					painter->save();
112 					painter->setPen(QColor(100, 100, 100, 200));
113 
114 					if (tabBarBaseOption->selectedTabRect.isValid())
115 					{
116 						QRect leftRectangle(tabBarBaseOption->tabBarRect);
117 						leftRectangle.setRight(tabBarBaseOption->selectedTabRect.left());
118 						leftRectangle.setLeft(option->rect.left());
119 
120 						QRect rightRectangle(tabBarBaseOption->tabBarRect);
121 						rightRectangle.setLeft(tabBarBaseOption->selectedTabRect.right());
122 						rightRectangle.setRight(option->rect.right());
123 
124 						painter->fillRect(leftRectangle, QColor(0, 0, 0, 50));
125 						painter->fillRect(rightRectangle, QColor(0, 0, 0, 50));
126 					}
127 					else
128 					{
129 						painter->fillRect(tabBarBaseOption->tabBarRect, QColor(0, 0, 0, 50));
130 					}
131 
132 					painter->drawLine(QPoint(option->rect.left(), tabBarBaseOption->tabBarRect.top()), QPoint(option->rect.right(), tabBarBaseOption->tabBarRect.top()));
133 					painter->drawLine(QPoint(option->rect.left(), tabBarBaseOption->tabBarRect.bottom()), QPoint(option->rect.right(), tabBarBaseOption->tabBarRect.bottom()));
134 
135 					if (tabBarBaseOption->tabBarRect.left() != option->rect.left())
136 					{
137 						painter->drawLine(tabBarBaseOption->tabBarRect.topLeft(), tabBarBaseOption->tabBarRect.bottomLeft());
138 					}
139 
140 					if (tabBarBaseOption->tabBarRect.right() != option->rect.right())
141 					{
142 						painter->drawLine(tabBarBaseOption->tabBarRect.topRight(), tabBarBaseOption->tabBarRect.bottomRight());
143 					}
144 
145 					painter->restore();
146 
147 					return;
148 				}
149 			}
150 
151 			break;
152 		default:
153 			break;
154 	}
155 
156 	Style::drawPrimitive(element, option, painter, widget);
157 }
158 
getStyleSheet() const159 QString MacPlatformStyle::getStyleSheet() const
160 {
161 	return QLatin1String("QToolBar QLineEdit {border:1px solid #AFAFAF;border-radius:4px;}");
162 }
163 
pixelMetric(PixelMetric metric,const QStyleOption * option,const QWidget * widget) const164 int MacPlatformStyle::pixelMetric(PixelMetric metric, const QStyleOption *option, const QWidget *widget) const
165 {
166 	if (metric == PM_ToolBarIconSize)
167 	{
168 		return 16;
169 	}
170 
171 	return Style::pixelMetric(metric, option, widget);
172 }
173 
174 }
175