1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-03-23 01:09:42 +01:00

* Try to fix search field behaviour on OSX.

This commit is contained in:
Christian Muehlhaeuser 2013-01-18 08:47:15 +01:00
parent 8ef825aba7
commit c3fb3df840
3 changed files with 54 additions and 46 deletions

View File

@ -145,4 +145,9 @@ void QSearchField::resizeEvent(QResizeEvent* e)
}
bool QSearchField::eventFilter(QObject *o, QEvent *e)
{
return QWidget::eventFilter(o, e);
}
#include "qsearchfield.moc"

View File

@ -10,19 +10,16 @@ class QSearchFieldPrivate;
class DLLEXPORT QSearchField : public QWidget
{
Q_OBJECT
Q_PROPERTY(QString placeholderText READ placeholderText WRITE setPlaceholderText);
public:
explicit QSearchField(QWidget *parent);
QString text() const;
QString placeholderText() const;
void setFocus(Qt::FocusReason);
void setFocus(Qt::FocusReason reason);
public slots:
void setText(const QString &text);
void setPlaceholderText(const QString& text);
void setPlaceholderText(const QString &text);
void clear();
void selectAll();
void setFocus();
@ -34,10 +31,13 @@ signals:
protected:
void resizeEvent(QResizeEvent*);
bool eventFilter(QObject*, QEvent*);
private:
friend class QSearchFieldPrivate;
QPointer <QSearchFieldPrivate> pimpl;
Q_PROPERTY(QString placeholderText READ placeholderText WRITE setPlaceholderText);
};
#endif // QSEARCHFIELD_H

View File

@ -1,6 +1,5 @@
/*
Copyright (C) 2011 by Mike McQuaid
Copyright (C) 2011 by Leo Franchi
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@ -22,20 +21,16 @@ THE SOFTWARE.
*/
#include "qsearchfield.h"
#include "moc_qsearchfield.cpp"
#include "qocoa_mac.h"
#import <Cocoa/Cocoa.h>
#import "Foundation/NSAutoreleasePool.h"
#import "Foundation/NSNotification.h"
#import "AppKit/NSSearchField.h"
#include <QApplication>
#include <QClipboard>
#define KEYCODE_A 0
#define KEYCODE_X 7
#define KEYCODE_C 8
#define KEYCODE_V 9
class QSearchFieldPrivate : public QObject
{
public:
@ -81,7 +76,6 @@ public:
}
-(void)controlTextDidEndEditing:(NSNotification*)notification {
Q_UNUSED(notification);
// No Q_ASSERT here as it is called on destruction.
if (pimpl)
pimpl->textDidEndEditing();
@ -91,6 +85,15 @@ public:
}
@end
namespace {
static const unsigned short kKeycodeA = 0;
static const unsigned short kKeycodeX = 7;
static const unsigned short kKeycodeC = 8;
static const unsigned short kKeycodeV = 9;
} // namespace
@interface QocoaSearchField : NSSearchField
-(BOOL)performKeyEquivalent:(NSEvent*)event;
@end
@ -100,24 +103,24 @@ public:
if ([event type] == NSKeyDown && [event modifierFlags] & NSCommandKeyMask)
{
const unsigned short keyCode = [event keyCode];
if (keyCode == KEYCODE_A)
if (keyCode == kKeycodeA) // Cmd+a
{
[self performSelector:@selector(selectText:)];
return YES;
}
else if (keyCode == KEYCODE_C)
else if (keyCode == kKeycodeC) // Cmd+c
{
QClipboard* clipboard = QApplication::clipboard();
clipboard->setText(toQString([self stringValue]));
return YES;
}
else if (keyCode == KEYCODE_V)
else if (keyCode == kKeycodeV) // Cmd+v
{
QClipboard* clipboard = QApplication::clipboard();
[self setStringValue:fromQString(clipboard->text())];
return YES;
}
else if (keyCode == KEYCODE_X)
else if (keyCode == kKeycodeX) // Cmd+x
{
QClipboard* clipboard = QApplication::clipboard();
clipboard->setText(toQString([self stringValue]));
@ -145,10 +148,8 @@ QSearchField::QSearchField(QWidget *parent) : QWidget(parent)
setFixedHeight(24);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
layout()->setContentsMargins(2, 0, 2, 0);
setStyleSheet( "* { background: #DDE4EB; }" );
[search release];
[pool drain];
}
@ -163,7 +164,7 @@ void QSearchField::setText(const QString &text)
[pool drain];
}
void QSearchField::setPlaceholderText(const QString& text)
void QSearchField::setPlaceholderText(const QString &text)
{
Q_ASSERT(pimpl);
if (!pimpl)
@ -174,6 +175,27 @@ void QSearchField::setPlaceholderText(const QString& text)
[pool drain];
}
QString QSearchField::placeholderText() const {
Q_ASSERT(pimpl);
NSString* placeholder = [[pimpl->nsSearchField cell] placeholderString];
return toQString(placeholder);
}
void QSearchField::setFocus(Qt::FocusReason reason)
{
Q_ASSERT(pimpl);
if (!pimpl)
return;
if ([pimpl->nsSearchField acceptsFirstResponder])
[[pimpl->nsSearchField window] makeFirstResponder: pimpl->nsSearchField];
}
void QSearchField::setFocus()
{
setFocus(Qt::OtherFocusReason);
}
void QSearchField::clear()
{
Q_ASSERT(pimpl);
@ -202,31 +224,12 @@ QString QSearchField::text() const
return toQString([pimpl->nsSearchField stringValue]);
}
QString QSearchField::placeholderText() const
{
Q_ASSERT(pimpl);
if (!pimpl)
return QString();
return toQString([[pimpl->nsSearchField cell] placeholderString]);
}
void QSearchField::setFocus(Qt::FocusReason reason)
{
Q_ASSERT(pimpl);
if (!pimpl)
return;
if ([pimpl->nsSearchField acceptsFirstResponder])
[[pimpl->nsSearchField window] makeFirstResponder: pimpl->nsSearchField];
}
void QSearchField::setFocus()
{
setFocus(Qt::OtherFocusReason);
}
void QSearchField::resizeEvent(QResizeEvent *resizeEvent)
{
QWidget::resizeEvent(resizeEvent);
}
bool QSearchField::eventFilter(QObject *o, QEvent *e)
{
return QWidget::eventFilter(o, e);
}