Rename SearchField
This commit is contained in:
@@ -1,49 +0,0 @@
|
||||
#ifndef QSEARCHFIELD_H
|
||||
#define QSEARCHFIELD_H
|
||||
|
||||
#include <QWidget>
|
||||
#include <QPointer>
|
||||
#include <QMenu>
|
||||
|
||||
class QSearchFieldPrivate;
|
||||
class QSearchField : public QWidget {
|
||||
Q_OBJECT
|
||||
|
||||
Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged USER true)
|
||||
Q_PROPERTY(QString placeholderText READ placeholderText WRITE setPlaceholderText)
|
||||
|
||||
public:
|
||||
explicit QSearchField(QWidget *parent);
|
||||
|
||||
void setIconSize(const int iconsize);
|
||||
|
||||
QString text() const;
|
||||
QString placeholderText() const;
|
||||
|
||||
#ifndef Q_OS_MACOS
|
||||
bool hasFocus() const;
|
||||
#endif
|
||||
void setFocus(Qt::FocusReason);
|
||||
|
||||
public Q_SLOTS:
|
||||
void setText(const QString &new_text);
|
||||
void setPlaceholderText(const QString &text);
|
||||
void clear();
|
||||
void selectAll();
|
||||
void setFocus();
|
||||
|
||||
Q_SIGNALS:
|
||||
void textChanged(const QString &text);
|
||||
void editingFinished();
|
||||
void returnPressed();
|
||||
|
||||
protected:
|
||||
void resizeEvent(QResizeEvent*) override;
|
||||
bool eventFilter(QObject*, QEvent*) override;
|
||||
|
||||
private:
|
||||
friend class QSearchFieldPrivate;
|
||||
QPointer<QSearchFieldPrivate> pimpl;
|
||||
};
|
||||
|
||||
#endif // QSEARCHFIELD_H
|
||||
74
src/widgets/searchfield.h
Normal file
74
src/widgets/searchfield.h
Normal file
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
Copyright (C) 2011 by Mike McQuaid
|
||||
Copyright (C) 2018-2024 by Jonas Kvinge <jonas@jkvinge.net>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef SEARCHFIELD_H
|
||||
#define SEARCHFIELD_H
|
||||
|
||||
#include <QWidget>
|
||||
#include <QPointer>
|
||||
#include <QMenu>
|
||||
|
||||
class QResizeEvent;
|
||||
class SearchFieldPrivate;
|
||||
|
||||
class SearchField : public QWidget {
|
||||
Q_OBJECT
|
||||
|
||||
Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged USER true)
|
||||
Q_PROPERTY(QString placeholderText READ placeholderText WRITE setPlaceholderText)
|
||||
|
||||
public:
|
||||
explicit SearchField(QWidget *parent);
|
||||
|
||||
void setIconSize(const int iconsize);
|
||||
|
||||
QString text() const;
|
||||
QString placeholderText() const;
|
||||
|
||||
#ifndef Q_OS_MACOS
|
||||
bool hasFocus() const;
|
||||
#endif
|
||||
void setFocus(Qt::FocusReason);
|
||||
|
||||
public Q_SLOTS:
|
||||
void setText(const QString &new_text);
|
||||
void setPlaceholderText(const QString &text);
|
||||
void clear();
|
||||
void selectAll();
|
||||
void setFocus();
|
||||
|
||||
Q_SIGNALS:
|
||||
void textChanged(const QString &text);
|
||||
void editingFinished();
|
||||
void returnPressed();
|
||||
|
||||
protected:
|
||||
void resizeEvent(QResizeEvent*) override;
|
||||
bool eventFilter(QObject *o, QEvent *e) override;
|
||||
|
||||
private:
|
||||
friend class SearchFieldPrivate;
|
||||
SearchFieldPrivate *pimpl;
|
||||
};
|
||||
|
||||
#endif // SEARCHFIELD_H
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
Copyright (C) 2011 by Mike McQuaid
|
||||
Copyright (C) 2018-2021 by Jonas Kvinge <jonas@jkvinge.net>
|
||||
Copyright (C) 2018-2024 by Jonas Kvinge <jonas@jkvinge.net>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
@@ -21,89 +21,111 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "qsearchfield.h"
|
||||
#include "qocoa_mac.h"
|
||||
|
||||
#import "Foundation/NSAutoreleasePool.h"
|
||||
#import "Foundation/NSNotification.h"
|
||||
#import "AppKit/NSSearchField.h"
|
||||
|
||||
#include <QApplication>
|
||||
#include <QObject>
|
||||
#include <QWidget>
|
||||
#include <QWindow>
|
||||
#include <QString>
|
||||
#include <QClipboard>
|
||||
#include <QBoxLayout>
|
||||
#include <QShowEvent>
|
||||
#include <QVBoxLayout>
|
||||
#include <QResizeEvent>
|
||||
#include <QKeyEvent>
|
||||
|
||||
class QSearchFieldPrivate : public QObject {
|
||||
public:
|
||||
QSearchFieldPrivate(QSearchField *qSearchField, NSSearchField *nsSearchField)
|
||||
#include "searchfield.h"
|
||||
#include "qocoa_mac.h"
|
||||
|
||||
class SearchFieldPrivate : public QObject {
|
||||
|
||||
public:
|
||||
|
||||
explicit SearchFieldPrivate(SearchField *qSearchField, NSSearchField *nsSearchField)
|
||||
: QObject(qSearchField), qSearchField(qSearchField), nsSearchField(nsSearchField) {}
|
||||
|
||||
void textDidChange(const QString &text) {
|
||||
if (qSearchField) emit qSearchField->textChanged(text);
|
||||
|
||||
if (qSearchField) {
|
||||
emit qSearchField->textChanged(text);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void textDidEndEditing() {
|
||||
if (qSearchField)
|
||||
emit qSearchField->editingFinished();
|
||||
|
||||
if (qSearchField) {
|
||||
Q_EMIT qSearchField->editingFinished();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void returnPressed() {
|
||||
|
||||
if (qSearchField) {
|
||||
emit qSearchField->returnPressed();
|
||||
Q_EMIT qSearchField->returnPressed();
|
||||
QKeyEvent *event = new QKeyEvent(QEvent::KeyPress, Qt::Key_Return, Qt::NoModifier);
|
||||
QApplication::postEvent(qSearchField, event);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void keyDownPressed() {
|
||||
|
||||
if (qSearchField) {
|
||||
QKeyEvent *event = new QKeyEvent(QEvent::KeyPress, Qt::Key_Down, Qt::NoModifier);
|
||||
QApplication::postEvent(qSearchField, event);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void keyUpPressed() {
|
||||
|
||||
if (qSearchField) {
|
||||
QKeyEvent *event = new QKeyEvent(QEvent::KeyPress, Qt::Key_Up, Qt::NoModifier);
|
||||
QApplication::postEvent(qSearchField, event);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
QPointer<QSearchField> qSearchField;
|
||||
QPointer<SearchField> qSearchField;
|
||||
NSSearchField *nsSearchField;
|
||||
|
||||
};
|
||||
|
||||
@interface QSearchFieldDelegate : NSObject<NSTextFieldDelegate> {
|
||||
@interface SearchFieldDelegate : NSObject<NSTextFieldDelegate> {
|
||||
@public
|
||||
QPointer<QSearchFieldPrivate> pimpl;
|
||||
QPointer<SearchFieldPrivate> pimpl;
|
||||
}
|
||||
-(void)controlTextDidChange:(NSNotification*)notification;
|
||||
-(void)controlTextDidEndEditing:(NSNotification*)notification;
|
||||
@end
|
||||
|
||||
@implementation QSearchFieldDelegate
|
||||
@implementation SearchFieldDelegate
|
||||
-(void)controlTextDidChange:(NSNotification*)notification {
|
||||
|
||||
Q_ASSERT(pimpl);
|
||||
|
||||
if (pimpl) pimpl->textDidChange(toQString([[notification object] stringValue]));
|
||||
|
||||
}
|
||||
|
||||
-(void)controlTextDidEndEditing:(NSNotification*)notification {
|
||||
|
||||
Q_UNUSED(notification);
|
||||
// No Q_ASSERT here as it is called on destruction.
|
||||
if (!pimpl) return;
|
||||
pimpl->textDidEndEditing();
|
||||
if ([[[notification userInfo] objectForKey:@"NSTextMovement"] intValue] == NSReturnTextMovement)
|
||||
pimpl->returnPressed();
|
||||
|
||||
}
|
||||
|
||||
-(BOOL)control: (NSControl*)control textView: (NSTextView*)textView doCommandBySelector: (SEL)commandSelector {
|
||||
|
||||
Q_UNUSED(control);
|
||||
Q_UNUSED(textView);
|
||||
Q_ASSERT(pimpl);
|
||||
|
||||
if (!pimpl) return NO;
|
||||
if (commandSelector == @selector(moveDown:)) {
|
||||
pimpl->keyDownPressed();
|
||||
@@ -113,7 +135,9 @@ public:
|
||||
pimpl->keyUpPressed();
|
||||
return YES;
|
||||
}
|
||||
|
||||
return NO;
|
||||
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -124,6 +148,7 @@ public:
|
||||
|
||||
@implementation QocoaSearchField
|
||||
-(BOOL)performKeyEquivalent:(NSEvent*)event {
|
||||
|
||||
// First, check if we have the focus.
|
||||
// If no, it probably means this event isn't for us.
|
||||
NSResponder *firstResponder = [[NSApp keyWindow] firstResponder];
|
||||
@@ -151,15 +176,16 @@ public:
|
||||
}
|
||||
|
||||
return NO;
|
||||
|
||||
}
|
||||
@end
|
||||
|
||||
QSearchField::QSearchField(QWidget *parent) : QWidget(parent) {
|
||||
SearchField::SearchField(QWidget *parent) : QWidget(parent) {
|
||||
|
||||
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
||||
NSSearchField *search = [[QocoaSearchField alloc] init];
|
||||
QSearchFieldDelegate *delegate = [[QSearchFieldDelegate alloc] init];
|
||||
pimpl = delegate->pimpl = new QSearchFieldPrivate(this, search);
|
||||
SearchFieldDelegate *delegate = [[SearchFieldDelegate alloc] init];
|
||||
pimpl = delegate->pimpl = new SearchFieldPrivate(this, search);
|
||||
[search setDelegate:static_cast<id<NSSearchFieldDelegate>>(delegate)];
|
||||
|
||||
setAttribute(Qt::WA_NativeWindow);
|
||||
@@ -174,12 +200,14 @@ QSearchField::QSearchField(QWidget *parent) : QWidget(parent) {
|
||||
|
||||
}
|
||||
|
||||
void QSearchField::setIconSize(const int iconsize) {
|
||||
void SearchField::setIconSize(const int iconsize) {
|
||||
Q_UNUSED(iconsize);
|
||||
}
|
||||
|
||||
void QSearchField::setText(const QString &text) {
|
||||
void SearchField::setText(const QString &text) {
|
||||
|
||||
Q_ASSERT(pimpl);
|
||||
|
||||
if (!pimpl) return;
|
||||
|
||||
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
||||
@@ -188,51 +216,72 @@ void QSearchField::setText(const QString &text) {
|
||||
[pimpl->nsSearchField selectText:pimpl->nsSearchField];
|
||||
[[pimpl->nsSearchField currentEditor] setSelectedRange:NSMakeRange([[pimpl->nsSearchField stringValue] length], 0)];
|
||||
}
|
||||
|
||||
[pool drain];
|
||||
|
||||
}
|
||||
|
||||
void QSearchField::setPlaceholderText(const QString &text) {
|
||||
void SearchField::setPlaceholderText(const QString &text) {
|
||||
|
||||
Q_ASSERT(pimpl);
|
||||
|
||||
if (!pimpl) return;
|
||||
|
||||
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
||||
[[pimpl->nsSearchField cell] setPlaceholderString:fromQString(text)];
|
||||
|
||||
[pool drain];
|
||||
|
||||
}
|
||||
|
||||
void QSearchField::clear() {
|
||||
void SearchField::clear() {
|
||||
|
||||
Q_ASSERT(pimpl);
|
||||
|
||||
if (!pimpl) return;
|
||||
|
||||
[pimpl->nsSearchField setStringValue:@""];
|
||||
emit textChanged(QString());
|
||||
|
||||
Q_EMIT textChanged(QString());
|
||||
|
||||
}
|
||||
|
||||
void QSearchField::selectAll() {
|
||||
void SearchField::selectAll() {
|
||||
|
||||
Q_ASSERT(pimpl);
|
||||
|
||||
if (!pimpl) return;
|
||||
[pimpl->nsSearchField performSelector:@selector(selectText:)];
|
||||
|
||||
}
|
||||
|
||||
QString QSearchField::text() const {
|
||||
QString SearchField::text() const {
|
||||
|
||||
Q_ASSERT(pimpl);
|
||||
|
||||
if (!pimpl) return QString();
|
||||
return toQString([pimpl->nsSearchField stringValue]);
|
||||
|
||||
}
|
||||
|
||||
QString QSearchField::placeholderText() const {
|
||||
QString SearchField::placeholderText() const {
|
||||
|
||||
Q_ASSERT(pimpl);
|
||||
|
||||
return toQString([[pimpl->nsSearchField cell] placeholderString]);
|
||||
|
||||
}
|
||||
|
||||
void QSearchField::setFocus(Qt::FocusReason) {}
|
||||
void SearchField::setFocus(Qt::FocusReason) {}
|
||||
|
||||
void QSearchField::setFocus() {
|
||||
void SearchField::setFocus() {
|
||||
setFocus(Qt::OtherFocusReason);
|
||||
}
|
||||
|
||||
void QSearchField::resizeEvent(QResizeEvent *resizeEvent) {
|
||||
void SearchField::resizeEvent(QResizeEvent *resizeEvent) {
|
||||
QWidget::resizeEvent(resizeEvent);
|
||||
}
|
||||
|
||||
bool QSearchField::eventFilter(QObject *o, QEvent *e) {
|
||||
bool SearchField::eventFilter(QObject *o, QEvent *e) {
|
||||
return QWidget::eventFilter(o, e);
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
Copyright (C) 2011 by Mike McQuaid
|
||||
Copyright (C) 2018-2021 by Jonas Kvinge <jonas@jkvinge.net>
|
||||
Copyright (C) 2018-2024 by Jonas Kvinge <jonas@jkvinge.net>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
@@ -21,55 +21,30 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "qsearchfield.h"
|
||||
#include "searchfield.h"
|
||||
#include "searchfield_qt_private.h"
|
||||
|
||||
#include <QtGlobal>
|
||||
#include <QObject>
|
||||
#include <QWidget>
|
||||
#include <QApplication>
|
||||
#include <QString>
|
||||
#include <QIcon>
|
||||
#include <QPointer>
|
||||
#include <QLineEdit>
|
||||
#include <QPushButton>
|
||||
#include <QVBoxLayout>
|
||||
#include <QStyle>
|
||||
#include <QSize>
|
||||
#include <QBoxLayout>
|
||||
#include <QEvent>
|
||||
#include <QResizeEvent>
|
||||
|
||||
#include "core/iconloader.h"
|
||||
|
||||
class QSearchFieldPrivate : public QObject { // clazy:exclude=missing-qobject-macro
|
||||
|
||||
public:
|
||||
QSearchFieldPrivate(QSearchField *searchField, QLineEdit *lineedit, QPushButton *clearbutton)
|
||||
: QObject(searchField), lineedit_(lineedit), clearbutton_(clearbutton) {}
|
||||
|
||||
int lineEditFrameWidth() const {
|
||||
return lineedit_->style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
|
||||
}
|
||||
|
||||
int clearButtonPaddedWidth() const {
|
||||
return clearbutton_->width() + lineEditFrameWidth() * 2;
|
||||
}
|
||||
|
||||
int clearButtonPaddedHeight() const {
|
||||
return clearbutton_->height() + lineEditFrameWidth() * 2;
|
||||
}
|
||||
|
||||
QPointer<QLineEdit> lineedit_;
|
||||
QPointer<QPushButton> clearbutton_;
|
||||
|
||||
};
|
||||
|
||||
QSearchField::QSearchField(QWidget *parent) : QWidget(parent) {
|
||||
SearchField::SearchField(QWidget *parent) : QWidget(parent) {
|
||||
|
||||
QLineEdit *lineEdit = new QLineEdit(this);
|
||||
QObject::connect(lineEdit, &QLineEdit::textChanged, this, &QSearchField::textChanged);
|
||||
QObject::connect(lineEdit, &QLineEdit::editingFinished, this, &QSearchField::editingFinished);
|
||||
QObject::connect(lineEdit, &QLineEdit::returnPressed, this, &QSearchField::returnPressed);
|
||||
QObject::connect(lineEdit, &QLineEdit::textChanged, this, &QSearchField::setText);
|
||||
QObject::connect(lineEdit, &QLineEdit::textChanged, this, &SearchField::textChanged);
|
||||
QObject::connect(lineEdit, &QLineEdit::editingFinished, this, &SearchField::editingFinished);
|
||||
QObject::connect(lineEdit, &QLineEdit::returnPressed, this, &SearchField::returnPressed);
|
||||
QObject::connect(lineEdit, &QLineEdit::textChanged, this, &SearchField::setText);
|
||||
|
||||
QPushButton *clearbutton = new QPushButton(this);
|
||||
QIcon clearIcon(IconLoader::Load(QStringLiteral("edit-clear-locationbar-ltr")));
|
||||
@@ -79,9 +54,9 @@ QSearchField::QSearchField(QWidget *parent) : QWidget(parent) {
|
||||
clearbutton->setStyleSheet(QStringLiteral("border: none; padding: 2px;"));
|
||||
clearbutton->resize(clearbutton->sizeHint());
|
||||
|
||||
QObject::connect(clearbutton, &QPushButton::clicked, this, &QSearchField::clear);
|
||||
QObject::connect(clearbutton, &QPushButton::clicked, this, &SearchField::clear);
|
||||
|
||||
pimpl = new QSearchFieldPrivate(this, lineEdit, clearbutton);
|
||||
pimpl = new SearchFieldPrivate(this, lineEdit, clearbutton);
|
||||
|
||||
const int frame_width = lineEdit->style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
|
||||
|
||||
@@ -98,7 +73,7 @@ QSearchField::QSearchField(QWidget *parent) : QWidget(parent) {
|
||||
|
||||
}
|
||||
|
||||
void QSearchField::setIconSize(const int iconsize) {
|
||||
void SearchField::setIconSize(const int iconsize) {
|
||||
|
||||
pimpl->clearbutton_->setIconSize(QSize(iconsize, iconsize));
|
||||
pimpl->clearbutton_->resize(pimpl->clearbutton_->sizeHint());
|
||||
@@ -111,7 +86,7 @@ void QSearchField::setIconSize(const int iconsize) {
|
||||
|
||||
}
|
||||
|
||||
void QSearchField::setText(const QString &new_text) {
|
||||
void SearchField::setText(const QString &new_text) {
|
||||
|
||||
Q_ASSERT(pimpl && pimpl->clearbutton_ && pimpl->lineedit_);
|
||||
if (!(pimpl && pimpl->clearbutton_ && pimpl->lineedit_)) return;
|
||||
@@ -119,7 +94,7 @@ void QSearchField::setText(const QString &new_text) {
|
||||
|
||||
}
|
||||
|
||||
void QSearchField::setPlaceholderText(const QString &text) {
|
||||
void SearchField::setPlaceholderText(const QString &text) {
|
||||
|
||||
Q_ASSERT(pimpl && pimpl->lineedit_);
|
||||
if (!(pimpl && pimpl->lineedit_)) return;
|
||||
@@ -127,25 +102,25 @@ void QSearchField::setPlaceholderText(const QString &text) {
|
||||
|
||||
}
|
||||
|
||||
QString QSearchField::placeholderText() const {
|
||||
QString SearchField::placeholderText() const {
|
||||
return pimpl->lineedit_->placeholderText();
|
||||
}
|
||||
|
||||
bool QSearchField::hasFocus() const {
|
||||
bool SearchField::hasFocus() const {
|
||||
Q_ASSERT(pimpl && pimpl->lineedit_);
|
||||
return pimpl && pimpl->lineedit_ && pimpl->lineedit_->hasFocus();
|
||||
}
|
||||
|
||||
void QSearchField::setFocus(Qt::FocusReason reason) {
|
||||
void SearchField::setFocus(Qt::FocusReason reason) {
|
||||
Q_ASSERT(pimpl && pimpl->lineedit_);
|
||||
if (pimpl && pimpl->lineedit_) pimpl->lineedit_->setFocus(reason);
|
||||
}
|
||||
|
||||
void QSearchField::setFocus() {
|
||||
void SearchField::setFocus() {
|
||||
setFocus(Qt::OtherFocusReason);
|
||||
}
|
||||
|
||||
void QSearchField::clear() {
|
||||
void SearchField::clear() {
|
||||
|
||||
Q_ASSERT(pimpl && pimpl->lineedit_);
|
||||
|
||||
@@ -154,7 +129,7 @@ void QSearchField::clear() {
|
||||
|
||||
}
|
||||
|
||||
void QSearchField::selectAll() {
|
||||
void SearchField::selectAll() {
|
||||
|
||||
Q_ASSERT(pimpl && pimpl->lineedit_);
|
||||
|
||||
@@ -163,7 +138,7 @@ void QSearchField::selectAll() {
|
||||
|
||||
}
|
||||
|
||||
QString QSearchField::text() const {
|
||||
QString SearchField::text() const {
|
||||
|
||||
Q_ASSERT(pimpl && pimpl->lineedit_);
|
||||
|
||||
@@ -172,7 +147,7 @@ QString QSearchField::text() const {
|
||||
|
||||
}
|
||||
|
||||
void QSearchField::resizeEvent(QResizeEvent *resizeEvent) {
|
||||
void SearchField::resizeEvent(QResizeEvent *resizeEvent) {
|
||||
|
||||
Q_ASSERT(pimpl && pimpl->clearbutton_ && pimpl->lineedit_);
|
||||
if (!(pimpl && pimpl->clearbutton_ && pimpl->lineedit_)) return;
|
||||
@@ -184,10 +159,10 @@ void QSearchField::resizeEvent(QResizeEvent *resizeEvent) {
|
||||
|
||||
}
|
||||
|
||||
bool QSearchField::eventFilter(QObject *o, QEvent *e) {
|
||||
bool SearchField::eventFilter(QObject *o, QEvent *e) {
|
||||
|
||||
if (pimpl && pimpl->lineedit_ && o == pimpl->lineedit_) {
|
||||
// Forward some lineEdit events to QSearchField (only those we need for
|
||||
// Forward some lineEdit events to SearchField (only those we need for
|
||||
// now, but some might be added later if needed)
|
||||
switch (e->type()) {
|
||||
case QEvent::FocusIn:
|
||||
@@ -198,6 +173,7 @@ bool QSearchField::eventFilter(QObject *o, QEvent *e) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return QWidget::eventFilter(o, e);
|
||||
|
||||
}
|
||||
44
src/widgets/searchfield_qt_private.cpp
Normal file
44
src/widgets/searchfield_qt_private.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
Copyright (C) 2011 by Mike McQuaid
|
||||
Copyright (C) 2018-2024 by Jonas Kvinge <jonas@jkvinge.net>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <QLineEdit>
|
||||
#include <QPushButton>
|
||||
#include <QStyle>
|
||||
|
||||
#include "searchfield_qt_private.h"
|
||||
#include "searchfield.h"
|
||||
|
||||
SearchFieldPrivate::SearchFieldPrivate(SearchField *searchField, QLineEdit *lineedit, QPushButton *clearbutton)
|
||||
: QObject(searchField), lineedit_(lineedit), clearbutton_(clearbutton) {}
|
||||
|
||||
int SearchFieldPrivate::lineEditFrameWidth() const {
|
||||
return lineedit_->style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
|
||||
}
|
||||
|
||||
int SearchFieldPrivate::clearButtonPaddedWidth() const {
|
||||
return clearbutton_->width() + lineEditFrameWidth() * 2;
|
||||
}
|
||||
|
||||
int SearchFieldPrivate::clearButtonPaddedHeight() const {
|
||||
return clearbutton_->height() + lineEditFrameWidth() * 2;
|
||||
}
|
||||
48
src/widgets/searchfield_qt_private.h
Normal file
48
src/widgets/searchfield_qt_private.h
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
Copyright (C) 2011 by Mike McQuaid
|
||||
Copyright (C) 2018-2024 by Jonas Kvinge <jonas@jkvinge.net>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef SEARCHFIELD_QT_PRIVATE_H
|
||||
#define SEARCHFIELD_QT_PRIVATE_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QPointer>
|
||||
|
||||
class QLineEdit;
|
||||
class QPushButton;
|
||||
class SearchField;
|
||||
|
||||
class SearchFieldPrivate : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit SearchFieldPrivate(SearchField *searchField, QLineEdit *lineedit, QPushButton *clearbutton);
|
||||
|
||||
int lineEditFrameWidth() const;
|
||||
int clearButtonPaddedWidth() const;
|
||||
int clearButtonPaddedHeight() const;
|
||||
|
||||
QPointer<QLineEdit> lineedit_;
|
||||
QPointer<QPushButton> clearbutton_;
|
||||
};
|
||||
|
||||
#endif // QSEARCHFIELD_QT_PRIVATE_H
|
||||
Reference in New Issue
Block a user