Textractor_test/extensions/extrawindow.cpp

173 lines
5.2 KiB
C++
Raw Normal View History

2018-12-21 15:11:12 -05:00
#include "extension.h"
2019-06-21 01:29:48 -04:00
#include "ui_extrawindow.h"
#include "defs.h"
#include <QInputDialog>
#include <QColorDialog>
#include <QMessageBox>
#include <QMenu>
2019-06-17 00:42:42 -04:00
#include <QFormLayout>
#include <QLineEdit>
#include <QSpinBox>
#include <QPushButton>
#include <QPainter>
#include <QMouseEvent>
#include <QSettings>
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-06-16 00:30:41 +03:00
extern const char* SHOW_ORIGINAL;
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
2019-06-21 01:29:48 -04:00
struct Window : QDialog
2018-12-21 15:11:12 -05:00
{
public:
2019-06-21 01:29:48 -04:00
Window()
2018-12-21 15:11:12 -05:00
{
2019-06-21 01:29:48 -04:00
ui.setupUi(this);
settings.beginGroup("Extra Window");
setWindowFlags(Qt::FramelessWindowHint);
setAttribute(Qt::WA_TranslucentBackground);
2019-06-21 01:29:48 -04:00
QMetaObject::invokeMethod(this, [this]
{
2019-06-21 01:29:48 -04:00
show();
QFont font = ui.display->font();
if (font.fromString(settings.value(FONT, font.toString()).toString())) ui.display->setFont(font);
setBackgroundColor(settings.value(BG_COLOR, palette().window().color()).value<QColor>());
setTextColor(settings.value(TEXT_COLOR, ui.display->palette().windowText().color()).value<QColor>());
setLock(settings.value(SIZE_LOCK, false).toBool());
setTopmost(settings.value(TOPMOST, false).toBool());
setGeometry(settings.value(WINDOW, geometry()).toRect());
menu.addAction(FONT, this, &Window::RequestFont);
menu.addAction(BG_COLOR, [this] { setBackgroundColor(QColorDialog::getColor(bgColor, this, BG_COLOR, QColorDialog::ShowAlphaChannel)); });
menu.addAction(TEXT_COLOR, [this] { setTextColor(QColorDialog::getColor(ui.display->palette().windowText().color(), this, TEXT_COLOR, QColorDialog::ShowAlphaChannel)); });
for (auto [name, default, slot] : Array<std::tuple<const char*, bool, void(Window::*)(bool)>>{
{ TOPMOST, false, &Window::setTopmost },
{ SIZE_LOCK, false, &Window::setLock },
{ SHOW_ORIGINAL, true, &Window::setShowOriginal }
})
2019-06-17 00:42:42 -04:00
{
2019-06-21 01:29:48 -04:00
auto action = menu.addAction(name, this, slot);
action->setCheckable(true);
action->setChecked(settings.value(name, default).toBool());
}
connect(ui.display, &QLabel::customContextMenuRequested, [this](QPoint point) { menu.exec(mapToGlobal(point)); });
QMetaObject::invokeMethod(this, [this] { ui.display->setText(EXTRA_WINDOW_INFO); }, Qt::QueuedConnection);
}, Qt::QueuedConnection);
}
2019-06-21 01:29:48 -04:00
~Window()
{
settings.setValue(WINDOW, geometry());
settings.sync();
}
Ui::ExtraWindow ui;
QSettings settings{ CONFIG_FILE, QSettings::IniFormat, this };
private:
2019-06-21 01:29:48 -04:00
void RequestFont()
{
QFont font = ui.display->font();
QDialog fontDialog(this, Qt::WindowCloseButtonHint);
fontDialog.setWindowTitle(FONT);
QFormLayout layout(&fontDialog);
QLineEdit fontFamily(font.family(), &fontDialog);
layout.addRow(FONT_FAMILY, &fontFamily);
QSpinBox fontSize(&fontDialog);
fontSize.setValue(font.pointSize());
layout.addRow(FONT_SIZE, &fontSize);
QSpinBox fontWeight(&fontDialog);
fontWeight.setValue(font.weight());
layout.addRow(FONT_WEIGHT, &fontWeight);
QPushButton save(SAVE_SETTINGS, &fontDialog);
layout.addWidget(&save);
connect(&save, &QPushButton::clicked, &fontDialog, &QDialog::accept);
if (!fontDialog.exec()) return;
font = QFont(fontFamily.text(), fontSize.value(), fontWeight.value());
settings.setValue(FONT, font.toString());
ui.display->setFont(font);
};
void setBackgroundColor(QColor color)
{
if (!color.isValid()) return;
if (color.alpha() == 0) color.setAlpha(1);
bgColor = color;
repaint();
settings.setValue(BG_COLOR, color);
};
void setTextColor(QColor color)
{
if (!color.isValid()) return;
auto newPalette = ui.display->palette();
newPalette.setColor(QPalette::WindowText, color);
ui.display->setPalette(newPalette);
settings.setValue(TEXT_COLOR, color);
};
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);
};
void setLock(bool lock)
{
locked = lock;
setSizeGripEnabled(!lock);
settings.setValue(SIZE_LOCK, lock);
};
void setShowOriginal(bool showOriginal)
{
if (!showOriginal) QMessageBox::information(this, SHOW_ORIGINAL, SHOW_ORIGINAL_INFO);
settings.setValue(SHOW_ORIGINAL, showOriginal);
};
void paintEvent(QPaintEvent*) override
{
QPainter(this).fillRect(rect(), bgColor);
2018-12-21 15:11:12 -05:00
}
2019-02-25 00:00:20 -05:00
void mousePressEvent(QMouseEvent* event) override
{
oldPos = event->globalPos();
}
2019-02-25 00:00:20 -05:00
void mouseMoveEvent(QMouseEvent* event) override
{
const QPoint delta = event->globalPos() - oldPos;
2019-02-25 00:00:20 -05:00
if (!locked) move(x() + delta.x(), y() + delta.y());
oldPos = event->globalPos();
}
2019-06-21 01:29:48 -04:00
QMenu menu{ ui.display };
bool locked = true;
QColor bgColor;
QPoint oldPos;
2019-06-21 01:29:48 -04:00
} window;
2018-12-21 15:11:12 -05:00
bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
{
2019-06-21 01:29:48 -04:00
if (!sentenceInfo["current select"]) return false;
2019-06-16 00:30:41 +03:00
QString qSentence = QString::fromStdWString(sentence);
2019-06-21 01:29:48 -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
2019-06-21 01:29:48 -04:00
QMetaObject::invokeMethod(&window, [=] { window.ui.display->setText(qSentence); });
2018-12-21 15:11:12 -05:00
return false;
}