feat: tab keybind for tab menu
This commit is contained in:
@@ -3,53 +3,74 @@
|
||||
#include <QLineEdit>
|
||||
#include <iostream>
|
||||
#include <qapplication.h>
|
||||
#include <qevent.h>
|
||||
#include <qlineedit.h>
|
||||
#include <qnamespace.h>
|
||||
#include <qwidget.h>
|
||||
#include <QEvent>
|
||||
|
||||
enzo::ui::TabMenu::TabMenu(QWidget *parent, Qt::WindowFlags f)
|
||||
: QWidget(parent, f)
|
||||
{
|
||||
setFocusPolicy(Qt::StrongFocus);
|
||||
std::cout << "ctor\n";
|
||||
searchBar_ = new QLineEdit("hello world");
|
||||
auto box = new QVBoxLayout(this);
|
||||
box->addWidget(searchBar_);
|
||||
|
||||
// searchBar_->setFocusPolicy(Qt::NoFocus);
|
||||
setDisabled(true);
|
||||
}
|
||||
|
||||
void enzo::ui::TabMenu::showOnMouse(float dx, float dy)
|
||||
{
|
||||
QPoint cursorPos = mapFromGlobal(QCursor::pos());
|
||||
std::cout << "inside" << cursorPos.x() << " " << cursorPos.y() << "\n";
|
||||
// searchBar_->setFocusPolicy(Qt::StrongFocus);
|
||||
setDisabled(false);
|
||||
std::cout << "showing\n";
|
||||
QPoint cursorPos = mapToParent(mapFromGlobal(QCursor::pos()));
|
||||
std::cout << "tab menu pos: " << cursorPos.x() << " " << cursorPos.y() << "\n";
|
||||
move(cursorPos + QPoint(dx, dy));
|
||||
show();
|
||||
setFocus();
|
||||
raise();
|
||||
}
|
||||
|
||||
void enzo::ui::TabMenu::focusOutEvent(QFocusEvent *event)
|
||||
{
|
||||
std::cout << "focus lost\n";
|
||||
QWidget::focusOutEvent(event);
|
||||
close();
|
||||
setDisabled(true);
|
||||
// searchBar_->setFocusPolicy(Qt::NoFocus);
|
||||
hide();
|
||||
}
|
||||
|
||||
bool enzo::ui::TabMenu::event(QEvent *event)
|
||||
{
|
||||
std::cout << "event\n";
|
||||
if(
|
||||
event->type() == QEvent::KeyPress ||
|
||||
event->type() == QEvent::KeyRelease
|
||||
(event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease)
|
||||
&& event->spontaneous()
|
||||
)
|
||||
{
|
||||
QApplication::postEvent(searchBar_, event->clone());
|
||||
auto *clone = static_cast<QKeyEvent *>(event)->clone();
|
||||
|
||||
if(event->type() == QEvent::KeyPress)
|
||||
{
|
||||
if(static_cast<QKeyEvent*>(event)->key()==Qt::Key_Tab)
|
||||
{
|
||||
|
||||
focusOutEvent(static_cast<QFocusEvent*>(event));
|
||||
return true;
|
||||
}
|
||||
std::cout << "key pressed: " << static_cast<QKeyEvent*>(event)->text().toStdString() << "\n";
|
||||
}
|
||||
else if(event->type() == QEvent::KeyRelease)
|
||||
{
|
||||
std::cout << "key release: " << static_cast<QKeyEvent*>(event)->text().toStdString() << "\n";
|
||||
}
|
||||
QApplication::sendEvent(searchBar_, clone);
|
||||
return true;
|
||||
}
|
||||
return QWidget::event(event);
|
||||
|
||||
}
|
||||
|
||||
// void enzo::ui::TabMenu::keyPressEvent(QKeyEvent *event)
|
||||
// {
|
||||
// searchBar_->keyPressEvent(event);
|
||||
// }
|
||||
// void enzo::ui::TabMenu::keyReleaseEvent(QKeyEvent *event)
|
||||
// {
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user