2019-06-29 11:16:31 +08:00
|
|
|
#include "qtcommon.h"
|
2018-12-22 04:11:12 +08:00
|
|
|
#include "extension.h"
|
2019-06-21 13:29:48 +08:00
|
|
|
#include "ui_extrawindow.h"
|
2018-12-31 11:10:56 +08:00
|
|
|
#include "defs.h"
|
2019-09-06 01:42:30 +08:00
|
|
|
#include <fstream>
|
|
|
|
#include <filesystem>
|
|
|
|
#include <process.h>
|
2018-12-31 11:10:56 +08:00
|
|
|
#include <QColorDialog>
|
2019-07-13 21:45:43 +08:00
|
|
|
#include <QFontDialog>
|
2018-12-31 11:10:56 +08:00
|
|
|
#include <QMenu>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QMouseEvent>
|
2019-08-12 22:43:34 +08:00
|
|
|
#include <QWheelEvent>
|
2018-12-22 04:11:12 +08:00
|
|
|
|
2019-02-28 00:33:17 +08:00
|
|
|
extern const char* EXTRA_WINDOW_INFO;
|
|
|
|
extern const char* TOPMOST;
|
2019-06-16 05:30:41 +08:00
|
|
|
extern const char* SHOW_ORIGINAL;
|
2019-06-17 03:50:17 +08:00
|
|
|
extern const char* SHOW_ORIGINAL_INFO;
|
2019-02-28 00:33:17 +08:00
|
|
|
extern const char* SIZE_LOCK;
|
2019-09-06 01:42:30 +08:00
|
|
|
extern const char* DICTIONARY;
|
|
|
|
extern const char* DICTIONARY_INSTRUCTIONS;
|
2019-02-28 00:33:17 +08:00
|
|
|
extern const char* BG_COLOR;
|
|
|
|
extern const char* TEXT_COLOR;
|
2019-06-17 12:42:42 +08:00
|
|
|
extern const char* FONT;
|
|
|
|
extern const char* SAVE_SETTINGS;
|
2019-02-28 00:33:17 +08:00
|
|
|
|
2019-09-06 01:42:30 +08:00
|
|
|
constexpr auto DICTIONARY_SAVE_FILE = u8"SavedDictionary.txt";
|
|
|
|
|
|
|
|
struct PrettyWindow : QDialog
|
2018-12-22 04:11:12 +08:00
|
|
|
{
|
2019-09-06 01:42:30 +08:00
|
|
|
PrettyWindow(const char* name)
|
2018-12-22 04:11:12 +08:00
|
|
|
{
|
2019-06-21 13:29:48 +08:00
|
|
|
ui.setupUi(this);
|
2018-12-31 11:10:56 +08:00
|
|
|
setWindowFlags(Qt::FramelessWindowHint);
|
|
|
|
setAttribute(Qt::WA_TranslucentBackground);
|
2019-06-21 13:29:48 +08:00
|
|
|
|
2019-09-06 01:42:30 +08:00
|
|
|
settings.beginGroup(name);
|
|
|
|
QFont font = ui.display->font();
|
|
|
|
if (font.fromString(settings.value(FONT, font.toString()).toString())) ui.display->setFont(font);
|
|
|
|
setBgColor(settings.value(BG_COLOR, bgColor).value<QColor>());
|
|
|
|
setTextColor(settings.value(TEXT_COLOR, textColor()).value<QColor>());
|
|
|
|
menu.addAction(FONT, this, &PrettyWindow::RequestFont);
|
|
|
|
menu.addAction(BG_COLOR, [this] { setBgColor(QColorDialog::getColor(bgColor, this, BG_COLOR, QColorDialog::ShowAlphaChannel)); });
|
|
|
|
menu.addAction(TEXT_COLOR, [this] { setTextColor(QColorDialog::getColor(textColor(), this, TEXT_COLOR, QColorDialog::ShowAlphaChannel)); });
|
|
|
|
connect(ui.display, &QLabel::customContextMenuRequested, [this](QPoint point) { menu.exec(mapToGlobal(point)); });
|
2018-12-31 11:10:56 +08:00
|
|
|
}
|
|
|
|
|
2019-09-06 01:42:30 +08:00
|
|
|
~PrettyWindow()
|
2019-06-21 13:29:48 +08:00
|
|
|
{
|
|
|
|
settings.sync();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ui::ExtraWindow ui;
|
2019-09-06 01:42:30 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
QMenu menu{ ui.display };
|
2019-06-21 13:29:48 +08:00
|
|
|
QSettings settings{ CONFIG_FILE, QSettings::IniFormat, this };
|
2019-01-02 11:40:29 +08:00
|
|
|
|
|
|
|
private:
|
2019-06-21 13:29:48 +08:00
|
|
|
void RequestFont()
|
|
|
|
{
|
2019-07-13 21:45:43 +08:00
|
|
|
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 13:29:48 +08:00
|
|
|
};
|
|
|
|
|
2019-09-06 01:42:30 +08:00
|
|
|
void setBgColor(QColor color)
|
2019-06-21 13:29:48 +08:00
|
|
|
{
|
|
|
|
if (!color.isValid()) return;
|
|
|
|
if (color.alpha() == 0) color.setAlpha(1);
|
|
|
|
bgColor = color;
|
|
|
|
repaint();
|
2019-09-06 01:42:30 +08:00
|
|
|
settings.setValue(BG_COLOR, "#" + QString::number(color.rgba(), 16));
|
2019-06-21 13:29:48 +08:00
|
|
|
};
|
|
|
|
|
2019-09-06 01:42:30 +08:00
|
|
|
QColor textColor()
|
|
|
|
{
|
|
|
|
return ui.display->palette().color(QPalette::WindowText);
|
|
|
|
}
|
|
|
|
|
2019-06-21 13:29:48 +08:00
|
|
|
void setTextColor(QColor color)
|
|
|
|
{
|
|
|
|
if (!color.isValid()) return;
|
2019-09-06 01:42:30 +08:00
|
|
|
ui.display->setPalette(QPalette(color, {}, {}, {}, {}, {}, {}));
|
|
|
|
settings.setValue(TEXT_COLOR, "#" + QString::number(color.rgba(), 16));
|
2019-06-21 13:29:48 +08:00
|
|
|
};
|
|
|
|
|
2019-09-06 01:42:30 +08:00
|
|
|
void paintEvent(QPaintEvent*) override
|
|
|
|
{
|
|
|
|
QPainter(this).fillRect(rect(), bgColor);
|
|
|
|
}
|
|
|
|
|
|
|
|
QColor bgColor{ palette().window().color() };
|
|
|
|
};
|
|
|
|
|
|
|
|
class ExtraWindow : public PrettyWindow
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ExtraWindow() :
|
|
|
|
PrettyWindow("Extra Window")
|
|
|
|
{
|
|
|
|
setGeometry(settings.value(WINDOW, geometry()).toRect());
|
|
|
|
|
|
|
|
for (auto [name, default, slot] : Array<std::tuple<const char*, bool, void(ExtraWindow::*)(bool)>>{
|
|
|
|
{ TOPMOST, false, &ExtraWindow::setTopmost },
|
|
|
|
{ SIZE_LOCK, false, &ExtraWindow::setLock },
|
|
|
|
{ SHOW_ORIGINAL, true, &ExtraWindow::setShowOriginal },
|
|
|
|
{ DICTIONARY, false, &ExtraWindow::setUseDictionary },
|
|
|
|
})
|
|
|
|
{
|
|
|
|
// delay processing anything until Textractor has finished initializing
|
|
|
|
QMetaObject::invokeMethod(this, std::bind(slot, this, default = settings.value(name, default).toBool()), Qt::QueuedConnection);
|
|
|
|
auto action = menu.addAction(name, this, slot);
|
|
|
|
action->setCheckable(true);
|
|
|
|
action->setChecked(default);
|
|
|
|
}
|
|
|
|
ui.display->installEventFilter(this);
|
|
|
|
|
|
|
|
QMetaObject::invokeMethod(this, [this]
|
|
|
|
{
|
|
|
|
show();
|
|
|
|
QMetaObject::invokeMethod(this, [this] { AddSentence(EXTRA_WINDOW_INFO); }, Qt::QueuedConnection);
|
|
|
|
}, Qt::QueuedConnection);
|
|
|
|
}
|
|
|
|
|
|
|
|
~ExtraWindow()
|
|
|
|
{
|
|
|
|
settings.setValue(WINDOW, geometry());
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddSentence(QString sentence)
|
|
|
|
{
|
|
|
|
if (!showOriginal) sentence = sentence.section('\n', sentence.count('\n') / 2 + 1);
|
|
|
|
sentenceHistory.push_back(sentence);
|
|
|
|
historyIndex = sentenceHistory.size() - 1;
|
|
|
|
ui.display->setText(sentence);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-06-21 13:29:48 +08:00
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2019-09-06 01:42:30 +08:00
|
|
|
void setLock(bool locked)
|
2019-06-21 13:29:48 +08:00
|
|
|
{
|
2019-09-06 01:42:30 +08:00
|
|
|
setSizeGripEnabled(!locked);
|
|
|
|
settings.setValue(SIZE_LOCK, this->locked = locked);
|
2019-06-21 13:29:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
void setShowOriginal(bool showOriginal)
|
|
|
|
{
|
2019-09-06 01:42:30 +08:00
|
|
|
if (!showOriginal && settings.value(SHOW_ORIGINAL, false).toBool()) QMessageBox::information(this, SHOW_ORIGINAL, SHOW_ORIGINAL_INFO);
|
|
|
|
settings.setValue(SHOW_ORIGINAL, this->showOriginal = showOriginal);
|
2019-06-21 13:29:48 +08:00
|
|
|
};
|
|
|
|
|
2019-09-06 01:42:30 +08:00
|
|
|
void setUseDictionary(bool useDictionary)
|
2018-12-31 11:10:56 +08:00
|
|
|
{
|
2019-09-06 01:42:30 +08:00
|
|
|
if (useDictionary)
|
|
|
|
{
|
|
|
|
dictionaryWindow.UpdateDictionary();
|
|
|
|
if (dictionaryWindow.dictionary.empty())
|
|
|
|
{
|
2019-09-10 10:08:24 +08:00
|
|
|
std::ofstream(DICTIONARY_SAVE_FILE) << u8"\ufeff" << DICTIONARY_INSTRUCTIONS;
|
2019-09-06 01:42:30 +08:00
|
|
|
_spawnlp(_P_DETACH, "notepad", "notepad", DICTIONARY_SAVE_FILE, NULL); // show file to user
|
|
|
|
}
|
|
|
|
}
|
|
|
|
settings.setValue(DICTIONARY, this->useDictionary = useDictionary);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool eventFilter(QObject*, QEvent* event) override
|
|
|
|
{
|
|
|
|
if (useDictionary && event->type() == QEvent::MouseButtonRelease && ui.display->hasSelectedText())
|
|
|
|
{
|
|
|
|
dictionaryWindow.ui.display->setFixedWidth(ui.display->width());
|
|
|
|
dictionaryWindow.setTerm(ui.display->text().mid(ui.display->selectionStart()));
|
|
|
|
dictionaryWindow.move({ x(), y() - dictionaryWindow.height() });
|
|
|
|
}
|
|
|
|
if (event->type() == QEvent::MouseButtonPress) dictionaryWindow.hide();
|
|
|
|
return false;
|
2018-12-22 04:11:12 +08:00
|
|
|
}
|
2018-12-31 11:10:56 +08:00
|
|
|
|
2019-02-25 13:00:20 +08:00
|
|
|
void mousePressEvent(QMouseEvent* event) override
|
2018-12-31 11:10:56 +08:00
|
|
|
{
|
2019-09-06 01:42:30 +08:00
|
|
|
dictionaryWindow.hide();
|
2019-01-02 11:40:29 +08:00
|
|
|
oldPos = event->globalPos();
|
2018-12-31 11:10:56 +08:00
|
|
|
}
|
|
|
|
|
2019-02-25 13:00:20 +08:00
|
|
|
void mouseMoveEvent(QMouseEvent* event) override
|
2018-12-31 11:10:56 +08:00
|
|
|
{
|
2019-09-06 01:42:30 +08:00
|
|
|
if (!locked) move(pos() + event->globalPos() - oldPos);
|
2019-01-02 11:40:29 +08:00
|
|
|
oldPos = event->globalPos();
|
2018-12-31 11:10:56 +08:00
|
|
|
}
|
|
|
|
|
2019-08-12 22:43:34 +08:00
|
|
|
void wheelEvent(QWheelEvent* event) override
|
|
|
|
{
|
2019-09-06 01:42:30 +08:00
|
|
|
int scroll = event->angleDelta().y();
|
|
|
|
if (scroll > 0 && historyIndex > 0) ui.display->setText(sentenceHistory[--historyIndex]);
|
|
|
|
if (scroll < 0 && historyIndex + 1 < sentenceHistory.size()) ui.display->setText(sentenceHistory[++historyIndex]);
|
2019-08-12 22:43:34 +08:00
|
|
|
}
|
|
|
|
|
2019-09-06 01:42:30 +08:00
|
|
|
bool locked, showOriginal, useDictionary;
|
2018-12-31 11:10:56 +08:00
|
|
|
QPoint oldPos;
|
2019-08-12 22:43:34 +08:00
|
|
|
std::vector<QString> sentenceHistory;
|
2019-09-06 01:42:30 +08:00
|
|
|
int historyIndex = 0;
|
2018-12-22 04:11:12 +08:00
|
|
|
|
2019-09-06 01:42:30 +08:00
|
|
|
class DictionaryWindow : public PrettyWindow
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DictionaryWindow() :
|
|
|
|
PrettyWindow("Dictionary Window")
|
|
|
|
{
|
|
|
|
ui.display->setSizePolicy({ QSizePolicy::Fixed, QSizePolicy::Minimum });
|
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateDictionary()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (dictionaryFileLastWrite == std::filesystem::last_write_time(DICTIONARY_SAVE_FILE)) return;
|
|
|
|
dictionaryFileLastWrite = std::filesystem::last_write_time(DICTIONARY_SAVE_FILE);
|
|
|
|
}
|
|
|
|
catch (std::filesystem::filesystem_error) { return; }
|
|
|
|
|
|
|
|
dictionary.clear();
|
|
|
|
owningStorage.clear();
|
|
|
|
|
|
|
|
auto StoreCopy = [&](const std::string& string)
|
|
|
|
{
|
|
|
|
return &*owningStorage.insert(owningStorage.end(), string.c_str(), string.c_str() + string.size() + 1);
|
|
|
|
};
|
|
|
|
|
|
|
|
std::string savedDictionary(std::istreambuf_iterator(std::ifstream(DICTIONARY_SAVE_FILE)), {});
|
|
|
|
owningStorage.reserve(savedDictionary.size());
|
|
|
|
for (size_t end = 0; ;)
|
|
|
|
{
|
|
|
|
size_t term = savedDictionary.find("|TERM|", end);
|
|
|
|
size_t definition = savedDictionary.find("|DEFINITION|", term);
|
|
|
|
if ((end = savedDictionary.find("|END|", definition)) == std::string::npos) break;
|
|
|
|
auto storedDefinition = StoreCopy(savedDictionary.substr(definition + 12, end - definition - 12));
|
|
|
|
for (size_t next; (next = savedDictionary.find("|TERM|", term + 1)) != std::string::npos && next < definition; term = next)
|
|
|
|
dictionary.push_back({ StoreCopy(savedDictionary.substr(term + 6, next - term - 6)), storedDefinition });
|
|
|
|
dictionary.push_back({ StoreCopy(savedDictionary.substr(term + 6, definition - term - 6)), storedDefinition });
|
|
|
|
}
|
|
|
|
auto oldData = owningStorage.data();
|
|
|
|
owningStorage.shrink_to_fit();
|
|
|
|
dictionary.shrink_to_fit();
|
|
|
|
for (auto& [term, definition] : dictionary)
|
|
|
|
{
|
|
|
|
term += owningStorage.data() - oldData;
|
|
|
|
definition += owningStorage.data() - oldData;
|
|
|
|
}
|
|
|
|
std::sort(dictionary.begin(), dictionary.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
void setTerm(QString term)
|
|
|
|
{
|
|
|
|
UpdateDictionary();
|
|
|
|
definitions.clear();
|
|
|
|
definitionIndex = 0;
|
|
|
|
for (QByteArray utf8term = term.left(200).toUtf8(); !utf8term.isEmpty(); utf8term.chop(1))
|
|
|
|
for (auto [it, end] = std::equal_range(dictionary.begin(), dictionary.end(), DictionaryEntry{ utf8term }); it != end; ++it)
|
|
|
|
definitions.push_back(QStringLiteral("<h3>%1 (%3/%4)</h3>%2").arg(utf8term, it->definition));
|
|
|
|
for (int i = 0; i < definitions.size(); ++i) definitions[i] = definitions[i].arg(i + 1).arg(definitions.size());
|
|
|
|
ShowDefinition();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShowDefinition()
|
|
|
|
{
|
|
|
|
if (definitions.empty()) return;
|
|
|
|
ui.display->setText(definitions[definitionIndex]);
|
|
|
|
adjustSize();
|
|
|
|
resize(width(), 1);
|
|
|
|
show();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct DictionaryEntry
|
|
|
|
{
|
|
|
|
const char* term;
|
|
|
|
const char* definition;
|
|
|
|
bool operator<(DictionaryEntry other) const { return strcmp(term, other.term) < 0; }
|
|
|
|
};
|
|
|
|
std::vector<DictionaryEntry> dictionary;
|
|
|
|
|
|
|
|
private:
|
|
|
|
void wheelEvent(QWheelEvent* event) override
|
|
|
|
{
|
|
|
|
int scroll = event->angleDelta().y();
|
|
|
|
if (scroll > 0 && definitionIndex > 0) definitionIndex -= 1;
|
|
|
|
if (scroll < 0 && definitionIndex + 1 < definitions.size()) definitionIndex += 1;
|
|
|
|
int oldHeight = height();
|
|
|
|
ShowDefinition();
|
|
|
|
move(x(), y() + oldHeight - height());
|
|
|
|
}
|
2019-06-16 05:30:41 +08:00
|
|
|
|
2019-09-06 01:42:30 +08:00
|
|
|
std::filesystem::file_time_type dictionaryFileLastWrite;
|
|
|
|
std::vector<char> owningStorage;
|
|
|
|
std::vector<QString> definitions;
|
|
|
|
int definitionIndex;
|
|
|
|
} dictionaryWindow;
|
|
|
|
} extraWindow;
|
2019-06-16 05:30:41 +08:00
|
|
|
|
2019-09-06 01:42:30 +08:00
|
|
|
bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
|
|
|
|
{
|
|
|
|
if (sentenceInfo["current select"]) QMetaObject::invokeMethod(&extraWindow, [sentence = S(sentence)] { extraWindow.AddSentence(sentence); });
|
2018-12-22 04:11:12 +08:00
|
|
|
return false;
|
|
|
|
}
|