SOL9 Sample: ImageViewWithPopupMenu

SOL9 2.0 Samples

1 Screenshot


2 Source code

/*
 * ImageViewWithPopupMenu.cpp 
 * Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


//2017/03/30

#define _CONSOLE_

#include <sol/ModuleFileName.h>
#include <sol/PopupMenu.h>

#include <sol/opencv/OpenCVApplicationView.h>
#include <sol/FileDialog.h>
#include <sol/opencv/OpenCVImageView.h>
#include <sol/PushButton.h>
#include <sol/openCV/OpenCVInnerView.h>

namespace SOL {

class MainView :public OpenCVApplicationView {

private:
  //Inner class starts.
  class SimpleView :public OpenCVImageView {
  private:
    cv::Mat image;
    
    cv::Mat& getMat()
    {
      return image;
    }
    
    void display()
    {
      show(image);
    }
    
  public:
    SimpleView(View* parent, const char* name, Args& args)
    :OpenCVImageView(parent, name, args)
    {
      try {
        const char* filename = (const char*)args.get(XmNimageFileName);
        int imageLoadingFlag = args.get(XmNimageLoadingFlag);

        image = readImage(filename, imageLoadingFlag);
        
      } catch (SOL::Exception ex) {
        caught(ex);
      }
    }
    
    ~SimpleView()
    {
    }      
  };
  //Inner class ends.

  SmartPtr<SimpleView>  view;
  SmartPtr<PushButton>  quit;
  SmartPtr<PopupMenu>   popupMenu;
  
  typedef enum {
    POPUP_CLEAR = 300,
    POPUP_COPY,
    POPUP_SAVEAS,
  } POPUPMENU;
  
  void resize(int w, int h)
  {
    if (view && quit) {
      view -> reshape(4, 60, w-100,    h-64);
      quit -> reshape(w-100 + 10, 10, 80, 30);      
    }
  }
  
  void mouseCallback(int event, int x, int y, int flags, void* param)
  {
    if(event == CV_EVENT_RBUTTONDOWN) {
      if (popupMenu) {
        popupMenu->track();
      }
    }
  }

  void popupClear(Action& action)
  {
    int rc = MessageBox(NULL, "PopupMenu calllback: clear", "Confirmation", 
                MB_OKCANCEL|MB_ICONEXCLAMATION);
    if (rc == IDOK) {
      ; //Do nothing here.
    }
  }

  void popupCopy(Action& action)
  {
    int rc = MessageBox(NULL, "PopupMenu callback: copy", "Confirmation", 
                MB_OKCANCEL|MB_ICONEXCLAMATION);
    if (rc == IDOK) {
     ; //Do nothing here.
    }
  }

  void popupSaveAs(Action& action)
  {
    Args ar;
    FileDialog savedlg;
    ar.set(XmNaccessMode, FileDialog::SAVE);
    savedlg.create(this, "FileSave", ar);
    savedlg.popup(action);
    if (action.getResult()) {
      char* filename = savedlg.getFileName();
      view->writeImage(filename);
    }
  }

  void confirm(Action& action)
  {
    int rc = MessageBox(NULL, "Are you sure to close this window?", "Confirmation", 
                MB_OKCANCEL|MB_ICONEXCLAMATION);
    if (rc == IDOK) {
      exit(action);
    }
  }
  

public:
  MainView(OpenCVApplication& applet, const char* name, Args& args)
  :OpenCVApplicationView(applet, name, args)
  {
    try {
      Args ar;
      ar.set(XmNimageFileName, "..\\images\\flower6.jpg");
      ar.set(XmNimageLoadingFlag, CV_LOAD_IMAGE_COLOR);
      view = new SimpleView(this, "cvwindow", ar); 

      ar.reset();
      quit = new PushButton(this, "Quit", ar);
      quit -> addCallback(XmNactivateCallback, this, 
          (Callback)&MainView::confirm, NULL); 
  
      PopupMenuItem popupMenuItems[] = {
        {POPUP_CLEAR,   "Clear   (&X)", (Callback)&MainView::popupClear},
        {POPUP_COPY,    "Copy    (&C)", (Callback)&MainView::popupCopy},
        {POPUP_SAVEAS,  "Save as (&S)", (Callback)&MainView::popupSaveAs},
      };
      popupMenu = new PopupMenu(this);
      for (int i = 0; i<CountOf(popupMenuItems); i++) {
        popupMenu->append(popupMenuItems[i].menuId, popupMenuItems[i].name);
        addCallback(XmNmenuCallback, popupMenuItems[i].menuId, this,
          popupMenuItems[i].callback, NULL);
      }
      
      view -> addCallback(XmNmouseCallback, this,
        (MouseCallback)&MainView::mouseCallback, NULL);

      addCallback(XmNmenuCallback, IDM_EXIT, this,
          (Callback)&MainView::confirm, NULL);
      
    } catch (Exception& ex) {
      caught(ex);
    }
  }

  ~MainView()
  {
  }
};
}

//
void main(int argc, char** argv) 
{
  cvInitSystem(argc, argv);
  
  try {
    ModuleFileName module(argv[0]);
    
    const char*  name = module.getAppName();
        
    OpenCVApplication applet(name, argc, argv);

    Args args;
    args.set(XmNwidth,  640);
    args.set(XmNheight, 480);
    MainView view(applet, name, args);
    view.realize();

    applet.run();
    
  } catch (SOL::Exception& ex) {
    caught(ex);
  }
}


Last modified: 2 Dec. 2017

Copyright (c) 2017 Antillia.com ALL RIGHTS RESERVED.