Files
GoldenCheetah/src/SearchBox.cpp
Mark Liversedge 4c60268245 Data Filter (Part 1 of 3)
Part 1 of Data filtering, this patch adds the ability
to enter and parse filter statements in the search box
(by clicking on the magnifying glass it becomes a filter
box).

The statements can reference metrics and metadata fields
allowing the user to define boolean expressions to filter
data. An example of the syntax;

    Average_Power > 200 and Duration > 3600

This references the metric Average_Power and ride Duration. But
will also support operations on metadata fields, for example;

    Workout_Code endsWith "SST"

The operators are;
    = <> > >= < <= matches contains endswith beginswith
    ( ) && and || or

Filters are syntactically and semantically validated. But at
this point the resulting tree is not evaluated, i.e. we can
parse the filters, but do not execute them.

Two further updates are pending (once written and tested):
- Part 2 of 3 : Execute filters and apply to the ride list
- Part 3 of 3 : Allow named filters and apply to LTM charts

Further updates will support a visual editor and allowing filters
to be applied to CP and Histogram charts and affect the PMC stress
calculators.
2012-10-28 20:05:12 +00:00

168 lines
5.8 KiB
C++

/*
* Copyright (c) 2012 Mark Liversedge (liversedge@gmail.com)
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "SearchBox.h"
#include <QToolButton>
#include <QStyle>
#include <QDebug>
SearchBox::SearchBox(QWidget *parent)
: QLineEdit(parent)
{
//clear button
clearButton = new QToolButton(this);
QPixmap pixmap(":images/toolbar/clear.png");
clearButton->setIcon(QIcon(pixmap));
clearButton->setIconSize(pixmap.size());
clearButton->setCursor(Qt::ArrowCursor);
clearButton->setStyleSheet("QToolButton { border: none; padding: 0px; }");
clearButton->hide();
connect(clearButton, SIGNAL(clicked()), this, SLOT(clear()));
connect(clearButton, SIGNAL(clicked()), this, SLOT(clearClicked()));
connect(this, SIGNAL(textChanged(const QString&)), this, SLOT(updateCloseButton(const QString&)));
// search button
searchButton = new QToolButton(this);
QPixmap search(":images/toolbar/search.png");
searchButton->setIcon(QIcon(search));
searchButton->setIconSize(search.size());
searchButton->setCursor(Qt::ArrowCursor);
searchButton->setStyleSheet("QToolButton { border: none; padding: 0px; }");
connect(searchButton, SIGNAL(clicked()), this, SLOT(toggleMode()));
int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
setStyleSheet(QString( //"QLineEdit { padding-right: %1px; } "
"QLineEdit {"
" selection-color: white; "
//" border: 0px groove gray;"
" border-radius: 5px;"
" padding: 0px %1px;"
"}"
"QLineEdit:focus {"
" selection-color: white; "
//" border: 0px groove gray;"
" border-radius: 5px;"
" padding: 0px %1px;"
"}"
""
"QLineEdit:edit-focus {"
" selection-color: white; "
//" border: 0px groove gray;"
" border-radius: 5px;"
" padding: 0px %1px;"
"}"
).arg(clearButton->sizeHint().width() + frameWidth + 1));
QSize msz = minimumSizeHint();
setMinimumSize(qMax(msz.width(), clearButton->sizeHint().height() + frameWidth * 2 + 2),
qMax(msz.height(), clearButton->sizeHint().height() /* + frameWidth * 2 + -2*/));
setPlaceholderText("Search...");
mode = Search;
setDragEnabled(true);
connect(this, SIGNAL(returnPressed()), this, SLOT(searchSubmit()));
}
void SearchBox::resizeEvent(QResizeEvent *)
{
QSize sz = clearButton->sizeHint();
int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
clearButton->move(rect().right() - frameWidth - sz.width(),
(rect().bottom() + 1 - sz.height())/2);
searchButton->move(rect().left() + frameWidth,
(rect().bottom() + 1 - sz.height())/2);
}
void SearchBox::toggleMode()
{
clear(); // clear whatever is there first
if (mode == Search) setMode(Filter);
else setMode(Search);
}
void SearchBox::setMode(SearchBoxMode mode)
{
switch (mode) {
case Filter:
{
QPixmap filter(":images/toolbar/filter.png");
searchButton->setIcon(QIcon(filter));
searchButton->setIconSize(filter.size());
setPlaceholderText("Filter...");
}
break;
case Search:
default:
{
QPixmap search(":images/toolbar/search.png");
searchButton->setIcon(QIcon(search));
searchButton->setIconSize(search.size());
setPlaceholderText("Search...");
}
break;
}
this->mode = mode;
}
void SearchBox::updateCloseButton(const QString& text)
{
if (clearButton->isVisible() && text.isEmpty()) mode == Search ? clearQuery() : clearFilter();
clearButton->setVisible(!text.isEmpty());
if (mode == Search) searchSubmit(); // only do search as you type in search mode
}
void SearchBox::searchSubmit()
{
// return hit / key pressed
if (text() != "") {
mode == Search ? submitQuery(text()) : submitFilter(text());
}
}
void SearchBox::clearClicked()
{
mode == Search ? clearQuery() : clearFilter();
}
// Drag and drop columns from the chooser...
void
SearchBox::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->data("application/x-columnchooser") != "")
event->acceptProposedAction(); // whatever you wanna drop we will try and process!
else
event->ignore();
}
void
SearchBox::dropEvent(QDropEvent *event)
{
QString name = event->mimeData()->data("application/x-columnchooser");
// fugly, but it works for BikeScore with the (TM) in it...
if (name == "BikeScore?") name = QString("BikeScore&#8482;").replace("&#8482;", QChar(0x2122));
// we do very little to the name, just space to _ and lower case it for now...
name.replace(' ', '_');
insert(name + (mode == Search ? ":\"\"" : ""));
}