2018-07-20 19:48:29 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
* gui/advsearch/expressionwidget.cpp *
|
|
|
|
* *
|
|
|
|
* LibResAPI: API for local socket server *
|
|
|
|
* *
|
|
|
|
* Copyright (C) RetroShare Team <retroshare.project@gmail.com> *
|
|
|
|
* *
|
|
|
|
* This program is free software: you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU Affero General Public License as *
|
|
|
|
* published by the Free Software Foundation, either version 3 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 Affero General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU Affero General Public License *
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
|
|
|
|
* *
|
|
|
|
*******************************************************************************/
|
2007-11-15 03:18:48 +00:00
|
|
|
#include "expressionwidget.h"
|
|
|
|
|
|
|
|
ExpressionWidget::ExpressionWidget(QWidget * parent, bool initial) : QWidget(parent)
|
|
|
|
{
|
|
|
|
setupUi(this);
|
|
|
|
|
|
|
|
inRangedConfig = false;
|
|
|
|
|
|
|
|
// the default search type
|
|
|
|
searchType = NameSearch;
|
|
|
|
|
|
|
|
exprLayout = this->layout();
|
|
|
|
|
|
|
|
exprOpFrame->setLayout (createLayout());
|
|
|
|
exprTermFrame->setLayout (createLayout());
|
|
|
|
exprConditionFrame->setLayout (createLayout());
|
|
|
|
exprParamFrame->setLayout (createLayout());
|
|
|
|
exprParamFrame->setSizePolicy(QSizePolicy::MinimumExpanding,
|
|
|
|
QSizePolicy::Fixed);
|
|
|
|
|
|
|
|
elements = new QList<GuiExprElement*>();
|
|
|
|
|
|
|
|
exprOpElem = new ExprOpElement();
|
|
|
|
exprOpFrame->layout()->addWidget(exprOpElem);
|
|
|
|
elements->append(exprOpElem);
|
|
|
|
|
|
|
|
exprTermElem = new ExprTermsElement();
|
|
|
|
exprTermFrame->layout()->addWidget(exprTermElem);
|
|
|
|
elements->append(exprTermElem);
|
|
|
|
connect (exprTermElem, SIGNAL(currentIndexChanged(int)),
|
|
|
|
this, SLOT (adjustExprForTermType(int)));
|
|
|
|
|
|
|
|
exprCondElem = new ExprConditionElement(searchType);
|
|
|
|
exprConditionFrame->layout()->addWidget(exprCondElem);
|
|
|
|
elements->append(exprCondElem);
|
|
|
|
connect (exprCondElem, SIGNAL (currentIndexChanged(int)),
|
|
|
|
this, SLOT (adjustExprForConditionType(int)));
|
|
|
|
|
|
|
|
exprParamElem= new ExprParamElement(searchType);
|
|
|
|
exprParamFrame->layout()->addWidget(exprParamElem);
|
|
|
|
elements->append(exprParamElem);
|
|
|
|
|
|
|
|
// set up the default search: a search on name
|
|
|
|
adjustExprForTermType(searchType);
|
|
|
|
isFirst = initial;
|
|
|
|
deleteExprButton ->setVisible(!isFirst);
|
|
|
|
exprOpElem ->setVisible(!isFirst);
|
|
|
|
exprTermFrame ->show();
|
|
|
|
exprConditionFrame ->show();
|
|
|
|
exprParamFrame ->show();
|
|
|
|
|
|
|
|
// connect the delete button signal
|
|
|
|
connect (deleteExprButton, SIGNAL (clicked()),
|
|
|
|
this, SLOT(deleteExpression()));
|
|
|
|
|
|
|
|
this->show();
|
|
|
|
}
|
|
|
|
|
|
|
|
QLayout * ExpressionWidget::createLayout(QWidget * parent)
|
|
|
|
{
|
|
|
|
QHBoxLayout * hboxLayout;
|
|
|
|
if (parent == 0)
|
|
|
|
{
|
|
|
|
hboxLayout = new QHBoxLayout();
|
|
|
|
} else {
|
|
|
|
hboxLayout = new QHBoxLayout(parent);
|
|
|
|
}
|
|
|
|
hboxLayout->setSpacing(0);
|
|
|
|
hboxLayout->setMargin(0);
|
|
|
|
return hboxLayout;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ExpressionWidget::isStringSearchExpression()
|
|
|
|
{
|
|
|
|
return (searchType == NameSearch || searchType == PathSearch
|
|
|
|
|| searchType == ExtSearch || searchType == HashSearch);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExpressionWidget::adjustExprForTermType(int index)
|
|
|
|
{
|
2015-06-18 16:29:29 +00:00
|
|
|
ExprSearchType type = GuiExprElement::TermsIndexMap[index];
|
2007-11-15 03:18:48 +00:00
|
|
|
searchType = type;
|
|
|
|
|
|
|
|
// now adjust the relevant elements
|
|
|
|
// the condition combobox
|
|
|
|
exprCondElem->adjustForSearchType(type);
|
|
|
|
|
|
|
|
// the parameter expression: can be a date, 1-2 edit fields
|
|
|
|
// or a size with units etc
|
|
|
|
exprParamElem->adjustForSearchType(type);
|
|
|
|
exprParamFrame->adjustSize();
|
|
|
|
|
|
|
|
exprLayout->invalidate();
|
|
|
|
this->adjustSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExpressionWidget::adjustExprForConditionType(int newCondition)
|
|
|
|
{
|
|
|
|
// we adjust the appearance for a ranged selection
|
|
|
|
inRangedConfig = (newCondition == GuiExprElement::RANGE_INDEX);
|
|
|
|
exprParamElem->setRangedSearch(inRangedConfig);
|
|
|
|
exprParamFrame->layout()->invalidate();
|
|
|
|
exprParamFrame->adjustSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExpressionWidget::deleteExpression()
|
|
|
|
{
|
|
|
|
this->hide();
|
|
|
|
emit signalDelete(this);
|
|
|
|
}
|
|
|
|
|
2016-09-13 12:05:22 +02:00
|
|
|
RsRegularExpression::LogicalOperator ExpressionWidget::getOperator()
|
2007-11-15 03:18:48 +00:00
|
|
|
{
|
|
|
|
return exprOpElem->getLogicalOperator();
|
|
|
|
}
|
|
|
|
|
2011-03-15 23:12:46 +00:00
|
|
|
static int checkedConversion(uint64_t s)
|
|
|
|
{
|
|
|
|
if(s > 0x7fffffff)
|
|
|
|
{
|
|
|
|
std::cerr << "Error: bad convertion from uint64_t s=" << s << " into int" << std::endl;
|
|
|
|
return 0 ;
|
|
|
|
}
|
|
|
|
return (int)s ;
|
|
|
|
}
|
|
|
|
|
2016-09-13 12:05:22 +02:00
|
|
|
RsRegularExpression::Expression* ExpressionWidget::getRsExpression()
|
2007-11-15 03:18:48 +00:00
|
|
|
{
|
2016-09-13 12:05:22 +02:00
|
|
|
RsRegularExpression::Expression * expr = NULL;
|
2007-11-15 03:18:48 +00:00
|
|
|
|
|
|
|
std::list<std::string> wordList;
|
2011-03-15 23:12:46 +00:00
|
|
|
uint64_t lowVal = 0;
|
|
|
|
uint64_t highVal = 0;
|
2007-11-15 03:18:48 +00:00
|
|
|
|
|
|
|
if (isStringSearchExpression())
|
|
|
|
{
|
|
|
|
QString txt = exprParamElem->getStrSearchValue();
|
|
|
|
QStringList words = txt.split(" ", QString::SkipEmptyParts);
|
|
|
|
for (int i = 0; i < words.size(); ++i)
|
2011-08-27 16:15:56 +00:00
|
|
|
wordList.push_back(words.at(i).toUtf8().constData());
|
2007-11-15 03:18:48 +00:00
|
|
|
} else if (inRangedConfig){
|
|
|
|
// correct for reversed ranges to be nice to the user
|
|
|
|
lowVal = exprParamElem->getIntLowValue();
|
|
|
|
highVal = exprParamElem->getIntHighValue();
|
|
|
|
if (lowVal >highVal)
|
|
|
|
{
|
2011-03-15 23:12:46 +00:00
|
|
|
lowVal = lowVal^highVal; // csoler: wow, that is some style!
|
2007-11-15 03:18:48 +00:00
|
|
|
highVal = lowVal^highVal;
|
|
|
|
lowVal = lowVal^highVal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (searchType)
|
|
|
|
{
|
|
|
|
case NameSearch:
|
2016-09-13 12:05:22 +02:00
|
|
|
expr = new RsRegularExpression::NameExpression(exprCondElem->getStringOperator(),
|
2007-11-15 03:18:48 +00:00
|
|
|
wordList,
|
|
|
|
exprParamElem->ignoreCase());
|
|
|
|
break;
|
|
|
|
case PathSearch:
|
2016-09-13 12:05:22 +02:00
|
|
|
expr = new RsRegularExpression::PathExpression(exprCondElem->getStringOperator(),
|
2007-11-15 03:18:48 +00:00
|
|
|
wordList,
|
|
|
|
exprParamElem->ignoreCase());
|
|
|
|
break;
|
|
|
|
case ExtSearch:
|
2016-09-13 12:05:22 +02:00
|
|
|
expr = new RsRegularExpression::ExtExpression(exprCondElem->getStringOperator(),
|
2007-11-15 03:18:48 +00:00
|
|
|
wordList,
|
|
|
|
exprParamElem->ignoreCase());
|
|
|
|
break;
|
|
|
|
case HashSearch:
|
2016-09-13 12:05:22 +02:00
|
|
|
expr = new RsRegularExpression::HashExpression(exprCondElem->getStringOperator(),
|
2007-11-15 03:18:48 +00:00
|
|
|
wordList);
|
|
|
|
break;
|
|
|
|
case DateSearch:
|
|
|
|
if (inRangedConfig) {
|
2016-09-13 12:05:22 +02:00
|
|
|
expr = new RsRegularExpression::DateExpression(exprCondElem->getRelOperator(), checkedConversion(lowVal), checkedConversion(highVal));
|
2007-11-15 03:18:48 +00:00
|
|
|
} else {
|
2016-09-13 12:05:22 +02:00
|
|
|
expr = new RsRegularExpression::DateExpression(exprCondElem->getRelOperator(), checkedConversion(exprParamElem->getIntValue()));
|
2007-11-15 03:18:48 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PopSearch:
|
|
|
|
if (inRangedConfig) {
|
2016-09-13 12:05:22 +02:00
|
|
|
expr = new RsRegularExpression::DateExpression(exprCondElem->getRelOperator(), checkedConversion(lowVal), checkedConversion(highVal));
|
2007-11-15 03:18:48 +00:00
|
|
|
} else {
|
2016-09-13 12:05:22 +02:00
|
|
|
expr = new RsRegularExpression::DateExpression(exprCondElem->getRelOperator(), checkedConversion(exprParamElem->getIntValue()));
|
2007-11-15 03:18:48 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SizeSearch:
|
2011-03-15 23:12:46 +00:00
|
|
|
if (inRangedConfig)
|
|
|
|
{
|
|
|
|
if(lowVal >= (uint64_t)(1024*1024*1024) || highVal >= (uint64_t)(1024*1024*1024))
|
2016-09-13 12:05:22 +02:00
|
|
|
expr = new RsRegularExpression::SizeExpressionMB(exprCondElem->getRelOperator(), (int)(lowVal / (1024*1024)), (int)(highVal / (1024*1024)));
|
2011-03-15 23:12:46 +00:00
|
|
|
else
|
2016-09-13 12:05:22 +02:00
|
|
|
expr = new RsRegularExpression::SizeExpression(exprCondElem->getRelOperator(), lowVal, highVal);
|
2011-03-15 23:12:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
uint64_t s = exprParamElem->getIntValue() ;
|
|
|
|
|
|
|
|
if(s >= (uint64_t)(1024*1024*1024))
|
2016-09-13 12:05:22 +02:00
|
|
|
expr = new RsRegularExpression::SizeExpressionMB(exprCondElem->getRelOperator(), (int)(s/(1024*1024))) ;
|
2011-03-15 23:12:46 +00:00
|
|
|
else
|
2016-09-13 12:05:22 +02:00
|
|
|
expr = new RsRegularExpression::SizeExpression(exprCondElem->getRelOperator(), (int)s) ;
|
2007-11-15 03:18:48 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
return expr;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString ExpressionWidget::toString()
|
|
|
|
{
|
|
|
|
QString str = "";
|
|
|
|
if (!isFirst)
|
|
|
|
{
|
|
|
|
str += exprOpElem->toString() + " ";
|
|
|
|
}
|
|
|
|
str += exprTermElem->toString() + " ";
|
|
|
|
str += exprCondElem->toString() + " ";
|
|
|
|
str += exprParamElem->toString();
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|