2019-06-29 11:16:31 +08:00
|
|
|
#include "qtcommon.h"
|
2018-12-22 04:11:12 +08:00
|
|
|
#include "extension.h"
|
2019-06-21 13:29:48 +08:00
|
|
|
#include "ui_extrawindow.h"
|
2018-12-31 11:10:56 +08:00
|
|
|
#include "defs.h"
|
2020-02-17 08:58:09 +08:00
|
|
|
#include "blockmarkup.h"
|
2019-09-06 01:42:30 +08:00
|
|
|
#include <fstream>
|
|
|
|
#include <process.h>
|
2020-02-26 15:59:47 +08:00
|
|
|
#include <QRegularExpression>
|
2018-12-31 11:10:56 +08:00
|
|
|
#include <QColorDialog>
|
2019-07-13 21:45:43 +08:00
|
|
|
#include <QFontDialog>
|
2018-12-31 11:10:56 +08:00
|
|
|
#include <QMenu>
|
|
|
|
#include <QPainter>
|
2019-11-11 01:48:32 +08:00
|
|
|
#include <QGraphicsEffect>
|
2020-02-23 11:58:47 +08:00
|
|
|
#include <QFontMetrics>
|
2018-12-31 11:10:56 +08:00
|
|
|
#include <QMouseEvent>
|
2019-08-12 22:43:34 +08:00
|
|
|
#include <QWheelEvent>
|
2018-12-22 04:11:12 +08:00
|
|
|
|
2019-02-28 00:33:17 +08:00
|
|
|
extern const char* EXTRA_WINDOW_INFO;
|
|
|
|
extern const char* TOPMOST;
|
2019-12-29 21:54:55 +08:00
|
|
|
extern const char* OPACITY;
|
2019-06-16 05:30:41 +08:00
|
|
|
extern const char* SHOW_ORIGINAL;
|
2019-06-17 03:50:17 +08:00
|
|
|
extern const char* SHOW_ORIGINAL_INFO;
|
2019-02-28 00:33:17 +08:00
|
|
|
extern const char* SIZE_LOCK;
|
2019-09-06 01:42:30 +08:00
|
|
|
extern const char* DICTIONARY;
|
|
|
|
extern const char* DICTIONARY_INSTRUCTIONS;
|
2019-02-28 00:33:17 +08:00
|
|
|
extern const char* BG_COLOR;
|
|
|
|
extern const char* TEXT_COLOR;
|
2019-11-11 01:48:32 +08:00
|
|
|
extern const char* TEXT_OUTLINE;
|
|
|
|
extern const char* OUTLINE_COLOR;
|
|
|
|
extern const char* OUTLINE_SIZE;
|
|
|
|
extern const char* OUTLINE_SIZE_INFO;
|
2019-06-17 12:42:42 +08:00
|
|
|
extern const char* FONT;
|
|
|
|
extern const char* SAVE_SETTINGS;
|
2019-02-28 00:33:17 +08:00
|
|
|
|
2019-09-06 01:42:30 +08:00
|
|
|
constexpr auto DICTIONARY_SAVE_FILE = u8"SavedDictionary.txt";
|
|
|
|
|
2019-12-29 21:54:55 +08:00
|
|
|
QColor colorPrompt(QWidget* parent, QColor default, const QString& title, bool customOpacity = true)
|
|
|
|
{
|
|
|
|
QColor color = QColorDialog::getColor(default, parent, title);
|
|
|
|
if (customOpacity) color.setAlpha(255 * QInputDialog::getDouble(parent, title, OPACITY, default.alpha() / 255.0, 0, 1, 3, nullptr, Qt::WindowCloseButtonHint));
|
|
|
|
return color;
|
|
|
|
}
|
|
|
|
|
2019-09-06 01:42:30 +08:00
|
|
|
struct PrettyWindow : QDialog
|
2018-12-22 04:11:12 +08:00
|
|
|
{
|
2019-09-06 01:42:30 +08:00
|
|
|
PrettyWindow(const char* name)
|
2018-12-22 04:11:12 +08:00
|
|
|
{
|
2019-06-21 13:29:48 +08:00
|
|
|
ui.setupUi(this);
|
2019-11-11 01:48:32 +08:00
|
|
|
ui.display->setGraphicsEffect(&outliner);
|
2018-12-31 11:10:56 +08:00
|
|
|
setWindowFlags(Qt::FramelessWindowHint);
|
|
|
|
setAttribute(Qt::WA_TranslucentBackground);
|
2019-06-21 13:29:48 +08:00
|
|
|
|
2019-09-06 01:42:30 +08:00
|
|
|
settings.beginGroup(name);
|
|
|
|
QFont font = ui.display->font();
|
|
|
|
if (font.fromString(settings.value(FONT, font.toString()).toString())) ui.display->setFont(font);
|
|
|
|
setBgColor(settings.value(BG_COLOR, bgColor).value<QColor>());
|
|
|
|
setTextColor(settings.value(TEXT_COLOR, textColor()).value<QColor>());
|
2019-11-11 01:48:32 +08:00
|
|
|
outliner.color = settings.value(OUTLINE_COLOR, outliner.color).value<QColor>();
|
|
|
|
outliner.size = settings.value(OUTLINE_SIZE, outliner.size).toDouble();
|
2019-09-06 01:42:30 +08:00
|
|
|
menu.addAction(FONT, this, &PrettyWindow::RequestFont);
|
2019-12-29 21:54:55 +08:00
|
|
|
menu.addAction(BG_COLOR, [this] { setBgColor(colorPrompt(this, bgColor, BG_COLOR)); });
|
|
|
|
menu.addAction(TEXT_COLOR, [this] { setTextColor(colorPrompt(this, textColor(), TEXT_COLOR)); });
|
2019-11-11 01:48:32 +08:00
|
|
|
QAction* outlineAction = menu.addAction(TEXT_OUTLINE, this, &PrettyWindow::setOutline);
|
|
|
|
outlineAction->setCheckable(true);
|
|
|
|
outlineAction->setChecked(outliner.size >= 0);
|
2019-09-06 01:42:30 +08:00
|
|
|
connect(ui.display, &QLabel::customContextMenuRequested, [this](QPoint point) { menu.exec(mapToGlobal(point)); });
|
2018-12-31 11:10:56 +08:00
|
|
|
}
|
|
|
|
|
2019-09-06 01:42:30 +08:00
|
|
|
~PrettyWindow()
|
2019-06-21 13:29:48 +08:00
|
|
|
{
|
|
|
|
settings.sync();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ui::ExtraWindow ui;
|
2019-09-06 01:42:30 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
QMenu menu{ ui.display };
|
2019-06-21 13:29:48 +08:00
|
|
|
QSettings settings{ CONFIG_FILE, QSettings::IniFormat, this };
|
2019-01-02 11:40:29 +08:00
|
|
|
|
|
|
|
private:
|
2019-06-21 13:29:48 +08:00
|
|
|
void RequestFont()
|
|
|
|
{
|
2019-07-13 21:45:43 +08:00
|
|
|
if (QFont font = QFontDialog::getFont(&ok, ui.display->font(), this, FONT); ok)
|
|
|
|
{
|
|
|
|
settings.setValue(FONT, font.toString());
|
|
|
|
ui.display->setFont(font);
|
|
|
|
}
|
2019-06-21 13:29:48 +08:00
|
|
|
};
|
|
|
|
|
2019-09-06 01:42:30 +08:00
|
|
|
void setBgColor(QColor color)
|
2019-06-21 13:29:48 +08:00
|
|
|
{
|
|
|
|
if (!color.isValid()) return;
|
|
|
|
if (color.alpha() == 0) color.setAlpha(1);
|
|
|
|
bgColor = color;
|
|
|
|
repaint();
|
2019-11-11 01:48:32 +08:00
|
|
|
settings.setValue(BG_COLOR, color.name(QColor::HexArgb));
|
2019-06-21 13:29:48 +08:00
|
|
|
};
|
|
|
|
|
2019-09-06 01:42:30 +08:00
|
|
|
QColor textColor()
|
|
|
|
{
|
|
|
|
return ui.display->palette().color(QPalette::WindowText);
|
|
|
|
}
|
|
|
|
|
2019-06-21 13:29:48 +08:00
|
|
|
void setTextColor(QColor color)
|
|
|
|
{
|
|
|
|
if (!color.isValid()) return;
|
2019-09-06 01:42:30 +08:00
|
|
|
ui.display->setPalette(QPalette(color, {}, {}, {}, {}, {}, {}));
|
2019-11-11 01:48:32 +08:00
|
|
|
settings.setValue(TEXT_COLOR, color.name(QColor::HexArgb));
|
2019-06-21 13:29:48 +08:00
|
|
|
};
|
|
|
|
|
2019-11-11 01:48:32 +08:00
|
|
|
void setOutline(bool enable)
|
|
|
|
{
|
|
|
|
if (enable)
|
|
|
|
{
|
2019-12-29 21:54:55 +08:00
|
|
|
QColor color = colorPrompt(this, outliner.color, OUTLINE_COLOR);
|
2019-11-11 01:48:32 +08:00
|
|
|
if (color.isValid()) outliner.color = color;
|
|
|
|
outliner.size = QInputDialog::getDouble(this, OUTLINE_SIZE, OUTLINE_SIZE_INFO, 0.5, 0, INT_MAX, 2, nullptr, Qt::WindowCloseButtonHint);
|
|
|
|
}
|
|
|
|
else outliner.size = -1;
|
|
|
|
settings.setValue(OUTLINE_COLOR, outliner.color.name(QColor::HexArgb));
|
|
|
|
settings.setValue(OUTLINE_SIZE, outliner.size);
|
|
|
|
}
|
|
|
|
|
2019-09-06 01:42:30 +08:00
|
|
|
void paintEvent(QPaintEvent*) override
|
|
|
|
{
|
|
|
|
QPainter(this).fillRect(rect(), bgColor);
|
|
|
|
}
|
|
|
|
|
|
|
|
QColor bgColor{ palette().window().color() };
|
2019-11-11 01:48:32 +08:00
|
|
|
struct : QGraphicsEffect
|
|
|
|
{
|
|
|
|
void draw(QPainter* painter) override
|
|
|
|
{
|
|
|
|
if (size < 0) return drawSource(painter);
|
|
|
|
QPoint offset;
|
|
|
|
QPixmap pixmap = sourcePixmap(Qt::LogicalCoordinates, &offset);
|
|
|
|
offset.setX(offset.x() + size);
|
|
|
|
for (auto offset2 : Array<QPointF>{ { 0, 1 }, { 0, -1 }, { 1, 0 }, { -1, 0 }, { 1, 1 }, { 1, -1 }, { -1, 1 }, { -1, -1 } })
|
|
|
|
{
|
|
|
|
QImage outline = pixmap.toImage();
|
|
|
|
QPainter outlinePainter(&outline);
|
|
|
|
outlinePainter.setCompositionMode(QPainter::CompositionMode_SourceIn);
|
|
|
|
outlinePainter.fillRect(outline.rect(), color);
|
|
|
|
painter->drawImage(offset + offset2 * size, outline);
|
|
|
|
}
|
|
|
|
painter->drawPixmap(offset, pixmap);
|
|
|
|
}
|
|
|
|
QColor color{ Qt::black };
|
|
|
|
double size = -1;
|
|
|
|
} outliner;
|
2019-09-06 01:42:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class ExtraWindow : public PrettyWindow
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ExtraWindow() :
|
|
|
|
PrettyWindow("Extra Window")
|
|
|
|
{
|
2019-09-26 19:07:58 +08:00
|
|
|
ui.display->setTextFormat(Qt::PlainText);
|
2020-02-23 11:58:47 +08:00
|
|
|
if (settings.contains(WINDOW) && QGuiApplication::screenAt(settings.value(WINDOW).toRect().bottomRight())) setGeometry(settings.value(WINDOW).toRect());
|
2019-09-06 01:42:30 +08:00
|
|
|
|
2020-01-19 14:25:57 +08:00
|
|
|
for (auto [name, default, slot] : Array<const char*, bool, void(ExtraWindow::*)(bool)>{
|
2019-09-06 01:42:30 +08:00
|
|
|
{ TOPMOST, false, &ExtraWindow::setTopmost },
|
|
|
|
{ SIZE_LOCK, false, &ExtraWindow::setLock },
|
|
|
|
{ SHOW_ORIGINAL, true, &ExtraWindow::setShowOriginal },
|
|
|
|
{ DICTIONARY, false, &ExtraWindow::setUseDictionary },
|
|
|
|
})
|
|
|
|
{
|
|
|
|
// delay processing anything until Textractor has finished initializing
|
|
|
|
QMetaObject::invokeMethod(this, std::bind(slot, this, default = settings.value(name, default).toBool()), Qt::QueuedConnection);
|
|
|
|
auto action = menu.addAction(name, this, slot);
|
|
|
|
action->setCheckable(true);
|
|
|
|
action->setChecked(default);
|
|
|
|
}
|
|
|
|
ui.display->installEventFilter(this);
|
2020-02-23 11:58:47 +08:00
|
|
|
ui.display->setMouseTracking(true);
|
2019-09-06 01:42:30 +08:00
|
|
|
|
|
|
|
QMetaObject::invokeMethod(this, [this]
|
|
|
|
{
|
|
|
|
show();
|
2019-12-29 21:57:33 +08:00
|
|
|
AddSentence(EXTRA_WINDOW_INFO);
|
2019-09-06 01:42:30 +08:00
|
|
|
}, Qt::QueuedConnection);
|
|
|
|
}
|
|
|
|
|
|
|
|
~ExtraWindow()
|
|
|
|
{
|
|
|
|
settings.setValue(WINDOW, geometry());
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddSentence(QString sentence)
|
|
|
|
{
|
|
|
|
if (!showOriginal) sentence = sentence.section('\n', sentence.count('\n') / 2 + 1);
|
2020-01-19 14:25:57 +08:00
|
|
|
sanitize(sentence);
|
2020-02-23 11:58:47 +08:00
|
|
|
sentence.chop(std::distance(std::remove(sentence.begin(), sentence.end(), QChar::Tabulation), sentence.end()));
|
2019-09-06 01:42:30 +08:00
|
|
|
sentenceHistory.push_back(sentence);
|
|
|
|
historyIndex = sentenceHistory.size() - 1;
|
|
|
|
ui.display->setText(sentence);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-06-21 13:29:48 +08:00
|
|
|
void setTopmost(bool topmost)
|
|
|
|
{
|
|
|
|
SetWindowPos((HWND)winId(), topmost ? HWND_TOPMOST : HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
|
|
|
|
settings.setValue(TOPMOST, topmost);
|
|
|
|
};
|
|
|
|
|
2019-09-06 01:42:30 +08:00
|
|
|
void setLock(bool locked)
|
2019-06-21 13:29:48 +08:00
|
|
|
{
|
2019-09-06 01:42:30 +08:00
|
|
|
setSizeGripEnabled(!locked);
|
|
|
|
settings.setValue(SIZE_LOCK, this->locked = locked);
|
2019-06-21 13:29:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
void setShowOriginal(bool showOriginal)
|
|
|
|
{
|
2019-09-06 01:42:30 +08:00
|
|
|
if (!showOriginal && settings.value(SHOW_ORIGINAL, false).toBool()) QMessageBox::information(this, SHOW_ORIGINAL, SHOW_ORIGINAL_INFO);
|
|
|
|
settings.setValue(SHOW_ORIGINAL, this->showOriginal = showOriginal);
|
2019-06-21 13:29:48 +08:00
|
|
|
};
|
|
|
|
|
2019-09-06 01:42:30 +08:00
|
|
|
void setUseDictionary(bool useDictionary)
|
2018-12-31 11:10:56 +08:00
|
|
|
{
|
2019-09-06 01:42:30 +08:00
|
|
|
if (useDictionary)
|
|
|
|
{
|
|
|
|
dictionaryWindow.UpdateDictionary();
|
|
|
|
if (dictionaryWindow.dictionary.empty())
|
|
|
|
{
|
2019-09-10 10:08:24 +08:00
|
|
|
std::ofstream(DICTIONARY_SAVE_FILE) << u8"\ufeff" << DICTIONARY_INSTRUCTIONS;
|
2019-09-06 01:42:30 +08:00
|
|
|
_spawnlp(_P_DETACH, "notepad", "notepad", DICTIONARY_SAVE_FILE, NULL); // show file to user
|
|
|
|
}
|
|
|
|
}
|
|
|
|
settings.setValue(DICTIONARY, this->useDictionary = useDictionary);
|
|
|
|
}
|
|
|
|
|
2020-02-23 11:58:47 +08:00
|
|
|
void computeDictionaryPosition(QPoint mouse)
|
2019-09-06 01:42:30 +08:00
|
|
|
{
|
2020-02-23 15:44:26 +08:00
|
|
|
const QFont& font = ui.display->font();
|
2020-02-23 11:58:47 +08:00
|
|
|
if (cachedDisplayInfo.compareExchange(ui.display))
|
2019-09-06 01:42:30 +08:00
|
|
|
{
|
2020-02-23 11:58:47 +08:00
|
|
|
QString sentence = ui.display->text();
|
2020-02-23 15:44:26 +08:00
|
|
|
QFontMetrics fontMetrics(font, ui.display);
|
2020-02-23 11:58:47 +08:00
|
|
|
textPositionMap.clear();
|
|
|
|
for (int i = 0, height = 0, lineBreak = 0; i < sentence.size(); ++i)
|
|
|
|
{
|
|
|
|
int block = 1;
|
|
|
|
for (int charHeight = fontMetrics.boundingRect(0, 0, 1, INT_MAX, Qt::TextWordWrap, sentence.mid(i, 1)).height();
|
|
|
|
i + block < sentence.size() && fontMetrics.boundingRect(0, 0, 1, INT_MAX, Qt::TextWordWrap, sentence.mid(i, block + 1)).height() < charHeight * 1.5; ++block);
|
|
|
|
auto boundingRect = fontMetrics.boundingRect(0, 0, ui.display->width(), INT_MAX, Qt::TextWordWrap, sentence.left(i + block));
|
|
|
|
if (boundingRect.height() > height)
|
|
|
|
{
|
|
|
|
height = boundingRect.height();
|
|
|
|
lineBreak = i;
|
|
|
|
}
|
|
|
|
textPositionMap.push_back({
|
|
|
|
fontMetrics.boundingRect(0, 0, ui.display->width(), INT_MAX, Qt::TextWordWrap, sentence.mid(lineBreak, i - lineBreak + 1)).width(),
|
|
|
|
height
|
|
|
|
});
|
|
|
|
}
|
2019-09-06 01:42:30 +08:00
|
|
|
}
|
2020-02-23 11:58:47 +08:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < textPositionMap.size(); ++i) if (textPositionMap[i].y() > mouse.y() && textPositionMap[i].x() > mouse.x()) break;
|
2020-02-23 15:44:26 +08:00
|
|
|
if (i == textPositionMap.size() || (mouse - textPositionMap[i]).manhattanLength() > font.pointSize() * 2) return dictionaryWindow.hide();
|
2020-02-23 11:58:47 +08:00
|
|
|
if (ui.display->text().mid(i) == dictionaryWindow.term) return dictionaryWindow.ShowDefinition();
|
2020-02-23 15:44:26 +08:00
|
|
|
dictionaryWindow.ui.display->setFixedWidth(ui.display->width() * 3 / 4);
|
2020-02-23 11:58:47 +08:00
|
|
|
dictionaryWindow.setTerm(ui.display->text().mid(i));
|
2020-02-26 15:59:47 +08:00
|
|
|
int left = i == 0 ? 0 : textPositionMap[i - 1].x(), right = textPositionMap[i].x(),
|
|
|
|
x = textPositionMap[i].x() > ui.display->width() / 2 ? -dictionaryWindow.width() + (right * 3 + left) / 4 : (left * 3 + right) / 4;
|
2020-02-23 15:44:26 +08:00
|
|
|
dictionaryWindow.move(ui.display->mapToGlobal(QPoint(x, textPositionMap[i].y())));
|
2020-02-23 11:58:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool eventFilter(QObject*, QEvent* event) override
|
|
|
|
{
|
|
|
|
if (useDictionary && event->type() == QEvent::MouseMove) computeDictionaryPosition(((QMouseEvent*)event)->localPos().toPoint());
|
2019-09-06 01:42:30 +08:00
|
|
|
if (event->type() == QEvent::MouseButtonPress) dictionaryWindow.hide();
|
|
|
|
return false;
|
2018-12-22 04:11:12 +08:00
|
|
|
}
|
2018-12-31 11:10:56 +08:00
|
|
|
|
2019-02-25 13:00:20 +08:00
|
|
|
void mousePressEvent(QMouseEvent* event) override
|
2018-12-31 11:10:56 +08:00
|
|
|
{
|
2019-09-06 01:42:30 +08:00
|
|
|
dictionaryWindow.hide();
|
2019-01-02 11:40:29 +08:00
|
|
|
oldPos = event->globalPos();
|
2018-12-31 11:10:56 +08:00
|
|
|
}
|
|
|
|
|
2019-02-25 13:00:20 +08:00
|
|
|
void mouseMoveEvent(QMouseEvent* event) override
|
2018-12-31 11:10:56 +08:00
|
|
|
{
|
2019-09-06 01:42:30 +08:00
|
|
|
if (!locked) move(pos() + event->globalPos() - oldPos);
|
2019-01-02 11:40:29 +08:00
|
|
|
oldPos = event->globalPos();
|
2018-12-31 11:10:56 +08:00
|
|
|
}
|
|
|
|
|
2019-08-12 22:43:34 +08:00
|
|
|
void wheelEvent(QWheelEvent* event) override
|
|
|
|
{
|
2019-09-06 01:42:30 +08:00
|
|
|
int scroll = event->angleDelta().y();
|
|
|
|
if (scroll > 0 && historyIndex > 0) ui.display->setText(sentenceHistory[--historyIndex]);
|
|
|
|
if (scroll < 0 && historyIndex + 1 < sentenceHistory.size()) ui.display->setText(sentenceHistory[++historyIndex]);
|
2019-08-12 22:43:34 +08:00
|
|
|
}
|
|
|
|
|
2019-09-06 01:42:30 +08:00
|
|
|
bool locked, showOriginal, useDictionary;
|
2018-12-31 11:10:56 +08:00
|
|
|
QPoint oldPos;
|
2020-02-23 11:58:47 +08:00
|
|
|
|
|
|
|
class
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
bool compareExchange(QLabel* display)
|
|
|
|
{
|
|
|
|
if (display->text() == text && display->font() == font && display->width() == width) return false;
|
|
|
|
text = display->text();
|
|
|
|
font = display->font();
|
|
|
|
width = display->width();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
QString text;
|
|
|
|
QFont font;
|
|
|
|
int width;
|
|
|
|
} cachedDisplayInfo;
|
|
|
|
std::vector<QPoint> textPositionMap;
|
|
|
|
|
2019-08-12 22:43:34 +08:00
|
|
|
std::vector<QString> sentenceHistory;
|
2019-09-06 01:42:30 +08:00
|
|
|
int historyIndex = 0;
|
2018-12-22 04:11:12 +08:00
|
|
|
|
2019-09-06 01:42:30 +08:00
|
|
|
class DictionaryWindow : public PrettyWindow
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DictionaryWindow() :
|
|
|
|
PrettyWindow("Dictionary Window")
|
|
|
|
{
|
|
|
|
ui.display->setSizePolicy({ QSizePolicy::Fixed, QSizePolicy::Minimum });
|
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateDictionary()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (dictionaryFileLastWrite == std::filesystem::last_write_time(DICTIONARY_SAVE_FILE)) return;
|
|
|
|
dictionaryFileLastWrite = std::filesystem::last_write_time(DICTIONARY_SAVE_FILE);
|
|
|
|
}
|
|
|
|
catch (std::filesystem::filesystem_error) { return; }
|
|
|
|
|
|
|
|
dictionary.clear();
|
2020-02-17 08:58:09 +08:00
|
|
|
charStorage.clear();
|
2019-09-06 01:42:30 +08:00
|
|
|
|
2020-02-17 08:58:09 +08:00
|
|
|
auto StoreCopy = [&](std::string_view string)
|
2019-09-06 01:42:30 +08:00
|
|
|
{
|
2020-02-17 08:58:09 +08:00
|
|
|
auto location = &*charStorage.insert(charStorage.end(), string.begin(), string.end());
|
|
|
|
charStorage.push_back(0);
|
|
|
|
return location;
|
2019-09-06 01:42:30 +08:00
|
|
|
};
|
|
|
|
|
2020-02-17 08:58:09 +08:00
|
|
|
charStorage.reserve(std::filesystem::file_size(DICTIONARY_SAVE_FILE));
|
|
|
|
std::ifstream stream(DICTIONARY_SAVE_FILE);
|
|
|
|
BlockMarkupIterator savedDictionary(stream, Array<std::string_view>{ "|TERM|", "|DEFINITION|" });
|
|
|
|
while (auto read = savedDictionary.Next())
|
2019-09-06 01:42:30 +08:00
|
|
|
{
|
2020-02-26 15:59:47 +08:00
|
|
|
const auto& [terms, definition] = read.value();
|
2020-02-17 08:58:09 +08:00
|
|
|
auto storedDefinition = StoreCopy(definition);
|
|
|
|
std::string_view termsView = terms;
|
|
|
|
size_t start = 0, end = termsView.find("|TERM|");
|
|
|
|
while (end != std::string::npos)
|
|
|
|
{
|
|
|
|
dictionary.push_back(DictionaryEntry{ StoreCopy(termsView.substr(start, end - start)), storedDefinition });
|
|
|
|
start = end + 6;
|
|
|
|
end = termsView.find("|TERM|", start);
|
|
|
|
}
|
|
|
|
dictionary.push_back(DictionaryEntry{ StoreCopy(termsView.substr(start)), storedDefinition });
|
2019-09-06 01:42:30 +08:00
|
|
|
}
|
2020-02-26 15:59:47 +08:00
|
|
|
std::stable_sort(dictionary.begin(), dictionary.end());
|
|
|
|
|
|
|
|
inflections.clear();
|
|
|
|
stream.seekg(0);
|
|
|
|
BlockMarkupIterator savedInflections(stream, Array<std::string_view>{ "|ROOT|", "|INFLECTS TO|", "|NAME|" });
|
|
|
|
while (auto read = savedInflections.Next())
|
|
|
|
{
|
|
|
|
const auto& [root, inflectsTo, name] = read.value();
|
|
|
|
if (!inflections.emplace_back(Inflection{
|
|
|
|
S(root),
|
|
|
|
QRegularExpression(QRegularExpression::anchoredPattern(S(inflectsTo)), QRegularExpression::UseUnicodePropertiesOption),
|
|
|
|
S(name)
|
|
|
|
}).inflectsTo.isValid()) TEXTRACTOR_MESSAGE(L"Invalid regex: %s", StringToWideString(inflectsTo));
|
|
|
|
}
|
2019-09-06 01:42:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void setTerm(QString term)
|
|
|
|
{
|
2020-02-23 11:58:47 +08:00
|
|
|
this->term = term;
|
2019-09-06 01:42:30 +08:00
|
|
|
UpdateDictionary();
|
|
|
|
definitions.clear();
|
|
|
|
definitionIndex = 0;
|
2020-02-26 15:59:47 +08:00
|
|
|
std::unordered_set<const char*> foundDefinitions;
|
|
|
|
for (term = term.left(500); !term.isEmpty(); term.chop(1))
|
|
|
|
for (const auto& [rootTerm, definition, inflections] : LookupDefinitions(term, foundDefinitions))
|
|
|
|
definitions.push_back(
|
2020-03-01 08:51:09 +08:00
|
|
|
QStringLiteral("<h3>%1 (%5/%6)</h3><small>%2%3</small><p>%4</p>").arg(
|
2020-02-26 15:59:47 +08:00
|
|
|
term,
|
|
|
|
rootTerm.split("<<")[0],
|
|
|
|
inflections.join(""),
|
|
|
|
definition
|
|
|
|
)
|
|
|
|
);
|
2019-09-06 01:42:30 +08:00
|
|
|
for (int i = 0; i < definitions.size(); ++i) definitions[i] = definitions[i].arg(i + 1).arg(definitions.size());
|
|
|
|
ShowDefinition();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShowDefinition()
|
|
|
|
{
|
2020-02-23 11:58:47 +08:00
|
|
|
if (definitions.empty()) return hide();
|
2019-09-06 01:42:30 +08:00
|
|
|
ui.display->setText(definitions[definitionIndex]);
|
|
|
|
adjustSize();
|
|
|
|
resize(width(), 1);
|
|
|
|
show();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct DictionaryEntry
|
|
|
|
{
|
|
|
|
const char* term;
|
|
|
|
const char* definition;
|
|
|
|
bool operator<(DictionaryEntry other) const { return strcmp(term, other.term) < 0; }
|
|
|
|
};
|
|
|
|
std::vector<DictionaryEntry> dictionary;
|
2020-02-23 11:58:47 +08:00
|
|
|
QString term;
|
2019-09-06 01:42:30 +08:00
|
|
|
|
|
|
|
private:
|
2020-02-26 15:59:47 +08:00
|
|
|
struct LookupResult
|
|
|
|
{
|
|
|
|
QString term;
|
|
|
|
QString definition;
|
|
|
|
QStringList inflectionsUsed;
|
|
|
|
};
|
|
|
|
std::vector<LookupResult> LookupDefinitions(QString term, std::unordered_set<const char*>& foundDefinitions, QStringList inflectionsUsed = {})
|
|
|
|
{
|
|
|
|
std::vector<LookupResult> results;
|
|
|
|
for (auto [it, end] = std::equal_range(dictionary.begin(), dictionary.end(), DictionaryEntry{ term.toUtf8() }); it != end; ++it)
|
|
|
|
if (foundDefinitions.emplace(it->definition).second)
|
|
|
|
results.push_back({ term, it->definition, inflectionsUsed });
|
|
|
|
for (const auto& inflection : inflections) if (auto match = inflection.inflectsTo.match(term); match.hasMatch())
|
|
|
|
{
|
|
|
|
QStringList currentInflectionsUsed = inflectionsUsed;
|
|
|
|
currentInflectionsUsed.push_front(inflection.name);
|
|
|
|
QString root = inflection.root;
|
|
|
|
for (int i = 0; i < root.size(); ++i) if (root[i].isDigit()) root.replace(i, 1, match.captured(root[i].unicode() - '0'));
|
|
|
|
for (const auto& definition : LookupDefinitions(root, foundDefinitions, currentInflectionsUsed)) results.push_back(definition);
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2019-09-06 01:42:30 +08:00
|
|
|
void wheelEvent(QWheelEvent* event) override
|
|
|
|
{
|
|
|
|
int scroll = event->angleDelta().y();
|
|
|
|
if (scroll > 0 && definitionIndex > 0) definitionIndex -= 1;
|
|
|
|
if (scroll < 0 && definitionIndex + 1 < definitions.size()) definitionIndex += 1;
|
|
|
|
ShowDefinition();
|
|
|
|
}
|
2019-06-16 05:30:41 +08:00
|
|
|
|
2020-02-26 15:59:47 +08:00
|
|
|
struct Inflection
|
|
|
|
{
|
|
|
|
QString root;
|
|
|
|
QRegularExpression inflectsTo;
|
|
|
|
QString name;
|
|
|
|
};
|
|
|
|
std::vector<Inflection> inflections;
|
|
|
|
|
2019-09-06 01:42:30 +08:00
|
|
|
std::filesystem::file_time_type dictionaryFileLastWrite;
|
2020-02-17 08:58:09 +08:00
|
|
|
std::vector<char> charStorage;
|
2019-09-06 01:42:30 +08:00
|
|
|
std::vector<QString> definitions;
|
|
|
|
int definitionIndex;
|
|
|
|
} dictionaryWindow;
|
|
|
|
} extraWindow;
|
2019-06-16 05:30:41 +08:00
|
|
|
|
2019-09-06 01:42:30 +08:00
|
|
|
bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
|
|
|
|
{
|
2019-12-29 21:57:33 +08:00
|
|
|
if (sentenceInfo["current select"] && sentenceInfo["text number"] != 0)
|
|
|
|
QMetaObject::invokeMethod(&extraWindow, [sentence = S(sentence)] { extraWindow.AddSentence(sentence); });
|
2018-12-22 04:11:12 +08:00
|
|
|
return false;
|
|
|
|
}
|