Textractor_test/extensions/extrawindow.cpp

151 lines
4.4 KiB
C++
Raw Normal View History

2018-12-22 04:11:12 +08:00
#include "extension.h"
#include "defs.h"
2018-12-22 04:11:12 +08:00
#include "text.h"
#include <QDialog>
#include <QInputDialog>
#include <QColorDialog>
#include <QMenu>
2018-12-22 04:11:12 +08:00
#include <QLayout>
#include <QLabel>
#include <QPainter>
#include <QMouseEvent>
#include <QSettings>
2018-12-22 04:11:12 +08:00
#include <QTimer>
std::mutex m;
struct : QDialog
2018-12-22 04:11:12 +08:00
{
public:
2018-12-22 04:11:12 +08:00
void launch()
{
settings->beginGroup("Extra Window");
(new QHBoxLayout(this))->addWidget(display = new QLabel(EXTRA_WINDOW_INFO, this));
2019-01-06 13:07:20 +08:00
display->setTextFormat(Qt::PlainText);
display->setTextInteractionFlags(Qt::TextSelectableByMouse);
display->setAlignment(Qt::AlignTop);
2019-01-05 16:48:25 +08:00
display->setWordWrap(true);
2019-01-06 13:07:20 +08:00
display->setMaximumHeight(600);
setWindowFlags(Qt::FramelessWindowHint);
setAttribute(Qt::WA_TranslucentBackground);
setSizeGripEnabled(true);
2019-01-06 13:07:20 +08:00
resize(400, 1);
2018-12-22 04:11:12 +08:00
show();
auto setBackgroundColor = [=](QColor color)
{
if (!color.isValid()) return;
if (color.alpha() == 0) color.setAlpha(1);
bgColor = color;
repaint();
settings->setValue(BG_COLOR, color);
};
auto setTextColor = [=](QColor color)
{
if (!color.isValid()) return;
auto newPalette = display->palette();
newPalette.setColor(QPalette::WindowText, color);
display->setPalette(newPalette);
settings->setValue(TEXT_COLOR, color);
};
auto setFontSize = [=](int pt)
{
QFont newFont = display->font();
newFont.setPointSize(pt);
display->setFont(newFont);
settings->setValue(FONT_SIZE, pt);
};
auto 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);
};
setGeometry(settings->value(WINDOW, geometry()).toRect());
setTopmost(settings->value(TOPMOST, false).toBool());
setFontSize(settings->value(FONT_SIZE, 16).toInt());
setBackgroundColor(settings->value(BG_COLOR, palette().window().color()).value<QColor>());
setTextColor(settings->value(TEXT_COLOR, display->palette().windowText().color()).value<QColor>());
2019-01-06 13:07:20 +08:00
auto menu = new QMenu(display);
auto topmost = menu->addAction(TOPMOST, setTopmost);
topmost->setCheckable(true);
2019-01-06 13:07:20 +08:00
topmost->setChecked(settings->value(TOPMOST, false).toBool());
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)); });
menu->addAction(FONT_SIZE, [=] { setFontSize(QInputDialog::getInt(this, FONT_SIZE, "", display->font().pointSize(), 0, INT_MAX, 1, nullptr, Qt::WindowCloseButtonHint)); });
2019-01-06 13:07:20 +08:00
display->setContextMenuPolicy(Qt::CustomContextMenu);
connect(display, &QLabel::customContextMenuRequested, [=](QPoint point) { menu->exec(mapToGlobal(point)); });
connect(this, &QDialog::destroyed, [=] { settings->setValue(WINDOW, geometry()); });
}
QSettings* settings = new QSettings(CONFIG_FILE, QSettings::IniFormat, this);
QLabel* display;
private:
void paintEvent(QPaintEvent*) override
{
QPainter(this).fillRect(rect(), bgColor);
2018-12-22 04:11:12 +08:00
}
2019-01-06 13:07:20 +08:00
void resizeEvent(QResizeEvent* event) override
{
display->setMaximumWidth(event->size().width());
QDialog::resizeEvent(event);
}
void mousePressEvent(QMouseEvent* event)
{
oldPos = event->globalPos();
}
void mouseMoveEvent(QMouseEvent* event)
{
const QPoint delta = event->globalPos() - oldPos;
move(x() + delta.x(), y() + delta.y());
oldPos = event->globalPos();
}
QColor bgColor;
QPoint oldPos;
2018-12-22 04:11:12 +08:00
}*window = nullptr;
BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
QTimer::singleShot(0, []
{
std::lock_guard l(m);
(window = new std::remove_pointer_t<decltype(window)>)->launch();
2018-12-22 04:11:12 +08:00
});
}
break;
case DLL_PROCESS_DETACH:
{
std::lock_guard l(m);
2019-01-06 13:07:20 +08:00
if (window != nullptr)
{
window->settings->setValue(WINDOW, window->geometry());
window->settings->sync();
}
2018-12-22 04:11:12 +08: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;
QMetaObject::invokeMethod(window, [=] { window->display->setText(QString::fromStdWString(sentence)); });
2018-12-22 04:11:12 +08:00
return false;
}