Issue #134: Bugfixing

This commit is contained in:
Jakub Melka 2023-12-26 18:44:28 +01:00
parent a085a54b00
commit 7cb6b5bb3a
3 changed files with 86 additions and 65 deletions

View File

@ -22,6 +22,8 @@
#include <QStandardItemModel> #include <QStandardItemModel>
#include <QSortFilterProxyModel> #include <QSortFilterProxyModel>
#include <QCompleter> #include <QCompleter>
#include <QAbstractItemView>
#include <QKeyEvent>
namespace pdfviewer namespace pdfviewer
{ {
@ -30,37 +32,26 @@ PDFActionComboBox::PDFActionComboBox(QWidget* parent) :
BaseClass(parent), BaseClass(parent),
m_model(nullptr) m_model(nullptr)
{ {
setEditable(true); setPlaceholderText(tr("Find action..."));
lineEdit()->setPlaceholderText(tr("Find action...")); setClearButtonEnabled(true);
lineEdit()->setClearButtonEnabled(true);
setMinimumWidth(pdf::PDFWidgetUtils::scaleDPI_x(this, DEFAULT_WIDTH)); setMinimumWidth(pdf::PDFWidgetUtils::scaleDPI_x(this, DEFAULT_WIDTH));
m_model = new QStandardItemModel(this); m_model = new QStandardItemModel(this);
m_proxyModel = new QSortFilterProxyModel(this);
m_proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
m_proxyModel->setDynamicSortFilter(true);
m_proxyModel->setFilterKeyColumn(0);
m_proxyModel->setFilterRole(Qt::DisplayRole);
m_proxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
m_proxyModel->setSortLocaleAware(true);
m_proxyModel->setSortRole(Qt::DisplayRole);
m_proxyModel->setSourceModel(m_model);
QCompleter* completer = new QCompleter(m_model, this);
setFocusPolicy(Qt::StrongFocus); setFocusPolicy(Qt::StrongFocus);
setCompleter(nullptr); setCompleter(completer);
setInsertPolicy(QComboBox::NoInsert);
/*
completer()->setCompletionMode(QCompleter::PopupCompletion);
completer()->setCompletionColumn(-1);
completer()->setFilterMode(Qt::MatchContains | Qt::MatchWildcard);
completer()->setCaseSensitivity(Qt::CaseInsensitive);
completer()->setModelSorting(QCompleter::UnsortedModel);*/
connect(this, &PDFActionComboBox::activated, this, &PDFActionComboBox::onActionActivated); completer->setCompletionMode(QCompleter::PopupCompletion);
connect(this, &PDFActionComboBox::editTextChanged, this, &PDFActionComboBox::onEditTextChanged, Qt::QueuedConnection); completer->setCompletionColumn(0);
completer->setCompletionRole(Qt::DisplayRole);
completer->setFilterMode(Qt::MatchContains);
completer->setCaseSensitivity(Qt::CaseInsensitive);
completer->setModelSorting(QCompleter::CaseInsensitivelySortedModel);
completer->setWrapAround(false);
completer->setMaxVisibleItems(20);
setModel(m_proxyModel); connect(this, &QLineEdit::editingFinished, this, &PDFActionComboBox::performExecuteAction, Qt::QueuedConnection);
} }
QSize PDFActionComboBox::sizeHint() const QSize PDFActionComboBox::sizeHint() const
@ -87,54 +78,85 @@ void PDFActionComboBox::addQuickFindAction(QAction* action)
} }
} }
bool PDFActionComboBox::event(QEvent* event)
{
if (event->type() == QEvent::ShortcutOverride)
{
QKeyEvent* keyEvent = dynamic_cast<QKeyEvent*>(event);
switch (keyEvent->key())
{
case Qt::Key_Down:
case Qt::Key_Up:
event->accept();
return true;
}
}
if (event->type() == QEvent::KeyPress)
{
QKeyEvent* keyEvent = dynamic_cast<QKeyEvent*>(event);
// Redirect up and down arrows to the completer
switch (keyEvent->key())
{
case Qt::Key_Down:
case Qt::Key_Up:
{
if (completer())
{
if (completer()->popup()->isVisible())
{
QCoreApplication::sendEvent(completer()->popup(), event);
}
else
{
completer()->complete();
}
}
return true;
}
case Qt::Key_Enter:
case Qt::Key_Return:
clearFocus();
return true;
default:
break;
}
}
return BaseClass::event(event);
}
void PDFActionComboBox::onActionChanged() void PDFActionComboBox::onActionChanged()
{ {
QAction* action = qobject_cast<QAction*>(sender()); QAction* action = qobject_cast<QAction*>(sender());
updateAction(action); updateAction(action);
} }
void PDFActionComboBox::onActionActivated(int index) void PDFActionComboBox::performExecuteAction()
{ {
QVariant actionData = itemData(index, Qt::UserRole); QString text = this->text();
QAction* action = actionData.value<QAction*>();
lineEdit()->clear(); QAction* action = nullptr;
setCurrentIndex(-1); for (QAction* currentAction : m_actions)
{
if (currentAction->text() == text)
{
action = currentAction;
}
}
if (action && action->isEnabled()) clear();
completer()->setCompletionPrefix(QString());
if (action)
{ {
action->trigger(); action->trigger();
} }
} }
void PDFActionComboBox::onEditTextChanged(const QString& text)
{
if (text.isEmpty())
{
m_proxyModel->setFilterFixedString(QString());
}
else if (text.contains(QChar('*')) || text.contains(QChar('?')))
{
m_proxyModel->setFilterWildcard(text);
}
else
{
m_proxyModel->setFilterFixedString(text);
}
if (!text.isEmpty())
{
showPopup();
}
else
{
hidePopup();
}
lineEdit()->setFocus();
lineEdit()->setCursorPosition(text.size());
}
void PDFActionComboBox::updateAction(QAction* action) void PDFActionComboBox::updateAction(QAction* action)
{ {
if (!action) if (!action)
@ -166,8 +188,6 @@ void PDFActionComboBox::updateAction(QAction* action)
m_model->removeRow(actionIndex); m_model->removeRow(actionIndex);
} }
} }
setCurrentIndex(-1);
} }
int PDFActionComboBox::findAction(QAction* action) int PDFActionComboBox::findAction(QAction* action)

