aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2017-08-19 10:48:35 +0200
committerAqua-sama <aqua@iserlohn-fortress.net>2017-08-19 10:48:35 +0200
commit992802bb5a3f00c563aa02244143d63a33ee1540 (patch)
tree5fa505549927ca5c6f7d06161c54f92db7abdb81
parentUpdated documentation (diff)
downloadsmolbote-992802bb5a3f00c563aa02244143d63a33ee1540.tar.xz
Added URL bar completer
-rw-r--r--src/widgets/urllineedit.cpp41
-rw-r--r--src/widgets/urllineedit.h7
2 files changed, 48 insertions, 0 deletions
diff --git a/src/widgets/urllineedit.cpp b/src/widgets/urllineedit.cpp
index b038da9..871cd53 100644
--- a/src/widgets/urllineedit.cpp
+++ b/src/widgets/urllineedit.cpp
@@ -26,6 +26,8 @@
#include <QStyle>
#include "browser.h"
+#include <QWidgetAction>
+
UrlLineEdit::UrlLineEdit(QWidget *parent) :
QLineEdit(parent)
{
@@ -47,6 +49,27 @@ UrlLineEdit::UrlLineEdit(QWidget *parent) :
QAction *contextAction = addAction(style()->standardIcon(QStyle::SP_TitleBarMinButton), ActionPosition::TrailingPosition);
contextAction->setShortcut(QKeySequence::fromString("F3"));
connect(contextAction, SIGNAL(triggered()), this, SLOT(showMenu()));
+
+ m_menu = new QMenu(this);
+ m_menu->setWindowFlags(Qt::FramelessWindowHint | Qt::Tool);
+
+ QListWidget *listWidget = new QListWidget();
+ listWidget->addItem("start.duckduckgo.com");
+ listWidget->addItem("neueland.iserlohn-fortress.net");
+
+ connect(listWidget, &QListWidget::itemActivated, [this](QListWidgetItem *item){
+ setUrl(urlFromUserInput(item->text()));
+ this->m_menu->hide();
+ });
+
+ QWidgetAction *listAction = new QWidgetAction(m_menu);
+ listAction->setDefaultWidget(listWidget);
+ m_menu->addAction(listAction);
+ QAction *closeAction = m_menu->addAction("Close");
+ connect(closeAction, SIGNAL(triggered()), m_menu, SLOT(hide()));
+
+ connect(this, SIGNAL(textEdited(QString)), this, SLOT(showCompleter(QString)));
+
}
void UrlLineEdit::setUrl(const QUrl &url)
@@ -71,20 +94,30 @@ QUrl UrlLineEdit::url()
void UrlLineEdit::focusInEvent(QFocusEvent *event)
{
clearTextFormat();
+
QLineEdit::focusInEvent(event);
// select the contents when receiving focus
// http://stackoverflow.com/a/35725950/1054406
// mousePressEvent triggers right after focusInEvent so text selected in focusInEvent unselects by mousePressEvent
QTimer::singleShot(0, this, SLOT(selectAll()));
+
+ //QTimer::singleShot(0, this, SLOT(showCompleter()));
}
void UrlLineEdit::focusOutEvent(QFocusEvent *event)
{
+ wasFocused = false;
setUrl(urlFromUserInput(text()));
QLineEdit::focusOutEvent(event);
}
+void UrlLineEdit::resizeEvent(QResizeEvent *event)
+{
+ QLineEdit::resizeEvent(event);
+ m_menu->setFixedWidth(width());
+}
+
// formatting taken from: https://forum.qt.io/topic/60962/setting-qlineedit-text-bold
void UrlLineEdit::setTextFormat(const QTextLayout::FormatRange &format)
{
@@ -108,6 +141,14 @@ QUrl UrlLineEdit::urlFromUserInput(const QString &input)
return QUrl::fromUserInput(input);
}
+// Completer
+
+void UrlLineEdit::showCompleter(const QString &text)
+{
+ m_menu->move(mapToGlobal(QPoint(0, height())));
+ m_menu->exec();
+}
+
// Menu
void UrlLineEdit::showMenu()
diff --git a/src/widgets/urllineedit.h b/src/widgets/urllineedit.h
index 959461c..548000a 100644
--- a/src/widgets/urllineedit.h
+++ b/src/widgets/urllineedit.h
@@ -25,6 +25,8 @@
#include <QTextLayout>
#include <QMenu>
+#include <QListWidget>
+
class UrlLineEdit : public QLineEdit
{
Q_OBJECT
@@ -40,8 +42,10 @@ public slots:
protected:
void focusInEvent(QFocusEvent *event);
void focusOutEvent(QFocusEvent *event);
+ void resizeEvent(QResizeEvent *event);
private slots:
+ void showCompleter(const QString &text);
void showMenu();
void copyUrl();
void pasteUrl();
@@ -56,6 +60,9 @@ private:
QTextLayout::FormatRange m_hostFormat;
QMenu *m_contextMenu;
+
+ bool wasFocused = false;
+ QMenu *m_menu;
};
#endif // URLLINEEDIT_H