Bugfix: prevent crash with current source when reordering

This commit is contained in:
brunoherbelin
2021-03-21 13:24:54 +01:00
parent 5f800f3723
commit 28d4d4acc4
6 changed files with 64 additions and 43 deletions

View File

@@ -772,6 +772,19 @@ void Mixer::setCurrentIndex(int index)
setCurrentSource( session_->at(index) ); setCurrentSource( session_->at(index) );
} }
void Mixer::moveIndex (int current_index, int target_index)
{
// remember ptr to current source
Source *previous_current_source_ = currentSource();
// change order
session_->move(current_index, target_index);
// restore current
unsetCurrentSource();
setCurrentSource(previous_current_source_);
}
void Mixer::setCurrentNext() void Mixer::setCurrentNext()
{ {
if (session_->numSource() > 0) { if (session_->numSource() > 0) {
@@ -831,11 +844,10 @@ int Mixer::indexCurrentSource()
Source *Mixer::currentSource() Source *Mixer::currentSource()
{ {
if ( current_source_ == session_->end() ) if ( current_source_ != session_->end() )
return (*current_source_);
else
return nullptr; return nullptr;
else {
return *(current_source_);
}
} }
// management of view // management of view

View File

@@ -73,6 +73,7 @@ public:
void unsetCurrentSource (); void unsetCurrentSource ();
void setCurrentIndex (int index); void setCurrentIndex (int index);
void moveIndex (int current_index, int target_index);
int indexCurrentSource (); int indexCurrentSource ();
// browsing into sources // browsing into sources

View File

@@ -16,6 +16,7 @@
#include "ImageShader.h" #include "ImageShader.h"
#include "ImageProcessingShader.h" #include "ImageProcessingShader.h"
#include "MediaPlayer.h" #include "MediaPlayer.h"
#include "SystemToolkit.h"
#include <tinyxml2.h> #include <tinyxml2.h>
#include "tinyxml2Toolkit.h" #include "tinyxml2Toolkit.h"
@@ -26,12 +27,13 @@ std::string SessionCreator::info(const std::string& filename)
{ {
std::string ret = ""; std::string ret = "";
// if the file exists
if (SystemToolkit::file_exists(filename)) {
// try to load the file
XMLDocument doc; XMLDocument doc;
XMLError eResult = doc.LoadFile(filename.c_str()); XMLError eResult = doc.LoadFile(filename.c_str());
if ( XMLResultError(eResult)) { // silently ignore on error
Log::Warning("%s could not be openned.", filename.c_str()); if ( !XMLResultError(eResult, false)) {
return ret;
}
XMLElement *header = doc.FirstChildElement(APP_NAME); XMLElement *header = doc.FirstChildElement(APP_NAME);
if (header != nullptr && header->Attribute("date") != 0) { if (header != nullptr && header->Attribute("date") != 0) {
@@ -48,6 +50,8 @@ std::string SessionCreator::info(const std::string& filename)
} }
} }
}
}
return ret; return ret;
} }

View File

@@ -1582,19 +1582,22 @@ void MediaController::setMediaPlayer(MediaPlayer *mp)
void MediaController::followCurrentSource() void MediaController::followCurrentSource()
{ {
Source *s = Mixer::manager().currentSource(); Source *s = Mixer::manager().currentSource();
if ( s != nullptr) { MediaSource *ms = nullptr;
MediaSource *ms = dynamic_cast<MediaSource *>(s);
if (ms) { if ( s != nullptr)
ms = dynamic_cast<MediaSource *>(s);
if ( ms != nullptr) {
// update the internal mediaplayer if changed // update the internal mediaplayer if changed
if (mp_ != ms->mediaplayer()) { if (mp_ != ms->mediaplayer()) {
mp_ = ms->mediaplayer(); mp_ = ms->mediaplayer();
media_playing_mode_ = mp_->isPlaying(); media_playing_mode_ = mp_->isPlaying();
} }
} else { }
else {
mp_ = nullptr; mp_ = nullptr;
media_playing_mode_ = false; media_playing_mode_ = false;
} }
}
} }
@@ -2153,11 +2156,11 @@ void Navigator::Render()
if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("DND_SOURCE")) if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("DND_SOURCE"))
{ {
if ( payload->DataSize == sizeof(int) ) { if ( payload->DataSize == sizeof(int) ) {
// drop means move index and reorder
int payload_index = *(const int*)payload->Data; int payload_index = *(const int*)payload->Data;
int current_source_index = Mixer::manager().indexCurrentSource(); Mixer::manager().moveIndex(payload_index, index);
Mixer::manager().session()->move(payload_index, index); // index of current source changed
if (payload_index == current_source_index) applyButtonSelection(Mixer::manager().indexCurrentSource());
applyButtonSelection(index);
} }
} }
ImGui::EndDragDropTarget(); ImGui::EndDragDropTarget();
@@ -2831,7 +2834,7 @@ void Navigator::RenderMainPannel()
for(auto it = sessions_list.begin(); it != sessions_list.end(); it++) { for(auto it = sessions_list.begin(); it != sessions_list.end(); it++) {
std::string sessionfilename(*it); std::string sessionfilename(*it);
if (sessionfilename.empty()) if (sessionfilename.empty())
break; continue;
std::string shortname = SystemToolkit::filename(*it); std::string shortname = SystemToolkit::filename(*it);
if (ImGui::Selectable( shortname.c_str(), false, ImGuiSelectableFlags_AllowDoubleClick )) { if (ImGui::Selectable( shortname.c_str(), false, ImGuiSelectableFlags_AllowDoubleClick )) {
if (ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left) || file_selected == it) { if (ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left) || file_selected == it) {
@@ -2857,7 +2860,7 @@ void Navigator::RenderMainPannel()
file_info.clear(); file_info.clear();
file_selected = sessions_list.end(); file_selected = sessions_list.end();
} }
if (!file_info.empty()) { else if (!file_info.empty()) {
ImGui::BeginTooltip(); ImGui::BeginTooltip();
ImGui::Text("%s", file_info.c_str()); ImGui::Text("%s", file_info.c_str());
ImGui::EndTooltip(); ImGui::EndTooltip();

View File

@@ -237,11 +237,12 @@ bool tinyxml2::XMLSaveDoc(XMLDocument * const doc, std::string filename)
return !XMLResultError(eResult); return !XMLResultError(eResult);
} }
bool tinyxml2::XMLResultError(int result) bool tinyxml2::XMLResultError(int result, bool verbose)
{ {
XMLError xmlresult = (XMLError) result; XMLError xmlresult = (XMLError) result;
if ( xmlresult != XML_SUCCESS) if ( xmlresult != XML_SUCCESS)
{ {
if (verbose)
Log::Info("XML error %i: %s", result, tinyxml2::XMLDocument::ErrorIDToName(xmlresult)); Log::Info("XML error %i: %s", result, tinyxml2::XMLDocument::ErrorIDToName(xmlresult));
return true; return true;
} }

View File

@@ -29,7 +29,7 @@ XMLElement *XMLElementEncodeArray(XMLDocument *doc, const void *array, uint arra
bool XMLElementDecodeArray(XMLElement *elem, void *array, uint arraysize); bool XMLElementDecodeArray(XMLElement *elem, void *array, uint arraysize);
bool XMLSaveDoc(tinyxml2::XMLDocument * const doc, std::string filename); bool XMLSaveDoc(tinyxml2::XMLDocument * const doc, std::string filename);
bool XMLResultError(int result); bool XMLResultError(int result, bool verbose = true);
} }