Path: blob/master/modules/highgui/src/window_winrt_bridge.hpp
16337 views
// highgui to XAML bridge for OpenCV12// Copyright (c) Microsoft Open Technologies, Inc.3// All rights reserved.4//5// (3 - clause BSD License)6//7// Redistribution and use in source and binary forms, with or without modification, are permitted provided that8// the following conditions are met:9//10// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the11// following disclaimer.12// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the13// following disclaimer in the documentation and/or other materials provided with the distribution.14// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or15// promote products derived from this software without specific prior written permission.16//17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED18// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A19// PARTICULAR PURPOSE ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY20// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO,21// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING23// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE24// POSSIBILITY OF SUCH DAMAGE.2526#pragma once2728#include <map>29#include <opencv2\core.hpp>3031using namespace Windows::UI::Xaml::Controls;3233class CvWindow;34class CvTrackbar;3536class HighguiBridge37{38public:3940/** @brief Instantiates a Highgui singleton (Meyers type).4142The function Instantiates a Highgui singleton (Meyers type) and returns reference to that instance.43*/44static HighguiBridge& getInstance();4546/** @brief Finds window by name and returns the reference to it.4748@param name Name of the window.4950The function finds window by name and returns the reference to it. Returns nullptr51if window with specified name is not found or name argument is null.52*/53CvWindow* findWindowByName(cv::String name);5455/** @brief Returns reference to the trackbar(slider) registered within window with a provided name.5657@param name Name of the window.5859The function returns reference to the trackbar(slider) registered within window with a provided name.60Returns nullptr if trackbar with specified name is not found or window reference is nullptr.61*/62CvTrackbar* findTrackbarByName(cv::String trackbarName, cv::String windowName);6364/** @brief Converts cv::String to Platform::String.6566@param name String to convert.6768The function converts cv::String to Platform::String.69Returns nullptr if conversion fails.70*/71Platform::String^ convertString(cv::String name);7273/** @brief Creates window if there is no window with this name, otherwise returns existing window.7475@param name Window name.7677The function creates window if there is no window with this name, otherwise returns existing window.78*/79CvWindow* namedWindow(cv::String name);8081/** @brief Shows provided window.8283The function shows provided window: makes provided window current, removes current container84contents and shows current window by putting it as a container content.85*/86void showWindow(CvWindow* window);8788/** @brief Destroys window if there exists window with this name, otherwise does nothing.8990@param name Window name.9192The function destroys window if there exists window with this name, otherwise does nothing.93If window being destroyed is the current one, it will be hidden by clearing the window container.94*/95void destroyWindow(cv::String name);9697/** @brief Destroys all windows.9899The function destroys all windows.100*/101void destroyAllWindows();102103/** @brief Assigns container used to display windows.104105@param _container Container reference.106107The function assigns container used to display windows.108*/109void setContainer(Windows::UI::Xaml::Controls::Panel^ _container);110111private:112113// Meyers singleton114HighguiBridge(const HighguiBridge &);115void operator=(HighguiBridge &);116HighguiBridge() {117windowsMap = new std::map<cv::String, CvWindow*>();118};119120/** @brief Creates window if there is no window with this name.121122@param name Window name.123124The function creates window if there is no window with this name.125*/126CvWindow* createWindow(cv::String name);127128/** @brief Cleans current container contents.129130The function cleans current container contents.131*/132void cleanContainer();133134// see https://msdn.microsoft.com/en-US/library/windows/apps/xaml/hh700103.aspx135// see https://msdn.microsoft.com/ru-ru/library/windows.foundation.collections.aspx136std::map<cv::String, CvWindow*>* windowsMap;137CvWindow* currentWindow;138139// Holds current container/content to manipulate with140Windows::UI::Xaml::Controls::Panel^ container;141};142143class CvTrackbar144{145public:146CvTrackbar(cv::String name, Slider^ slider, CvWindow* parent);147~CvTrackbar();148149double getPosition();150void setPosition(double pos);151double getMaxPosition();152void setMaxPosition(double pos);153double getMinPosition();154void setMinPosition(double pos);155Slider^ getSlider();156void setSlider(Slider^ pos);157158CvTrackbarCallback2 callback;159160private:161cv::String name;162Slider^ slider;163CvWindow* parent;164};165166class CvWindow167{168public:169CvWindow(cv::String name, int flag = CV_WINDOW_NORMAL);170~CvWindow();171172/** @brief NOTE: prototype.173174Should create button if there is no button with this name already.175*/176void createButton(cv::String name);177178/** @brief Creates slider if there is no slider with this name already.179180The function creates slider if there is no slider with this name already OR resets181provided values for the existing one.182*/183void createSlider(cv::String name, int* val, int count, CvTrackbarCallback2 on_notify, void* userdata);184185/** @brief Updates window image.186187@param src Image data object reference.188189The function updates window image. If argument is null or image control is not found - does nothing.190*/191void updateImage(CvMat* arr);192193/** @brief Returns reference to the trackbar(slider) registered within provided window.194195@param name Name of the window.196197The function returns reference to the trackbar(slider) registered within provided window.198Returns nullptr if trackbar with specified name is not found or window reference is nullptr.199*/200CvTrackbar* findTrackbarByName(cv::String name);201Page^ getPage();202203private:204cv::String name;205206// Holds image data in CV format207CvMat* imageData;208209// Map of all sliders assigned to this window210std::map<cv::String, CvTrackbar*>* sliderMap;211212// Window contents holder213Page^ page;214215// Image control displayed by this window216Image^ imageControl;217218// Container for sliders219Panel^ sliderPanel;220221// Container for buttons222// TODO: prototype, not available via API223Panel^ buttonPanel;224225// Holds image width to arrange other UI elements.226// Required since imageData->width value gets recalculated when processing227int imageWidth;228229// Default markup for the container content allowing for proper components placement230static const Platform::String^ markupContent;231232// Default Slider size, fallback solution for unexpected edge cases233static const double sliderDefaultWidth;234};235236