From b4b1ce03d673a8249aff1c49c1812e8b76d2b431 Mon Sep 17 00:00:00 2001 From: nowrep Date: Fri, 22 Feb 2013 15:20:28 +0100 Subject: [PATCH] LocationBar: Proper fix for determing when to draw cursor. Cursor will be properly drawn by Qt when starting writing into empty lineedit (eg. select all and start typing). After the completion popup is hidden for the first time, Qt will stop drawing cursor and will only paint it again after losing and gaining focus. --- .../completer/locationcompleter.cpp | 8 +++-- .../navigation/completer/locationcompleter.h | 3 +- src/lib/navigation/locationbar.cpp | 34 +++++++++++++------ src/lib/navigation/locationbar.h | 5 ++- 4 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/lib/navigation/completer/locationcompleter.cpp b/src/lib/navigation/completer/locationcompleter.cpp index 424a5b514..0ad5e7e51 100644 --- a/src/lib/navigation/completer/locationcompleter.cpp +++ b/src/lib/navigation/completer/locationcompleter.cpp @@ -88,12 +88,14 @@ void LocationCompleter::currentChanged(const QModelIndex &index) emit showCompletion(completion); } -void LocationCompleter::popupClosed() +void LocationCompleter::slotPopupClosed() { disconnect(s_view->selectionModel(), SIGNAL(currentChanged(QModelIndex, QModelIndex)), this, SLOT(currentChanged(QModelIndex))); disconnect(s_view, SIGNAL(clicked(QModelIndex)), this, SIGNAL(completionActivated())); - disconnect(s_view, SIGNAL(closed()), this, SLOT(popupClosed())); + disconnect(s_view, SIGNAL(closed()), this, SLOT(slotPopupClosed())); disconnect(s_view, SIGNAL(aboutToActivateTab(TabPosition)), m_locationBar, SLOT(clear())); + + emit popupClosed(); } void LocationCompleter::showPopup() @@ -118,7 +120,7 @@ void LocationCompleter::showPopup() connect(s_view->selectionModel(), SIGNAL(currentChanged(QModelIndex, QModelIndex)), this, SLOT(currentChanged(QModelIndex))); connect(s_view, SIGNAL(clicked(QModelIndex)), this, SIGNAL(completionActivated())); - connect(s_view, SIGNAL(closed()), this, SLOT(popupClosed())); + connect(s_view, SIGNAL(closed()), this, SLOT(slotPopupClosed())); connect(s_view, SIGNAL(aboutToActivateTab(TabPosition)), m_locationBar, SLOT(clear())); adjustPopupSize(); diff --git a/src/lib/navigation/completer/locationcompleter.h b/src/lib/navigation/completer/locationcompleter.h index 79b3a9012..bb40b7b94 100644 --- a/src/lib/navigation/completer/locationcompleter.h +++ b/src/lib/navigation/completer/locationcompleter.h @@ -43,6 +43,7 @@ public: signals: void showCompletion(const QString &); void completionActivated(); + void popupClosed(); public slots: void complete(const QString &string); @@ -50,7 +51,7 @@ public slots: private slots: void currentChanged(const QModelIndex &index); - void popupClosed(); + void slotPopupClosed(); private: void showPopup(); diff --git a/src/lib/navigation/locationbar.cpp b/src/lib/navigation/locationbar.cpp index 2c7c50e9c..6b352038c 100644 --- a/src/lib/navigation/locationbar.cpp +++ b/src/lib/navigation/locationbar.cpp @@ -54,7 +54,9 @@ LocationBar::LocationBar(QupZilla* mainClass) , m_holdingAlt(false) , m_loadProgress(0) , m_progressVisible(false) - , m_forceLineEditPaintEvent(false) + , m_forcePaintEvent(false) + , m_drawCursor(true) + , m_popupClosed(false) { setObjectName("locationbar"); setDragEnabled(true); @@ -82,6 +84,7 @@ LocationBar::LocationBar(QupZilla* mainClass) m_completer.setLocationBar(this); connect(&m_completer, SIGNAL(showCompletion(QString)), this, SLOT(showCompletion(QString))); connect(&m_completer, SIGNAL(completionActivated()), this, SLOT(urlEnter())); + connect(&m_completer, SIGNAL(popupClosed()), this, SLOT(completionPopupClosed())); connect(this, SIGNAL(textEdited(QString)), this, SLOT(textEdit())); connect(m_goIcon, SIGNAL(clicked(QPoint)), this, SLOT(urlEnter())); @@ -118,7 +121,7 @@ void LocationBar::setWebView(TabbedWebView* view) void LocationBar::setText(const QString &text) { LineEdit::setText(text); - m_forceLineEditPaintEvent = true; + m_forcePaintEvent = true; setCursorPosition(0); } @@ -138,6 +141,12 @@ void LocationBar::showCompletion(const QString &newText) end(false); } +void LocationBar::completionPopupClosed() +{ + m_popupClosed = true; + m_drawCursor = true; +} + QUrl LocationBar::createUrl() { QUrl urlToLoad; @@ -172,7 +181,7 @@ QString LocationBar::convertUrlToText(const QUrl &url) const QString stringUrl = QzTools::urlEncodeQueryString(url); if (stringUrl == QLatin1String("qupzilla:speeddial") || stringUrl == QLatin1String("about:blank")) { - stringUrl = ""; + stringUrl.clear(); } return stringUrl; @@ -195,6 +204,11 @@ void LocationBar::textEdit() m_completer.closePopup(); } + // Decide whether to draw cursor + if (text().length() <= 1 && m_drawCursor && !m_popupClosed) { + m_drawCursor = false; + } + showGoButton(); } @@ -401,7 +415,8 @@ void LocationBar::focusOutEvent(QFocusEvent* event) return; } - m_forceLineEditPaintEvent = true; + m_popupClosed = false; + m_forcePaintEvent = true; setCursorPosition(0); hideGoButton(); @@ -552,11 +567,10 @@ void LocationBar::hideProgress() void LocationBar::paintEvent(QPaintEvent* event) { - if (m_completer.isPopupVisible() && !m_completer.showingMostVisited()) { + if (m_drawCursor && m_completer.isPopupVisible() && !m_completer.showingMostVisited()) { // We need to draw cursor when popup is visible // But don't paint it if we are just showing most visited sites LineEdit::paintEvent(event); - QStyleOptionFrameV3 option; initStyleOption(&option); @@ -569,7 +583,7 @@ void LocationBar::paintEvent(QPaintEvent* event) const QFontMetrics &fm = fontMetrics(); QString textPart = text().left(cursorPosition()); - int cursorXpos = lm + fontMetrics().width(textPart) + 3; + int cursorXpos = contentsRect.x() + 3 + fontMetrics().width(textPart); int cursorYpos = contentsRect.y() + (contentsRect.height() - fm.height() + 1) / 2; int cursorWidth = style()->pixelMetric(QStyle::PM_TextCursorWidth, &option, this); int cursorHeight = fontMetrics().height(); @@ -583,10 +597,10 @@ void LocationBar::paintEvent(QPaintEvent* event) return; } - if (hasFocus() || text().isEmpty() || m_forceLineEditPaintEvent) { + if (hasFocus() || text().isEmpty() || m_forcePaintEvent) { LineEdit::paintEvent(event); - if (m_forceLineEditPaintEvent) { - m_forceLineEditPaintEvent = false; + if (m_forcePaintEvent) { + m_forcePaintEvent = false; update(); } return; diff --git a/src/lib/navigation/locationbar.h b/src/lib/navigation/locationbar.h index d56066b76..74e44c675 100644 --- a/src/lib/navigation/locationbar.h +++ b/src/lib/navigation/locationbar.h @@ -71,6 +71,7 @@ private slots: void updatePlaceHolderText(); void showCompletion(const QString &newText); + void completionPopupClosed(); void onLoadStarted(); void onLoadProgress(int progress); @@ -123,7 +124,9 @@ private: ProgressStyle m_progressStyle; QColor m_progressColor; - bool m_forceLineEditPaintEvent; + bool m_forcePaintEvent; + bool m_drawCursor; + bool m_popupClosed; }; #endif // LOCATIONBAR_H