Skip to content

Commit

Permalink
[Fix] focus handling
Browse files Browse the repository at this point in the history
  • Loading branch information
SakuraKoi committed Feb 17, 2021
1 parent 8c97421 commit 71755a7
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 17 deletions.
7 changes: 6 additions & 1 deletion BattlefieldChat/BattlefieldChat.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,12 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="GlobalVariables.cpp" />
<ClCompile Include="InputDialog.cpp" />
<ClCompile Include="InputDialog.cpp">
<DynamicSource Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">input</DynamicSource>
<QtMocFileName Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">%(Filename).moc</QtMocFileName>
<DynamicSource Condition="'$(Configuration)|$(Platform)'=='Release|x64'">input</DynamicSource>
<QtMocFileName Condition="'$(Configuration)|$(Platform)'=='Release|x64'">%(Filename).moc</QtMocFileName>
</ClCompile>
<ClCompile Include="Preprocessor.cpp" />
<ClCompile Include="Offsets.cpp" />
<ClCompile Include="Pointer.cpp" />
Expand Down
6 changes: 3 additions & 3 deletions BattlefieldChat/BattlefieldChat.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@
<ClCompile Include="Preprocessor.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="InputDialog.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Translator.cpp">
<Filter>Source Files\translator</Filter>
</ClCompile>
<ClCompile Include="InputDialog.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="cpp.hint" />
Expand Down
39 changes: 26 additions & 13 deletions BattlefieldChat/InputDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@
#include "Utils.h"
#include "GlobalVariables.h"

InputDialog::InputDialog(QWidget *parent)
: QWidget(parent)
{
InputDialog::InputDialog(QWidget* parent)
: QWidget(parent) {
ui.setupUi(this);
this->setFocusPolicy(Qt::StrongFocus);
QObject::connect(ui.editContent, SIGNAL(returnPressed()), this, SLOT(enterPressed()));
QObject::connect(ui.editContent, SIGNAL(editingFinished()), this, SLOT(lostFocus()));
QObject::connect(this, SIGNAL(enterProcessed()), this, SLOT(onEnterProcessed()));
QObject::connect(ui.editContent, SIGNAL(textChanged(QString)), this, SLOT(textTyped(QString)));

qRegisterMetaType<Qt::WindowFlags>("Qt::WindowFlags");
QObject::connect(this, SIGNAL(callInitializeWindow(Qt::WindowFlags, QSize, QSize, QPoint)),
QObject::connect(this, SIGNAL(callInitializeWindow(Qt::WindowFlags, QSize, QSize, QPoint)),
this, SLOT(handleInitializeWindow(Qt::WindowFlags, QSize, QSize, QPoint)));
ui.editContent->installEventFilter(this);
}

InputDialog::~InputDialog()
{
InputDialog::~InputDialog() {
}

void InputDialog::keyPressEvent(QKeyEvent* e) {
Expand All @@ -29,6 +28,7 @@ void InputDialog::keyPressEvent(QKeyEvent* e) {
}

QString InputDialog::showAndWaitForResult(HWND window, InputDisplayMode mode) {
showing = true;
QSize size = this->size();
QSize editSize = ui.editContent->size();
QPoint pos;
Expand Down Expand Up @@ -56,7 +56,7 @@ QString InputDialog::showAndWaitForResult(HWND window, InputDisplayMode mode) {
}
}

cancelled = true;
cancelled = false;

emit callInitializeWindow(style, size, editSize, pos);

Expand All @@ -70,6 +70,13 @@ QString InputDialog::showAndWaitForResult(HWND window, InputDisplayMode mode) {
return ui.editContent->text();
}

bool InputDialog::eventFilter(QObject* obj, QEvent* event) {
if (event->type() == QEvent::FocusOut)
lostFocus();

return false;
}

void InputDialog::escPressed() {
QString result = preprocessor->escPressed(ui.editContent->text());
if (result == Q_NULLPTR) {
Expand All @@ -80,29 +87,33 @@ void InputDialog::escPressed() {
}

void InputDialog::lostFocus() {
if (cancelled) {
if (!cancelled && !showing) {
cancelled = true;
waitCondition.wakeAll();
}
}

void InputDialog::enterPressed() {
ui.lblStatus->setText(QString::fromUtf8(u8"处理中"));
ui.editContent->setEnabled(false);
ui.editContent->setReadOnly(true);
QtConcurrent::run([=]() {
try {
QString result = preprocessor->enterPressed(ui.editContent->text());
ui.lblStatus->setText(QString::fromUtf8(u8"完成"));
if (result == Q_NULLPTR) {
cancelled = false;
waitCondition.wakeAll(); // send
} else {
ui.editContent->setText(result);
}
} catch (std::string error) {
ui.lblStatus->setText(QString::fromUtf8(error.c_str()));
}
ui.editContent->setEnabled(true);
});
emit enterProcessed();
});
}

void InputDialog::onEnterProcessed() {
ui.editContent->setReadOnly(false);
}

void InputDialog::handleInitializeWindow(Qt::WindowFlags style, QSize size, QSize editSize, QPoint pos) {
Expand All @@ -120,6 +131,8 @@ void InputDialog::handleInitializeWindow(Qt::WindowFlags style, QSize size, QSiz
this->activateWindow();
this->setFocus();
ui.editContent->setFocus();

showing = false;
}

void InputDialog::textTyped(const QString& text) {
Expand Down
5 changes: 5 additions & 0 deletions BattlefieldChat/InputDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,20 @@ class InputDialog : public QWidget
InputDialog(QWidget *parent = Q_NULLPTR);
~InputDialog();

bool eventFilter(QObject* obj, QEvent* event) override;

QString showAndWaitForResult(HWND window, InputDisplayMode mode);

signals:
void callInitializeWindow(Qt::WindowFlags windowType, QSize size, QSize editSize, QPoint pos);
void enterProcessed();

public slots:
void escPressed();
void lostFocus();
void enterPressed();
void textTyped(const QString& text);
void onEnterProcessed();

void handleInitializeWindow(Qt::WindowFlags windowType, QSize size, QSize editSize, QPoint pos);

Expand All @@ -41,4 +45,5 @@ public slots:
QMutex mutex;
QWaitCondition waitCondition;
bool cancelled = false;
bool showing = false;
};

0 comments on commit 71755a7

Please sign in to comment.