mirror of
https://github.com/strawberrymusicplayer/strawberry
synced 2024-12-19 12:03:48 +01:00
Simplify enabling/disabling tabs code
This commit is contained in:
parent
96424be0da
commit
740ead4059
@ -287,12 +287,11 @@ class FancyTabBar: public QTabBar {
|
|||||||
class TabData : public QObject {
|
class TabData : public QObject {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
TabData(QWidget *widget_view, const QString name, const QIcon icon, const QString label, const bool enabled, const int idx, QWidget *parent) :
|
TabData(QWidget *widget_view, const QString name, const QIcon icon, const QString label, const int idx, QWidget *parent) :
|
||||||
QObject(parent),
|
QObject(parent),
|
||||||
widget_view_(widget_view),
|
widget_view_(widget_view),
|
||||||
name_(name), icon_(icon),
|
name_(name), icon_(icon),
|
||||||
label_(label),
|
label_(label),
|
||||||
enabled_(enabled),
|
|
||||||
index_(idx),
|
index_(idx),
|
||||||
page_(new QWidget()) {
|
page_(new QWidget()) {
|
||||||
// In order to achieve the same effect as the "Bottom Widget" of the old Nokia based FancyTabWidget a VBoxLayout is used on each page
|
// In order to achieve the same effect as the "Bottom Widget" of the old Nokia based FancyTabWidget a VBoxLayout is used on each page
|
||||||
@ -311,17 +310,13 @@ class TabData : public QObject {
|
|||||||
QIcon icon() { return icon_; }
|
QIcon icon() { return icon_; }
|
||||||
QString label() { return label_; }
|
QString label() { return label_; }
|
||||||
QWidget *page() { return page_; }
|
QWidget *page() { return page_; }
|
||||||
bool enabled() { return enabled_; }
|
|
||||||
int index() { return index_; }
|
int index() { return index_; }
|
||||||
|
|
||||||
void set_enabled(bool enabled) { enabled_ = enabled; }
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QWidget *widget_view_;
|
QWidget *widget_view_;
|
||||||
QString name_;
|
QString name_;
|
||||||
QIcon icon_;
|
QIcon icon_;
|
||||||
QString label_;
|
QString label_;
|
||||||
bool enabled_;
|
|
||||||
int index_;
|
int index_;
|
||||||
QWidget *page_;
|
QWidget *page_;
|
||||||
|
|
||||||
@ -333,8 +328,8 @@ FancyTabWidget::~FancyTabWidget() {}
|
|||||||
void FancyTabWidget::addSpacer() {
|
void FancyTabWidget::addSpacer() {
|
||||||
|
|
||||||
QWidget *spacer = new QWidget(this);
|
QWidget *spacer = new QWidget(this);
|
||||||
const int index = insertTab(count(), spacer, QIcon(), QString());
|
const int idx = insertTab(count(), spacer, QIcon(), QString());
|
||||||
setTabEnabled(index, false);
|
setTabEnabled(idx, false);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -345,23 +340,25 @@ void FancyTabWidget::setBackgroundPixmap(const QPixmap& pixmap) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void FancyTabWidget::setCurrentIndex(int index) {
|
void FancyTabWidget::setCurrentIndex(int idx) {
|
||||||
|
|
||||||
if (index >= count()) index = 0;
|
Q_ASSERT(count() > 0);
|
||||||
|
|
||||||
QWidget *currentPage = widget(index);
|
if (idx >= count() || idx < 0) idx = 0;
|
||||||
|
|
||||||
|
QWidget *currentPage = widget(idx);
|
||||||
QLayout *layout = currentPage->layout();
|
QLayout *layout = currentPage->layout();
|
||||||
if (bottom_widget_) layout->addWidget(bottom_widget_);
|
if (bottom_widget_) layout->addWidget(bottom_widget_);
|
||||||
QTabWidget::setCurrentIndex(index);
|
QTabWidget::setCurrentIndex(idx);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void FancyTabWidget::currentTabChanged(int index) {
|
void FancyTabWidget::currentTabChanged(int idx) {
|
||||||
|
|
||||||
QWidget *currentPage = currentWidget();
|
QWidget *currentPage = currentWidget();
|
||||||
QLayout *layout = currentPage->layout();
|
QLayout *layout = currentPage->layout();
|
||||||
if (bottom_widget_) layout->addWidget(bottom_widget_);
|
if (bottom_widget_) layout->addWidget(bottom_widget_);
|
||||||
emit CurrentChanged(index);
|
emit CurrentChanged(idx);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -386,7 +383,8 @@ void FancyTabWidget::Load(const QString &kSettingsGroup) {
|
|||||||
s.beginGroup(kSettingsGroup);
|
s.beginGroup(kSettingsGroup);
|
||||||
QMultiMap <int, TabData*> tabs;
|
QMultiMap <int, TabData*> tabs;
|
||||||
for (TabData *tab : tabs_) {
|
for (TabData *tab : tabs_) {
|
||||||
const int idx = s.value("tab_" + tab->name(), tab->index()).toInt();
|
int idx = s.value("tab_" + tab->name(), tab->index()).toInt();
|
||||||
|
while (tabs.contains(idx)) { ++idx; }
|
||||||
tabs.insert(idx, tab);
|
tabs.insert(idx, tab);
|
||||||
}
|
}
|
||||||
s.endGroup();
|
s.endGroup();
|
||||||
@ -394,15 +392,14 @@ void FancyTabWidget::Load(const QString &kSettingsGroup) {
|
|||||||
QMultiMap <int, TabData*> ::iterator i;
|
QMultiMap <int, TabData*> ::iterator i;
|
||||||
for (i = tabs.begin() ; i != tabs.end() ; ++i) {
|
for (i = tabs.begin() ; i != tabs.end() ; ++i) {
|
||||||
TabData *tab = i.value();
|
TabData *tab = i.value();
|
||||||
const int actualIndex = insertTab(i.key(), tab->page(), tab->icon(), tab->label());
|
const int idx = insertTab(i.key(), tab->page(), tab->icon(), tab->label());
|
||||||
tabBar()->setTabData(actualIndex, QVariant(tab->name()));
|
tabBar()->setTabData(idx, QVariant(tab->name()));
|
||||||
tab->set_enabled(true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int FancyTabWidget::insertTab(int index, QWidget *page, const QIcon &icon, const QString &label) {
|
int FancyTabWidget::insertTab(int idx, QWidget *page, const QIcon &icon, const QString &label) {
|
||||||
return QTabWidget::insertTab(index, page, icon, label);
|
return QTabWidget::insertTab(idx, page, icon, label);
|
||||||
}
|
}
|
||||||
|
|
||||||
void FancyTabWidget::SaveSettings(const QString &kSettingsGroup) {
|
void FancyTabWidget::SaveSettings(const QString &kSettingsGroup) {
|
||||||
@ -413,9 +410,15 @@ void FancyTabWidget::SaveSettings(const QString &kSettingsGroup) {
|
|||||||
s.setValue("tab_mode", mode_);
|
s.setValue("tab_mode", mode_);
|
||||||
s.setValue("current_tab", currentIndex());
|
s.setValue("current_tab", currentIndex());
|
||||||
|
|
||||||
for (int i = 0 ; i < count() ; i++) {
|
for (TabData *tab : tabs_) {
|
||||||
QString k = "tab_" + tabBar()->tabData(i).toString().toLower();
|
QString k = "tab_" + tab->name();
|
||||||
s.setValue(k, i);
|
int idx = QTabWidget::indexOf(tab->page());
|
||||||
|
if (idx < 0) {
|
||||||
|
if (s.contains(k)) s.remove(k);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
s.setValue(k, idx);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
s.endGroup();
|
s.endGroup();
|
||||||
@ -428,7 +431,7 @@ void FancyTabWidget::addBottomWidget(QWidget *widget_view) {
|
|||||||
|
|
||||||
void FancyTabWidget::AddTab(QWidget *widget_view, const QString &name, const QIcon &icon, const QString &label) {
|
void FancyTabWidget::AddTab(QWidget *widget_view, const QString &name, const QIcon &icon, const QString &label) {
|
||||||
|
|
||||||
TabData *tab = new TabData(widget_view, name, icon, label, false, tabs_.count(), this);
|
TabData *tab = new TabData(widget_view, name, icon, label, tabs_.count(), this);
|
||||||
tabs_.insert(widget_view, tab);
|
tabs_.insert(widget_view, tab);
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -437,10 +440,10 @@ bool FancyTabWidget::EnableTab(QWidget *widget_view) {
|
|||||||
|
|
||||||
if (!tabs_.contains(widget_view)) return false;
|
if (!tabs_.contains(widget_view)) return false;
|
||||||
TabData *tab = tabs_.value(widget_view);
|
TabData *tab = tabs_.value(widget_view);
|
||||||
if (tab->enabled()) return true;
|
|
||||||
const int actualIndex = QTabWidget::insertTab(count(), tab->page(), tab->icon(), tab->label());
|
if (QTabWidget::indexOf(tab->page()) >= 0) return true;
|
||||||
tabBar()->setTabData(actualIndex, QVariant(tab->name()));
|
const int idx = QTabWidget::insertTab(count(), tab->page(), tab->icon(), tab->label());
|
||||||
tab->set_enabled(true);
|
tabBar()->setTabData(idx, QVariant(tab->name()));
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
@ -451,14 +454,12 @@ bool FancyTabWidget::DisableTab(QWidget *widget_view) {
|
|||||||
if (!tabs_.contains(widget_view)) return false;
|
if (!tabs_.contains(widget_view)) return false;
|
||||||
TabData *tab = tabs_.value(widget_view);
|
TabData *tab = tabs_.value(widget_view);
|
||||||
|
|
||||||
for (int i = 0 ; i < count() ; i++) {
|
int idx = QTabWidget::indexOf(tab->page());
|
||||||
if (tabBar()->tabData(i).toString() == tab->name()) {
|
if (idx < 0) return false;
|
||||||
removeTab(i);
|
|
||||||
tab->set_enabled(false);
|
removeTab(idx);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user