Use string_view in more UI code

simplify_debugging
Evil Eye 2 years ago
parent 5491512905
commit 492e336c0c

@ -47,7 +47,7 @@ namespace MWGui
playerModel->update(); playerModel->update();
ItemModel::ModelIndex newIndex = -1; ItemModel::ModelIndex newIndex = -1;
for (unsigned int i = 0; i < playerModel->getItemCount(); ++i) for (size_t i = 0; i < playerModel->getItemCount(); ++i)
{ {
if (playerModel->getItem(i).mBase == item) if (playerModel->getItem(i).mBase == item)
{ {

@ -779,7 +779,7 @@ namespace MWGui
if (selected != -1) if (selected != -1)
lastId = model.getItem(selected).mBase.getCellRef().getRefId(); lastId = model.getItem(selected).mBase.getCellRef().getRefId();
ItemModel::ModelIndex cycled = selected; ItemModel::ModelIndex cycled = selected;
for (unsigned int i = 0; i < model.getItemCount(); ++i) for (size_t i = 0; i < model.getItemCount(); ++i)
{ {
cycled += incr; cycled += incr;
cycled = (cycled + model.getItemCount()) % model.getItemCount(); cycled = (cycled + model.getItemCount()) % model.getItemCount();

@ -116,10 +116,10 @@ namespace MWGui
std::string link = utf8text.substr(pos_begin + 1, pos_end - pos_begin - 1); std::string link = utf8text.substr(pos_begin + 1, pos_end - pos_begin - 1);
const char specialPseudoAsteriskCharacter = 127; const char specialPseudoAsteriskCharacter = 127;
std::replace(link.begin(), link.end(), specialPseudoAsteriskCharacter, '*'); std::replace(link.begin(), link.end(), specialPseudoAsteriskCharacter, '*');
std::string topicName = MWBase::Environment::get() std::string_view topicName = MWBase::Environment::get()
.getWindowManager() .getWindowManager()
->getTranslationDataStorage() ->getTranslationDataStorage()
.topicStandardForm(link); .topicStandardForm(link);
std::string displayName = link; std::string displayName = link;
while (displayName[displayName.size() - 1] == '*') while (displayName[displayName.size() - 1] == '*')

@ -472,7 +472,7 @@ namespace MWGui
void SettingsWindow::onResolutionAccept() void SettingsWindow::onResolutionAccept()
{ {
std::string resStr = mResolutionList->getItemNameAt(mResolutionList->getIndexSelected()); const std::string& resStr = mResolutionList->getItemNameAt(mResolutionList->getIndexSelected());
int resX, resY; int resX, resY;
parseResolution(resX, resY, resStr); parseResolution(resX, resY, resStr);
@ -844,7 +844,7 @@ namespace MWGui
// check if this resolution is supported in fullscreen // check if this resolution is supported in fullscreen
if (mResolutionList->getIndexSelected() != MyGUI::ITEM_NONE) if (mResolutionList->getIndexSelected() != MyGUI::ITEM_NONE)
{ {
std::string resStr = mResolutionList->getItemNameAt(mResolutionList->getIndexSelected()); const std::string& resStr = mResolutionList->getItemNameAt(mResolutionList->getIndexSelected());
int resX, resY; int resX, resY;
parseResolution(resX, resY, resStr); parseResolution(resX, resY, resStr);
Settings::Manager::setInt("resolution x", "Video", resX); Settings::Manager::setInt("resolution x", "Video", resX);
@ -853,9 +853,9 @@ namespace MWGui
bool supported = false; bool supported = false;
int fallbackX = 0, fallbackY = 0; int fallbackX = 0, fallbackY = 0;
for (unsigned int i = 0; i < mResolutionList->getItemCount(); ++i) for (size_t i = 0; i < mResolutionList->getItemCount(); ++i)
{ {
std::string resStr = mResolutionList->getItemNameAt(i); const std::string& resStr = mResolutionList->getItemNameAt(i);
int resX, resY; int resX, resY;
parseResolution(resX, resY, resStr); parseResolution(resX, resY, resStr);

@ -162,7 +162,7 @@ namespace MWGui
// match model against line // match model against line
// if don't match, then major change has happened, so do a full update // if don't match, then major change has happened, so do a full update
if (mModel->getItemCount() <= static_cast<unsigned>(spellIndex)) if (mModel->getItemCount() <= static_cast<size_t>(spellIndex))
{ {
fullUpdateRequired = true; fullUpdateRequired = true;
break; break;

@ -1096,26 +1096,25 @@ namespace MWGui
void WindowManager::onRetrieveTag(const MyGUI::UString& _tag, MyGUI::UString& _result) void WindowManager::onRetrieveTag(const MyGUI::UString& _tag, MyGUI::UString& _result)
{ {
std::string tag(_tag); std::string_view tag = _tag.asUTF8();
std::string MyGuiPrefix = "setting="; std::string_view MyGuiPrefix = "setting=";
size_t MyGuiPrefixLength = MyGuiPrefix.length();
std::string tokenToFind = "sCell="; std::string_view tokenToFind = "sCell=";
size_t tokenLength = tokenToFind.length();
if (tag.compare(0, MyGuiPrefixLength, MyGuiPrefix) == 0) if (tag.starts_with(MyGuiPrefix))
{ {
tag = tag.substr(MyGuiPrefixLength, tag.length()); tag = tag.substr(MyGuiPrefix.length());
size_t comma_pos = tag.find(','); size_t comma_pos = tag.find(',');
std::string settingSection = tag.substr(0, comma_pos); std::string_view settingSection = tag.substr(0, comma_pos);
std::string settingTag = tag.substr(comma_pos + 1, tag.length()); std::string_view settingTag = tag.substr(comma_pos + 1, tag.length());
_result = Settings::Manager::getString(settingTag, settingSection); _result = Settings::Manager::getString(settingTag, settingSection);
} }
else if (tag.compare(0, tokenLength, tokenToFind) == 0) else if (tag.starts_with(tokenToFind))
{ {
_result = mTranslationDataStorage.translateCellName(tag.substr(tokenLength)); std::string_view cellName = mTranslationDataStorage.translateCellName(tag.substr(tokenToFind.length()));
_result.assign(cellName.data(), cellName.size());
_result = MyGUI::TextIterator::toTagsString(_result); _result = MyGUI::TextIterator::toTagsString(_result);
} }
else if (Gui::replaceTag(tag, _result)) else if (Gui::replaceTag(tag, _result))
@ -1125,7 +1124,7 @@ namespace MWGui
else else
{ {
std::vector<std::string> split; std::vector<std::string> split;
Misc::StringUtils::split(tag, split, ":"); Misc::StringUtils::split(std::string{ tag }, split, ":");
l10n::Manager& l10nManager = *MWBase::Environment::get().getL10nManager(); l10n::Manager& l10nManager = *MWBase::Environment::get().getL10nManager();
@ -1141,7 +1140,7 @@ namespace MWGui
{ {
Log(Debug::Error) << "Error: WindowManager::onRetrieveTag: no Store set up yet, can not replace '" Log(Debug::Error) << "Error: WindowManager::onRetrieveTag: no Store set up yet, can not replace '"
<< tag << "'"; << tag << "'";
_result = tag; _result.assign(tag.data(), tag.size());
return; return;
} }
const ESM::GameSetting* setting = mStore->get<ESM::GameSetting>().search(tag); const ESM::GameSetting* setting = mStore->get<ESM::GameSetting>().search(tag);
@ -1149,7 +1148,7 @@ namespace MWGui
if (setting && setting->mValue.getType() == ESM::VT_String) if (setting && setting->mValue.getType() == ESM::VT_String)
_result = setting->mValue.getString(); _result = setting->mValue.getString();
else else
_result = tag; _result.assign(tag.data(), tag.size());
} }
} }

@ -9,7 +9,7 @@ namespace Translation
{ {
} }
void Storage::loadTranslationData(const Files::Collections& dataFileCollections, const std::string& esmFileName) void Storage::loadTranslationData(const Files::Collections& dataFileCollections, std::string_view esmFileName)
{ {
std::string esmNameNoExtension(Misc::StringUtils::lowerCase(esmFileName)); std::string esmNameNoExtension(Misc::StringUtils::lowerCase(esmFileName));
// changing the extension // changing the extension
@ -64,9 +64,9 @@ namespace Translation
} }
} }
std::string Storage::translateCellName(const std::string& cellName) const std::string_view Storage::translateCellName(std::string_view cellName) const
{ {
std::map<std::string, std::string>::const_iterator entry = mCellNamesTranslations.find(cellName); auto entry = mCellNamesTranslations.find(cellName);
if (entry == mCellNamesTranslations.end()) if (entry == mCellNamesTranslations.end())
return cellName; return cellName;
@ -74,12 +74,12 @@ namespace Translation
return entry->second; return entry->second;
} }
std::string Storage::topicID(const std::string& phrase) const std::string_view Storage::topicID(std::string_view phrase) const
{ {
std::string result = topicStandardForm(phrase); std::string_view result = topicStandardForm(phrase);
// seeking for the topic ID // seeking for the topic ID
std::map<std::string, std::string>::const_iterator topicIDIterator = mTopicIDs.find(result); auto topicIDIterator = mTopicIDs.find(result);
if (topicIDIterator != mTopicIDs.end()) if (topicIDIterator != mTopicIDs.end())
result = topicIDIterator->second; result = topicIDIterator->second;
@ -87,9 +87,9 @@ namespace Translation
return result; return result;
} }
std::string Storage::topicStandardForm(const std::string& phrase) const std::string_view Storage::topicStandardForm(std::string_view phrase) const
{ {
std::map<std::string, std::string>::const_iterator phraseFormsIterator = mPhraseForms.find(phrase); auto phraseFormsIterator = mPhraseForms.find(phrase);
if (phraseFormsIterator != mPhraseForms.end()) if (phraseFormsIterator != mPhraseForms.end())
return phraseFormsIterator->second; return phraseFormsIterator->second;

@ -11,20 +11,20 @@ namespace Translation
public: public:
Storage(); Storage();
void loadTranslationData(const Files::Collections& dataFileCollections, const std::string& esmFileName); void loadTranslationData(const Files::Collections& dataFileCollections, std::string_view esmFileName);
std::string translateCellName(const std::string& cellName) const; std::string_view translateCellName(std::string_view cellName) const;
std::string topicID(const std::string& phrase) const; std::string_view topicID(std::string_view phrase) const;
// Standard form usually means nominative case // Standard form usually means nominative case
std::string topicStandardForm(const std::string& phrase) const; std::string_view topicStandardForm(std::string_view phrase) const;
void setEncoder(ToUTF8::Utf8Encoder* encoder); void setEncoder(ToUTF8::Utf8Encoder* encoder);
bool hasTranslation() const; bool hasTranslation() const;
private: private:
typedef std::map<std::string, std::string> ContainerType; typedef std::map<std::string, std::string, std::less<>> ContainerType;
void loadData(ContainerType& container, const std::string& fileNameNoExtension, const std::string& extension, void loadData(ContainerType& container, const std::string& fileNameNoExtension, const std::string& extension,
const Files::Collections& dataFileCollections); const Files::Collections& dataFileCollections);

@ -36,7 +36,7 @@ namespace Gui
void MWList::addSeparator() void MWList::addSeparator()
{ {
mItems.emplace_back(""); mItems.emplace_back(std::string{});
} }
void MWList::adjustSize() void MWList::adjustSize()
@ -46,9 +46,9 @@ namespace Gui
void MWList::redraw(bool scrollbarShown) void MWList::redraw(bool scrollbarShown)
{ {
const int _scrollBarWidth = 20; // fetch this from skin? constexpr int _scrollBarWidth = 20; // fetch this from skin?
const int scrollBarWidth = scrollbarShown ? _scrollBarWidth : 0; const int scrollBarWidth = scrollbarShown ? _scrollBarWidth : 0;
const int spacing = 3; constexpr int spacing = 3;
int viewPosition = -mScrollView->getViewOffset().top; int viewPosition = -mScrollView->getViewOffset().top;
while (mScrollView->getChildCount()) while (mScrollView->getChildCount())
@ -58,16 +58,16 @@ namespace Gui
mItemHeight = 0; mItemHeight = 0;
int i = 0; int i = 0;
for (std::vector<std::string>::const_iterator it = mItems.begin(); it != mItems.end(); ++it) for (const auto& item : mItems)
{ {
if (*it != "") if (!item.empty())
{ {
if (mListItemSkin.empty()) if (mListItemSkin.empty())
return; return;
MyGUI::Button* button = mScrollView->createWidget<MyGUI::Button>(mListItemSkin, MyGUI::Button* button = mScrollView->createWidget<MyGUI::Button>(mListItemSkin,
MyGUI::IntCoord(0, mItemHeight, mScrollView->getSize().width - scrollBarWidth - 2, 24), MyGUI::IntCoord(0, mItemHeight, mScrollView->getSize().width - scrollBarWidth - 2, 24),
MyGUI::Align::Left | MyGUI::Align::Top, getName() + "_item_" + (*it)); MyGUI::Align::Left | MyGUI::Align::Top, getName() + "_item_" + item);
button->setCaption((*it)); button->setCaption(item);
button->getSubWidgetText()->setWordWrap(true); button->getSubWidgetText()->setWordWrap(true);
button->getSubWidgetText()->setTextAlign(MyGUI::Align::Left); button->getSubWidgetText()->setTextAlign(MyGUI::Align::Left);
button->eventMouseWheel += MyGUI::newDelegate(this, &MWList::onMouseWheelMoved); button->eventMouseWheel += MyGUI::newDelegate(this, &MWList::onMouseWheelMoved);
@ -115,12 +115,12 @@ namespace Gui
Base::setPropertyOverride(_key, _value); Base::setPropertyOverride(_key, _value);
} }
unsigned int MWList::getItemCount() size_t MWList::getItemCount()
{ {
return static_cast<unsigned int>(mItems.size()); return mItems.size();
} }
std::string MWList::getItemNameAt(unsigned int at) const std::string& MWList::getItemNameAt(size_t at)
{ {
assert(at < mItems.size() && "List item out of bounds"); assert(at < mItems.size() && "List item out of bounds");
return mItems[at]; return mItems[at];
@ -134,8 +134,9 @@ namespace Gui
void MWList::removeItem(const std::string& name) void MWList::removeItem(const std::string& name)
{ {
assert(std::find(mItems.begin(), mItems.end(), name) != mItems.end()); auto it = std::find(mItems.begin(), mItems.end(), name);
mItems.erase(std::find(mItems.begin(), mItems.end(), name)); assert(it != mItems.end());
mItems.erase(it);
} }
void MWList::clear() void MWList::clear()

@ -39,9 +39,9 @@ namespace Gui
void addItem(std::string_view name); void addItem(std::string_view name);
void addSeparator(); ///< add a seperator between the current and the next item. void addSeparator(); ///< add a seperator between the current and the next item.
void removeItem(const std::string& name); void removeItem(const std::string& name);
unsigned int getItemCount(); size_t getItemCount();
std::string getItemNameAt(unsigned int at); ///< \attention if there are separators, this method will return "" const std::string& getItemNameAt(size_t at); ///< \attention if there are separators, this method will return ""
///< at the place where the separator is ///< at the place where the separator is
void clear(); void clear();
MyGUI::Button* getItemWidget(std::string_view name); MyGUI::Button* getItemWidget(std::string_view name);

@ -7,17 +7,16 @@
namespace Gui namespace Gui
{ {
bool replaceTag(const MyGUI::UString& tag, MyGUI::UString& out) bool replaceTag(std::string_view tag, MyGUI::UString& out)
{ {
std::string fontcolour = "fontcolour="; std::string_view fontcolour = "fontcolour=";
size_t fontcolourLength = fontcolour.length();
std::string fontcolourhtml = "fontcolourhtml="; std::string_view fontcolourhtml = "fontcolourhtml=";
size_t fontcolourhtmlLength = fontcolourhtml.length();
if (tag.compare(0, fontcolourLength, fontcolour) == 0) if (tag.starts_with(fontcolour))
{ {
std::string fallbackName = "FontColor_color_" + tag.substr(fontcolourLength); std::string fallbackName = "FontColor_color_";
fallbackName += tag.substr(fontcolour.length());
std::string_view str = Fallback::Map::getString(fallbackName); std::string_view str = Fallback::Map::getString(fallbackName);
if (str.empty()) if (str.empty())
throw std::runtime_error("Unknown fallback name: " + fallbackName); throw std::runtime_error("Unknown fallback name: " + fallbackName);
@ -36,9 +35,10 @@ namespace Gui
out = col.print(); out = col.print();
return true; return true;
} }
else if (tag.compare(0, fontcolourhtmlLength, fontcolourhtml) == 0) else if (tag.starts_with(fontcolourhtml))
{ {
std::string fallbackName = "FontColor_color_" + tag.substr(fontcolourhtmlLength); std::string fallbackName = "FontColor_color_";
fallbackName += tag.substr(fontcolourhtml.length());
std::string_view str = Fallback::Map::getString(fallbackName); std::string_view str = Fallback::Map::getString(fallbackName);
if (str.empty()) if (str.empty())
throw std::runtime_error("Unknown fallback name: " + fallbackName); throw std::runtime_error("Unknown fallback name: " + fallbackName);

@ -2,14 +2,13 @@
#define OPENMW_WIDGETS_TAGS_H #define OPENMW_WIDGETS_TAGS_H
#include <MyGUI_UString.h> #include <MyGUI_UString.h>
#include <map> #include <string_view>
#include <string>
namespace Gui namespace Gui
{ {
/// Try to replace a tag. Returns true on success and writes the result to \a out. /// Try to replace a tag. Returns true on success and writes the result to \a out.
bool replaceTag(const MyGUI::UString& tag, MyGUI::UString& out); bool replaceTag(std::string_view tag, MyGUI::UString& out);
} }

Loading…
Cancel
Save