레이블이 OSX인 게시물을 표시합니다. 모든 게시물 표시
레이블이 OSX인 게시물을 표시합니다. 모든 게시물 표시

2013년 11월 1일 금요일

File Association



파인더에서 파일을 더블클릭했을때 어플에서 처리하려면 아래와 같이 한다.

1. 파일의 속성을 선택하고 어플을 지정하여 열도록 한다.

2. application:openFile을 app delegate에 추가한다.
   openFile은 아래 순서로 호출된다.


   applicationWillFinishLaunching
   openFile
   applicationDidFinishLaunching

- (void)applicationWillFinishLaunching:(NSNotification *)notification
{
    isLaunched = FALSE;
}

- (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename
{
    if(isLaunched){  // 어플이 이미 떠 있을때

        // 윈도우가 close 되어 있으면 다시 띄운다.
        [[[self getMainViewwindow]makeKeyAndOrderFront:[[self getMainView ] window]];

        // 파일을 처리한다.
        [[self getMainViewopenFile:filename];
    }
    else{  // 어플이 최초로 실행될때
        // 파일을 처리한다.
        [self setStartPath:filename];

        // 아래 코드를 넣지 않으면 5초 있다가 뜬다.
        NSApplication * myapp = [NSApplication sharedApplication];
        [myapp activateIgnoringOtherApps:YES];
        [[self window] makeKeyAndOrderFront:[self window]];

        // 키보드 이벤트를 받을 윈도우에 포커스를 준다. 
        [dicomImage.window makeFirstResponder:dicomImage];
    }
        
    return YES;

}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    ...    
    isLaunched = TRUE;
}

- (BOOL)getIsLaunched
{
    return isLaunched;
}


2013년 7월 21일 일요일

메뉴 UI 체크하기


메뉴를 처리하는 뷰에서 아래와 같이 재정의해서 사용한다.
onImagePointer는 메소드 이름이다.

-(BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)item
{
    SEL action = [item action];

    MainView * mainView = [[NSApp delegate] getMainView];
    KMyDicomNSImage * dicomImage = [mainView getMyImage];
    KDicomImage * pImage = [dicomImage getImage];
    
    if(action == @selector(onImagePointer:))
    {
        if(pImage->m_nLMMode == POINTER)
            [(id)item setState:NSOnState];
        else
            [(id)item setState:NSOffState];
    }
    if(action == @selector(onImageZoom:))
    {
        if(pImage->m_nLMMode == ZOOM)
            [(id)item setState:NSOnState];
        else
            [(id)item setState:NSOffState];
    }
    else if(action == @selector(onImagePan:))
    {
        if(pImage->m_nLMMode == PAN)
            [(id)item setState:NSOnState];
        else
            [(id)item setState:NSOffState];
    }
    return YES;
}

마우스 이벤트 처리

1. 왼쪽 마우스 버튼 처리

- (void) mouseDown:(NSEvent *)theEvent
{
    NSPoint mouseLocationWindow = [theEvent locationInWindow];
    NSPoint mouseLocationView   = [self convertPoint:mouseLocationWindow fromView:nil];

    m_pImage->OnLButtonDown(CPoint(mouseLocationView.x, mouseLocationView.y));
    
    [self setNeedsDisplay:YES];
}

- (void) mouseUp:(NSEvent *)theEvent
{
    NSPoint mouseLocationWindow = [theEvent locationInWindow];
    NSPoint mouseLocationView   = [self convertPoint:mouseLocationWindow fromView:nil];

    m_pImage->OnLButtonUp(CPoint(mouseLocationView.x, mouseLocationView.y));

    [self setNeedsDisplay:YES];
}

- (void) mouseDragged:(NSEvent *)theEvent
{
    NSPoint mouseLocationWindow = [theEvent locationInWindow];
    NSPoint mouseLocationView   = [self convertPoint:mouseLocationWindow fromView:nil];
    
    m_pImage->OnMouseMove(CPoint(mouseLocationView.x, mouseLocationView.y));

    [self setNeedsDisplay:YES];
}

2. 오른쪽 마우스 버튼 처리

- (void) rightMouseDown:(NSEvent *)theEvent
{
    NSPoint mouseLocationWindow = [theEvent locationInWindow];
    NSPoint mouseLocationView   = [self convertPoint:mouseLocationWindow fromView:nil];

    m_pImage->OnRButtonDown(CPoint(mouseLocationView.x, mouseLocationView.y));

    [self setNeedsDisplay:YES];
}


- (void) rightMouseDragged:(NSEvent *)theEvent
{
    NSPoint mouseLocationWindow = [theEvent locationInWindow];
    NSPoint mouseLocationView   = [self convertPoint:mouseLocationWindow fromView:nil];
    
    m_pImage->OnMouseMove(CPoint(mouseLocationView.x, mouseLocationView.y));
    
    [self setNeedsDisplay:YES];    
}

- (void) rightMouseUp:(NSEvent *)theEvent
{
    NSPoint mouseLocationWindow = [theEvent locationInWindow];
    NSPoint mouseLocationView   = [self convertPoint:mouseLocationWindow fromView:nil];
    
    m_pImage->OnRButtonUp(CPoint(mouseLocationView.x, mouseLocationView.y));
    
    [self setNeedsDisplay:YES];
}

NSView의 사이징 이벤트 windowDidResize



1. 노티피케이션 센터에 옵저버를 추가한다.

- (void)viewDidMoveToWindow
{
    
    [[NSNotificationCenter defaultCenteraddObserver:self
                                             selector:@selector(windowDidResize:)
                                                 name:NSWindowDidResizeNotification
                                               object:nil];
}

2. 사용이 끝난 옵저버는 제거해줘야 한다.

- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
    
    delete m_pImage;
    
    [super dealloc];
}

3. 다음 메소드를 재정의한다. [self bounds]가 클라이언트 영역의 크기이다.

- (void)windowDidResize:(NSNotification *)notification
{
    NSRect bounds = [self bounds];
    CRect rectClient(0., 0., bounds.size.width, bounds.size.height);
    m_pImage->OnSizeWindow(rectClient);
}

서브 뷰를 리사이즈 할 경우에는 아래와 같이 한다.


    [dicomImage setFrame:bounds];




영상 출력하기


- (void)drawRect:(NSRect)dirtyRect
{
    // Get Current Context
    NSGraphicsContext * nsGraphichContext = [NSGraphicsContext currentContext];
    CGContextRef context = (CGContextRef) [nsGraphichContext graphicsPort];

    // Provider
    CGDataProviderRef provider = CGDataProviderCreateWithData(
                            NULL,
                            pDib->m_pData,      // data
                            pDib->m_nWidth * pDib->m_nHeight,   // data size
                            NULL);              // release callback
    
    // color space
    unsigned char * colorTable = new unsigned char[256];
    for(int i=0;i<256;i++)
        colorTable[i] = 255 - i;

    // 시스템에서 제공하는 기본 그레이스케일
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceGray();

    // 커스텀 그레이스케일 입력하기

    CGColorSpaceRef colorSpaceRef2 = CGColorSpaceCreateIndexed(
                    colorSpaceRef, 255, colorTable);
    delete[] colorTable;
    
    // bitmap info
    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
    
    // intent
    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
    
    // Create CGImage
    CGImageRef imageRef = CGImageCreate(
                            pDib->m_nWidth,     // width
                            pDib->m_nHeight,    // height
                            8,                  // bitsPerComponent
                            8,                  // bitsPerPixel
                            pDib->m_nWidth,     // bytesPerRow
                            colorSpaceRef2,      // colorspace
                            bitmapInfo,         // bitmapInfo
                            provider,           // provider
                            NULL,               // decode
                            NO,                 // shoudInterpolate
                            renderingIntent);   // intent

    // Render
    NSRect myRect = NSMakeRect(dst.left, dst.top, dst.Width(), dst.Height());
    CGContextDrawImage(context, myRect, imageRef);
    
    CGImageRelease(imageRef);
}
    

2013년 7월 17일 수요일

Intel C++ Composer XE 2013 빌드하기


1. header file 경로와 lib 경로를 잡아준다.

   header: /opt/intel/ipp/include
   lib: /opt/intel/ipp/lib


2. 라이브러리를 링크한다.


3. 샘플 코드

MyView.mm

#import "MyTest.h"
#import "ippi.h"

@implementation MyTest

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
        IppiSize size;
        size.width  = 100;
        size.height = 100;
        Ipp8u * pSrc = new Ipp8u[100 * 100];
        for(int i=0;i<100 * 100;i++)
            pSrc[i] = 200;
        Ipp8u * pDst = new Ipp8u[100 * 100];
        
        ippiCopy_8u_C1R(pSrc, 100, pDst, 100, size);
        
        delete[] pSrc;
        delete[] pDst;
    }
    
    return self;
}

