Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYTreeStateHelper.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) <2012-2024> <EDF-DTG> <FRANCE>
3  * This file is part of Code_TYMPAN (R).
4  * Code_TYMPAN (R) is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * Code_TYMPAN (R) is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  * You should have received a copy of the GNU General Public License along
13  * with Code_TYMPAN (R). If not, see <https://www.gnu.org/licenses/>.
14  */
15 
21 #include "TYTreeStateHelper.h"
22 #include <QScrollBar>
23 #include <QVariant>
24 
25 void TYTreeStateHelper::capture(QTreeWidget* tree, TYTreeViewState& outState)
26 {
27  outState.expansion.clear();
28 
29  // 1) Sauvegarde expansions
30  for (int i = 0; i < tree->topLevelItemCount(); ++i)
31  {
32  saveExpansionRec(tree->topLevelItem(i), outState.expansion);
33  }
34 
35  // 2) Sauvegarde scroll
36  if (tree->verticalScrollBar())
37  outState.vScroll = tree->verticalScrollBar()->value();
38  else
39  outState.vScroll = 0;
40 
41  if (tree->horizontalScrollBar())
42  outState.hScroll = tree->horizontalScrollBar()->value();
43  else
44  outState.hScroll = 0;
45 
46  // >>> Points d’arrêt utiles :
47  // qDebug() << "[CAPTURE] vScroll=" << outState.vScroll << "hScroll=" << outState.hScroll;
48 }
49 
50 void TYTreeStateHelper::apply(QTreeWidget* tree, const TYTreeViewState& state)
51 {
52  // 1) Restaure expansions
53  for (int i = 0; i < tree->topLevelItemCount(); ++i)
54  {
55  restoreExpansionRec(tree->topLevelItem(i), state.expansion);
56  }
57 
58  // 2) Restaure scroll (après expansions pour que les barres soient dimensionnées)
59  if (tree->verticalScrollBar())
60  tree->verticalScrollBar()->setValue(state.vScroll);
61 
62  if (tree->horizontalScrollBar())
63  tree->horizontalScrollBar()->setValue(state.hScroll);
64 
65  // >>> Points d’arrêt utiles :
66  // qDebug() << "[APPLY] vScroll->" << state.vScroll << "hScroll->" << state.hScroll;
67 }
68 
69 void TYTreeStateHelper::saveExpansionRec(QTreeWidgetItem* item, QHash<QString, bool>& map)
70 {
71  const QString key = makeKey(item);
72  map.insert(key, item->isExpanded());
73 
74  // >>> Breakpoint utile :
75  // qDebug() << "[SAVE EXP]" << key << "expanded=" << item->isExpanded();
76 
77  const int n = item->childCount();
78  for (int i = 0; i < n; ++i)
79  {
80  saveExpansionRec(item->child(i), map);
81  }
82 }
83 
84 void TYTreeStateHelper::restoreExpansionRec(QTreeWidgetItem* item, const QHash<QString, bool>& map)
85 {
86  const QString key = makeKey(item);
87  const auto it = map.constFind(key);
88  if (it != map.constEnd())
89  {
90  item->setExpanded(it.value());
91  // >>> Breakpoint utile :
92  // qDebug() << "[RESTORE EXP] found" << key << "expanded=" << it.value();
93  }
94  else
95  {
96  // >>> En debug, voir pourquoi cette clé manque :
97  // qDebug() << "[RESTORE EXP] missing key for" << key;
98  }
99 
100  const int n = item->childCount();
101  for (int i = 0; i < n; ++i)
102  {
103  restoreExpansionRec(item->child(i), map);
104  }
105 }
106 
107 QString TYTreeStateHelper::makeKey(QTreeWidgetItem* item)
108 {
109  const QVariant v = item->data(0, Qt::UserRole);
110  if (v.isValid())
111  {
112  const QString id = v.toString();
113  if (!id.isEmpty())
114  return id; // Clé stable prioritaire
115  }
116 
117  // Fallback : chemin textuel
118  QStringList path;
119  for (QTreeWidgetItem* it = item; it; it = it->parent())
120  path.prepend(it->text(0));
121  return path.join(u"/");
122 }
int id
Helper pour la gestion des arbres Site et Projet (fichier header)
static void restoreExpansionRec(QTreeWidgetItem *item, const QHash< QString, bool > &map)
static void saveExpansionRec(QTreeWidgetItem *item, QHash< QString, bool > &map)
static QString makeKey(QTreeWidgetItem *item)
static void capture(QTreeWidget *tree, TYTreeViewState &outState)
Capture l'état complet (expansions + scroll) du QTreeWidget.
static void apply(QTreeWidget *tree, const TYTreeViewState &state)
Applique l'état complet (expansions + scroll) au QTreeWidget.
QHash< QString, bool > expansion