ui: Colors in SearchView

Show the right colors in SearchView for the different NodeTypes or QueryNodeTyps

Note: add colors to your Applcationsettings like in the template

fortune cookie message = recognition will come from unexpected sources
This commit is contained in:
Andreas Stallinger
2015-03-04 11:55:25 +01:00
parent 284fb336a6
commit da1a6d69c4
32 changed files with 455 additions and 100 deletions
+28 -5
View File
@@ -3,6 +3,8 @@
#include <QPainter>
#include <QScrollBar>
#include "ApplicationSettings.h"
QtAutocompletionModel::QtAutocompletionModel(QObject* parent)
: QAbstractTableModel(parent)
{
@@ -26,7 +28,7 @@ int QtAutocompletionModel::rowCount(const QModelIndex &parent) const
int QtAutocompletionModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 3;
return 4;
}
QVariant QtAutocompletionModel::data(const QModelIndex &index, int role) const
@@ -53,6 +55,8 @@ QVariant QtAutocompletionModel::data(const QModelIndex &index, int role) const
}
return indices;
}
case 3:
return match.nodeType;
default:
return QVariant();
}
@@ -69,7 +73,7 @@ const SearchMatch* QtAutocompletionModel::getSearchMatchAt(int idx) const
QtAutocompletionDelegate::QtAutocompletionDelegate(QObject* parent)
: QItemDelegate(parent)
: QStyledItemDelegate(parent)
{
}
@@ -79,6 +83,8 @@ QtAutocompletionDelegate::~QtAutocompletionDelegate()
void QtAutocompletionDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
painter->save();
if (option.state & QStyle::State_Selected)
{
painter->fillRect(option.rect, option.palette.color(QPalette::Highlight));
@@ -92,12 +98,19 @@ void QtAutocompletionDelegate::paint(QPainter* painter, const QStyleOptionViewIt
float charWidth = option.fontMetrics.width("FontWidth") / 9.0f;
QString type = index.sibling(index.row(), index.column() + 1).data().toString();
QColor color("#FFFFFF");
QColor color("#FFE47A");
if (type.size())
Node::NodeType nodeType = static_cast<Node::NodeType>(index.sibling(index.row(), index.column() + 3).data().toInt());
if(type.size() && nodeType)
{
color = QColor("#CC8D91");
color = QColor(ApplicationSettings::getInstance()->getNodeTypeColor(nodeType).c_str());
}
else
{
color = QColor(ApplicationSettings::getInstance()->getQueryNodeTypeColor(QueryNode::QUERYNODETYPE_COMMAND).c_str());
}
QList<QVariant> indices = index.sibling(index.row(), index.column() + 2).data().toList();
if (indices.size())
@@ -146,8 +159,16 @@ void QtAutocompletionDelegate::paint(QPainter* painter, const QStyleOptionViewIt
painter->setFont(font);
painter->setPen(pen);
}
painter->restore();
}
QSize QtAutocompletionDelegate::sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
QString name = index.data().toString();
QString type = index.sibling(index.row(), index.column() + 1).data().toString();
return QSize( option.fontMetrics.width(name+type)+5, option.fontMetrics.height());
}
QtAutocompletionList::QtAutocompletionList(QWidget* parent)
: QCompleter(parent)
@@ -172,6 +193,7 @@ QtAutocompletionList::~QtAutocompletionList()
{
}
void QtAutocompletionList::completeAt(const QPoint& pos, const std::vector<SearchMatch>& autocompletionList)
{
m_model->setMatchList(autocompletionList);
@@ -191,6 +213,7 @@ void QtAutocompletionList::completeAt(const QPoint& pos, const std::vector<Searc
list->setCurrentIndex(index);
complete(QRect(pos.x(), pos.y(), minSize.width(), 1));
//complete();
QRect rect = list->visualRect(index);
minSize.setHeight(std::min(minSize.height(), m_model->rowCount(index) * rect.height() + 16));
+3 -2
View File
@@ -6,7 +6,7 @@
#include <QAbstractTableModel>
#include <QCompleter>
#include <QItemDelegate>
#include <QStyledItemDelegate>
#include <QListView>
#include "data/search/SearchMatch.h"
@@ -35,13 +35,14 @@ private:
class QtAutocompletionDelegate
: public QItemDelegate
: public QStyledItemDelegate
{
public:
explicit QtAutocompletionDelegate(QObject* parent = 0);
virtual ~QtAutocompletionDelegate();
virtual void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const;
virtual QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const;
};
+5
View File
@@ -64,6 +64,11 @@ void QtSearchBar::setText(const std::string& text)
m_searchBox->setQuery(text);
}
void QtSearchBar::setMatch(const SearchMatch& match)
{
m_searchBox->setQuery(match);
}
void QtSearchBar::setFocus()
{
m_searchBox->setFocus();
+1
View File
@@ -23,6 +23,7 @@ public:
virtual QSize sizeHint() const;
void setText(const std::string& text);
void setMatch(const SearchMatch& match);
void setFocus();
void setAutocompletionList(const std::vector<SearchMatch>& autocompletionList);
+64 -27
View File
@@ -6,6 +6,9 @@
#include <QClipboard>
#include <QKeyEvent>
#include "ApplicationSettings.h"
#include "utility/logging/logging.h"
#include "utility/messaging/type/MessageSearch.h"
#include "utility/messaging/type/MessageSearchAutocomplete.h"
#include "utility/text/TextAccess.h"
@@ -14,6 +17,8 @@
#include "data/query/QueryTree.h"
#include "qt/element/QtAutocompletionList.h"
#include <iostream>
QtQueryElement::QtQueryElement(const QString& text, QWidget* parent)
: QPushButton(text, parent)
{
@@ -31,7 +36,8 @@ void QtQueryElement::onChecked(bool)
void QtSmartSearchBox::search()
{
editTextToElement();
MessageSearch(utility::join(m_tokens, "") + text().toStdString()).dispatch();
LOG_INFO(utility::join(SearchMatch::searchMatchDequeToStringDeque(m_tokens), "")+ text().toStdString());
MessageSearch(utility::join(SearchMatch::searchMatchDequeToStringDeque(m_tokens), "") + text().toStdString()).dispatch();
}
QtSmartSearchBox::QtSmartSearchBox(QWidget* parent)
@@ -72,10 +78,19 @@ void QtSmartSearchBox::setAutocompletionList(const std::vector<SearchMatch>& aut
}
}
void QtSmartSearchBox::setQuery(const SearchMatch& match)
{
clearLineEdit();
m_tokens.clear();
m_tokens.push_back(match);
m_cursorIndex = m_tokens.size();
updateElements();
}
void QtSmartSearchBox::setQuery(const std::string& text)
{
clearLineEdit();
m_tokens = QueryTree::tokenizeQuery(text);
m_tokens = SearchMatch::stringDequeToSearchMatchDeque(QueryTree::tokenizeQuery(text));
m_cursorIndex = m_tokens.size();
updateElements();
@@ -343,24 +358,25 @@ void QtSmartSearchBox::onTextEdited(const QString& text)
bool tokensChanged = false;
std::string token;
std::deque<std::string> tokens = QueryTree::tokenizeQuery(text.toStdString());
SearchMatch token;
std::deque<SearchMatch> tokens = SearchMatch::stringDequeToSearchMatchDeque(QueryTree::tokenizeQuery(text.toStdString()));
while (tokens.size())
{
token = tokens.front();
tokens.pop_front();
if (tokens.size() || QueryTree::getTokenTypeName(token) != "none")
if (tokens.size() || token.queryNodeType != QueryNode::QUERYNODETYPE_NONE)
{
textToToken(token);
token.clear();
matchToToken(token);
token.decodeFromQuery("");
tokensChanged = true;
}
}
if (tokensChanged)
{
setEditText(QString::fromStdString(token));
setEditText(QString::fromStdString(token.fullName));
updateElements();
}
else
@@ -368,7 +384,7 @@ void QtSmartSearchBox::onTextEdited(const QString& text)
layoutElements();
}
if (token.size() || m_elements.size())
if (token.fullName.size() || m_elements.size())
{
requestAutoCompletions();
}
@@ -466,23 +482,25 @@ void QtSmartSearchBox::moveCursorTo(int target)
}
}
void QtSmartSearchBox::textToToken(std::string text)
void QtSmartSearchBox::matchToToken(const SearchMatch& match)
{
if (!text.size())
if(!match.fullName.size())
{
return;
}
if (completer()->popup()->isVisible())
const SearchMatch* matchPtr = &match;
if(completer()->popup()->isVisible())
{
const SearchMatch* match = dynamic_cast<QtAutocompletionList*>(completer())->getSearchMatchAt(0);
if (match && utility::equalsCaseInsensitive(text, match->fullName))
const SearchMatch* m = dynamic_cast<QtAutocompletionList*>(completer())->getSearchMatchAt(0);
if (m && utility::equalsCaseInsensitive(match.fullName, m->fullName))
{
text = match->encodeForQuery();
matchPtr = m;
}
}
m_tokens.insert(m_tokens.begin() + m_cursorIndex, text);
m_tokens.insert(m_tokens.begin() + m_cursorIndex, *matchPtr);
m_cursorIndex++;
}
@@ -493,8 +511,7 @@ void QtSmartSearchBox::searchMatchToToken(const SearchMatch& match)
m_oldText.clear();
clearLineEdit();
std::string name = match.encodeForQuery();
textToToken(name);
matchToToken(match);
updateElements();
}
}
@@ -509,7 +526,7 @@ bool QtSmartSearchBox::editTextToElement()
{
if (text().size())
{
textToToken(text().toStdString());
matchToToken(SearchMatch(text().toStdString()));
clearLineEdit();
updateElements();
@@ -530,7 +547,7 @@ void QtSmartSearchBox::editElement(QtQueryElement* element)
}
}
std::string token = QueryTree::getTokenName(m_tokens[m_cursorIndex]);
std::string token = m_tokens[m_cursorIndex].fullName;
m_tokens.erase(m_tokens.begin() + m_cursorIndex);
setEditText(QString::fromStdString(token));
@@ -543,16 +560,37 @@ void QtSmartSearchBox::updateElements()
{
m_elements.clear();
for (const std::string& token : m_tokens)
for (const SearchMatch& token : m_tokens)
{
std::string name = QueryTree::getTokenName(token);
std::string name = token.fullName;
name = utility::replace(name, "&", "&&");
std::shared_ptr<QtQueryElement> element = std::make_shared<QtQueryElement>(QString::fromStdString(name), this);
m_elements.push_back(element);
std::string color = "#000000";
std::string hoverColor = "#000000";
element->setObjectName(QString::fromStdString("search_element_" + QueryTree::getTokenTypeName(token)));
if(token.queryNodeType == QueryNode::QUERYNODETYPE_TOKEN)
{
element->setObjectName(QString::fromStdString("search_element_" + token.getNodeTypeAsString()));
color = ApplicationSettings::getInstance()->getNodeTypeColor(token.nodeType);
hoverColor = ApplicationSettings::getInstance()->getNodeTypeColor(token.nodeType, "hover");
}
else
{
element->setObjectName(QString::fromStdString("search_element_" + QueryTree::getTokenTypeName(token.queryNodeType)));
color = ApplicationSettings::getInstance()->getQueryNodeTypeColor(token.queryNodeType);
hoverColor = ApplicationSettings::getInstance()->getQueryNodeTypeColor(token.queryNodeType, "hover");
}
std::string stylesheet = "QPushButton {background-color:";
stylesheet += color;
stylesheet += ";} QPushButton:hover{ background-color:";
stylesheet += hoverColor;
stylesheet += ";}";
element->setStyleSheet(stylesheet.c_str());
connect(element.get(), SIGNAL(wasChecked(QtQueryElement*)), this, SLOT(onElementSelected(QtQueryElement*)));
}
@@ -650,7 +688,6 @@ bool QtSmartSearchBox::hasSelectedElements() const
return true;
}
}
return false;
}
@@ -661,7 +698,7 @@ std::string QtSmartSearchBox::getSelectedString() const
{
if (m_elements[i]->isChecked())
{
str += m_tokens[i];
str += m_tokens[i].encodeForQuery();
}
}
return str;
@@ -748,7 +785,7 @@ void QtSmartSearchBox::requestAutoCompletions() const
for (size_t i = 0; i < m_tokens.size() && i < m_cursorIndex; i++)
{
query += m_tokens[i];
query += m_tokens[i].encodeForQuery();
}
MessageSearchAutocomplete(query, text().toStdString()).dispatch();
+3 -2
View File
@@ -39,6 +39,7 @@ public:
virtual ~QtSmartSearchBox();
void setAutocompletionList(const std::vector<SearchMatch>& autocompletionList);
void setQuery(const SearchMatch& match);
void setQuery(const std::string& text);
void setFocus();
@@ -65,7 +66,7 @@ private:
void moveCursor(int offset);
void moveCursorTo(int goal);
void textToToken(std::string text);
void matchToToken(const SearchMatch& match);
void searchMatchToToken(const SearchMatch& match);
void setEditText(const QString& text);
bool editTextToElement();
@@ -90,7 +91,7 @@ private:
bool m_allowTextChange;
QString m_oldText;
std::deque<std::string> m_tokens;
std::deque<SearchMatch> m_tokens;
std::vector<std::shared_ptr<QtQueryElement>> m_elements;
size_t m_cursorIndex;
+11
View File
@@ -8,6 +8,7 @@ QtSearchView::QtSearchView(ViewLayout* viewLayout)
: SearchView(viewLayout)
, m_refreshViewFunctor(std::bind(&QtSearchView::doRefreshView, this))
, m_setTextFunctor(std::bind(&QtSearchView::doSetText, this, std::placeholders::_1))
, m_setMatchFunctor(std::bind(&QtSearchView::doSetMatch, this, std::placeholders::_1))
, m_setFocusFunctor(std::bind(&QtSearchView::doSetFocus, this))
, m_setAutocompletionListFunctor(std::bind(&QtSearchView::doSetAutocompletionList, this, std::placeholders::_1))
{
@@ -38,6 +39,11 @@ void QtSearchView::setText(const std::string& text)
m_setTextFunctor(text);
}
void QtSearchView::setMatch(const SearchMatch& match)
{
m_setMatchFunctor(match);
}
void QtSearchView::setFocus()
{
m_setFocusFunctor();
@@ -53,6 +59,11 @@ void QtSearchView::doRefreshView()
setStyleSheet();
}
void QtSearchView::doSetMatch(const SearchMatch& match)
{
m_widget->setMatch(match);
}
void QtSearchView::doSetText(const std::string& text)
{
m_widget->setText(text);
+3
View File
@@ -20,11 +20,13 @@ public:
// SearchView implementation
virtual void setText(const std::string& text);
virtual void setMatch(const SearchMatch& match);
virtual void setFocus();
virtual void setAutocompletionList(const std::vector<SearchMatch>& autocompletionList);
private:
void doRefreshView();
void doSetMatch(const SearchMatch& match);
void doSetText(const std::string& text);
void doSetFocus();
void doSetAutocompletionList(const std::vector<SearchMatch>& autocompletionList);
@@ -33,6 +35,7 @@ private:
QtThreadedFunctor<> m_refreshViewFunctor;
QtThreadedFunctor<const std::string&> m_setTextFunctor;
QtThreadedFunctor<const SearchMatch&> m_setMatchFunctor;
QtThreadedFunctor<> m_setFocusFunctor;
QtThreadedFunctor<const std::vector<SearchMatch>&> m_setAutocompletionListFunctor;
@@ -4,6 +4,8 @@
#include <QGraphicsSceneEvent>
#include <QPen>
#include "ApplicationSettings.h"
#include "utility/messaging/type/MessageActivateTokens.h"
#include "utility/messaging/type/MessageGraphNodeMove.h"
@@ -337,7 +339,7 @@ void QtGraphNode::setStyle()
m_rect->setShadow(QColor(0, 0, 0, 128), 5);
}
color = "#ededed";
color = ApplicationSettings::getInstance()->getNodeTypeColor(m_data->getType()).c_str();
if (m_subNodes.size())
{
radius = 20.0f;
@@ -359,12 +361,12 @@ void QtGraphNode::setStyle()
case Node::NODE_METHOD:
if (m_isActive || m_isHovering)
{
color = "#ffcc00";
color = ApplicationSettings::getInstance()->getNodeTypeColor(m_data->getType(), "hover").c_str();
font.setWeight(QFont::Bold);
}
else
{
color = "#ffe47a";
color = ApplicationSettings::getInstance()->getNodeTypeColor(m_data->getType()).c_str();
}
radius = 8.0f;
@@ -380,12 +382,12 @@ void QtGraphNode::setStyle()
case Node::NODE_ENUM_CONSTANT:
if (m_isActive || m_isHovering)
{
color = "#62b29d";
color = ApplicationSettings::getInstance()->getNodeTypeColor(m_data->getType(), "hover").c_str();
font.setWeight(QFont::Bold);
}
else
{
color = "#9ab4ad";
color = ApplicationSettings::getInstance()->getNodeTypeColor(m_data->getType()).c_str();
}
radius = 8.0f;