Skip to content
This repository has been archived by the owner on Aug 13, 2019. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeMcQuaid committed Aug 18, 2011
0 parents commit 4498031
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 0 deletions.
19 changes: 19 additions & 0 deletions LICENSE.txt
@@ -0,0 +1,19 @@
Copyright (C) 2011 by Mike McQuaid

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.
13 changes: 13 additions & 0 deletions README.md
@@ -0,0 +1,13 @@
Qocoa
=====

Qocoa is a collection of Qt wrappers for OSX's Cocoa widgets.

They have the following features/goals:

- Fallback to sensible Qt types on non-OSX platforms
- A shared header which exposes no implementation details
- Signal/slot based API

Qocoa is licensed under the MIT license.
The full license text is available in LICENSE.txt.
11 changes: 11 additions & 0 deletions gallery.cpp
@@ -0,0 +1,11 @@
#include "gallery.h"

#include <QVBoxLayout>

#include "qsearchfield.h"

Gallery::Gallery(QWidget *parent) : QWidget(parent)
{
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(new QSearchField(this));
}
14 changes: 14 additions & 0 deletions gallery.h
@@ -0,0 +1,14 @@
#ifndef GALLERY_H
#define GALLERY_H

#include <QWidget>

class Gallery : public QWidget
{
Q_OBJECT

public:
explicit Gallery(QWidget *parent = 0);
};

#endif // WIDGET_H
12 changes: 12 additions & 0 deletions main.cpp
@@ -0,0 +1,12 @@
#include <QtGui/QApplication>
#include "gallery.h"

int main(int argc, char *argv[])
{
QApplication application(argc, argv);

Gallery gallery;
gallery.show();

return application.exec();
}
9 changes: 9 additions & 0 deletions qocoa.pro
@@ -0,0 +1,9 @@
SOURCES += main.cpp\
gallery.cpp

HEADERS += gallery.h \
qsearchfield.h

!mac:SOURCES += qsearchfield.cpp
mac:SOURCES += qsearchfield_mac.mm
mac:LIBS += -framework Foundation -framework Appkit
28 changes: 28 additions & 0 deletions qsearchfield.cpp
@@ -0,0 +1,28 @@
#include "qsearchfield.h"

#include <QLineEdit>
#include <QVBoxLayout>

class QSearchFieldPrivate
{
public:
QSearchFieldPrivate(QLineEdit *lineEdit) : lineEdit(lineEdit) {}
QLineEdit *lineEdit;
};

QSearchField::QSearchField(QWidget *parent) : QWidget(parent)
{
QLineEdit *lineEdit = new QLineEdit(this);
connect(lineEdit, SIGNAL(textChanged(QString)),
this, SIGNAL(textChanged(QString)));
pimpl = new QSearchFieldPrivate(lineEdit);

QVBoxLayout *layout = new QVBoxLayout(this);
layout->setMargin(0);
layout->addWidget(lineEdit);
}

void QSearchField::setText(const QString &text)
{
pimpl->lineEdit->setText(text);
}
24 changes: 24 additions & 0 deletions qsearchfield.h
@@ -0,0 +1,24 @@
#ifndef QSEARCHFIELD_H
#define QSEARCHFIELD_H

#include <QWidget>

class QSearchFieldPrivate;
class QSearchField : public QWidget
{
Q_OBJECT
public:
explicit QSearchField(QWidget *parent);

public slots:
void setText(const QString &text);

signals:
void textChanged(const QString &text);

private:
friend class QSearchFieldPrivate;
QSearchFieldPrivate *pimpl;
};

#endif // QSEARCHFIELD_H
79 changes: 79 additions & 0 deletions qsearchfield_mac.mm
@@ -0,0 +1,79 @@
#include "qsearchfield.h"

#include <QMacCocoaViewContainer>
#include <QVBoxLayout>

#import "Foundation/NSAutoreleasePool.h"
#import "Foundation/NSNotification.h"
#import "AppKit/NSTextField.h"
#import "AppKit/NSSearchField.h"

NSString* fromQString(const QString &string)
{
char* cString = string.toUtf8().data();
return [[NSString alloc] initWithUTF8String:cString];
}

QString toQString(NSString *string)
{
if (!string)
return QString();
return QString::fromUtf8([string UTF8String]);
}

class QSearchFieldPrivate
{
public:
QSearchFieldPrivate(QSearchField *qSearchField, NSSearchField *nsSearchField)
: qSearchField(qSearchField), nsSearchField(nsSearchField) {}

void textDidChange(const QString &text)
{
emit qSearchField->textChanged(text);
}
QSearchField *qSearchField;
NSSearchField *nsSearchField;
};

@interface QSearchFieldDelegate : NSObject<NSTextFieldDelegate>
{
@public
QSearchFieldPrivate* pimpl;
}
-(void)controlTextDidChange:(NSNotification*)notification;
@end

@implementation QSearchFieldDelegate
-(void)controlTextDidChange:(NSNotification*)notification {
pimpl->textDidChange(toQString([[notification object] stringValue]));
}
@end

QSearchField::QSearchField(QWidget *parent) : QWidget(parent)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

NSSearchField *search = [[NSSearchField alloc] init];
pimpl = new QSearchFieldPrivate(this, search);
[search sizeToFit];

QSearchFieldDelegate *delegate = [[QSearchFieldDelegate alloc] init];
delegate->pimpl = pimpl;
[search setDelegate:delegate];

QVBoxLayout *layout = new QVBoxLayout(this);
layout->setMargin(0);
layout->addWidget(new QMacCocoaViewContainer(search, this));

setFixedHeight(20);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

[search release];

[pool release];
}

void QSearchField::setText(const QString &text)
{
[pimpl->nsSearchField setStringValue:fromQString(text)];
}

0 comments on commit 4498031

Please sign in to comment.