Skip to content

Commit

Permalink
Resolve #5207: Added Move buttons to conversation dialog to re-order …
Browse files Browse the repository at this point in the history
…existing conversations.
  • Loading branch information
codereader committed Apr 6, 2020
1 parent 564726c commit 9828924
Show file tree
Hide file tree
Showing 6 changed files with 364 additions and 330 deletions.
531 changes: 214 additions & 317 deletions install/ui/conversationdialog.fbp

Large diffs are not rendered by default.

40 changes: 40 additions & 0 deletions install/ui/conversationdialog.xrc
Expand Up @@ -46,6 +46,8 @@
<size>80,-1</size>
<label>Add</label>
<default>0</default>
<markup>0</markup>
<bitmap />
</object>
</object>
<object class="sizeritem">
Expand All @@ -56,6 +58,8 @@
<size>80,-1</size>
<label>Delete</label>
<default>0</default>
<markup>0</markup>
<bitmap />
</object>
</object>
</object>
Expand Down Expand Up @@ -102,6 +106,8 @@
<size>80,-1</size>
<label>Add</label>
<default>0</default>
<markup>0</markup>
<bitmap />
</object>
</object>
<object class="sizeritem">
Expand All @@ -112,6 +118,32 @@
<size>80,-1</size>
<label>Edit</label>
<default>0</default>
<markup>0</markup>
<bitmap />
</object>
</object>
<object class="sizeritem">
<option>0</option>
<flag>wxBOTTOM|wxEXPAND</flag>
<border>6</border>
<object class="wxButton" name="ConvDialogMoveUpConvButton">
<size>80,-1</size>
<label>Move Up</label>
<default>0</default>
<markup>0</markup>
<bitmap />
</object>
</object>
<object class="sizeritem">
<option>0</option>
<flag>wxBOTTOM|wxEXPAND</flag>
<border>6</border>
<object class="wxButton" name="ConvDialogMoveDownConvButton">
<size>80,-1</size>
<label>Move Down</label>
<default>0</default>
<markup>0</markup>
<bitmap />
</object>
</object>
<object class="sizeritem">
Expand All @@ -122,6 +154,8 @@
<size>80,-1</size>
<label>Delete</label>
<default>0</default>
<markup>0</markup>
<bitmap />
</object>
</object>
<object class="sizeritem">
Expand All @@ -132,6 +166,8 @@
<size>80,-1</size>
<label>Clear</label>
<default>0</default>
<markup>0</markup>
<bitmap />
</object>
</object>
</object>
Expand All @@ -151,6 +187,8 @@
<object class="wxButton" name="ConvDialogCancelButton">
<label>Cancel</label>
<default>0</default>
<markup>0</markup>
<bitmap />
</object>
</object>
<object class="sizeritem">
Expand All @@ -160,6 +198,8 @@
<object class="wxButton" name="ConvDialogOkButton">
<label>OK</label>
<default>0</default>
<markup>0</markup>
<bitmap />
</object>
</object>
</object>
Expand Down
75 changes: 62 additions & 13 deletions plugins/dm.conversation/ConversationDialog.cpp
Expand Up @@ -34,9 +34,17 @@ namespace
ConversationDialog::ConversationDialog() :
DialogBase(_(WINDOW_TITLE)),
_entityList(new wxutil::TreeModel(_convEntityColumns, true)),
_entityView(NULL),
_entityView(nullptr),
_convList(new wxutil::TreeModel(_convColumns, true)),
_convView(NULL)
_convView(nullptr),
_addConvButton(nullptr),
_editConvButton(nullptr),
_deleteConvButton(nullptr),
_moveUpConvButton(nullptr),
_moveDownConvButton(nullptr),
_clearConvButton(nullptr),
_addEntityButton(nullptr),
_deleteEntityButton(nullptr)
{
// Create the widgets
populateWindow();
Expand Down Expand Up @@ -87,19 +95,27 @@ void ConversationDialog::populateWindow()

// Wire up button signals
_addConvButton = findNamedObject<wxButton>(this, "ConvDialogAddConvButton");
_addConvButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(ConversationDialog::onAddConversation), NULL, this);
_addConvButton->Bind(wxEVT_BUTTON, &ConversationDialog::onAddConversation, this);
_addConvButton->Enable(false); // not enabled without selection

