~ubuntu-branches/ubuntu/trusty/tomahawk/trusty-proposed

« back to all changes in this revision

Viewing changes to src/libtomahawk/thirdparty/Qocoa/qsearchfield_mac.mm

  • Committer: Package Import Robot
  • Author(s): Harald Sitter
  • Date: 2013-03-07 21:50:13 UTC
  • Revision ID: package-import@ubuntu.com-20130307215013-6gdjkdds7i9uenvs
Tags: upstream-0.6.0+dfsg
ImportĀ upstreamĀ versionĀ 0.6.0+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (C) 2011 by Mike McQuaid
 
3
 
 
4
Permission is hereby granted, free of charge, to any person obtaining a copy
 
5
of this software and associated documentation files (the "Software"), to deal
 
6
in the Software without restriction, including without limitation the rights
 
7
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
8
copies of the Software, and to permit persons to whom the Software is
 
9
furnished to do so, subject to the following conditions:
 
10
 
 
11
The above copyright notice and this permission notice shall be included in
 
12
all copies or substantial portions of the Software.
 
13
 
 
14
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
15
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
16
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
17
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
18
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
19
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
20
THE SOFTWARE.
 
21
*/
 
22
 
 
23
#include "qsearchfield.h"
 
24
#include "moc_qsearchfield.cpp"
 
25
 
 
26
#include "qocoa_mac.h"
 
27
#include "utils/Logger.h"
 
28
 
 
29
#import "Foundation/NSAutoreleasePool.h"
 
30
#import "Foundation/NSNotification.h"
 
31
#import "AppKit/NSSearchField.h"
 
32
 
 
33
#include <QApplication>
 
34
#include <QClipboard>
 
35
 
 
36
class QSearchFieldPrivate : public QObject
 
37
{
 
38
public:
 
39
    QSearchFieldPrivate(QSearchField *qSearchField, NSSearchField *nsSearchField)
 
40
        : QObject(qSearchField), qSearchField(qSearchField), nsSearchField(nsSearchField) {}
 
41
 
 
42
    void textDidChange(const QString &text)
 
43
    {
 
44
        if (qSearchField)
 
45
            emit qSearchField->textChanged(text);
 
46
    }
 
47
 
 
48
    void textDidEndEditing()
 
49
    {
 
50
        if (qSearchField)
 
51
            emit qSearchField->editingFinished();
 
52
    }
 
53
 
 
54
    void returnPressed()
 
55
    {
 
56
        if (qSearchField)
 
57
            emit qSearchField->returnPressed();
 
58
    }
 
59
 
 
60
    QPointer<QSearchField> qSearchField;
 
61
    NSSearchField *nsSearchField;
 
62
};
 
63
 
 
64
@interface QSearchFieldDelegate : NSObject<NSTextFieldDelegate>
 
65
{
 
66
@public
 
67
    QPointer<QSearchFieldPrivate> pimpl;
 
68
}
 
69
-(void)controlTextDidChange:(NSNotification*)notification;
 
70
-(void)controlTextDidEndEditing:(NSNotification*)notification;
 
71
@end
 
72
 
 
73
@implementation QSearchFieldDelegate
 
74
-(void)controlTextDidChange:(NSNotification*)notification {
 
75
    Q_ASSERT(pimpl);
 
76
    if (pimpl)
 
77
        pimpl->textDidChange(toQString([[notification object] stringValue]));
 
78
}
 
79
 
 
80
-(void)controlTextDidEndEditing:(NSNotification*)notification {
 
81
    // No Q_ASSERT here as it is called on destruction.
 
82
    if (pimpl)
 
83
        pimpl->textDidEndEditing();
 
84
 
 
85
    if ([[[notification userInfo] objectForKey:@"NSTextMovement"] intValue] == NSReturnTextMovement)
 
86
        pimpl->returnPressed();
 
87
}
 
88
@end
 
89
 
 
90
namespace {
 
91
 
 
92
static const unsigned short kKeycodeA = 0;
 
93
static const unsigned short kKeycodeX = 7;
 
94
static const unsigned short kKeycodeC = 8;
 
95
static const unsigned short kKeycodeV = 9;
 
96
 
 
97
}  // namespace
 
98
 
 
99
@interface QocoaSearchField : NSSearchField
 
100
-(BOOL)performKeyEquivalent:(NSEvent*)event;
 
101
@end
 
102
 
 
103
@implementation QocoaSearchField
 
