2018-12-21 15:11:12 -05:00
|
|
|
#include "extension.h"
|
2018-12-30 22:10:56 -05:00
|
|
|
#include "defs.h"
|
|
|
|
#include <QDialog>
|
|
|
|
#include <QInputDialog>
|
|
|
|
#include <QColorDialog>
|
2019-06-16 15:50:17 -04:00
|
|
|
#include <QMessageBox>
|
2018-12-30 22:10:56 -05:00
|
|
|
#include <QMenu>
|
2018-12-21 15:11:12 -05:00
|
|
|
#include <QLayout>
|
|
|
|
#include <QLabel>
|
2019-06-17 00:42:42 -04:00
|
|
|
#include <QFormLayout>
|
|
|
|
#include <QLineEdit>
|
|
|
|
#include <QSpinBox>
|
|
|
|
#include <QPushButton>
|
2018-12-30 22:10:56 -05:00
|
|
|
#include <QPainter>
|
|
|
|
#include <QMouseEvent>
|
|
|
|
#include <QSettings>
|
2018-12-21 15:11:12 -05:00
|
|
|
#include <QTimer>
|
|
|
|
|
2019-02-27 11:33:17 -05:00
|
|
|
extern const char* EXTRA_WINDOW_INFO;
|
|
|
|
extern const char* TOPMOST;
|
2019-06-16 00:30:41 +03:00
|
|
|
extern const char* SHOW_ORIGINAL;
|
2019-06-16 15:50:17 -04:00
|
|
|
extern const char* SHOW_ORIGINAL_INFO;
|
2019-02-27 11:33:17 -05:00
|
|
|
extern const char* SIZE_LOCK;
|
|
|
|
extern const char* BG_COLOR;
|
|
|
|
extern const char* TEXT_COLOR;
|
2019-06-17 00:42:42 -04:00
|
|
|
extern const char* FONT;
|
2019-02-27 11:33:17 -05:00
|
|
|
extern const char* FONT_SIZE;
|
2019-06-17 00:42:42 -04:00
|
|
|
extern const char* FONT_FAMILY;
|
|
|
|
extern const char* FONT_WEIGHT;
|
|
|
|
extern const char* SAVE_SETTINGS;
|
2019-02-27 11:33:17 -05:00
|
|
|
|
2018-12-21 15:11:12 -05:00
|
|
|
std::mutex m;
|
|
|
|
|
2018-12-30 22:10:56 -05:00
|
|
|
struct : QDialog
|
2018-12-21 15:11:12 -05:00
|
|
|
{
|
2019-01-01 22:40:29 -05:00
|
|
|
public:
|
2018-12-21 15:11:12 -05:00
|
|
|
void launch()
|
|
|
|
{
|
2018-12-30 22:10:56 -05:00
|
|
|
settings->beginGroup("Extra Window");
|
2019-01-01 22:40:29 -05:00
|
|
|
(new QHBoxLayout(this))->addWidget(display = new QLabel(EXTRA_WINDOW_INFO, this));
|
2019-01-06 00:07:20 -05:00
|
|
|
display->setTextFormat(Qt::PlainText);
|
|
|
|
display->setTextInteractionFlags(Qt::TextSelectableByMouse);
|
2019-01-01 22:40:29 -05:00
|
|
|
display->setAlignment(Qt::AlignTop);
|
2019-01-05 03:48:25 -05:00
|
|
|
display->setWordWrap(true);
|
2019-02-25 00:00:20 -05:00
|
|
|
display->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
|
2018-12-30 22:10:56 -05:00
|
|
|
setWindowFlags(Qt::FramelessWindowHint);
|
|
|
|
setAttribute(Qt::WA_TranslucentBackground);
|
|
|
|
setSizeGripEnabled(true);
|
2019-02-25 00:00:20 -05:00
|
|
|
resize(400, 300);
|
2018-12-21 15:11:12 -05:00
|
|
|
show();
|
2018-12-30 22:10:56 -05:00
|
|
|
|
|
|
|
auto setBackgroundColor = [=](QColor color)
|
|
|
|
{
|
|
|
|
if (!color.isValid()) return;
|
|
|
|
if (color.alpha() == 0) color.setAlpha(1);
|
|
|
|
bgColor = color;
|
|
|
|
repaint();
|
2019-01-01 22:40:29 -05:00
|
|
|
settings->setValue(BG_COLOR, color);
|
2018-12-30 22:10:56 -05:00
|
|
|
};
|
|
|
|
auto setTextColor = [=](QColor color)
|
|
|
|
{
|
|
|
|
if (!color.isValid()) return;
|
|
|
|
auto newPalette = display->palette();
|
|
|
|
newPalette.setColor(QPalette::WindowText, color);
|
|
|
|
display->setPalette(newPalette);
|
2019-01-01 22:40:29 -05:00
|
|
|
settings->setValue(TEXT_COLOR, color);
|
2018-12-30 22:10:56 -05:00
|
|
|
};
|
2019-06-17 00:42:42 -04:00
|
|
|
auto requestFont = [=]
|
2018-12-30 22:10:56 -05:00
|
|
|
{
|
2019-06-17 00:42:42 -04:00
|
|
|
QFont font = display->font();
|
|
|
|
auto fontDialog = new QDialog(this, Qt::WindowCloseButtonHint);
|
|
|
|
fontDialog->setAttribute(Qt::WA_DeleteOnClose);
|
|
|
|
fontDialog->setWindowTitle(FONT);
|
|
|
|
auto layout = new QFormLayout(fontDialog);
|
|
|
|
fontDialog->setLayout(layout);
|
|
|
|
auto fontFamily = new QLineEdit(font.family(), fontDialog);
|
|
|
|
layout->addRow(FONT_FAMILY, fontFamily);
|
|
|
|
auto fontSize = new QSpinBox(fontDialog);
|
|
|
|
fontSize->setValue(font.pointSize());
|
|
|
|
layout->addRow(FONT_SIZE, fontSize);
|
|
|
|
auto fontWeight = new QSpinBox(fontDialog);
|
|
|
|
fontWeight->setValue(font.weight());
|
|
|
|
layout->addRow(FONT_WEIGHT, fontWeight);
|
|
|
|
auto save = new QPushButton(SAVE_SETTINGS, fontDialog);
|
|
|
|
layout->addWidget(save);
|
|
|
|
connect(save, &QPushButton::clicked, fontDialog, &QDialog::accept);
|
|
|
|
fontDialog->open();
|
|
|
|
connect(fontDialog, &QDialog::accepted, [=]
|
|
|
|
{
|
|
|
|
QFont font(fontFamily->text(), fontSize->value(), fontWeight->value());
|
|
|
|
settings->setValue(FONT, font.toString());
|
|
|
|
display->setFont(font);
|
|
|
|
});
|
2018-12-30 22:10:56 -05:00
|
|
|
};
|
|
|
|
auto setTopmost = [=](bool topmost)
|
|
|
|
{
|
|
|
|
SetWindowPos((HWND)winId(), topmost ? HWND_TOPMOST : HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
|
2019-01-01 22:40:29 -05:00
|
|
|
settings->setValue(TOPMOST, topmost);
|
2018-12-30 22:10:56 -05:00
|
|
|
};
|
2019-02-25 00:00:20 -05:00
|
|
|
auto setLock = [=](bool lock)
|
|
|
|
{
|
|
|
|
locked = lock;
|
|
|
|
setSizeGripEnabled(!lock);
|
|
|
|
settings->setValue(SIZE_LOCK, lock);
|
|
|
|
};
|
2019-06-16 15:50:17 -04:00
|
|
|
auto setShowOriginal = [=](bool showOriginal)
|
|
|
|
{
|
|
|
|
if (!showOriginal) QMessageBox::information(this, SHOW_ORIGINAL, SHOW_ORIGINAL_INFO);
|
2019-06-16 00:30:41 +03:00
|
|
|
settings->setValue(SHOW_ORIGINAL, showOriginal);
|
|
|
|
};
|
2019-01-01 22:40:29 -05:00
|
|
|
setGeometry(settings->value(WINDOW, geometry()).toRect());
|
2019-02-25 00:00:20 -05:00
|
|
|
setLock(settings->value(SIZE_LOCK, false).toBool());
|
2019-01-01 22:40:29 -05:00
|
|
|
setTopmost(settings->value(TOPMOST, false).toBool());
|
2019-06-17 00:42:42 -04:00
|
|
|
QFont font = display->font();
|
|
|
|
font.setPointSize(16);
|
|
|
|
font.fromString(settings->value(FONT, font.toString()).toString());
|
|
|
|
display->setFont(font);
|
2019-01-01 22:40:29 -05:00
|
|
|
setBackgroundColor(settings->value(BG_COLOR, palette().window().color()).value<QColor>());
|
|
|
|
setTextColor(settings->value(TEXT_COLOR, display->palette().windowText().color()).value<QColor>());
|
2018-12-30 22:10:56 -05:00
|
|
|
|
2019-01-06 00:07:20 -05:00
|
|
|
auto menu = new QMenu(display);
|
2019-01-01 22:40:29 -05:00
|
|
|
auto topmost = menu->addAction(TOPMOST, setTopmost);
|
|
|
|
topmost->setCheckable(true);
|
2019-01-06 00:07:20 -05:00
|
|
|
topmost->setChecked(settings->value(TOPMOST, false).toBool());
|
2019-02-25 00:00:20 -05:00
|
|
|
auto lock = menu->addAction(SIZE_LOCK, setLock);
|
|
|
|
lock->setCheckable(true);
|
|
|
|
lock->setChecked(settings->value(SIZE_LOCK, false).toBool());
|
2019-06-16 00:30:41 +03:00
|
|
|
auto showOriginal = menu->addAction(SHOW_ORIGINAL, setShowOriginal);
|
|
|
|
showOriginal->setCheckable(true);
|
|
|
|
showOriginal->setChecked(settings->value(SHOW_ORIGINAL, true).toBool());
|
2019-01-01 22:40:29 -05:00
|
|
|
menu->addAction(BG_COLOR, [=] { setBackgroundColor(QColorDialog::getColor(bgColor, this, BG_COLOR, QColorDialog::ShowAlphaChannel)); });
|
|
|
|
menu->addAction(TEXT_COLOR, [=] { setTextColor(QColorDialog::getColor(display->palette().windowText().color(), this, TEXT_COLOR, QColorDialog::ShowAlphaChannel)); });
|
2019-06-17 00:42:42 -04:00
|
|
|
menu->addAction(FONT, requestFont);
|
2019-01-06 00:07:20 -05:00
|
|
|
display->setContextMenuPolicy(Qt::CustomContextMenu);
|
|
|
|
connect(display, &QLabel::customContextMenuRequested, [=](QPoint point) { menu->exec(mapToGlobal(point)); });
|
2019-01-01 22:40:29 -05:00
|
|
|
connect(this, &QDialog::destroyed, [=] { settings->setValue(WINDOW, geometry()); });
|
2018-12-30 22:10:56 -05:00
|
|
|
}
|
|
|
|
|
2019-01-01 22:40:29 -05:00
|
|
|
QSettings* settings = new QSettings(CONFIG_FILE, QSettings::IniFormat, this);
|
|
|
|
QLabel* display;
|
|
|
|
|
|
|
|
private:
|
2018-12-30 22:10:56 -05:00
|
|
|
void paintEvent(QPaintEvent*) override
|
|
|
|
{
|
|
|
|
QPainter(this).fillRect(rect(), bgColor);
|
2018-12-21 15:11:12 -05:00
|
|
|
}
|
2018-12-30 22:10:56 -05:00
|
|
|
|
2019-02-25 00:00:20 -05:00
|
|
|
void mousePressEvent(QMouseEvent* event) override
|
2018-12-30 22:10:56 -05:00
|
|
|
{
|
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
|
|
|
{
|
2019-01-01 22:40:29 -05:00
|
|
|
const QPoint delta = event->globalPos() - oldPos;
|
2019-02-25 00:00:20 -05:00
|
|
|
if (!locked) move(x() + delta.x(), y() + delta.y());
|
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
|
|
|
bool locked;
|
2019-01-01 22:40:29 -05:00
|
|
|
QColor bgColor;
|
2018-12-30 22:10:56 -05:00
|
|
|
QPoint oldPos;
|
2018-12-21 15:11:12 -05:00
|
|
|
}*window = nullptr;
|
|
|
|
|
|
|
|
BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
|
|
|
|
{
|
|
|
|
switch (ul_reason_for_call)
|
|
|
|
{
|
|
|
|
case DLL_PROCESS_ATTACH:
|
|
|
|
{
|
2018-12-30 22:10:56 -05:00
|
|
|
QTimer::singleShot(0, []
|
|
|
|
{
|
|
|
|
std::lock_guard l(m);
|
|
|
|
(window = new std::remove_pointer_t<decltype(window)>)->launch();
|
2018-12-21 15:11:12 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DLL_PROCESS_DETACH:
|
|
|
|
{
|
2018-12-30 22:10:56 -05:00
|
|
|
std::lock_guard l(m);
|
2019-06-16 17:15:47 -04:00
|
|
|
if (window)
|
2019-01-06 00:07:20 -05:00
|
|
|
{
|
|
|
|
window->settings->setValue(WINDOW, window->geometry());
|
|
|
|
window->settings->sync();
|
|
|
|
}
|
2018-12-21 15:11:12 -05:00
|
|
|
if (lpReserved == NULL) // https://blogs.msdn.microsoft.com/oldnewthing/20120105-00/?p=8683
|
|
|
|
{
|
|
|
|
delete window;
|
|
|
|
window = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
|
|
|
|
{
|
|
|
|
std::lock_guard l(m);
|
|
|
|
if (window == nullptr || !sentenceInfo["current select"]) return false;
|
2019-06-16 00:30:41 +03:00
|
|
|
|
|
|
|
QString qSentence = QString::fromStdWString(sentence);
|
2019-06-16 15:50:17 -04:00
|
|
|
if (!window->settings->value(SHOW_ORIGINAL).toBool()) qSentence = qSentence.section('\n', qSentence.count('\n') / 2 + 1);
|
2019-06-16 00:30:41 +03:00
|
|
|
|
|
|
|
QMetaObject::invokeMethod(window, [=] { window->display->setText(qSentence); });
|
2018-12-21 15:11:12 -05:00
|
|
|
return false;
|
|
|
|
}
|