#include "searchfilteredit.h"
#include "searchlistbox.h"

#include<QKeyEvent>

SearchFilterEdit::SearchFilterEdit() :
	QLineEdit(),
	m_searchListBox(NULL)
{
#if (QT_VERSION >= QT_VERSION_CHECK(5, 2, 0))
	setClearButtonEnabled(true);
#endif
}

void SearchFilterEdit::setSearchListbox(SearchListbox *slb)
{
	m_searchListBox = slb;
}

void SearchFilterEdit::keyPressEvent(QKeyEvent *event)
{
	int key = event->key();
	switch (key) {
	case Qt::Key_Up:
	case Qt::Key_Down:
	case Qt::Key_PageUp:
	case Qt::Key_PageDown:
	case Qt::Key_Home:
	case Qt::Key_End:
	case Qt::Key_Left:
	case Qt::Key_Right:
	case Qt::Key_Enter:
	case Qt::Key_Return:
		// These key presses are for navigating the QTreeView and are
		// handled by the parent class.
		// https://doc.qt.io/qt-6/qtreeview.html#key-bindings
		if (m_searchListBox)
			m_searchListBox->keyPressEvent(event);
		break;
	default:
		// Anything else is assumed to be the user trying to search for
		// an entry in the tree, those key presses go to the search box
		QLineEdit::keyPressEvent(event);
	}
}
