2012-04-22 17:09:43 +02:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2013-01-28 19:38:03 +01:00
|
|
|
* Copyright (C) 2010-2013 David Rosca <nowrep@gmail.com>
|
2012-04-22 17:09:43 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
* ============================================================ */
|
|
|
|
#include "locationcompleter.h"
|
|
|
|
#include "locationcompletermodel.h"
|
|
|
|
#include "locationcompleterview.h"
|
|
|
|
#include "locationcompleterdelegate.h"
|
|
|
|
#include "locationbar.h"
|
|
|
|
|
|
|
|
LocationCompleterView* LocationCompleter::s_view = 0;
|
|
|
|
LocationCompleterModel* LocationCompleter::s_model = 0;
|
|
|
|
|
|
|
|
LocationCompleter::LocationCompleter(QObject* parent)
|
|
|
|
: QObject(parent)
|
|
|
|
, m_locationBar(0)
|
2012-12-08 18:12:31 +01:00
|
|
|
, m_ignoreCurrentChangedSignal(false)
|
2013-02-11 13:33:02 +01:00
|
|
|
, m_showingMostVisited(false)
|
2012-04-22 17:09:43 +02:00
|
|
|
{
|
|
|
|
if (!s_view) {
|
|
|
|
s_model = new LocationCompleterModel;
|
|
|
|
s_view = new LocationCompleterView;
|
|
|
|
|
|
|
|
s_view->setModel(s_model);
|
2012-12-04 16:16:45 +01:00
|
|
|
s_view->setItemDelegate(new LocationCompleterDelegate(s_view));
|
2012-04-22 17:09:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocationCompleter::setLocationBar(LocationBar* locationBar)
|
|
|
|
{
|
|
|
|
m_locationBar = locationBar;
|
|
|
|
}
|
|
|
|
|
2013-02-11 13:33:02 +01:00
|
|
|
bool LocationCompleter::showingMostVisited() const
|
|
|
|
{
|
|
|
|
return m_showingMostVisited;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LocationCompleter::isPopupVisible() const
|
2013-01-28 19:38:03 +01:00
|
|
|
{
|
|
|
|
return s_view->isVisible();
|
|
|
|
}
|
|
|
|
|
2012-04-22 17:09:43 +02:00
|
|
|
void LocationCompleter::closePopup()
|
|
|
|
{
|
2013-02-11 13:33:02 +01:00
|
|
|
m_showingMostVisited = false;
|
2012-04-22 17:09:43 +02:00
|
|
|
s_view->close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocationCompleter::complete(const QString &string)
|
|
|
|
{
|
2013-02-11 13:33:02 +01:00
|
|
|
m_showingMostVisited = string.isEmpty();
|
2012-04-22 17:09:43 +02:00
|
|
|
|
2013-02-11 13:33:02 +01:00
|
|
|
s_model->refreshCompletions(string);
|
2012-04-22 17:09:43 +02:00
|
|
|
showPopup();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocationCompleter::showMostVisited()
|
|
|
|
{
|
2013-02-11 13:33:02 +01:00
|
|
|
complete(QString());
|
2012-04-22 17:09:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LocationCompleter::currentChanged(const QModelIndex &index)
|
|
|
|
{
|
2012-12-08 18:12:31 +01:00
|
|
|
if (m_ignoreCurrentChangedSignal) {
|
2012-12-08 16:50:16 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-04-22 17:09:43 +02:00
|
|
|
QString completion = index.data().toString();
|
2012-09-02 15:19:12 +02:00
|
|
|
|
2012-04-22 17:09:43 +02:00
|
|
|
if (completion.isEmpty()) {
|
|
|
|
completion = m_originalText;
|
|
|
|
}
|
|
|
|
|
2012-09-08 22:52:32 +02:00
|
|
|
emit showCompletion(completion);
|
2012-04-22 17:09:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LocationCompleter::popupClosed()
|
|
|
|
{
|
|
|
|
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()));
|
2012-12-04 14:29:27 +01:00
|
|
|
disconnect(s_view, SIGNAL(aboutToActivateTab(TabPosition)), m_locationBar, SLOT(clear()));
|
2012-04-22 17:09:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LocationCompleter::showPopup()
|
|
|
|
{
|
|
|
|
Q_ASSERT(m_locationBar);
|
|
|
|
|
|
|
|
if (s_model->rowCount() == 0) {
|
|
|
|
s_view->close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s_view->isVisible()) {
|
|
|
|
adjustPopupSize();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QRect popupRect(m_locationBar->mapToGlobal(m_locationBar->pos()), m_locationBar->size());
|
|
|
|
popupRect.setY(popupRect.bottom());
|
|
|
|
|
|
|
|
s_view->setFocusProxy(m_locationBar);
|
|
|
|
s_view->setGeometry(popupRect);
|
|
|
|
|
|
|
|
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()));
|
2012-12-04 14:29:27 +01:00
|
|
|
connect(s_view, SIGNAL(aboutToActivateTab(TabPosition)), m_locationBar, SLOT(clear()));
|
2012-04-22 17:09:43 +02:00
|
|
|
|
|
|
|
adjustPopupSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocationCompleter::adjustPopupSize()
|
|
|
|
{
|
|
|
|
const int maxItemsCount = 6;
|
|
|
|
|
|
|
|
int popupHeight = s_view->sizeHintForRow(0) * qMin(maxItemsCount, s_model->rowCount());
|
|
|
|
popupHeight += 2 * s_view->frameWidth();
|
|
|
|
|
|
|
|
s_view->resize(s_view->width(), popupHeight);
|
2012-12-08 18:12:31 +01:00
|
|
|
m_ignoreCurrentChangedSignal = true;
|
2012-04-22 17:09:43 +02:00
|
|
|
s_view->setCurrentIndex(QModelIndex());
|
2012-12-09 08:15:21 +01:00
|
|
|
m_ignoreCurrentChangedSignal = false;
|
2012-04-22 17:09:43 +02:00
|
|
|
s_view->show();
|
|
|
|
|
|
|
|
m_originalText = m_locationBar->text();
|
|
|
|
}
|