Permalink
Browse files

Improve non-Mac QSearchField.

  • Loading branch information...
1 parent 7f6a4ad commit 59ad3d8fa8ca8d983ca8f1f85777a6f5e6621a4e @MikeMcQuaid committed Feb 13, 2012
Showing with 59 additions and 4 deletions.
  1. +1 −0 Qocoa.pro
  2. +2 −0 README.txt
  3. +3 −0 qsearchfield.h
  4. +7 −1 qsearchfield_mac.mm
  5. +41 −3 qsearchfield_nonmac.cpp
  6. BIN qsearchfield_nonmac.png
  7. +5 −0 qsearchfield_nonmac.qrc
View
@@ -13,6 +13,7 @@ mac {
QMAKE_CFLAGS += -mmacosx-version-min=10.6
} else {
SOURCES += qsearchfield_nonmac.cpp qbutton_nonmac.cpp qprogressindicatorspinning_nonmac.cpp
+ RESOURCES += qsearchfield_nonmac.qrc
}
contains(QT_CONFIG, embedded):debug {
View
@@ -13,4 +13,6 @@ They have the following features/goals:
Qocoa is licensed under the [MIT License](http://en.wikipedia.org/wiki/MIT_License).
The full license text is available in [LICENSE.txt](https://github.com/mikemcquaid/Qocoa/blob/master/LICENSE.txt).
+The non-Mac QSearchField clear icon is taken from the Oxygen Project and is licensed under the [Creative Commons Attribution-ShareAlike 3.0 License](http://creativecommons.org/licenses/by-sa/3.0/).
+
![Qocoa Gallery](https://github.com/mikemcquaid/Qocoa/raw/master/gallery.png)
View
@@ -21,6 +21,9 @@ public slots:
void textChanged(const QString &text);
void editingFinished();
+protected:
+ void resizeEvent(QResizeEvent*);
+
private:
friend class QSearchFieldPrivate;
QSearchFieldPrivate *pimpl;
View
@@ -81,7 +81,7 @@ -(void)controlTextDidEndEditing:(NSNotification*)notification {
setupLayout(search, this);
- setFixedHeight(20);
+ setFixedHeight(24);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
[search release];
@@ -106,9 +106,15 @@ -(void)controlTextDidEndEditing:(NSNotification*)notification {
void QSearchField::clear()
{
[pimpl->nsSearchField setStringValue:@""];
+ emit textChanged(QString());
}
QString QSearchField::text() const
{
return toQString([pimpl->nsSearchField stringValue]);
}
+
+void QSearchField::resizeEvent(QResizeEvent *resizeEvent)
+{
+ QWidget::resizeEvent(resizeEvent);
+}
View
@@ -24,12 +24,19 @@ THE SOFTWARE.
#include <QLineEdit>
#include <QVBoxLayout>
+#include <QToolButton>
+#include <QStyle>
+
+#include <QDir>
+#include <QDebug>
class QSearchFieldPrivate
{
public:
- QSearchFieldPrivate(QLineEdit *lineEdit) : lineEdit(lineEdit) {}
+ QSearchFieldPrivate(QLineEdit *lineEdit, QToolButton *clearButton)
+ : lineEdit(lineEdit), clearButton(clearButton) {}
QLineEdit *lineEdit;
+ QToolButton *clearButton;
};
QSearchField::QSearchField(QWidget *parent) : QWidget(parent)
@@ -39,7 +46,26 @@ QSearchField::QSearchField(QWidget *parent) : QWidget(parent)
this, SIGNAL(textChanged(QString)));
connect(lineEdit, SIGNAL(editingFinished()),
this, SIGNAL(editingFinished()));
- pimpl = new QSearchFieldPrivate(lineEdit);
+ connect(lineEdit, SIGNAL(textChanged(QString)),
+ this, SLOT(setText(QString)));
+
+ QToolButton *clearButton = new QToolButton(this);
+ QPixmap clearIcon(QString(":/Qocoa/qsearchfield_nonmac.png"));
+ clearButton->setIcon(QIcon(clearIcon));
+ clearButton->setIconSize(clearIcon.size());
+ clearButton->setFixedSize(clearIcon.size());
+ clearButton->setStyleSheet("border: none;");
+ clearButton->hide();
+ qDebug() << clearButton->size() << QDir(":/").entryList();
+ connect(clearButton, SIGNAL(clicked()), this, SLOT(clear()));
+
+ const int frameWidth = lineEdit->style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
+ lineEdit->setStyleSheet(QString("QLineEdit { padding-right: %1px; } ").arg(clearIcon.width() - frameWidth - 1));
+ const int width = qMax(lineEdit->minimumSizeHint().width(), clearButton->width() + frameWidth * 2);
+ const int height = qMax(lineEdit->minimumSizeHint().height(), clearButton->height() + frameWidth * 2);
+ lineEdit->setMinimumSize(width, height);
+
+ pimpl = new QSearchFieldPrivate(lineEdit, clearButton);
QVBoxLayout *layout = new QVBoxLayout(this);
layout->setMargin(0);
@@ -48,7 +74,10 @@ QSearchField::QSearchField(QWidget *parent) : QWidget(parent)
void QSearchField::setText(const QString &text)
{
- pimpl->lineEdit->setText(text);
+ pimpl->clearButton->setVisible(!text.isEmpty());
+
+ if (text != this->text())
+ pimpl->lineEdit->setText(text);
}
void QSearchField::setPlaceholderText(const QString &text)
@@ -65,3 +94,12 @@ QString QSearchField::text() const
{
return pimpl->lineEdit->text();
}
+
+void QSearchField::resizeEvent(QResizeEvent *resizeEvent)
+{
+ QWidget::resizeEvent(resizeEvent);
+ const int frameWidth = pimpl->lineEdit->style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
+ const int x = sizeHint().width() - pimpl->clearButton->width() - frameWidth;
+ const int y = sizeHint().height() - pimpl->clearButton->height()/2 - frameWidth*2;
+ pimpl->clearButton->move(x, y);
+}
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
@@ -0,0 +1,5 @@
+<RCC>
+ <qresource prefix="/Qocoa">
+ <file>qsearchfield_nonmac.png</file>
+ </qresource>
+</RCC>

0 comments on commit 59ad3d8

Please sign in to comment.