OSX에 Intel C++ Composer XE 2013 설치


다운 받은 후에 그냥 설치하면 오류 메시지 나오고 안된다.
아래와 같이 사전 작업이 필요하다.
1. sudo mkdir /Users/Shared/Library/Application\ Support/Intel
2. sudo mkdir /Users/Shared/Library/Application\ Support/Intel/Licenses
3. sudo chmod -R a+rw /Users/Shared/Library/Application\ Support/Intel/Licenses
4. 라이센스 파일을 /Users/Shared/Library/Application\ Support/Intel/Licenses에 복사한다.
이제 설치하면 잘 된다.

2013년 7월 11일 목요일

drawRect에 각종 그리기


- (void)drawRect:(NSRect)dirtyRect
{
    // 여기에 그리는 코드가 들어간다.
}

// 사각형 그리기
[[NSColor blackColorset];
NSRectFill(dirtyRect);

// 텍스트 그리기

NSString * str1 = [[NSString alloc] initWithFormat:@"Hello world %d, 100];
NSMutableDictionary * str_attributes = [[NSMutableDictionary alloc] init];
[str_attributes setObject:[NSColor blueColor] forKey:NSForegroundColorAttributeName];
[str drawAtPoint:NSMakePoint(100,100) withAttributes:str_attributes];
[str release];

메시지 상자 띄우기


NSAlert * alert = [[[NSAlert alloc] init] autorelease];
[alert addButtonWithTitle:@"OK"];
[alert setMessageText:@"Failed to load Dataset"];
[alert beginSheetModalForWindow:[[NSApp delegate] window]
                  modalDelegate:self
                 didEndSelector:NULL
                    contextInfo:nil];

AppDelegate에서 NSView 추가하기

1. 메인 윈도우의 사이징과 같이 사이징되는 NSView


- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    NSView *superview = [self.window contentView];

    mainView = [[MainView alloc] initWithFrame:[superview frame]];
    [superview addSubview:mainView];
    [superview setAutoresizesSubviews:YES];
    [mainView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
    [mainView release];
}

2. 고정 크기를 가지는 NSView


- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    NSView *superview = [self.window contentView];

    mainView = [[MainView allocinitWithFrame:[NSMakeRect(0,0,100,100]];
    [superview addSubview:mainView];
    [mainView release];
}

* mainView는 NSView에서 상속받아 만든 클래스이다.

3. 해제

- (void)applicationWillTerminate:(NSNotification *)notification
{
    [super dealloc];

}