104
-(BOOL)performKeyEquivalent:(NSEvent*)event {
 
105
    if ([event type] == NSKeyDown && [event modifierFlags] & NSCommandKeyMask)
 
106
    {
 
107
        const unsigned short keyCode = [event keyCode];
 
108
/*        if (keyCode == kKeycodeA)  // Cmd+a
 
109
        {
 
110
            [self performSelector:@selector(selectText:)];
 
111
            return YES;
 
112
        }
 
113
        else*/ if (keyCode == kKeycodeC)  // Cmd+c
 
114
        {
 
115
            QClipboard* clipboard = QApplication::clipboard();
 
116
            clipboard->setText(toQString([self stringValue]));
 
117
            return YES;
 
118
        }
 
119
        else if (keyCode == kKeycodeV)  // Cmd+v
 
120
        {
 
121
            QClipboard* clipboard = QApplication::clipboard();
 
122
            [self setStringValue:fromQString(clipboard->text())];
 
123
            return YES;
 
124
        }
 
125
        else if (keyCode == kKeycodeX)  // Cmd+x
 
126
        {
 
127
            QClipboard* clipboard = QApplication::clipboard();
 
128
            clipboard->setText(toQString([self stringValue]));
 
129
            [self setStringValue:@""];
 
130
            return YES;
 
131
        }
 
132
    }
 
133
 
 
134
    return NO;
 
135
}
 
136
@end
 
137
 
 
138
QSearchField::QSearchField(QWidget *parent) : QWidget(parent)
 
139
{
 
140
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 
141
 
 
142
    NSSearchField *search = [[QocoaSearchField alloc] init];
 
143
 
 
144
    QSearchFieldDelegate *delegate = [[QSearchFieldDelegate alloc] init];
 
145
    pimpl = delegate->pimpl = new QSearchFieldPrivate(this, search);
 
146
    [search setDelegate:delegate];
 
147
 
 
148
    setupLayout(search, this);
 
149
 
 
150
    setFixedHeight(24);
 
151
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
 
152
 
 
153
    [search release];
 
154
 
 
155
    [pool drain];
 
156
}
 
157
 
 
158
void QSearchField::setText(const QString &text)
 
159
{
 
160
    Q_ASSERT(pimpl);
 
161
    if (!pimpl)
 
162
        return;
 
163
 
 
164
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 
165
    [pimpl->nsSearchField setStringValue:fromQString(text)];
 
166
    [pool drain];
 
167
}
 
168
 
 
169
void QSearchField::setPlaceholderText(const QString &text)
 
170
{
 
171
    Q_ASSERT(pimpl);
 
172
    if (!pimpl)
 
173
        return;
 
174
 
 
175
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 
176
    [[pimpl->nsSearchField cell] setPlaceholderString:fromQString(text)];
 
177
    [pool drain];
 
178
}
 
179
 
 
180
QString QSearchField::placeholderText() const {
 
181
    Q_ASSERT(pimpl);
 
182
    NSString* placeholder = [[pimpl->nsSearchField cell] placeholderString];
 
183
    return toQString(placeholder);
 
184
}
 
185
 
 
186
void QSearchField::setFocus(Qt::FocusReason reason)
 
187
{
 
188
    Q_ASSERT(pimpl);
 
189
    if (!pimpl)
 
190
        return;
 
191
 
 
192
    if ([pimpl->nsSearchField acceptsFirstResponder])
 
193
        [[pimpl->nsSearchField window] makeFirstResponder: pimpl->nsSearchField];
 
194
}
 
195
 
 
196
void QSearchField::setFocus()
 
197
{
 
198
    setFocus(Qt::OtherFocusReason);
 
199
}
 
200
 
 
201
void QSearchField::clear()
 
202
{
 
203
    Q_ASSERT(pimpl);
 
204
    if (!pimpl)
 
205
        return;
 
206
 
 
207
    [pimpl->nsSearchField setStringValue:@""];
 
208
    emit textChanged(QString());
 
209
}
 
210
 
 
211
void QSearchField::selectAll()
 
212
{
 
213
    Q_ASSERT(pimpl);
 
214
    if (!pimpl)
 
215
        return;
 
216
 
 
217
    [pimpl->nsSearchField performSelector:@selector(selectText:)];
 
218
}
 
219
 
 
220
QString QSearchField::text() const
 
221
{
 
222
    Q_ASSERT(pimpl);
 
223
    if (!pimpl)
 
224
        return QString();
 
225
 
 
226
    return toQString([pimpl->nsSearchField stringValue]);
 
227
}
 
228
 
 
229
void QSearchField::resizeEvent(QResizeEvent *resizeEvent)
 
230
{
 
231
    QWidget::resizeEvent(resizeEvent);
 
232
}
 
233
 
 
234
bool QSearchField::eventFilter(QObject *o, QEvent *e)
 
235
{
 
236
    return QWidget::eventFilter(o, e);
 
237
}