1 /**************************************************************************
2 * Otter Browser: Web browser controlled by the user, not vice-versa.
3 * Copyright (C) 2013 - 2018 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 "TextLabelWidget.h"
21 #include "../core/ThemesManager.h"
22 
23 #include <QtGui/QClipboard>
24 #include <QtGui/QDesktopServices>
25 #include <QtGui/QMouseEvent>
26 #include <QtWidgets/QApplication>
27 #include <QtWidgets/QMenu>
28 #include <QtWidgets/QStyle>
29 #include <QtWidgets/QStyleOptionFrame>
30 
31 namespace Otter
32 {
33 
TextLabelWidget(QWidget * parent)34 TextLabelWidget::TextLabelWidget(QWidget *parent) : QLineEdit(parent),
35 	m_isEmpty(true)
36 {
37 	updateStyle();
38 	setFrame(false);
39 	setReadOnly(true);
40 	setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
41 	setStyleSheet(QLatin1String("QLineEdit {background:transparent;}"));
42 	QLineEdit::setText(tr("<empty>"));
43 }
44 
mousePressEvent(QMouseEvent * event)45 void TextLabelWidget::mousePressEvent(QMouseEvent *event)
46 {
47 	QLineEdit::mousePressEvent(event);
48 
49 	m_dragStartPosition = event->pos();
50 }
51 
mouseReleaseEvent(QMouseEvent * event)52 void TextLabelWidget::mouseReleaseEvent(QMouseEvent *event)
53 {
54 	QLineEdit::mouseReleaseEvent(event);
55 
56 	if (m_url.isValid() && (event->pos() - m_dragStartPosition).manhattanLength() < QApplication::startDragDistance())
57 	{
58 		QDesktopServices::openUrl(m_url);
59 	}
60 }
61 
contextMenuEvent(QContextMenuEvent * event)62 void TextLabelWidget::contextMenuEvent(QContextMenuEvent *event)
63 {
64 	if (m_isEmpty)
65 	{
66 		return;
67 	}
68 
69 	QMenu menu(this);
70 	menu.addAction(ThemesManager::createIcon(QLatin1String("edit-copy")), tr("Copy"), this, &TextLabelWidget::copy, QKeySequence(QKeySequence::Copy))->setEnabled(hasSelectedText());
71 
72 	if (m_url.isValid())
73 	{
74 		menu.addAction(tr("Copy Link Location"), this, &TextLabelWidget::copyUrl);
75 	}
76 
77 	menu.addSeparator();
78 	menu.addAction(ThemesManager::createIcon(QLatin1String("edit-select-all")), tr("Select All"), this, &TextLabelWidget::selectAll, QKeySequence(QKeySequence::SelectAll))->setEnabled(!text().isEmpty());
79 	menu.exec(event->globalPos());
80 }
81 
copyUrl()82 void TextLabelWidget::copyUrl()
83 {
84 	QGuiApplication::clipboard()->setText(m_url.toString(QUrl::EncodeReserved | QUrl::EncodeSpaces));
85 }
86 
clear()87 void TextLabelWidget::clear()
88 {
89 	QLineEdit::setText(tr("<empty>"));
90 
91 	m_url.clear();
92 	m_isEmpty = true;
93 
94 	updateStyle();
95 }
96 
updateStyle()97 void TextLabelWidget::updateStyle()
98 {
99 	QFont font(this->font());
100 	font.setUnderline(m_url.isValid() && style()->styleHint(QStyle::SH_UnderlineShortcut) > 0);
101 
102 	QPalette palette(this->palette());
103 
104 	if (m_isEmpty)
105 	{
106 		palette.setColor(QPalette::Text, QGuiApplication::palette().color(QPalette::Disabled, QPalette::WindowText));
107 	}
108 	else
109 	{
110 		palette.setColor(QPalette::Text, QGuiApplication::palette().color(m_url.isValid() ? QPalette::Link : QPalette::WindowText));
111 	}
112 
113 	setCursor(m_url.isValid() ? Qt::PointingHandCursor : Qt::ArrowCursor);
114 	setFont(font);
115 	setPalette(palette);
116 }
117 
setText(const QString & text)118 void TextLabelWidget::setText(const QString &text)
119 {
120 	if (text != this->text())
121 	{
122 		if (text.isEmpty() != m_isEmpty)
123 		{
124 			m_isEmpty = text.isEmpty();
125 
126 			updateStyle();
127 		}
128 
129 		QLineEdit::setText(m_isEmpty ? tr("<empty>") : text);
130 		setCursorPosition(0);
131 		updateGeometry();
132 	}
133 
134 	setReadOnly(true);
135 }
136 
setUrl(const QUrl & url)137 void TextLabelWidget::setUrl(const QUrl &url)
138 {
139 	m_url = url;
140 	m_isEmpty = url.isEmpty();
141 
142 	updateStyle();
143 }
144 
sizeHint() const145 QSize TextLabelWidget::sizeHint() const
146 {
147 	const QMargins margins(textMargins());
148 	QSize size(fontMetrics().size(Qt::TextSingleLine, text()));
149 	size.setWidth(size.width() + margins.left() + margins.right() + 6);
150 	size.setHeight(size.height() + margins.top() + margins.bottom());
151 
152 	QStyleOptionFrame option;
153 
154 	initStyleOption(&option);
155 
156 	return style()->sizeFromContents(QStyle::CT_LineEdit, &option, size, this);
157 }
158 
event(QEvent * event)159 bool TextLabelWidget::event(QEvent *event)
160 {
161 	const bool result(QLineEdit::event(event));
162 
163 	switch (event->type())
164 	{
165 		case QEvent::ApplicationPaletteChange:
166 		case QEvent::StyleChange:
167 			updateStyle();
168 
169 			break;
170 		case QEvent::LanguageChange:
171 			if (m_isEmpty)
172 			{
173 				QLineEdit::setText(tr("<empty>"));
174 			}
175 
176 			break;
177 		default:
178 			break;
179 	}
180 
181 	return result;
182 }
183 
184 }
185