aboutsummaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/navigation/urllineedit.cpp47
-rw-r--r--src/lib/navigation/urllineedit.h4
2 files changed, 45 insertions, 6 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