Skip to content

Commit de26f98

Browse files
fix(editstackedwidget): enhance text input handling and max length management
- Refactored the text input handling in the NameTextEdit class to improve cursor positioning and character limit enforcement. - Introduced a new method `setMaxLength` to dynamically set the maximum length for text input based on file suffixes. - Updated the renameFile method to reserve space for file suffixes, ensuring that the main file name is editable without truncating the suffix. These changes enhance user experience by providing better control over text input and ensuring that file names are handled correctly. bug: https://pms.uniontech.com/bug-view-321657.html
1 parent 86f0a0a commit de26f98

File tree

2 files changed

+42
-36
lines changed

2 files changed

+42
-36
lines changed

src/plugins/common/dfmplugin-propertydialog/views/editstackedwidget.cpp

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -77,21 +77,30 @@ void NameTextEdit::slotTextChanged()
7777
Q_UNUSED(blocker)
7878

7979
QString text = this->toPlainText();
80-
const QString old_text = text;
81-
82-
int text_length = text.length();
83-
84-
text.remove('/');
85-
text.remove(QChar(0));
86-
87-
int cursor_pos = this->textCursor().position() - text_length + text.length();
80+
if (text.isEmpty()) {
81+
return;
82+
}
8883

89-
while (textLength(text) > NAME_MAX) {
90-
text.chop(1);
84+
// 参考视图区域的逻辑:先预处理文件名,再处理长度
85+
QString dstText = FileUtils::preprocessingFileName(text);
86+
87+
bool hasInvalidChar = text != dstText;
88+
if (hasInvalidChar) {
89+
showAlertMessage(tr("%1 are not allowed").arg("|/\\*:\"'?<>"));
9190
}
9291

93-
if (text.size() != old_text.size()) {
94-
this->setPlainText(text);
92+
int cursor_pos = this->textCursor().position();
93+
cursor_pos += dstText.length() - text.length();
94+
95+
int limit = (maxLengthVal > 0 ? maxLengthVal : NAME_MAX);
96+
FileUtils::processLength(dstText, cursor_pos, limit, useCharCount, dstText, cursor_pos);
97+
98+
if (text != dstText) {
99+
this->setPlainText(dstText);
100+
QTextCursor cursor = this->textCursor();
101+
cursor.setPosition(cursor_pos);
102+
this->setTextCursor(cursor);
103+
this->setAlignment(Qt::AlignHCenter);
95104
}
96105

97106
QTextCursor cursor = this->textCursor();
@@ -104,33 +113,19 @@ void NameTextEdit::slotTextChanged()
104113
format.setLineHeight(kTextLineHeight, QTextBlockFormat::FixedHeight);
105114
cursor.setBlockFormat(format);
106115
} while (cursor.movePosition(QTextCursor::NextBlock));
107-
116+
108117
cursor.setPosition(cursor_pos);
109118

110119
this->setTextCursor(cursor);
111120
this->setAlignment(Qt::AlignHCenter);
112121

113122
if (this->isReadOnly())
114123
this->setFixedHeight(static_cast<int>(this->document()->size().height()));
124+
}
115125

116-
QString dstText = FileUtils::preprocessingFileName(text);
117-
118-
bool hasInvalidChar = text != dstText;
119-
120-
int endPos = this->textCursor().position() + (dstText.length() - text.length());
121-
122-
FileUtils::processLength(dstText, endPos, NAME_MAX, true, dstText, endPos);
123-
if (text != dstText) {
124-
this->setPlainText(dstText);
125-
QTextCursor cursor = this->textCursor();
126-
cursor.setPosition(endPos);
127-
this->setTextCursor(cursor);
128-
this->setAlignment(Qt::AlignHCenter);
129-
}
130-
131-
if (hasInvalidChar) {
132-
showAlertMessage(tr("%1 are not allowed").arg("|/\\*:\"'?<>"));
133-
}
126+
void NameTextEdit::setMaxLength(int len)
127+
{
128+
maxLengthVal = qMax(0, len);
134129
}
135130

136131
void NameTextEdit::showAlertMessage(const QString &text, int duration)
@@ -170,11 +165,14 @@ void NameTextEdit::keyPressEvent(QKeyEvent *event)
170165
if (event->key() == Qt::Key_Escape) {
171166
setIsCanceled(true);
172167
emit editFinished();
168+
event->accept();
173169
return;
174170
}
175171
if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) {
176172
setIsCanceled(false);
177173
emit editFinished();
174+
event->accept();
175+
return;
178176
}
179177
QTextEdit::keyPressEvent(event);
180178
}
@@ -295,20 +293,26 @@ void EditStackedWidget::renameFile()
295293
{
296294
QFileInfo info(fileUrl.path());
297295

298-
if (FileUtils::supportLongName(fileUrl))
296+
// 设置最大长度:为后缀预留长度(与图标/列表一致)
297+
const QString suffix = info.suffix();
298+
const bool isLongNameFs = FileUtils::supportLongName(fileUrl);
299+
int reserve = suffix.isEmpty() ? 0 : (isLongNameFs ? (suffix.length() + 1) : (suffix.toLocal8Bit().size() + 1));
300+
int maxLen = NAME_MAX - reserve;
301+
if (isLongNameFs)
299302
fileNameEdit->setCharCountLimit();
303+
fileNameEdit->setMaxLength(maxLen);
300304

301305
fileNameEdit->setPlainText(info.fileName());
302306
this->setCurrentIndex(0);
303307
fileNameEdit->setFixedHeight(textShowFrame->height());
304308
fileNameEdit->setFocus();
305309

306-
fileNameEdit->selectAll();
307-
int endPos = fileNameEdit->toPlainText().length();
308-
310+
// 仅选择主文件名部分,保留后缀不被覆盖
311+
const QString fullName = info.fileName();
312+
const int baseLen = info.completeBaseName().length();
309313
QTextCursor cursor = fileNameEdit->textCursor();
310314
cursor.setPosition(0);
311-
cursor.setPosition(endPos, QTextCursor::KeepAnchor);
315+
cursor.setPosition(qMin(baseLen, fullName.length()), QTextCursor::KeepAnchor);
312316
fileNameEdit->setTextCursor(cursor);
313317
}
314318

src/plugins/common/dfmplugin-propertydialog/views/editstackedwidget.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class NameTextEdit : public DTK_WIDGET_NAMESPACE::DTextEdit
2828
bool isCanceled() const;
2929
void setIsCanceled(bool isCancele);
3030
inline void setCharCountLimit() { useCharCount = true; }
31+
void setMaxLength(int len);
3132

3233
signals:
3334
void editFinished();
@@ -50,6 +51,7 @@ public slots:
5051

5152
bool isCancel = false;
5253
bool useCharCount = false;
54+
int maxLengthVal = 0;
5355

5456
DTK_WIDGET_NAMESPACE::DArrowRectangle *tooltip { nullptr };
5557
};

0 commit comments

Comments
 (0)