Textractor/extensions/regexfilter.cpp

44 lines
964 B
C++
Raw Normal View History

2019-06-29 08:46:31 +05:30
#include "qtcommon.h"
#include "extension.h"
#include "ui_regexfilter.h"
2019-02-27 11:33:17 -05:00
extern const char* REGEX_FILTER;
extern const char* INVALID_REGEX;
extern const char* CURRENT_FILTER;
std::wregex regex;
2019-01-23 13:59:34 -05:00
std::shared_mutex m;
2019-06-21 01:29:48 -04:00
class Window : public QMainWindow
{
2019-06-21 01:29:48 -04:00
public:
Window()
{
ui.setupUi(this);
2019-06-21 01:29:48 -04:00
connect(ui.input, &QLineEdit::textEdited, this, &Window::setRegex);
setWindowTitle(REGEX_FILTER);
2019-06-21 01:29:48 -04:00
QMetaObject::invokeMethod(this, &QWidget::show, Qt::QueuedConnection);
}
2019-06-21 01:29:48 -04:00
private:
2019-06-29 08:46:31 +05:30
void setRegex(QString regex)
{
2019-06-21 01:29:48 -04:00
std::lock_guard l(m);
2019-06-29 08:46:31 +05:30
try { ::regex = S(regex); }
2019-06-21 01:29:48 -04:00
catch (std::regex_error) { return ui.output->setText(INVALID_REGEX); }
2019-06-29 08:46:31 +05:30
ui.output->setText(QString(CURRENT_FILTER).arg(regex));
}
2019-06-21 01:29:48 -04:00
Ui::FilterWindow ui;
} window;
bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
{
2019-02-21 13:09:44 -05:00
if (sentenceInfo["text number"] == 0) return false;
2019-06-21 01:29:48 -04:00
std::shared_lock l(m);
sentence = std::regex_replace(sentence, regex, L"");
return true;
}