Textractor/extensions/extrawindow.cpp

169 lines
4.8 KiB
C++
Raw Normal View History

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"
#include "defs.h"
#include <QColorDialog>
#include <QFontDialog>
#include <QMenu>
#include <QPainter>
#include <QMouseEvent>
2019-08-12 10:43:34 -04:00
#include <QWheelEvent>
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;
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)); });
2019-08-12 10:43:34 -04:00
QMetaObject::invokeMethod(this, [this] { AddSentence(EXTRA_WINDOW_INFO); }, Qt::QueuedConnection);
2019-06-21 01:29:48 -04:00
}, Qt::QueuedConnection);
}
2019-06-21 01:29:48 -04:00
~Window()
{
settings.setValue(WINDOW, geometry());
settings.sync();
}
2019-08-12 10:43:34 -04:00
void AddSentence(const QString& sentence)
{
sentenceHistory.push_back(sentence);
i = sentenceHistory.size() - 1;
ui.display->setText(sentence);
}
2019-06-21 01:29:48 -04:00
Ui::ExtraWindow ui;
QSettings settings{ CONFIG_FILE, QSettings::IniFormat, this };
private:
2019-06-21 01:29:48 -04:00
void RequestFont()
{
bool ok;
if (QFont font = QFontDialog::getFont(&ok, ui.display->font(), this, FONT); ok)
{
settings.setValue(FONT, font.toString());
ui.display->setFont(font);
}
2019-06-21 01:29:48 -04:00
};
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-08-12 10:43:34 -04:00
void wheelEvent(QWheelEvent* event) override
{
QPoint scroll = event->angleDelta();
if (scroll.y() > 0 && i > 0) ui.display->setText(sentenceHistory[--i]);
if (scroll.y() < 0 && i + 1 < sentenceHistory.size()) ui.display->setText(sentenceHistory[++i]);
}
2019-06-21 01:29:48 -04:00
QMenu menu{ ui.display };
bool locked = true;
QColor bgColor;
QPoint oldPos;
2019-08-12 10:43:34 -04:00
std::vector<QString> sentenceHistory;
int i = 0;
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
2019-06-29 08:46:31 +05:30
QString qSentence = S(sentence);
if (!window.settings.value(SHOW_ORIGINAL, true).toBool()) qSentence = qSentence.section('\n', qSentence.count('\n') / 2 + 1);
2019-06-16 00:30:41 +03:00
2019-08-12 10:43:34 -04:00
QMetaObject::invokeMethod(&window, [=] { window.AddSentence(qSentence); });
2018-12-21 15:11:12 -05:00
return false;
}