fix(compressor): fix cursor jumping to end when editing in rename dialog#420
Conversation
Reviewer's guide (collapsed on small PRs)Reviewer's GuideRefactors the RenameDialog input handling to use a validator-based filename filter instead of post-processing text changes, preventing the cursor from jumping to the end while still enforcing disallowed-character rules and keeping the OK button enablement logic intact. Sequence diagram for RenameDialog validator-based input handlingsequenceDiagram
actor User
participant RenameDialog
participant DLineEdit
participant QValidator
participant OkButton
User->>DLineEdit: type character
DLineEdit->>QValidator: validate(input)
alt [invalid input]
QValidator-->>DLineEdit: reject change
else [valid input]
QValidator-->>DLineEdit: accept change
DLineEdit-->>RenameDialog: textChanged
RenameDialog->>DLineEdit: text
RenameDialog->>OkButton: setEnabled(bool)
end
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- The Qt5 and Qt6 regular expressions diverge slightly (
\svs explicit space/tab and different ordering of escape sequences); consider defining a single shared pattern (e.g., as a QString or raw string literal) and using it for both validators to ensure identical behavior and easier maintenance. - The inline regex strings are fairly dense and heavily escaped; using raw string literals and/or a short comment describing the allowed character set (especially for the first vs subsequent characters) would make the validation rules easier to understand and update later.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The Qt5 and Qt6 regular expressions diverge slightly (`\s` vs explicit space/tab and different ordering of escape sequences); consider defining a single shared pattern (e.g., as a QString or raw string literal) and using it for both validators to ensure identical behavior and easier maintenance.
- The inline regex strings are fairly dense and heavily escaped; using raw string literals and/or a short comment describing the allowed character set (especially for the first vs subsequent characters) would make the validation rules easier to understand and update later.
## Individual Comments
### Comment 1
<location path="src/source/dialog/popupdialog.cpp" line_range="20-27" />
<code_context>
#include <QDebug>
#include <DPasswordEdit>
#include <QFileInfo>
+#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
+#include <QRegularExpressionValidator>
+#else
</code_context>
<issue_to_address>
**suggestion (bug_risk):** Align the Qt5/Qt6 regex semantics around whitespace handling to avoid subtle behavioral differences.
In Qt6, the `QRegularExpression` pattern excludes all `\s` (including Unicode whitespace) in the first character, while the Qt5 `QRegExp` version only excludes space, `\t`, and `\r\n`. As a result, filenames starting with less-common whitespace are rejected in Qt6 but allowed in Qt5. If this divergence isn’t intentional, consider making the patterns equivalent (either both use an explicit minimal whitespace set or both approximate `\s`) so Qt5/Qt6 builds behave consistently.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
- Replace textChanged+setText filtering with QRegularExpressionValidator
to reject illegal characters at input time in RenameDialog
- Use QValidator to prevent / \ : * " ' ? < > | etc. from being
entered, instead of removing them after textChanged fires
- Avoid setText() in textChanged slot which resets cursor position
在重命名对话框中使用 QValidator 在输入阶段直接拒绝非法字符,
替代在 textChanged 信号中使用 setText 移除字符的方式,
解决输入时光标会跳转到末尾的问题。
PMS: BUG-363723
Influence: 重命名对话框输入时光标不再跳转到末尾
a5fe26e to
1a3cc08
Compare
deepin pr auto review你好!我是CodeGeex,很高兴为你审查这段代码。 这段代码的主要改进是将输入校验从被动的 不过,在语法逻辑、代码质量和安全性方面,还有一些需要优化和注意的地方。以下是详细的审查意见: 一、 语法与逻辑问题
二、 代码性能
三、 代码安全与健壮性
四、 代码质量与可读性
改进后的代码建议综合以上意见,我为你修改了代码,你可以参考: // 引入头文件部分(如果 Qt5 版本高于 5.4,建议直接使用 QRegularExpressionValidator,去掉分支)
#if QT_VERSION >= QT_VERSION_CHECK(5, 4, 0)
#include <QRegularExpressionValidator>
#else
#include <QRegExpValidator>
#endif
// ... 中间代码不变 ...
int RenameDialog::showDialog(const QString &strReName, const QString &strAlias,
bool isDirectory, bool isRepeat)
{
qDebug() << "Showing RenameDialog for:" << strReName << "isDirectory:" << isDirectory << "isRepeat:" << isRepeat;
if(m_lineEdit == NULL) {
m_lineEdit = new DLineEdit(this);
// 限制最大输入长度,防止超长字符串导致正则回溯性能问题或越界
m_lineEdit->lineEdit()->setMaxLength(g_nTextLength);
// 规则:第一位不允许空格和特殊字符,后续位不允许特殊字符(中间空格允许)
const QString validationPattern = "^[^\\s/\\\\:*\"'?<>|\\r\\n\\t][^/\\\\:*\"'?<>|\\r\\n\\t]*$";
#if QT_VERSION >= QT_VERSION_CHECK(5, 4, 0)
QRegularExpression re(validationPattern);
// 将 validator 的 parent 设为真正挂载的 widget 或 this
m_lineEdit->lineEdit()->setValidator(new QRegularExpressionValidator(re, m_lineEdit->lineEdit()));
#else
QRegExp re(validationPattern);
m_lineEdit->lineEdit()->setValidator(new QRegExpValidator(re, m_lineEdit->lineEdit()));
#endif
// 输入字符格式与文管保持一致
connect(m_lineEdit, &DLineEdit::textChanged, this, [this](){
// 使用引用避免 QString 拷贝开销
const QString &text = m_lineEdit->text();
if(m_nOkBtnIndex >= 0) {
// 移除 isNull() 判断,使用 trimmed() 防止纯空格通过校验
if(text.trimmed().isEmpty()){
getButton(m_nOkBtnIndex)->setEnabled(false);
} else {
getButton(m_nOkBtnIndex)->setEnabled(true);
}
}
});
// ... 其他逻辑
}
// ... 其他逻辑
}总结:你原本的改进思路非常正确,用 Validator 替换 setText 是解决光标跳动和输入法问题的标准做法。通过补充上述的边界条件(长度限制、纯空格拦截)和性能优化(引用传递),这段代码将变得更加健壮和安全。 |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: dengzhongyuan365-dev, lzwind The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
/forcemerge |
Replace textChanged+setText filtering with QRegularExpressionValidator to reject illegal characters at input time in RenameDialog
在重命名对话框中使用 QValidator 在输入阶段直接拒绝非法字符,
替代在 textChanged 信号中使用 setText 移除字符的方式,
解决输入时光标会跳转到末尾的问题。
PMS: BUG-363723
Influence: 重命名对话框输入时光标不再跳转到末尾
Summary by Sourcery
Prevent cursor position from jumping to the end when editing text in the rename dialog by validating input at entry time.
Bug Fixes:
Enhancements: