Simplify Tabs::m_list iterations

This commit is contained in:
David Capello 2015-02-15 11:45:23 -03:00
parent 5a5c0424e7
commit c473c99525
2 changed files with 27 additions and 46 deletions

View File

@ -112,10 +112,9 @@ Tabs::~Tabs()
stopAni(); stopAni();
// Remove all tabs // Remove all tabs
TabsListIterator it, end = m_list_of_tabs.end(); for (Tab* tab : m_list)
for (it = m_list_of_tabs.begin(); it != end; ++it) delete tab;
delete *it; // tab m_list.clear();
m_list_of_tabs.clear();
delete m_button_left; // widget delete m_button_left; // widget
delete m_button_right; // widget delete m_button_right; // widget
@ -126,7 +125,7 @@ void Tabs::addTab(TabView* tabView)
Tab* tab = new Tab(tabView); Tab* tab = new Tab(tabView);
calcTabWidth(tab); calcTabWidth(tab);
m_list_of_tabs.push_back(tab); m_list.push_back(tab);
// Update scroll (in the same position if we can // Update scroll (in the same position if we can
setScrollX(m_scrollX); setScrollX(m_scrollX);
@ -150,11 +149,11 @@ void Tabs::removeTab(TabView* tabView)
} }
TabsListIterator it = TabsListIterator it =
std::find(m_list_of_tabs.begin(), m_list_of_tabs.end(), tab); std::find(m_list.begin(), m_list.end(), tab);
ASSERT(it != m_list_of_tabs.end() && "Removing a tab that is not part of the Tabs widget"); ASSERT(it != m_list.end() && "Removing a tab that is not part of the Tabs widget");
it = m_list_of_tabs.erase(it); it = m_list.erase(it);
// Width of the removed tab // Width of the removed tab
if (m_removedTab) { if (m_removedTab) {
@ -164,7 +163,7 @@ void Tabs::removeTab(TabView* tabView)
m_removedTab = tab; m_removedTab = tab;
// Next tab in the list // Next tab in the list
if (it != m_list_of_tabs.end()) if (it != m_list.end())
m_nextTabOfTheRemovedOne = *it; m_nextTabOfTheRemovedOne = *it;
else else
m_nextTabOfTheRemovedOne = NULL; m_nextTabOfTheRemovedOne = NULL;
@ -177,11 +176,7 @@ void Tabs::removeTab(TabView* tabView)
void Tabs::updateTabsText() void Tabs::updateTabsText()
{ {
TabsListIterator it, end = m_list_of_tabs.end(); for (Tab* tab : m_list) {
for (it = m_list_of_tabs.begin(); it != end; ++it) {
Tab* tab = *it;
// Change text of the tab // Change text of the tab
calcTabWidth(tab); calcTabWidth(tab);
} }
@ -201,10 +196,10 @@ void Tabs::selectNextTab()
{ {
TabsListIterator currentTabIt = getTabIteratorByView(m_selected->view); TabsListIterator currentTabIt = getTabIteratorByView(m_selected->view);
TabsListIterator it = currentTabIt; TabsListIterator it = currentTabIt;
if (it != m_list_of_tabs.end()) { if (it != m_list.end()) {
// If we are at the end of the list, cycle to the first tab. // If we are at the end of the list, cycle to the first tab.
if (it == --m_list_of_tabs.end()) if (it == --m_list.end())
it = m_list_of_tabs.begin(); it = m_list.begin();
// Go to next tab. // Go to next tab.
else else
++it; ++it;
@ -221,10 +216,10 @@ void Tabs::selectPreviousTab()
{ {
TabsListIterator currentTabIt = getTabIteratorByView(m_selected->view); TabsListIterator currentTabIt = getTabIteratorByView(m_selected->view);
TabsListIterator it = currentTabIt; TabsListIterator it = currentTabIt;
if (it != m_list_of_tabs.end()) { if (it != m_list.end()) {
// If we are at the beginning of the list, cycle to the last tab. // If we are at the beginning of the list, cycle to the last tab.
if (it == m_list_of_tabs.begin()) if (it == m_list.begin())
it = --m_list_of_tabs.end(); it = --m_list.end();
// Go to previous tab. // Go to previous tab.
else else
--it; --it;
@ -357,7 +352,7 @@ void Tabs::onPaint(PaintEvent& ev)
gfx::Rect rect = getClientBounds(); gfx::Rect rect = getClientBounds();
gfx::Rect box(rect.x-m_scrollX, rect.y, gfx::Rect box(rect.x-m_scrollX, rect.y,
2*guiscale(), 2*guiscale(),
m_list_of_tabs.empty() ? 0: theme->get_part(PART_TAB_FILLER)->height()); m_list.empty() ? 0: theme->get_part(PART_TAB_FILLER)->height());
g->fillRect(theme->getColorById(kWindowFaceColorId), g->getClipBounds()); g->fillRect(theme->getColorById(kWindowFaceColorId), g->getClipBounds());
@ -367,11 +362,7 @@ void Tabs::onPaint(PaintEvent& ev)
box.x = box.x2(); box.x = box.x2();
// For each tab... // For each tab...
TabsListIterator it, end = m_list_of_tabs.end(); for (Tab* tab : m_list) {
for (it = m_list_of_tabs.begin(); it != end; ++it) {
Tab* tab = *it;
box.w = tab->width; box.w = tab->width;
int x_delta = 0; int x_delta = 0;
@ -432,7 +423,7 @@ void Tabs::onPreferredSize(PreferredSizeEvent& ev)
SkinTheme* theme = static_cast<SkinTheme*>(this->getTheme()); SkinTheme* theme = static_cast<SkinTheme*>(this->getTheme());
gfx::Size reqsize(0, theme->get_part(PART_TAB_BOTTOM_NORMAL)->height()); gfx::Size reqsize(0, theme->get_part(PART_TAB_BOTTOM_NORMAL)->height());
if (!m_list_of_tabs.empty()) { if (!m_list.empty()) {
reqsize.h += theme->get_part(PART_TAB_FILLER)->height(); reqsize.h += theme->get_part(PART_TAB_FILLER)->height();
} }
@ -451,12 +442,8 @@ void Tabs::onInitTheme(InitThemeEvent& ev)
void Tabs::onSetText() void Tabs::onSetText()
{ {
TabsListIterator it, end = m_list_of_tabs.end(); for (Tab* tab : m_list)
for (it = m_list_of_tabs.begin(); it != end; ++it) {
Tab* tab = *it;
calcTabWidth(tab); calcTabWidth(tab);
}
} }
void Tabs::selectTabInternal(Tab* tab) void Tabs::selectTabInternal(Tab* tab)
@ -522,9 +509,9 @@ void Tabs::drawTab(Graphics* g, const gfx::Rect& box, Tab* tab, int y_delta, boo
Tabs::TabsListIterator Tabs::getTabIteratorByView(TabView* tabView) Tabs::TabsListIterator Tabs::getTabIteratorByView(TabView* tabView)
{ {
TabsListIterator it, end = m_list_of_tabs.end(); TabsListIterator it, end = m_list.end();
for (it = m_list_of_tabs.begin(); it != end; ++it) { for (it = m_list.begin(); it != end; ++it) {
if ((*it)->view == tabView) if ((*it)->view == tabView)
break; break;
} }
@ -535,7 +522,7 @@ Tabs::TabsListIterator Tabs::getTabIteratorByView(TabView* tabView)
Tabs::Tab* Tabs::getTabByView(TabView* tabView) Tabs::Tab* Tabs::getTabByView(TabView* tabView)
{ {
TabsListIterator it = getTabIteratorByView(tabView); TabsListIterator it = getTabIteratorByView(tabView);
if (it != m_list_of_tabs.end()) if (it != m_list.end())
return *it; return *it;
else else
return NULL; return NULL;
@ -543,10 +530,10 @@ Tabs::Tab* Tabs::getTabByView(TabView* tabView)
int Tabs::getMaxScrollX() int Tabs::getMaxScrollX()
{ {
TabsListIterator it, end = m_list_of_tabs.end(); TabsListIterator it, end = m_list.end();
int x = 0; int x = 0;
for (it = m_list_of_tabs.begin(); it != end; ++it) { for (it = m_list.begin(); it != end; ++it) {
Tab* tab = *it; Tab* tab = *it;
x += tab->width; x += tab->width;
} }
@ -563,11 +550,8 @@ void Tabs::makeTabVisible(Tab* make_visible_this_tab)
{ {
int x = 0; int x = 0;
int extra_x = getMaxScrollX() > 0 ? ARROW_W*2: 0; int extra_x = getMaxScrollX() > 0 ? ARROW_W*2: 0;
TabsListIterator it, end = m_list_of_tabs.end();
for (it = m_list_of_tabs.begin(); it != end; ++it) {
Tab* tab = *it;
for (Tab* tab : m_list) {
if (tab == make_visible_this_tab) { if (tab == make_visible_this_tab) {
if (x - m_scrollX < 0) { if (x - m_scrollX < 0) {
setScrollX(x); setScrollX(x);
@ -627,12 +611,9 @@ void Tabs::calculateHot()
gfx::Rect rect = getBounds(); gfx::Rect rect = getBounds();
gfx::Rect box(rect.x-m_scrollX, rect.y, 0, rect.h-1); gfx::Rect box(rect.x-m_scrollX, rect.y, 0, rect.h-1);
Tab *hot = NULL; Tab *hot = NULL;
TabsListIterator it, end = m_list_of_tabs.end();
// For each tab // For each tab
for (it = m_list_of_tabs.begin(); it != end; ++it) { for (Tab* tab : m_list) {
Tab* tab = *it;
box.w = tab->width; box.w = tab->width;
if (box.contains(ui::get_mouse_position())) { if (box.contains(ui::get_mouse_position())) {

View File

@ -103,7 +103,7 @@ namespace app {
void calculateHot(); void calculateHot();
void calcTabWidth(Tab* tab); void calcTabWidth(Tab* tab);
TabsList m_list_of_tabs; TabsList m_list;
Tab* m_hot; Tab* m_hot;
Tab* m_selected; Tab* m_selected;
int m_scrollX; int m_scrollX;