ui: Fixes for search bar

* fixed clicking on search element did not focus search bar
* fixed moving cursor between elements
* no smart pointers for search elements to fix deletion issues
This commit is contained in:
Eberhard Graether
2017-11-03 14:44:07 +01:00
parent bc0c674dc2
commit 8baf8cd49b
4 changed files with 25 additions and 16 deletions
@@ -24,7 +24,6 @@ QLineEdit#search_box {
background-color: transparent; background-color: transparent;
border: none; border: none;
color: <color:search/field/text>; color: <color:search/field/text>;
margin-left: 5px;
selection-background-color: <color:search/field/highlight>; selection-background-color: <color:search/field/highlight>;
} }
+1 -1
View File
@@ -34,7 +34,7 @@ QtSearchBar::QtSearchBar()
layout->addWidget(m_searchBoxContainer); layout->addWidget(m_searchBoxContainer);
QBoxLayout* innerLayout = new QHBoxLayout(); QBoxLayout* innerLayout = new QHBoxLayout();
innerLayout->setContentsMargins(7, 3, 5, 2); innerLayout->setContentsMargins(12, 3, 5, 2);
m_searchBoxContainer->setLayout(innerLayout); m_searchBoxContainer->setLayout(innerLayout);
m_searchBox = new QtSmartSearchBox(m_searchBoxContainer); m_searchBox = new QtSmartSearchBox(m_searchBoxContainer);
+23 -12
View File
@@ -241,7 +241,7 @@ void QtSmartSearchBox::focusInEvent(QFocusEvent* event)
if (m_elements.size() == 1) if (m_elements.size() == 1)
{ {
SearchMatch match = editElement(m_elements[0].get()); SearchMatch match = editElement(m_elements[0]);
if (match.searchType != SearchMatch::SEARCH_NONE) if (match.searchType != SearchMatch::SEARCH_NONE)
{ {
m_oldMatch = match; m_oldMatch = match;
@@ -368,6 +368,11 @@ void QtSmartSearchBox::keyPressEvent(QKeyEvent* event)
addMatchAndUpdate(m_highlightedMatch); addMatchAndUpdate(m_highlightedMatch);
return; return;
} }
else if (m_cursorIndex < m_elements.size())
{
editTextToElement();
moveCursor(1);
}
} }
} }
else if (event->matches(QKeySequence::SelectPreviousChar)) else if (event->matches(QKeySequence::SelectPreviousChar))
@@ -615,6 +620,12 @@ void QtSmartSearchBox::onElementSelected(QtSearchElement* element)
return; return;
} }
if (!hasFocus())
{
setFocus();
return;
}
if (!hasSelectedElements() && !m_shiftKeyDown) if (!hasSelectedElements() && !m_shiftKeyDown)
{ {
editElement(element); editElement(element);
@@ -625,7 +636,7 @@ void QtSmartSearchBox::onElementSelected(QtSearchElement* element)
bool checked = element->isChecked(); bool checked = element->isChecked();
for (size_t i = 0; i < m_elements.size(); i++) for (size_t i = 0; i < m_elements.size(); i++)
{ {
if (m_elements[i].get() == element) if (m_elements[i] == element)
{ {
idx = i; idx = i;
break; break;
@@ -641,7 +652,7 @@ void QtSmartSearchBox::onElementSelected(QtSearchElement* element)
editTextToElement(); editTextToElement();
element = m_elements[idx].get(); element = m_elements[idx];
element->setChecked(checked); element->setChecked(checked);
} }
@@ -744,7 +755,7 @@ SearchMatch QtSmartSearchBox::editElement(QtSearchElement* element)
{ {
for (int i = m_elements.size() - 1; i >= 0; i--) for (int i = m_elements.size() - 1; i >= 0; i--)
{ {
if (m_elements[i].get() == element) if (m_elements[i] == element)
{ {
m_cursorIndex = i; m_cursorIndex = i;
break; break;
@@ -762,10 +773,10 @@ SearchMatch QtSmartSearchBox::editElement(QtSearchElement* element)
void QtSmartSearchBox::updateElements() void QtSmartSearchBox::updateElements()
{ {
m_oldElements = m_elements; for (auto e : m_elements)
for (auto e : m_oldElements)
{ {
e->hide(); e->hide();
e->deleteLater();
} }
m_elements.clear(); m_elements.clear();
@@ -781,7 +792,7 @@ void QtSmartSearchBox::updateElements()
name += ':'; name += ':';
} }
std::shared_ptr<QtSearchElement> element = std::make_shared<QtSearchElement>(QString::fromStdString(name), this); QtSearchElement* element = new QtSearchElement(QString::fromStdString(name), this);
m_elements.push_back(element); m_elements.push_back(element);
std::string color; std::string color;
@@ -812,7 +823,7 @@ void QtSmartSearchBox::updateElements()
element->setStyleSheet(css.str().c_str()); element->setStyleSheet(css.str().c_str());
connect(element.get(), &QtSearchElement::wasChecked, this, &QtSmartSearchBox::onElementSelected); connect(element, &QtSearchElement::wasChecked, this, &QtSmartSearchBox::onElementSelected);
} }
updatePlaceholder(); updatePlaceholder();
@@ -853,7 +864,7 @@ void QtSmartSearchBox::layoutElements()
if (i < m_elements.size()) if (i < m_elements.size())
{ {
QtSearchElement* button = m_elements[i].get(); QtSearchElement* button = m_elements[i];
if (button->isChecked() && !highlightBegin) if (button->isChecked() && !highlightBegin)
{ {
@@ -878,7 +889,7 @@ void QtSmartSearchBox::layoutElements()
for (size_t i = 0; i < elementX.size(); i++) for (size_t i = 0; i < elementX.size(); i++)
{ {
QtSearchElement* button = m_elements[i].get(); QtSearchElement* button = m_elements[i];
QSize size = button->minimumSizeHint(); QSize size = button->minimumSizeHint();
int y = (rect().height() - size.height()) / 2.0; int y = (rect().height() - size.height()) / 2.0;
button->setGeometry(elementX[i] + offsetX, y, size.width(), size.height()); button->setGeometry(elementX[i] + offsetX, y, size.width(), size.height());
@@ -899,7 +910,7 @@ void QtSmartSearchBox::layoutElements()
bool QtSmartSearchBox::hasSelectedElements() const bool QtSmartSearchBox::hasSelectedElements() const
{ {
for (const std::shared_ptr<QtSearchElement>& element : m_elements) for (const QtSearchElement* element : m_elements)
{ {
if (element->isChecked()) if (element->isChecked())
{ {
@@ -924,7 +935,7 @@ std::string QtSmartSearchBox::getSelectedString() const
void QtSmartSearchBox::selectAllElementsWith(bool selected) void QtSmartSearchBox::selectAllElementsWith(bool selected)
{ {
for (const std::shared_ptr<QtSearchElement>& element : m_elements) for (QtSearchElement* element : m_elements)
{ {
element->setChecked(selected); element->setChecked(selected);
} }
+1 -2
View File
@@ -108,8 +108,7 @@ private:
std::deque<SearchMatch> m_matches; std::deque<SearchMatch> m_matches;
SearchMatch m_oldMatch; SearchMatch m_oldMatch;
std::vector<std::shared_ptr<QtSearchElement>> m_elements; std::vector<QtSearchElement*> m_elements;
std::vector<std::shared_ptr<QtSearchElement>> m_oldElements;
size_t m_cursorIndex; size_t m_cursorIndex;