Commit 8d3e6b14 authored by hjk's avatar hjk
Browse files

ProjectExplorer: More KitOptionsPage simplification



Change-Id: I07244e510e44a3ee2af5e771450ab6413a5a5c2d
Reviewed-by: default avatarChristian Stenger <christian.stenger@theqtcompany.com>
Reviewed-by: default avatarTobias Hunger <tobias.hunger@theqtcompany.com>
parent 41d1c7e0
......@@ -124,22 +124,28 @@ KitModel::KitModel(QBoxLayout *parentLayout, QObject *parent) :
changeDefaultKit();
connect(KitManager::instance(), SIGNAL(kitAdded(ProjectExplorer::Kit*)),
this, SLOT(addKit(ProjectExplorer::Kit*)));
connect(KitManager::instance(), SIGNAL(kitUpdated(ProjectExplorer::Kit*)),
this, SLOT(updateKit(ProjectExplorer::Kit*)));
connect(KitManager::instance(), SIGNAL(unmanagedKitUpdated(ProjectExplorer::Kit*)),
this, SLOT(updateKit(ProjectExplorer::Kit*)));
connect(KitManager::instance(), SIGNAL(kitRemoved(ProjectExplorer::Kit*)),
this, SLOT(removeKit(ProjectExplorer::Kit*)));
connect(KitManager::instance(), SIGNAL(defaultkitChanged()),
this, SLOT(changeDefaultKit()));
connect(KitManager::instance(), &KitManager::kitAdded,
this, &KitModel::addKit);
connect(KitManager::instance(), &KitManager::kitUpdated,
this, &KitModel::updateKit);
connect(KitManager::instance(), &KitManager::unmanagedKitUpdated,
this, &KitModel::updateKit);
connect(KitManager::instance(), &KitManager::kitRemoved,
this, &KitModel::removeKit);
connect(KitManager::instance(), &KitManager::defaultkitChanged,
this, &KitModel::changeDefaultKit);
}
Kit *KitModel::kit(const QModelIndex &index)
{
KitNode *n = kitNode(index);
return n ? n->widget->workingCopy() : 0;
}
KitNode *KitModel::kitNode(const QModelIndex &index)
{
TreeItem *n = itemFromIndex(index);
return n && n->level() == 2 ? static_cast<KitNode *>(n)->widget->workingCopy() : 0;
return n && n->level() == 2 ? static_cast<KitNode *>(n) : 0;
}
QModelIndex KitModel::indexOf(Kit *k) const
......@@ -150,83 +156,53 @@ QModelIndex KitModel::indexOf(Kit *k) const
void KitModel::setDefaultKit(const QModelIndex &index)
{
TreeItem *n = itemFromIndex(index);
if (n && n->level() == 2)
setDefaultNode(static_cast<KitNode *>(n));
if (KitNode *n = kitNode(index))
setDefaultNode(n);
}
bool KitModel::isDefaultKit(const QModelIndex &index)
bool KitModel::isDefaultKit(Kit *k) const
{
TreeItem *n = itemFromIndex(index);
return n && n->level() == 2 && n == m_defaultNode;
return m_defaultNode && m_defaultNode->widget->workingCopy() == k;
}
KitManagerConfigWidget *KitModel::widget(const QModelIndex &index)
{
if (!index.isValid())
return 0;
KitNode *node = static_cast<KitNode *>(index.internalPointer());
if (node == m_autoRoot || node == m_manualRoot)
return 0;
return node->widget;
}
bool KitModel::isDirty() const
{
return Utils::anyOf(m_manualRoot->children(), [](TreeItem *n) {
return static_cast<KitNode *>(n)->widget->isDirty();
});
}
bool KitModel::isDirty(Kit *k) const
{
KitNode *n = findWorkingCopy(k);
return n ? n->widget->isDirty() : false;
KitNode *n = kitNode(index);
return n ? n->widget : 0;
}
void KitModel::validateKitNames()
{
QHash<QString, int> nameHash;
foreach (TreeItem *group, rootItem()->children()) {
foreach (TreeItem *item, group->children()) {
KitNode *n = static_cast<KitNode *>(item);
const QString displayName = n->widget->displayName();
if (nameHash.contains(displayName))
++nameHash[displayName];
else
nameHash.insert(displayName, 1);
}
foreach (KitNode *n, treeLevelItems<KitNode *>(2)) {
const QString displayName = n->widget->displayName();
if (nameHash.contains(displayName))
++nameHash[displayName];
else
nameHash.insert(displayName, 1);
}
foreach (TreeItem *group, rootItem()->children()) {
foreach (TreeItem *item, group->children()) {
KitNode *n = static_cast<KitNode *>(item);
const QString displayName = n->widget->displayName();
n->widget->setHasUniqueName(nameHash.value(displayName) == 1);
}
foreach (KitNode *n, treeLevelItems<KitNode *>(2)) {
const QString displayName = n->widget->displayName();
n->widget->setHasUniqueName(nameHash.value(displayName) == 1);
}
}
void KitModel::apply()
{
// Remove unused kits:
QList<KitNode *> nodes = m_toRemoveList;
foreach (KitNode *n, nodes)
foreach (KitNode *n, m_toRemoveList)
n->widget->removeKit();
// Update kits:
foreach (TreeItem *group, rootItem()->children()) {
//QList<TreeItems *> nodes = rootItem()->children(); // These can be dirty due to being made default!
//nodes.append(m_manualRoot->children());
foreach (TreeItem *item, group->children()) {
KitNode *n = static_cast<KitNode *>(item);
Q_ASSERT(n->widget);
if (n->widget->isDirty()) {
n->widget->apply();
updateItem(item);
}
foreach (KitNode *n, treeLevelItems<KitNode *>(2)) {
if (n->widget->isDirty()) {
n->widget->apply();
updateItem(n);
}
}
layoutChanged(); // Force update.
}
void KitModel::markForRemoval(Kit *k)
......@@ -274,12 +250,9 @@ Kit *KitModel::markForAddition(Kit *baseKit)
KitNode *KitModel::findWorkingCopy(Kit *k) const
{
foreach (TreeItem *group, rootItem()->children()) {
foreach (TreeItem *item, group->children()) {
KitNode *n = static_cast<KitNode *>(item);
if (n->widget->workingCopy() == k)
return n;
}
foreach (KitNode *n, treeLevelItems<KitNode *>(2)) {
if (n->widget->workingCopy() == k)
return n;
}
return 0;
}
......@@ -340,13 +313,10 @@ void KitModel::removeKit(Kit *k)
}
KitNode *node = 0;
foreach (TreeItem *group, rootItem()->children()) {
foreach (TreeItem *item, group->children()) {
KitNode *n = static_cast<KitNode *>(item);
if (n->widget->configures(k)) {
node = n;
break;
}
foreach (KitNode *n, treeLevelItems<KitNode *>(2)) {
if (n->widget->configures(k)) {
node = n;
break;
}
}
......@@ -362,13 +332,10 @@ void KitModel::removeKit(Kit *k)
void KitModel::changeDefaultKit()
{
Kit *defaultKit = KitManager::defaultKit();
foreach (TreeItem *group, rootItem()->children()) {
foreach (TreeItem *item, group->children()) {
KitNode *n = static_cast<KitNode *>(item);
if (n->widget->configures(defaultKit)) {
setDefaultNode(n);
return;
}
foreach (KitNode *n, treeLevelItems<KitNode *>(2)) {
if (n->widget->configures(defaultKit)) {
setDefaultNode(n);
return;
}
}
}
......
......@@ -46,8 +46,8 @@ class KitFactory;
class KitManager;
namespace Internal {
class KitManagerConfigWidget;
class KitManagerConfigWidget;
class KitNode;
// --------------------------------------------------------------------------
......@@ -62,16 +62,14 @@ public:
explicit KitModel(QBoxLayout *parentLayout, QObject *parent = 0);
Kit *kit(const QModelIndex &);
KitNode *kitNode(const QModelIndex &);
QModelIndex indexOf(Kit *k) const;
void setDefaultKit(const QModelIndex &index);
bool isDefaultKit(const QModelIndex &index);
bool isDefaultKit(Kit *k) const;
ProjectExplorer::Internal::KitManagerConfigWidget *widget(const QModelIndex &);
bool isDirty() const;
bool isDirty(Kit *k) const;
void apply();
void markForRemoval(Kit *k);
......@@ -80,15 +78,13 @@ public:
signals:
void kitStateChanged();
private slots:
private:
void addKit(ProjectExplorer::Kit *k);
void updateKit(ProjectExplorer::Kit *k);
void removeKit(ProjectExplorer::Kit *k);
void changeDefaultKit();
void validateKitNames();
private:
QModelIndex index(KitNode *, int column = 0) const;
KitNode *findWorkingCopy(Kit *k) const;
KitNode *createNode(Kit *k);
void setDefaultNode(KitNode *node);
......
......@@ -93,7 +93,7 @@ QWidget *KitOptionsPage::widget()
verticalLayout->addLayout(horizontalLayout);
m_model = new Internal::KitModel(verticalLayout);
connect(m_model, SIGNAL(kitStateChanged()), this, SLOT(updateState()));
connect(m_model, &Internal::KitModel::kitStateChanged, this, &KitOptionsPage::updateState);
verticalLayout->setStretch(0, 1);
verticalLayout->setStretch(1, 0);
......@@ -102,20 +102,20 @@ QWidget *KitOptionsPage::widget()
m_kitsView->expandAll();
m_selectionModel = m_kitsView->selectionModel();
connect(m_selectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
this, SLOT(kitSelectionChanged()));
connect(KitManager::instance(), SIGNAL(kitAdded(ProjectExplorer::Kit*)),
this, SLOT(kitSelectionChanged()));
connect(KitManager::instance(), SIGNAL(kitRemoved(ProjectExplorer::Kit*)),
this, SLOT(kitSelectionChanged()));
connect(KitManager::instance(), SIGNAL(kitUpdated(ProjectExplorer::Kit*)),
this, SLOT(kitSelectionChanged()));
connect(m_selectionModel, &QItemSelectionModel::selectionChanged,
this, &KitOptionsPage::kitSelectionChanged);
connect(KitManager::instance(), &KitManager::kitAdded,
this, &KitOptionsPage::kitSelectionChanged);
connect(KitManager::instance(), &KitManager::kitRemoved,
this, &KitOptionsPage::kitSelectionChanged);
connect(KitManager::instance(), &KitManager::kitUpdated,
this, &KitOptionsPage::kitSelectionChanged);
// Set up add menu:
connect(m_addButton, SIGNAL(clicked()), this, SLOT(addNewKit()));
connect(m_cloneButton, SIGNAL(clicked()), this, SLOT(cloneKit()));
connect(m_delButton, SIGNAL(clicked()), this, SLOT(removeKit()));
connect(m_makeDefaultButton, SIGNAL(clicked()), this, SLOT(makeDefaultKit()));
connect(m_addButton, &QAbstractButton::clicked, this, &KitOptionsPage::addNewKit);
connect(m_cloneButton, &QAbstractButton::clicked, this, &KitOptionsPage::cloneKit);
connect(m_delButton, &QAbstractButton::clicked, this, &KitOptionsPage::removeKit);
connect(m_makeDefaultButton, &QAbstractButton::clicked, this, &KitOptionsPage::makeDefaultKit);
updateState();
......@@ -148,7 +148,7 @@ void KitOptionsPage::finish()
delete m_configWidget;
m_selectionModel = 0; // child of m_configWidget
m_kitsView = 0; // child of m_configWidget
m_currentWidget = 0; // deleted by the model
m_currentWidget = 0; // not owned
m_toShow = 0;
}
......@@ -160,7 +160,7 @@ void KitOptionsPage::showKit(Kit *k)
void KitOptionsPage::kitSelectionChanged()
{
QModelIndex current = currentIndex();
QWidget *newWidget = current.isValid() ? m_model->widget(current) : 0;
QWidget *newWidget = m_model->widget(current);
if (newWidget == m_currentWidget)
return;
......@@ -187,9 +187,14 @@ void KitOptionsPage::addNewKit()
| QItemSelectionModel::Rows);
}
Kit *KitOptionsPage::currentKit() const
{
return m_model->kit(currentIndex());
}
void KitOptionsPage::cloneKit()
{
Kit *current = m_model->kit(currentIndex());
Kit *current = currentKit();
if (!current)
return;
......@@ -204,10 +209,8 @@ void KitOptionsPage::cloneKit()
void KitOptionsPage::removeKit()
{
Kit *k = m_model->kit(currentIndex());
if (!k)
return;
m_model->markForRemoval(k);
if (Kit *k = currentKit())
m_model->markForRemoval(k);
}
void KitOptionsPage::makeDefaultKit()
......@@ -224,12 +227,11 @@ void KitOptionsPage::updateState()
bool canCopy = false;
bool canDelete = false;
bool canMakeDefault = false;
QModelIndex index = currentIndex();
Kit *k = m_model->kit(index);
if (k) {
if (Kit *k = currentKit()) {
canCopy = true;
canDelete = !k->isAutoDetected();
canMakeDefault = !m_model->isDefaultKit(index);
canMakeDefault = !m_model->isDefaultKit(k);
}
m_cloneButton->setEnabled(canCopy);
......
......@@ -66,7 +66,6 @@ public:
void finish();
void showKit(Kit *k);
private slots:
void kitSelectionChanged();
void addNewKit();
......@@ -77,6 +76,7 @@ private slots:
private:
QModelIndex currentIndex() const;
Kit *currentKit() const;
QTreeView *m_kitsView;
QPushButton *m_addButton;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment