cutter/src/widgets/Omnibar.cpp

79 lines
1.9 KiB
C++
Raw Normal View History

#include "Omnibar.h"
#include "core/MainWindow.h"
#include <QStringListModel>
#include <QCompleter>
#include <QShortcut>
2017-04-27 23:48:21 +00:00
#include <QAbstractItemView>
Omnibar::Omnibar(MainWindow *main, QWidget *parent) :
QLineEdit(parent),
2017-12-21 15:16:43 +00:00
main(main)
{
// QLineEdit basic features
this->setMinimumHeight(16);
this->setFrame(false);
2018-02-03 19:27:00 +00:00
this->setPlaceholderText(tr("Type flag name or address here"));
this->setStyleSheet("border-radius: 5px; padding: 0 8px; margin: 5px 0;");
this->setTextMargins(10, 0, 0, 0);
this->setClearButtonEnabled(true);
connect(this, SIGNAL(returnPressed()), this, SLOT(on_gotoEntry_returnPressed()));
// Esc clears omnibar
2017-04-09 19:55:06 +00:00
QShortcut *clear_shortcut = new QShortcut(QKeySequence(Qt::Key_Escape), this);
connect(clear_shortcut, SIGNAL(activated()), this, SLOT(clear()));
2017-05-18 10:20:53 +00:00
clear_shortcut->setContext(Qt::WidgetWithChildrenShortcut);
}
2017-04-09 19:55:06 +00:00
void Omnibar::setupCompleter()
{
// Set gotoEntry completer for jump history
2017-12-21 15:16:43 +00:00
QCompleter *completer = new QCompleter(flags, this);
completer->setMaxVisibleItems(20);
completer->setCompletionMode(QCompleter::PopupCompletion);
completer->setModelSorting(QCompleter::CaseSensitivelySortedModel);
completer->setCaseSensitivity(Qt::CaseInsensitive);
completer->setFilterMode(Qt::MatchContains);
this->setCompleter(completer);
}
void Omnibar::refresh(const QStringList &flagList)
{
flags = flagList;
setupCompleter();
}
void Omnibar::restoreCompleter()
{
QCompleter *completer = this->completer();
2019-01-13 15:00:07 +00:00
if (!completer) {
return;
}
completer->setFilterMode(Qt::MatchContains);
}
2017-04-27 23:48:21 +00:00
void Omnibar::clear()
{
QLineEdit::clear();
// Close the potential shown completer popup
clearFocus();
setFocus();
}
void Omnibar::on_gotoEntry_returnPressed()
{
QString str = this->text();
2018-03-21 20:32:32 +00:00
if (!str.isEmpty()) {
2017-12-21 15:16:43 +00:00
Core()->seek(str);
}
this->setText("");
this->clearFocus();
this->restoreCompleter();
}