config-section.cpp 4.89 KB
Newer Older
1 2
/*
 * %kadu copyright begin%
Rafał Malinowski's avatar
Rafał Malinowski committed
3
 * Copyright 2010, 2010, 2011 Piotr Galiszewski (piotr.galiszewski@kadu.im)
4
 * Copyright 2009, 2012 Wojciech Treter (juzefwt@gmail.com)
Piotr Galiszewski's avatar
Piotr Galiszewski committed
5
 * Copyright 2009 Bartłomiej Zimoń (uzi18@o2.pl)
Rafał Malinowski's avatar
Rafał Malinowski committed
6
 * Copyright 2008, 2009, 2010, 2011 Rafał Malinowski (rafal.przemyslaw.malinowski@gmail.com)
7
 * Copyright 2010, 2011, 2013 Bartosz Brachaczek (b.brachaczek@gmail.com)
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * %kadu copyright end%
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
23

24 25 26 27
#include <QtWidgets/QApplication>
#include <QtWidgets/QGroupBox>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QListWidget>
28

29
#include "configuration/deprecated-configuration-api.h"
30
#include "gui/widgets/configuration/config-group-box.h"
Rafał Malinowski's avatar
Rafał Malinowski committed
31
#include "gui/widgets/configuration/config-section.h"
32 33
#include "gui/widgets/configuration/config-tab.h"
#include "gui/widgets/configuration/config-widget.h"
34
#include "kadu-application.h"
35

Bartosz Brachaczek's avatar
Bartosz Brachaczek committed
36
ConfigSection::ConfigSection(const QString &name, ConfigurationWidget *configurationWidget,
37 38
		QListWidgetItem *listWidgetItem, QWidget *parentConfigGroupBoxWidget, const KaduIcon &icon) :
		QObject(configurationWidget), Name(name), MyConfigurationWidget(configurationWidget), Icon(icon),
Bartosz Brachaczek's avatar
Bartosz Brachaczek committed
39
		ListWidgetItem(listWidgetItem), Activated(false), ParentConfigGroupBoxWidget(parentConfigGroupBoxWidget)
40
{
41 42 43 44
	TabWidget = new KaduTabWidget(ParentConfigGroupBoxWidget);
	ParentConfigGroupBoxWidget->layout()->addWidget(TabWidget);
	TabWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
	TabWidget->hide();
45

46
	connect(IconsManager::instance(), SIGNAL(themeChanged()), this, SLOT(iconThemeChanged()));
47 48 49 50
}

ConfigSection::~ConfigSection()
{
51 52 53 54 55 56 57
	/* NOTE: It's needed to call ConfigurationWidget::configSectionDestroyed() before this
	 * ConfigSection will be destroyed. If we relied on QObject to send this signal,
	 * it'd be called after destroying all ConfigSection data but we need that data.
	 */
	blockSignals(false);
	emit destroyed(this);

58
	KaduApplication::instance()->deprecatedConfigurationApi()->writeEntry("General", "ConfigurationWindow_" + MyConfigurationWidget->name() + '_' + Name,
59
			TabWidget->tabText(TabWidget->currentIndex()));
60 61 62 63 64 65 66 67 68 69 70 71

	// delete them here, since they manually delete child widgets of our TabWidget
	// qDeleteAll() won't work here because of connection to destroyed() signal
	foreach (const ConfigTab *ct, ConfigTabs)
	{
		disconnect(ct, SIGNAL(destroyed(QObject *)), this, SLOT(configTabDestroyed(QObject *)));
		delete ct;
	}

	delete ListWidgetItem;
	ListWidgetItem = 0;

72
	delete TabWidget;
73
	TabWidget = 0;
74 75
}

76 77 78 79 80 81 82 83 84 85 86
ConfigGroupBox * ConfigSection::configGroupBox(const QString &tab, const QString &groupBox, bool create)
{
	ConfigTab *ct = configTab(tab, create);
	if (!ct)
		return 0;

	return ct->configGroupBox(groupBox, create);
}

void ConfigSection::activate()
{
Bartosz Brachaczek's avatar
Bartosz Brachaczek committed
87
	ListWidgetItem->listWidget()->setCurrentItem(ListWidgetItem);
88

Bartosz Brachaczek's avatar
Bartosz Brachaczek committed
89
	if (Activated)
90 91
		return;

92
	QString tab = KaduApplication::instance()->deprecatedConfigurationApi()->readEntry("General", "ConfigurationWindow_" + MyConfigurationWidget->name() + '_' + Name);
93
	if (ConfigTabs.contains(tab))
Piotr Galiszewski's avatar
Piotr Galiszewski committed
94
		TabWidget->setCurrentWidget(ConfigTabs.value(tab)->widget());
Bartosz Brachaczek's avatar
Bartosz Brachaczek committed
95
	Activated = true;
96 97
}

98
ConfigTab * ConfigSection::configTab(const QString &name, bool create)
99
{
Bartosz Brachaczek's avatar
Bartosz Brachaczek committed
100
	if (ConfigTabs.contains(name))
Piotr Galiszewski's avatar
Piotr Galiszewski committed
101
		return ConfigTabs.value(name);
102 103 104 105

	if (!create)
		return 0;

106
	ConfigTab *newConfigTab = new ConfigTab(name, this, TabWidget);
Piotr Galiszewski's avatar
Piotr Galiszewski committed
107
	ConfigTabs.insert(name, newConfigTab);
108
	connect(newConfigTab, SIGNAL(destroyed(QObject *)), this, SLOT(configTabDestroyed(QObject *)));
109

110 111 112
	TabWidget->addTab(newConfigTab->widget(), newConfigTab->name());

	TabWidget->setTabBarVisible(ConfigTabs.count() > 1);
113

114 115 116
	return newConfigTab;
}

117
void ConfigSection::configTabDestroyed(QObject *obj)
118
{
119
	// see ConfigTab::~ConfigTab()
120
	disconnect(obj, SIGNAL(destroyed(QObject *)), this, SLOT(configTabDestroyed(QObject *)));
121

122 123 124 125 126
	QMap<QString, ConfigTab *>::iterator i = ConfigTabs.find(static_cast<ConfigTab *>(obj)->name());

	if (TabWidget)
		TabWidget->removeTab(TabWidget->indexOf((*i)->widget()));
	ConfigTabs.erase(i);
127 128
	if (TabWidget)
		TabWidget->setTabBarVisible(ConfigTabs.count() > 1);
129

130
	if (ConfigTabs.isEmpty())
131
		deleteLater();
132 133 134 135
}

void ConfigSection::iconThemeChanged()
{
136
	ListWidgetItem->setIcon(Icon.icon());
Rafał Malinowski's avatar
Rafał Malinowski committed
137
}
138 139 140 141 142

void ConfigSection::addFullPageWidget(const QString &name, QWidget *widget)
{
	TabWidget->addTab(widget, name);
}
Bartosz Brachaczek's avatar
Bartosz Brachaczek committed
143 144

#include "moc_config-section.cpp"