42#include <QStringListModel>
43#include <QContextMenuEvent>
44#include <QStyleOptionFrame>
52 , m_currentTextLength(0)
54 , m_progressVisible(false)
56 setObjectName(
"locationbar");
60 setProperty(
"_kde_no_animations", QVariant(
true));
63 m_goIcon =
new GoIcon(
this);
84 m_domainCompleterModel =
new QStringListModel(
this);
85 auto* domainCompleter =
new QCompleter(
this);
86 domainCompleter->setCompletionMode(QCompleter::InlineCompletion);
87 domainCompleter->setModel(m_domainCompleterModel);
88 setCompleter(domainCompleter);
90 m_progressTimer =
new QTimer(
this);
91 m_progressTimer->setInterval(700);
92 m_progressTimer->setSingleShot(
true);
93 connect(m_progressTimer, &QTimer::timeout,
this, &LocationBar::hideProgress);
99 connect(
this, SIGNAL(textEdited(QString)),
this, SLOT(textEdited(QString)));
111 m_goIcon->setVisible(
qzSettings->alwaysShowGoIcon);
112 m_autofillIcon->hide();
114 QTimer::singleShot(0,
this, &LocationBar::updatePlaceHolderText);
143 connect(m_webView, &QWebEngineView::loadStarted,
this, &LocationBar::loadStarted);
144 connect(m_webView, &QWebEngineView::loadProgress,
this, &LocationBar::loadProgress);
145 connect(m_webView, &QWebEngineView::loadFinished,
this, &LocationBar::loadFinished);
152 m_oldTextLength = text.length();
153 m_currentTextLength = m_oldTextLength;
155 LineEdit::setText(text);
160void LocationBar::updatePlaceHolderText()
163 setPlaceholderText(tr(
"Enter address or search with %1").arg(
searchEngine().name));
165 setPlaceholderText(tr(
"Enter address"));
168void LocationBar::showCompletion(
const QString &completion,
bool completeDomain)
170 LineEdit::setText(completion);
175 if (completeDomain) {
176 completer()->complete();
182void LocationBar::clearCompletion()
184 m_webView->setFocus();
188void LocationBar::showDomainCompletion(
const QString &completion)
190 m_domainCompleterModel->setStringList(QStringList() << completion);
194 if (!completion.isEmpty() && m_oldTextLength < m_currentTextLength)
195 completer()->complete();
202 if (url.scheme().isEmpty() || url.scheme() ==
QL1S(
"javascript")) {
203 return QUrl::fromPercentEncoding(url.toEncoded());
208 if (stringUrl ==
QL1S(
"falkon:speeddial") || stringUrl ==
QL1S(
"about:blank")) {
219 }
else if (
qzSettings->searchWithDefaultEngine) {
220 return mApp->searchEnginesManager()->defaultEngine();
222 return mApp->searchEnginesManager()->activeEngine();
230 const QString &
t = text.trimmed();
237 const int firstSpacePos =
t.indexOf(QLatin1Char(
' '));
238 if (
qzSettings->searchFromAddressBar && firstSpacePos != -1) {
239 const QString shortcut =
t.left(firstSpacePos);
240 const QString searchedString =
t.mid(firstSpacePos).trimmed();
242 SearchEngine en =
mApp->searchEnginesManager()->engineForShortcut(shortcut);
246 action.
loadRequest =
mApp->searchEnginesManager()->searchResult(en, searchedString);
252 const QList<BookmarkItem*> items =
mApp->bookmarks()->searchKeyword(
t);
253 if (!items.isEmpty()) {
262 const QUrl &guessedUrl = QUrl::fromUserInput(
t);
263 if (guessedUrl.isValid()) {
271 if (
t !=
QL1S(
"localhost")
273 && !
t.contains(
QL1C(
'.'))
274 && !
t.contains(
QL1C(
':'))
275 && !
t.contains(
QL1C(
'/'))
284 const QUrl guessedUrl = QUrl::fromUserInput(
t);
285 if (guessedUrl.isValid()) {
287 const bool forceLoad = guessedUrl.scheme() ==
QL1S(
"javascript");
306void LocationBar::refreshTextFormat()
313 const QString hostName = m_webView->url().isEmpty() ? QUrl(text()).host() : m_webView->url().host();
315 if (!hostName.isEmpty()) {
316 const int hostPos = text().indexOf(hostName);
319 QTextCharFormat format;
320 format.setForeground(
Colors::mid(palette().color(QPalette::Base), palette().color(QPalette::Text), 1, 1));
322 QTextLayout::FormatRange schemePart;
323 schemePart.start = 0;
324 schemePart.length = hostPos;
325 schemePart.format = format;
327 QTextLayout::FormatRange hostPart;
328 hostPart.start = hostPos;
329 hostPart.length = hostName.size();
331 QTextLayout::FormatRange remainingPart;
332 remainingPart.start = hostPos + hostName.size();
333 remainingPart.length = text().size() - remainingPart.start;
334 remainingPart.format = format;
336 textFormat.append(schemePart);
337 textFormat.append(hostPart);
338 textFormat.append(remainingPart);
345void LocationBar::requestLoadUrl()
351void LocationBar::textEdited(
const QString &text)
353 m_oldTextLength = m_currentTextLength;
354 m_currentTextLength = text.length();
356 if (!text.isEmpty()) {
358 m_siteIcon->
setIcon(QIcon::fromTheme(
QSL(
"edit-find"), QIcon(
QSL(
":icons/menu/search-icon.svg"))));
364 setGoIconVisible(
true);
367void LocationBar::setGoIconVisible(
bool state)
370 m_bookmarkIcon->hide();
375 m_bookmarkIcon->show();
388 if (hasFocus() || url.isEmpty()) {
394 if (text() == stringUrl) {
418 m_webView->setFocus();
420 if (urlString != text()) {
427void LocationBar::updateSiteIcon()
430 m_siteIcon->
setIcon(QIcon::fromTheme(
QSL(
"edit-find"), QIcon(
QSL(
":icons/menu/search-icon.svg"))));
433 if (property(
"secured").toBool()) {
434 icon = QIcon::fromTheme(
QSL(
"document-encrypted"), icon);
436 m_siteIcon->
setIcon(QIcon(icon.pixmap(16)));
440void LocationBar::setPrivacyState(
bool state)
442 m_siteIcon->setProperty(
"secured", QVariant(
state));
443 m_siteIcon->style()->unpolish(m_siteIcon);
444 m_siteIcon->style()->polish(m_siteIcon);
446 setProperty(
"secured", QVariant(
state));
447 style()->unpolish(
this);
448 style()->polish(
this);
453void LocationBar::pasteAndGo()
460void LocationBar::contextMenuEvent(QContextMenuEvent* event)
463 menu->setAttribute(Qt::WA_DeleteOnClose);
466 QPoint pos =
event->globalPos();
467 pos.setY(pos.y() + 1);
471void LocationBar::showEvent(QShowEvent* event)
473 LineEdit::showEvent(
event);
478void LocationBar::focusInEvent(QFocusEvent* event)
484 if (stringUrl != text()) {
485 setGoIconVisible(
true);
492 if (m_window &&
Settings().
value(
QSL(
"Browser-View-Settings/instantBookmarksToolbar")).toBool()) {
497void LocationBar::focusOutEvent(QFocusEvent* event)
501 if (
event->reason() == Qt::PopupFocusReason) {
505 LineEdit::focusOutEvent(
event);
507 setGoIconVisible(
false);
509 if (text().trimmed().isEmpty()) {
515 if (m_window &&
Settings().
value(
QSL(
"Browser-View-Settings/instantBookmarksToolbar")).toBool()) {
520void LocationBar::dropEvent(QDropEvent* event)
522 if (
event->mimeData()->hasUrls()) {
523 const QUrl dropUrl =
event->mimeData()->urls().at(0);
528 QFocusEvent
event(QFocusEvent::FocusOut);
529 LineEdit::focusOutEvent(&
event);
533 else if (
event->mimeData()->hasText()) {
534 const QString dropText =
event->mimeData()->text().trimmed();
535 const QUrl dropUrl = QUrl(dropText);
540 QFocusEvent
event(QFocusEvent::FocusOut);
541 LineEdit::focusOutEvent(&
event);
550 LineEdit::dropEvent(
event);
553void LocationBar::keyPressEvent(QKeyEvent* event)
555 switch (
event->key()) {
557 if (
event->modifiers() == (Qt::ControlModifier | Qt::ShiftModifier)) {
573 m_webView->setFocus();
584 switch (
event->modifiers()) {
585 case Qt::ControlModifier:
586 if (!text().endsWith(
QL1S(
".com")))
589 m_holdingAlt =
false;
592 case Qt::AltModifier:
597 m_holdingAlt =
false;
602 m_holdingAlt =
false;
617 if (
event->modifiers() & Qt::AltModifier ||
event->modifiers() & Qt::ControlModifier) {
619 m_holdingAlt =
false;
625 m_holdingAlt =
false;
628 LineEdit::keyPressEvent(
event);
631void LocationBar::loadStarted()
633 m_progressVisible =
true;
634 m_progressTimer->stop();
635 m_autofillIcon->hide();
638void LocationBar::loadProgress(
int progress)
641 m_loadProgress = progress;
646void LocationBar::loadFinished()
649 m_progressTimer->start();
652 auto* page = qobject_cast<WebPage*>(m_webView->
page());
654 if (page && !page->autoFillUsernames().isEmpty()) {
655 m_autofillIcon->
setUsernames(page->autoFillUsernames());
656 m_autofillIcon->show();
660void LocationBar::loadSettings()
664 m_progressStyle =
static_cast<ProgressStyle
>(settings.
value(
QSL(
"ProgressStyle"), 0).toInt());
665 bool customColor = settings.
value(
QSL(
"UseCustomProgressColor"),
false).toBool();
666 m_progressColor = customColor ? settings.
value(
QSL(
"CustomProgressColor"), palette().color(QPalette::Highlight)).value<QColor>() : QColor();
670void LocationBar::hideProgress()
673 m_progressVisible =
false;
678void LocationBar::paintEvent(QPaintEvent* event)
680 LineEdit::paintEvent(
event);
683 if (
qzSettings->showLoadingProgress && m_progressVisible) {
684 QStyleOptionFrame option;
685 initStyleOption(&option);
687 QMargins margins = textMargins();
689 QRect contentsRect = style()->subElementRect(QStyle::SE_LineEditContents, &option,
this);
691 margins.left(), margins.top(), -margins.right(), -margins.bottom()
694 QColor
bg = m_progressColor;
695 if (!
bg.isValid() ||
bg.alpha() == 0) {
696 bg =
Colors::mid(palette().color(QPalette::Base), palette().color(QPalette::Text), m_progressStyle > 0 ? 4 : 8, 1);
700 p.setBrush(QBrush(
bg));
705 QPen outlinePen(
bg.darker(110), 0.8);
706 p.setPen(outlinePen);
708 switch (m_progressStyle) {
709 case ProgressFilled: {
710 QRect bar = contentsRect.adjusted(0, 1, 0, -1);
711 bar.setWidth(bar.width() * m_loadProgress / 100);
712 const int roundness = bar.height() / 4.0;
713 p.drawRoundedRect(bar, roundness, roundness);
716 case ProgressBottom: {
717 outlinePen.setWidthF(0.3);
718 outlinePen.setColor(outlinePen.color().darker(130));
719 p.setPen(outlinePen);
720 QRect bar(contentsRect.x(), contentsRect.bottom() - 3,
721 contentsRect.width() * m_loadProgress / 100.0, 3);
722 p.drawRoundedRect(bar, 1, 1);
726 outlinePen.setWidthF(0.3);
727 outlinePen.setColor(outlinePen.color().darker(130));
728 p.setPen(outlinePen);
729 QRect bar(contentsRect.x(), contentsRect.top() + 1, contentsRect.width() * m_loadProgress / 100.0, 3);
730 p.drawRoundedRect(bar, 1, 1);
void setWebView(WebView *view)
void setUsernames(const QStringList &usernames)
void checkBookmark(const QUrl &url, bool forceCheck=false)
void setWebView(WebView *view)
TabWidget * tabWidget() const
BookmarksToolbar * bookmarksToolbar() const
static QIcon emptyWebIcon()
QAction * editAction(EditAction action) const
void addWidget(QWidget *widget, WidgetPosition position)
void setTextFormat(const TextFormat &format)
QMenu * createContextMenu()
void focusInEvent(QFocusEvent *event) override
bool event(QEvent *event) override
QList< QTextLayout::FormatRange > TextFormat
void setUrl(const QUrl &url)
void setBrowserWindow(BrowserWindow *window)
void loadRequest(const LoadRequest &request)
void setWebView(TabbedWebView *view)
static SearchEngine searchEngine()
static QString convertUrlToText(const QUrl &url)
BrowserWindow * browserWindow() const
LocationBar(QWidget *parent=nullptr)
void setText(const QString &text)
TabbedWebView * webView() const
static LoadAction loadAction(const QString &text)
void showUrl(const QUrl &url)
void setLocationBar(LocationBar *locationBar)
void setMainWindow(BrowserWindow *window)
void showCompletion(const QString &completion, bool completeDomain)
void loadRequested(const LoadRequest &request)
void complete(const QString &string)
void showDomainCompletion(const QString &completion)
void defaultEngineChanged()
void activeEngineChanged()
void beginGroup(const QString &prefix)
QVariant value(const QString &key, const QVariant &defaultValue=QVariant())
void setIcon(const QIcon &icon)
void setBrowserWindow(BrowserWindow *window)
void setWebView(WebView *view)
void userLoadAction(const LoadRequest &req)
static QStringList supportedSchemes()
static bool isUrlValid(const QUrl &url)
void privacyChanged(bool)
void setWebView(WebView *view)
QColor mid(const QColor &oc1, const QColor &c2, int w1=1, int w2=1)
int value(const QColor &c)
const QColor & bg(const QPalette &pal, const QWidget *w)
SearchEngine searchEngine