From 597a1378546e4382beb27a3777e33d6a03cde4d1 Mon Sep 17 00:00:00 2001 From: matlabbe Date: Fri, 21 Sep 2012 14:59:09 +0000 Subject: [PATCH] Fixed compilation errors (cannot convert const char * to const wchar *) with UNICODE projects (when "-DUNICODE" is set). Fixed control bar showing -2 when webcam is used. git-svn-id: http://find-object.googlecode.com/svn/trunk/find_object@174 620bd6b2-0a58-f614-fd9a-1bd335dccda9 --- CMakeLists.txt | 1 + src/MainWindow.cpp | 3 ++- src/utilite/UDirectory.cpp | 48 +++++++++++++++++++++++++++++++++++++- 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8789782f..003df21f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,7 @@ SET(PROJECT_PREFIX find_object) ADD_DEFINITIONS(-DPROJECT_PREFIX="${PROJECT_PREFIX}") ADD_DEFINITIONS(-DPROJECT_NAME="${PROJECT_NAME}") ADD_DEFINITIONS( "-Wall" ) +#ADD_DEFINITIONS("-DUNICODE") # to test with UNICODE projects ####################### # VERSION diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 8a6ef06e..fa44b0c4 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -130,6 +130,7 @@ connect(ui_->toolBox, SIGNAL(parametersChanged(const QStringList &)), this, SLOT ui_->pushButton_pause->setVisible(false); ui_->pushButton_stop->setEnabled(true); ui_->horizontalSlider_frames->setEnabled(false); + ui_->label_frame->setVisible(false); ui_->objects_area->addAction(ui_->actionAdd_object_from_scene); ui_->objects_area->addAction(ui_->actionAdd_objects_from_files); @@ -693,7 +694,7 @@ void MainWindow::startProcessing() ui_->pushButton_pause->setVisible(true); ui_->pushButton_stop->setEnabled(true); int totalFrames = camera_->getTotalFrames(); - if(totalFrames) + if(totalFrames>0) { ui_->label_frame->setVisible(true); ui_->horizontalSlider_frames->setEnabled(true); diff --git a/src/utilite/UDirectory.cpp b/src/utilite/UDirectory.cpp index 2e4b1dcf..724a8b45 100644 --- a/src/utilite/UDirectory.cpp +++ b/src/utilite/UDirectory.cpp @@ -103,6 +103,26 @@ void UDirectory::setPath(const std::string & path, const std::string & extension this->update(); } +#ifdef WIN32 +// returned whar_t * must be deleted : delete [] wText; +wchar_t * createWCharFromChar(const char * text) +{ + DWORD length = MultiByteToWideChar (CP_ACP, 0, text, -1, NULL, 0); + wchar_t * wText = new wchar_t[length]; + MultiByteToWideChar (CP_ACP, 0, text, -1, wText, length ); + return wText; +} + +// returned char * must be deleted : delete [] text; +char * createCharFromWChar(const wchar_t * wText) +{ + DWORD length = WideCharToMultiByte (CP_ACP, 0, wText, -1, NULL, 0, NULL, NULL); + char * text = new char[length]; + WideCharToMultiByte (CP_ACP, 0, wText, -1, text, length, NULL, NULL); + return text; +} +#endif + void UDirectory::update() { if(exists(path_)) @@ -122,12 +142,24 @@ void UDirectory::update() fileNames_.clear(); #ifdef WIN32 WIN32_FIND_DATA fileInformation; + #ifdef UNICODE + wchar_t * pathAll = createWCharFromChar((path_+"\\*").c_str()); + HANDLE hFile = ::FindFirstFile(pathAll, &fileInformation); + delete [] pathAll; + #else HANDLE hFile = ::FindFirstFile((path_+"\\*").c_str(), &fileInformation); + #endif if(hFile != INVALID_HANDLE_VALUE) { do { + #ifdef UNICODE + char * fileName = createCharFromWChar(fileInformation.cFileName); + fileNames_.push_back(fileName); + delete [] fileName; + #else fileNames_.push_back(fileInformation.cFileName); + #endif } while(::FindNextFile(hFile, &fileInformation) == TRUE); ::FindClose(hFile); std::vector vFileNames = uListToVector(fileNames_); @@ -229,8 +261,14 @@ bool UDirectory::exists(const std::string & dirPath) { bool r = false; #if WIN32 + #ifdef UNICODE + wchar_t * wDirPath = createWCharFromChar(dirPath.c_str()); + DWORD dwAttrib = GetFileAttributes(wDirPath); + delete [] wDirPath; + #else DWORD dwAttrib = GetFileAttributes(dirPath.c_str()); - r = (dwAttrib != INVALID_FILE_ATTRIBUTES && (dwAttrib & FILE_ATTRIBUTE_DIRECTORY)); + #endif + r = (dwAttrib != INVALID_FILE_ATTRIBUTES && (dwAttrib & FILE_ATTRIBUTE_DIRECTORY)); #else DIR *dp; if((dp = opendir(dirPath.c_str())) != NULL) @@ -329,9 +367,17 @@ std::string UDirectory::homeDir() { std::string path; #if WIN32 + #ifdef UNICODE + wchar_t wProfilePath[250]; + ExpandEnvironmentStrings(L"%userprofile%",wProfilePath,250); + char * profilePath = createCharFromWChar(wProfilePath); + path = profilePath; + delete [] profilePath; + #else char profilePath[250]; ExpandEnvironmentStrings("%userprofile%",profilePath,250); path = profilePath; + #endif #else path = getenv("HOME"); #endif