Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion src/widgets/dpasswordedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
#include <DSuggestButton>

#include <QDebug>
#include <QTimer>

Check warning on line 14 in src/widgets/dpasswordedit.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QTimer> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 14 in src/widgets/dpasswordedit.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QTimer> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QPushButton>

Check warning on line 15 in src/widgets/dpasswordedit.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QPushButton> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 15 in src/widgets/dpasswordedit.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QPushButton> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QLineEdit>

Check warning on line 16 in src/widgets/dpasswordedit.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QLineEdit> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 16 in src/widgets/dpasswordedit.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QLineEdit> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QMenu>

Check warning on line 17 in src/widgets/dpasswordedit.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QMenu> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 17 in src/widgets/dpasswordedit.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QMenu> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QKeyEvent>

Check warning on line 18 in src/widgets/dpasswordedit.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QKeyEvent> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 18 in src/widgets/dpasswordedit.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QKeyEvent> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QKeySequence>

Check warning on line 19 in src/widgets/dpasswordedit.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QKeySequence> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 19 in src/widgets/dpasswordedit.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QKeySequence> not found. Please note: Cppcheck does not need standard library headers to get proper results.


DWIDGET_BEGIN_NAMESPACE
Expand Down Expand Up @@ -42,6 +44,9 @@
D_D(DPasswordEdit);

d->init();

setCopyEnabled(false);
setCutEnabled(false);
}

/*!
Expand Down Expand Up @@ -135,13 +140,49 @@
}
}
#endif

if (watcher == lineEdit() && event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if (keyEvent->matches(QKeySequence::Undo)
|| keyEvent->matches(QKeySequence::Redo)) {
return true;
}
}

if (watcher == lineEdit() && event->type() == QEvent::ContextMenu) {
QMenu *menu = lineEdit()->createStandardContextMenu();
if (!menu)
return DLineEdit::eventFilter(watcher, event);

for (QAction *action : menu->actions()) {
const auto &text = action->text();
if (text.startsWith(QLineEdit::tr("&Undo"))
|| text.startsWith(QLineEdit::tr("&Redo"))) {
action->setEnabled(false);
}
if (text.startsWith(QLineEdit::tr("Cu&t")) && !cutEnabled()) {
action->setEnabled(false);
}
if (text.startsWith(QLineEdit::tr("&Copy")) && !copyEnabled()) {
action->setEnabled(false);
}
if (text.startsWith(QLineEdit::tr("&Paste")) && !pasteEnabled()) {
action->setEnabled(false);
}
}

menu->popup(static_cast<QContextMenuEvent *>(event)->globalPos());
event->accept();
lineEdit()->setFocus();
return true;
}

return DLineEdit::eventFilter(watcher, event);
}

DPasswordEditPrivate::DPasswordEditPrivate(DPasswordEdit *q)

Check warning on line 183 in src/widgets/dpasswordedit.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Member variable 'DPasswordEditPrivate::togglePasswordVisibleButton' is not initialized in the constructor. Member variables of native types, pointers, or references are left uninitialized when the class is instantiated. That may cause bugs or undefined behavior.

Check warning on line 183 in src/widgets/dpasswordedit.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Member variable 'DPasswordEditPrivate::togglePasswordVisibleButton' is not initialized in the constructor. Member variables of native types, pointers, or references are left uninitialized when the class is instantiated. That may cause bugs or undefined behavior.
: DLineEditPrivate(q)
{

}

void DPasswordEditPrivate::init()
Expand Down
17 changes: 16 additions & 1 deletion tests/testcases/widgets/ut_dpasswordedit.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2021 - 2022 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2021 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later

Expand Down Expand Up @@ -38,6 +38,8 @@ TEST_F(ut_DPasswordEdit, setEchoMode)

TEST_F(ut_DPasswordEdit, setCopyEnabled)
{
ASSERT_FALSE(target->copyEnabled());

target->setCopyEnabled(true);
ASSERT_TRUE(target->copyEnabled());

Expand All @@ -47,10 +49,23 @@ TEST_F(ut_DPasswordEdit, setCopyEnabled)

TEST_F(ut_DPasswordEdit, setCutEnabled)
{
ASSERT_FALSE(target->cutEnabled());

target->setCutEnabled(true);
ASSERT_TRUE(target->cutEnabled());

target->setCutEnabled(false);
ASSERT_FALSE(target->cutEnabled());
}

TEST_F(ut_DPasswordEdit, setPasteEnabled)
{
ASSERT_TRUE(target->pasteEnabled());

target->setPasteEnabled(true);
ASSERT_TRUE(target->pasteEnabled());

target->setPasteEnabled(false);
ASSERT_FALSE(target->pasteEnabled());
}

Loading