#include <QUrlQuery>
DolphinSearchBox::DolphinSearchBox(QWidget *parent)
- : QWidget(parent)
+ : AnimatedHeightWidget(parent)
, m_startedSearching(false)
, m_active(true)
, m_topLayout(nullptr)
, m_facetsWidget(nullptr)
, m_searchPath()
, m_startSearchTimer(nullptr)
+ , m_initialized(false)
{
}
void DolphinSearchBox::setSearchPath(const QUrl &url)
{
- if (url == m_searchPath) {
+ if (url == m_searchPath || !m_initialized) {
return;
}
url.setScheme(QStringLiteral("filenamesearch"));
QUrlQuery query;
- query.addQueryItem(QStringLiteral("search"), m_searchInput->text());
+ query.addQueryItem(QStringLiteral("search"), QUrl::toPercentEncoding(m_searchInput->text()));
if (m_contentButton->isChecked()) {
query.addQueryItem(QStringLiteral("checkContent"), QStringLiteral("yes"));
}
- query.addQueryItem(QStringLiteral("url"), searchPath().url());
- query.addQueryItem(QStringLiteral("title"), queryTitle(m_searchInput->text()));
+ query.addQueryItem(QStringLiteral("url"), QUrl::toPercentEncoding(searchPath().url()));
+ query.addQueryItem(QStringLiteral("title"), QUrl::toPercentEncoding(queryTitle(m_searchInput->text())));
url.setQuery(query);
}
updateFromQuery(query);
} else if (url.scheme() == QLatin1String("filenamesearch")) {
const QUrlQuery query(url);
- setText(query.queryItemValue(QStringLiteral("search")));
+ setText(query.queryItemValue(QStringLiteral("search"), QUrl::FullyDecoded));
if (m_searchPath.scheme() != url.scheme()) {
m_searchPath = QUrl();
}
- setSearchPath(QUrl::fromUserInput(query.queryItemValue(QStringLiteral("url")), QString(), QUrl::AssumeLocalFile));
+ setSearchPath(QUrl::fromUserInput(query.queryItemValue(QStringLiteral("url"), QUrl::FullyDecoded), QString(), QUrl::AssumeLocalFile));
m_contentButton->setChecked(query.queryItemValue(QStringLiteral("checkContent")) == QLatin1String("yes"));
} else {
setText(QString());
return m_active;
}
-bool DolphinSearchBox::event(QEvent *event)
+void DolphinSearchBox::setVisible(bool visible, Animated animated)
{
- if (event->type() == QEvent::Polish) {
+ if (visible) {
init();
}
- return QWidget::event(event);
+ AnimatedHeightWidget::setVisible(visible, animated);
}
void DolphinSearchBox::showEvent(QShowEvent *event)
{
Q_UNUSED(event)
m_startedSearching = false;
- m_startSearchTimer->stop();
+ if (m_startSearchTimer) {
+ m_startSearchTimer->stop();
+ }
}
void DolphinSearchBox::keyReleaseEvent(QKeyEvent *event)
void DolphinSearchBox::saveSettings()
{
- SearchSettings::setLocation(m_fromHereButton->isChecked() ? QStringLiteral("FromHere") : QStringLiteral("Everywhere"));
- SearchSettings::setWhat(m_fileNameButton->isChecked() ? QStringLiteral("FileName") : QStringLiteral("Content"));
- SearchSettings::self()->save();
+ if (m_initialized) {
+ SearchSettings::setLocation(m_fromHereButton->isChecked() ? QStringLiteral("FromHere") : QStringLiteral("Everywhere"));
+ SearchSettings::setWhat(m_fileNameButton->isChecked() ? QStringLiteral("FileName") : QStringLiteral("Content"));
+ SearchSettings::self()->save();
+ }
}
void DolphinSearchBox::init()
{
+ if (m_initialized) {
+ return; // This object is already initialised.
+ }
+
+ QWidget *contentsContainer = prepareContentsContainer();
+
// Create search box
- m_searchInput = new QLineEdit(this);
+ m_searchInput = new QLineEdit(contentsContainer);
m_searchInput->setPlaceholderText(i18n("Search…"));
m_searchInput->installEventFilter(this);
m_searchInput->setClearButtonEnabled(true);
connect(m_saveSearchAction, &QAction::triggered, this, &DolphinSearchBox::slotSearchSaved);
// Create close button
- QToolButton *closeButton = new QToolButton(this);
+ QToolButton *closeButton = new QToolButton(contentsContainer);
closeButton->setAutoRaise(true);
closeButton->setIcon(QIcon::fromTheme(QStringLiteral("dialog-close")));
closeButton->setToolTip(i18nc("@info:tooltip", "Quit searching"));
searchInputLayout->addWidget(closeButton);
// Create "Filename" and "Content" button
- m_fileNameButton = new QToolButton(this);
+ m_fileNameButton = new QToolButton(contentsContainer);
m_fileNameButton->setText(i18nc("action:button", "Filename"));
initButton(m_fileNameButton);
m_contentButton->setText(i18nc("action:button", "Content"));
initButton(m_contentButton);
- QButtonGroup *searchWhatGroup = new QButtonGroup(this);
+ QButtonGroup *searchWhatGroup = new QButtonGroup(contentsContainer);
searchWhatGroup->addButton(m_fileNameButton);
searchWhatGroup->addButton(m_contentButton);
- m_separator = new KSeparator(Qt::Vertical, this);
+ m_separator = new KSeparator(Qt::Vertical, contentsContainer);
// Create "From Here" and "Your files" buttons
- m_fromHereButton = new QToolButton(this);
+ m_fromHereButton = new QToolButton(contentsContainer);
m_fromHereButton->setText(i18nc("action:button", "From Here"));
initButton(m_fromHereButton);
- m_everywhereButton = new QToolButton(this);
+ m_everywhereButton = new QToolButton(contentsContainer);
m_everywhereButton->setText(i18nc("action:button", "Your files"));
m_everywhereButton->setToolTip(i18nc("action:button", "Search in your home directory"));
m_everywhereButton->setIcon(QIcon::fromTheme(QStringLiteral("user-home")));
m_everywhereButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
initButton(m_everywhereButton);
- QButtonGroup *searchLocationGroup = new QButtonGroup(this);
+ QButtonGroup *searchLocationGroup = new QButtonGroup(contentsContainer);
searchLocationGroup->addButton(m_fromHereButton);
searchLocationGroup->addButton(m_everywhereButton);
QToolButton *kfindToolsButton = nullptr;
if (kfind) {
- kfindToolsButton = new QToolButton(this);
+ kfindToolsButton = new QToolButton(contentsContainer);
kfindToolsButton->setAutoRaise(true);
kfindToolsButton->setPopupMode(QToolButton::InstantPopup);
kfindToolsButton->setIcon(QIcon::fromTheme("arrow-down-double"));
}
// Create "Facets" widget
- m_facetsWidget = new DolphinFacetsWidget(this);
+ m_facetsWidget = new DolphinFacetsWidget(contentsContainer);
m_facetsWidget->installEventFilter(this);
m_facetsWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
m_facetsWidget->layout()->setSpacing(Dolphin::LAYOUT_SPACING_SMALL);
// Put the options into a QScrollArea. This prevents increasing the view width
// in case that not enough width for the options is available.
- QWidget *optionsContainer = new QWidget(this);
+ QWidget *optionsContainer = new QWidget(contentsContainer);
// Apply layout for the options
QHBoxLayout *optionsLayout = new QHBoxLayout(optionsContainer);
optionsLayout->addWidget(m_separator);
optionsLayout->addWidget(m_fromHereButton);
optionsLayout->addWidget(m_everywhereButton);
- optionsLayout->addWidget(new KSeparator(Qt::Vertical, this));
+ optionsLayout->addWidget(new KSeparator(Qt::Vertical, contentsContainer));
if (kfindToolsButton) {
optionsLayout->addWidget(kfindToolsButton);
}
optionsLayout->addStretch(1);
- m_optionsScrollArea = new QScrollArea(this);
+ m_optionsScrollArea = new QScrollArea(contentsContainer);
m_optionsScrollArea->setFrameShape(QFrame::NoFrame);
m_optionsScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_optionsScrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_optionsScrollArea->setWidget(optionsContainer);
m_optionsScrollArea->setWidgetResizable(true);
- m_topLayout = new QVBoxLayout(this);
+ m_topLayout = new QVBoxLayout(contentsContainer);
m_topLayout->setContentsMargins(0, Dolphin::LAYOUT_SPACING_SMALL, 0, 0);
m_topLayout->setSpacing(Dolphin::LAYOUT_SPACING_SMALL);
m_topLayout->addLayout(searchInputLayout);
m_startSearchTimer->setSingleShot(true);
m_startSearchTimer->setInterval(500);
connect(m_startSearchTimer, &QTimer::timeout, this, &DolphinSearchBox::emitSearchRequest);
+
+ m_initialized = true;
}
QString DolphinSearchBox::queryTitle(const QString &text) const
const bool indexingEnabled = isIndexingEnabled();
m_facetsWidget->setEnabled(indexingEnabled);
m_facetsWidget->setVisible(indexingEnabled);
+
+ // The m_facetsWidget might have changed visibility. We smoothly animate towards the updated height.
+ if (isVisible() && isEnabled()) {
+ setVisible(true, WithAnimation);
+ }
}
bool DolphinSearchBox::isIndexingEnabled() const
#endif
}
+int DolphinSearchBox::preferredHeight() const
+{
+ return m_initialized ? m_topLayout->sizeHint().height() : 0;
+}
+
#include "moc_dolphinsearchbox.cpp"