Added all the features developed by the last PR

This commit is contained in:
Blu3train 2021-11-09 19:29:13 +01:00 committed by Akash Mozumdar
parent 0346f95707
commit 5df90e2c01
2 changed files with 169 additions and 32 deletions

View File

@ -13,6 +13,7 @@
#include <QFontMetrics>
#include <QMouseEvent>
#include <QWheelEvent>
#include <QScrollArea>
#include <QAbstractNativeEventFilter>
extern const char* EXTRA_WINDOW_INFO;
@ -22,9 +23,14 @@ extern const char* TOPMOST;
extern const char* OPACITY;
extern const char* SHOW_ORIGINAL;
extern const char* SHOW_ORIGINAL_INFO;
extern const char* SHOW_ORIGINAL_AFTER_TRANSLATION;
extern const char* SIZE_LOCK;
extern const char* POSITION_LOCK;
extern const char* CENTERED_TEXT;
extern const char* AUTO_RESIZE_WINDOW_HEIGHT;
extern const char* CLICK_THROUGH;
extern const char* HIDE_TEXT_MOUSEOVER;
extern const char* HIDE_TEXT;
extern const char* DICTIONARY;
extern const char* DICTIONARY_INSTRUCTIONS;
extern const char* BG_COLOR;
@ -32,12 +38,15 @@ extern const char* TEXT_COLOR;
extern const char* TEXT_OUTLINE;
extern const char* OUTLINE_COLOR;
extern const char* OUTLINE_SIZE;
extern const char* OUTLINE_LAST_SIZE;
extern const char* OUTLINE_SIZE_INFO;
extern const char* FONT;
extern const char* SAVE_SETTINGS;
constexpr auto DICTIONARY_SAVE_FILE = u8"SavedDictionary.txt";
const qreal COLOR_ALFAF_HIDE_WINDOW = 0.05;
constexpr int CLICK_THROUGH_HOTKEY = 0xc0d0;
constexpr int HIDE_TEXT_HOTKEY = 0xc0d1;
QColor colorPrompt(QWidget* parent, QColor default, const QString& title, bool customOpacity = true)
{
@ -48,6 +57,9 @@ QColor colorPrompt(QWidget* parent, QColor default, const QString& title, bool c
struct PrettyWindow : QDialog, Localizer
{
QString savedSentence = "";
bool hideTextMouseover=false, hideText=false;
PrettyWindow(const char* name)
{
ui.setupUi(this);
@ -78,6 +90,29 @@ struct PrettyWindow : QDialog, Localizer
Ui::ExtraWindow ui;
void SetBackgroundColorHideText()
{
if (hideText)
{
QColor color = settings.value(BG_COLOR, backgroundColor).value<QColor>();
if (!color.isValid()) return;
color.setAlphaF(COLOR_ALFAF_HIDE_WINDOW);
backgroundColor = color;
repaint();
ui.display->setText("");
}
else
{
QColor color = settings.value(BG_COLOR, backgroundColor).value<QColor>();
if (!color.isValid()) return;
if (color.alpha() == 0) color.setAlpha(1);
backgroundColor = color;
repaint();
ui.display->setText(savedSentence);
}
}
protected:
QMenu menu{ ui.display };
Settings settings{ this };
@ -85,7 +120,10 @@ protected:
private:
void RequestFont()
{
if (QFont font = QFontDialog::getFont(&ok, ui.display->font(), this, FONT); ok)
QFont font;
// Forced reading of the font from settings otherwise the font window does not have the size set
if (!font.fromString(settings.value(FONT, font.toString()).toString())) font = ui.display->font();
if (font = QFontDialog::getFont(&ok, font, this, FONT); ok)
{
settings.setValue(FONT, font.toString());
ui.display->setFont(font);
@ -119,9 +157,13 @@ private:
{
QColor color = colorPrompt(this, outliner->color, OUTLINE_COLOR);
if (color.isValid()) outliner->color = color;
outliner->size = QInputDialog::getDouble(this, OUTLINE_SIZE, OUTLINE_SIZE_INFO, 0.5, 0, INT_MAX, 2, nullptr, Qt::WindowCloseButtonHint);
outliner->size = QInputDialog::getDouble(this, OUTLINE_SIZE, OUTLINE_SIZE_INFO, settings.value(OUTLINE_LAST_SIZE, outliner->size).toDouble(), 0, INT_MAX, 2, nullptr, Qt::WindowCloseButtonHint);
}
else
{
settings.setValue(OUTLINE_LAST_SIZE, outliner->size);
outliner->size = -1;
}
else outliner->size = -1;
settings.setValue(OUTLINE_COLOR, outliner->color.name(QColor::HexArgb));
settings.setValue(OUTLINE_SIZE, outliner->size);
}
@ -153,6 +195,27 @@ private:
QColor color{ Qt::black };
double size = -1;
}* outliner;
void enterEvent(QEvent* event)
{
if (hideTextMouseover)
{
hideText = true;
SetBackgroundColorHideText();
}
QWidget::enterEvent(event);
}
void leaveEvent(QEvent* event)
{
if (hideTextMouseover)
{
hideText = false;
SetBackgroundColorHideText();
}
QWidget::leaveEvent(event);
}
};
class ExtraWindow : public PrettyWindow, QAbstractNativeEventFilter
@ -167,8 +230,13 @@ public:
for (auto [name, default, slot] : Array<const char*, bool, void(ExtraWindow::*)(bool)>{
{ TOPMOST, false, &ExtraWindow::SetTopmost },
{ SIZE_LOCK, false, &ExtraWindow::SetLock },
{ POSITION_LOCK, false, &ExtraWindow::SetPositionLock },
{ CENTERED_TEXT, false, &ExtraWindow::SetCenteredText },
{ AUTO_RESIZE_WINDOW_HEIGHT, false, &ExtraWindow::SetAutoResizeHeight },
{ HIDE_TEXT, false, &ExtraWindow::SetHideText },
{ HIDE_TEXT_MOUSEOVER, false, &ExtraWindow::SetHideTextMouseover },
{ SHOW_ORIGINAL, true, &ExtraWindow::SetShowOriginal },
{ SHOW_ORIGINAL_AFTER_TRANSLATION, true, &ExtraWindow::SetShowOriginalAfterTranslation },
{ DICTIONARY, false, &ExtraWindow::SetUseDictionary },
})
{
@ -177,6 +245,10 @@ public:
auto action = menu.addAction(name, this, slot);
action->setCheckable(true);
action->setChecked(default);
if (slot == &ExtraWindow::SetHideTextMouseover)
hideTextMouseoveAction = action;
if (slot == &ExtraWindow::SetHideText)
hideTextAction = action;
}
menu.addAction(CLICK_THROUGH, this, &ExtraWindow::ToggleClickThrough);
@ -191,7 +263,8 @@ public:
QMetaObject::invokeMethod(this, [this]
{
RegisterHotKey((HWND)winId(), CLICK_THROUGH_HOTKEY, MOD_SHIFT | MOD_NOREPEAT, 0x58);
RegisterHotKey((HWND)winId(), HIDE_TEXT_HOTKEY, MOD_ALT | MOD_SHIFT | MOD_NOREPEAT, 0x48);
RegisterHotKey((HWND)winId(), CLICK_THROUGH_HOTKEY, MOD_ALT | MOD_SHIFT | MOD_NOREPEAT, 0x54);
show();
AddSentence(EXTRA_WINDOW_INFO);
}, Qt::QueuedConnection);
@ -205,13 +278,18 @@ public:
void AddSentence(QString sentence)
{
if (sentence.size() > maxSentenceSize) sentence = SENTENCE_TOO_BIG;
if (!showOriginal && sentence.contains(u8"\x200b \n")) sentence = sentence.split(u8"\x200b \n")[1];
if (showOriginal){
if (showOriginalAfterTranslation && sentence.contains(u8"\x200b \n"))
sentence = sentence.split(u8"\x200b \n")[1] + "\n" + sentence.split(u8"\x200b \n")[0];
} else
if (sentence.contains(u8"\x200b \n")) sentence = sentence.split(u8"\x200b \n")[1];
sanitize(sentence);
sentence.chop(std::distance(std::remove(sentence.begin(), sentence.end(), QChar::Tabulation), sentence.end()));
sentenceHistory.push_back(sentence);
if (sentenceHistory.size() > maxHistoryIndex) sentenceHistory.erase(sentenceHistory.begin());
historyIndex = sentenceHistory.size() - 1;
ui.display->setText(sentence);
savedSentence = sentence;
if (!hideText) ui.display->setText(sentence);
AutoResize(sentence);
}
@ -223,21 +301,31 @@ private:
settings.setValue(TOPMOST, topmost);
};
void SetPositionLock(bool locked)
{
settings.setValue(POSITION_LOCK, this->locked = locked);
};
void SetLock(bool locked)
{
setSizeGripEnabled(!locked);
settings.setValue(SIZE_LOCK, this->locked = locked);
settings.setValue(SIZE_LOCK, locked);
};
void SetCenteredText(bool centeredCenter)
{
if (centeredCenter)
ui.display->setAlignment(Qt::AlignCenter);
else
ui.display->setAlignment(Qt::AlignLeft);
settings.setValue(CENTERED_TEXT, this->centeredCenter = centeredCenter);
};
void SetAutoResizeHeight(bool autoResizeHeight)
{
settings.setValue(AUTO_RESIZE_WINDOW_HEIGHT, this->autoResizeHeight = autoResizeHeight);
// If we disable this then we need to reset window to a default height. For now we will just use value from settings
if (!autoResizeHeight && settings.contains(WINDOW) && QApplication::screenAt(settings.value(WINDOW).toRect().bottomRight()))
{
setGeometry(settings.value(WINDOW).toRect());
}
if (autoResizeHeight) AutoResize(savedSentence);
};
void AutoResize(QString sentence)
@ -272,12 +360,45 @@ private:
show();
};
void SetHideTextMouseover(bool hideTextMouseover)
{
this->hideTextMouseover = hideTextMouseover;
if (hideTextMouseover && this->hideText)
{
this->hideText = false;
hideTextAction->setChecked(false);
SetBackgroundColorHideText();
}
};
void SetHideText(bool hideText)
{
this->hideText = hideText;
if (this->hideTextMouseover)
{
this->hideTextMouseover = false;
hideTextMouseoveAction->setChecked(false);
}
SetBackgroundColorHideText();
};
void ToggleHideText()
{
hideTextAction->setChecked(!hideText);
SetHideText(!hideText);
};
void SetShowOriginal(bool showOriginal)
{
if (!showOriginal && settings.value(SHOW_ORIGINAL, false).toBool()) QMessageBox::information(this, SHOW_ORIGINAL, SHOW_ORIGINAL_INFO);
settings.setValue(SHOW_ORIGINAL, this->showOriginal = showOriginal);
};
void SetShowOriginalAfterTranslation(bool showOriginalAfterTranslation)
{
settings.setValue(SHOW_ORIGINAL_AFTER_TRANSLATION, this->showOriginalAfterTranslation = showOriginalAfterTranslation);
};
void SetUseDictionary(bool useDictionary)
{
if (useDictionary)
@ -332,9 +453,23 @@ private:
bool nativeEventFilter(const QByteArray&, void* message, long* result) override
{
auto msg = (MSG*)message;
if (msg->message != WM_HOTKEY || msg->wParam != CLICK_THROUGH_HOTKEY) return false;
ToggleClickThrough();
return true;
if (msg->message == WM_HOTKEY)
{
switch (msg->wParam)
{
case HIDE_TEXT_HOTKEY:
{
ToggleHideText();
return true;
}
case CLICK_THROUGH_HOTKEY:
{
ToggleClickThrough();
return true;
}
}
}
return false;
}
bool eventFilter(QObject*, QEvent* event) override
@ -364,7 +499,8 @@ private:
AutoResize(sentenceHistory[historyIndex]);
}
bool locked, autoResizeHeight, showOriginal, useDictionary, clickThrough;
QAction *hideTextMouseoveAction, *hideTextAction;
bool locked, centeredCenter, autoResizeHeight, showOriginal, showOriginalAfterTranslation, useDictionary, clickThrough;
int maxSentenceSize = 1000;
QPoint oldPos;
@ -389,6 +525,7 @@ private:
std::vector<QString> sentenceHistory;
int historyIndex = 0;
const int maxHistoryIndex = 20;
class DictionaryWindow : public PrettyWindow
{

View File

@ -178,15 +178,21 @@ This file must be encoded in UTF-8.)";
const char* SHOW_ORIGINAL = u8"Original text";
const char* SHOW_ORIGINAL_INFO = u8R"(Original text will not be shown
Only works if this extension is used directly after a translation extension)";
const char* SHOW_ORIGINAL_AFTER_TRANSLATION = u8"Show original text after translation";
const char* SIZE_LOCK = u8"Size lock";
const char* POSITION_LOCK = u8"Position lock";
const char* CENTERED_TEXT = u8"Centered text";
const char* AUTO_RESIZE_WINDOW_HEIGHT = u8"Auto resize window height";
const char* CLICK_THROUGH = u8"Click through";
const char* CLICK_THROUGH = u8"Click through\tAlt+Shift+T";
const char* HIDE_TEXT_MOUSEOVER = u8"Hide text mouseover";
const char* HIDE_TEXT = u8"Hide text\tAlt+Shift+H";
const char* OPACITY = u8"Opacity";
const char* BG_COLOR = u8"Background color";
const char* TEXT_COLOR = u8"Text color";
const char* TEXT_OUTLINE = u8"Text outline";
const char* OUTLINE_COLOR = u8"Outline color";
const char* OUTLINE_SIZE = u8"Outline size";
const char* OUTLINE_LAST_SIZE = u8"Last used outline size";
const char* OUTLINE_SIZE_INFO = u8"Size in pixels (recommended to stay below 20% of the font size)";
const char* FONT = u8"Font";
const char* LUA_INTRO = u8R"(--[[
@ -437,8 +443,6 @@ Clic y arrastra los bordes de la ventana para moverla, o en la esquina inferior
使)";
OPACITY = u8"透明度";
SIZE_LOCK = u8"锁定窗口大小";
AUTO_RESIZE_WINDOW_HEIGHT = u8"Auto resize window height";
CLICK_THROUGH = u8"Click Through";
BG_COLOR = u8"背景颜色";
TEXT_COLOR = u8"文本颜色";
TEXT_OUTLINE = u8"文字边框";
@ -628,8 +632,6 @@ Textractor отобразит конечный корневой термин, а
SHOW_ORIGINAL_INFO = u8R"(Исходный текст будет скрыт
Работает только если это расширение используется после расширения перевода)";
SIZE_LOCK = u8"Фиксированный размер";
AUTO_RESIZE_WINDOW_HEIGHT = u8"Auto resize window height";
CLICK_THROUGH = u8"Click Through";
OPACITY = u8"Прозрачность";
BG_COLOR = u8"Цвет заднего фона";
TEXT_COLOR = u8"Цвет текста";
@ -889,15 +891,21 @@ Questo file deve essere codificato in UTF-8.)";
SHOW_ORIGINAL = u8"Testo originale";
SHOW_ORIGINAL_INFO = u8R"(Testo originale non sarà mostrato
Funziona solo se questa estenzione è usata direttamente dopo un'estensione di traduzione)";
SHOW_ORIGINAL_AFTER_TRANSLATION = u8"Mostra testo originale dopo traduzione";
SIZE_LOCK = u8"Lock delle dimensione";
POSITION_LOCK = u8"Lock delle posizione";
CENTERED_TEXT = u8"Testo centrato";
AUTO_RESIZE_WINDOW_HEIGHT = u8"Auto resize altezza finestra";
CLICK_THROUGH = u8"Clicca attraverso";
CLICK_THROUGH = u8"Clicca attraverso\tAlt+Shift+T";
HIDE_TEXT_MOUSEOVER = u8"Nascondi testo mouseover";
HIDE_TEXT = u8"Nascondi testo\tAlt+Shift+H";
OPACITY = u8"Opacità";
BG_COLOR = u8"Colore dello sfondo";
TEXT_COLOR = u8"Colore del testo";
TEXT_OUTLINE = u8"Contorno del testo";
OUTLINE_COLOR = u8"Colore del contorno";
OUTLINE_SIZE = u8"Dimensione del contorno";
OUTLINE_LAST_SIZE = u8"Ultima dimensione del contorno utilizzata";
OUTLINE_SIZE_INFO = u8"Dimensione in pixel (consigliato di rimanere sotto il 20% della dimensione del font)";
FONT = u8"Font";
LUA_INTRO = u8R"(--[[
@ -1006,8 +1014,6 @@ Clique e arraste nas beiradas da janela para mover, ou no canto inferior direito
SHOW_ORIGINAL_INFO = u8R"(Texto original não será mostrado
Apenas funciona se essa extensão for usada diretamente após uma extensão de tradução)";
SIZE_LOCK = u8"Travar o Tamanho";
AUTO_RESIZE_WINDOW_HEIGHT = u8"Auto resize window height";
CLICK_THROUGH = u8"Click Through";
BG_COLOR = u8"Cor de fundo";
TEXT_COLOR = u8"Cor do Texto";
FONT = u8"Fonte";
@ -1083,8 +1089,6 @@ Source code สามารถหาได้จากส่วนของ GPLv
TOPMOST = u8"หน้าต่างอยู่บนโปรแกรมอื่น";
SHOW_ORIGINAL = u8"ข้อความดังเดิมก่อนแปลภาษา";
SIZE_LOCK = u8"ปรับให้ไม่สามารถเปลี่ยนขนาดได้";
AUTO_RESIZE_WINDOW_HEIGHT = u8"Auto resize window height";
CLICK_THROUGH = u8"Click Through";
FONT = u8"ฟ้อนต์";
#endif // THAI
@ -1150,8 +1154,6 @@ Source code สามารถหาได้จากส่วนของ GPLv
SHOW_ORIGINAL_INFO = u8R"(원문이 출력되지 않음
)";
SIZE_LOCK = u8"사이즈 고정";
AUTO_RESIZE_WINDOW_HEIGHT = u8"Auto resize window height";
CLICK_THROUGH = u8"Click Through";
BG_COLOR = u8"배경색";
TEXT_COLOR = u8"글씨색";
FONT = u8"폰트";
@ -1317,8 +1319,6 @@ Ce fichier doit être encodé en UTF-8.)";
SHOW_ORIGINAL_INFO = u8R"(Le texte d'origine ne sera pas affiché
Fonctionne uniquement si cette extension est utilisée directement après une extension de traduction)";
SIZE_LOCK = u8"Verouiller la taille";
AUTO_RESIZE_WINDOW_HEIGHT = u8"Auto resize window height";
CLICK_THROUGH = u8"Click Through";
OPACITY = u8"Opacité";
BG_COLOR = u8"Couleur d'arrière-plan";
TEXT_COLOR = u8"Couleur du texte";