diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/navigation/urllineedit.cpp | 47 | ||||
-rw-r--r-- | src/lib/navigation/urllineedit.h | 4 | ||||
-rw-r--r-- | src/mainwindow.cpp | 14 | ||||
-rw-r--r-- | src/mainwindow.h | 1 |
4 files changed, 53 insertions, 13 deletions
diff --git a/src/lib/navigation/urllineedit.cpp b/src/lib/navigation/urllineedit.cpp index 8083088..f3fcccd 100644 --- a/src/lib/navigation/urllineedit.cpp +++ b/src/lib/navigation/urllineedit.cpp @@ -24,17 +24,16 @@ #include <QAction> #include <QStyle> -//#include "browser.h" #include <QWidgetAction> UrlLineEdit::UrlLineEdit(QWidget *parent) : QLineEdit(parent) { - //setStyleSheet("color: #808080"); setPlaceholderText(tr("Enter address")); setContextMenuPolicy(Qt::NoContextMenu); + QTextCharFormat hostnameFormat; hostnameFormat.setFontWeight(QFont::Bold); m_hostFormat.format = hostnameFormat; @@ -53,10 +52,13 @@ UrlLineEdit::UrlLineEdit(QWidget *parent) : m_menu = new QMenu(this); m_menu->setWindowFlags(Qt::FramelessWindowHint | Qt::Tool); - QListWidget *listWidget = new QListWidget(); + listWidget = new QListWidget(); listWidget->addItem("start.duckduckgo.com"); listWidget->addItem("neueland.iserlohn-fortress.net"); + connect(listWidget, &QListWidget::itemSelectionChanged, [this](){ + setUrl(urlFromUserInput(listWidget->currentItem()->text())); + }); connect(listWidget, &QListWidget::itemActivated, [this](QListWidgetItem *item){ setUrl(urlFromUserInput(item->text())); this->m_menu->hide(); @@ -68,7 +70,16 @@ UrlLineEdit::UrlLineEdit(QWidget *parent) : QAction *closeAction = m_menu->addAction("Close"); connect(closeAction, SIGNAL(triggered()), m_menu, SLOT(hide())); + // connect signals connect(this, SIGNAL(textEdited(QString)), this, SLOT(showCompleter(QString))); + connect(this, &QLineEdit::returnPressed, [this](){ + if(text().startsWith('#')) { + emit searchTermEntered(text()); + } else { + emit addressEntered(url()); + } + m_menu->hide(); + }); } @@ -118,6 +129,32 @@ void UrlLineEdit::resizeEvent(QResizeEvent *event) m_menu->setFixedWidth(width()); } +void UrlLineEdit::keyPressEvent(QKeyEvent *event) +{ + if(event->key() == Qt::Key_Down) { + if(!listWidget->isVisible()) { + showCompleter(text()); + return; + } else { + // listWidget is visible + int newIndex = listWidget->currentRow()+1; + if(newIndex < listWidget->count()) { + listWidget->setCurrentRow(newIndex, QItemSelectionModel::SelectCurrent); + } + return; + } + } else if(event->key() == Qt::Key_Up) { + if(listWidget->isVisible()) { + int newIndex = listWidget->currentRow()-1; + if(newIndex >= 0) { + listWidget->setCurrentRow(newIndex, QItemSelectionModel::SelectCurrent); + } + return; + } + } + QLineEdit::keyPressEvent(event); +} + // formatting taken from: https://forum.qt.io/topic/60962/setting-qlineedit-text-bold void UrlLineEdit::setTextFormat(const QTextLayout::FormatRange &format) { @@ -135,9 +172,6 @@ void UrlLineEdit::clearTextFormat() QUrl UrlLineEdit::urlFromUserInput(const QString &input) { -// if(input.startsWith('#')) { -// return QUrl::fromUserInput(qApp->settings()->value("general.search").toString().replace("$term", input.mid(1))); -// } return QUrl::fromUserInput(input); } @@ -146,6 +180,7 @@ QUrl UrlLineEdit::urlFromUserInput(const QString &input) void UrlLineEdit::showCompleter(const QString &text) { m_menu->move(mapToGlobal(QPoint(0, height()))); + listWidget->setCurrentRow(0, QItemSelectionModel::SelectCurrent); m_menu->exec(); } diff --git a/src/lib/navigation/urllineedit.h b/src/lib/navigation/urllineedit.h index 548000a..0865d47 100644 --- a/src/lib/navigation/urllineedit.h +++ b/src/lib/navigation/urllineedit.h @@ -34,6 +34,8 @@ public: explicit UrlLineEdit(QWidget *parent = 0); signals: + void addressEntered(const QUrl &url); + void searchTermEntered(const QString &term); public slots: void setUrl(const QUrl &url); @@ -43,6 +45,7 @@ protected: void focusInEvent(QFocusEvent *event); void focusOutEvent(QFocusEvent *event); void resizeEvent(QResizeEvent *event); + void keyPressEvent(QKeyEvent *event); private slots: void showCompleter(const QString &text); @@ -63,6 +66,7 @@ private: bool wasFocused = false; QMenu *m_menu; + QListWidget *listWidget; }; #endif // URLLINEEDIT_H diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 856298d..b42fb7e 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -91,7 +91,14 @@ MainWindow::MainWindow(QUrl defaultUrl, QWidget *parent) : this->addToolBar(Qt::TopToolBarArea, navigationToolBar); // connect signals - connect(urlLineEdit, SIGNAL(returnPressed()), this, SLOT(handleUrlChanged())); + connect(urlLineEdit, &UrlLineEdit::addressEntered, [&](const QUrl &url){ + tabBar->currentView()->load(url); + }); + connect(urlLineEdit, &UrlLineEdit::searchTermEntered, [&](const QString &string){ + QString term = string.mid(1); + term.replace(' ', '+'); + tabBar->currentView()->load(QUrl::fromUserInput(qApp->settings()->value("general.search").toString().replace("$term", term))); + }); connect(tabBar, SIGNAL(currentTabChanged(WebView*)), this, SLOT(handleTabChanged(WebView*))); // Load profile @@ -232,11 +239,6 @@ void MainWindow::handleTabChanged(WebView *view) centralWidget()->setFocus(); } -void MainWindow::handleUrlChanged() -{ - tabBar->currentView()->load(urlLineEdit->url()); -} - void MainWindow::handleTitleUpdated(const QString &title) { setWindowTitle(sSettings->value("window.title").toString().replace("title", title).replace("profile", tabBar->profile()->name())); diff --git a/src/mainwindow.h b/src/mainwindow.h index 12ebb0d..6db3102 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -63,7 +63,6 @@ protected: private slots: void focusAddress(); void handleTabChanged(WebView *view); - void handleUrlChanged(); void handleTitleUpdated(const QString &title); private: |