_editConvButton = findNamedObject<wxButton>(this, "ConvDialogEditConvButton");
_editConvButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(ConversationDialog::onEditConversation), NULL, this);
_editConvButton->Bind(wxEVT_BUTTON, &ConversationDialog::onEditConversation, this);
_editConvButton->Enable(false); // not enabled without selection

_deleteConvButton = findNamedObject<wxButton>(this, "ConvDialogDeleteConvButton");
_deleteConvButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(ConversationDialog::onDeleteConversation), NULL, this);
_deleteConvButton = findNamedObject<wxButton>(this, "ConvDialogDeleteConvButton");
_deleteConvButton->Bind(wxEVT_BUTTON, &ConversationDialog::onDeleteConversation, this);
_deleteConvButton->Enable(false); // not enabled without selection

_moveUpConvButton = findNamedObject<wxButton>(this, "ConvDialogMoveUpConvButton");
_moveUpConvButton->Bind(wxEVT_BUTTON, &ConversationDialog::onMoveConversationUpOrDown, this);
_moveUpConvButton->Enable(false); // not enabled without selection

_moveDownConvButton = findNamedObject<wxButton>(this, "ConvDialogMoveDownConvButton");
_moveDownConvButton->Bind(wxEVT_BUTTON, &ConversationDialog::onMoveConversationUpOrDown, this);
_moveDownConvButton->Enable(false); // not enabled without selection

_clearConvButton = findNamedObject<wxButton>(this, "ConvDialogClearConvButton");
_clearConvButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(ConversationDialog::onClearConversations), NULL, this);
_clearConvButton->Bind(wxEVT_BUTTON, &ConversationDialog::onClearConversations, this);
_clearConvButton->Enable(false); // requires >0 conversations

// Boldify a few labels
Expand Down Expand Up @@ -229,6 +245,8 @@ void ConversationDialog::updateConversationPanelSensitivity()
_addConvButton->Enable(false);
_editConvButton->Enable(false);
_deleteConvButton->Enable(false);
_moveUpConvButton->Enable(false);
_moveDownConvButton->Enable(false);
_clearConvButton->Enable(false);
}
}
Expand Down Expand Up @@ -315,18 +333,24 @@ void ConversationDialog::handleConversationSelectionChange()
{
// Get the selection
_currentConversation = _convView->GetSelection();
int index = getSelectedConvIndex();

if (_currentConversation.IsOk())
{
// Enable the edit and delete buttons
_editConvButton->Enable(true);
_deleteConvButton->Enable(true);

_moveUpConvButton->Enable(index > 1);
_moveDownConvButton->Enable(index < _curEntity->second->getHighestIndex());
}
else
{
// Disable the edit and delete buttons
// Disable all manipulation buttons
_editConvButton->Enable(false);
_deleteConvButton->Enable(false);
_moveUpConvButton->Enable(false);
_moveDownConvButton->Enable(false);
}
}

Expand All @@ -337,11 +361,21 @@ void ConversationDialog::onAddConversation(wxCommandEvent& ev)
refreshConversationList();
}

void ConversationDialog::onEditConversation(wxCommandEvent& ev)
int ConversationDialog::getSelectedConvIndex()
{
if (!_currentConversation.IsOk())
{
return -1;
}

// Retrieve the index of the current conversation
wxutil::TreeModel::Row row(_currentConversation, *_convList);
int index = row[_convColumns.index].getInteger();
return row[_convColumns.index].getInteger();
}

