Skip to content

Commit

Permalink
Qt: Add a setType function to DisplayFilterEdit for use with ui files
Browse files Browse the repository at this point in the history
Qt ui files only call a constructor with a QWidget first parameter
and can't pass other parameters or call other constructors.
Add a function to set the type after initial construction that can
be called in the constructors of other widgets after setupUi.

This makes it possible to use other DisplayFilterEditTypes in ui files.
  • Loading branch information
johnthacker committed Nov 23, 2024
1 parent ade0caa commit fd6004f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 28 deletions.
69 changes: 44 additions & 25 deletions ui/qt/widgets/display_filter_edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ static const QString fld_abbrev_chars_ = ":-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXY

DisplayFilterEdit::DisplayFilterEdit(QWidget *parent, DisplayFilterEditType type) :
SyntaxLineEdit(parent),
type_(type),
type_(DisplayFilterToEnter),
save_action_(NULL),
remove_action_(NULL),
actions_(Q_NULLPTR),
bookmark_button_(NULL),
clear_button_(NULL),
apply_button_(NULL),
bookmark_button_(nullptr),
clear_button_(nullptr),
apply_button_(nullptr),
leftAlignActions_(false),
last_applied_(QString()),
filter_word_preamble_(QString()),
Expand All @@ -88,20 +88,42 @@ DisplayFilterEdit::DisplayFilterEdit(QWidget *parent, DisplayFilterEditType type
setCompleter(new QCompleter(completion_model_, this));
setCompletionTokenChars(fld_abbrev_chars_);

QString buttonStyle = QStringLiteral(
"QToolButton {"
" border: none;"
" background: transparent;" // Disables platform style on Windows.
" padding: 0 0 0 0;"
"}"
"QToolButton::menu-indicator {"
" image: none;"
"}"
);

leftAlignActions_ = recent.gui_geometry_leftalign_actions;

if (type_ == DisplayFilterToApply) {
setDefaultPlaceholderText();
setType(type);

connect(this, &DisplayFilterEdit::textChanged, this,
static_cast<void (DisplayFilterEdit::*)(const QString &)>(&DisplayFilterEdit::checkFilter));

connect(mainApp, &MainApplication::appInitialized, this, &DisplayFilterEdit::updateBookmarkMenu);
connect(mainApp, &MainApplication::displayFilterListChanged, this, &DisplayFilterEdit::updateBookmarkMenu);
connect(mainApp, &MainApplication::preferencesChanged, this, [=](){ checkFilter(); });

connect(mainApp, &MainApplication::appInitialized, this, &DisplayFilterEdit::connectToMainWindow);
}

void DisplayFilterEdit::setType(DisplayFilterEditType type)
{
if (type_ == type) {
return;
}

type_ = type;

if (type == DisplayFilterToApply && apply_button_ == nullptr) {

QString buttonStyle = QStringLiteral(
"QToolButton {"
" border: none;"
" background: transparent;" // Disables platform style on Windows.
" padding: 0 0 0 0;"
"}"
"QToolButton::menu-indicator {"
" image: none;"
"}"
);

bookmark_button_ = new StockIconToolButton(this, "x-display-filter-bookmark");
bookmark_button_->setMenu(new QMenu(bookmark_button_));
bookmark_button_->setPopupMode(QToolButton::InstantPopup);
Expand All @@ -126,18 +148,15 @@ DisplayFilterEdit::DisplayFilterEdit(QWidget *parent, DisplayFilterEditType type
connect(clear_button_, &StockIconToolButton::clicked, this, &DisplayFilterEdit::clearFilter);
connect(apply_button_, &StockIconToolButton::clicked, this, &DisplayFilterEdit::applyDisplayFilter);
connect(this, &DisplayFilterEdit::returnPressed, this, &DisplayFilterEdit::applyDisplayFilter);
} else if (apply_button_) {
disconnect(this, &DisplayFilterEdit::returnPressed, this, &DisplayFilterEdit::applyDisplayFilter);

delete bookmark_button_;
delete clear_button_;
delete apply_button_;
}

setDefaultPlaceholderText();

connect(this, &DisplayFilterEdit::textChanged, this,
static_cast<void (DisplayFilterEdit::*)(const QString &)>(&DisplayFilterEdit::checkFilter));

connect(mainApp, &MainApplication::appInitialized, this, &DisplayFilterEdit::updateBookmarkMenu);
connect(mainApp, &MainApplication::displayFilterListChanged, this, &DisplayFilterEdit::updateBookmarkMenu);
connect(mainApp, &MainApplication::preferencesChanged, this, [=](){ checkFilter(); });

connect(mainApp, &MainApplication::appInitialized, this, &DisplayFilterEdit::connectToMainWindow);
}

void DisplayFilterEdit::connectToMainWindow()
Expand Down
8 changes: 5 additions & 3 deletions ui/qt/widgets/display_filter_edit.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include <QDrag>
#include <QActionGroup>
#include <QPointer>

#include <ui/qt/widgets/syntax_line_edit.h>

Expand All @@ -29,6 +30,7 @@ class DisplayFilterEdit : public SyntaxLineEdit
Q_OBJECT
public:
explicit DisplayFilterEdit(QWidget *parent = 0, DisplayFilterEditType type = DisplayFilterToEnter);
void setType(DisplayFilterEditType type);

protected:
void paintEvent(QPaintEvent *evt);
Expand Down Expand Up @@ -72,9 +74,9 @@ private slots:
QAction *save_action_;
QAction *remove_action_;
QActionGroup * actions_;
StockIconToolButton *bookmark_button_;
StockIconToolButton *clear_button_;
StockIconToolButton *apply_button_;
QPointer<StockIconToolButton> bookmark_button_;
QPointer<StockIconToolButton> clear_button_;
QPointer<StockIconToolButton> apply_button_;
bool leftAlignActions_;
QString last_applied_;
QString filter_word_preamble_;
Expand Down

0 comments on commit fd6004f

Please sign in to comment.