2019-06-29 08:46:31 +05:30
|
|
|
#include "qtcommon.h"
|
2018-12-21 15:11:12 -05:00
|
|
|
#include "extension.h"
|
2019-06-21 01:29:48 -04:00
|
|
|
#include "ui_extrawindow.h"
|
2020-02-16 17:58:09 -07:00
|
|
|
#include "blockmarkup.h"
|
2019-09-05 13:42:30 -04:00
|
|
|
#include <fstream>
|
|
|
|
#include <process.h>
|
2020-02-26 00:59:47 -07:00
|
|
|
#include <QRegularExpression>
|
2018-12-30 22:10:56 -05:00
|
|
|
#include <QColorDialog>
|
2019-07-13 19:15:43 +05:30
|
|
|
#include <QFontDialog>
|
2018-12-30 22:10:56 -05:00
|
|
|
#include <QMenu>
|
|
|
|
#include <QPainter>
|
2019-11-10 12:48:32 -05:00
|
|
|
#include <QGraphicsEffect>
|
2020-02-22 20:58:47 -07:00
|
|
|
#include <QFontMetrics>
|
2018-12-30 22:10:56 -05:00
|
|
|
#include <QMouseEvent>
|
2019-08-12 10:43:34 -04:00
|
|
|
#include <QWheelEvent>
|
2021-11-09 19:29:13 +01:00
|
|
|
#include <QScrollArea>
|
2021-11-06 16:52:26 -06:00
|
|
|
#include <QAbstractNativeEventFilter>
|
2018-12-21 15:11:12 -05:00
|
|
|
|
2019-02-27 11:33:17 -05:00
|
|
|
extern const char* EXTRA_WINDOW_INFO;
|
|
|
|
extern const char* TOPMOST;
|
2019-12-29 08:54:55 -05:00
|
|
|
extern const char* OPACITY;
|
2019-06-16 00:30:41 +03:00
|
|
|
extern const char* SHOW_ORIGINAL;
|
2021-11-28 04:41:42 -07:00
|
|
|
extern const char* ORIGINAL_AFTER_TRANSLATION;
|
2019-02-27 11:33:17 -05:00
|
|
|
extern const char* SIZE_LOCK;
|
2021-11-09 19:29:13 +01:00
|
|
|
extern const char* POSITION_LOCK;
|
|
|
|
extern const char* CENTERED_TEXT;
|
2021-05-14 17:46:22 +02:00
|
|
|
extern const char* AUTO_RESIZE_WINDOW_HEIGHT;
|
|
|
|
extern const char* CLICK_THROUGH;
|
2021-11-28 04:41:42 -07:00
|
|
|
extern const char* HIDE_MOUSEOVER;
|
2019-09-05 13:42:30 -04:00
|
|
|
extern const char* DICTIONARY;
|
|
|
|
extern const char* DICTIONARY_INSTRUCTIONS;
|
2019-02-27 11:33:17 -05:00
|
|
|
extern const char* BG_COLOR;
|
|
|
|
extern const char* TEXT_COLOR;
|
2019-11-10 12:48:32 -05: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 00:42:42 -04:00
|
|
|
extern const char* FONT;
|
2019-02-27 11:33:17 -05:00
|
|
|
|
2019-09-05 13:42:30 -04:00
|
|
|
constexpr auto DICTIONARY_SAVE_FILE = u8"SavedDictionary.txt";
|
2021-11-06 16:52:26 -06:00
|
|
|
constexpr int CLICK_THROUGH_HOTKEY = 0xc0d0;
|
2019-09-05 13:42:30 -04:00
|
|
|
|
2019-12-29 08:54:55 -05: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;
|
|
|
|
}
|
|
|
|
|
2021-01-21 07:07:09 -07:00
|
|
|
struct PrettyWindow : QDialog, Localizer
|
2018-12-21 15:11:12 -05:00
|
|
|
{
|
2019-09-05 13:42:30 -04:00
|
|
|
PrettyWindow(const char* name)
|
2018-12-21 15:11:12 -05:00
|
|
|
{
|
2019-06-21 01:29:48 -04:00
|
|
|
ui.setupUi(this);
|
2021-03-08 08:41:34 -07:00
|
|
|
ui.display->setGraphicsEffect(outliner = new Outliner);
|
2018-12-30 22:10:56 -05:00
|
|
|
setWindowFlags(Qt::FramelessWindowHint);
|
|
|
|
setAttribute(Qt::WA_TranslucentBackground);
|
2019-06-21 01:29:48 -04:00
|
|
|
|
2019-09-05 13:42:30 -04:00
|
|
|
settings.beginGroup(name);
|
|
|
|
QFont font = ui.display->font();
|
|
|
|
if (font.fromString(settings.value(FONT, font.toString()).toString())) ui.display->setFont(font);
|
2021-01-18 18:58:21 -07:00
|
|
|
SetBackgroundColor(settings.value(BG_COLOR, backgroundColor).value<QColor>());
|
|
|
|
SetTextColor(settings.value(TEXT_COLOR, TextColor()).value<QColor>());
|
2021-03-08 08:41:34 -07:00
|
|
|
outliner->color = settings.value(OUTLINE_COLOR, outliner->color).value<QColor>();
|
|
|
|
outliner->size = settings.value(OUTLINE_SIZE, outliner->size).toDouble();
|
2021-11-28 04:41:42 -07:00
|
|
|
autoHide = settings.value(HIDE_MOUSEOVER, autoHide).toBool();
|
2019-09-05 13:42:30 -04:00
|
|
|
menu.addAction(FONT, this, &PrettyWindow::RequestFont);
|
2021-01-18 18:58:21 -07:00
|
|
|
menu.addAction(BG_COLOR, [this] { SetBackgroundColor(colorPrompt(this, backgroundColor, BG_COLOR)); });
|
|
|
|
menu.addAction(TEXT_COLOR, [this] { SetTextColor(colorPrompt(this, TextColor(), TEXT_COLOR)); });
|
|
|
|
QAction* outlineAction = menu.addAction(TEXT_OUTLINE, this, &PrettyWindow::SetOutline);
|
2019-11-10 12:48:32 -05:00
|
|
|
outlineAction->setCheckable(true);
|
2021-03-08 08:41:34 -07:00
|
|
|
outlineAction->setChecked(outliner->size >= 0);
|
2021-11-28 04:41:42 -07:00
|
|
|
QAction* autoHideAction = menu.addAction(HIDE_MOUSEOVER, this, [this](bool autoHide) { settings.setValue(HIDE_MOUSEOVER, this->autoHide = autoHide); });
|
|
|
|
autoHideAction->setCheckable(true);
|
|
|
|
autoHideAction->setChecked(autoHide);
|
|
|
|
connect(this, &QDialog::customContextMenuRequested, [this](QPoint point) { menu.exec(mapToGlobal(point)); });
|
|
|
|
connect(ui.display, &QLabel::customContextMenuRequested, [this](QPoint point) { menu.exec(ui.display->mapToGlobal(point)); });
|
|
|
|
startTimer(50);
|
2018-12-30 22:10:56 -05:00
|
|
|
}
|
|
|
|
|
2019-09-05 13:42:30 -04:00
|
|
|
~PrettyWindow()
|
2019-06-21 01:29:48 -04:00
|
|
|
{
|
|
|
|
settings.sync();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ui::ExtraWindow ui;
|
2019-09-05 13:42:30 -04:00
|
|
|
|
2021-11-28 04:41:42 -07:00
|
|
|
protected:
|
|
|
|
void timerEvent(QTimerEvent*) override
|
2021-11-09 19:29:13 +01:00
|
|
|
{
|
2021-11-28 07:58:16 -07:00
|
|
|
if (autoHide && geometry().contains(QCursor::pos()))
|
2021-11-09 19:29:13 +01:00
|
|
|
{
|
2021-11-28 07:58:16 -07:00
|
|
|
if (!hidden)
|
|
|
|
{
|
|
|
|
if (backgroundColor.alphaF() > 0.05) backgroundColor.setAlphaF(0.05);
|
|
|
|
if (outliner->color.alphaF() > 0.05) outliner->color.setAlphaF(0.05);
|
|
|
|
QColor hiddenTextColor = TextColor();
|
|
|
|
if (hiddenTextColor.alphaF() > 0.05) hiddenTextColor.setAlphaF(0.05);
|
|
|
|
ui.display->setPalette(QPalette(hiddenTextColor, {}, {}, {}, {}, {}, {}));
|
|
|
|
hidden = true;
|
|
|
|
repaint();
|
|
|
|
}
|
2021-11-09 19:29:13 +01:00
|
|
|
}
|
2021-11-28 04:41:42 -07:00
|
|
|
else if (hidden)
|
2021-11-09 19:29:13 +01:00
|
|
|
{
|
2021-11-28 04:41:42 -07:00
|
|
|
backgroundColor.setAlpha(settings.value(BG_COLOR).value<QColor>().alpha());
|
|
|
|
outliner->color.setAlpha(settings.value(OUTLINE_COLOR).value<QColor>().alpha());
|
|
|
|
ui.display->setPalette(QPalette(settings.value(TEXT_COLOR).value<QColor>(), {}, {}, {}, {}, {}, {}));
|
|
|
|
hidden = false;
|
2021-11-28 07:58:16 -07:00
|
|
|
repaint();
|
2021-11-09 19:29:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-05 13:42:30 -04:00
|
|
|
QMenu menu{ ui.display };
|
2020-11-02 06:27:21 -07:00
|
|
|
Settings settings{ this };
|
2019-01-01 22:40:29 -05:00
|
|
|
|
|
|
|
private:
|
2019-06-21 01:29:48 -04:00
|
|
|
void RequestFont()
|
|
|
|
{
|
2022-01-01 21:27:42 -07:00
|
|
|
if (QFont font = QFontDialog::getFont(&ok, ui.display->font(), this, FONT); ok)
|
2019-07-13 19:15:43 +05:30
|
|
|
{
|
|
|
|
settings.setValue(FONT, font.toString());
|
|
|
|
ui.display->setFont(font);
|
|
|
|
}
|
2019-06-21 01:29:48 -04:00
|
|
|
};
|
|
|
|
|
2021-01-18 18:58:21 -07:00
|
|
|
void SetBackgroundColor(QColor color)
|
2019-06-21 01:29:48 -04:00
|
|
|
{
|
|
|
|
if (!color.isValid()) return;
|
|
|
|
if (color.alpha() == 0) color.setAlpha(1);
|
2021-01-18 18:58:21 -07:00
|
|
|
backgroundColor = color;
|
2019-06-21 01:29:48 -04:00
|
|
|
repaint();
|
2019-11-10 12:48:32 -05:00
|
|
|
settings.setValue(BG_COLOR, color.name(QColor::HexArgb));
|
2019-06-21 01:29:48 -04:00
|
|
|
};
|
|
|
|
|
2021-01-18 18:58:21 -07:00
|
|
|
QColor TextColor()
|
2019-09-05 13:42:30 -04:00
|
|
|
{
|
|
|
|
return ui.display->palette().color(QPalette::WindowText);
|
|
|
|
}
|
|
|
|
|
2021-01-18 18:58:21 -07:00
|
|
|
void SetTextColor(QColor color)
|
2019-06-21 01:29:48 -04:00
|
|
|
{
|
|
|
|
if (!color.isValid()) return;
|
2019-09-05 13:42:30 -04:00
|
|
|
ui.display->setPalette(QPalette(color, {}, {}, {}, {}, {}, {}));
|
2019-11-10 12:48:32 -05:00
|
|
|
settings.setValue(TEXT_COLOR, color.name(QColor::HexArgb));
|
2019-06-21 01:29:48 -04:00
|
|
|
};
|
|
|
|
|
2021-01-18 18:58:21 -07:00
|
|
|
void SetOutline(bool enable)
|
2019-11-10 12:48:32 -05:00
|
|
|
{
|
|
|
|
if (enable)
|
|
|
|
{
|
2021-03-08 08:41:34 -07:00
|
|
|
QColor color = colorPrompt(this, outliner->color, OUTLINE_COLOR);
|
|
|
|
if (color.isValid()) outliner->color = color;
|
2021-11-28 04:41:42 -07:00
|
|
|
outliner->size = QInputDialog::getDouble(this, OUTLINE_SIZE, OUTLINE_SIZE_INFO, -outliner->size, 0, INT_MAX, 2, nullptr, Qt::WindowCloseButtonHint);
|
2019-11-10 12:48:32 -05:00
|
|
|
}
|
2021-11-28 04:41:42 -07:00
|
|
|
else outliner->size = -outliner->size;
|
2021-03-08 08:41:34 -07:00
|
|
|
settings.setValue(OUTLINE_COLOR, outliner->color.name(QColor::HexArgb));
|
|
|
|
settings.setValue(OUTLINE_SIZE, outliner->size);
|
2019-11-10 12:48:32 -05:00
|
|
|
}
|
|
|
|
|
2019-09-05 13:42:30 -04:00
|
|
|
void paintEvent(QPaintEvent*) override
|
|
|
|
{
|
2021-01-18 18:58:21 -07:00
|
|
|
QPainter(this).fillRect(rect(), backgroundColor);
|
2019-09-05 13:42:30 -04:00
|
|
|
}
|
|
|
|
|
2021-11-28 04:41:42 -07:00
|
|
|
bool autoHide = false, hidden = false;
|
2021-01-18 18:58:21 -07:00
|
|
|
QColor backgroundColor{ palette().window().color() };
|
2021-03-08 08:41:34 -07:00
|
|
|
struct Outliner : QGraphicsEffect
|
2019-11-10 12:48:32 -05:00
|
|
|
{
|
|
|
|
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 };
|
2021-11-28 04:41:42 -07:00
|
|
|
double size = -0.5;
|
2021-03-08 08:41:34 -07:00
|
|
|
}* outliner;
|
2019-09-05 13:42:30 -04:00
|
|
|
};
|
|
|
|
|
2021-11-06 16:52:26 -06:00
|
|
|
class ExtraWindow : public PrettyWindow, QAbstractNativeEventFilter
|
2019-09-05 13:42:30 -04:00
|
|
|
{
|
|
|
|
public:
|
2021-01-30 12:07:37 -07:00
|
|
|
ExtraWindow() : PrettyWindow("Extra Window")
|
2019-09-05 13:42:30 -04:00
|
|
|
{
|
2019-09-26 07:07:58 -04:00
|
|
|
ui.display->setTextFormat(Qt::PlainText);
|
2021-01-21 07:07:09 -07:00
|
|
|
if (settings.contains(WINDOW) && QApplication::screenAt(settings.value(WINDOW).toRect().bottomRight())) setGeometry(settings.value(WINDOW).toRect());
|
2019-09-05 13:42:30 -04:00
|
|
|
|
2020-01-18 23:25:57 -07:00
|
|
|
for (auto [name, default, slot] : Array<const char*, bool, void(ExtraWindow::*)(bool)>{
|
2021-01-18 18:58:21 -07:00
|
|
|
{ TOPMOST, false, &ExtraWindow::SetTopmost },
|
2021-11-28 04:41:42 -07:00
|
|
|
{ SIZE_LOCK, false, &ExtraWindow::SetSizeLock },
|
2021-11-09 19:29:13 +01:00
|
|
|
{ POSITION_LOCK, false, &ExtraWindow::SetPositionLock },
|
|
|
|
{ CENTERED_TEXT, false, &ExtraWindow::SetCenteredText },
|
2021-11-28 04:41:42 -07:00
|
|
|
{ AUTO_RESIZE_WINDOW_HEIGHT, false, &ExtraWindow::SetAutoResize },
|
2021-01-18 18:58:21 -07:00
|
|
|
{ SHOW_ORIGINAL, true, &ExtraWindow::SetShowOriginal },
|
2021-11-28 04:41:42 -07:00
|
|
|
{ ORIGINAL_AFTER_TRANSLATION, true, &ExtraWindow::SetShowOriginalAfterTranslation },
|
2021-01-18 18:58:21 -07:00
|
|
|
{ DICTIONARY, false, &ExtraWindow::SetUseDictionary },
|
2019-09-05 13:42:30 -04:00
|
|
|
})
|
|
|
|
{
|
|
|
|
// 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);
|
|
|
|
}
|
2021-11-06 16:52:26 -06:00
|
|
|
|
|
|
|
menu.addAction(CLICK_THROUGH, this, &ExtraWindow::ToggleClickThrough);
|
|
|
|
|
2019-09-05 13:42:30 -04:00
|
|
|
ui.display->installEventFilter(this);
|
2021-11-06 16:52:26 -06:00
|
|
|
qApp->installNativeEventFilter(this);
|
2019-09-05 13:42:30 -04:00
|
|
|
|
|
|
|
QMetaObject::invokeMethod(this, [this]
|
|
|
|
{
|
2022-01-30 01:00:00 -07:00
|
|
|
RegisterHotKey((HWND)winId(), CLICK_THROUGH_HOTKEY, MOD_ALT | MOD_NOREPEAT, 0x58);
|
2019-09-05 13:42:30 -04:00
|
|
|
show();
|
2019-12-29 08:57:33 -05:00
|
|
|
AddSentence(EXTRA_WINDOW_INFO);
|
2019-09-05 13:42:30 -04:00
|
|
|
}, Qt::QueuedConnection);
|
|
|
|
}
|
|
|
|
|
|
|
|
~ExtraWindow()
|
|
|
|
{
|
|
|
|
settings.setValue(WINDOW, geometry());
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddSentence(QString sentence)
|
|
|
|
{
|
2020-01-18 23:25:57 -07:00
|
|
|
sanitize(sentence);
|
2020-02-22 20:58:47 -07:00
|
|
|
sentence.chop(std::distance(std::remove(sentence.begin(), sentence.end(), QChar::Tabulation), sentence.end()));
|
2019-09-05 13:42:30 -04:00
|
|
|
sentenceHistory.push_back(sentence);
|
2021-11-28 04:41:42 -07:00
|
|
|
if (sentenceHistory.size() > 1000) sentenceHistory.erase(sentenceHistory.begin());
|
2019-09-05 13:42:30 -04:00
|
|
|
historyIndex = sentenceHistory.size() - 1;
|
2021-11-28 04:41:42 -07:00
|
|
|
DisplaySentence();
|
2019-09-05 13:42:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-11-28 04:41:42 -07:00
|
|
|
void DisplaySentence()
|
|
|
|
{
|
2021-11-28 07:58:16 -07:00
|
|
|
if (sentenceHistory.empty()) return;
|
2021-11-28 04:41:42 -07:00
|
|
|
QString sentence = sentenceHistory[historyIndex];
|
|
|
|
if (sentence.contains(u8"\x200b \n"))
|
|
|
|
if (!showOriginal) sentence = sentence.split(u8"\x200b \n")[1];
|
|
|
|
else if (showOriginalAfterTranslation) sentence = sentence.split(u8"\x200b \n")[1] + "\n" + sentence.split(u8"\x200b \n")[0];
|
|
|
|
|
2022-01-01 21:27:42 -07:00
|
|
|
if (sizeLock && !autoResize)
|
2021-11-28 04:41:42 -07:00
|
|
|
{
|
|
|
|
QFontMetrics fontMetrics(ui.display->font(), ui.display);
|
|
|
|
int low = 0, high = sentence.size(), last = 0;
|
2022-01-01 21:27:42 -07:00
|
|
|
while (low <= high)
|
2021-11-28 04:41:42 -07:00
|
|
|
{
|
|
|
|
int mid = (low + high) / 2;
|
2022-01-01 21:27:42 -07:00
|
|
|
if (fontMetrics.boundingRect(0, 0, ui.display->width(), INT_MAX, Qt::TextWordWrap, sentence.left(mid)).height() <= ui.display->height())
|
2021-11-28 04:41:42 -07:00
|
|
|
{
|
|
|
|
last = mid;
|
|
|
|
low = mid + 1;
|
|
|
|
}
|
2022-01-01 21:27:42 -07:00
|
|
|
else high = mid - 1;
|
2021-11-28 04:41:42 -07:00
|
|
|
}
|
|
|
|
sentence = sentence.left(last);
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.display->setText(sentence);
|
2022-01-01 21:27:42 -07:00
|
|
|
if (autoResize)
|
|
|
|
resize(width(), height() - ui.display->height() +
|
|
|
|
QFontMetrics(ui.display->font(), ui.display).boundingRect(0, 0, ui.display->width(), INT_MAX, Qt::TextWordWrap, sentence).height()
|
|
|
|
);
|
2021-11-28 04:41:42 -07:00
|
|
|
}
|
|
|
|
|
2021-01-18 18:58:21 -07:00
|
|
|
void SetTopmost(bool topmost)
|
2019-06-21 01:29:48 -04:00
|
|
|
{
|
2020-03-02 23:48:29 -07:00
|
|
|
for (auto window : { winId(), dictionaryWindow.winId() })
|
|
|
|
SetWindowPos((HWND)window, topmost ? HWND_TOPMOST : HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
|
2019-06-21 01:29:48 -04:00
|
|
|
settings.setValue(TOPMOST, topmost);
|
|
|
|
};
|
|
|
|
|
2021-11-09 19:29:13 +01:00
|
|
|
void SetPositionLock(bool locked)
|
|
|
|
{
|
2021-11-28 07:58:16 -07:00
|
|
|
settings.setValue(POSITION_LOCK, posLock = locked);
|
2021-11-09 19:29:13 +01:00
|
|
|
};
|
|
|
|
|
2021-11-28 04:41:42 -07:00
|
|
|
void SetSizeLock(bool locked)
|
2019-06-21 01:29:48 -04:00
|
|
|
{
|
2019-09-05 13:42:30 -04:00
|
|
|
setSizeGripEnabled(!locked);
|
2021-11-28 07:58:16 -07:00
|
|
|
settings.setValue(SIZE_LOCK, sizeLock = locked);
|
2021-11-09 19:29:13 +01:00
|
|
|
};
|
|
|
|
|
2021-11-28 04:41:42 -07:00
|
|
|
void SetCenteredText(bool centeredText)
|
2021-11-09 19:29:13 +01:00
|
|
|
{
|
2021-11-28 04:41:42 -07:00
|
|
|
ui.display->setAlignment(centeredText ? Qt::AlignHCenter : Qt::AlignLeft);
|
|
|
|
settings.setValue(CENTERED_TEXT, this->centeredText = centeredText);
|
2021-11-09 19:29:13 +01:00
|
|
|
};
|
|
|
|
|
2021-11-28 04:41:42 -07:00
|
|
|
void SetAutoResize(bool autoResize)
|
2021-11-09 19:29:13 +01:00
|
|
|
{
|
2021-11-28 04:41:42 -07:00
|
|
|
settings.setValue(AUTO_RESIZE_WINDOW_HEIGHT, this->autoResize = autoResize);
|
|
|
|
DisplaySentence();
|
2021-11-09 19:29:13 +01:00
|
|
|
};
|
|
|
|
|
2021-01-18 18:58:21 -07:00
|
|
|
void SetShowOriginal(bool showOriginal)
|
2019-06-21 01:29:48 -04:00
|
|
|
{
|
2019-09-05 13:42:30 -04:00
|
|
|
settings.setValue(SHOW_ORIGINAL, this->showOriginal = showOriginal);
|
2021-11-28 04:41:42 -07:00
|
|
|
DisplaySentence();
|
2019-06-21 01:29:48 -04:00
|
|
|
};
|
|
|
|
|
2021-11-09 19:29:13 +01:00
|
|
|
void SetShowOriginalAfterTranslation(bool showOriginalAfterTranslation)
|
|
|
|
{
|
2021-11-28 04:41:42 -07:00
|
|
|
settings.setValue(ORIGINAL_AFTER_TRANSLATION, this->showOriginalAfterTranslation = showOriginalAfterTranslation);
|
|
|
|
DisplaySentence();
|
2021-11-09 19:29:13 +01:00
|
|
|
};
|
|
|
|
|
2021-01-18 18:58:21 -07:00
|
|
|
void SetUseDictionary(bool useDictionary)
|
2018-12-30 22:10:56 -05:00
|
|
|
{
|
2019-09-05 13:42:30 -04:00
|
|
|
if (useDictionary)
|
|
|
|
{
|
|
|
|
dictionaryWindow.UpdateDictionary();
|
|
|
|
if (dictionaryWindow.dictionary.empty())
|
|
|
|
{
|
2019-09-09 22:08:24 -04:00
|
|
|
std::ofstream(DICTIONARY_SAVE_FILE) << u8"\ufeff" << DICTIONARY_INSTRUCTIONS;
|
2019-09-05 13:42:30 -04:00
|
|
|
_spawnlp(_P_DETACH, "notepad", "notepad", DICTIONARY_SAVE_FILE, NULL); // show file to user
|
|
|
|
}
|
|
|
|
}
|
|
|
|
settings.setValue(DICTIONARY, this->useDictionary = useDictionary);
|
|
|
|
}
|
|
|
|
|
2021-11-28 04:41:42 -07:00
|
|
|
void ToggleClickThrough()
|
|
|
|
{
|
|
|
|
clickThrough = !clickThrough;
|
|
|
|
for (auto window : { winId(), dictionaryWindow.winId() })
|
|
|
|
{
|
|
|
|
unsigned exStyle = GetWindowLongPtrW((HWND)window, GWL_EXSTYLE);
|
|
|
|
if (clickThrough) exStyle |= WS_EX_TRANSPARENT;
|
|
|
|
else exStyle &= ~WS_EX_TRANSPARENT;
|
|
|
|
SetWindowLongPtrW((HWND)window, GWL_EXSTYLE, exStyle);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void ShowDictionary(QPoint mouse)
|
2019-09-05 13:42:30 -04:00
|
|
|
{
|
2020-03-02 23:48:29 -07:00
|
|
|
QString sentence = ui.display->text();
|
2020-02-23 00:44:26 -07:00
|
|
|
const QFont& font = ui.display->font();
|
2021-01-18 18:58:21 -07:00
|
|
|
if (cachedDisplayInfo.CompareExchange(ui.display))
|
2019-09-05 13:42:30 -04:00
|
|
|
{
|
2020-02-23 00:44:26 -07:00
|
|
|
QFontMetrics fontMetrics(font, ui.display);
|
2021-11-28 04:41:42 -07:00
|
|
|
int flags = Qt::TextWordWrap | (ui.display->alignment() & (Qt::AlignLeft | Qt::AlignHCenter));
|
2020-02-22 20:58:47 -07:00
|
|
|
textPositionMap.clear();
|
|
|
|
for (int i = 0, height = 0, lineBreak = 0; i < sentence.size(); ++i)
|
|
|
|
{
|
|
|
|
int block = 1;
|
2021-11-28 04:41:42 -07:00
|
|
|
for (int charHeight = fontMetrics.boundingRect(0, 0, 1, INT_MAX, flags, sentence.mid(i, 1)).height();
|
|
|
|
i + block < sentence.size() && fontMetrics.boundingRect(0, 0, 1, INT_MAX, flags, sentence.mid(i, block + 1)).height() < charHeight * 1.5; ++block);
|
|
|
|
auto boundingRect = fontMetrics.boundingRect(0, 0, ui.display->width(), INT_MAX, flags, sentence.left(i + block));
|
2020-02-22 20:58:47 -07:00
|
|
|
if (boundingRect.height() > height)
|
|
|
|
{
|
|
|
|
height = boundingRect.height();
|
|
|
|
lineBreak = i;
|
|
|
|
}
|
|
|
|
textPositionMap.push_back({
|
2021-11-28 04:41:42 -07:00
|
|
|
fontMetrics.boundingRect(0, 0, ui.display->width(), INT_MAX, flags, sentence.mid(lineBreak, i - lineBreak + 1)).right() + 1,
|
2020-02-22 20:58:47 -07:00
|
|
|
height
|
|
|
|
});
|
|
|
|
}
|
2019-09-05 13:42:30 -04:00
|
|
|
}
|
2020-02-22 20:58:47 -07:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < textPositionMap.size(); ++i) if (textPositionMap[i].y() > mouse.y() && textPositionMap[i].x() > mouse.x()) break;
|
2020-03-27 05:55:11 -06:00
|
|
|
if (i == textPositionMap.size() || (mouse - textPositionMap[i]).manhattanLength() > font.pointSize() * 3) return dictionaryWindow.hide();
|
2020-03-02 23:48:29 -07:00
|
|
|
if (sentence.mid(i) == dictionaryWindow.term) return dictionaryWindow.ShowDefinition();
|
2020-02-23 00:44:26 -07:00
|
|
|
dictionaryWindow.ui.display->setFixedWidth(ui.display->width() * 3 / 4);
|
2021-01-18 18:58:21 -07:00
|
|
|
dictionaryWindow.SetTerm(sentence.mid(i));
|
2020-02-26 00:59:47 -07:00
|
|
|
int left = i == 0 ? 0 : textPositionMap[i - 1].x(), right = textPositionMap[i].x(),
|
2020-03-26 05:41:21 -06:00
|
|
|
x = textPositionMap[i].x() > ui.display->width() / 2 ? -dictionaryWindow.width() + (right * 3 + left) / 4 : (left * 3 + right) / 4, y = 0;
|
|
|
|
for (auto point : textPositionMap) if (point.y() > y && point.y() < textPositionMap[i].y()) y = point.y();
|
2020-03-27 05:55:11 -06:00
|
|
|
dictionaryWindow.move(ui.display->mapToGlobal(QPoint(x, y - dictionaryWindow.height())));
|
2020-02-22 20:58:47 -07:00
|
|
|
}
|
|
|
|
|
2021-11-06 17:14:44 -06:00
|
|
|
bool nativeEventFilter(const QByteArray&, void* message, long* result) override
|
2021-11-06 16:52:26 -06:00
|
|
|
{
|
|
|
|
auto msg = (MSG*)message;
|
2021-11-09 19:29:13 +01:00
|
|
|
if (msg->message == WM_HOTKEY)
|
2021-11-28 04:41:42 -07:00
|
|
|
if (msg->wParam == CLICK_THROUGH_HOTKEY) return ToggleClickThrough(), true;
|
2021-11-09 19:29:13 +01:00
|
|
|
return false;
|
2021-11-06 16:52:26 -06:00
|
|
|
}
|
|
|
|
|
2020-02-22 20:58:47 -07:00
|
|
|
bool eventFilter(QObject*, QEvent* event) override
|
|
|
|
{
|
2021-11-28 04:41:42 -07:00
|
|
|
if (event->type() == QEvent::MouseButtonPress) mousePressEvent((QMouseEvent*)event);
|
2019-09-05 13:42:30 -04:00
|
|
|
return false;
|
2018-12-21 15:11:12 -05:00
|
|
|
}
|
2018-12-30 22:10:56 -05:00
|
|
|
|
2021-11-28 04:41:42 -07:00
|
|
|
void timerEvent(QTimerEvent* event) override
|
|
|
|
{
|
|
|
|
if (useDictionary && QCursor::pos() != oldPos && (!dictionaryWindow.isVisible() || !dictionaryWindow.geometry().contains(QCursor::pos())))
|
|
|
|
ShowDictionary(ui.display->mapFromGlobal(QCursor::pos()));
|
|
|
|
PrettyWindow::timerEvent(event);
|
|
|
|
}
|
|
|
|
|
2019-02-25 00:00:20 -05:00
|
|
|
void mousePressEvent(QMouseEvent* event) override
|
2018-12-30 22:10:56 -05:00
|
|
|
{
|
2019-09-05 13:42:30 -04:00
|
|
|
dictionaryWindow.hide();
|
2019-01-01 22:40:29 -05:00
|
|
|
oldPos = event->globalPos();
|
2018-12-30 22:10:56 -05:00
|
|
|
}
|
|
|
|
|
2019-02-25 00:00:20 -05:00
|
|
|
void mouseMoveEvent(QMouseEvent* event) override
|
2018-12-30 22:10:56 -05:00
|
|
|
{
|
2021-11-28 07:58:16 -07:00
|
|
|
if (!posLock) move(pos() + event->globalPos() - oldPos);
|
2019-01-01 22:40:29 -05:00
|
|
|
oldPos = event->globalPos();
|
2018-12-30 22:10:56 -05:00
|
|
|
}
|
|
|
|
|
2019-08-12 10:43:34 -04:00
|
|
|
void wheelEvent(QWheelEvent* event) override
|
|
|
|
{
|
2019-09-05 13:42:30 -04:00
|
|
|
int scroll = event->angleDelta().y();
|
2021-11-28 04:41:42 -07:00
|
|
|
if (scroll > 0 && historyIndex > 0) --historyIndex;
|
|
|
|
if (scroll < 0 && historyIndex + 1 < sentenceHistory.size()) ++historyIndex;
|
|
|
|
DisplaySentence();
|
2019-08-12 10:43:34 -04:00
|
|
|
}
|
|
|
|
|
2021-11-28 07:58:16 -07:00
|
|
|
bool sizeLock, posLock, centeredText, autoResize, showOriginal, showOriginalAfterTranslation, useDictionary, clickThrough;
|
2018-12-30 22:10:56 -05:00
|
|
|
QPoint oldPos;
|
2020-02-22 20:58:47 -07:00
|
|
|
|
|
|
|
class
|
|
|
|
{
|
|
|
|
public:
|
2021-01-18 18:58:21 -07:00
|
|
|
bool CompareExchange(QLabel* display)
|
2020-02-22 20:58:47 -07:00
|
|
|
{
|
2021-11-28 04:41:42 -07:00
|
|
|
if (display->text() == text && display->font() == font && display->width() == width && display->alignment() == alignment) return false;
|
2020-02-22 20:58:47 -07:00
|
|
|
text = display->text();
|
|
|
|
font = display->font();
|
|
|
|
width = display->width();
|
2021-11-28 04:41:42 -07:00
|
|
|
alignment = display->alignment();
|
2020-02-22 20:58:47 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
QString text;
|
|
|
|
QFont font;
|
|
|
|
int width;
|
2021-11-28 04:41:42 -07:00
|
|
|
Qt::Alignment alignment;
|
2020-02-22 20:58:47 -07:00
|
|
|
} cachedDisplayInfo;
|
|
|
|
std::vector<QPoint> textPositionMap;
|
|
|
|
|
2019-08-12 10:43:34 -04:00
|
|
|
std::vector<QString> sentenceHistory;
|
2019-09-05 13:42:30 -04:00
|
|
|
int historyIndex = 0;
|
2018-12-21 15:11:12 -05:00
|
|
|
|
2019-09-05 13:42:30 -04:00
|
|
|
class DictionaryWindow : public PrettyWindow
|
|
|
|
{
|
|
|
|
public:
|
2021-01-30 12:07:37 -07:00
|
|
|
DictionaryWindow() : PrettyWindow("Dictionary Window")
|
2019-09-05 13:42:30 -04:00
|
|
|
{
|
|
|
|
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-16 17:58:09 -07:00
|
|
|
charStorage.clear();
|
2019-09-05 13:42:30 -04:00
|
|
|
|
2020-02-16 17:58:09 -07:00
|
|
|
auto StoreCopy = [&](std::string_view string)
|
2019-09-05 13:42:30 -04:00
|
|
|
{
|
2020-02-16 17:58:09 -07:00
|
|
|
auto location = &*charStorage.insert(charStorage.end(), string.begin(), string.end());
|
|
|
|
charStorage.push_back(0);
|
|
|
|
return location;
|
2019-09-05 13:42:30 -04:00
|
|
|
};
|
|
|
|
|
2020-02-16 17:58:09 -07: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-05 13:42:30 -04:00
|
|
|
{
|
2020-02-26 00:59:47 -07:00
|
|
|
const auto& [terms, definition] = read.value();
|
2020-02-16 17:58:09 -07: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-05 13:42:30 -04:00
|
|
|
}
|
2020-02-26 00:59:47 -07: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-05 13:42:30 -04:00
|
|
|
}
|
|
|
|
|
2021-01-18 18:58:21 -07:00
|
|
|
void SetTerm(QString term)
|
2019-09-05 13:42:30 -04:00
|
|
|
{
|
2020-02-22 20:58:47 -07:00
|
|
|
this->term = term;
|
2019-09-05 13:42:30 -04:00
|
|
|
UpdateDictionary();
|
|
|
|
definitions.clear();
|
|
|
|
definitionIndex = 0;
|
2020-02-26 00:59:47 -07:00
|
|
|
std::unordered_set<const char*> foundDefinitions;
|
2020-03-04 23:30:59 -07:00
|
|
|
for (term = term.left(100); !term.isEmpty(); term.chop(1))
|
2020-02-26 00:59:47 -07:00
|
|
|
for (const auto& [rootTerm, definition, inflections] : LookupDefinitions(term, foundDefinitions))
|
|
|
|
definitions.push_back(
|
2020-03-26 05:41:21 -06:00
|
|
|
QStringLiteral("<h3>%1 (%5/%6)</h3><small>%2%3</small>%4").arg(
|
2020-03-07 04:06:45 -07:00
|
|
|
term.split("<<")[0].toHtmlEscaped(),
|
|
|
|
rootTerm.split("<<")[0].toHtmlEscaped(),
|
2020-02-26 00:59:47 -07:00
|
|
|
inflections.join(""),
|
|
|
|
definition
|
|
|
|
)
|
|
|
|
);
|
2019-09-05 13:42:30 -04:00
|
|
|
for (int i = 0; i < definitions.size(); ++i) definitions[i] = definitions[i].arg(i + 1).arg(definitions.size());
|
|
|
|
ShowDefinition();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShowDefinition()
|
|
|
|
{
|
2020-02-22 20:58:47 -07:00
|
|
|
if (definitions.empty()) return hide();
|
2019-09-05 13:42:30 -04: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-22 20:58:47 -07:00
|
|
|
QString term;
|
2019-09-05 13:42:30 -04:00
|
|
|
|
|
|
|
private:
|
2020-02-26 00:59:47 -07: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)
|
2021-01-15 06:07:23 -07:00
|
|
|
if (foundDefinitions.emplace(it->definition).second) results.push_back({ term, it->definition, inflectionsUsed });
|
2020-02-26 00:59:47 -07:00
|
|
|
for (const auto& inflection : inflections) if (auto match = inflection.inflectsTo.match(term); match.hasMatch())
|
|
|
|
{
|
|
|
|
QStringList currentInflectionsUsed = inflectionsUsed;
|
|
|
|
currentInflectionsUsed.push_front(inflection.name);
|
2020-09-09 15:13:19 -06:00
|
|
|
QString root;
|
|
|
|
for (const auto& ch : inflection.root) root += ch.isDigit() ? match.captured(ch.digitValue()) : ch;
|
2020-02-26 00:59:47 -07:00
|
|
|
for (const auto& definition : LookupDefinitions(root, foundDefinitions, currentInflectionsUsed)) results.push_back(definition);
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2019-09-05 13:42:30 -04: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;
|
2020-03-26 05:41:21 -06:00
|
|
|
int oldHeight = height();
|
2019-09-05 13:42:30 -04:00
|
|
|
ShowDefinition();
|
2020-03-26 05:41:21 -06:00
|
|
|
move(x(), y() + oldHeight - height());
|
2019-09-05 13:42:30 -04:00
|
|
|
}
|
2019-06-16 00:30:41 +03:00
|
|
|
|
2020-02-26 00:59:47 -07:00
|
|
|
struct Inflection
|
|
|
|
{
|
|
|
|
QString root;
|
|
|
|
QRegularExpression inflectsTo;
|
|
|
|
QString name;
|
|
|
|
};
|
|
|
|
std::vector<Inflection> inflections;
|
|
|
|
|
2019-09-05 13:42:30 -04:00
|
|
|
std::filesystem::file_time_type dictionaryFileLastWrite;
|
2020-02-16 17:58:09 -07:00
|
|
|
std::vector<char> charStorage;
|
2019-09-05 13:42:30 -04:00
|
|
|
std::vector<QString> definitions;
|
|
|
|
int definitionIndex;
|
|
|
|
} dictionaryWindow;
|
|
|
|
} extraWindow;
|
2019-06-16 00:30:41 +03:00
|
|
|
|
2019-09-05 13:42:30 -04:00
|
|
|
bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
|
|
|
|
{
|
2019-12-29 08:57:33 -05:00
|
|
|
if (sentenceInfo["current select"] && sentenceInfo["text number"] != 0)
|
|
|
|
QMetaObject::invokeMethod(&extraWindow, [sentence = S(sentence)] { extraWindow.AddSentence(sentence); });
|
2018-12-21 15:11:12 -05:00
|
|
|
return false;
|
|
|
|
}
|