void ConversationDialog::onEditConversation(wxCommandEvent& ev)
{
int index = getSelectedConvIndex();

conversation::Conversation& conv = _curEntity->second->getConversation(index);

Expand All @@ -357,9 +391,7 @@ void ConversationDialog::onEditConversation(wxCommandEvent& ev)

void ConversationDialog::onDeleteConversation(wxCommandEvent& ev)
{
// Get the index of the current conversation
wxutil::TreeModel::Row row(_currentConversation, *_convList);
int index = row[_convColumns.index].getInteger();
int index = getSelectedConvIndex();

// Tell the ConversationEntity to delete this conversation
_curEntity->second->deleteConversation(index);
Expand All @@ -368,6 +400,23 @@ void ConversationDialog::onDeleteConversation(wxCommandEvent& ev)
refreshConversationList();
}

void ConversationDialog::onMoveConversationUpOrDown(wxCommandEvent& ev)
{
bool moveUp = ev.GetEventObject() == _moveUpConvButton;
int index = getSelectedConvIndex();

int newIndex = _curEntity->second->moveConversation(index, moveUp);

// Repopulate the conversation list
refreshConversationList();

// Try to select the moved item
auto item = _convList->FindInteger(newIndex, _convColumns.index);
_convView->Select(item);

handleConversationSelectionChange();
}

void ConversationDialog::onClearConversations(wxCommandEvent& ev)
{
// Clear the entity and refresh the list
Expand Down
5 changes: 5 additions & 0 deletions plugins/dm.conversation/ConversationDialog.h
Expand Up @@ -47,6 +47,8 @@ class ConversationDialog :
wxButton* _addConvButton;
wxButton* _editConvButton;
wxButton* _deleteConvButton;
wxButton* _moveUpConvButton;
wxButton* _moveDownConvButton;
wxButton* _clearConvButton;

wxButton* _addEntityButton;
Expand Down Expand Up @@ -90,7 +92,10 @@ class ConversationDialog :
void onAddConversation(wxCommandEvent& ev);
void onEditConversation(wxCommandEvent& ev);
void onDeleteConversation(wxCommandEvent& ev);
void onMoveConversationUpOrDown(wxCommandEvent& ev);
void onClearConversations(wxCommandEvent& ev);

int getSelectedConvIndex();
};

} // namespace ui
34 changes: 34 additions & 0 deletions plugins/dm.conversation/ConversationEntity.cpp
Expand Up @@ -39,6 +39,16 @@ void ConversationEntity::deleteWorldNode()
}
}

int ConversationEntity::getHighestIndex()
{
if (_conversations.empty())
{
return -1;
}

return _conversations.rbegin()->first;
}

// Add a new conversation
void ConversationEntity::addConversation()
{
Expand Down Expand Up @@ -91,6 +101,30 @@ void ConversationEntity::deleteConversation(int index)
}
}

int ConversationEntity::moveConversation(int index, bool moveUp)
{
if (moveUp && index <= 1 || !moveUp && index >= getHighestIndex())
{
return index; // no change
}

int targetIndex = index + (moveUp ? -1 : +1);

if (_conversations.find(targetIndex) != _conversations.end())
{
// Swap with existing element
std::swap(_conversations[index], _conversations[targetIndex]);
}
else
{
// Nothing present at the target index, just move
_conversations[targetIndex] = _conversations[index];
_conversations.erase(index);
}

return targetIndex;
}

void ConversationEntity::populateListStore(wxutil::TreeModel& store, const ConversationColumns& columns) const
{
for (const auto& pair : _conversations)
Expand Down
9 changes: 9 additions & 0 deletions plugins/dm.conversation/ConversationEntity.h
Expand Up @@ -78,6 +78,9 @@ class ConversationEntity
return _conversations[iIndex];
}

// Returns the highest used conversation index (will return -1 if no conversations are present)
int getHighestIndex();

/**
* Add a new conversation, starting from the first unused conversation ID.
*/
Expand All @@ -89,6 +92,12 @@ class ConversationEntity
*/
void deleteConversation(int index);

/**
* Move the conversation with the given index either up or down.
* Will return the conversation's index after the move
*/
int moveConversation(int index, bool moveUp);

/**
* Clear all conversations.
*/
Expand Down

0 comments on commit 9828924

Please sign in to comment.