From ba871065e0f40e9197fa4ee0ffe76530bb6fca11 Mon Sep 17 00:00:00 2001 From: Jake Petroules Date: Thu, 1 Feb 2018 10:32:07 -0800 Subject: Clean up our Objective-C usage MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Move ivars into @implementation - Use instancetype where applicable - Use dot notation for property access - Use subscript operator for dictionaries and arrays - Format selectors consistently - Use proper style for init methods - Use generics instead of void pointers where possible - Use "range for" loops instead of indexing - Replace or replace IBAction/IBOutlet with void Change-Id: I1667812a51d4dfe44ae80fe337cb1f4bc9699d92 Reviewed-by: Jake Petroules Reviewed-by: Tor Arne Vestbø --- .../qmaccocoaviewcontainer/TestMouseMovedNSView.h | 7 +---- .../qmaccocoaviewcontainer/TestMouseMovedNSView.m | 25 ++++++++++------- tests/manual/cocoa/qmaccocoaviewcontainer/main.mm | 2 +- tests/manual/cocoa/qt_on_cocoa/main.mm | 32 ++++++++++------------ 4 files changed, 31 insertions(+), 35 deletions(-) (limited to 'tests/manual/cocoa') diff --git a/tests/manual/cocoa/qmaccocoaviewcontainer/TestMouseMovedNSView.h b/tests/manual/cocoa/qmaccocoaviewcontainer/TestMouseMovedNSView.h index be716aa582..4a145a729f 100644 --- a/tests/manual/cocoa/qmaccocoaviewcontainer/TestMouseMovedNSView.h +++ b/tests/manual/cocoa/qmaccocoaviewcontainer/TestMouseMovedNSView.h @@ -28,10 +28,5 @@ #import -@interface TestMouseMovedNSView : NSView { - NSPoint mouseMovedPoint_; - BOOL wasAcceptingMouseEvents_; - NSTrackingRectTag trackingRect_; - NSTrackingArea* trackingArea_; -} +@interface TestMouseMovedNSView : NSView @end diff --git a/tests/manual/cocoa/qmaccocoaviewcontainer/TestMouseMovedNSView.m b/tests/manual/cocoa/qmaccocoaviewcontainer/TestMouseMovedNSView.m index 65b42dbb2f..20a3fcc513 100644 --- a/tests/manual/cocoa/qmaccocoaviewcontainer/TestMouseMovedNSView.m +++ b/tests/manual/cocoa/qmaccocoaviewcontainer/TestMouseMovedNSView.m @@ -28,9 +28,14 @@ #import "TestMouseMovedNSView.h" -@implementation TestMouseMovedNSView +@implementation TestMouseMovedNSView { + NSPoint mouseMovedPoint_; + BOOL wasAcceptingMouseEvents_; + NSTrackingRectTag trackingRect_; + NSTrackingArea* trackingArea_; +} -- (id)initWithFrame:(NSRect)frame +- (instancetype)initWithFrame:(NSRect)frame { self = [super initWithFrame:frame]; if (self) @@ -40,13 +45,13 @@ - (void)viewDidMoveToWindow { - trackingArea_ = [[NSTrackingArea alloc] initWithRect:[self bounds] options: (NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways) owner:self userInfo:nil]; + trackingArea_ = [[NSTrackingArea alloc] initWithRect:self.bounds options:(NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways) owner:self userInfo:nil]; [self addTrackingArea:trackingArea_]; } - (void)viewWillMoveToWindow:(NSWindow *)newWindow { - if ([self window] && trackingArea_) + if (self.window && trackingArea_) [self removeTrackingArea:trackingArea_]; } @@ -54,7 +59,7 @@ { [super updateTrackingAreas]; [self removeTrackingArea: trackingArea_]; - trackingArea_ = [[NSTrackingArea alloc] initWithRect:[self bounds] options: (NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways) owner:self userInfo:nil]; + trackingArea_ = [[NSTrackingArea alloc] initWithRect:self.bounds options:(NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways) owner:self userInfo:nil]; [self addTrackingArea:trackingArea_]; } @@ -64,20 +69,20 @@ - (void)mouseEntered:(NSEvent *)theEvent { wasAcceptingMouseEvents_ = [[self window] acceptsMouseMovedEvents]; - [[self window] setAcceptsMouseMovedEvents:YES]; - [[self window] makeFirstResponder:self]; + [self.window setAcceptsMouseMovedEvents:YES]; + [self.window makeFirstResponder:self]; } - (void)mouseExited:(NSEvent *)theEvent { - [[self window] setAcceptsMouseMovedEvents:wasAcceptingMouseEvents_]; + [self.window setAcceptsMouseMovedEvents:wasAcceptingMouseEvents_]; [self setNeedsDisplay:YES]; [self displayIfNeeded]; } -(void)mouseMoved:(NSEvent *)pTheEvent { - mouseMovedPoint_ = [self convertPoint:[pTheEvent locationInWindow] fromView:nil]; + mouseMovedPoint_ = [self convertPoint:pTheEvent.locationInWindow fromView:nil]; [self setNeedsDisplay:YES]; [self displayIfNeeded]; } @@ -88,7 +93,7 @@ NSRectFill(dirtyRect); NSGraphicsContext *nsGraphicsContext = [NSGraphicsContext currentContext]; - CGContextRef cgContextRef = (CGContextRef) [nsGraphicsContext graphicsPort]; + CGContextRef cgContextRef = nsGraphicsContext.CGContext; CGContextSetRGBStrokeColor(cgContextRef, 0, 0, 0, .5); CGContextSetLineWidth(cgContextRef, 1.0); diff --git a/tests/manual/cocoa/qmaccocoaviewcontainer/main.mm b/tests/manual/cocoa/qmaccocoaviewcontainer/main.mm index 9cf06391ca..8b05b64a1e 100644 --- a/tests/manual/cocoa/qmaccocoaviewcontainer/main.mm +++ b/tests/manual/cocoa/qmaccocoaviewcontainer/main.mm @@ -83,7 +83,7 @@ int main(int argc, char **argv) w.resize(300, 300); w.setWindowTitle("QMacCocoaViewContainer"); NSRect r = NSMakeRect(0, 0, 100, 100); - NSView *view = [[TestMouseMovedNSView alloc] initWithFrame: r]; + NSView *view = [[TestMouseMovedNSView alloc] initWithFrame:r]; QMacCocoaViewContainer *nativeChild = new QMacCocoaViewContainer(view, &w); QVBoxLayout *vbox = new QVBoxLayout; vbox->addWidget(nativeChild); diff --git a/tests/manual/cocoa/qt_on_cocoa/main.mm b/tests/manual/cocoa/qt_on_cocoa/main.mm index 805ef0d7c2..e6218e48ac 100644 --- a/tests/manual/cocoa/qt_on_cocoa/main.mm +++ b/tests/manual/cocoa/qt_on_cocoa/main.mm @@ -50,31 +50,30 @@ } @end -@interface AppDelegate : NSObject { +@interface AppDelegate : NSObject +@end + +@implementation AppDelegate { QGuiApplication *m_app; QWindow *m_window; } -- (AppDelegate *) initWithArgc:(int)argc argv:(const char **)argv; -- (void) applicationWillFinishLaunching: (NSNotification *)notification; -- (void)applicationWillTerminate:(NSNotification *)notification; -@end - -@implementation AppDelegate -- (AppDelegate *) initWithArgc:(int)argc argv:(const char **)argv +- (instancetype)initWithArgc:(int)argc argv:(const char **)argv { - m_app = new QGuiApplication(argc, const_cast(argv)); + if ((self = [self init])) { + m_app = new QGuiApplication(argc, const_cast(argv)); + } return self; } -- (void) applicationWillFinishLaunching: (NSNotification *)notification +- (void)applicationWillFinishLaunching:(NSNotification *)notification { Q_UNUSED(notification); // Create the NSWindow NSRect frame = NSMakeRect(500, 500, 500, 500); - NSWindow* window = [[NSWindow alloc] initWithContentRect:frame - styleMask:NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask + NSWindow *window = [[NSWindow alloc] initWithContentRect:frame + styleMask:NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask backing:NSBackingStoreBuffered defer:NO]; @@ -100,7 +99,7 @@ childWindow->setGeometry(50, 50, 100, 100); NSTextField *textField = [[NSTextField alloc] initWithFrame:NSMakeRect(10, 10, 80, 25)]; - [(NSView*)childWindow->winId() addSubview:textField]; + [reinterpret_cast(childWindow->winId()) addSubview:textField]; [contentView addSubview:reinterpret_cast(m_window->winId())]; @@ -125,10 +124,7 @@ int main(int argc, const char *argv[]) { // Create NSApplicaiton with delgate - NSApplication *app =[NSApplication sharedApplication]; + NSApplication *app = [NSApplication sharedApplication]; app.delegate = [[AppDelegate alloc] initWithArgc:argc argv:argv]; - return NSApplicationMain (argc, argv); + return NSApplicationMain(argc, argv); } - - - -- cgit v1.2.3