Textractor_test/extensions/threadlinker.cpp

80 lines
2.5 KiB
C++
Raw Permalink Normal View History

2019-06-29 11:16:31 +08:00
#include "qtcommon.h"
2019-01-23 03:35:34 +08:00
#include "extension.h"
2021-08-18 14:22:23 +08:00
#include "ui_threadlinker.h"
2019-01-23 03:35:34 +08:00
#include <QKeyEvent>
2019-02-28 00:33:17 +08:00
extern const char* THREAD_LINKER;
extern const char* LINK;
extern const char* UNLINK;
2019-02-28 00:33:17 +08:00
extern const char* THREAD_LINK_FROM;
extern const char* THREAD_LINK_TO;
2019-06-13 16:01:29 +08:00
extern const char* HEXADECIMAL;
2019-02-28 00:33:17 +08:00
2021-08-18 14:22:23 +08:00
std::unordered_map<int64_t, std::unordered_set<int64_t>> links;
std::unordered_set<int64_t> universalLinks, empty;
2021-09-06 16:02:36 +08:00
bool separateSentences = false; // allow user to change?
2021-06-05 16:58:53 +08:00
concurrency::reader_writer_lock m;
2019-01-23 03:35:34 +08:00
class Window : public QDialog, Localizer
2019-01-23 03:35:34 +08:00
{
2019-06-21 13:29:48 +08:00
public:
2021-01-31 03:07:37 +08:00
Window() : QDialog(nullptr, Qt::WindowMinMaxButtonsHint)
2019-01-23 03:35:34 +08:00
{
2021-08-18 14:22:23 +08:00
ui.setupUi(this);
ui.linkButton->setText(LINK);
ui.unlinkButton->setText(UNLINK);
connect(ui.linkButton, &QPushButton::clicked, this, &Window::Link);
connect(ui.unlinkButton, &QPushButton::clicked, this, &Window::Unlink);
2019-01-23 03:35:34 +08:00
setWindowTitle(THREAD_LINKER);
2019-06-21 13:29:48 +08:00
QMetaObject::invokeMethod(this, &QWidget::show, Qt::QueuedConnection);
2019-01-23 03:35:34 +08:00
}
2019-06-21 13:29:48 +08:00
private:
void Link()
2019-01-23 03:35:34 +08:00
{
2019-06-21 13:29:48 +08:00
bool ok1, ok2, ok3, ok4;
2021-09-06 16:02:36 +08:00
QString fromInput = QInputDialog::getText(this, THREAD_LINK_FROM, HEXADECIMAL, QLineEdit::Normal, "All", &ok1, Qt::WindowCloseButtonHint);
int from = fromInput.toInt(&ok2, 16);
if (ok1 && (fromInput == "All" || ok2))
2019-01-23 03:35:34 +08:00
{
2021-09-06 16:02:36 +08:00
int to = QInputDialog::getText(this, THREAD_LINK_TO, HEXADECIMAL, QLineEdit::Normal, "", &ok3, Qt::WindowCloseButtonHint).toInt(&ok4, 16);
if (ok3 && ok4)
{
std::scoped_lock lock(m);
if ((ok2 ? links[from] : universalLinks).insert(to).second)
ui.linkList->addItem((ok2 ? QString::number(from, 16) : "All") + "->" + QString::number(to, 16));
}
2019-06-21 13:29:48 +08:00
}
2019-01-23 03:35:34 +08:00
}
2019-06-21 13:29:48 +08:00
void Unlink()
2019-01-23 03:35:34 +08:00
{
2021-08-18 14:22:23 +08:00
if (ui.linkList->currentItem())
2019-01-23 03:35:34 +08:00
{
2021-08-18 14:22:23 +08:00
QStringList link = ui.linkList->currentItem()->text().split("->");
ui.linkList->takeItem(ui.linkList->currentRow());
std::scoped_lock lock(m);
2021-09-06 16:02:36 +08:00
(link[0] == "All" ? universalLinks : links[link[0].toInt(nullptr, 16)]).erase(link[1].toInt(nullptr, 16));
2019-01-23 03:35:34 +08:00
}
}
2019-06-21 13:29:48 +08:00
void keyPressEvent(QKeyEvent* event) override
{
if (event->key() == Qt::Key_Delete) Unlink();
}
2021-08-18 14:22:23 +08:00
Ui::LinkWindow ui;
2019-06-21 13:29:48 +08:00
} window;
2019-01-23 03:35:34 +08:00
bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
{
2021-06-05 16:58:53 +08:00
concurrency::reader_writer_lock::scoped_lock_read readLock(m);
2021-08-18 14:22:23 +08:00
auto action = separateSentences ? sentenceInfo["add sentence"] : sentenceInfo["add text"];
auto it = links.find(sentenceInfo["text number"]);
2021-09-06 16:02:36 +08:00
for (const auto& linkSet : { it != links.end() ? it->second : empty, sentenceInfo["text number"] > 1 ? universalLinks : empty })
2021-08-18 14:22:23 +08:00
for (auto link : linkSet)
((void(*)(int64_t, const wchar_t*))action)(link, sentence.c_str());
return false;
2019-01-23 03:35:34 +08:00
}