View File

@ -22,6 +22,7 @@
#include <QAction> #include <QAction>
#include <QComboBox> #include <QComboBox>
#include <QLineEdit>
class QStandardItemModel; class QStandardItemModel;
class QSortFilterProxyModel; class QSortFilterProxyModel;
@ -29,18 +30,19 @@ class QSortFilterProxyModel;
namespace pdfviewer namespace pdfviewer
{ {
class PDFActionComboBox : public QComboBox class PDFActionComboBox : public QLineEdit
{ {
Q_OBJECT Q_OBJECT
private: private:
using BaseClass = QComboBox; using BaseClass = QLineEdit;
public: public:
PDFActionComboBox(QWidget* parent); PDFActionComboBox(QWidget* parent);
virtual QSize sizeHint() const override; virtual QSize sizeHint() const override;
virtual QSize minimumSizeHint() const override; virtual QSize minimumSizeHint() const override;
virtual bool event(QEvent* event) override;
void addQuickFindAction(QAction* action); void addQuickFindAction(QAction* action);
@ -48,15 +50,13 @@ private:
static constexpr int DEFAULT_WIDTH = 220; static constexpr int DEFAULT_WIDTH = 220;
void onActionChanged(); void onActionChanged();
void onActionActivated(int index); void performExecuteAction();
void onEditTextChanged(const QString& text);
void updateAction(QAction* action); void updateAction(QAction* action);
int findAction(QAction* action); int findAction(QAction* action);
std::vector<QAction*> m_actions; std::vector<QAction*> m_actions;
QStandardItemModel* m_model; QStandardItemModel* m_model;
QSortFilterProxyModel* m_proxyModel;
}; };
} // namespace pdfviewer } // namespace pdfviewer

View File

@ -1,4 +1,5 @@
CURRENT: CURRENT:
- Issue #134: Add search bar for actions
- Issue #129: Cannot compile with lcms 2.16 - Issue #129: Cannot compile with lcms 2.16
- Issue #128: Create list of markup annotations - Issue #128: Create list of markup annotations
- Issue #126: Remove <QtCore> include from main headers - Issue #126: Remove <QtCore> include from main headers